repo
stringlengths
5
54
path
stringlengths
4
155
func_name
stringlengths
1
118
original_string
stringlengths
52
85.5k
language
stringclasses
1 value
code
stringlengths
52
85.5k
code_tokens
list
docstring
stringlengths
6
2.61k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
85
252
partition
stringclasses
1 value
lightninglabs/neutrino
headerfs/store.go
FetchHeader
func (f *FilterHeaderStore) FetchHeader(hash *chainhash.Hash) (*chainhash.Hash, error) { // Lock store for read. f.mtx.RLock() defer f.mtx.RUnlock() height, err := f.heightFromHash(hash) if err != nil { return nil, err } return f.readHeader(height) }
go
func (f *FilterHeaderStore) FetchHeader(hash *chainhash.Hash) (*chainhash.Hash, error) { // Lock store for read. f.mtx.RLock() defer f.mtx.RUnlock() height, err := f.heightFromHash(hash) if err != nil { return nil, err } return f.readHeader(height) }
[ "func", "(", "f", "*", "FilterHeaderStore", ")", "FetchHeader", "(", "hash", "*", "chainhash", ".", "Hash", ")", "(", "*", "chainhash", ".", "Hash", ",", "error", ")", "{", "// Lock store for read.", "f", ".", "mtx", ".", "RLock", "(", ")", "\n", "defer", "f", ".", "mtx", ".", "RUnlock", "(", ")", "\n\n", "height", ",", "err", ":=", "f", ".", "heightFromHash", "(", "hash", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "return", "f", ".", "readHeader", "(", "height", ")", "\n", "}" ]
// FetchHeader returns the filter header that corresponds to the passed block // height.
[ "FetchHeader", "returns", "the", "filter", "header", "that", "corresponds", "to", "the", "passed", "block", "height", "." ]
a655679fe131a5d1b4417872cc834fc3862ac70e
https://github.com/lightninglabs/neutrino/blob/a655679fe131a5d1b4417872cc834fc3862ac70e/headerfs/store.go#L698-L709
train
lightninglabs/neutrino
headerfs/store.go
FetchHeaderByHeight
func (f *FilterHeaderStore) FetchHeaderByHeight(height uint32) (*chainhash.Hash, error) { // Lock store for read. f.mtx.RLock() defer f.mtx.RUnlock() return f.readHeader(height) }
go
func (f *FilterHeaderStore) FetchHeaderByHeight(height uint32) (*chainhash.Hash, error) { // Lock store for read. f.mtx.RLock() defer f.mtx.RUnlock() return f.readHeader(height) }
[ "func", "(", "f", "*", "FilterHeaderStore", ")", "FetchHeaderByHeight", "(", "height", "uint32", ")", "(", "*", "chainhash", ".", "Hash", ",", "error", ")", "{", "// Lock store for read.", "f", ".", "mtx", ".", "RLock", "(", ")", "\n", "defer", "f", ".", "mtx", ".", "RUnlock", "(", ")", "\n\n", "return", "f", ".", "readHeader", "(", "height", ")", "\n", "}" ]
// FetchHeaderByHeight returns the filter header for a particular block height.
[ "FetchHeaderByHeight", "returns", "the", "filter", "header", "for", "a", "particular", "block", "height", "." ]
a655679fe131a5d1b4417872cc834fc3862ac70e
https://github.com/lightninglabs/neutrino/blob/a655679fe131a5d1b4417872cc834fc3862ac70e/headerfs/store.go#L712-L718
train
lightninglabs/neutrino
headerfs/store.go
FetchHeaderAncestors
func (f *FilterHeaderStore) FetchHeaderAncestors(numHeaders uint32, stopHash *chainhash.Hash) ([]chainhash.Hash, uint32, error) { // First, we'll find the final header in the range, this will be the // ending height of our scan. endHeight, err := f.heightFromHash(stopHash) if err != nil { return nil, 0, err } startHeight := endHeight - numHeaders headers, err := f.readHeaderRange(startHeight, endHeight) if err != nil { return nil, 0, err } return headers, startHeight, nil }
go
func (f *FilterHeaderStore) FetchHeaderAncestors(numHeaders uint32, stopHash *chainhash.Hash) ([]chainhash.Hash, uint32, error) { // First, we'll find the final header in the range, this will be the // ending height of our scan. endHeight, err := f.heightFromHash(stopHash) if err != nil { return nil, 0, err } startHeight := endHeight - numHeaders headers, err := f.readHeaderRange(startHeight, endHeight) if err != nil { return nil, 0, err } return headers, startHeight, nil }
[ "func", "(", "f", "*", "FilterHeaderStore", ")", "FetchHeaderAncestors", "(", "numHeaders", "uint32", ",", "stopHash", "*", "chainhash", ".", "Hash", ")", "(", "[", "]", "chainhash", ".", "Hash", ",", "uint32", ",", "error", ")", "{", "// First, we'll find the final header in the range, this will be the", "// ending height of our scan.", "endHeight", ",", "err", ":=", "f", ".", "heightFromHash", "(", "stopHash", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "0", ",", "err", "\n", "}", "\n", "startHeight", ":=", "endHeight", "-", "numHeaders", "\n\n", "headers", ",", "err", ":=", "f", ".", "readHeaderRange", "(", "startHeight", ",", "endHeight", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "0", ",", "err", "\n", "}", "\n\n", "return", "headers", ",", "startHeight", ",", "nil", "\n", "}" ]
// FetchHeaderAncestors fetches the numHeaders filter headers that are the // ancestors of the target stop block hash. A total of numHeaders+1 headers will be // returned, as we'll walk back numHeaders distance to collect each header, // then return the final header specified by the stop hash. We'll also return // the starting height of the header range as well so callers can compute the // height of each header without knowing the height of the stop hash.
[ "FetchHeaderAncestors", "fetches", "the", "numHeaders", "filter", "headers", "that", "are", "the", "ancestors", "of", "the", "target", "stop", "block", "hash", ".", "A", "total", "of", "numHeaders", "+", "1", "headers", "will", "be", "returned", "as", "we", "ll", "walk", "back", "numHeaders", "distance", "to", "collect", "each", "header", "then", "return", "the", "final", "header", "specified", "by", "the", "stop", "hash", ".", "We", "ll", "also", "return", "the", "starting", "height", "of", "the", "header", "range", "as", "well", "so", "callers", "can", "compute", "the", "height", "of", "each", "header", "without", "knowing", "the", "height", "of", "the", "stop", "hash", "." ]
a655679fe131a5d1b4417872cc834fc3862ac70e
https://github.com/lightninglabs/neutrino/blob/a655679fe131a5d1b4417872cc834fc3862ac70e/headerfs/store.go#L726-L743
train
lightninglabs/neutrino
headerfs/store.go
toIndexEntry
func (f *FilterHeader) toIndexEntry() headerEntry { return headerEntry{ hash: f.HeaderHash, height: f.Height, } }
go
func (f *FilterHeader) toIndexEntry() headerEntry { return headerEntry{ hash: f.HeaderHash, height: f.Height, } }
[ "func", "(", "f", "*", "FilterHeader", ")", "toIndexEntry", "(", ")", "headerEntry", "{", "return", "headerEntry", "{", "hash", ":", "f", ".", "HeaderHash", ",", "height", ":", "f", ".", "Height", ",", "}", "\n", "}" ]
// toIndexEntry converts the filter header into a index entry to be stored // within the database.
[ "toIndexEntry", "converts", "the", "filter", "header", "into", "a", "index", "entry", "to", "be", "stored", "within", "the", "database", "." ]
a655679fe131a5d1b4417872cc834fc3862ac70e
https://github.com/lightninglabs/neutrino/blob/a655679fe131a5d1b4417872cc834fc3862ac70e/headerfs/store.go#L762-L767
train
lightninglabs/neutrino
headerfs/store.go
WriteHeaders
func (f *FilterHeaderStore) WriteHeaders(hdrs ...FilterHeader) error { // Lock store for write. f.mtx.Lock() defer f.mtx.Unlock() // If there are 0 headers to be written, return immediately. This // prevents the newTip assignment from panicking because of an index // of -1. if len(hdrs) == 0 { return nil } // First, we'll grab a buffer from the write buffer pool so we an // reduce our total number of allocations, and also write the headers // in a single swoop. headerBuf := headerBufPool.Get().(*bytes.Buffer) headerBuf.Reset() defer headerBufPool.Put(headerBuf) // Next, we'll write out all the passed headers in series into the // buffer we just extracted from the pool. for _, header := range hdrs { if _, err := headerBuf.Write(header.FilterHash[:]); err != nil { return err } } // With all the headers written to the buffer, we'll now write out the // entire batch in a single write call. if err := f.appendRaw(headerBuf.Bytes()); err != nil { return err } // As the block headers should already be written, we only need to // update the tip pointer for this particular header type. newTip := hdrs[len(hdrs)-1].toIndexEntry().hash return f.truncateIndex(&newTip, false) }
go
func (f *FilterHeaderStore) WriteHeaders(hdrs ...FilterHeader) error { // Lock store for write. f.mtx.Lock() defer f.mtx.Unlock() // If there are 0 headers to be written, return immediately. This // prevents the newTip assignment from panicking because of an index // of -1. if len(hdrs) == 0 { return nil } // First, we'll grab a buffer from the write buffer pool so we an // reduce our total number of allocations, and also write the headers // in a single swoop. headerBuf := headerBufPool.Get().(*bytes.Buffer) headerBuf.Reset() defer headerBufPool.Put(headerBuf) // Next, we'll write out all the passed headers in series into the // buffer we just extracted from the pool. for _, header := range hdrs { if _, err := headerBuf.Write(header.FilterHash[:]); err != nil { return err } } // With all the headers written to the buffer, we'll now write out the // entire batch in a single write call. if err := f.appendRaw(headerBuf.Bytes()); err != nil { return err } // As the block headers should already be written, we only need to // update the tip pointer for this particular header type. newTip := hdrs[len(hdrs)-1].toIndexEntry().hash return f.truncateIndex(&newTip, false) }
[ "func", "(", "f", "*", "FilterHeaderStore", ")", "WriteHeaders", "(", "hdrs", "...", "FilterHeader", ")", "error", "{", "// Lock store for write.", "f", ".", "mtx", ".", "Lock", "(", ")", "\n", "defer", "f", ".", "mtx", ".", "Unlock", "(", ")", "\n\n", "// If there are 0 headers to be written, return immediately. This", "// prevents the newTip assignment from panicking because of an index", "// of -1.", "if", "len", "(", "hdrs", ")", "==", "0", "{", "return", "nil", "\n", "}", "\n\n", "// First, we'll grab a buffer from the write buffer pool so we an", "// reduce our total number of allocations, and also write the headers", "// in a single swoop.", "headerBuf", ":=", "headerBufPool", ".", "Get", "(", ")", ".", "(", "*", "bytes", ".", "Buffer", ")", "\n", "headerBuf", ".", "Reset", "(", ")", "\n", "defer", "headerBufPool", ".", "Put", "(", "headerBuf", ")", "\n\n", "// Next, we'll write out all the passed headers in series into the", "// buffer we just extracted from the pool.", "for", "_", ",", "header", ":=", "range", "hdrs", "{", "if", "_", ",", "err", ":=", "headerBuf", ".", "Write", "(", "header", ".", "FilterHash", "[", ":", "]", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "}", "\n\n", "// With all the headers written to the buffer, we'll now write out the", "// entire batch in a single write call.", "if", "err", ":=", "f", ".", "appendRaw", "(", "headerBuf", ".", "Bytes", "(", ")", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n\n", "// As the block headers should already be written, we only need to", "// update the tip pointer for this particular header type.", "newTip", ":=", "hdrs", "[", "len", "(", "hdrs", ")", "-", "1", "]", ".", "toIndexEntry", "(", ")", ".", "hash", "\n", "return", "f", ".", "truncateIndex", "(", "&", "newTip", ",", "false", ")", "\n", "}" ]
// WriteHeaders writes a batch of filter headers to persistent storage. The // headers themselves are appended to the flat file, and then the index updated // to reflect the new entires.
[ "WriteHeaders", "writes", "a", "batch", "of", "filter", "headers", "to", "persistent", "storage", ".", "The", "headers", "themselves", "are", "appended", "to", "the", "flat", "file", "and", "then", "the", "index", "updated", "to", "reflect", "the", "new", "entires", "." ]
a655679fe131a5d1b4417872cc834fc3862ac70e
https://github.com/lightninglabs/neutrino/blob/a655679fe131a5d1b4417872cc834fc3862ac70e/headerfs/store.go#L772-L809
train
lightninglabs/neutrino
headerfs/store.go
ChainTip
func (f *FilterHeaderStore) ChainTip() (*chainhash.Hash, uint32, error) { // Lock store for read. f.mtx.RLock() defer f.mtx.RUnlock() _, tipHeight, err := f.chainTip() if err != nil { return nil, 0, fmt.Errorf("unable to fetch chain tip: %v", err) } latestHeader, err := f.readHeader(tipHeight) if err != nil { return nil, 0, fmt.Errorf("unable to read header: %v", err) } return latestHeader, tipHeight, nil }
go
func (f *FilterHeaderStore) ChainTip() (*chainhash.Hash, uint32, error) { // Lock store for read. f.mtx.RLock() defer f.mtx.RUnlock() _, tipHeight, err := f.chainTip() if err != nil { return nil, 0, fmt.Errorf("unable to fetch chain tip: %v", err) } latestHeader, err := f.readHeader(tipHeight) if err != nil { return nil, 0, fmt.Errorf("unable to read header: %v", err) } return latestHeader, tipHeight, nil }
[ "func", "(", "f", "*", "FilterHeaderStore", ")", "ChainTip", "(", ")", "(", "*", "chainhash", ".", "Hash", ",", "uint32", ",", "error", ")", "{", "// Lock store for read.", "f", ".", "mtx", ".", "RLock", "(", ")", "\n", "defer", "f", ".", "mtx", ".", "RUnlock", "(", ")", "\n\n", "_", ",", "tipHeight", ",", "err", ":=", "f", ".", "chainTip", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "0", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n\n", "latestHeader", ",", "err", ":=", "f", ".", "readHeader", "(", "tipHeight", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "0", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "err", ")", "\n", "}", "\n\n", "return", "latestHeader", ",", "tipHeight", ",", "nil", "\n", "}" ]
// ChainTip returns the latest filter header and height known to the // FilterHeaderStore.
[ "ChainTip", "returns", "the", "latest", "filter", "header", "and", "height", "known", "to", "the", "FilterHeaderStore", "." ]
a655679fe131a5d1b4417872cc834fc3862ac70e
https://github.com/lightninglabs/neutrino/blob/a655679fe131a5d1b4417872cc834fc3862ac70e/headerfs/store.go#L813-L829
train
lightninglabs/neutrino
headerfs/store.go
RollbackLastBlock
func (f *FilterHeaderStore) RollbackLastBlock(newTip *chainhash.Hash) (*waddrmgr.BlockStamp, error) { // Lock store for write. f.mtx.Lock() defer f.mtx.Unlock() // First, we'll obtain the latest height that the index knows of. _, chainTipHeight, err := f.chainTip() if err != nil { return nil, err } // With this height obtained, we'll use it to read what will be the new // chain tip from disk. newHeightTip := chainTipHeight - 1 newHeaderTip, err := f.readHeader(newHeightTip) if err != nil { return nil, err } // Now that we have the information we need to return from this // function, we can now truncate both the header file and the index. if err := f.singleTruncate(); err != nil { return nil, err } if err := f.truncateIndex(newTip, false); err != nil { return nil, err } // TODO(roasbeef): return chain hash also? return &waddrmgr.BlockStamp{ Height: int32(newHeightTip), Hash: *newHeaderTip, }, nil }
go
func (f *FilterHeaderStore) RollbackLastBlock(newTip *chainhash.Hash) (*waddrmgr.BlockStamp, error) { // Lock store for write. f.mtx.Lock() defer f.mtx.Unlock() // First, we'll obtain the latest height that the index knows of. _, chainTipHeight, err := f.chainTip() if err != nil { return nil, err } // With this height obtained, we'll use it to read what will be the new // chain tip from disk. newHeightTip := chainTipHeight - 1 newHeaderTip, err := f.readHeader(newHeightTip) if err != nil { return nil, err } // Now that we have the information we need to return from this // function, we can now truncate both the header file and the index. if err := f.singleTruncate(); err != nil { return nil, err } if err := f.truncateIndex(newTip, false); err != nil { return nil, err } // TODO(roasbeef): return chain hash also? return &waddrmgr.BlockStamp{ Height: int32(newHeightTip), Hash: *newHeaderTip, }, nil }
[ "func", "(", "f", "*", "FilterHeaderStore", ")", "RollbackLastBlock", "(", "newTip", "*", "chainhash", ".", "Hash", ")", "(", "*", "waddrmgr", ".", "BlockStamp", ",", "error", ")", "{", "// Lock store for write.", "f", ".", "mtx", ".", "Lock", "(", ")", "\n", "defer", "f", ".", "mtx", ".", "Unlock", "(", ")", "\n\n", "// First, we'll obtain the latest height that the index knows of.", "_", ",", "chainTipHeight", ",", "err", ":=", "f", ".", "chainTip", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "// With this height obtained, we'll use it to read what will be the new", "// chain tip from disk.", "newHeightTip", ":=", "chainTipHeight", "-", "1", "\n", "newHeaderTip", ",", "err", ":=", "f", ".", "readHeader", "(", "newHeightTip", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "// Now that we have the information we need to return from this", "// function, we can now truncate both the header file and the index.", "if", "err", ":=", "f", ".", "singleTruncate", "(", ")", ";", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n", "if", "err", ":=", "f", ".", "truncateIndex", "(", "newTip", ",", "false", ")", ";", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n\n", "// TODO(roasbeef): return chain hash also?", "return", "&", "waddrmgr", ".", "BlockStamp", "{", "Height", ":", "int32", "(", "newHeightTip", ")", ",", "Hash", ":", "*", "newHeaderTip", ",", "}", ",", "nil", "\n", "}" ]
// RollbackLastBlock rollsback both the index, and on-disk header file by a // _single_ filter header. This method is meant to be used in the case of // re-org which disconnects the latest filter header from the end of the main // chain. The information about the latest header tip after truncation is // returned.
[ "RollbackLastBlock", "rollsback", "both", "the", "index", "and", "on", "-", "disk", "header", "file", "by", "a", "_single_", "filter", "header", ".", "This", "method", "is", "meant", "to", "be", "used", "in", "the", "case", "of", "re", "-", "org", "which", "disconnects", "the", "latest", "filter", "header", "from", "the", "end", "of", "the", "main", "chain", ".", "The", "information", "about", "the", "latest", "header", "tip", "after", "truncation", "is", "returned", "." ]
a655679fe131a5d1b4417872cc834fc3862ac70e
https://github.com/lightninglabs/neutrino/blob/a655679fe131a5d1b4417872cc834fc3862ac70e/headerfs/store.go#L836-L869
train
Workiva/go-datastructures
numerics/optimization/global.go
search
func (results *results) search(result *nmVertex) int { return sort.Search(len(results.vertices), func(i int) bool { return !results.vertices[i].less(results.config, result) }) }
go
func (results *results) search(result *nmVertex) int { return sort.Search(len(results.vertices), func(i int) bool { return !results.vertices[i].less(results.config, result) }) }
[ "func", "(", "results", "*", "results", ")", "search", "(", "result", "*", "nmVertex", ")", "int", "{", "return", "sort", ".", "Search", "(", "len", "(", "results", ".", "vertices", ")", ",", "func", "(", "i", "int", ")", "bool", "{", "return", "!", "results", ".", "vertices", "[", "i", "]", ".", "less", "(", "results", ".", "config", ",", "result", ")", "\n", "}", ")", "\n", "}" ]
// search will search this list of results based on order, order // being defined in the NelderMeadConfiguration, that is a defined // target will be treated
[ "search", "will", "search", "this", "list", "of", "results", "based", "on", "order", "order", "being", "defined", "in", "the", "NelderMeadConfiguration", "that", "is", "a", "defined", "target", "will", "be", "treated" ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/optimization/global.go#L79-L83
train
Workiva/go-datastructures
numerics/optimization/global.go
reSort
func (results *results) reSort(vertex *nmVertex) { results.insert(vertex) bestGuess := results.vertices[0] sigma := calculateSigma(len(results.config.Vars), len(results.vertices)) results.pbs.calculateProbabilities(bestGuess, sigma) results.pbs.sort() }
go
func (results *results) reSort(vertex *nmVertex) { results.insert(vertex) bestGuess := results.vertices[0] sigma := calculateSigma(len(results.config.Vars), len(results.vertices)) results.pbs.calculateProbabilities(bestGuess, sigma) results.pbs.sort() }
[ "func", "(", "results", "*", "results", ")", "reSort", "(", "vertex", "*", "nmVertex", ")", "{", "results", ".", "insert", "(", "vertex", ")", "\n\n", "bestGuess", ":=", "results", ".", "vertices", "[", "0", "]", "\n", "sigma", ":=", "calculateSigma", "(", "len", "(", "results", ".", "config", ".", "Vars", ")", ",", "len", "(", "results", ".", "vertices", ")", ")", "\n", "results", ".", "pbs", ".", "calculateProbabilities", "(", "bestGuess", ",", "sigma", ")", "\n", "results", ".", "pbs", ".", "sort", "(", ")", "\n", "}" ]
// reSort will re-sort the list of possible guess vertices // based upon the latest calculated result. It was also // add this result to the list of results.
[ "reSort", "will", "re", "-", "sort", "the", "list", "of", "possible", "guess", "vertices", "based", "upon", "the", "latest", "calculated", "result", ".", "It", "was", "also", "add", "this", "result", "to", "the", "list", "of", "results", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/optimization/global.go#L145-L152
train
Workiva/go-datastructures
rangetree/immutable.go
Add
func (irt *immutableRangeTree) Add(entries ...Entry) *immutableRangeTree { if len(entries) == 0 { return irt } cache := newCache(irt.dimensions) top := make(orderedNodes, len(irt.top)) copy(top, irt.top) added := uint64(0) for _, entry := range entries { irt.add(&top, cache, entry, &added) } tree := newImmutableRangeTree(irt.dimensions) tree.top = top tree.number = irt.number + added return tree }
go
func (irt *immutableRangeTree) Add(entries ...Entry) *immutableRangeTree { if len(entries) == 0 { return irt } cache := newCache(irt.dimensions) top := make(orderedNodes, len(irt.top)) copy(top, irt.top) added := uint64(0) for _, entry := range entries { irt.add(&top, cache, entry, &added) } tree := newImmutableRangeTree(irt.dimensions) tree.top = top tree.number = irt.number + added return tree }
[ "func", "(", "irt", "*", "immutableRangeTree", ")", "Add", "(", "entries", "...", "Entry", ")", "*", "immutableRangeTree", "{", "if", "len", "(", "entries", ")", "==", "0", "{", "return", "irt", "\n", "}", "\n\n", "cache", ":=", "newCache", "(", "irt", ".", "dimensions", ")", "\n", "top", ":=", "make", "(", "orderedNodes", ",", "len", "(", "irt", ".", "top", ")", ")", "\n", "copy", "(", "top", ",", "irt", ".", "top", ")", "\n", "added", ":=", "uint64", "(", "0", ")", "\n", "for", "_", ",", "entry", ":=", "range", "entries", "{", "irt", ".", "add", "(", "&", "top", ",", "cache", ",", "entry", ",", "&", "added", ")", "\n", "}", "\n\n", "tree", ":=", "newImmutableRangeTree", "(", "irt", ".", "dimensions", ")", "\n", "tree", ".", "top", "=", "top", "\n", "tree", ".", "number", "=", "irt", ".", "number", "+", "added", "\n", "return", "tree", "\n", "}" ]
// Add will add the provided entries into the tree and return // a new tree with those entries added.
[ "Add", "will", "add", "the", "provided", "entries", "into", "the", "tree", "and", "return", "a", "new", "tree", "with", "those", "entries", "added", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/immutable.go#L78-L95
train
Workiva/go-datastructures
rangetree/immutable.go
InsertAtDimension
func (irt *immutableRangeTree) InsertAtDimension(dimension uint64, index, number int64) (*immutableRangeTree, Entries, Entries) { if dimension > irt.dimensions || number == 0 { return irt, nil, nil } modified, deleted := make(Entries, 0, 100), make(Entries, 0, 100) tree := newImmutableRangeTree(irt.dimensions) tree.top = irt.top.immutableInsert( dimension, 1, irt.dimensions, index, number, &modified, &deleted, ) tree.number = irt.number - uint64(len(deleted)) return tree, modified, deleted }
go
func (irt *immutableRangeTree) InsertAtDimension(dimension uint64, index, number int64) (*immutableRangeTree, Entries, Entries) { if dimension > irt.dimensions || number == 0 { return irt, nil, nil } modified, deleted := make(Entries, 0, 100), make(Entries, 0, 100) tree := newImmutableRangeTree(irt.dimensions) tree.top = irt.top.immutableInsert( dimension, 1, irt.dimensions, index, number, &modified, &deleted, ) tree.number = irt.number - uint64(len(deleted)) return tree, modified, deleted }
[ "func", "(", "irt", "*", "immutableRangeTree", ")", "InsertAtDimension", "(", "dimension", "uint64", ",", "index", ",", "number", "int64", ")", "(", "*", "immutableRangeTree", ",", "Entries", ",", "Entries", ")", "{", "if", "dimension", ">", "irt", ".", "dimensions", "||", "number", "==", "0", "{", "return", "irt", ",", "nil", ",", "nil", "\n", "}", "\n\n", "modified", ",", "deleted", ":=", "make", "(", "Entries", ",", "0", ",", "100", ")", ",", "make", "(", "Entries", ",", "0", ",", "100", ")", "\n\n", "tree", ":=", "newImmutableRangeTree", "(", "irt", ".", "dimensions", ")", "\n", "tree", ".", "top", "=", "irt", ".", "top", ".", "immutableInsert", "(", "dimension", ",", "1", ",", "irt", ".", "dimensions", ",", "index", ",", "number", ",", "&", "modified", ",", "&", "deleted", ",", ")", "\n", "tree", ".", "number", "=", "irt", ".", "number", "-", "uint64", "(", "len", "(", "deleted", ")", ")", "\n\n", "return", "tree", ",", "modified", ",", "deleted", "\n", "}" ]
// InsertAtDimension will increment items at and above the given index // by the number provided. Provide a negative number to to decrement. // Returned are two lists and the modified tree. The first list is a // list of entries that were moved. The second is a list entries that // were deleted. These lists are exclusive.
[ "InsertAtDimension", "will", "increment", "items", "at", "and", "above", "the", "given", "index", "by", "the", "number", "provided", ".", "Provide", "a", "negative", "number", "to", "to", "decrement", ".", "Returned", "are", "two", "lists", "and", "the", "modified", "tree", ".", "The", "first", "list", "is", "a", "list", "of", "entries", "that", "were", "moved", ".", "The", "second", "is", "a", "list", "entries", "that", "were", "deleted", ".", "These", "lists", "are", "exclusive", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/immutable.go#L102-L120
train
Workiva/go-datastructures
threadsafe/err/error.go
Set
func (e *Error) Set(err error) { e.lock.Lock() defer e.lock.Unlock() e.err = err }
go
func (e *Error) Set(err error) { e.lock.Lock() defer e.lock.Unlock() e.err = err }
[ "func", "(", "e", "*", "Error", ")", "Set", "(", "err", "error", ")", "{", "e", ".", "lock", ".", "Lock", "(", ")", "\n", "defer", "e", ".", "lock", ".", "Unlock", "(", ")", "\n\n", "e", ".", "err", "=", "err", "\n", "}" ]
// Set will set the error of this structure to the provided // value.
[ "Set", "will", "set", "the", "error", "of", "this", "structure", "to", "the", "provided", "value", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/threadsafe/err/error.go#L36-L41
train
Workiva/go-datastructures
threadsafe/err/error.go
Get
func (e *Error) Get() error { e.lock.RLock() defer e.lock.RUnlock() return e.err }
go
func (e *Error) Get() error { e.lock.RLock() defer e.lock.RUnlock() return e.err }
[ "func", "(", "e", "*", "Error", ")", "Get", "(", ")", "error", "{", "e", ".", "lock", ".", "RLock", "(", ")", "\n", "defer", "e", ".", "lock", ".", "RUnlock", "(", ")", "\n\n", "return", "e", ".", "err", "\n", "}" ]
// Get will return any error associated with this structure.
[ "Get", "will", "return", "any", "error", "associated", "with", "this", "structure", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/threadsafe/err/error.go#L44-L49
train
Workiva/go-datastructures
rangetree/orderedtree.go
add
func (ot *orderedTree) add(entry Entry) *node { var node *node list := &ot.top for i := uint64(1); i <= ot.dimensions; i++ { if isLastDimension(ot.dimensions, i) { overwritten := list.add( newNode(entry.ValueAtDimension(i), entry, false), ) if overwritten == nil { ot.number++ } return overwritten } node, _ = list.getOrAdd(entry, i, ot.dimensions) list = &node.orderedNodes } return nil }
go
func (ot *orderedTree) add(entry Entry) *node { var node *node list := &ot.top for i := uint64(1); i <= ot.dimensions; i++ { if isLastDimension(ot.dimensions, i) { overwritten := list.add( newNode(entry.ValueAtDimension(i), entry, false), ) if overwritten == nil { ot.number++ } return overwritten } node, _ = list.getOrAdd(entry, i, ot.dimensions) list = &node.orderedNodes } return nil }
[ "func", "(", "ot", "*", "orderedTree", ")", "add", "(", "entry", "Entry", ")", "*", "node", "{", "var", "node", "*", "node", "\n", "list", ":=", "&", "ot", ".", "top", "\n\n", "for", "i", ":=", "uint64", "(", "1", ")", ";", "i", "<=", "ot", ".", "dimensions", ";", "i", "++", "{", "if", "isLastDimension", "(", "ot", ".", "dimensions", ",", "i", ")", "{", "overwritten", ":=", "list", ".", "add", "(", "newNode", "(", "entry", ".", "ValueAtDimension", "(", "i", ")", ",", "entry", ",", "false", ")", ",", ")", "\n", "if", "overwritten", "==", "nil", "{", "ot", ".", "number", "++", "\n", "}", "\n", "return", "overwritten", "\n", "}", "\n", "node", ",", "_", "=", "list", ".", "getOrAdd", "(", "entry", ",", "i", ",", "ot", ".", "dimensions", ")", "\n", "list", "=", "&", "node", ".", "orderedNodes", "\n", "}", "\n\n", "return", "nil", "\n", "}" ]
// add will add the provided entry to the rangetree and return an // entry if one was overwritten.
[ "add", "will", "add", "the", "provided", "entry", "to", "the", "rangetree", "and", "return", "an", "entry", "if", "one", "was", "overwritten", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/orderedtree.go#L44-L63
train
Workiva/go-datastructures
slice/int64.go
Search
func (s Int64Slice) Search(x int64) int { return sort.Search(len(s), func(i int) bool { return s[i] >= x }) }
go
func (s Int64Slice) Search(x int64) int { return sort.Search(len(s), func(i int) bool { return s[i] >= x }) }
[ "func", "(", "s", "Int64Slice", ")", "Search", "(", "x", "int64", ")", "int", "{", "return", "sort", ".", "Search", "(", "len", "(", "s", ")", ",", "func", "(", "i", "int", ")", "bool", "{", "return", "s", "[", "i", "]", ">=", "x", "\n", "}", ")", "\n", "}" ]
// Search will search this slice and return an index that corresponds // to the lowest position of that value. You'll need to check // separately if the value at that position is equal to x. The // behavior of this method is undefinited if the slice is not sorted.
[ "Search", "will", "search", "this", "slice", "and", "return", "an", "index", "that", "corresponds", "to", "the", "lowest", "position", "of", "that", "value", ".", "You", "ll", "need", "to", "check", "separately", "if", "the", "value", "at", "that", "position", "is", "equal", "to", "x", ".", "The", "behavior", "of", "this", "method", "is", "undefinited", "if", "the", "slice", "is", "not", "sorted", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/int64.go#L45-L49
train
Workiva/go-datastructures
slice/int64.go
Exists
func (s Int64Slice) Exists(x int64) bool { i := s.Search(x) if i == len(s) { return false } return s[i] == x }
go
func (s Int64Slice) Exists(x int64) bool { i := s.Search(x) if i == len(s) { return false } return s[i] == x }
[ "func", "(", "s", "Int64Slice", ")", "Exists", "(", "x", "int64", ")", "bool", "{", "i", ":=", "s", ".", "Search", "(", "x", ")", "\n", "if", "i", "==", "len", "(", "s", ")", "{", "return", "false", "\n", "}", "\n\n", "return", "s", "[", "i", "]", "==", "x", "\n", "}" ]
// Exists returns a bool indicating if the provided value exists // in this list. This has undefined behavior if the list is not // sorted.
[ "Exists", "returns", "a", "bool", "indicating", "if", "the", "provided", "value", "exists", "in", "this", "list", ".", "This", "has", "undefined", "behavior", "if", "the", "list", "is", "not", "sorted", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/int64.go#L65-L72
train
Workiva/go-datastructures
slice/int64.go
Insert
func (s Int64Slice) Insert(x int64) Int64Slice { i := s.Search(x) if i == len(s) { return append(s, x) } if s[i] == x { return s } s = append(s, 0) copy(s[i+1:], s[i:]) s[i] = x return s }
go
func (s Int64Slice) Insert(x int64) Int64Slice { i := s.Search(x) if i == len(s) { return append(s, x) } if s[i] == x { return s } s = append(s, 0) copy(s[i+1:], s[i:]) s[i] = x return s }
[ "func", "(", "s", "Int64Slice", ")", "Insert", "(", "x", "int64", ")", "Int64Slice", "{", "i", ":=", "s", ".", "Search", "(", "x", ")", "\n", "if", "i", "==", "len", "(", "s", ")", "{", "return", "append", "(", "s", ",", "x", ")", "\n", "}", "\n\n", "if", "s", "[", "i", "]", "==", "x", "{", "return", "s", "\n", "}", "\n\n", "s", "=", "append", "(", "s", ",", "0", ")", "\n", "copy", "(", "s", "[", "i", "+", "1", ":", "]", ",", "s", "[", "i", ":", "]", ")", "\n", "s", "[", "i", "]", "=", "x", "\n", "return", "s", "\n", "}" ]
// Insert will insert x into the sorted position in this list // and return a list with the value added. If this slice has not // been sorted Insert's behavior is undefined.
[ "Insert", "will", "insert", "x", "into", "the", "sorted", "position", "in", "this", "list", "and", "return", "a", "list", "with", "the", "value", "added", ".", "If", "this", "slice", "has", "not", "been", "sorted", "Insert", "s", "behavior", "is", "undefined", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/int64.go#L77-L91
train
Workiva/go-datastructures
btree/immutable/config.go
DefaultConfig
func DefaultConfig(persister Persister, comparator Comparator) Config { return Config{ NodeWidth: 10000, Persister: persister, Comparator: comparator, } }
go
func DefaultConfig(persister Persister, comparator Comparator) Config { return Config{ NodeWidth: 10000, Persister: persister, Comparator: comparator, } }
[ "func", "DefaultConfig", "(", "persister", "Persister", ",", "comparator", "Comparator", ")", "Config", "{", "return", "Config", "{", "NodeWidth", ":", "10000", ",", "Persister", ":", "persister", ",", "Comparator", ":", "comparator", ",", "}", "\n", "}" ]
// DefaultConfig returns a configuration with the persister set. All other // fields are set to smart defaults for persistence.
[ "DefaultConfig", "returns", "a", "configuration", "with", "the", "persister", "set", ".", "All", "other", "fields", "are", "set", "to", "smart", "defaults", "for", "persistence", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/config.go#L37-L43
train
Workiva/go-datastructures
trie/xfast/iterator.go
exhaust
func (iter *Iterator) exhaust() Entries { entries := make(Entries, 0, 100) for it := iter; it.Next(); { entries = append(entries, it.Value()) } return entries }
go
func (iter *Iterator) exhaust() Entries { entries := make(Entries, 0, 100) for it := iter; it.Next(); { entries = append(entries, it.Value()) } return entries }
[ "func", "(", "iter", "*", "Iterator", ")", "exhaust", "(", ")", "Entries", "{", "entries", ":=", "make", "(", "Entries", ",", "0", ",", "100", ")", "\n", "for", "it", ":=", "iter", ";", "it", ".", "Next", "(", ")", ";", "{", "entries", "=", "append", "(", "entries", ",", "it", ".", "Value", "(", ")", ")", "\n", "}", "\n\n", "return", "entries", "\n", "}" ]
// exhaust is a helper function that will exhaust this iterator // and return a list of entries. This is for internal use only.
[ "exhaust", "is", "a", "helper", "function", "that", "will", "exhaust", "this", "iterator", "and", "return", "a", "list", "of", "entries", ".", "This", "is", "for", "internal", "use", "only", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/iterator.go#L53-L60
train
Workiva/go-datastructures
hashmap/fastinteger/hashmap.go
roundUp
func roundUp(v uint64) uint64 { v-- v |= v >> 1 v |= v >> 2 v |= v >> 4 v |= v >> 8 v |= v >> 16 v |= v >> 32 v++ return v }
go
func roundUp(v uint64) uint64 { v-- v |= v >> 1 v |= v >> 2 v |= v >> 4 v |= v >> 8 v |= v >> 16 v |= v >> 32 v++ return v }
[ "func", "roundUp", "(", "v", "uint64", ")", "uint64", "{", "v", "--", "\n", "v", "|=", "v", ">>", "1", "\n", "v", "|=", "v", ">>", "2", "\n", "v", "|=", "v", ">>", "4", "\n", "v", "|=", "v", ">>", "8", "\n", "v", "|=", "v", ">>", "16", "\n", "v", "|=", "v", ">>", "32", "\n", "v", "++", "\n", "return", "v", "\n", "}" ]
// roundUp takes a uint64 greater than 0 and rounds it up to the next // power of 2.
[ "roundUp", "takes", "a", "uint64", "greater", "than", "0", "and", "rounds", "it", "up", "to", "the", "next", "power", "of", "2", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/hashmap/fastinteger/hashmap.go#L27-L37
train
Workiva/go-datastructures
hashmap/fastinteger/hashmap.go
rebuild
func (fi *FastIntegerHashMap) rebuild() { packets := make(packets, roundUp(uint64(len(fi.packets))+1)) for _, packet := range fi.packets { if packet == nil { continue } packets.set(packet) } fi.packets = packets }
go
func (fi *FastIntegerHashMap) rebuild() { packets := make(packets, roundUp(uint64(len(fi.packets))+1)) for _, packet := range fi.packets { if packet == nil { continue } packets.set(packet) } fi.packets = packets }
[ "func", "(", "fi", "*", "FastIntegerHashMap", ")", "rebuild", "(", ")", "{", "packets", ":=", "make", "(", "packets", ",", "roundUp", "(", "uint64", "(", "len", "(", "fi", ".", "packets", ")", ")", "+", "1", ")", ")", "\n", "for", "_", ",", "packet", ":=", "range", "fi", ".", "packets", "{", "if", "packet", "==", "nil", "{", "continue", "\n", "}", "\n\n", "packets", ".", "set", "(", "packet", ")", "\n", "}", "\n", "fi", ".", "packets", "=", "packets", "\n", "}" ]
// rebuild is an expensive operation which requires us to iterate // over the current bucket and rehash the keys for insertion into // the new bucket. The new bucket is twice as large as the old // bucket by default.
[ "rebuild", "is", "an", "expensive", "operation", "which", "requires", "us", "to", "iterate", "over", "the", "current", "bucket", "and", "rehash", "the", "keys", "for", "insertion", "into", "the", "new", "bucket", ".", "The", "new", "bucket", "is", "twice", "as", "large", "as", "the", "old", "bucket", "by", "default", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/hashmap/fastinteger/hashmap.go#L109-L119
train
Workiva/go-datastructures
hashmap/fastinteger/hashmap.go
Get
func (fi *FastIntegerHashMap) Get(key uint64) (uint64, bool) { return fi.packets.get(key) }
go
func (fi *FastIntegerHashMap) Get(key uint64) (uint64, bool) { return fi.packets.get(key) }
[ "func", "(", "fi", "*", "FastIntegerHashMap", ")", "Get", "(", "key", "uint64", ")", "(", "uint64", ",", "bool", ")", "{", "return", "fi", ".", "packets", ".", "get", "(", "key", ")", "\n", "}" ]
// Get returns an item from the map if it exists. Otherwise, // returns false for the second argument.
[ "Get", "returns", "an", "item", "from", "the", "map", "if", "it", "exists", ".", "Otherwise", "returns", "false", "for", "the", "second", "argument", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/hashmap/fastinteger/hashmap.go#L123-L125
train
Workiva/go-datastructures
hashmap/fastinteger/hashmap.go
Set
func (fi *FastIntegerHashMap) Set(key, value uint64) { if float64(fi.count+1)/float64(len(fi.packets)) > ratio { fi.rebuild() } fi.packets.set(&packet{key: key, value: value}) fi.count++ }
go
func (fi *FastIntegerHashMap) Set(key, value uint64) { if float64(fi.count+1)/float64(len(fi.packets)) > ratio { fi.rebuild() } fi.packets.set(&packet{key: key, value: value}) fi.count++ }
[ "func", "(", "fi", "*", "FastIntegerHashMap", ")", "Set", "(", "key", ",", "value", "uint64", ")", "{", "if", "float64", "(", "fi", ".", "count", "+", "1", ")", "/", "float64", "(", "len", "(", "fi", ".", "packets", ")", ")", ">", "ratio", "{", "fi", ".", "rebuild", "(", ")", "\n", "}", "\n\n", "fi", ".", "packets", ".", "set", "(", "&", "packet", "{", "key", ":", "key", ",", "value", ":", "value", "}", ")", "\n", "fi", ".", "count", "++", "\n", "}" ]
// Set will set the provided key with the provided value.
[ "Set", "will", "set", "the", "provided", "key", "with", "the", "provided", "value", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/hashmap/fastinteger/hashmap.go#L128-L135
train
Workiva/go-datastructures
hashmap/fastinteger/hashmap.go
Exists
func (fi *FastIntegerHashMap) Exists(key uint64) bool { return fi.packets.exists(key) }
go
func (fi *FastIntegerHashMap) Exists(key uint64) bool { return fi.packets.exists(key) }
[ "func", "(", "fi", "*", "FastIntegerHashMap", ")", "Exists", "(", "key", "uint64", ")", "bool", "{", "return", "fi", ".", "packets", ".", "exists", "(", "key", ")", "\n", "}" ]
// Exists will return a bool indicating if the provided key // exists in the map.
[ "Exists", "will", "return", "a", "bool", "indicating", "if", "the", "provided", "key", "exists", "in", "the", "map", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/hashmap/fastinteger/hashmap.go#L139-L141
train
Workiva/go-datastructures
hashmap/fastinteger/hashmap.go
Delete
func (fi *FastIntegerHashMap) Delete(key uint64) { if fi.packets.delete(key) { fi.count-- } }
go
func (fi *FastIntegerHashMap) Delete(key uint64) { if fi.packets.delete(key) { fi.count-- } }
[ "func", "(", "fi", "*", "FastIntegerHashMap", ")", "Delete", "(", "key", "uint64", ")", "{", "if", "fi", ".", "packets", ".", "delete", "(", "key", ")", "{", "fi", ".", "count", "--", "\n", "}", "\n", "}" ]
// Delete will remove the provided key from the hashmap. If // the key cannot be found, this is a no-op.
[ "Delete", "will", "remove", "the", "provided", "key", "from", "the", "hashmap", ".", "If", "the", "key", "cannot", "be", "found", "this", "is", "a", "no", "-", "op", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/hashmap/fastinteger/hashmap.go#L145-L149
train
Workiva/go-datastructures
hashmap/fastinteger/hashmap.go
New
func New(hint uint64) *FastIntegerHashMap { if hint == 0 { hint = 16 } hint = roundUp(hint) return &FastIntegerHashMap{ count: 0, packets: make(packets, hint), } }
go
func New(hint uint64) *FastIntegerHashMap { if hint == 0 { hint = 16 } hint = roundUp(hint) return &FastIntegerHashMap{ count: 0, packets: make(packets, hint), } }
[ "func", "New", "(", "hint", "uint64", ")", "*", "FastIntegerHashMap", "{", "if", "hint", "==", "0", "{", "hint", "=", "16", "\n", "}", "\n\n", "hint", "=", "roundUp", "(", "hint", ")", "\n", "return", "&", "FastIntegerHashMap", "{", "count", ":", "0", ",", "packets", ":", "make", "(", "packets", ",", "hint", ")", ",", "}", "\n", "}" ]
// New returns a new FastIntegerHashMap with a bucket size specified // by hint.
[ "New", "returns", "a", "new", "FastIntegerHashMap", "with", "a", "bucket", "size", "specified", "by", "hint", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/hashmap/fastinteger/hashmap.go#L163-L173
train
Workiva/go-datastructures
rangetree/skiplist/skiplist.go
Compare
func (se skipEntry) Compare(other common.Comparator) int { otherSe := other.(skipEntry) if se == otherSe { return 0 } if se > otherSe { return 1 } return -1 }
go
func (se skipEntry) Compare(other common.Comparator) int { otherSe := other.(skipEntry) if se == otherSe { return 0 } if se > otherSe { return 1 } return -1 }
[ "func", "(", "se", "skipEntry", ")", "Compare", "(", "other", "common", ".", "Comparator", ")", "int", "{", "otherSe", ":=", "other", ".", "(", "skipEntry", ")", "\n", "if", "se", "==", "otherSe", "{", "return", "0", "\n", "}", "\n\n", "if", "se", ">", "otherSe", "{", "return", "1", "\n", "}", "\n\n", "return", "-", "1", "\n", "}" ]
// Compare is required by the Comparator interface.
[ "Compare", "is", "required", "by", "the", "Comparator", "interface", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/skiplist/skiplist.go#L51-L62
train
Workiva/go-datastructures
rangetree/skiplist/skiplist.go
isLastDimension
func isLastDimension(dimension, lastDimension uint64) bool { if dimension >= lastDimension { // useful in testing and denotes a serious problem panic(`Dimension is greater than possible dimensions.`) } return dimension == lastDimension-1 }
go
func isLastDimension(dimension, lastDimension uint64) bool { if dimension >= lastDimension { // useful in testing and denotes a serious problem panic(`Dimension is greater than possible dimensions.`) } return dimension == lastDimension-1 }
[ "func", "isLastDimension", "(", "dimension", ",", "lastDimension", "uint64", ")", "bool", "{", "if", "dimension", ">=", "lastDimension", "{", "// useful in testing and denotes a serious problem", "panic", "(", "`Dimension is greater than possible dimensions.`", ")", "\n", "}", "\n\n", "return", "dimension", "==", "lastDimension", "-", "1", "\n", "}" ]
// isLastDimension simply returns dimension == lastDimension-1. // This panics if dimension >= lastDimension.
[ "isLastDimension", "simply", "returns", "dimension", "==", "lastDimension", "-", "1", ".", "This", "panics", "if", "dimension", ">", "=", "lastDimension", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/skiplist/skiplist.go#L70-L76
train
Workiva/go-datastructures
rangetree/skiplist/skiplist.go
needsDeletion
func needsDeletion(value, index, number int64) bool { if number > 0 { return false } number = -number // get the magnitude offset := value - index return offset >= 0 && offset < number }
go
func needsDeletion(value, index, number int64) bool { if number > 0 { return false } number = -number // get the magnitude offset := value - index return offset >= 0 && offset < number }
[ "func", "needsDeletion", "(", "value", ",", "index", ",", "number", "int64", ")", "bool", "{", "if", "number", ">", "0", "{", "return", "false", "\n", "}", "\n\n", "number", "=", "-", "number", "// get the magnitude", "\n", "offset", ":=", "value", "-", "index", "\n\n", "return", "offset", ">=", "0", "&&", "offset", "<", "number", "\n", "}" ]
// needsDeletion returns a bool indicating if the provided value // needs to be deleted based on the provided index and number.
[ "needsDeletion", "returns", "a", "bool", "indicating", "if", "the", "provided", "value", "needs", "to", "be", "deleted", "based", "on", "the", "provided", "index", "and", "number", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/skiplist/skiplist.go#L80-L89
train
Workiva/go-datastructures
rangetree/skiplist/skiplist.go
Get
func (rt *skipListRT) Get(entries ...rangetree.Entry) rangetree.Entries { results := make(rangetree.Entries, 0, len(entries)) for _, e := range entries { results = append(results, rt.get(e)) } return results }
go
func (rt *skipListRT) Get(entries ...rangetree.Entry) rangetree.Entries { results := make(rangetree.Entries, 0, len(entries)) for _, e := range entries { results = append(results, rt.get(e)) } return results }
[ "func", "(", "rt", "*", "skipListRT", ")", "Get", "(", "entries", "...", "rangetree", ".", "Entry", ")", "rangetree", ".", "Entries", "{", "results", ":=", "make", "(", "rangetree", ".", "Entries", ",", "0", ",", "len", "(", "entries", ")", ")", "\n", "for", "_", ",", "e", ":=", "range", "entries", "{", "results", "=", "append", "(", "results", ",", "rt", ".", "get", "(", "e", ")", ")", "\n", "}", "\n\n", "return", "results", "\n", "}" ]
// Get will return any rangetree.Entries matching the provided entries. // Similar in functionality to a key lookup, this returns nil for any // entry that could not be found.
[ "Get", "will", "return", "any", "rangetree", ".", "Entries", "matching", "the", "provided", "entries", ".", "Similar", "in", "functionality", "to", "a", "key", "lookup", "this", "returns", "nil", "for", "any", "entry", "that", "could", "not", "be", "found", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/skiplist/skiplist.go#L235-L242
train
Workiva/go-datastructures
rangetree/skiplist/skiplist.go
deleteRecursive
func (rt *skipListRT) deleteRecursive(sl *skip.SkipList, dimension uint64, entry rangetree.Entry) rangetree.Entry { value := entry.ValueAtDimension(dimension) if isLastDimension(dimension, rt.dimensions) { entries := sl.Delete(skipEntry(value)) if entries[0] == nil { return nil } rt.number-- return entries[0].(*lastBundle).entry } db, ok := sl.Get(skipEntry(value))[0].(*dimensionalBundle) if !ok { // value was not found return nil } result := rt.deleteRecursive(db.sl, dimension+1, entry) if result == nil { return nil } if db.sl.Len() == 0 { sl.Delete(db) } return result }
go
func (rt *skipListRT) deleteRecursive(sl *skip.SkipList, dimension uint64, entry rangetree.Entry) rangetree.Entry { value := entry.ValueAtDimension(dimension) if isLastDimension(dimension, rt.dimensions) { entries := sl.Delete(skipEntry(value)) if entries[0] == nil { return nil } rt.number-- return entries[0].(*lastBundle).entry } db, ok := sl.Get(skipEntry(value))[0].(*dimensionalBundle) if !ok { // value was not found return nil } result := rt.deleteRecursive(db.sl, dimension+1, entry) if result == nil { return nil } if db.sl.Len() == 0 { sl.Delete(db) } return result }
[ "func", "(", "rt", "*", "skipListRT", ")", "deleteRecursive", "(", "sl", "*", "skip", ".", "SkipList", ",", "dimension", "uint64", ",", "entry", "rangetree", ".", "Entry", ")", "rangetree", ".", "Entry", "{", "value", ":=", "entry", ".", "ValueAtDimension", "(", "dimension", ")", "\n", "if", "isLastDimension", "(", "dimension", ",", "rt", ".", "dimensions", ")", "{", "entries", ":=", "sl", ".", "Delete", "(", "skipEntry", "(", "value", ")", ")", "\n", "if", "entries", "[", "0", "]", "==", "nil", "{", "return", "nil", "\n", "}", "\n\n", "rt", ".", "number", "--", "\n", "return", "entries", "[", "0", "]", ".", "(", "*", "lastBundle", ")", ".", "entry", "\n", "}", "\n\n", "db", ",", "ok", ":=", "sl", ".", "Get", "(", "skipEntry", "(", "value", ")", ")", "[", "0", "]", ".", "(", "*", "dimensionalBundle", ")", "\n", "if", "!", "ok", "{", "// value was not found", "return", "nil", "\n", "}", "\n\n", "result", ":=", "rt", ".", "deleteRecursive", "(", "db", ".", "sl", ",", "dimension", "+", "1", ",", "entry", ")", "\n", "if", "result", "==", "nil", "{", "return", "nil", "\n", "}", "\n\n", "if", "db", ".", "sl", ".", "Len", "(", ")", "==", "0", "{", "sl", ".", "Delete", "(", "db", ")", "\n", "}", "\n\n", "return", "result", "\n", "}" ]
// deleteRecursive is used by the delete logic. The recursion depth // only goes as far as the number of dimensions, so this shouldn't be an // issue.
[ "deleteRecursive", "is", "used", "by", "the", "delete", "logic", ".", "The", "recursion", "depth", "only", "goes", "as", "far", "as", "the", "number", "of", "dimensions", "so", "this", "shouldn", "t", "be", "an", "issue", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/skiplist/skiplist.go#L252-L281
train
Workiva/go-datastructures
rangetree/skiplist/skiplist.go
Apply
func (rt *skipListRT) Apply(interval rangetree.Interval, fn func(rangetree.Entry) bool) { rt.apply(rt.top, 0, interval, fn) }
go
func (rt *skipListRT) Apply(interval rangetree.Interval, fn func(rangetree.Entry) bool) { rt.apply(rt.top, 0, interval, fn) }
[ "func", "(", "rt", "*", "skipListRT", ")", "Apply", "(", "interval", "rangetree", ".", "Interval", ",", "fn", "func", "(", "rangetree", ".", "Entry", ")", "bool", ")", "{", "rt", ".", "apply", "(", "rt", ".", "top", ",", "0", ",", "interval", ",", "fn", ")", "\n", "}" ]
// Apply will call the provided function with each entry that exists // within the provided range, in order. Return false at any time to // cancel iteration. Altering the entry in such a way that its location // changes will result in undefined behavior.
[ "Apply", "will", "call", "the", "provided", "function", "with", "each", "entry", "that", "exists", "within", "the", "provided", "range", "in", "order", ".", "Return", "false", "at", "any", "time", "to", "cancel", "iteration", ".", "Altering", "the", "entry", "in", "such", "a", "way", "that", "its", "location", "changes", "will", "result", "in", "undefined", "behavior", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/skiplist/skiplist.go#L332-L334
train
Workiva/go-datastructures
rangetree/skiplist/skiplist.go
Query
func (rt *skipListRT) Query(interval rangetree.Interval) rangetree.Entries { entries := make(rangetree.Entries, 0, 100) rt.apply(rt.top, 0, interval, func(e rangetree.Entry) bool { entries = append(entries, e) return true }) return entries }
go
func (rt *skipListRT) Query(interval rangetree.Interval) rangetree.Entries { entries := make(rangetree.Entries, 0, 100) rt.apply(rt.top, 0, interval, func(e rangetree.Entry) bool { entries = append(entries, e) return true }) return entries }
[ "func", "(", "rt", "*", "skipListRT", ")", "Query", "(", "interval", "rangetree", ".", "Interval", ")", "rangetree", ".", "Entries", "{", "entries", ":=", "make", "(", "rangetree", ".", "Entries", ",", "0", ",", "100", ")", "\n", "rt", ".", "apply", "(", "rt", ".", "top", ",", "0", ",", "interval", ",", "func", "(", "e", "rangetree", ".", "Entry", ")", "bool", "{", "entries", "=", "append", "(", "entries", ",", "e", ")", "\n", "return", "true", "\n", "}", ")", "\n\n", "return", "entries", "\n", "}" ]
// Query will return a list of entries that fall within // the provided interval.
[ "Query", "will", "return", "a", "list", "of", "entries", "that", "fall", "within", "the", "provided", "interval", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/skiplist/skiplist.go#L338-L346
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
Swap
func (u uintSlice) Swap(i, j int64) { u[i], u[j] = u[j], u[i] }
go
func (u uintSlice) Swap(i, j int64) { u[i], u[j] = u[j], u[i] }
[ "func", "(", "u", "uintSlice", ")", "Swap", "(", "i", ",", "j", "int64", ")", "{", "u", "[", "i", "]", ",", "u", "[", "j", "]", "=", "u", "[", "j", "]", ",", "u", "[", "i", "]", "\n", "}" ]
// Swap swaps values in this slice at the positions given.
[ "Swap", "swaps", "values", "in", "this", "slice", "at", "the", "positions", "given", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L32-L34
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
Less
func (u uintSlice) Less(i, j int64) bool { return u[i] < u[j] }
go
func (u uintSlice) Less(i, j int64) bool { return u[i] < u[j] }
[ "func", "(", "u", "uintSlice", ")", "Less", "(", "i", ",", "j", "int64", ")", "bool", "{", "return", "u", "[", "i", "]", "<", "u", "[", "j", "]", "\n", "}" ]
// Less returns a bool indicating if the value at position i is // less than position j.
[ "Less", "returns", "a", "bool", "indicating", "if", "the", "value", "at", "position", "i", "is", "less", "than", "position", "j", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L38-L40
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
SetBit
func (sba *sparseBitArray) SetBit(k uint64) error { index, position := getIndexAndRemainder(k) i, inserted := sba.indices.insert(index) if inserted { sba.blocks.insert(i) } sba.blocks[i] = sba.blocks[i].insert(position) return nil }
go
func (sba *sparseBitArray) SetBit(k uint64) error { index, position := getIndexAndRemainder(k) i, inserted := sba.indices.insert(index) if inserted { sba.blocks.insert(i) } sba.blocks[i] = sba.blocks[i].insert(position) return nil }
[ "func", "(", "sba", "*", "sparseBitArray", ")", "SetBit", "(", "k", "uint64", ")", "error", "{", "index", ",", "position", ":=", "getIndexAndRemainder", "(", "k", ")", "\n", "i", ",", "inserted", ":=", "sba", ".", "indices", ".", "insert", "(", "index", ")", "\n\n", "if", "inserted", "{", "sba", ".", "blocks", ".", "insert", "(", "i", ")", "\n", "}", "\n", "sba", ".", "blocks", "[", "i", "]", "=", "sba", ".", "blocks", "[", "i", "]", ".", "insert", "(", "position", ")", "\n", "return", "nil", "\n", "}" ]
// SetBit sets the bit at the given position.
[ "SetBit", "sets", "the", "bit", "at", "the", "given", "position", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L108-L117
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
GetBit
func (sba *sparseBitArray) GetBit(k uint64) (bool, error) { index, position := getIndexAndRemainder(k) i := sba.indices.get(index) if i == -1 { return false, nil } return sba.blocks[i].get(position), nil }
go
func (sba *sparseBitArray) GetBit(k uint64) (bool, error) { index, position := getIndexAndRemainder(k) i := sba.indices.get(index) if i == -1 { return false, nil } return sba.blocks[i].get(position), nil }
[ "func", "(", "sba", "*", "sparseBitArray", ")", "GetBit", "(", "k", "uint64", ")", "(", "bool", ",", "error", ")", "{", "index", ",", "position", ":=", "getIndexAndRemainder", "(", "k", ")", "\n", "i", ":=", "sba", ".", "indices", ".", "get", "(", "index", ")", "\n", "if", "i", "==", "-", "1", "{", "return", "false", ",", "nil", "\n", "}", "\n\n", "return", "sba", ".", "blocks", "[", "i", "]", ".", "get", "(", "position", ")", ",", "nil", "\n", "}" ]
// GetBit gets the bit at the given position.
[ "GetBit", "gets", "the", "bit", "at", "the", "given", "position", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L120-L128
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
ToNums
func (sba *sparseBitArray) ToNums() []uint64 { if len(sba.indices) == 0 { return nil } diff := uint64(len(sba.indices)) * s nums := make([]uint64, 0, diff/4) for i, offset := range sba.indices { sba.blocks[i].toNums(offset*s, &nums) } return nums }
go
func (sba *sparseBitArray) ToNums() []uint64 { if len(sba.indices) == 0 { return nil } diff := uint64(len(sba.indices)) * s nums := make([]uint64, 0, diff/4) for i, offset := range sba.indices { sba.blocks[i].toNums(offset*s, &nums) } return nums }
[ "func", "(", "sba", "*", "sparseBitArray", ")", "ToNums", "(", ")", "[", "]", "uint64", "{", "if", "len", "(", "sba", ".", "indices", ")", "==", "0", "{", "return", "nil", "\n", "}", "\n\n", "diff", ":=", "uint64", "(", "len", "(", "sba", ".", "indices", ")", ")", "*", "s", "\n", "nums", ":=", "make", "(", "[", "]", "uint64", ",", "0", ",", "diff", "/", "4", ")", "\n\n", "for", "i", ",", "offset", ":=", "range", "sba", ".", "indices", "{", "sba", ".", "blocks", "[", "i", "]", ".", "toNums", "(", "offset", "*", "s", ",", "&", "nums", ")", "\n", "}", "\n\n", "return", "nums", "\n", "}" ]
// ToNums converts this sparse bitarray to a list of numbers contained // within it.
[ "ToNums", "converts", "this", "sparse", "bitarray", "to", "a", "list", "of", "numbers", "contained", "within", "it", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L132-L145
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
ClearBit
func (sba *sparseBitArray) ClearBit(k uint64) error { index, position := getIndexAndRemainder(k) i := sba.indices.get(index) if i == -1 { return nil } sba.blocks[i] = sba.blocks[i].remove(position) if sba.blocks[i] == 0 { sba.blocks.deleteAtIndex(i) sba.indices.deleteAtIndex(i) } return nil }
go
func (sba *sparseBitArray) ClearBit(k uint64) error { index, position := getIndexAndRemainder(k) i := sba.indices.get(index) if i == -1 { return nil } sba.blocks[i] = sba.blocks[i].remove(position) if sba.blocks[i] == 0 { sba.blocks.deleteAtIndex(i) sba.indices.deleteAtIndex(i) } return nil }
[ "func", "(", "sba", "*", "sparseBitArray", ")", "ClearBit", "(", "k", "uint64", ")", "error", "{", "index", ",", "position", ":=", "getIndexAndRemainder", "(", "k", ")", "\n", "i", ":=", "sba", ".", "indices", ".", "get", "(", "index", ")", "\n", "if", "i", "==", "-", "1", "{", "return", "nil", "\n", "}", "\n\n", "sba", ".", "blocks", "[", "i", "]", "=", "sba", ".", "blocks", "[", "i", "]", ".", "remove", "(", "position", ")", "\n", "if", "sba", ".", "blocks", "[", "i", "]", "==", "0", "{", "sba", ".", "blocks", ".", "deleteAtIndex", "(", "i", ")", "\n", "sba", ".", "indices", ".", "deleteAtIndex", "(", "i", ")", "\n", "}", "\n\n", "return", "nil", "\n", "}" ]
// ClearBit clears the bit at the given position.
[ "ClearBit", "clears", "the", "bit", "at", "the", "given", "position", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L148-L162
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
Reset
func (sba *sparseBitArray) Reset() { sba.blocks = sba.blocks[:0] sba.indices = sba.indices[:0] }
go
func (sba *sparseBitArray) Reset() { sba.blocks = sba.blocks[:0] sba.indices = sba.indices[:0] }
[ "func", "(", "sba", "*", "sparseBitArray", ")", "Reset", "(", ")", "{", "sba", ".", "blocks", "=", "sba", ".", "blocks", "[", ":", "0", "]", "\n", "sba", ".", "indices", "=", "sba", ".", "indices", "[", ":", "0", "]", "\n", "}" ]
// Reset erases all values from this bitarray.
[ "Reset", "erases", "all", "values", "from", "this", "bitarray", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L165-L168
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
Equals
func (sba *sparseBitArray) Equals(other BitArray) bool { if other.Capacity() == 0 && sba.Capacity() > 0 { return false } var selfIndex uint64 for iter := other.Blocks(); iter.Next(); { otherIndex, otherBlock := iter.Value() if len(sba.indices) == 0 { if otherBlock > 0 { return false } continue } if selfIndex >= uint64(len(sba.indices)) { return false } if otherIndex < sba.indices[selfIndex] { if otherBlock > 0 { return false } continue } if otherIndex > sba.indices[selfIndex] { return false } if !sba.blocks[selfIndex].equals(otherBlock) { return false } selfIndex++ } return true }
go
func (sba *sparseBitArray) Equals(other BitArray) bool { if other.Capacity() == 0 && sba.Capacity() > 0 { return false } var selfIndex uint64 for iter := other.Blocks(); iter.Next(); { otherIndex, otherBlock := iter.Value() if len(sba.indices) == 0 { if otherBlock > 0 { return false } continue } if selfIndex >= uint64(len(sba.indices)) { return false } if otherIndex < sba.indices[selfIndex] { if otherBlock > 0 { return false } continue } if otherIndex > sba.indices[selfIndex] { return false } if !sba.blocks[selfIndex].equals(otherBlock) { return false } selfIndex++ } return true }
[ "func", "(", "sba", "*", "sparseBitArray", ")", "Equals", "(", "other", "BitArray", ")", "bool", "{", "if", "other", ".", "Capacity", "(", ")", "==", "0", "&&", "sba", ".", "Capacity", "(", ")", ">", "0", "{", "return", "false", "\n", "}", "\n\n", "var", "selfIndex", "uint64", "\n", "for", "iter", ":=", "other", ".", "Blocks", "(", ")", ";", "iter", ".", "Next", "(", ")", ";", "{", "otherIndex", ",", "otherBlock", ":=", "iter", ".", "Value", "(", ")", "\n", "if", "len", "(", "sba", ".", "indices", ")", "==", "0", "{", "if", "otherBlock", ">", "0", "{", "return", "false", "\n", "}", "\n\n", "continue", "\n", "}", "\n\n", "if", "selfIndex", ">=", "uint64", "(", "len", "(", "sba", ".", "indices", ")", ")", "{", "return", "false", "\n", "}", "\n\n", "if", "otherIndex", "<", "sba", ".", "indices", "[", "selfIndex", "]", "{", "if", "otherBlock", ">", "0", "{", "return", "false", "\n", "}", "\n", "continue", "\n", "}", "\n\n", "if", "otherIndex", ">", "sba", ".", "indices", "[", "selfIndex", "]", "{", "return", "false", "\n", "}", "\n\n", "if", "!", "sba", ".", "blocks", "[", "selfIndex", "]", ".", "equals", "(", "otherBlock", ")", "{", "return", "false", "\n", "}", "\n\n", "selfIndex", "++", "\n", "}", "\n\n", "return", "true", "\n", "}" ]
// Equals returns a bool indicating if the provided bit array // equals this bitarray.
[ "Equals", "returns", "a", "bool", "indicating", "if", "the", "provided", "bit", "array", "equals", "this", "bitarray", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L187-L226
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
Or
func (sba *sparseBitArray) Or(other BitArray) BitArray { if ba, ok := other.(*sparseBitArray); ok { return orSparseWithSparseBitArray(sba, ba) } return orSparseWithDenseBitArray(sba, other.(*bitArray)) }
go
func (sba *sparseBitArray) Or(other BitArray) BitArray { if ba, ok := other.(*sparseBitArray); ok { return orSparseWithSparseBitArray(sba, ba) } return orSparseWithDenseBitArray(sba, other.(*bitArray)) }
[ "func", "(", "sba", "*", "sparseBitArray", ")", "Or", "(", "other", "BitArray", ")", "BitArray", "{", "if", "ba", ",", "ok", ":=", "other", ".", "(", "*", "sparseBitArray", ")", ";", "ok", "{", "return", "orSparseWithSparseBitArray", "(", "sba", ",", "ba", ")", "\n", "}", "\n\n", "return", "orSparseWithDenseBitArray", "(", "sba", ",", "other", ".", "(", "*", "bitArray", ")", ")", "\n", "}" ]
// Or will perform a bitwise or operation with the provided bitarray and // return a new result bitarray.
[ "Or", "will", "perform", "a", "bitwise", "or", "operation", "with", "the", "provided", "bitarray", "and", "return", "a", "new", "result", "bitarray", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L230-L236
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
And
func (sba *sparseBitArray) And(other BitArray) BitArray { if ba, ok := other.(*sparseBitArray); ok { return andSparseWithSparseBitArray(sba, ba) } return andSparseWithDenseBitArray(sba, other.(*bitArray)) }
go
func (sba *sparseBitArray) And(other BitArray) BitArray { if ba, ok := other.(*sparseBitArray); ok { return andSparseWithSparseBitArray(sba, ba) } return andSparseWithDenseBitArray(sba, other.(*bitArray)) }
[ "func", "(", "sba", "*", "sparseBitArray", ")", "And", "(", "other", "BitArray", ")", "BitArray", "{", "if", "ba", ",", "ok", ":=", "other", ".", "(", "*", "sparseBitArray", ")", ";", "ok", "{", "return", "andSparseWithSparseBitArray", "(", "sba", ",", "ba", ")", "\n", "}", "\n\n", "return", "andSparseWithDenseBitArray", "(", "sba", ",", "other", ".", "(", "*", "bitArray", ")", ")", "\n", "}" ]
// And will perform a bitwise and operation with the provided bitarray and // return a new result bitarray.
[ "And", "will", "perform", "a", "bitwise", "and", "operation", "with", "the", "provided", "bitarray", "and", "return", "a", "new", "result", "bitarray", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L240-L246
train
Workiva/go-datastructures
bitarray/sparse_bitarray.go
Intersects
func (sba *sparseBitArray) Intersects(other BitArray) bool { if other.Capacity() == 0 { return true } var selfIndex int64 for iter := other.Blocks(); iter.Next(); { otherI, otherBlock := iter.Value() if len(sba.indices) == 0 { if otherBlock > 0 { return false } continue } // here we grab where the block should live in ourselves i := uintSlice(sba.indices[selfIndex:]).search(otherI) // this is a block we don't have, doesn't intersect if i == int64(len(sba.indices)) { return false } if sba.indices[i] != otherI { return false } if !sba.blocks[i].intersects(otherBlock) { return false } selfIndex = i } return true }
go
func (sba *sparseBitArray) Intersects(other BitArray) bool { if other.Capacity() == 0 { return true } var selfIndex int64 for iter := other.Blocks(); iter.Next(); { otherI, otherBlock := iter.Value() if len(sba.indices) == 0 { if otherBlock > 0 { return false } continue } // here we grab where the block should live in ourselves i := uintSlice(sba.indices[selfIndex:]).search(otherI) // this is a block we don't have, doesn't intersect if i == int64(len(sba.indices)) { return false } if sba.indices[i] != otherI { return false } if !sba.blocks[i].intersects(otherBlock) { return false } selfIndex = i } return true }
[ "func", "(", "sba", "*", "sparseBitArray", ")", "Intersects", "(", "other", "BitArray", ")", "bool", "{", "if", "other", ".", "Capacity", "(", ")", "==", "0", "{", "return", "true", "\n", "}", "\n\n", "var", "selfIndex", "int64", "\n", "for", "iter", ":=", "other", ".", "Blocks", "(", ")", ";", "iter", ".", "Next", "(", ")", ";", "{", "otherI", ",", "otherBlock", ":=", "iter", ".", "Value", "(", ")", "\n", "if", "len", "(", "sba", ".", "indices", ")", "==", "0", "{", "if", "otherBlock", ">", "0", "{", "return", "false", "\n", "}", "\n", "continue", "\n", "}", "\n", "// here we grab where the block should live in ourselves", "i", ":=", "uintSlice", "(", "sba", ".", "indices", "[", "selfIndex", ":", "]", ")", ".", "search", "(", "otherI", ")", "\n", "// this is a block we don't have, doesn't intersect", "if", "i", "==", "int64", "(", "len", "(", "sba", ".", "indices", ")", ")", "{", "return", "false", "\n", "}", "\n\n", "if", "sba", ".", "indices", "[", "i", "]", "!=", "otherI", "{", "return", "false", "\n", "}", "\n\n", "if", "!", "sba", ".", "blocks", "[", "i", "]", ".", "intersects", "(", "otherBlock", ")", "{", "return", "false", "\n", "}", "\n\n", "selfIndex", "=", "i", "\n", "}", "\n\n", "return", "true", "\n", "}" ]
// Intersects returns a bool indicating if the provided bit array // intersects with this bitarray.
[ "Intersects", "returns", "a", "bool", "indicating", "if", "the", "provided", "bit", "array", "intersects", "with", "this", "bitarray", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/sparse_bitarray.go#L277-L310
train
Workiva/go-datastructures
augmentedtree/atree.go
compare
func compare(nodeLow, ivLow int64, nodeID, ivID uint64) int { if ivLow > nodeLow { return 1 } if ivLow < nodeLow { return 0 } return intFromBool(ivID > nodeID) }
go
func compare(nodeLow, ivLow int64, nodeID, ivID uint64) int { if ivLow > nodeLow { return 1 } if ivLow < nodeLow { return 0 } return intFromBool(ivID > nodeID) }
[ "func", "compare", "(", "nodeLow", ",", "ivLow", "int64", ",", "nodeID", ",", "ivID", "uint64", ")", "int", "{", "if", "ivLow", ">", "nodeLow", "{", "return", "1", "\n", "}", "\n\n", "if", "ivLow", "<", "nodeLow", "{", "return", "0", "\n", "}", "\n\n", "return", "intFromBool", "(", "ivID", ">", "nodeID", ")", "\n", "}" ]
// compare returns an int indicating which direction the node // should go.
[ "compare", "returns", "an", "int", "indicating", "which", "direction", "the", "node", "should", "go", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/augmentedtree/atree.go#L43-L53
train
Workiva/go-datastructures
augmentedtree/atree.go
add
func (tree *tree) add(iv Interval) { if tree.root == nil { tree.root = newNode( iv, iv.LowAtDimension(1), iv.HighAtDimension(1), 1, ) tree.root.red = false tree.number++ return } tree.resetDummy() var ( dummy = tree.dummy parent, grandParent *node node = tree.root dir, last int otherLast = 1 id = iv.ID() max = iv.HighAtDimension(1) ivLow = iv.LowAtDimension(1) helper = &dummy ) // set this AFTER clearing dummy helper.children[1] = tree.root for { if node == nil { node = newNode(iv, ivLow, max, 1) parent.children[dir] = node tree.number++ } else if isRed(node.children[0]) && isRed(node.children[1]) { node.red = true node.children[0].red = false node.children[1].red = false } if max > node.max { node.max = max } if ivLow < node.min { node.min = ivLow } if isRed(parent) && isRed(node) { localDir := intFromBool(helper.children[1] == grandParent) if node == parent.children[last] { helper.children[localDir] = rotate(grandParent, otherLast) } else { helper.children[localDir] = doubleRotate(grandParent, otherLast) } } if node.id == id { break } last = dir otherLast = takeOpposite(last) dir = compare(node.interval.LowAtDimension(1), ivLow, node.id, id) if grandParent != nil { helper = grandParent } grandParent, parent, node = parent, node, node.children[dir] } tree.root = dummy.children[1] tree.root.red = false }
go
func (tree *tree) add(iv Interval) { if tree.root == nil { tree.root = newNode( iv, iv.LowAtDimension(1), iv.HighAtDimension(1), 1, ) tree.root.red = false tree.number++ return } tree.resetDummy() var ( dummy = tree.dummy parent, grandParent *node node = tree.root dir, last int otherLast = 1 id = iv.ID() max = iv.HighAtDimension(1) ivLow = iv.LowAtDimension(1) helper = &dummy ) // set this AFTER clearing dummy helper.children[1] = tree.root for { if node == nil { node = newNode(iv, ivLow, max, 1) parent.children[dir] = node tree.number++ } else if isRed(node.children[0]) && isRed(node.children[1]) { node.red = true node.children[0].red = false node.children[1].red = false } if max > node.max { node.max = max } if ivLow < node.min { node.min = ivLow } if isRed(parent) && isRed(node) { localDir := intFromBool(helper.children[1] == grandParent) if node == parent.children[last] { helper.children[localDir] = rotate(grandParent, otherLast) } else { helper.children[localDir] = doubleRotate(grandParent, otherLast) } } if node.id == id { break } last = dir otherLast = takeOpposite(last) dir = compare(node.interval.LowAtDimension(1), ivLow, node.id, id) if grandParent != nil { helper = grandParent } grandParent, parent, node = parent, node, node.children[dir] } tree.root = dummy.children[1] tree.root.red = false }
[ "func", "(", "tree", "*", "tree", ")", "add", "(", "iv", "Interval", ")", "{", "if", "tree", ".", "root", "==", "nil", "{", "tree", ".", "root", "=", "newNode", "(", "iv", ",", "iv", ".", "LowAtDimension", "(", "1", ")", ",", "iv", ".", "HighAtDimension", "(", "1", ")", ",", "1", ",", ")", "\n", "tree", ".", "root", ".", "red", "=", "false", "\n", "tree", ".", "number", "++", "\n", "return", "\n", "}", "\n\n", "tree", ".", "resetDummy", "(", ")", "\n", "var", "(", "dummy", "=", "tree", ".", "dummy", "\n", "parent", ",", "grandParent", "*", "node", "\n", "node", "=", "tree", ".", "root", "\n", "dir", ",", "last", "int", "\n", "otherLast", "=", "1", "\n", "id", "=", "iv", ".", "ID", "(", ")", "\n", "max", "=", "iv", ".", "HighAtDimension", "(", "1", ")", "\n", "ivLow", "=", "iv", ".", "LowAtDimension", "(", "1", ")", "\n", "helper", "=", "&", "dummy", "\n", ")", "\n\n", "// set this AFTER clearing dummy", "helper", ".", "children", "[", "1", "]", "=", "tree", ".", "root", "\n", "for", "{", "if", "node", "==", "nil", "{", "node", "=", "newNode", "(", "iv", ",", "ivLow", ",", "max", ",", "1", ")", "\n", "parent", ".", "children", "[", "dir", "]", "=", "node", "\n", "tree", ".", "number", "++", "\n", "}", "else", "if", "isRed", "(", "node", ".", "children", "[", "0", "]", ")", "&&", "isRed", "(", "node", ".", "children", "[", "1", "]", ")", "{", "node", ".", "red", "=", "true", "\n", "node", ".", "children", "[", "0", "]", ".", "red", "=", "false", "\n", "node", ".", "children", "[", "1", "]", ".", "red", "=", "false", "\n", "}", "\n", "if", "max", ">", "node", ".", "max", "{", "node", ".", "max", "=", "max", "\n", "}", "\n\n", "if", "ivLow", "<", "node", ".", "min", "{", "node", ".", "min", "=", "ivLow", "\n", "}", "\n\n", "if", "isRed", "(", "parent", ")", "&&", "isRed", "(", "node", ")", "{", "localDir", ":=", "intFromBool", "(", "helper", ".", "children", "[", "1", "]", "==", "grandParent", ")", "\n\n", "if", "node", "==", "parent", ".", "children", "[", "last", "]", "{", "helper", ".", "children", "[", "localDir", "]", "=", "rotate", "(", "grandParent", ",", "otherLast", ")", "\n", "}", "else", "{", "helper", ".", "children", "[", "localDir", "]", "=", "doubleRotate", "(", "grandParent", ",", "otherLast", ")", "\n", "}", "\n", "}", "\n\n", "if", "node", ".", "id", "==", "id", "{", "break", "\n", "}", "\n\n", "last", "=", "dir", "\n", "otherLast", "=", "takeOpposite", "(", "last", ")", "\n", "dir", "=", "compare", "(", "node", ".", "interval", ".", "LowAtDimension", "(", "1", ")", ",", "ivLow", ",", "node", ".", "id", ",", "id", ")", "\n\n", "if", "grandParent", "!=", "nil", "{", "helper", "=", "grandParent", "\n", "}", "\n", "grandParent", ",", "parent", ",", "node", "=", "parent", ",", "node", ",", "node", ".", "children", "[", "dir", "]", "\n", "}", "\n\n", "tree", ".", "root", "=", "dummy", ".", "children", "[", "1", "]", "\n", "tree", ".", "root", ".", "red", "=", "false", "\n", "}" ]
// add will add the provided interval to the tree.
[ "add", "will", "add", "the", "provided", "interval", "to", "the", "tree", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/augmentedtree/atree.go#L148-L219
train
Workiva/go-datastructures
augmentedtree/atree.go
Add
func (tree *tree) Add(intervals ...Interval) { for _, iv := range intervals { tree.add(iv) } }
go
func (tree *tree) Add(intervals ...Interval) { for _, iv := range intervals { tree.add(iv) } }
[ "func", "(", "tree", "*", "tree", ")", "Add", "(", "intervals", "...", "Interval", ")", "{", "for", "_", ",", "iv", ":=", "range", "intervals", "{", "tree", ".", "add", "(", "iv", ")", "\n", "}", "\n", "}" ]
// Add will add the provided intervals to this tree.
[ "Add", "will", "add", "the", "provided", "intervals", "to", "this", "tree", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/augmentedtree/atree.go#L222-L226
train
Workiva/go-datastructures
augmentedtree/atree.go
Delete
func (tree *tree) Delete(intervals ...Interval) { for _, iv := range intervals { tree.delete(iv) } if tree.root != nil { tree.root.adjustRanges() } }
go
func (tree *tree) Delete(intervals ...Interval) { for _, iv := range intervals { tree.delete(iv) } if tree.root != nil { tree.root.adjustRanges() } }
[ "func", "(", "tree", "*", "tree", ")", "Delete", "(", "intervals", "...", "Interval", ")", "{", "for", "_", ",", "iv", ":=", "range", "intervals", "{", "tree", ".", "delete", "(", "iv", ")", "\n", "}", "\n", "if", "tree", ".", "root", "!=", "nil", "{", "tree", ".", "root", ".", "adjustRanges", "(", ")", "\n", "}", "\n", "}" ]
// Delete will remove the provided intervals from this tree.
[ "Delete", "will", "remove", "the", "provided", "intervals", "from", "this", "tree", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/augmentedtree/atree.go#L310-L317
train
Workiva/go-datastructures
augmentedtree/atree.go
Query
func (tree *tree) Query(interval Interval) Intervals { if tree.root == nil { return nil } var ( Intervals = intervalsPool.Get().(Intervals) ivLow = interval.LowAtDimension(1) ivHigh = interval.HighAtDimension(1) ) tree.root.query(ivLow, ivHigh, interval, tree.maxDimension, func(node *node) { Intervals = append(Intervals, node.interval) }) return Intervals }
go
func (tree *tree) Query(interval Interval) Intervals { if tree.root == nil { return nil } var ( Intervals = intervalsPool.Get().(Intervals) ivLow = interval.LowAtDimension(1) ivHigh = interval.HighAtDimension(1) ) tree.root.query(ivLow, ivHigh, interval, tree.maxDimension, func(node *node) { Intervals = append(Intervals, node.interval) }) return Intervals }
[ "func", "(", "tree", "*", "tree", ")", "Query", "(", "interval", "Interval", ")", "Intervals", "{", "if", "tree", ".", "root", "==", "nil", "{", "return", "nil", "\n", "}", "\n\n", "var", "(", "Intervals", "=", "intervalsPool", ".", "Get", "(", ")", ".", "(", "Intervals", ")", "\n", "ivLow", "=", "interval", ".", "LowAtDimension", "(", "1", ")", "\n", "ivHigh", "=", "interval", ".", "HighAtDimension", "(", "1", ")", "\n", ")", "\n\n", "tree", ".", "root", ".", "query", "(", "ivLow", ",", "ivHigh", ",", "interval", ",", "tree", ".", "maxDimension", ",", "func", "(", "node", "*", "node", ")", "{", "Intervals", "=", "append", "(", "Intervals", ",", "node", ".", "interval", ")", "\n", "}", ")", "\n\n", "return", "Intervals", "\n", "}" ]
// Query will return a list of intervals that intersect the provided // interval. The provided interval's ID method is ignored so the // provided ID is irrelevant.
[ "Query", "will", "return", "a", "list", "of", "intervals", "that", "intersect", "the", "provided", "interval", ".", "The", "provided", "interval", "s", "ID", "method", "is", "ignored", "so", "the", "provided", "ID", "is", "irrelevant", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/augmentedtree/atree.go#L322-L338
train
Workiva/go-datastructures
fibheap/fibheap.go
Enqueue
func (heap *FloatingFibonacciHeap) Enqueue(priority float64) *Entry { singleton := newEntry(priority) // Merge singleton list with heap heap.min = mergeLists(heap.min, singleton) heap.size++ return singleton }
go
func (heap *FloatingFibonacciHeap) Enqueue(priority float64) *Entry { singleton := newEntry(priority) // Merge singleton list with heap heap.min = mergeLists(heap.min, singleton) heap.size++ return singleton }
[ "func", "(", "heap", "*", "FloatingFibonacciHeap", ")", "Enqueue", "(", "priority", "float64", ")", "*", "Entry", "{", "singleton", ":=", "newEntry", "(", "priority", ")", "\n\n", "// Merge singleton list with heap", "heap", ".", "min", "=", "mergeLists", "(", "heap", ".", "min", ",", "singleton", ")", "\n", "heap", ".", "size", "++", "\n", "return", "singleton", "\n", "}" ]
// Enqueue adds and element to the heap
[ "Enqueue", "adds", "and", "element", "to", "the", "heap" ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/fibheap/fibheap.go#L118-L125
train
Workiva/go-datastructures
fibheap/fibheap.go
Min
func (heap *FloatingFibonacciHeap) Min() (*Entry, error) { if heap.IsEmpty() { return nil, EmptyHeapError("Trying to get minimum element of empty heap") } return heap.min, nil }
go
func (heap *FloatingFibonacciHeap) Min() (*Entry, error) { if heap.IsEmpty() { return nil, EmptyHeapError("Trying to get minimum element of empty heap") } return heap.min, nil }
[ "func", "(", "heap", "*", "FloatingFibonacciHeap", ")", "Min", "(", ")", "(", "*", "Entry", ",", "error", ")", "{", "if", "heap", ".", "IsEmpty", "(", ")", "{", "return", "nil", ",", "EmptyHeapError", "(", "\"", "\"", ")", "\n", "}", "\n", "return", "heap", ".", "min", ",", "nil", "\n", "}" ]
// Min returns the minimum element in the heap
[ "Min", "returns", "the", "minimum", "element", "in", "the", "heap" ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/fibheap/fibheap.go#L128-L133
train
Workiva/go-datastructures
fibheap/fibheap.go
DequeueMin
func (heap *FloatingFibonacciHeap) DequeueMin() (*Entry, error) { if heap.IsEmpty() { return nil, EmptyHeapError("Cannot dequeue minimum of empty heap") } heap.size-- // Copy pointer. Will need it later. min := heap.min if min.next == min { // This is the only root node heap.min = nil } else { // There are more root nodes heap.min.prev.next = heap.min.next heap.min.next.prev = heap.min.prev heap.min = heap.min.next // Arbitrary element of the root list } if min.child != nil { // Keep track of the first visited node curr := min.child for ok := true; ok; ok = (curr != min.child) { curr.parent = nil curr = curr.next } } heap.min = mergeLists(heap.min, min.child) if heap.min == nil { // If there are no entries left, we're done. return min, nil } treeSlice := make([]*Entry, 0, heap.size) toVisit := make([]*Entry, 0, heap.size) for curr := heap.min; len(toVisit) == 0 || toVisit[0] != curr; curr = curr.next { toVisit = append(toVisit, curr) } for _, curr := range toVisit { for { for curr.degree >= len(treeSlice) { treeSlice = append(treeSlice, nil) } if treeSlice[curr.degree] == nil { treeSlice[curr.degree] = curr break } other := treeSlice[curr.degree] treeSlice[curr.degree] = nil // Determine which of two trees has the smaller root var minT, maxT *Entry if other.Priority < curr.Priority { minT = other maxT = curr } else { minT = curr maxT = other } // Break max out of the root list, // then merge it into min's child list maxT.next.prev = maxT.prev maxT.prev.next = maxT.next // Make it a singleton so that we can merge it maxT.prev = maxT maxT.next = maxT minT.child = mergeLists(minT.child, maxT) // Reparent max appropriately maxT.parent = minT // Clear max's mark, since it can now lose another child maxT.marked = false // Increase min's degree. It has another child. minT.degree++ // Continue merging this tree curr = minT } /* Update the global min based on this node. Note that we compare * for <= instead of < here. That's because if we just did a * reparent operation that merged two different trees of equal * priority, we need to make sure that the min pointer points to * the root-level one. */ if curr.Priority <= heap.min.Priority { heap.min = curr } } return min, nil }
go
func (heap *FloatingFibonacciHeap) DequeueMin() (*Entry, error) { if heap.IsEmpty() { return nil, EmptyHeapError("Cannot dequeue minimum of empty heap") } heap.size-- // Copy pointer. Will need it later. min := heap.min if min.next == min { // This is the only root node heap.min = nil } else { // There are more root nodes heap.min.prev.next = heap.min.next heap.min.next.prev = heap.min.prev heap.min = heap.min.next // Arbitrary element of the root list } if min.child != nil { // Keep track of the first visited node curr := min.child for ok := true; ok; ok = (curr != min.child) { curr.parent = nil curr = curr.next } } heap.min = mergeLists(heap.min, min.child) if heap.min == nil { // If there are no entries left, we're done. return min, nil } treeSlice := make([]*Entry, 0, heap.size) toVisit := make([]*Entry, 0, heap.size) for curr := heap.min; len(toVisit) == 0 || toVisit[0] != curr; curr = curr.next { toVisit = append(toVisit, curr) } for _, curr := range toVisit { for { for curr.degree >= len(treeSlice) { treeSlice = append(treeSlice, nil) } if treeSlice[curr.degree] == nil { treeSlice[curr.degree] = curr break } other := treeSlice[curr.degree] treeSlice[curr.degree] = nil // Determine which of two trees has the smaller root var minT, maxT *Entry if other.Priority < curr.Priority { minT = other maxT = curr } else { minT = curr maxT = other } // Break max out of the root list, // then merge it into min's child list maxT.next.prev = maxT.prev maxT.prev.next = maxT.next // Make it a singleton so that we can merge it maxT.prev = maxT maxT.next = maxT minT.child = mergeLists(minT.child, maxT) // Reparent max appropriately maxT.parent = minT // Clear max's mark, since it can now lose another child maxT.marked = false // Increase min's degree. It has another child. minT.degree++ // Continue merging this tree curr = minT } /* Update the global min based on this node. Note that we compare * for <= instead of < here. That's because if we just did a * reparent operation that merged two different trees of equal * priority, we need to make sure that the min pointer points to * the root-level one. */ if curr.Priority <= heap.min.Priority { heap.min = curr } } return min, nil }
[ "func", "(", "heap", "*", "FloatingFibonacciHeap", ")", "DequeueMin", "(", ")", "(", "*", "Entry", ",", "error", ")", "{", "if", "heap", ".", "IsEmpty", "(", ")", "{", "return", "nil", ",", "EmptyHeapError", "(", "\"", "\"", ")", "\n", "}", "\n\n", "heap", ".", "size", "--", "\n\n", "// Copy pointer. Will need it later.", "min", ":=", "heap", ".", "min", "\n\n", "if", "min", ".", "next", "==", "min", "{", "// This is the only root node", "heap", ".", "min", "=", "nil", "\n", "}", "else", "{", "// There are more root nodes", "heap", ".", "min", ".", "prev", ".", "next", "=", "heap", ".", "min", ".", "next", "\n", "heap", ".", "min", ".", "next", ".", "prev", "=", "heap", ".", "min", ".", "prev", "\n", "heap", ".", "min", "=", "heap", ".", "min", ".", "next", "// Arbitrary element of the root list", "\n", "}", "\n\n", "if", "min", ".", "child", "!=", "nil", "{", "// Keep track of the first visited node", "curr", ":=", "min", ".", "child", "\n", "for", "ok", ":=", "true", ";", "ok", ";", "ok", "=", "(", "curr", "!=", "min", ".", "child", ")", "{", "curr", ".", "parent", "=", "nil", "\n", "curr", "=", "curr", ".", "next", "\n", "}", "\n", "}", "\n\n", "heap", ".", "min", "=", "mergeLists", "(", "heap", ".", "min", ",", "min", ".", "child", ")", "\n\n", "if", "heap", ".", "min", "==", "nil", "{", "// If there are no entries left, we're done.", "return", "min", ",", "nil", "\n", "}", "\n\n", "treeSlice", ":=", "make", "(", "[", "]", "*", "Entry", ",", "0", ",", "heap", ".", "size", ")", "\n", "toVisit", ":=", "make", "(", "[", "]", "*", "Entry", ",", "0", ",", "heap", ".", "size", ")", "\n\n", "for", "curr", ":=", "heap", ".", "min", ";", "len", "(", "toVisit", ")", "==", "0", "||", "toVisit", "[", "0", "]", "!=", "curr", ";", "curr", "=", "curr", ".", "next", "{", "toVisit", "=", "append", "(", "toVisit", ",", "curr", ")", "\n", "}", "\n\n", "for", "_", ",", "curr", ":=", "range", "toVisit", "{", "for", "{", "for", "curr", ".", "degree", ">=", "len", "(", "treeSlice", ")", "{", "treeSlice", "=", "append", "(", "treeSlice", ",", "nil", ")", "\n", "}", "\n\n", "if", "treeSlice", "[", "curr", ".", "degree", "]", "==", "nil", "{", "treeSlice", "[", "curr", ".", "degree", "]", "=", "curr", "\n", "break", "\n", "}", "\n\n", "other", ":=", "treeSlice", "[", "curr", ".", "degree", "]", "\n", "treeSlice", "[", "curr", ".", "degree", "]", "=", "nil", "\n\n", "// Determine which of two trees has the smaller root", "var", "minT", ",", "maxT", "*", "Entry", "\n", "if", "other", ".", "Priority", "<", "curr", ".", "Priority", "{", "minT", "=", "other", "\n", "maxT", "=", "curr", "\n", "}", "else", "{", "minT", "=", "curr", "\n", "maxT", "=", "other", "\n", "}", "\n\n", "// Break max out of the root list,", "// then merge it into min's child list", "maxT", ".", "next", ".", "prev", "=", "maxT", ".", "prev", "\n", "maxT", ".", "prev", ".", "next", "=", "maxT", ".", "next", "\n\n", "// Make it a singleton so that we can merge it", "maxT", ".", "prev", "=", "maxT", "\n", "maxT", ".", "next", "=", "maxT", "\n", "minT", ".", "child", "=", "mergeLists", "(", "minT", ".", "child", ",", "maxT", ")", "\n\n", "// Reparent max appropriately", "maxT", ".", "parent", "=", "minT", "\n\n", "// Clear max's mark, since it can now lose another child", "maxT", ".", "marked", "=", "false", "\n\n", "// Increase min's degree. It has another child.", "minT", ".", "degree", "++", "\n\n", "// Continue merging this tree", "curr", "=", "minT", "\n", "}", "\n\n", "/* Update the global min based on this node. Note that we compare\n\t\t * for <= instead of < here. That's because if we just did a\n\t\t * reparent operation that merged two different trees of equal\n\t\t * priority, we need to make sure that the min pointer points to\n\t\t * the root-level one.\n\t\t */", "if", "curr", ".", "Priority", "<=", "heap", ".", "min", ".", "Priority", "{", "heap", ".", "min", "=", "curr", "\n", "}", "\n", "}", "\n\n", "return", "min", ",", "nil", "\n", "}" ]
// DequeueMin removes and returns the // minimal element in the heap
[ "DequeueMin", "removes", "and", "returns", "the", "minimal", "element", "in", "the", "heap" ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/fibheap/fibheap.go#L147-L247
train
Workiva/go-datastructures
fibheap/fibheap.go
DecreaseKey
func (heap *FloatingFibonacciHeap) DecreaseKey(node *Entry, newPriority float64) (*Entry, error) { if heap.IsEmpty() { return nil, EmptyHeapError("Cannot decrease key in an empty heap") } if node == nil { return nil, NilError("Cannot decrease key: given node is nil") } if newPriority >= node.Priority { return nil, fmt.Errorf("The given new priority: %v, is larger than or equal to the old: %v", newPriority, node.Priority) } decreaseKeyUnchecked(heap, node, newPriority) return node, nil }
go
func (heap *FloatingFibonacciHeap) DecreaseKey(node *Entry, newPriority float64) (*Entry, error) { if heap.IsEmpty() { return nil, EmptyHeapError("Cannot decrease key in an empty heap") } if node == nil { return nil, NilError("Cannot decrease key: given node is nil") } if newPriority >= node.Priority { return nil, fmt.Errorf("The given new priority: %v, is larger than or equal to the old: %v", newPriority, node.Priority) } decreaseKeyUnchecked(heap, node, newPriority) return node, nil }
[ "func", "(", "heap", "*", "FloatingFibonacciHeap", ")", "DecreaseKey", "(", "node", "*", "Entry", ",", "newPriority", "float64", ")", "(", "*", "Entry", ",", "error", ")", "{", "if", "heap", ".", "IsEmpty", "(", ")", "{", "return", "nil", ",", "EmptyHeapError", "(", "\"", "\"", ")", "\n", "}", "\n\n", "if", "node", "==", "nil", "{", "return", "nil", ",", "NilError", "(", "\"", "\"", ")", "\n", "}", "\n\n", "if", "newPriority", ">=", "node", ".", "Priority", "{", "return", "nil", ",", "fmt", ".", "Errorf", "(", "\"", "\"", ",", "newPriority", ",", "node", ".", "Priority", ")", "\n", "}", "\n\n", "decreaseKeyUnchecked", "(", "heap", ",", "node", ",", "newPriority", ")", "\n", "return", "node", ",", "nil", "\n", "}" ]
// DecreaseKey decreases the key of the given element, sets it to the new // given priority and returns the node if successfully set
[ "DecreaseKey", "decreases", "the", "key", "of", "the", "given", "element", "sets", "it", "to", "the", "new", "given", "priority", "and", "returns", "the", "node", "if", "successfully", "set" ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/fibheap/fibheap.go#L251-L268
train
Workiva/go-datastructures
fibheap/fibheap.go
Delete
func (heap *FloatingFibonacciHeap) Delete(node *Entry) error { if heap.IsEmpty() { return EmptyHeapError("Cannot delete element from an empty heap") } if node == nil { return NilError("Cannot delete node: given node is nil") } decreaseKeyUnchecked(heap, node, -math.MaxFloat64) heap.DequeueMin() return nil }
go
func (heap *FloatingFibonacciHeap) Delete(node *Entry) error { if heap.IsEmpty() { return EmptyHeapError("Cannot delete element from an empty heap") } if node == nil { return NilError("Cannot delete node: given node is nil") } decreaseKeyUnchecked(heap, node, -math.MaxFloat64) heap.DequeueMin() return nil }
[ "func", "(", "heap", "*", "FloatingFibonacciHeap", ")", "Delete", "(", "node", "*", "Entry", ")", "error", "{", "if", "heap", ".", "IsEmpty", "(", ")", "{", "return", "EmptyHeapError", "(", "\"", "\"", ")", "\n", "}", "\n\n", "if", "node", "==", "nil", "{", "return", "NilError", "(", "\"", "\"", ")", "\n", "}", "\n\n", "decreaseKeyUnchecked", "(", "heap", ",", "node", ",", "-", "math", ".", "MaxFloat64", ")", "\n", "heap", ".", "DequeueMin", "(", ")", "\n", "return", "nil", "\n", "}" ]
// Delete deletes the given element in the heap
[ "Delete", "deletes", "the", "given", "element", "in", "the", "heap" ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/fibheap/fibheap.go#L271-L284
train
Workiva/go-datastructures
fibheap/fibheap.go
Merge
func (heap *FloatingFibonacciHeap) Merge(other *FloatingFibonacciHeap) (FloatingFibonacciHeap, error) { if heap == nil || other == nil { return FloatingFibonacciHeap{}, NilError("One of the heaps to merge is nil. Cannot merge") } resultSize := heap.size + other.size resultMin := mergeLists(heap.min, other.min) heap.min = nil other.min = nil heap.size = 0 other.size = 0 return FloatingFibonacciHeap{resultMin, resultSize}, nil }
go
func (heap *FloatingFibonacciHeap) Merge(other *FloatingFibonacciHeap) (FloatingFibonacciHeap, error) { if heap == nil || other == nil { return FloatingFibonacciHeap{}, NilError("One of the heaps to merge is nil. Cannot merge") } resultSize := heap.size + other.size resultMin := mergeLists(heap.min, other.min) heap.min = nil other.min = nil heap.size = 0 other.size = 0 return FloatingFibonacciHeap{resultMin, resultSize}, nil }
[ "func", "(", "heap", "*", "FloatingFibonacciHeap", ")", "Merge", "(", "other", "*", "FloatingFibonacciHeap", ")", "(", "FloatingFibonacciHeap", ",", "error", ")", "{", "if", "heap", "==", "nil", "||", "other", "==", "nil", "{", "return", "FloatingFibonacciHeap", "{", "}", ",", "NilError", "(", "\"", "\"", ")", "\n", "}", "\n\n", "resultSize", ":=", "heap", ".", "size", "+", "other", ".", "size", "\n\n", "resultMin", ":=", "mergeLists", "(", "heap", ".", "min", ",", "other", ".", "min", ")", "\n\n", "heap", ".", "min", "=", "nil", "\n", "other", ".", "min", "=", "nil", "\n", "heap", ".", "size", "=", "0", "\n", "other", ".", "size", "=", "0", "\n\n", "return", "FloatingFibonacciHeap", "{", "resultMin", ",", "resultSize", "}", ",", "nil", "\n", "}" ]
// Merge returns a new Fibonacci heap that contains // all of the elements of the two heaps. Each of the input heaps is // destructively modified by having all its elements removed. You can // continue to use those heaps, but be aware that they will be empty // after this call completes.
[ "Merge", "returns", "a", "new", "Fibonacci", "heap", "that", "contains", "all", "of", "the", "elements", "of", "the", "two", "heaps", ".", "Each", "of", "the", "input", "heaps", "is", "destructively", "modified", "by", "having", "all", "its", "elements", "removed", ".", "You", "can", "continue", "to", "use", "those", "heaps", "but", "be", "aware", "that", "they", "will", "be", "empty", "after", "this", "call", "completes", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/fibheap/fibheap.go#L291-L307
train
Workiva/go-datastructures
batcher/batcher.go
Put
func (b *basicBatcher) Put(item interface{}) error { b.lock.Lock() if b.disposed { b.lock.Unlock() return ErrDisposed } b.items = append(b.items, item) if b.calculateBytes != nil { b.availableBytes += b.calculateBytes(item) } if b.ready() { // To guarantee ordering this MUST be in the lock, otherwise multiple // flush calls could be blocked at the same time, in which case // there's no guarantee each batch is placed into the channel in // the proper order b.flush() } b.lock.Unlock() return nil }
go
func (b *basicBatcher) Put(item interface{}) error { b.lock.Lock() if b.disposed { b.lock.Unlock() return ErrDisposed } b.items = append(b.items, item) if b.calculateBytes != nil { b.availableBytes += b.calculateBytes(item) } if b.ready() { // To guarantee ordering this MUST be in the lock, otherwise multiple // flush calls could be blocked at the same time, in which case // there's no guarantee each batch is placed into the channel in // the proper order b.flush() } b.lock.Unlock() return nil }
[ "func", "(", "b", "*", "basicBatcher", ")", "Put", "(", "item", "interface", "{", "}", ")", "error", "{", "b", ".", "lock", ".", "Lock", "(", ")", "\n", "if", "b", ".", "disposed", "{", "b", ".", "lock", ".", "Unlock", "(", ")", "\n", "return", "ErrDisposed", "\n", "}", "\n\n", "b", ".", "items", "=", "append", "(", "b", ".", "items", ",", "item", ")", "\n", "if", "b", ".", "calculateBytes", "!=", "nil", "{", "b", ".", "availableBytes", "+=", "b", ".", "calculateBytes", "(", "item", ")", "\n", "}", "\n", "if", "b", ".", "ready", "(", ")", "{", "// To guarantee ordering this MUST be in the lock, otherwise multiple", "// flush calls could be blocked at the same time, in which case", "// there's no guarantee each batch is placed into the channel in", "// the proper order", "b", ".", "flush", "(", ")", "\n", "}", "\n\n", "b", ".", "lock", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "}" ]
// Put adds items to the batcher.
[ "Put", "adds", "items", "to", "the", "batcher", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/batcher/batcher.go#L121-L142
train
Workiva/go-datastructures
batcher/batcher.go
Get
func (b *basicBatcher) Get() ([]interface{}, error) { // Don't check disposed yet so any items remaining in the queue // will be returned properly. var timeout <-chan time.Time if b.maxTime > 0 { timeout = time.After(b.maxTime) } select { case items, ok := <-b.batchChan: // If there's something on the batch channel, we definitely want that. if !ok { return nil, ErrDisposed } return items, nil case <-timeout: // It's possible something was added to the channel after something // was received on the timeout channel, in which case that must // be returned first to satisfy our ordering guarantees. // We can't just grab the lock here in case the batch channel is full, // in which case a Put or Flush will be blocked and holding // onto the lock. In that case, there should be something on the // batch channel for { if b.lock.TryLock() { // We have a lock, try to read from channel first in case // something snuck in select { case items, ok := <-b.batchChan: b.lock.Unlock() if !ok { return nil, ErrDisposed } return items, nil default: } // If that is unsuccessful, nothing was added to the channel, // and the temp buffer can't have changed because of the lock, // so grab that items := b.items b.items = make([]interface{}, 0, b.maxItems) b.availableBytes = 0 b.lock.Unlock() return items, nil } else { // If we didn't get a lock, there are two cases: // 1) The batch chan is full. // 2) A Put or Flush temporarily has the lock. // In either case, trying to read something off the batch chan, // and going back to trying to get a lock if unsuccessful // works. select { case items, ok := <-b.batchChan: if !ok { return nil, ErrDisposed } return items, nil default: } } } } }
go
func (b *basicBatcher) Get() ([]interface{}, error) { // Don't check disposed yet so any items remaining in the queue // will be returned properly. var timeout <-chan time.Time if b.maxTime > 0 { timeout = time.After(b.maxTime) } select { case items, ok := <-b.batchChan: // If there's something on the batch channel, we definitely want that. if !ok { return nil, ErrDisposed } return items, nil case <-timeout: // It's possible something was added to the channel after something // was received on the timeout channel, in which case that must // be returned first to satisfy our ordering guarantees. // We can't just grab the lock here in case the batch channel is full, // in which case a Put or Flush will be blocked and holding // onto the lock. In that case, there should be something on the // batch channel for { if b.lock.TryLock() { // We have a lock, try to read from channel first in case // something snuck in select { case items, ok := <-b.batchChan: b.lock.Unlock() if !ok { return nil, ErrDisposed } return items, nil default: } // If that is unsuccessful, nothing was added to the channel, // and the temp buffer can't have changed because of the lock, // so grab that items := b.items b.items = make([]interface{}, 0, b.maxItems) b.availableBytes = 0 b.lock.Unlock() return items, nil } else { // If we didn't get a lock, there are two cases: // 1) The batch chan is full. // 2) A Put or Flush temporarily has the lock. // In either case, trying to read something off the batch chan, // and going back to trying to get a lock if unsuccessful // works. select { case items, ok := <-b.batchChan: if !ok { return nil, ErrDisposed } return items, nil default: } } } } }
[ "func", "(", "b", "*", "basicBatcher", ")", "Get", "(", ")", "(", "[", "]", "interface", "{", "}", ",", "error", ")", "{", "// Don't check disposed yet so any items remaining in the queue", "// will be returned properly.", "var", "timeout", "<-", "chan", "time", ".", "Time", "\n", "if", "b", ".", "maxTime", ">", "0", "{", "timeout", "=", "time", ".", "After", "(", "b", ".", "maxTime", ")", "\n", "}", "\n\n", "select", "{", "case", "items", ",", "ok", ":=", "<-", "b", ".", "batchChan", ":", "// If there's something on the batch channel, we definitely want that.", "if", "!", "ok", "{", "return", "nil", ",", "ErrDisposed", "\n", "}", "\n", "return", "items", ",", "nil", "\n", "case", "<-", "timeout", ":", "// It's possible something was added to the channel after something", "// was received on the timeout channel, in which case that must", "// be returned first to satisfy our ordering guarantees.", "// We can't just grab the lock here in case the batch channel is full,", "// in which case a Put or Flush will be blocked and holding", "// onto the lock. In that case, there should be something on the", "// batch channel", "for", "{", "if", "b", ".", "lock", ".", "TryLock", "(", ")", "{", "// We have a lock, try to read from channel first in case", "// something snuck in", "select", "{", "case", "items", ",", "ok", ":=", "<-", "b", ".", "batchChan", ":", "b", ".", "lock", ".", "Unlock", "(", ")", "\n", "if", "!", "ok", "{", "return", "nil", ",", "ErrDisposed", "\n", "}", "\n", "return", "items", ",", "nil", "\n", "default", ":", "}", "\n\n", "// If that is unsuccessful, nothing was added to the channel,", "// and the temp buffer can't have changed because of the lock,", "// so grab that", "items", ":=", "b", ".", "items", "\n", "b", ".", "items", "=", "make", "(", "[", "]", "interface", "{", "}", ",", "0", ",", "b", ".", "maxItems", ")", "\n", "b", ".", "availableBytes", "=", "0", "\n", "b", ".", "lock", ".", "Unlock", "(", ")", "\n", "return", "items", ",", "nil", "\n", "}", "else", "{", "// If we didn't get a lock, there are two cases:", "// 1) The batch chan is full.", "// 2) A Put or Flush temporarily has the lock.", "// In either case, trying to read something off the batch chan,", "// and going back to trying to get a lock if unsuccessful", "// works.", "select", "{", "case", "items", ",", "ok", ":=", "<-", "b", ".", "batchChan", ":", "if", "!", "ok", "{", "return", "nil", ",", "ErrDisposed", "\n", "}", "\n", "return", "items", ",", "nil", "\n", "default", ":", "}", "\n", "}", "\n", "}", "\n", "}", "\n", "}" ]
// Get retrieves a batch from the batcher. This call will block until // one of the conditions for a "complete" batch is reached.
[ "Get", "retrieves", "a", "batch", "from", "the", "batcher", ".", "This", "call", "will", "block", "until", "one", "of", "the", "conditions", "for", "a", "complete", "batch", "is", "reached", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/batcher/batcher.go#L146-L210
train
Workiva/go-datastructures
batcher/batcher.go
Flush
func (b *basicBatcher) Flush() error { // This is the same pattern as a Put b.lock.Lock() if b.disposed { b.lock.Unlock() return ErrDisposed } b.flush() b.lock.Unlock() return nil }
go
func (b *basicBatcher) Flush() error { // This is the same pattern as a Put b.lock.Lock() if b.disposed { b.lock.Unlock() return ErrDisposed } b.flush() b.lock.Unlock() return nil }
[ "func", "(", "b", "*", "basicBatcher", ")", "Flush", "(", ")", "error", "{", "// This is the same pattern as a Put", "b", ".", "lock", ".", "Lock", "(", ")", "\n", "if", "b", ".", "disposed", "{", "b", ".", "lock", ".", "Unlock", "(", ")", "\n", "return", "ErrDisposed", "\n", "}", "\n", "b", ".", "flush", "(", ")", "\n", "b", ".", "lock", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "}" ]
// Flush forcibly completes the batch currently being built
[ "Flush", "forcibly", "completes", "the", "batch", "currently", "being", "built" ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/batcher/batcher.go#L213-L223
train
Workiva/go-datastructures
batcher/batcher.go
Dispose
func (b *basicBatcher) Dispose() { for { if b.lock.TryLock() { // We've got a lock if b.disposed { b.lock.Unlock() return } b.disposed = true b.items = nil b.drainBatchChan() close(b.batchChan) b.lock.Unlock() } else { // Two cases here: // 1) Something is blocked and holding onto the lock // 2) Something temporarily has a lock // For case 1, we have to clear at least some space so the blocked // Put/Flush can release the lock. For case 2, nothing bad // will happen here b.drainBatchChan() } } }
go
func (b *basicBatcher) Dispose() { for { if b.lock.TryLock() { // We've got a lock if b.disposed { b.lock.Unlock() return } b.disposed = true b.items = nil b.drainBatchChan() close(b.batchChan) b.lock.Unlock() } else { // Two cases here: // 1) Something is blocked and holding onto the lock // 2) Something temporarily has a lock // For case 1, we have to clear at least some space so the blocked // Put/Flush can release the lock. For case 2, nothing bad // will happen here b.drainBatchChan() } } }
[ "func", "(", "b", "*", "basicBatcher", ")", "Dispose", "(", ")", "{", "for", "{", "if", "b", ".", "lock", ".", "TryLock", "(", ")", "{", "// We've got a lock", "if", "b", ".", "disposed", "{", "b", ".", "lock", ".", "Unlock", "(", ")", "\n", "return", "\n", "}", "\n\n", "b", ".", "disposed", "=", "true", "\n", "b", ".", "items", "=", "nil", "\n", "b", ".", "drainBatchChan", "(", ")", "\n", "close", "(", "b", ".", "batchChan", ")", "\n", "b", ".", "lock", ".", "Unlock", "(", ")", "\n", "}", "else", "{", "// Two cases here:", "// 1) Something is blocked and holding onto the lock", "// 2) Something temporarily has a lock", "// For case 1, we have to clear at least some space so the blocked", "// Put/Flush can release the lock. For case 2, nothing bad", "// will happen here", "b", ".", "drainBatchChan", "(", ")", "\n", "}", "\n\n", "}", "\n", "}" ]
// Dispose will dispose of the batcher. Any calls to Put or Flush // will return ErrDisposed, calls to Get will return an error iff // there are no more ready batches. Any items not flushed and retrieved // by a Get may or may not be retrievable after calling this.
[ "Dispose", "will", "dispose", "of", "the", "batcher", ".", "Any", "calls", "to", "Put", "or", "Flush", "will", "return", "ErrDisposed", "calls", "to", "Get", "will", "return", "an", "error", "iff", "there", "are", "no", "more", "ready", "batches", ".", "Any", "items", "not", "flushed", "and", "retrieved", "by", "a", "Get", "may", "or", "may", "not", "be", "retrievable", "after", "calling", "this", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/batcher/batcher.go#L229-L254
train
Workiva/go-datastructures
batcher/batcher.go
IsDisposed
func (b *basicBatcher) IsDisposed() bool { b.lock.Lock() disposed := b.disposed b.lock.Unlock() return disposed }
go
func (b *basicBatcher) IsDisposed() bool { b.lock.Lock() disposed := b.disposed b.lock.Unlock() return disposed }
[ "func", "(", "b", "*", "basicBatcher", ")", "IsDisposed", "(", ")", "bool", "{", "b", ".", "lock", ".", "Lock", "(", ")", "\n", "disposed", ":=", "b", ".", "disposed", "\n", "b", ".", "lock", ".", "Unlock", "(", ")", "\n", "return", "disposed", "\n", "}" ]
// IsDisposed will determine if the batcher is disposed
[ "IsDisposed", "will", "determine", "if", "the", "batcher", "is", "disposed" ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/batcher/batcher.go#L257-L262
train
Workiva/go-datastructures
batcher/batcher.go
flush
func (b *basicBatcher) flush() { b.batchChan <- b.items b.items = make([]interface{}, 0, b.maxItems) b.availableBytes = 0 }
go
func (b *basicBatcher) flush() { b.batchChan <- b.items b.items = make([]interface{}, 0, b.maxItems) b.availableBytes = 0 }
[ "func", "(", "b", "*", "basicBatcher", ")", "flush", "(", ")", "{", "b", ".", "batchChan", "<-", "b", ".", "items", "\n", "b", ".", "items", "=", "make", "(", "[", "]", "interface", "{", "}", ",", "0", ",", "b", ".", "maxItems", ")", "\n", "b", ".", "availableBytes", "=", "0", "\n", "}" ]
// flush adds the batch currently being built to the queue of completed batches. // flush is not threadsafe, so should be synchronized externally.
[ "flush", "adds", "the", "batch", "currently", "being", "built", "to", "the", "queue", "of", "completed", "batches", ".", "flush", "is", "not", "threadsafe", "so", "should", "be", "synchronized", "externally", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/batcher/batcher.go#L266-L270
train
Workiva/go-datastructures
bitarray/iterator.go
Value
func (iter *sparseBitArrayIterator) Value() (uint64, block) { return iter.sba.indices[iter.index], iter.sba.blocks[iter.index] }
go
func (iter *sparseBitArrayIterator) Value() (uint64, block) { return iter.sba.indices[iter.index], iter.sba.blocks[iter.index] }
[ "func", "(", "iter", "*", "sparseBitArrayIterator", ")", "Value", "(", ")", "(", "uint64", ",", "block", ")", "{", "return", "iter", ".", "sba", ".", "indices", "[", "iter", ".", "index", "]", ",", "iter", ".", "sba", ".", "blocks", "[", "iter", ".", "index", "]", "\n", "}" ]
// Value returns the index and block at the given index.
[ "Value", "returns", "the", "index", "and", "block", "at", "the", "given", "index", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/iterator.go#L32-L34
train
Workiva/go-datastructures
bitarray/iterator.go
Value
func (iter *bitArrayIterator) Value() (uint64, block) { return uint64(iter.index), iter.ba.blocks[iter.index] }
go
func (iter *bitArrayIterator) Value() (uint64, block) { return uint64(iter.index), iter.ba.blocks[iter.index] }
[ "func", "(", "iter", "*", "bitArrayIterator", ")", "Value", "(", ")", "(", "uint64", ",", "block", ")", "{", "return", "uint64", "(", "iter", ".", "index", ")", ",", "iter", ".", "ba", ".", "blocks", "[", "iter", ".", "index", "]", "\n", "}" ]
// Value returns an index and the block at this index.
[ "Value", "returns", "an", "index", "and", "the", "block", "at", "this", "index", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/iterator.go#L57-L59
train
Workiva/go-datastructures
rangetree/entries.go
Dispose
func (entries *Entries) Dispose() { for i := 0; i < len(*entries); i++ { (*entries)[i] = nil } *entries = (*entries)[:0] entriesPool.Put(*entries) }
go
func (entries *Entries) Dispose() { for i := 0; i < len(*entries); i++ { (*entries)[i] = nil } *entries = (*entries)[:0] entriesPool.Put(*entries) }
[ "func", "(", "entries", "*", "Entries", ")", "Dispose", "(", ")", "{", "for", "i", ":=", "0", ";", "i", "<", "len", "(", "*", "entries", ")", ";", "i", "++", "{", "(", "*", "entries", ")", "[", "i", "]", "=", "nil", "\n", "}", "\n\n", "*", "entries", "=", "(", "*", "entries", ")", "[", ":", "0", "]", "\n", "entriesPool", ".", "Put", "(", "*", "entries", ")", "\n", "}" ]
// Dispose will free the resources consumed by this list and // allow the list to be reused.
[ "Dispose", "will", "free", "the", "resources", "consumed", "by", "this", "list", "and", "allow", "the", "list", "to", "be", "reused", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/entries.go#L33-L40
train
Workiva/go-datastructures
tree/avl/node.go
copy
func (n *node) copy() *node { return &node{ balance: n.balance, children: [2]*node{n.children[0], n.children[1]}, entry: n.entry, } }
go
func (n *node) copy() *node { return &node{ balance: n.balance, children: [2]*node{n.children[0], n.children[1]}, entry: n.entry, } }
[ "func", "(", "n", "*", "node", ")", "copy", "(", ")", "*", "node", "{", "return", "&", "node", "{", "balance", ":", "n", ".", "balance", ",", "children", ":", "[", "2", "]", "*", "node", "{", "n", ".", "children", "[", "0", "]", ",", "n", ".", "children", "[", "1", "]", "}", ",", "entry", ":", "n", ".", "entry", ",", "}", "\n", "}" ]
// copy returns a copy of this node with pointers to the original // children.
[ "copy", "returns", "a", "copy", "of", "this", "node", "with", "pointers", "to", "the", "original", "children", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/tree/avl/node.go#L35-L41
train
Workiva/go-datastructures
tree/avl/avl.go
copy
func (immutable *Immutable) copy() *Immutable { var root *node if immutable.root != nil { root = immutable.root.copy() } cp := &Immutable{ root: root, number: immutable.number, dummy: *newNode(nil), } return cp }
go
func (immutable *Immutable) copy() *Immutable { var root *node if immutable.root != nil { root = immutable.root.copy() } cp := &Immutable{ root: root, number: immutable.number, dummy: *newNode(nil), } return cp }
[ "func", "(", "immutable", "*", "Immutable", ")", "copy", "(", ")", "*", "Immutable", "{", "var", "root", "*", "node", "\n", "if", "immutable", ".", "root", "!=", "nil", "{", "root", "=", "immutable", ".", "root", ".", "copy", "(", ")", "\n", "}", "\n", "cp", ":=", "&", "Immutable", "{", "root", ":", "root", ",", "number", ":", "immutable", ".", "number", ",", "dummy", ":", "*", "newNode", "(", "nil", ")", ",", "}", "\n", "return", "cp", "\n", "}" ]
// copy returns a copy of this immutable tree with a copy // of the root and a new dummy helper for the insertion operation.
[ "copy", "returns", "a", "copy", "of", "this", "immutable", "tree", "with", "a", "copy", "of", "the", "root", "and", "a", "new", "dummy", "helper", "for", "the", "insertion", "operation", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/tree/avl/avl.go#L47-L58
train
Workiva/go-datastructures
tree/avl/avl.go
Get
func (immutable *Immutable) Get(entries ...Entry) Entries { result := make(Entries, 0, len(entries)) for _, e := range entries { result = append(result, immutable.get(e)) } return result }
go
func (immutable *Immutable) Get(entries ...Entry) Entries { result := make(Entries, 0, len(entries)) for _, e := range entries { result = append(result, immutable.get(e)) } return result }
[ "func", "(", "immutable", "*", "Immutable", ")", "Get", "(", "entries", "...", "Entry", ")", "Entries", "{", "result", ":=", "make", "(", "Entries", ",", "0", ",", "len", "(", "entries", ")", ")", "\n", "for", "_", ",", "e", ":=", "range", "entries", "{", "result", "=", "append", "(", "result", ",", "immutable", ".", "get", "(", "e", ")", ")", "\n", "}", "\n\n", "return", "result", "\n", "}" ]
// Get will get the provided Entries from the tree. If no matching // Entry is found, a nil is returned in its place.
[ "Get", "will", "get", "the", "provided", "Entries", "from", "the", "tree", ".", "If", "no", "matching", "Entry", "is", "found", "a", "nil", "is", "returned", "in", "its", "place", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/tree/avl/avl.go#L90-L97
train
Workiva/go-datastructures
tree/avl/avl.go
Insert
func (immutable *Immutable) Insert(entries ...Entry) (*Immutable, Entries) { if len(entries) == 0 { return immutable, Entries{} } overwritten := make(Entries, 0, len(entries)) cp := immutable.copy() for _, e := range entries { overwritten = append(overwritten, cp.insert(e)) } return cp, overwritten }
go
func (immutable *Immutable) Insert(entries ...Entry) (*Immutable, Entries) { if len(entries) == 0 { return immutable, Entries{} } overwritten := make(Entries, 0, len(entries)) cp := immutable.copy() for _, e := range entries { overwritten = append(overwritten, cp.insert(e)) } return cp, overwritten }
[ "func", "(", "immutable", "*", "Immutable", ")", "Insert", "(", "entries", "...", "Entry", ")", "(", "*", "Immutable", ",", "Entries", ")", "{", "if", "len", "(", "entries", ")", "==", "0", "{", "return", "immutable", ",", "Entries", "{", "}", "\n", "}", "\n\n", "overwritten", ":=", "make", "(", "Entries", ",", "0", ",", "len", "(", "entries", ")", ")", "\n", "cp", ":=", "immutable", ".", "copy", "(", ")", "\n", "for", "_", ",", "e", ":=", "range", "entries", "{", "overwritten", "=", "append", "(", "overwritten", ",", "cp", ".", "insert", "(", "e", ")", ")", "\n", "}", "\n\n", "return", "cp", ",", "overwritten", "\n", "}" ]
// Insert will add the provided entries into the tree and return the new // state. Also returned is a list of Entries that were overwritten. If // nothing was overwritten for an Entry, a nil is returned in its place.
[ "Insert", "will", "add", "the", "provided", "entries", "into", "the", "tree", "and", "return", "the", "new", "state", ".", "Also", "returned", "is", "a", "list", "of", "Entries", "that", "were", "overwritten", ".", "If", "nothing", "was", "overwritten", "for", "an", "Entry", "a", "nil", "is", "returned", "in", "its", "place", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/tree/avl/avl.go#L190-L202
train
Workiva/go-datastructures
tree/avl/avl.go
Delete
func (immutable *Immutable) Delete(entries ...Entry) (*Immutable, Entries) { if len(entries) == 0 { return immutable, Entries{} } deleted := make(Entries, 0, len(entries)) cp := immutable.copy() for _, e := range entries { deleted = append(deleted, cp.delete(e)) } return cp, deleted }
go
func (immutable *Immutable) Delete(entries ...Entry) (*Immutable, Entries) { if len(entries) == 0 { return immutable, Entries{} } deleted := make(Entries, 0, len(entries)) cp := immutable.copy() for _, e := range entries { deleted = append(deleted, cp.delete(e)) } return cp, deleted }
[ "func", "(", "immutable", "*", "Immutable", ")", "Delete", "(", "entries", "...", "Entry", ")", "(", "*", "Immutable", ",", "Entries", ")", "{", "if", "len", "(", "entries", ")", "==", "0", "{", "return", "immutable", ",", "Entries", "{", "}", "\n", "}", "\n\n", "deleted", ":=", "make", "(", "Entries", ",", "0", ",", "len", "(", "entries", ")", ")", "\n", "cp", ":=", "immutable", ".", "copy", "(", ")", "\n", "for", "_", ",", "e", ":=", "range", "entries", "{", "deleted", "=", "append", "(", "deleted", ",", "cp", ".", "delete", "(", "e", ")", ")", "\n", "}", "\n\n", "return", "cp", ",", "deleted", "\n", "}" ]
// Delete will remove the provided entries from this AVL tree and // return a new tree and any entries removed. If an entry could not // be found, nil is returned in its place.
[ "Delete", "will", "remove", "the", "provided", "entries", "from", "this", "AVL", "tree", "and", "return", "a", "new", "tree", "and", "any", "entries", "removed", ".", "If", "an", "entry", "could", "not", "be", "found", "nil", "is", "returned", "in", "its", "place", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/tree/avl/avl.go#L314-L326
train
Workiva/go-datastructures
queue/ring.go
Put
func (rb *RingBuffer) Put(item interface{}) error { _, err := rb.put(item, false) return err }
go
func (rb *RingBuffer) Put(item interface{}) error { _, err := rb.put(item, false) return err }
[ "func", "(", "rb", "*", "RingBuffer", ")", "Put", "(", "item", "interface", "{", "}", ")", "error", "{", "_", ",", "err", ":=", "rb", ".", "put", "(", "item", ",", "false", ")", "\n", "return", "err", "\n", "}" ]
// Put adds the provided item to the queue. If the queue is full, this // call will block until an item is added to the queue or Dispose is called // on the queue. An error will be returned if the queue is disposed.
[ "Put", "adds", "the", "provided", "item", "to", "the", "queue", ".", "If", "the", "queue", "is", "full", "this", "call", "will", "block", "until", "an", "item", "is", "added", "to", "the", "queue", "or", "Dispose", "is", "called", "on", "the", "queue", ".", "An", "error", "will", "be", "returned", "if", "the", "queue", "is", "disposed", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/ring.go#L75-L78
train
Workiva/go-datastructures
queue/ring.go
Offer
func (rb *RingBuffer) Offer(item interface{}) (bool, error) { return rb.put(item, true) }
go
func (rb *RingBuffer) Offer(item interface{}) (bool, error) { return rb.put(item, true) }
[ "func", "(", "rb", "*", "RingBuffer", ")", "Offer", "(", "item", "interface", "{", "}", ")", "(", "bool", ",", "error", ")", "{", "return", "rb", ".", "put", "(", "item", ",", "true", ")", "\n", "}" ]
// Offer adds the provided item to the queue if there is space. If the queue // is full, this call will return false. An error will be returned if the // queue is disposed.
[ "Offer", "adds", "the", "provided", "item", "to", "the", "queue", "if", "there", "is", "space", ".", "If", "the", "queue", "is", "full", "this", "call", "will", "return", "false", ".", "An", "error", "will", "be", "returned", "if", "the", "queue", "is", "disposed", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/ring.go#L83-L85
train
Workiva/go-datastructures
queue/ring.go
Poll
func (rb *RingBuffer) Poll(timeout time.Duration) (interface{}, error) { var ( n *node pos = atomic.LoadUint64(&rb.dequeue) start time.Time ) if timeout > 0 { start = time.Now() } L: for { if atomic.LoadUint64(&rb.disposed) == 1 { return nil, ErrDisposed } n = rb.nodes[pos&rb.mask] seq := atomic.LoadUint64(&n.position) switch dif := seq - (pos + 1); { case dif == 0: if atomic.CompareAndSwapUint64(&rb.dequeue, pos, pos+1) { break L } case dif < 0: panic(`Ring buffer in compromised state during a get operation.`) default: pos = atomic.LoadUint64(&rb.dequeue) } if timeout > 0 && time.Since(start) >= timeout { return nil, ErrTimeout } runtime.Gosched() // free up the cpu before the next iteration } data := n.data n.data = nil atomic.StoreUint64(&n.position, pos+rb.mask+1) return data, nil }
go
func (rb *RingBuffer) Poll(timeout time.Duration) (interface{}, error) { var ( n *node pos = atomic.LoadUint64(&rb.dequeue) start time.Time ) if timeout > 0 { start = time.Now() } L: for { if atomic.LoadUint64(&rb.disposed) == 1 { return nil, ErrDisposed } n = rb.nodes[pos&rb.mask] seq := atomic.LoadUint64(&n.position) switch dif := seq - (pos + 1); { case dif == 0: if atomic.CompareAndSwapUint64(&rb.dequeue, pos, pos+1) { break L } case dif < 0: panic(`Ring buffer in compromised state during a get operation.`) default: pos = atomic.LoadUint64(&rb.dequeue) } if timeout > 0 && time.Since(start) >= timeout { return nil, ErrTimeout } runtime.Gosched() // free up the cpu before the next iteration } data := n.data n.data = nil atomic.StoreUint64(&n.position, pos+rb.mask+1) return data, nil }
[ "func", "(", "rb", "*", "RingBuffer", ")", "Poll", "(", "timeout", "time", ".", "Duration", ")", "(", "interface", "{", "}", ",", "error", ")", "{", "var", "(", "n", "*", "node", "\n", "pos", "=", "atomic", ".", "LoadUint64", "(", "&", "rb", ".", "dequeue", ")", "\n", "start", "time", ".", "Time", "\n", ")", "\n", "if", "timeout", ">", "0", "{", "start", "=", "time", ".", "Now", "(", ")", "\n", "}", "\n", "L", ":", "for", "{", "if", "atomic", ".", "LoadUint64", "(", "&", "rb", ".", "disposed", ")", "==", "1", "{", "return", "nil", ",", "ErrDisposed", "\n", "}", "\n\n", "n", "=", "rb", ".", "nodes", "[", "pos", "&", "rb", ".", "mask", "]", "\n", "seq", ":=", "atomic", ".", "LoadUint64", "(", "&", "n", ".", "position", ")", "\n", "switch", "dif", ":=", "seq", "-", "(", "pos", "+", "1", ")", ";", "{", "case", "dif", "==", "0", ":", "if", "atomic", ".", "CompareAndSwapUint64", "(", "&", "rb", ".", "dequeue", ",", "pos", ",", "pos", "+", "1", ")", "{", "break", "L", "\n", "}", "\n", "case", "dif", "<", "0", ":", "panic", "(", "`Ring buffer in compromised state during a get operation.`", ")", "\n", "default", ":", "pos", "=", "atomic", ".", "LoadUint64", "(", "&", "rb", ".", "dequeue", ")", "\n", "}", "\n\n", "if", "timeout", ">", "0", "&&", "time", ".", "Since", "(", "start", ")", ">=", "timeout", "{", "return", "nil", ",", "ErrTimeout", "\n", "}", "\n\n", "runtime", ".", "Gosched", "(", ")", "// free up the cpu before the next iteration", "\n", "}", "\n", "data", ":=", "n", ".", "data", "\n", "n", ".", "data", "=", "nil", "\n", "atomic", ".", "StoreUint64", "(", "&", "n", ".", "position", ",", "pos", "+", "rb", ".", "mask", "+", "1", ")", "\n", "return", "data", ",", "nil", "\n", "}" ]
// Poll will return the next item in the queue. This call will block // if the queue is empty. This call will unblock when an item is added // to the queue, Dispose is called on the queue, or the timeout is reached. An // error will be returned if the queue is disposed or a timeout occurs. A // non-positive timeout will block indefinitely.
[ "Poll", "will", "return", "the", "next", "item", "in", "the", "queue", ".", "This", "call", "will", "block", "if", "the", "queue", "is", "empty", ".", "This", "call", "will", "unblock", "when", "an", "item", "is", "added", "to", "the", "queue", "Dispose", "is", "called", "on", "the", "queue", "or", "the", "timeout", "is", "reached", ".", "An", "error", "will", "be", "returned", "if", "the", "queue", "is", "disposed", "or", "a", "timeout", "occurs", ".", "A", "non", "-", "positive", "timeout", "will", "block", "indefinitely", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/ring.go#L134-L172
train
Workiva/go-datastructures
queue/ring.go
Len
func (rb *RingBuffer) Len() uint64 { return atomic.LoadUint64(&rb.queue) - atomic.LoadUint64(&rb.dequeue) }
go
func (rb *RingBuffer) Len() uint64 { return atomic.LoadUint64(&rb.queue) - atomic.LoadUint64(&rb.dequeue) }
[ "func", "(", "rb", "*", "RingBuffer", ")", "Len", "(", ")", "uint64", "{", "return", "atomic", ".", "LoadUint64", "(", "&", "rb", ".", "queue", ")", "-", "atomic", ".", "LoadUint64", "(", "&", "rb", ".", "dequeue", ")", "\n", "}" ]
// Len returns the number of items in the queue.
[ "Len", "returns", "the", "number", "of", "items", "in", "the", "queue", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/ring.go#L175-L177
train
Workiva/go-datastructures
queue/ring.go
NewRingBuffer
func NewRingBuffer(size uint64) *RingBuffer { rb := &RingBuffer{} rb.init(size) return rb }
go
func NewRingBuffer(size uint64) *RingBuffer { rb := &RingBuffer{} rb.init(size) return rb }
[ "func", "NewRingBuffer", "(", "size", "uint64", ")", "*", "RingBuffer", "{", "rb", ":=", "&", "RingBuffer", "{", "}", "\n", "rb", ".", "init", "(", "size", ")", "\n", "return", "rb", "\n", "}" ]
// NewRingBuffer will allocate, initialize, and return a ring buffer // with the specified size.
[ "NewRingBuffer", "will", "allocate", "initialize", "and", "return", "a", "ring", "buffer", "with", "the", "specified", "size", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/ring.go#L199-L203
train
Workiva/go-datastructures
numerics/optimization/nelder_mead.go
generateRandomVerticesFromGuess
func generateRandomVerticesFromGuess(guess *nmVertex, num int) vertices { // summed allows us to prevent duplicate guesses, checking // all previous guesses for every guess created would be too // time consuming so we take an indexed shortcut here. summed // is a map of a sum of the vars to the vertices that have an // identical sum. In this way, we can sum the vars of a new guess // and check only a small subset of previous guesses to determine // if this is an identical guess. summed := make(map[float64]vertices, num) dimensions := len(guess.vars) vs := make(vertices, 0, num) i := 0 r := rand.New(rand.NewSource(time.Now().UnixNano())) Guess: for i < num { sum := float64(0) vars := make([]float64, 0, dimensions) for j := 0; j < dimensions; j++ { v := r.Float64() * 1000 // we do a separate random check here to determine // sign so we don't end up with all high v's one sign // and low v's another if r.Float64() > .5 { v = -v } sum += v vars = append(vars, v) } guess := &nmVertex{ vars: vars, } if vs, ok := summed[sum]; !ok { vs = make(vertices, 0, dimensions) // dimensions is really just a guess, no real way of knowing what this is vs = append(vs, guess) summed[sum] = vs } else { for _, vertex := range vs { // if we've already guessed this, try the loop again if guess.equalToVertex(vertex) { continue Guess } } vs = append(vs, guess) } vs = append(vs, guess) i++ } return vs }
go
func generateRandomVerticesFromGuess(guess *nmVertex, num int) vertices { // summed allows us to prevent duplicate guesses, checking // all previous guesses for every guess created would be too // time consuming so we take an indexed shortcut here. summed // is a map of a sum of the vars to the vertices that have an // identical sum. In this way, we can sum the vars of a new guess // and check only a small subset of previous guesses to determine // if this is an identical guess. summed := make(map[float64]vertices, num) dimensions := len(guess.vars) vs := make(vertices, 0, num) i := 0 r := rand.New(rand.NewSource(time.Now().UnixNano())) Guess: for i < num { sum := float64(0) vars := make([]float64, 0, dimensions) for j := 0; j < dimensions; j++ { v := r.Float64() * 1000 // we do a separate random check here to determine // sign so we don't end up with all high v's one sign // and low v's another if r.Float64() > .5 { v = -v } sum += v vars = append(vars, v) } guess := &nmVertex{ vars: vars, } if vs, ok := summed[sum]; !ok { vs = make(vertices, 0, dimensions) // dimensions is really just a guess, no real way of knowing what this is vs = append(vs, guess) summed[sum] = vs } else { for _, vertex := range vs { // if we've already guessed this, try the loop again if guess.equalToVertex(vertex) { continue Guess } } vs = append(vs, guess) } vs = append(vs, guess) i++ } return vs }
[ "func", "generateRandomVerticesFromGuess", "(", "guess", "*", "nmVertex", ",", "num", "int", ")", "vertices", "{", "// summed allows us to prevent duplicate guesses, checking", "// all previous guesses for every guess created would be too", "// time consuming so we take an indexed shortcut here. summed", "// is a map of a sum of the vars to the vertices that have an", "// identical sum. In this way, we can sum the vars of a new guess", "// and check only a small subset of previous guesses to determine", "// if this is an identical guess.", "summed", ":=", "make", "(", "map", "[", "float64", "]", "vertices", ",", "num", ")", "\n", "dimensions", ":=", "len", "(", "guess", ".", "vars", ")", "\n", "vs", ":=", "make", "(", "vertices", ",", "0", ",", "num", ")", "\n", "i", ":=", "0", "\n", "r", ":=", "rand", ".", "New", "(", "rand", ".", "NewSource", "(", "time", ".", "Now", "(", ")", ".", "UnixNano", "(", ")", ")", ")", "\n\n", "Guess", ":", "for", "i", "<", "num", "{", "sum", ":=", "float64", "(", "0", ")", "\n", "vars", ":=", "make", "(", "[", "]", "float64", ",", "0", ",", "dimensions", ")", "\n", "for", "j", ":=", "0", ";", "j", "<", "dimensions", ";", "j", "++", "{", "v", ":=", "r", ".", "Float64", "(", ")", "*", "1000", "\n", "// we do a separate random check here to determine", "// sign so we don't end up with all high v's one sign", "// and low v's another", "if", "r", ".", "Float64", "(", ")", ">", ".5", "{", "v", "=", "-", "v", "\n", "}", "\n", "sum", "+=", "v", "\n", "vars", "=", "append", "(", "vars", ",", "v", ")", "\n", "}", "\n\n", "guess", ":=", "&", "nmVertex", "{", "vars", ":", "vars", ",", "}", "\n\n", "if", "vs", ",", "ok", ":=", "summed", "[", "sum", "]", ";", "!", "ok", "{", "vs", "=", "make", "(", "vertices", ",", "0", ",", "dimensions", ")", "// dimensions is really just a guess, no real way of knowing what this is", "\n", "vs", "=", "append", "(", "vs", ",", "guess", ")", "\n", "summed", "[", "sum", "]", "=", "vs", "\n", "}", "else", "{", "for", "_", ",", "vertex", ":=", "range", "vs", "{", "// if we've already guessed this, try the loop again", "if", "guess", ".", "equalToVertex", "(", "vertex", ")", "{", "continue", "Guess", "\n", "}", "\n", "}", "\n", "vs", "=", "append", "(", "vs", ",", "guess", ")", "\n", "}", "\n\n", "vs", "=", "append", "(", "vs", ",", "guess", ")", "\n", "i", "++", "\n", "}", "\n\n", "return", "vs", "\n", "}" ]
// generateRandomVerticesFromGuess will generate num number of vertices // with random
[ "generateRandomVerticesFromGuess", "will", "generate", "num", "number", "of", "vertices", "with", "random" ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/optimization/nelder_mead.go#L29-L82
train
Workiva/go-datastructures
numerics/optimization/nelder_mead.go
findMidpoint
func findMidpoint(vertices ...*nmVertex) *nmVertex { num := len(vertices) // this is what we divide by vars := make([]float64, 0, num) for i := 0; i < num; i++ { sum := float64(0) for _, v := range vertices { sum += v.vars[i] } vars = append(vars, sum/float64(num)) } return &nmVertex{ vars: vars, } }
go
func findMidpoint(vertices ...*nmVertex) *nmVertex { num := len(vertices) // this is what we divide by vars := make([]float64, 0, num) for i := 0; i < num; i++ { sum := float64(0) for _, v := range vertices { sum += v.vars[i] } vars = append(vars, sum/float64(num)) } return &nmVertex{ vars: vars, } }
[ "func", "findMidpoint", "(", "vertices", "...", "*", "nmVertex", ")", "*", "nmVertex", "{", "num", ":=", "len", "(", "vertices", ")", "// this is what we divide by", "\n", "vars", ":=", "make", "(", "[", "]", "float64", ",", "0", ",", "num", ")", "\n\n", "for", "i", ":=", "0", ";", "i", "<", "num", ";", "i", "++", "{", "sum", ":=", "float64", "(", "0", ")", "\n", "for", "_", ",", "v", ":=", "range", "vertices", "{", "sum", "+=", "v", ".", "vars", "[", "i", "]", "\n", "}", "\n", "vars", "=", "append", "(", "vars", ",", "sum", "/", "float64", "(", "num", ")", ")", "\n", "}", "\n\n", "return", "&", "nmVertex", "{", "vars", ":", "vars", ",", "}", "\n", "}" ]
// findMidpoint will find the midpoint of the provided vertices // and return a new vertex.
[ "findMidpoint", "will", "find", "the", "midpoint", "of", "the", "provided", "vertices", "and", "return", "a", "new", "vertex", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/optimization/nelder_mead.go#L101-L116
train
Workiva/go-datastructures
numerics/optimization/nelder_mead.go
evaluate
func (vertices vertices) evaluate(config NelderMeadConfiguration) { for _, v := range vertices { v.evaluate(config) } vertices.sort(config) }
go
func (vertices vertices) evaluate(config NelderMeadConfiguration) { for _, v := range vertices { v.evaluate(config) } vertices.sort(config) }
[ "func", "(", "vertices", "vertices", ")", "evaluate", "(", "config", "NelderMeadConfiguration", ")", "{", "for", "_", ",", "v", ":=", "range", "vertices", "{", "v", ".", "evaluate", "(", "config", ")", "\n", "}", "\n\n", "vertices", ".", "sort", "(", "config", ")", "\n", "}" ]
// evaluate will call evaluate on all the verticies in this list // and order them by distance to target.
[ "evaluate", "will", "call", "evaluate", "on", "all", "the", "verticies", "in", "this", "list", "and", "order", "them", "by", "distance", "to", "target", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/optimization/nelder_mead.go#L135-L141
train
Workiva/go-datastructures
numerics/optimization/nelder_mead.go
Less
func (sorter sorter) Less(i, j int) bool { return sorter.vertices[i].less(sorter.config, sorter.vertices[j]) }
go
func (sorter sorter) Less(i, j int) bool { return sorter.vertices[i].less(sorter.config, sorter.vertices[j]) }
[ "func", "(", "sorter", "sorter", ")", "Less", "(", "i", ",", "j", "int", ")", "bool", "{", "return", "sorter", ".", "vertices", "[", "i", "]", ".", "less", "(", "sorter", ".", "config", ",", "sorter", ".", "vertices", "[", "j", "]", ")", "\n", "}" ]
// the following methods are required for sort.Interface. We // use the standard libraries sort here as it uses an adaptive // sort and we really don't expect there to be a ton of dimensions // here so mulithreaded sort in this repo really isn't // necessary.
[ "the", "following", "methods", "are", "required", "for", "sort", ".", "Interface", ".", "We", "use", "the", "standard", "libraries", "sort", "here", "as", "it", "uses", "an", "adaptive", "sort", "and", "we", "really", "don", "t", "expect", "there", "to", "be", "a", "ton", "of", "dimensions", "here", "so", "mulithreaded", "sort", "in", "this", "repo", "really", "isn", "t", "necessary", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/optimization/nelder_mead.go#L166-L168
train
Workiva/go-datastructures
numerics/optimization/nelder_mead.go
euclideanDistance
func (nm *nmVertex) euclideanDistance(other *nmVertex) float64 { sum := float64(0) // first we want to sum all the distances between the points for i, otherPoint := range other.vars { // distance between points is defined by (qi-ri)^2 sum += math.Pow(otherPoint-nm.vars[i], 2) } return math.Sqrt(sum) }
go
func (nm *nmVertex) euclideanDistance(other *nmVertex) float64 { sum := float64(0) // first we want to sum all the distances between the points for i, otherPoint := range other.vars { // distance between points is defined by (qi-ri)^2 sum += math.Pow(otherPoint-nm.vars[i], 2) } return math.Sqrt(sum) }
[ "func", "(", "nm", "*", "nmVertex", ")", "euclideanDistance", "(", "other", "*", "nmVertex", ")", "float64", "{", "sum", ":=", "float64", "(", "0", ")", "\n", "// first we want to sum all the distances between the points", "for", "i", ",", "otherPoint", ":=", "range", "other", ".", "vars", "{", "// distance between points is defined by (qi-ri)^2", "sum", "+=", "math", ".", "Pow", "(", "otherPoint", "-", "nm", ".", "vars", "[", "i", "]", ",", "2", ")", "\n", "}", "\n\n", "return", "math", ".", "Sqrt", "(", "sum", ")", "\n", "}" ]
// euclideanDistance determines the euclidean distance between two points.
[ "euclideanDistance", "determines", "the", "euclidean", "distance", "between", "two", "points", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/optimization/nelder_mead.go#L301-L310
train
Workiva/go-datastructures
numerics/optimization/nelder_mead.go
reflect
func (nm *nelderMead) reflect(vertices vertices, midpoint *nmVertex) *nmVertex { toScalar := midpoint.subtract(nm.lastVertex(vertices)) toScalar = toScalar.multiply(alpha) toScalar = midpoint.add(toScalar) return nm.evaluateWithConstraints(vertices, toScalar) }
go
func (nm *nelderMead) reflect(vertices vertices, midpoint *nmVertex) *nmVertex { toScalar := midpoint.subtract(nm.lastVertex(vertices)) toScalar = toScalar.multiply(alpha) toScalar = midpoint.add(toScalar) return nm.evaluateWithConstraints(vertices, toScalar) }
[ "func", "(", "nm", "*", "nelderMead", ")", "reflect", "(", "vertices", "vertices", ",", "midpoint", "*", "nmVertex", ")", "*", "nmVertex", "{", "toScalar", ":=", "midpoint", ".", "subtract", "(", "nm", ".", "lastVertex", "(", "vertices", ")", ")", "\n", "toScalar", "=", "toScalar", ".", "multiply", "(", "alpha", ")", "\n", "toScalar", "=", "midpoint", ".", "add", "(", "toScalar", ")", "\n", "return", "nm", ".", "evaluateWithConstraints", "(", "vertices", ",", "toScalar", ")", "\n", "}" ]
// reflect will find the reflection point between the two best guesses // with the provided midpoint.
[ "reflect", "will", "find", "the", "reflection", "point", "between", "the", "two", "best", "guesses", "with", "the", "provided", "midpoint", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/optimization/nelder_mead.go#L365-L370
train
Workiva/go-datastructures
numerics/optimization/nelder_mead.go
checkIteration
func (nm *nelderMead) checkIteration(vertices vertices) bool { // this will never be true for += inf if math.Abs(vertices[0].result-nm.config.Target) < delta { return false } best := vertices[0] // here we are checking distance convergence. If all vertices // are near convergence, that is they are all within some delta // from the expected value, we can go ahead and quit early. This // can only be performed on convergence checks, not for finding // min/max. if !isInf(nm.config.Target) { for _, v := range vertices[1:] { if math.Abs(best.distance-v.distance) >= delta { return true } } } // next we want to check to see if the changes in our polytopes // dip below some threshold. That is, we want to look at the // euclidean distances between the best guess and all the other // guesses to see if they are converged upon some point. If // all of the vertices have converged close enough, it may be // worth it to cease iteration. for _, v := range vertices[1:] { if best.euclideanDistance(v) >= delta { return true } } return false }
go
func (nm *nelderMead) checkIteration(vertices vertices) bool { // this will never be true for += inf if math.Abs(vertices[0].result-nm.config.Target) < delta { return false } best := vertices[0] // here we are checking distance convergence. If all vertices // are near convergence, that is they are all within some delta // from the expected value, we can go ahead and quit early. This // can only be performed on convergence checks, not for finding // min/max. if !isInf(nm.config.Target) { for _, v := range vertices[1:] { if math.Abs(best.distance-v.distance) >= delta { return true } } } // next we want to check to see if the changes in our polytopes // dip below some threshold. That is, we want to look at the // euclidean distances between the best guess and all the other // guesses to see if they are converged upon some point. If // all of the vertices have converged close enough, it may be // worth it to cease iteration. for _, v := range vertices[1:] { if best.euclideanDistance(v) >= delta { return true } } return false }
[ "func", "(", "nm", "*", "nelderMead", ")", "checkIteration", "(", "vertices", "vertices", ")", "bool", "{", "// this will never be true for += inf", "if", "math", ".", "Abs", "(", "vertices", "[", "0", "]", ".", "result", "-", "nm", ".", "config", ".", "Target", ")", "<", "delta", "{", "return", "false", "\n", "}", "\n\n", "best", ":=", "vertices", "[", "0", "]", "\n", "// here we are checking distance convergence. If all vertices", "// are near convergence, that is they are all within some delta", "// from the expected value, we can go ahead and quit early. This", "// can only be performed on convergence checks, not for finding", "// min/max.", "if", "!", "isInf", "(", "nm", ".", "config", ".", "Target", ")", "{", "for", "_", ",", "v", ":=", "range", "vertices", "[", "1", ":", "]", "{", "if", "math", ".", "Abs", "(", "best", ".", "distance", "-", "v", ".", "distance", ")", ">=", "delta", "{", "return", "true", "\n", "}", "\n", "}", "\n", "}", "\n\n", "// next we want to check to see if the changes in our polytopes", "// dip below some threshold. That is, we want to look at the", "// euclidean distances between the best guess and all the other", "// guesses to see if they are converged upon some point. If", "// all of the vertices have converged close enough, it may be", "// worth it to cease iteration.", "for", "_", ",", "v", ":=", "range", "vertices", "[", "1", ":", "]", "{", "if", "best", ".", "euclideanDistance", "(", "v", ")", ">=", "delta", "{", "return", "true", "\n", "}", "\n", "}", "\n\n", "return", "false", "\n", "}" ]
// checkIteration checks some key values to determine if // iteration should be complete. Returns false if iteration // should be terminated and true if iteration should continue.
[ "checkIteration", "checks", "some", "key", "values", "to", "determine", "if", "iteration", "should", "be", "complete", ".", "Returns", "false", "if", "iteration", "should", "be", "terminated", "and", "true", "if", "iteration", "should", "continue", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/optimization/nelder_mead.go#L419-L452
train
Workiva/go-datastructures
numerics/optimization/nelder_mead.go
NelderMead
func NelderMead(config NelderMeadConfiguration) []float64 { nm := newNelderMead(config) nm.evaluate() return nm.results.vertices[0].vars }
go
func NelderMead(config NelderMeadConfiguration) []float64 { nm := newNelderMead(config) nm.evaluate() return nm.results.vertices[0].vars }
[ "func", "NelderMead", "(", "config", "NelderMeadConfiguration", ")", "[", "]", "float64", "{", "nm", ":=", "newNelderMead", "(", "config", ")", "\n", "nm", ".", "evaluate", "(", ")", "\n", "return", "nm", ".", "results", ".", "vertices", "[", "0", "]", ".", "vars", "\n", "}" ]
// NelderMead takes a configuration and returns a list // of floats that can be plugged into the provided function // to converge at the target value.
[ "NelderMead", "takes", "a", "configuration", "and", "returns", "a", "list", "of", "floats", "that", "can", "be", "plugged", "into", "the", "provided", "function", "to", "converge", "at", "the", "target", "value", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/optimization/nelder_mead.go#L555-L559
train
Workiva/go-datastructures
sort/symmerge.go
symSearch
func symSearch(u, w Comparators) int { start, stop, p := 0, len(u), len(w)-1 for start < stop { mid := (start + stop) / 2 if u[mid].Compare(w[p-mid]) <= 0 { start = mid + 1 } else { stop = mid } } return start }
go
func symSearch(u, w Comparators) int { start, stop, p := 0, len(u), len(w)-1 for start < stop { mid := (start + stop) / 2 if u[mid].Compare(w[p-mid]) <= 0 { start = mid + 1 } else { stop = mid } } return start }
[ "func", "symSearch", "(", "u", ",", "w", "Comparators", ")", "int", "{", "start", ",", "stop", ",", "p", ":=", "0", ",", "len", "(", "u", ")", ",", "len", "(", "w", ")", "-", "1", "\n", "for", "start", "<", "stop", "{", "mid", ":=", "(", "start", "+", "stop", ")", "/", "2", "\n", "if", "u", "[", "mid", "]", ".", "Compare", "(", "w", "[", "p", "-", "mid", "]", ")", "<=", "0", "{", "start", "=", "mid", "+", "1", "\n", "}", "else", "{", "stop", "=", "mid", "\n", "}", "\n", "}", "\n\n", "return", "start", "\n", "}" ]
// symSearch is like symBinarySearch but operates // on two sorted lists instead of a sorted list and an index. // It's duplication of code but you buy performance.
[ "symSearch", "is", "like", "symBinarySearch", "but", "operates", "on", "two", "sorted", "lists", "instead", "of", "a", "sorted", "list", "and", "an", "index", ".", "It", "s", "duplication", "of", "code", "but", "you", "buy", "performance", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/sort/symmerge.go#L11-L23
train
Workiva/go-datastructures
sort/symmerge.go
swap
func swap(u, w Comparators, index int) { for i := index; i < len(u); i++ { u[i], w[i-index] = w[i-index], u[i] } }
go
func swap(u, w Comparators, index int) { for i := index; i < len(u); i++ { u[i], w[i-index] = w[i-index], u[i] } }
[ "func", "swap", "(", "u", ",", "w", "Comparators", ",", "index", "int", ")", "{", "for", "i", ":=", "index", ";", "i", "<", "len", "(", "u", ")", ";", "i", "++", "{", "u", "[", "i", "]", ",", "w", "[", "i", "-", "index", "]", "=", "w", "[", "i", "-", "index", "]", ",", "u", "[", "i", "]", "\n", "}", "\n", "}" ]
// swap will swap positions of the two lists from index // to the end of the list. It expects that these lists // are the same size or one different.
[ "swap", "will", "swap", "positions", "of", "the", "two", "lists", "from", "index", "to", "the", "end", "of", "the", "list", ".", "It", "expects", "that", "these", "lists", "are", "the", "same", "size", "or", "one", "different", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/sort/symmerge.go#L28-L32
train
Workiva/go-datastructures
sort/symmerge.go
decomposeForSymMerge
func decomposeForSymMerge(length int, comparators Comparators) (v1 Comparators, w Comparators, v2 Comparators) { if length >= len(comparators) { panic(`INCORRECT PARAMS FOR SYM MERGE.`) } overhang := (len(comparators) - length) / 2 v1 = comparators[:overhang] w = comparators[overhang : overhang+length] v2 = comparators[overhang+length:] return }
go
func decomposeForSymMerge(length int, comparators Comparators) (v1 Comparators, w Comparators, v2 Comparators) { if length >= len(comparators) { panic(`INCORRECT PARAMS FOR SYM MERGE.`) } overhang := (len(comparators) - length) / 2 v1 = comparators[:overhang] w = comparators[overhang : overhang+length] v2 = comparators[overhang+length:] return }
[ "func", "decomposeForSymMerge", "(", "length", "int", ",", "comparators", "Comparators", ")", "(", "v1", "Comparators", ",", "w", "Comparators", ",", "v2", "Comparators", ")", "{", "if", "length", ">=", "len", "(", "comparators", ")", "{", "panic", "(", "`INCORRECT PARAMS FOR SYM MERGE.`", ")", "\n", "}", "\n\n", "overhang", ":=", "(", "len", "(", "comparators", ")", "-", "length", ")", "/", "2", "\n", "v1", "=", "comparators", "[", ":", "overhang", "]", "\n", "w", "=", "comparators", "[", "overhang", ":", "overhang", "+", "length", "]", "\n", "v2", "=", "comparators", "[", "overhang", "+", "length", ":", "]", "\n", "return", "\n", "}" ]
// decomposeForSymMerge pulls an active site out of the list // of length in size. W becomes the active site for future sym // merges and v1, v2 are decomposed and split among the other // list to be merged and w.
[ "decomposeForSymMerge", "pulls", "an", "active", "site", "out", "of", "the", "list", "of", "length", "in", "size", ".", "W", "becomes", "the", "active", "site", "for", "future", "sym", "merges", "and", "v1", "v2", "are", "decomposed", "and", "split", "among", "the", "other", "list", "to", "be", "merged", "and", "w", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/sort/symmerge.go#L38-L51
train
Workiva/go-datastructures
sort/symmerge.go
symBinarySearch
func symBinarySearch(u Comparators, start, stop, total int) int { for start < stop { mid := (start + stop) / 2 if u[mid].Compare(u[total-mid]) <= 0 { start = mid + 1 } else { stop = mid } } return start }
go
func symBinarySearch(u Comparators, start, stop, total int) int { for start < stop { mid := (start + stop) / 2 if u[mid].Compare(u[total-mid]) <= 0 { start = mid + 1 } else { stop = mid } } return start }
[ "func", "symBinarySearch", "(", "u", "Comparators", ",", "start", ",", "stop", ",", "total", "int", ")", "int", "{", "for", "start", "<", "stop", "{", "mid", ":=", "(", "start", "+", "stop", ")", "/", "2", "\n", "if", "u", "[", "mid", "]", ".", "Compare", "(", "u", "[", "total", "-", "mid", "]", ")", "<=", "0", "{", "start", "=", "mid", "+", "1", "\n", "}", "else", "{", "stop", "=", "mid", "\n", "}", "\n", "}", "\n\n", "return", "start", "\n", "}" ]
// symBinarySearch will perform a binary search between the provided // indices and find the index at which a rotation should occur.
[ "symBinarySearch", "will", "perform", "a", "binary", "search", "between", "the", "provided", "indices", "and", "find", "the", "index", "at", "which", "a", "rotation", "should", "occur", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/sort/symmerge.go#L55-L66
train
Workiva/go-datastructures
sort/symmerge.go
symSwap
func symSwap(u Comparators, start1, start2, end int) { for i := 0; i < end; i++ { u[start1+i], u[start2+i] = u[start2+i], u[start1+i] } }
go
func symSwap(u Comparators, start1, start2, end int) { for i := 0; i < end; i++ { u[start1+i], u[start2+i] = u[start2+i], u[start1+i] } }
[ "func", "symSwap", "(", "u", "Comparators", ",", "start1", ",", "start2", ",", "end", "int", ")", "{", "for", "i", ":=", "0", ";", "i", "<", "end", ";", "i", "++", "{", "u", "[", "start1", "+", "i", "]", ",", "u", "[", "start2", "+", "i", "]", "=", "u", "[", "start2", "+", "i", "]", ",", "u", "[", "start1", "+", "i", "]", "\n", "}", "\n", "}" ]
// symSwap will perform a rotation or swap between the provided // indices. Again, there is duplication here with swap, but // we are buying performance.
[ "symSwap", "will", "perform", "a", "rotation", "or", "swap", "between", "the", "provided", "indices", ".", "Again", "there", "is", "duplication", "here", "with", "swap", "but", "we", "are", "buying", "performance", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/sort/symmerge.go#L71-L75
train
Workiva/go-datastructures
sort/symmerge.go
symRotate
func symRotate(u Comparators, start1, start2, end int) { i := start2 - start1 if i == 0 { return } j := end - start2 if j == 0 { return } if i == j { symSwap(u, start1, start2, i) return } p := start1 + i for i != j { if i > j { symSwap(u, p-i, p, j) i -= j } else { symSwap(u, p-i, p+j-i, i) j -= i } } symSwap(u, p-i, p, i) }
go
func symRotate(u Comparators, start1, start2, end int) { i := start2 - start1 if i == 0 { return } j := end - start2 if j == 0 { return } if i == j { symSwap(u, start1, start2, i) return } p := start1 + i for i != j { if i > j { symSwap(u, p-i, p, j) i -= j } else { symSwap(u, p-i, p+j-i, i) j -= i } } symSwap(u, p-i, p, i) }
[ "func", "symRotate", "(", "u", "Comparators", ",", "start1", ",", "start2", ",", "end", "int", ")", "{", "i", ":=", "start2", "-", "start1", "\n", "if", "i", "==", "0", "{", "return", "\n", "}", "\n\n", "j", ":=", "end", "-", "start2", "\n", "if", "j", "==", "0", "{", "return", "\n", "}", "\n\n", "if", "i", "==", "j", "{", "symSwap", "(", "u", ",", "start1", ",", "start2", ",", "i", ")", "\n", "return", "\n", "}", "\n\n", "p", ":=", "start1", "+", "i", "\n", "for", "i", "!=", "j", "{", "if", "i", ">", "j", "{", "symSwap", "(", "u", ",", "p", "-", "i", ",", "p", ",", "j", ")", "\n", "i", "-=", "j", "\n", "}", "else", "{", "symSwap", "(", "u", ",", "p", "-", "i", ",", "p", "+", "j", "-", "i", ",", "i", ")", "\n", "j", "-=", "i", "\n", "}", "\n", "}", "\n", "symSwap", "(", "u", ",", "p", "-", "i", ",", "p", ",", "i", ")", "\n", "}" ]
// symRotate determines the indices to use in a symSwap and // performs the swap.
[ "symRotate", "determines", "the", "indices", "to", "use", "in", "a", "symSwap", "and", "performs", "the", "swap", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/sort/symmerge.go#L79-L106
train
Workiva/go-datastructures
sort/symmerge.go
symMerge
func symMerge(u Comparators, start1, start2, last int) { if start1 < start2 && start2 < last { mid := (start1 + last) / 2 n := mid + start2 var start int if start2 > mid { start = symBinarySearch(u, n-last, mid, n-1) } else { start = symBinarySearch(u, start1, start2, n-1) } end := n - start symRotate(u, start, start2, end) symMerge(u, start1, start, mid) symMerge(u, mid, end, last) } }
go
func symMerge(u Comparators, start1, start2, last int) { if start1 < start2 && start2 < last { mid := (start1 + last) / 2 n := mid + start2 var start int if start2 > mid { start = symBinarySearch(u, n-last, mid, n-1) } else { start = symBinarySearch(u, start1, start2, n-1) } end := n - start symRotate(u, start, start2, end) symMerge(u, start1, start, mid) symMerge(u, mid, end, last) } }
[ "func", "symMerge", "(", "u", "Comparators", ",", "start1", ",", "start2", ",", "last", "int", ")", "{", "if", "start1", "<", "start2", "&&", "start2", "<", "last", "{", "mid", ":=", "(", "start1", "+", "last", ")", "/", "2", "\n", "n", ":=", "mid", "+", "start2", "\n", "var", "start", "int", "\n", "if", "start2", ">", "mid", "{", "start", "=", "symBinarySearch", "(", "u", ",", "n", "-", "last", ",", "mid", ",", "n", "-", "1", ")", "\n", "}", "else", "{", "start", "=", "symBinarySearch", "(", "u", ",", "start1", ",", "start2", ",", "n", "-", "1", ")", "\n", "}", "\n", "end", ":=", "n", "-", "start", "\n\n", "symRotate", "(", "u", ",", "start", ",", "start2", ",", "end", ")", "\n", "symMerge", "(", "u", ",", "start1", ",", "start", ",", "mid", ")", "\n", "symMerge", "(", "u", ",", "mid", ",", "end", ",", "last", ")", "\n", "}", "\n", "}" ]
// symMerge is the recursive and internal form of SymMerge.
[ "symMerge", "is", "the", "recursive", "and", "internal", "form", "of", "SymMerge", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/sort/symmerge.go#L109-L125
train
Workiva/go-datastructures
slice/skip/skip.go
Int63
func (ls *lockedSource) Int63() (n int64) { ls.mu.Lock() n = ls.src.Int63() ls.mu.Unlock() return }
go
func (ls *lockedSource) Int63() (n int64) { ls.mu.Lock() n = ls.src.Int63() ls.mu.Unlock() return }
[ "func", "(", "ls", "*", "lockedSource", ")", "Int63", "(", ")", "(", "n", "int64", ")", "{", "ls", ".", "mu", ".", "Lock", "(", ")", "\n", "n", "=", "ls", ".", "src", ".", "Int63", "(", ")", "\n", "ls", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "\n", "}" ]
// Int63 implements the rand.Source interface.
[ "Int63", "implements", "the", "rand", ".", "Source", "interface", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/skip.go#L86-L91
train
Workiva/go-datastructures
slice/skip/skip.go
Seed
func (ls *lockedSource) Seed(seed int64) { ls.mu.Lock() ls.src.Seed(seed) ls.mu.Unlock() }
go
func (ls *lockedSource) Seed(seed int64) { ls.mu.Lock() ls.src.Seed(seed) ls.mu.Unlock() }
[ "func", "(", "ls", "*", "lockedSource", ")", "Seed", "(", "seed", "int64", ")", "{", "ls", ".", "mu", ".", "Lock", "(", ")", "\n", "ls", ".", "src", ".", "Seed", "(", "seed", ")", "\n", "ls", ".", "mu", ".", "Unlock", "(", ")", "\n", "}" ]
// Seed implements the rand.Source interface.
[ "Seed", "implements", "the", "rand", ".", "Source", "interface", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/skip.go#L94-L98
train
Workiva/go-datastructures
slice/skip/skip.go
init
func (sl *SkipList) init(ifc interface{}) { switch ifc.(type) { case uint8: sl.maxLevel = 8 case uint16: sl.maxLevel = 16 case uint32: sl.maxLevel = 32 case uint64, uint: sl.maxLevel = 64 } sl.cache = make(nodes, sl.maxLevel) sl.posCache = make(widths, sl.maxLevel) sl.head = newNode(nil, sl.maxLevel) }
go
func (sl *SkipList) init(ifc interface{}) { switch ifc.(type) { case uint8: sl.maxLevel = 8 case uint16: sl.maxLevel = 16 case uint32: sl.maxLevel = 32 case uint64, uint: sl.maxLevel = 64 } sl.cache = make(nodes, sl.maxLevel) sl.posCache = make(widths, sl.maxLevel) sl.head = newNode(nil, sl.maxLevel) }
[ "func", "(", "sl", "*", "SkipList", ")", "init", "(", "ifc", "interface", "{", "}", ")", "{", "switch", "ifc", ".", "(", "type", ")", "{", "case", "uint8", ":", "sl", ".", "maxLevel", "=", "8", "\n", "case", "uint16", ":", "sl", ".", "maxLevel", "=", "16", "\n", "case", "uint32", ":", "sl", ".", "maxLevel", "=", "32", "\n", "case", "uint64", ",", "uint", ":", "sl", ".", "maxLevel", "=", "64", "\n", "}", "\n", "sl", ".", "cache", "=", "make", "(", "nodes", ",", "sl", ".", "maxLevel", ")", "\n", "sl", ".", "posCache", "=", "make", "(", "widths", ",", "sl", ".", "maxLevel", ")", "\n", "sl", ".", "head", "=", "newNode", "(", "nil", ",", "sl", ".", "maxLevel", ")", "\n", "}" ]
// init will initialize this skiplist. The parameter is expected // to be of some uint type which will set this skiplist's maximum // level.
[ "init", "will", "initialize", "this", "skiplist", ".", "The", "parameter", "is", "expected", "to", "be", "of", "some", "uint", "type", "which", "will", "set", "this", "skiplist", "s", "maximum", "level", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/skip.go#L204-L218
train
Workiva/go-datastructures
slice/skip/skip.go
GetWithPosition
func (sl *SkipList) GetWithPosition(cmp common.Comparator) (common.Comparator, uint64) { n, pos := sl.search(cmp, nil, nil) if n == nil { return nil, 0 } return n.entry, pos - 1 }
go
func (sl *SkipList) GetWithPosition(cmp common.Comparator) (common.Comparator, uint64) { n, pos := sl.search(cmp, nil, nil) if n == nil { return nil, 0 } return n.entry, pos - 1 }
[ "func", "(", "sl", "*", "SkipList", ")", "GetWithPosition", "(", "cmp", "common", ".", "Comparator", ")", "(", "common", ".", "Comparator", ",", "uint64", ")", "{", "n", ",", "pos", ":=", "sl", ".", "search", "(", "cmp", ",", "nil", ",", "nil", ")", "\n", "if", "n", "==", "nil", "{", "return", "nil", ",", "0", "\n", "}", "\n\n", "return", "n", ".", "entry", ",", "pos", "-", "1", "\n", "}" ]
// GetWithPosition will retrieve the value with the provided key and // return the position of that value within the list. Returns nil, 0 // if an associated value could not be found.
[ "GetWithPosition", "will", "retrieve", "the", "value", "with", "the", "provided", "key", "and", "return", "the", "position", "of", "that", "value", "within", "the", "list", ".", "Returns", "nil", "0", "if", "an", "associated", "value", "could", "not", "be", "found", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/skip.go#L306-L313
train
Workiva/go-datastructures
slice/skip/skip.go
ByPosition
func (sl *SkipList) ByPosition(position uint64) common.Comparator { n, _ := sl.searchByPosition(position+1, nil, nil) if n == nil { return nil } return n.entry }
go
func (sl *SkipList) ByPosition(position uint64) common.Comparator { n, _ := sl.searchByPosition(position+1, nil, nil) if n == nil { return nil } return n.entry }
[ "func", "(", "sl", "*", "SkipList", ")", "ByPosition", "(", "position", "uint64", ")", "common", ".", "Comparator", "{", "n", ",", "_", ":=", "sl", ".", "searchByPosition", "(", "position", "+", "1", ",", "nil", ",", "nil", ")", "\n", "if", "n", "==", "nil", "{", "return", "nil", "\n", "}", "\n\n", "return", "n", ".", "entry", "\n", "}" ]
// ByPosition returns the Comparator at the given position.
[ "ByPosition", "returns", "the", "Comparator", "at", "the", "given", "position", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/skip.go#L316-L323
train
Workiva/go-datastructures
slice/skip/skip.go
InsertAtPosition
func (sl *SkipList) InsertAtPosition(position uint64, cmp common.Comparator) { sl.insertAtPosition(position, cmp) }
go
func (sl *SkipList) InsertAtPosition(position uint64, cmp common.Comparator) { sl.insertAtPosition(position, cmp) }
[ "func", "(", "sl", "*", "SkipList", ")", "InsertAtPosition", "(", "position", "uint64", ",", "cmp", "common", ".", "Comparator", ")", "{", "sl", ".", "insertAtPosition", "(", "position", ",", "cmp", ")", "\n", "}" ]
// InsertAtPosition will insert the provided Comparator at the provided position. // If position is greater than the length of the skiplist, the Comparator // is appended. This method bypasses order checks and checks for // duplicates so use with caution.
[ "InsertAtPosition", "will", "insert", "the", "provided", "Comparator", "at", "the", "provided", "position", ".", "If", "position", "is", "greater", "than", "the", "length", "of", "the", "skiplist", "the", "Comparator", "is", "appended", ".", "This", "method", "bypasses", "order", "checks", "and", "checks", "for", "duplicates", "so", "use", "with", "caution", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/skip.go#L354-L356
train
Workiva/go-datastructures
slice/skip/skip.go
ReplaceAtPosition
func (sl *SkipList) ReplaceAtPosition(position uint64, cmp common.Comparator) { sl.replaceAtPosition(position, cmp) }
go
func (sl *SkipList) ReplaceAtPosition(position uint64, cmp common.Comparator) { sl.replaceAtPosition(position, cmp) }
[ "func", "(", "sl", "*", "SkipList", ")", "ReplaceAtPosition", "(", "position", "uint64", ",", "cmp", "common", ".", "Comparator", ")", "{", "sl", ".", "replaceAtPosition", "(", "position", ",", "cmp", ")", "\n", "}" ]
// Replace at position will replace the Comparator at the provided position // with the provided Comparator. If the provided position does not exist, // this operation is a no-op.
[ "Replace", "at", "position", "will", "replace", "the", "Comparator", "at", "the", "provided", "position", "with", "the", "provided", "Comparator", ".", "If", "the", "provided", "position", "does", "not", "exist", "this", "operation", "is", "a", "no", "-", "op", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/skip.go#L370-L372
train
Workiva/go-datastructures
slice/skip/skip.go
Iter
func (sl *SkipList) Iter(cmp common.Comparator) Iterator { return sl.iter(cmp) }
go
func (sl *SkipList) Iter(cmp common.Comparator) Iterator { return sl.iter(cmp) }
[ "func", "(", "sl", "*", "SkipList", ")", "Iter", "(", "cmp", "common", ".", "Comparator", ")", "Iterator", "{", "return", "sl", ".", "iter", "(", "cmp", ")", "\n", "}" ]
// Iter will return an iterator that can be used to iterate // over all the values with a key equal to or greater than // the key provided.
[ "Iter", "will", "return", "an", "iterator", "that", "can", "be", "used", "to", "iterate", "over", "all", "the", "values", "with", "a", "key", "equal", "to", "or", "greater", "than", "the", "key", "provided", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/skip.go#L454-L456
train
Workiva/go-datastructures
slice/skip/skip.go
SplitAt
func (sl *SkipList) SplitAt(index uint64) (*SkipList, *SkipList) { index++ // 0-index offset if index >= sl.Len() { return sl, nil } return splitAt(sl, index) }
go
func (sl *SkipList) SplitAt(index uint64) (*SkipList, *SkipList) { index++ // 0-index offset if index >= sl.Len() { return sl, nil } return splitAt(sl, index) }
[ "func", "(", "sl", "*", "SkipList", ")", "SplitAt", "(", "index", "uint64", ")", "(", "*", "SkipList", ",", "*", "SkipList", ")", "{", "index", "++", "// 0-index offset", "\n", "if", "index", ">=", "sl", ".", "Len", "(", ")", "{", "return", "sl", ",", "nil", "\n", "}", "\n", "return", "splitAt", "(", "sl", ",", "index", ")", "\n", "}" ]
// SplitAt will split the current skiplist into two lists. The first // skiplist returned is the "left" list and the second is the "right." // The index defines the last item in the left list. If index is greater // then the length of this list, only the left skiplist is returned // and the right will be nil. This is a mutable operation and modifies // the content of this list.
[ "SplitAt", "will", "split", "the", "current", "skiplist", "into", "two", "lists", ".", "The", "first", "skiplist", "returned", "is", "the", "left", "list", "and", "the", "second", "is", "the", "right", ".", "The", "index", "defines", "the", "last", "item", "in", "the", "left", "list", ".", "If", "index", "is", "greater", "then", "the", "length", "of", "this", "list", "only", "the", "left", "skiplist", "is", "returned", "and", "the", "right", "will", "be", "nil", ".", "This", "is", "a", "mutable", "operation", "and", "modifies", "the", "content", "of", "this", "list", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/skip.go#L464-L470
train
Workiva/go-datastructures
sort/interface.go
Less
func (c Comparators) Less(i, j int) bool { return c[i].Compare(c[j]) < 0 }
go
func (c Comparators) Less(i, j int) bool { return c[i].Compare(c[j]) < 0 }
[ "func", "(", "c", "Comparators", ")", "Less", "(", "i", ",", "j", "int", ")", "bool", "{", "return", "c", "[", "i", "]", ".", "Compare", "(", "c", "[", "j", "]", ")", "<", "0", "\n", "}" ]
// Less returns a bool indicating if the comparator at index i // is less than the comparator at index j.
[ "Less", "returns", "a", "bool", "indicating", "if", "the", "comparator", "at", "index", "i", "is", "less", "than", "the", "comparator", "at", "index", "j", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/sort/interface.go#L8-L10
train
Workiva/go-datastructures
sort/interface.go
Swap
func (c Comparators) Swap(i, j int) { c[j], c[i] = c[i], c[j] }
go
func (c Comparators) Swap(i, j int) { c[j], c[i] = c[i], c[j] }
[ "func", "(", "c", "Comparators", ")", "Swap", "(", "i", ",", "j", "int", ")", "{", "c", "[", "j", "]", ",", "c", "[", "i", "]", "=", "c", "[", "i", "]", ",", "c", "[", "j", "]", "\n", "}" ]
// Swap swaps the values at positions i and j.
[ "Swap", "swaps", "the", "values", "at", "positions", "i", "and", "j", "." ]
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/sort/interface.go#L19-L21
train