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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
Workiva/go-datastructures
|
slice/skip/node.go
|
newNode
|
func newNode(cmp common.Comparator, maxLevels uint8) *node {
return &node{
entry: cmp,
forward: make(nodes, maxLevels),
widths: make(widths, maxLevels),
}
}
|
go
|
func newNode(cmp common.Comparator, maxLevels uint8) *node {
return &node{
entry: cmp,
forward: make(nodes, maxLevels),
widths: make(widths, maxLevels),
}
}
|
[
"func",
"newNode",
"(",
"cmp",
"common",
".",
"Comparator",
",",
"maxLevels",
"uint8",
")",
"*",
"node",
"{",
"return",
"&",
"node",
"{",
"entry",
":",
"cmp",
",",
"forward",
":",
"make",
"(",
"nodes",
",",
"maxLevels",
")",
",",
"widths",
":",
"make",
"(",
"widths",
",",
"maxLevels",
")",
",",
"}",
"\n",
"}"
] |
// newNode will allocate and return a new node with the entry
// provided. maxLevels will determine the length of the forward
// pointer list associated with this node.
|
[
"newNode",
"will",
"allocate",
"and",
"return",
"a",
"new",
"node",
"with",
"the",
"entry",
"provided",
".",
"maxLevels",
"will",
"determine",
"the",
"length",
"of",
"the",
"forward",
"pointer",
"list",
"associated",
"with",
"this",
"node",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/node.go#L44-L50
|
train
|
Workiva/go-datastructures
|
futures/futures.go
|
GetResult
|
func (f *Future) GetResult() (interface{}, error) {
f.lock.Lock()
if f.triggered {
f.lock.Unlock()
return f.item, f.err
}
f.lock.Unlock()
f.wg.Wait()
return f.item, f.err
}
|
go
|
func (f *Future) GetResult() (interface{}, error) {
f.lock.Lock()
if f.triggered {
f.lock.Unlock()
return f.item, f.err
}
f.lock.Unlock()
f.wg.Wait()
return f.item, f.err
}
|
[
"func",
"(",
"f",
"*",
"Future",
")",
"GetResult",
"(",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"f",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"if",
"f",
".",
"triggered",
"{",
"f",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"f",
".",
"item",
",",
"f",
".",
"err",
"\n",
"}",
"\n",
"f",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"f",
".",
"wg",
".",
"Wait",
"(",
")",
"\n",
"return",
"f",
".",
"item",
",",
"f",
".",
"err",
"\n",
"}"
] |
// GetResult will immediately fetch the result if it exists
// or wait on the result until it is ready.
|
[
"GetResult",
"will",
"immediately",
"fetch",
"the",
"result",
"if",
"it",
"exists",
"or",
"wait",
"on",
"the",
"result",
"until",
"it",
"is",
"ready",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/futures/futures.go#L52-L62
|
train
|
Workiva/go-datastructures
|
futures/futures.go
|
HasResult
|
func (f *Future) HasResult() bool {
f.lock.Lock()
hasResult := f.triggered
f.lock.Unlock()
return hasResult
}
|
go
|
func (f *Future) HasResult() bool {
f.lock.Lock()
hasResult := f.triggered
f.lock.Unlock()
return hasResult
}
|
[
"func",
"(",
"f",
"*",
"Future",
")",
"HasResult",
"(",
")",
"bool",
"{",
"f",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"hasResult",
":=",
"f",
".",
"triggered",
"\n",
"f",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"hasResult",
"\n",
"}"
] |
// HasResult will return true iff the result exists
|
[
"HasResult",
"will",
"return",
"true",
"iff",
"the",
"result",
"exists"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/futures/futures.go#L65-L70
|
train
|
Workiva/go-datastructures
|
futures/futures.go
|
New
|
func New(completer Completer, timeout time.Duration) *Future {
f := &Future{}
f.wg.Add(1)
var wg sync.WaitGroup
wg.Add(1)
go listenForResult(f, completer, timeout, &wg)
wg.Wait()
return f
}
|
go
|
func New(completer Completer, timeout time.Duration) *Future {
f := &Future{}
f.wg.Add(1)
var wg sync.WaitGroup
wg.Add(1)
go listenForResult(f, completer, timeout, &wg)
wg.Wait()
return f
}
|
[
"func",
"New",
"(",
"completer",
"Completer",
",",
"timeout",
"time",
".",
"Duration",
")",
"*",
"Future",
"{",
"f",
":=",
"&",
"Future",
"{",
"}",
"\n",
"f",
".",
"wg",
".",
"Add",
"(",
"1",
")",
"\n",
"var",
"wg",
"sync",
".",
"WaitGroup",
"\n",
"wg",
".",
"Add",
"(",
"1",
")",
"\n",
"go",
"listenForResult",
"(",
"f",
",",
"completer",
",",
"timeout",
",",
"&",
"wg",
")",
"\n",
"wg",
".",
"Wait",
"(",
")",
"\n",
"return",
"f",
"\n",
"}"
] |
// New is the constructor to generate a new future. Pass the completed
// item to the toComplete channel and any listeners will get
// notified. If timeout is hit before toComplete is called,
// any listeners will get passed an error.
|
[
"New",
"is",
"the",
"constructor",
"to",
"generate",
"a",
"new",
"future",
".",
"Pass",
"the",
"completed",
"item",
"to",
"the",
"toComplete",
"channel",
"and",
"any",
"listeners",
"will",
"get",
"notified",
".",
"If",
"timeout",
"is",
"hit",
"before",
"toComplete",
"is",
"called",
"any",
"listeners",
"will",
"get",
"passed",
"an",
"error",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/futures/futures.go#L97-L105
|
train
|
Workiva/go-datastructures
|
rangetree/ordered.go
|
addAt
|
func (nodes *orderedNodes) addAt(i int, node *node) *node {
if i == len(*nodes) {
*nodes = append(*nodes, node)
return nil
}
if (*nodes)[i].value == node.value {
overwritten := (*nodes)[i]
// this is a duplicate, there can't be a duplicate
// point in the last dimension
(*nodes)[i] = node
return overwritten
}
*nodes = append(*nodes, nil)
copy((*nodes)[i+1:], (*nodes)[i:])
(*nodes)[i] = node
return nil
}
|
go
|
func (nodes *orderedNodes) addAt(i int, node *node) *node {
if i == len(*nodes) {
*nodes = append(*nodes, node)
return nil
}
if (*nodes)[i].value == node.value {
overwritten := (*nodes)[i]
// this is a duplicate, there can't be a duplicate
// point in the last dimension
(*nodes)[i] = node
return overwritten
}
*nodes = append(*nodes, nil)
copy((*nodes)[i+1:], (*nodes)[i:])
(*nodes)[i] = node
return nil
}
|
[
"func",
"(",
"nodes",
"*",
"orderedNodes",
")",
"addAt",
"(",
"i",
"int",
",",
"node",
"*",
"node",
")",
"*",
"node",
"{",
"if",
"i",
"==",
"len",
"(",
"*",
"nodes",
")",
"{",
"*",
"nodes",
"=",
"append",
"(",
"*",
"nodes",
",",
"node",
")",
"\n",
"return",
"nil",
"\n",
"}",
"\n\n",
"if",
"(",
"*",
"nodes",
")",
"[",
"i",
"]",
".",
"value",
"==",
"node",
".",
"value",
"{",
"overwritten",
":=",
"(",
"*",
"nodes",
")",
"[",
"i",
"]",
"\n",
"// this is a duplicate, there can't be a duplicate",
"// point in the last dimension",
"(",
"*",
"nodes",
")",
"[",
"i",
"]",
"=",
"node",
"\n",
"return",
"overwritten",
"\n",
"}",
"\n\n",
"*",
"nodes",
"=",
"append",
"(",
"*",
"nodes",
",",
"nil",
")",
"\n",
"copy",
"(",
"(",
"*",
"nodes",
")",
"[",
"i",
"+",
"1",
":",
"]",
",",
"(",
"*",
"nodes",
")",
"[",
"i",
":",
"]",
")",
"\n",
"(",
"*",
"nodes",
")",
"[",
"i",
"]",
"=",
"node",
"\n",
"return",
"nil",
"\n",
"}"
] |
// addAt will add the provided node at the provided index. Returns
// a node if one was overwritten.
|
[
"addAt",
"will",
"add",
"the",
"provided",
"node",
"at",
"the",
"provided",
"index",
".",
"Returns",
"a",
"node",
"if",
"one",
"was",
"overwritten",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rangetree/ordered.go#L34-L52
|
train
|
Workiva/go-datastructures
|
trie/yfast/yfast.go
|
getBucketKey
|
func (yfast *YFastTrie) getBucketKey(key uint64) uint64 {
i := key/uint64(yfast.bits) + 1
return uint64(yfast.bits)*i - 1
}
|
go
|
func (yfast *YFastTrie) getBucketKey(key uint64) uint64 {
i := key/uint64(yfast.bits) + 1
return uint64(yfast.bits)*i - 1
}
|
[
"func",
"(",
"yfast",
"*",
"YFastTrie",
")",
"getBucketKey",
"(",
"key",
"uint64",
")",
"uint64",
"{",
"i",
":=",
"key",
"/",
"uint64",
"(",
"yfast",
".",
"bits",
")",
"+",
"1",
"\n",
"return",
"uint64",
"(",
"yfast",
".",
"bits",
")",
"*",
"i",
"-",
"1",
"\n",
"}"
] |
// getBucketKey finds the largest possible value in this key's bucket.
// This is the representative value for the entry in the x-fast trie.
|
[
"getBucketKey",
"finds",
"the",
"largest",
"possible",
"value",
"in",
"this",
"key",
"s",
"bucket",
".",
"This",
"is",
"the",
"representative",
"value",
"for",
"the",
"entry",
"in",
"the",
"x",
"-",
"fast",
"trie",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/yfast.go#L71-L74
|
train
|
Workiva/go-datastructures
|
trie/yfast/yfast.go
|
Insert
|
func (yfast *YFastTrie) Insert(entries ...Entry) Entries {
overwritten := make(Entries, 0, len(entries))
for _, e := range entries {
overwritten = append(overwritten, yfast.insert(e))
}
return overwritten
}
|
go
|
func (yfast *YFastTrie) Insert(entries ...Entry) Entries {
overwritten := make(Entries, 0, len(entries))
for _, e := range entries {
overwritten = append(overwritten, yfast.insert(e))
}
return overwritten
}
|
[
"func",
"(",
"yfast",
"*",
"YFastTrie",
")",
"Insert",
"(",
"entries",
"...",
"Entry",
")",
"Entries",
"{",
"overwritten",
":=",
"make",
"(",
"Entries",
",",
"0",
",",
"len",
"(",
"entries",
")",
")",
"\n",
"for",
"_",
",",
"e",
":=",
"range",
"entries",
"{",
"overwritten",
"=",
"append",
"(",
"overwritten",
",",
"yfast",
".",
"insert",
"(",
"e",
")",
")",
"\n",
"}",
"\n\n",
"return",
"overwritten",
"\n",
"}"
] |
// Insert will insert the provided entries into the y-fast trie
// and return a list of entries that were overwritten.
|
[
"Insert",
"will",
"insert",
"the",
"provided",
"entries",
"into",
"the",
"y",
"-",
"fast",
"trie",
"and",
"return",
"a",
"list",
"of",
"entries",
"that",
"were",
"overwritten",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/yfast.go#L107-L114
|
train
|
Workiva/go-datastructures
|
trie/yfast/yfast.go
|
Delete
|
func (yfast *YFastTrie) Delete(keys ...uint64) Entries {
entries := make(Entries, 0, len(keys))
for _, key := range keys {
entries = append(entries, yfast.delete(key))
}
return entries
}
|
go
|
func (yfast *YFastTrie) Delete(keys ...uint64) Entries {
entries := make(Entries, 0, len(keys))
for _, key := range keys {
entries = append(entries, yfast.delete(key))
}
return entries
}
|
[
"func",
"(",
"yfast",
"*",
"YFastTrie",
")",
"Delete",
"(",
"keys",
"...",
"uint64",
")",
"Entries",
"{",
"entries",
":=",
"make",
"(",
"Entries",
",",
"0",
",",
"len",
"(",
"keys",
")",
")",
"\n",
"for",
"_",
",",
"key",
":=",
"range",
"keys",
"{",
"entries",
"=",
"append",
"(",
"entries",
",",
"yfast",
".",
"delete",
"(",
"key",
")",
")",
"\n",
"}",
"\n\n",
"return",
"entries",
"\n",
"}"
] |
// Delete will delete the provided keys from the y-fast trie
// and return a list of entries that were deleted.
|
[
"Delete",
"will",
"delete",
"the",
"provided",
"keys",
"from",
"the",
"y",
"-",
"fast",
"trie",
"and",
"return",
"a",
"list",
"of",
"entries",
"that",
"were",
"deleted",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/yfast.go#L141-L148
|
train
|
Workiva/go-datastructures
|
trie/yfast/yfast.go
|
Get
|
func (yfast *YFastTrie) Get(key uint64) Entry {
entry := yfast.get(key)
if entry == nil {
return nil
}
return entry
}
|
go
|
func (yfast *YFastTrie) Get(key uint64) Entry {
entry := yfast.get(key)
if entry == nil {
return nil
}
return entry
}
|
[
"func",
"(",
"yfast",
"*",
"YFastTrie",
")",
"Get",
"(",
"key",
"uint64",
")",
"Entry",
"{",
"entry",
":=",
"yfast",
".",
"get",
"(",
"key",
")",
"\n",
"if",
"entry",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"return",
"entry",
"\n",
"}"
] |
// Get will look for the provided key in the y-fast trie and return
// the associated value if it is found. If it is not found, this
// method returns nil.
|
[
"Get",
"will",
"look",
"for",
"the",
"provided",
"key",
"in",
"the",
"y",
"-",
"fast",
"trie",
"and",
"return",
"the",
"associated",
"value",
"if",
"it",
"is",
"found",
".",
"If",
"it",
"is",
"not",
"found",
"this",
"method",
"returns",
"nil",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/yfast.go#L168-L175
|
train
|
Workiva/go-datastructures
|
trie/yfast/yfast.go
|
Successor
|
func (yfast *YFastTrie) Successor(key uint64) Entry {
entry := yfast.successor(key)
if entry == nil {
return nil
}
return entry
}
|
go
|
func (yfast *YFastTrie) Successor(key uint64) Entry {
entry := yfast.successor(key)
if entry == nil {
return nil
}
return entry
}
|
[
"func",
"(",
"yfast",
"*",
"YFastTrie",
")",
"Successor",
"(",
"key",
"uint64",
")",
"Entry",
"{",
"entry",
":=",
"yfast",
".",
"successor",
"(",
"key",
")",
"\n",
"if",
"entry",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"return",
"entry",
"\n",
"}"
] |
// Successor returns an Entry with a key equal to or immediately
// greater than the provided key. If such an Entry does not exist
// this returns nil.
|
[
"Successor",
"returns",
"an",
"Entry",
"with",
"a",
"key",
"equal",
"to",
"or",
"immediately",
"greater",
"than",
"the",
"provided",
"key",
".",
"If",
"such",
"an",
"Entry",
"does",
"not",
"exist",
"this",
"returns",
"nil",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/yfast.go#L199-L206
|
train
|
Workiva/go-datastructures
|
trie/yfast/yfast.go
|
Predecessor
|
func (yfast *YFastTrie) Predecessor(key uint64) Entry {
entry := yfast.predecessor(key)
if entry == nil {
return nil
}
return entry
}
|
go
|
func (yfast *YFastTrie) Predecessor(key uint64) Entry {
entry := yfast.predecessor(key)
if entry == nil {
return nil
}
return entry
}
|
[
"func",
"(",
"yfast",
"*",
"YFastTrie",
")",
"Predecessor",
"(",
"key",
"uint64",
")",
"Entry",
"{",
"entry",
":=",
"yfast",
".",
"predecessor",
"(",
"key",
")",
"\n",
"if",
"entry",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"return",
"entry",
"\n",
"}"
] |
// Predecessor returns an Entry with a key equal to or immediately
// preceeding than the provided key. If such an Entry does not exist
// this returns nil.
|
[
"Predecessor",
"returns",
"an",
"Entry",
"with",
"a",
"key",
"equal",
"to",
"or",
"immediately",
"preceeding",
"than",
"the",
"provided",
"key",
".",
"If",
"such",
"an",
"Entry",
"does",
"not",
"exist",
"this",
"returns",
"nil",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/yfast.go#L242-L249
|
train
|
Workiva/go-datastructures
|
graph/simple.go
|
V
|
func (g *SimpleGraph) V() int {
g.mutex.RLock()
defer g.mutex.RUnlock()
return g.v
}
|
go
|
func (g *SimpleGraph) V() int {
g.mutex.RLock()
defer g.mutex.RUnlock()
return g.v
}
|
[
"func",
"(",
"g",
"*",
"SimpleGraph",
")",
"V",
"(",
")",
"int",
"{",
"g",
".",
"mutex",
".",
"RLock",
"(",
")",
"\n",
"defer",
"g",
".",
"mutex",
".",
"RUnlock",
"(",
")",
"\n\n",
"return",
"g",
".",
"v",
"\n",
"}"
] |
// V returns the number of vertices in the SimpleGraph
|
[
"V",
"returns",
"the",
"number",
"of",
"vertices",
"in",
"the",
"SimpleGraph"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/graph/simple.go#L52-L57
|
train
|
Workiva/go-datastructures
|
graph/simple.go
|
E
|
func (g *SimpleGraph) E() int {
g.mutex.RLock()
defer g.mutex.RUnlock()
return g.e
}
|
go
|
func (g *SimpleGraph) E() int {
g.mutex.RLock()
defer g.mutex.RUnlock()
return g.e
}
|
[
"func",
"(",
"g",
"*",
"SimpleGraph",
")",
"E",
"(",
")",
"int",
"{",
"g",
".",
"mutex",
".",
"RLock",
"(",
")",
"\n",
"defer",
"g",
".",
"mutex",
".",
"RUnlock",
"(",
")",
"\n\n",
"return",
"g",
".",
"e",
"\n",
"}"
] |
// E returns the number of edges in the SimpleGraph
|
[
"E",
"returns",
"the",
"number",
"of",
"edges",
"in",
"the",
"SimpleGraph"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/graph/simple.go#L60-L65
|
train
|
Workiva/go-datastructures
|
graph/simple.go
|
AddEdge
|
func (g *SimpleGraph) AddEdge(v, w interface{}) error {
g.mutex.Lock()
defer g.mutex.Unlock()
if v == w {
return ErrSelfLoop
}
g.addVertex(v)
g.addVertex(w)
if _, ok := g.adjacencyList[v][w]; ok {
return ErrParallelEdge
}
g.adjacencyList[v][w] = struct{}{}
g.adjacencyList[w][v] = struct{}{}
g.e++
return nil
}
|
go
|
func (g *SimpleGraph) AddEdge(v, w interface{}) error {
g.mutex.Lock()
defer g.mutex.Unlock()
if v == w {
return ErrSelfLoop
}
g.addVertex(v)
g.addVertex(w)
if _, ok := g.adjacencyList[v][w]; ok {
return ErrParallelEdge
}
g.adjacencyList[v][w] = struct{}{}
g.adjacencyList[w][v] = struct{}{}
g.e++
return nil
}
|
[
"func",
"(",
"g",
"*",
"SimpleGraph",
")",
"AddEdge",
"(",
"v",
",",
"w",
"interface",
"{",
"}",
")",
"error",
"{",
"g",
".",
"mutex",
".",
"Lock",
"(",
")",
"\n",
"defer",
"g",
".",
"mutex",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"v",
"==",
"w",
"{",
"return",
"ErrSelfLoop",
"\n",
"}",
"\n\n",
"g",
".",
"addVertex",
"(",
"v",
")",
"\n",
"g",
".",
"addVertex",
"(",
"w",
")",
"\n\n",
"if",
"_",
",",
"ok",
":=",
"g",
".",
"adjacencyList",
"[",
"v",
"]",
"[",
"w",
"]",
";",
"ok",
"{",
"return",
"ErrParallelEdge",
"\n",
"}",
"\n\n",
"g",
".",
"adjacencyList",
"[",
"v",
"]",
"[",
"w",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"g",
".",
"adjacencyList",
"[",
"w",
"]",
"[",
"v",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"g",
".",
"e",
"++",
"\n",
"return",
"nil",
"\n",
"}"
] |
// AddEdge will create an edge between vertices v and w
|
[
"AddEdge",
"will",
"create",
"an",
"edge",
"between",
"vertices",
"v",
"and",
"w"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/graph/simple.go#L68-L87
|
train
|
Workiva/go-datastructures
|
graph/simple.go
|
Adj
|
func (g *SimpleGraph) Adj(v interface{}) ([]interface{}, error) {
g.mutex.RLock()
defer g.mutex.RUnlock()
deg, err := g.Degree(v)
if err != nil {
return nil, ErrVertexNotFound
}
adj := make([]interface{}, deg)
i := 0
for key := range g.adjacencyList[v] {
adj[i] = key
i++
}
return adj, nil
}
|
go
|
func (g *SimpleGraph) Adj(v interface{}) ([]interface{}, error) {
g.mutex.RLock()
defer g.mutex.RUnlock()
deg, err := g.Degree(v)
if err != nil {
return nil, ErrVertexNotFound
}
adj := make([]interface{}, deg)
i := 0
for key := range g.adjacencyList[v] {
adj[i] = key
i++
}
return adj, nil
}
|
[
"func",
"(",
"g",
"*",
"SimpleGraph",
")",
"Adj",
"(",
"v",
"interface",
"{",
"}",
")",
"(",
"[",
"]",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"g",
".",
"mutex",
".",
"RLock",
"(",
")",
"\n",
"defer",
"g",
".",
"mutex",
".",
"RUnlock",
"(",
")",
"\n\n",
"deg",
",",
"err",
":=",
"g",
".",
"Degree",
"(",
"v",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"ErrVertexNotFound",
"\n",
"}",
"\n\n",
"adj",
":=",
"make",
"(",
"[",
"]",
"interface",
"{",
"}",
",",
"deg",
")",
"\n",
"i",
":=",
"0",
"\n",
"for",
"key",
":=",
"range",
"g",
".",
"adjacencyList",
"[",
"v",
"]",
"{",
"adj",
"[",
"i",
"]",
"=",
"key",
"\n",
"i",
"++",
"\n",
"}",
"\n",
"return",
"adj",
",",
"nil",
"\n",
"}"
] |
// Adj returns the list of all vertices connected to v
|
[
"Adj",
"returns",
"the",
"list",
"of",
"all",
"vertices",
"connected",
"to",
"v"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/graph/simple.go#L90-L106
|
train
|
Workiva/go-datastructures
|
graph/simple.go
|
Degree
|
func (g *SimpleGraph) Degree(v interface{}) (int, error) {
g.mutex.RLock()
defer g.mutex.RUnlock()
val, ok := g.adjacencyList[v]
if !ok {
return 0, ErrVertexNotFound
}
return len(val), nil
}
|
go
|
func (g *SimpleGraph) Degree(v interface{}) (int, error) {
g.mutex.RLock()
defer g.mutex.RUnlock()
val, ok := g.adjacencyList[v]
if !ok {
return 0, ErrVertexNotFound
}
return len(val), nil
}
|
[
"func",
"(",
"g",
"*",
"SimpleGraph",
")",
"Degree",
"(",
"v",
"interface",
"{",
"}",
")",
"(",
"int",
",",
"error",
")",
"{",
"g",
".",
"mutex",
".",
"RLock",
"(",
")",
"\n",
"defer",
"g",
".",
"mutex",
".",
"RUnlock",
"(",
")",
"\n\n",
"val",
",",
"ok",
":=",
"g",
".",
"adjacencyList",
"[",
"v",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"0",
",",
"ErrVertexNotFound",
"\n",
"}",
"\n",
"return",
"len",
"(",
"val",
")",
",",
"nil",
"\n",
"}"
] |
// Degree returns the number of vertices connected to v
|
[
"Degree",
"returns",
"the",
"number",
"of",
"vertices",
"connected",
"to",
"v"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/graph/simple.go#L109-L118
|
train
|
Workiva/go-datastructures
|
btree/immutable/path.go
|
pop
|
func (p *path) pop() *pathBundle {
if pb := p.tail; pb != nil {
p.tail = pb.prev
pb.prev = nil
return pb
}
return nil
}
|
go
|
func (p *path) pop() *pathBundle {
if pb := p.tail; pb != nil {
p.tail = pb.prev
pb.prev = nil
return pb
}
return nil
}
|
[
"func",
"(",
"p",
"*",
"path",
")",
"pop",
"(",
")",
"*",
"pathBundle",
"{",
"if",
"pb",
":=",
"p",
".",
"tail",
";",
"pb",
"!=",
"nil",
"{",
"p",
".",
"tail",
"=",
"pb",
".",
"prev",
"\n",
"pb",
".",
"prev",
"=",
"nil",
"\n",
"return",
"pb",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// pop removes the last item from the path. Note that it also nils
// out the returned pathBundle's prev field. Returns nil if no items
// remain.
|
[
"pop",
"removes",
"the",
"last",
"item",
"from",
"the",
"path",
".",
"Note",
"that",
"it",
"also",
"nils",
"out",
"the",
"returned",
"pathBundle",
"s",
"prev",
"field",
".",
"Returns",
"nil",
"if",
"no",
"items",
"remain",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/path.go#L53-L61
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
copyToGen
|
func (i *iNode) copyToGen(gen *generation, ctrie *Ctrie) *iNode {
nin := &iNode{gen: gen}
main := gcasRead(i, ctrie)
atomic.StorePointer(
(*unsafe.Pointer)(unsafe.Pointer(&nin.main)), unsafe.Pointer(main))
return nin
}
|
go
|
func (i *iNode) copyToGen(gen *generation, ctrie *Ctrie) *iNode {
nin := &iNode{gen: gen}
main := gcasRead(i, ctrie)
atomic.StorePointer(
(*unsafe.Pointer)(unsafe.Pointer(&nin.main)), unsafe.Pointer(main))
return nin
}
|
[
"func",
"(",
"i",
"*",
"iNode",
")",
"copyToGen",
"(",
"gen",
"*",
"generation",
",",
"ctrie",
"*",
"Ctrie",
")",
"*",
"iNode",
"{",
"nin",
":=",
"&",
"iNode",
"{",
"gen",
":",
"gen",
"}",
"\n",
"main",
":=",
"gcasRead",
"(",
"i",
",",
"ctrie",
")",
"\n",
"atomic",
".",
"StorePointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"nin",
".",
"main",
")",
")",
",",
"unsafe",
".",
"Pointer",
"(",
"main",
")",
")",
"\n",
"return",
"nin",
"\n",
"}"
] |
// copyToGen returns a copy of this I-node copied to the given generation.
|
[
"copyToGen",
"returns",
"a",
"copy",
"of",
"this",
"I",
"-",
"node",
"copied",
"to",
"the",
"given",
"generation",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L80-L86
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
newMainNode
|
func newMainNode(x *sNode, xhc uint32, y *sNode, yhc uint32, lev uint, gen *generation) *mainNode {
if lev < exp2 {
xidx := (xhc >> lev) & 0x1f
yidx := (yhc >> lev) & 0x1f
bmp := uint32((1 << xidx) | (1 << yidx))
if xidx == yidx {
// Recurse when indexes are equal.
main := newMainNode(x, xhc, y, yhc, lev+w, gen)
iNode := &iNode{main: main, gen: gen}
return &mainNode{cNode: &cNode{bmp, []branch{iNode}, gen}}
}
if xidx < yidx {
return &mainNode{cNode: &cNode{bmp, []branch{x, y}, gen}}
}
return &mainNode{cNode: &cNode{bmp, []branch{y, x}, gen}}
}
l := list.Empty.Add(x).Add(y)
return &mainNode{lNode: &lNode{l}}
}
|
go
|
func newMainNode(x *sNode, xhc uint32, y *sNode, yhc uint32, lev uint, gen *generation) *mainNode {
if lev < exp2 {
xidx := (xhc >> lev) & 0x1f
yidx := (yhc >> lev) & 0x1f
bmp := uint32((1 << xidx) | (1 << yidx))
if xidx == yidx {
// Recurse when indexes are equal.
main := newMainNode(x, xhc, y, yhc, lev+w, gen)
iNode := &iNode{main: main, gen: gen}
return &mainNode{cNode: &cNode{bmp, []branch{iNode}, gen}}
}
if xidx < yidx {
return &mainNode{cNode: &cNode{bmp, []branch{x, y}, gen}}
}
return &mainNode{cNode: &cNode{bmp, []branch{y, x}, gen}}
}
l := list.Empty.Add(x).Add(y)
return &mainNode{lNode: &lNode{l}}
}
|
[
"func",
"newMainNode",
"(",
"x",
"*",
"sNode",
",",
"xhc",
"uint32",
",",
"y",
"*",
"sNode",
",",
"yhc",
"uint32",
",",
"lev",
"uint",
",",
"gen",
"*",
"generation",
")",
"*",
"mainNode",
"{",
"if",
"lev",
"<",
"exp2",
"{",
"xidx",
":=",
"(",
"xhc",
">>",
"lev",
")",
"&",
"0x1f",
"\n",
"yidx",
":=",
"(",
"yhc",
">>",
"lev",
")",
"&",
"0x1f",
"\n",
"bmp",
":=",
"uint32",
"(",
"(",
"1",
"<<",
"xidx",
")",
"|",
"(",
"1",
"<<",
"yidx",
")",
")",
"\n\n",
"if",
"xidx",
"==",
"yidx",
"{",
"// Recurse when indexes are equal.",
"main",
":=",
"newMainNode",
"(",
"x",
",",
"xhc",
",",
"y",
",",
"yhc",
",",
"lev",
"+",
"w",
",",
"gen",
")",
"\n",
"iNode",
":=",
"&",
"iNode",
"{",
"main",
":",
"main",
",",
"gen",
":",
"gen",
"}",
"\n",
"return",
"&",
"mainNode",
"{",
"cNode",
":",
"&",
"cNode",
"{",
"bmp",
",",
"[",
"]",
"branch",
"{",
"iNode",
"}",
",",
"gen",
"}",
"}",
"\n",
"}",
"\n",
"if",
"xidx",
"<",
"yidx",
"{",
"return",
"&",
"mainNode",
"{",
"cNode",
":",
"&",
"cNode",
"{",
"bmp",
",",
"[",
"]",
"branch",
"{",
"x",
",",
"y",
"}",
",",
"gen",
"}",
"}",
"\n",
"}",
"\n",
"return",
"&",
"mainNode",
"{",
"cNode",
":",
"&",
"cNode",
"{",
"bmp",
",",
"[",
"]",
"branch",
"{",
"y",
",",
"x",
"}",
",",
"gen",
"}",
"}",
"\n",
"}",
"\n",
"l",
":=",
"list",
".",
"Empty",
".",
"Add",
"(",
"x",
")",
".",
"Add",
"(",
"y",
")",
"\n",
"return",
"&",
"mainNode",
"{",
"lNode",
":",
"&",
"lNode",
"{",
"l",
"}",
"}",
"\n",
"}"
] |
// newMainNode is a recursive constructor which creates a new mainNode. This
// mainNode will consist of cNodes as long as the hashcode chunks of the two
// keys are equal at the given level. If the level exceeds 2^w, an lNode is
// created.
|
[
"newMainNode",
"is",
"a",
"recursive",
"constructor",
"which",
"creates",
"a",
"new",
"mainNode",
".",
"This",
"mainNode",
"will",
"consist",
"of",
"cNodes",
"as",
"long",
"as",
"the",
"hashcode",
"chunks",
"of",
"the",
"two",
"keys",
"are",
"equal",
"at",
"the",
"given",
"level",
".",
"If",
"the",
"level",
"exceeds",
"2^w",
"an",
"lNode",
"is",
"created",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L116-L135
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
inserted
|
func (c *cNode) inserted(pos, flag uint32, br branch, gen *generation) *cNode {
length := uint32(len(c.array))
bmp := c.bmp
array := make([]branch, length+1)
copy(array, c.array)
array[pos] = br
for i, x := pos, uint32(0); x < length-pos; i++ {
array[i+1] = c.array[i]
x++
}
ncn := &cNode{bmp: bmp | flag, array: array, gen: gen}
return ncn
}
|
go
|
func (c *cNode) inserted(pos, flag uint32, br branch, gen *generation) *cNode {
length := uint32(len(c.array))
bmp := c.bmp
array := make([]branch, length+1)
copy(array, c.array)
array[pos] = br
for i, x := pos, uint32(0); x < length-pos; i++ {
array[i+1] = c.array[i]
x++
}
ncn := &cNode{bmp: bmp | flag, array: array, gen: gen}
return ncn
}
|
[
"func",
"(",
"c",
"*",
"cNode",
")",
"inserted",
"(",
"pos",
",",
"flag",
"uint32",
",",
"br",
"branch",
",",
"gen",
"*",
"generation",
")",
"*",
"cNode",
"{",
"length",
":=",
"uint32",
"(",
"len",
"(",
"c",
".",
"array",
")",
")",
"\n",
"bmp",
":=",
"c",
".",
"bmp",
"\n",
"array",
":=",
"make",
"(",
"[",
"]",
"branch",
",",
"length",
"+",
"1",
")",
"\n",
"copy",
"(",
"array",
",",
"c",
".",
"array",
")",
"\n",
"array",
"[",
"pos",
"]",
"=",
"br",
"\n",
"for",
"i",
",",
"x",
":=",
"pos",
",",
"uint32",
"(",
"0",
")",
";",
"x",
"<",
"length",
"-",
"pos",
";",
"i",
"++",
"{",
"array",
"[",
"i",
"+",
"1",
"]",
"=",
"c",
".",
"array",
"[",
"i",
"]",
"\n",
"x",
"++",
"\n",
"}",
"\n",
"ncn",
":=",
"&",
"cNode",
"{",
"bmp",
":",
"bmp",
"|",
"flag",
",",
"array",
":",
"array",
",",
"gen",
":",
"gen",
"}",
"\n",
"return",
"ncn",
"\n",
"}"
] |
// inserted returns a copy of this cNode with the new entry at the given
// position.
|
[
"inserted",
"returns",
"a",
"copy",
"of",
"this",
"cNode",
"with",
"the",
"new",
"entry",
"at",
"the",
"given",
"position",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L139-L151
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
updated
|
func (c *cNode) updated(pos uint32, br branch, gen *generation) *cNode {
array := make([]branch, len(c.array))
copy(array, c.array)
array[pos] = br
ncn := &cNode{bmp: c.bmp, array: array, gen: gen}
return ncn
}
|
go
|
func (c *cNode) updated(pos uint32, br branch, gen *generation) *cNode {
array := make([]branch, len(c.array))
copy(array, c.array)
array[pos] = br
ncn := &cNode{bmp: c.bmp, array: array, gen: gen}
return ncn
}
|
[
"func",
"(",
"c",
"*",
"cNode",
")",
"updated",
"(",
"pos",
"uint32",
",",
"br",
"branch",
",",
"gen",
"*",
"generation",
")",
"*",
"cNode",
"{",
"array",
":=",
"make",
"(",
"[",
"]",
"branch",
",",
"len",
"(",
"c",
".",
"array",
")",
")",
"\n",
"copy",
"(",
"array",
",",
"c",
".",
"array",
")",
"\n",
"array",
"[",
"pos",
"]",
"=",
"br",
"\n",
"ncn",
":=",
"&",
"cNode",
"{",
"bmp",
":",
"c",
".",
"bmp",
",",
"array",
":",
"array",
",",
"gen",
":",
"gen",
"}",
"\n",
"return",
"ncn",
"\n",
"}"
] |
// updated returns a copy of this cNode with the entry at the given index
// updated.
|
[
"updated",
"returns",
"a",
"copy",
"of",
"this",
"cNode",
"with",
"the",
"entry",
"at",
"the",
"given",
"index",
"updated",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L155-L161
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
renewed
|
func (c *cNode) renewed(gen *generation, ctrie *Ctrie) *cNode {
array := make([]branch, len(c.array))
for i, br := range c.array {
switch t := br.(type) {
case *iNode:
array[i] = t.copyToGen(gen, ctrie)
default:
array[i] = br
}
}
return &cNode{bmp: c.bmp, array: array, gen: gen}
}
|
go
|
func (c *cNode) renewed(gen *generation, ctrie *Ctrie) *cNode {
array := make([]branch, len(c.array))
for i, br := range c.array {
switch t := br.(type) {
case *iNode:
array[i] = t.copyToGen(gen, ctrie)
default:
array[i] = br
}
}
return &cNode{bmp: c.bmp, array: array, gen: gen}
}
|
[
"func",
"(",
"c",
"*",
"cNode",
")",
"renewed",
"(",
"gen",
"*",
"generation",
",",
"ctrie",
"*",
"Ctrie",
")",
"*",
"cNode",
"{",
"array",
":=",
"make",
"(",
"[",
"]",
"branch",
",",
"len",
"(",
"c",
".",
"array",
")",
")",
"\n",
"for",
"i",
",",
"br",
":=",
"range",
"c",
".",
"array",
"{",
"switch",
"t",
":=",
"br",
".",
"(",
"type",
")",
"{",
"case",
"*",
"iNode",
":",
"array",
"[",
"i",
"]",
"=",
"t",
".",
"copyToGen",
"(",
"gen",
",",
"ctrie",
")",
"\n",
"default",
":",
"array",
"[",
"i",
"]",
"=",
"br",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"&",
"cNode",
"{",
"bmp",
":",
"c",
".",
"bmp",
",",
"array",
":",
"array",
",",
"gen",
":",
"gen",
"}",
"\n",
"}"
] |
// renewed returns a copy of this cNode with the I-nodes below it copied to the
// given generation.
|
[
"renewed",
"returns",
"a",
"copy",
"of",
"this",
"cNode",
"with",
"the",
"I",
"-",
"nodes",
"below",
"it",
"copied",
"to",
"the",
"given",
"generation",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L182-L193
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
untombed
|
func (t *tNode) untombed() *sNode {
return &sNode{&Entry{Key: t.Key, hash: t.hash, Value: t.Value}}
}
|
go
|
func (t *tNode) untombed() *sNode {
return &sNode{&Entry{Key: t.Key, hash: t.hash, Value: t.Value}}
}
|
[
"func",
"(",
"t",
"*",
"tNode",
")",
"untombed",
"(",
")",
"*",
"sNode",
"{",
"return",
"&",
"sNode",
"{",
"&",
"Entry",
"{",
"Key",
":",
"t",
".",
"Key",
",",
"hash",
":",
"t",
".",
"hash",
",",
"Value",
":",
"t",
".",
"Value",
"}",
"}",
"\n",
"}"
] |
// untombed returns the S-node contained by the T-node.
|
[
"untombed",
"returns",
"the",
"S",
"-",
"node",
"contained",
"by",
"the",
"T",
"-",
"node",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L202-L204
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
entry
|
func (l *lNode) entry() *sNode {
head, _ := l.Head()
return head.(*sNode)
}
|
go
|
func (l *lNode) entry() *sNode {
head, _ := l.Head()
return head.(*sNode)
}
|
[
"func",
"(",
"l",
"*",
"lNode",
")",
"entry",
"(",
")",
"*",
"sNode",
"{",
"head",
",",
"_",
":=",
"l",
".",
"Head",
"(",
")",
"\n",
"return",
"head",
".",
"(",
"*",
"sNode",
")",
"\n",
"}"
] |
// entry returns the first S-node contained in the L-node.
|
[
"entry",
"returns",
"the",
"first",
"S",
"-",
"node",
"contained",
"in",
"the",
"L",
"-",
"node",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L213-L216
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
lookup
|
func (l *lNode) lookup(e *Entry) (interface{}, bool) {
found, ok := l.Find(func(sn interface{}) bool {
return bytes.Equal(e.Key, sn.(*sNode).Key)
})
if !ok {
return nil, false
}
return found.(*sNode).Value, true
}
|
go
|
func (l *lNode) lookup(e *Entry) (interface{}, bool) {
found, ok := l.Find(func(sn interface{}) bool {
return bytes.Equal(e.Key, sn.(*sNode).Key)
})
if !ok {
return nil, false
}
return found.(*sNode).Value, true
}
|
[
"func",
"(",
"l",
"*",
"lNode",
")",
"lookup",
"(",
"e",
"*",
"Entry",
")",
"(",
"interface",
"{",
"}",
",",
"bool",
")",
"{",
"found",
",",
"ok",
":=",
"l",
".",
"Find",
"(",
"func",
"(",
"sn",
"interface",
"{",
"}",
")",
"bool",
"{",
"return",
"bytes",
".",
"Equal",
"(",
"e",
".",
"Key",
",",
"sn",
".",
"(",
"*",
"sNode",
")",
".",
"Key",
")",
"\n",
"}",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"false",
"\n",
"}",
"\n",
"return",
"found",
".",
"(",
"*",
"sNode",
")",
".",
"Value",
",",
"true",
"\n",
"}"
] |
// lookup returns the value at the given entry in the L-node or returns false
// if it's not contained.
|
[
"lookup",
"returns",
"the",
"value",
"at",
"the",
"given",
"entry",
"in",
"the",
"L",
"-",
"node",
"or",
"returns",
"false",
"if",
"it",
"s",
"not",
"contained",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L220-L228
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
inserted
|
func (l *lNode) inserted(entry *Entry) *lNode {
return &lNode{l.removed(entry).Add(&sNode{entry})}
}
|
go
|
func (l *lNode) inserted(entry *Entry) *lNode {
return &lNode{l.removed(entry).Add(&sNode{entry})}
}
|
[
"func",
"(",
"l",
"*",
"lNode",
")",
"inserted",
"(",
"entry",
"*",
"Entry",
")",
"*",
"lNode",
"{",
"return",
"&",
"lNode",
"{",
"l",
".",
"removed",
"(",
"entry",
")",
".",
"Add",
"(",
"&",
"sNode",
"{",
"entry",
"}",
")",
"}",
"\n",
"}"
] |
// inserted creates a new L-node with the added entry.
|
[
"inserted",
"creates",
"a",
"new",
"L",
"-",
"node",
"with",
"the",
"added",
"entry",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L231-L233
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
removed
|
func (l *lNode) removed(e *Entry) *lNode {
idx := l.FindIndex(func(sn interface{}) bool {
return bytes.Equal(e.Key, sn.(*sNode).Key)
})
if idx < 0 {
return l
}
nl, _ := l.Remove(uint(idx))
return &lNode{nl}
}
|
go
|
func (l *lNode) removed(e *Entry) *lNode {
idx := l.FindIndex(func(sn interface{}) bool {
return bytes.Equal(e.Key, sn.(*sNode).Key)
})
if idx < 0 {
return l
}
nl, _ := l.Remove(uint(idx))
return &lNode{nl}
}
|
[
"func",
"(",
"l",
"*",
"lNode",
")",
"removed",
"(",
"e",
"*",
"Entry",
")",
"*",
"lNode",
"{",
"idx",
":=",
"l",
".",
"FindIndex",
"(",
"func",
"(",
"sn",
"interface",
"{",
"}",
")",
"bool",
"{",
"return",
"bytes",
".",
"Equal",
"(",
"e",
".",
"Key",
",",
"sn",
".",
"(",
"*",
"sNode",
")",
".",
"Key",
")",
"\n",
"}",
")",
"\n",
"if",
"idx",
"<",
"0",
"{",
"return",
"l",
"\n",
"}",
"\n",
"nl",
",",
"_",
":=",
"l",
".",
"Remove",
"(",
"uint",
"(",
"idx",
")",
")",
"\n",
"return",
"&",
"lNode",
"{",
"nl",
"}",
"\n",
"}"
] |
// removed creates a new L-node with the entry removed.
|
[
"removed",
"creates",
"a",
"new",
"L",
"-",
"node",
"with",
"the",
"entry",
"removed",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L236-L245
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
New
|
func New(hashFactory HashFactory) *Ctrie {
if hashFactory == nil {
hashFactory = defaultHashFactory
}
root := &iNode{main: &mainNode{cNode: &cNode{}}}
return newCtrie(root, hashFactory, false)
}
|
go
|
func New(hashFactory HashFactory) *Ctrie {
if hashFactory == nil {
hashFactory = defaultHashFactory
}
root := &iNode{main: &mainNode{cNode: &cNode{}}}
return newCtrie(root, hashFactory, false)
}
|
[
"func",
"New",
"(",
"hashFactory",
"HashFactory",
")",
"*",
"Ctrie",
"{",
"if",
"hashFactory",
"==",
"nil",
"{",
"hashFactory",
"=",
"defaultHashFactory",
"\n",
"}",
"\n",
"root",
":=",
"&",
"iNode",
"{",
"main",
":",
"&",
"mainNode",
"{",
"cNode",
":",
"&",
"cNode",
"{",
"}",
"}",
"}",
"\n",
"return",
"newCtrie",
"(",
"root",
",",
"hashFactory",
",",
"false",
")",
"\n",
"}"
] |
// New creates an empty Ctrie which uses the provided HashFactory for key
// hashing. If nil is passed in, it will default to FNV-1a hashing.
|
[
"New",
"creates",
"an",
"empty",
"Ctrie",
"which",
"uses",
"the",
"provided",
"HashFactory",
"for",
"key",
"hashing",
".",
"If",
"nil",
"is",
"passed",
"in",
"it",
"will",
"default",
"to",
"FNV",
"-",
"1a",
"hashing",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L269-L275
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
Insert
|
func (c *Ctrie) Insert(key []byte, value interface{}) {
c.assertReadWrite()
c.insert(&Entry{
Key: key,
Value: value,
hash: c.hash(key),
})
}
|
go
|
func (c *Ctrie) Insert(key []byte, value interface{}) {
c.assertReadWrite()
c.insert(&Entry{
Key: key,
Value: value,
hash: c.hash(key),
})
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"Insert",
"(",
"key",
"[",
"]",
"byte",
",",
"value",
"interface",
"{",
"}",
")",
"{",
"c",
".",
"assertReadWrite",
"(",
")",
"\n",
"c",
".",
"insert",
"(",
"&",
"Entry",
"{",
"Key",
":",
"key",
",",
"Value",
":",
"value",
",",
"hash",
":",
"c",
".",
"hash",
"(",
"key",
")",
",",
"}",
")",
"\n",
"}"
] |
// Insert adds the key-value pair to the Ctrie, replacing the existing value if
// the key already exists.
|
[
"Insert",
"adds",
"the",
"key",
"-",
"value",
"pair",
"to",
"the",
"Ctrie",
"replacing",
"the",
"existing",
"value",
"if",
"the",
"key",
"already",
"exists",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L287-L294
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
Lookup
|
func (c *Ctrie) Lookup(key []byte) (interface{}, bool) {
return c.lookup(&Entry{Key: key, hash: c.hash(key)})
}
|
go
|
func (c *Ctrie) Lookup(key []byte) (interface{}, bool) {
return c.lookup(&Entry{Key: key, hash: c.hash(key)})
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"Lookup",
"(",
"key",
"[",
"]",
"byte",
")",
"(",
"interface",
"{",
"}",
",",
"bool",
")",
"{",
"return",
"c",
".",
"lookup",
"(",
"&",
"Entry",
"{",
"Key",
":",
"key",
",",
"hash",
":",
"c",
".",
"hash",
"(",
"key",
")",
"}",
")",
"\n",
"}"
] |
// Lookup returns the value for the associated key or returns false if the key
// doesn't exist.
|
[
"Lookup",
"returns",
"the",
"value",
"for",
"the",
"associated",
"key",
"or",
"returns",
"false",
"if",
"the",
"key",
"doesn",
"t",
"exist",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L298-L300
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
Remove
|
func (c *Ctrie) Remove(key []byte) (interface{}, bool) {
c.assertReadWrite()
return c.remove(&Entry{Key: key, hash: c.hash(key)})
}
|
go
|
func (c *Ctrie) Remove(key []byte) (interface{}, bool) {
c.assertReadWrite()
return c.remove(&Entry{Key: key, hash: c.hash(key)})
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"Remove",
"(",
"key",
"[",
"]",
"byte",
")",
"(",
"interface",
"{",
"}",
",",
"bool",
")",
"{",
"c",
".",
"assertReadWrite",
"(",
")",
"\n",
"return",
"c",
".",
"remove",
"(",
"&",
"Entry",
"{",
"Key",
":",
"key",
",",
"hash",
":",
"c",
".",
"hash",
"(",
"key",
")",
"}",
")",
"\n",
"}"
] |
// Remove deletes the value for the associated key, returning true if it was
// removed or false if the entry doesn't exist.
|
[
"Remove",
"deletes",
"the",
"value",
"for",
"the",
"associated",
"key",
"returning",
"true",
"if",
"it",
"was",
"removed",
"or",
"false",
"if",
"the",
"entry",
"doesn",
"t",
"exist",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L304-L307
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
snapshot
|
func (c *Ctrie) snapshot(readOnly bool) *Ctrie {
if readOnly && c.readOnly {
return c
}
for {
root := c.readRoot()
main := gcasRead(root, c)
if c.rdcssRoot(root, main, root.copyToGen(&generation{}, c)) {
if readOnly {
// For a read-only snapshot, we can share the old generation
// root.
return newCtrie(root, c.hashFactory, readOnly)
}
// For a read-write snapshot, we need to take a copy of the root
// in the new generation.
return newCtrie(c.readRoot().copyToGen(&generation{}, c), c.hashFactory, readOnly)
}
}
}
|
go
|
func (c *Ctrie) snapshot(readOnly bool) *Ctrie {
if readOnly && c.readOnly {
return c
}
for {
root := c.readRoot()
main := gcasRead(root, c)
if c.rdcssRoot(root, main, root.copyToGen(&generation{}, c)) {
if readOnly {
// For a read-only snapshot, we can share the old generation
// root.
return newCtrie(root, c.hashFactory, readOnly)
}
// For a read-write snapshot, we need to take a copy of the root
// in the new generation.
return newCtrie(c.readRoot().copyToGen(&generation{}, c), c.hashFactory, readOnly)
}
}
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"snapshot",
"(",
"readOnly",
"bool",
")",
"*",
"Ctrie",
"{",
"if",
"readOnly",
"&&",
"c",
".",
"readOnly",
"{",
"return",
"c",
"\n",
"}",
"\n",
"for",
"{",
"root",
":=",
"c",
".",
"readRoot",
"(",
")",
"\n",
"main",
":=",
"gcasRead",
"(",
"root",
",",
"c",
")",
"\n",
"if",
"c",
".",
"rdcssRoot",
"(",
"root",
",",
"main",
",",
"root",
".",
"copyToGen",
"(",
"&",
"generation",
"{",
"}",
",",
"c",
")",
")",
"{",
"if",
"readOnly",
"{",
"// For a read-only snapshot, we can share the old generation",
"// root.",
"return",
"newCtrie",
"(",
"root",
",",
"c",
".",
"hashFactory",
",",
"readOnly",
")",
"\n",
"}",
"\n",
"// For a read-write snapshot, we need to take a copy of the root",
"// in the new generation.",
"return",
"newCtrie",
"(",
"c",
".",
"readRoot",
"(",
")",
".",
"copyToGen",
"(",
"&",
"generation",
"{",
"}",
",",
"c",
")",
",",
"c",
".",
"hashFactory",
",",
"readOnly",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// snapshot wraps up the CAS logic to make a snapshot or a read-only snapshot.
|
[
"snapshot",
"wraps",
"up",
"the",
"CAS",
"logic",
"to",
"make",
"a",
"snapshot",
"or",
"a",
"read",
"-",
"only",
"snapshot",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L322-L340
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
Clear
|
func (c *Ctrie) Clear() {
for {
root := c.readRoot()
gen := &generation{}
newRoot := &iNode{
main: &mainNode{cNode: &cNode{array: make([]branch, 0), gen: gen}},
gen: gen,
}
if c.rdcssRoot(root, gcasRead(root, c), newRoot) {
return
}
}
}
|
go
|
func (c *Ctrie) Clear() {
for {
root := c.readRoot()
gen := &generation{}
newRoot := &iNode{
main: &mainNode{cNode: &cNode{array: make([]branch, 0), gen: gen}},
gen: gen,
}
if c.rdcssRoot(root, gcasRead(root, c), newRoot) {
return
}
}
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"Clear",
"(",
")",
"{",
"for",
"{",
"root",
":=",
"c",
".",
"readRoot",
"(",
")",
"\n",
"gen",
":=",
"&",
"generation",
"{",
"}",
"\n",
"newRoot",
":=",
"&",
"iNode",
"{",
"main",
":",
"&",
"mainNode",
"{",
"cNode",
":",
"&",
"cNode",
"{",
"array",
":",
"make",
"(",
"[",
"]",
"branch",
",",
"0",
")",
",",
"gen",
":",
"gen",
"}",
"}",
",",
"gen",
":",
"gen",
",",
"}",
"\n",
"if",
"c",
".",
"rdcssRoot",
"(",
"root",
",",
"gcasRead",
"(",
"root",
",",
"c",
")",
",",
"newRoot",
")",
"{",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Clear removes all keys from the Ctrie.
|
[
"Clear",
"removes",
"all",
"keys",
"from",
"the",
"Ctrie",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L343-L355
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
Iterator
|
func (c *Ctrie) Iterator(cancel <-chan struct{}) <-chan *Entry {
ch := make(chan *Entry)
snapshot := c.ReadOnlySnapshot()
go func() {
snapshot.traverse(snapshot.readRoot(), ch, cancel)
close(ch)
}()
return ch
}
|
go
|
func (c *Ctrie) Iterator(cancel <-chan struct{}) <-chan *Entry {
ch := make(chan *Entry)
snapshot := c.ReadOnlySnapshot()
go func() {
snapshot.traverse(snapshot.readRoot(), ch, cancel)
close(ch)
}()
return ch
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"Iterator",
"(",
"cancel",
"<-",
"chan",
"struct",
"{",
"}",
")",
"<-",
"chan",
"*",
"Entry",
"{",
"ch",
":=",
"make",
"(",
"chan",
"*",
"Entry",
")",
"\n",
"snapshot",
":=",
"c",
".",
"ReadOnlySnapshot",
"(",
")",
"\n",
"go",
"func",
"(",
")",
"{",
"snapshot",
".",
"traverse",
"(",
"snapshot",
".",
"readRoot",
"(",
")",
",",
"ch",
",",
"cancel",
")",
"\n",
"close",
"(",
"ch",
")",
"\n",
"}",
"(",
")",
"\n",
"return",
"ch",
"\n",
"}"
] |
// Iterator returns a channel which yields the Entries of the Ctrie. If a
// cancel channel is provided, closing it will terminate and close the iterator
// channel. Note that if a cancel channel is not used and not every entry is
// read from the iterator, a goroutine will leak.
|
[
"Iterator",
"returns",
"a",
"channel",
"which",
"yields",
"the",
"Entries",
"of",
"the",
"Ctrie",
".",
"If",
"a",
"cancel",
"channel",
"is",
"provided",
"closing",
"it",
"will",
"terminate",
"and",
"close",
"the",
"iterator",
"channel",
".",
"Note",
"that",
"if",
"a",
"cancel",
"channel",
"is",
"not",
"used",
"and",
"not",
"every",
"entry",
"is",
"read",
"from",
"the",
"iterator",
"a",
"goroutine",
"will",
"leak",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L361-L369
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
Size
|
func (c *Ctrie) Size() uint {
// TODO: The size operation can be optimized further by caching the size
// information in main nodes of a read-only Ctrie – this reduces the
// amortized complexity of the size operation to O(1) because the size
// computation is amortized across the update operations that occurred
// since the last snapshot.
size := uint(0)
for _ = range c.Iterator(nil) {
size++
}
return size
}
|
go
|
func (c *Ctrie) Size() uint {
// TODO: The size operation can be optimized further by caching the size
// information in main nodes of a read-only Ctrie – this reduces the
// amortized complexity of the size operation to O(1) because the size
// computation is amortized across the update operations that occurred
// since the last snapshot.
size := uint(0)
for _ = range c.Iterator(nil) {
size++
}
return size
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"Size",
"(",
")",
"uint",
"{",
"// TODO: The size operation can be optimized further by caching the size",
"// information in main nodes of a read-only Ctrie – this reduces the",
"// amortized complexity of the size operation to O(1) because the size",
"// computation is amortized across the update operations that occurred",
"// since the last snapshot.",
"size",
":=",
"uint",
"(",
"0",
")",
"\n",
"for",
"_",
"=",
"range",
"c",
".",
"Iterator",
"(",
"nil",
")",
"{",
"size",
"++",
"\n",
"}",
"\n",
"return",
"size",
"\n",
"}"
] |
// Size returns the number of keys in the Ctrie.
|
[
"Size",
"returns",
"the",
"number",
"of",
"keys",
"in",
"the",
"Ctrie",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L372-L383
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
iinsert
|
func (c *Ctrie) iinsert(i *iNode, entry *Entry, lev uint, parent *iNode, startGen *generation) bool {
// Linearization point.
main := gcasRead(i, c)
switch {
case main.cNode != nil:
cn := main.cNode
flag, pos := flagPos(entry.hash, lev, cn.bmp)
if cn.bmp&flag == 0 {
// If the relevant bit is not in the bitmap, then a copy of the
// cNode with the new entry is created. The linearization point is
// a successful CAS.
rn := cn
if cn.gen != i.gen {
rn = cn.renewed(i.gen, c)
}
ncn := &mainNode{cNode: rn.inserted(pos, flag, &sNode{entry}, i.gen)}
return gcas(i, main, ncn, c)
}
// If the relevant bit is present in the bitmap, then its corresponding
// branch is read from the array.
branch := cn.array[pos]
switch branch.(type) {
case *iNode:
// If the branch is an I-node, then iinsert is called recursively.
in := branch.(*iNode)
if startGen == in.gen {
return c.iinsert(in, entry, lev+w, i, startGen)
}
if gcas(i, main, &mainNode{cNode: cn.renewed(startGen, c)}, c) {
return c.iinsert(i, entry, lev, parent, startGen)
}
return false
case *sNode:
sn := branch.(*sNode)
if !bytes.Equal(sn.Key, entry.Key) {
// If the branch is an S-node and its key is not equal to the
// key being inserted, then the Ctrie has to be extended with
// an additional level. The C-node is replaced with its updated
// version, created using the updated function that adds a new
// I-node at the respective position. The new Inode has its
// main node pointing to a C-node with both keys. The
// linearization point is a successful CAS.
rn := cn
if cn.gen != i.gen {
rn = cn.renewed(i.gen, c)
}
nsn := &sNode{entry}
nin := &iNode{main: newMainNode(sn, sn.hash, nsn, nsn.hash, lev+w, i.gen), gen: i.gen}
ncn := &mainNode{cNode: rn.updated(pos, nin, i.gen)}
return gcas(i, main, ncn, c)
}
// If the key in the S-node is equal to the key being inserted,
// then the C-node is replaced with its updated version with a new
// S-node. The linearization point is a successful CAS.
ncn := &mainNode{cNode: cn.updated(pos, &sNode{entry}, i.gen)}
return gcas(i, main, ncn, c)
default:
panic("Ctrie is in an invalid state")
}
case main.tNode != nil:
clean(parent, lev-w, c)
return false
case main.lNode != nil:
nln := &mainNode{lNode: main.lNode.inserted(entry)}
return gcas(i, main, nln, c)
default:
panic("Ctrie is in an invalid state")
}
}
|
go
|
func (c *Ctrie) iinsert(i *iNode, entry *Entry, lev uint, parent *iNode, startGen *generation) bool {
// Linearization point.
main := gcasRead(i, c)
switch {
case main.cNode != nil:
cn := main.cNode
flag, pos := flagPos(entry.hash, lev, cn.bmp)
if cn.bmp&flag == 0 {
// If the relevant bit is not in the bitmap, then a copy of the
// cNode with the new entry is created. The linearization point is
// a successful CAS.
rn := cn
if cn.gen != i.gen {
rn = cn.renewed(i.gen, c)
}
ncn := &mainNode{cNode: rn.inserted(pos, flag, &sNode{entry}, i.gen)}
return gcas(i, main, ncn, c)
}
// If the relevant bit is present in the bitmap, then its corresponding
// branch is read from the array.
branch := cn.array[pos]
switch branch.(type) {
case *iNode:
// If the branch is an I-node, then iinsert is called recursively.
in := branch.(*iNode)
if startGen == in.gen {
return c.iinsert(in, entry, lev+w, i, startGen)
}
if gcas(i, main, &mainNode{cNode: cn.renewed(startGen, c)}, c) {
return c.iinsert(i, entry, lev, parent, startGen)
}
return false
case *sNode:
sn := branch.(*sNode)
if !bytes.Equal(sn.Key, entry.Key) {
// If the branch is an S-node and its key is not equal to the
// key being inserted, then the Ctrie has to be extended with
// an additional level. The C-node is replaced with its updated
// version, created using the updated function that adds a new
// I-node at the respective position. The new Inode has its
// main node pointing to a C-node with both keys. The
// linearization point is a successful CAS.
rn := cn
if cn.gen != i.gen {
rn = cn.renewed(i.gen, c)
}
nsn := &sNode{entry}
nin := &iNode{main: newMainNode(sn, sn.hash, nsn, nsn.hash, lev+w, i.gen), gen: i.gen}
ncn := &mainNode{cNode: rn.updated(pos, nin, i.gen)}
return gcas(i, main, ncn, c)
}
// If the key in the S-node is equal to the key being inserted,
// then the C-node is replaced with its updated version with a new
// S-node. The linearization point is a successful CAS.
ncn := &mainNode{cNode: cn.updated(pos, &sNode{entry}, i.gen)}
return gcas(i, main, ncn, c)
default:
panic("Ctrie is in an invalid state")
}
case main.tNode != nil:
clean(parent, lev-w, c)
return false
case main.lNode != nil:
nln := &mainNode{lNode: main.lNode.inserted(entry)}
return gcas(i, main, nln, c)
default:
panic("Ctrie is in an invalid state")
}
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"iinsert",
"(",
"i",
"*",
"iNode",
",",
"entry",
"*",
"Entry",
",",
"lev",
"uint",
",",
"parent",
"*",
"iNode",
",",
"startGen",
"*",
"generation",
")",
"bool",
"{",
"// Linearization point.",
"main",
":=",
"gcasRead",
"(",
"i",
",",
"c",
")",
"\n",
"switch",
"{",
"case",
"main",
".",
"cNode",
"!=",
"nil",
":",
"cn",
":=",
"main",
".",
"cNode",
"\n",
"flag",
",",
"pos",
":=",
"flagPos",
"(",
"entry",
".",
"hash",
",",
"lev",
",",
"cn",
".",
"bmp",
")",
"\n",
"if",
"cn",
".",
"bmp",
"&",
"flag",
"==",
"0",
"{",
"// If the relevant bit is not in the bitmap, then a copy of the",
"// cNode with the new entry is created. The linearization point is",
"// a successful CAS.",
"rn",
":=",
"cn",
"\n",
"if",
"cn",
".",
"gen",
"!=",
"i",
".",
"gen",
"{",
"rn",
"=",
"cn",
".",
"renewed",
"(",
"i",
".",
"gen",
",",
"c",
")",
"\n",
"}",
"\n",
"ncn",
":=",
"&",
"mainNode",
"{",
"cNode",
":",
"rn",
".",
"inserted",
"(",
"pos",
",",
"flag",
",",
"&",
"sNode",
"{",
"entry",
"}",
",",
"i",
".",
"gen",
")",
"}",
"\n",
"return",
"gcas",
"(",
"i",
",",
"main",
",",
"ncn",
",",
"c",
")",
"\n",
"}",
"\n",
"// If the relevant bit is present in the bitmap, then its corresponding",
"// branch is read from the array.",
"branch",
":=",
"cn",
".",
"array",
"[",
"pos",
"]",
"\n",
"switch",
"branch",
".",
"(",
"type",
")",
"{",
"case",
"*",
"iNode",
":",
"// If the branch is an I-node, then iinsert is called recursively.",
"in",
":=",
"branch",
".",
"(",
"*",
"iNode",
")",
"\n",
"if",
"startGen",
"==",
"in",
".",
"gen",
"{",
"return",
"c",
".",
"iinsert",
"(",
"in",
",",
"entry",
",",
"lev",
"+",
"w",
",",
"i",
",",
"startGen",
")",
"\n",
"}",
"\n",
"if",
"gcas",
"(",
"i",
",",
"main",
",",
"&",
"mainNode",
"{",
"cNode",
":",
"cn",
".",
"renewed",
"(",
"startGen",
",",
"c",
")",
"}",
",",
"c",
")",
"{",
"return",
"c",
".",
"iinsert",
"(",
"i",
",",
"entry",
",",
"lev",
",",
"parent",
",",
"startGen",
")",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"case",
"*",
"sNode",
":",
"sn",
":=",
"branch",
".",
"(",
"*",
"sNode",
")",
"\n",
"if",
"!",
"bytes",
".",
"Equal",
"(",
"sn",
".",
"Key",
",",
"entry",
".",
"Key",
")",
"{",
"// If the branch is an S-node and its key is not equal to the",
"// key being inserted, then the Ctrie has to be extended with",
"// an additional level. The C-node is replaced with its updated",
"// version, created using the updated function that adds a new",
"// I-node at the respective position. The new Inode has its",
"// main node pointing to a C-node with both keys. The",
"// linearization point is a successful CAS.",
"rn",
":=",
"cn",
"\n",
"if",
"cn",
".",
"gen",
"!=",
"i",
".",
"gen",
"{",
"rn",
"=",
"cn",
".",
"renewed",
"(",
"i",
".",
"gen",
",",
"c",
")",
"\n",
"}",
"\n",
"nsn",
":=",
"&",
"sNode",
"{",
"entry",
"}",
"\n",
"nin",
":=",
"&",
"iNode",
"{",
"main",
":",
"newMainNode",
"(",
"sn",
",",
"sn",
".",
"hash",
",",
"nsn",
",",
"nsn",
".",
"hash",
",",
"lev",
"+",
"w",
",",
"i",
".",
"gen",
")",
",",
"gen",
":",
"i",
".",
"gen",
"}",
"\n",
"ncn",
":=",
"&",
"mainNode",
"{",
"cNode",
":",
"rn",
".",
"updated",
"(",
"pos",
",",
"nin",
",",
"i",
".",
"gen",
")",
"}",
"\n",
"return",
"gcas",
"(",
"i",
",",
"main",
",",
"ncn",
",",
"c",
")",
"\n",
"}",
"\n",
"// If the key in the S-node is equal to the key being inserted,",
"// then the C-node is replaced with its updated version with a new",
"// S-node. The linearization point is a successful CAS.",
"ncn",
":=",
"&",
"mainNode",
"{",
"cNode",
":",
"cn",
".",
"updated",
"(",
"pos",
",",
"&",
"sNode",
"{",
"entry",
"}",
",",
"i",
".",
"gen",
")",
"}",
"\n",
"return",
"gcas",
"(",
"i",
",",
"main",
",",
"ncn",
",",
"c",
")",
"\n",
"default",
":",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"case",
"main",
".",
"tNode",
"!=",
"nil",
":",
"clean",
"(",
"parent",
",",
"lev",
"-",
"w",
",",
"c",
")",
"\n",
"return",
"false",
"\n",
"case",
"main",
".",
"lNode",
"!=",
"nil",
":",
"nln",
":=",
"&",
"mainNode",
"{",
"lNode",
":",
"main",
".",
"lNode",
".",
"inserted",
"(",
"entry",
")",
"}",
"\n",
"return",
"gcas",
"(",
"i",
",",
"main",
",",
"nln",
",",
"c",
")",
"\n",
"default",
":",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}"
] |
// iinsert attempts to insert the entry into the Ctrie. If false is returned,
// the operation should be retried.
|
[
"iinsert",
"attempts",
"to",
"insert",
"the",
"entry",
"into",
"the",
"Ctrie",
".",
"If",
"false",
"is",
"returned",
"the",
"operation",
"should",
"be",
"retried",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L464-L532
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
ilookup
|
func (c *Ctrie) ilookup(i *iNode, entry *Entry, lev uint, parent *iNode, startGen *generation) (interface{}, bool, bool) {
// Linearization point.
main := gcasRead(i, c)
switch {
case main.cNode != nil:
cn := main.cNode
flag, pos := flagPos(entry.hash, lev, cn.bmp)
if cn.bmp&flag == 0 {
// If the bitmap does not contain the relevant bit, a key with the
// required hashcode prefix is not present in the trie.
return nil, false, true
}
// Otherwise, the relevant branch at index pos is read from the array.
branch := cn.array[pos]
switch branch.(type) {
case *iNode:
// If the branch is an I-node, the ilookup procedure is called
// recursively at the next level.
in := branch.(*iNode)
if c.readOnly || startGen == in.gen {
return c.ilookup(in, entry, lev+w, i, startGen)
}
if gcas(i, main, &mainNode{cNode: cn.renewed(startGen, c)}, c) {
return c.ilookup(i, entry, lev, parent, startGen)
}
return nil, false, false
case *sNode:
// If the branch is an S-node, then the key within the S-node is
// compared with the key being searched – these two keys have the
// same hashcode prefixes, but they need not be equal. If they are
// equal, the corresponding value from the S-node is
// returned and a NOTFOUND value otherwise.
sn := branch.(*sNode)
if bytes.Equal(sn.Key, entry.Key) {
return sn.Value, true, true
}
return nil, false, true
default:
panic("Ctrie is in an invalid state")
}
case main.tNode != nil:
return cleanReadOnly(main.tNode, lev, parent, c, entry)
case main.lNode != nil:
// Hash collisions are handled using L-nodes, which are essentially
// persistent linked lists.
val, ok := main.lNode.lookup(entry)
return val, ok, true
default:
panic("Ctrie is in an invalid state")
}
}
|
go
|
func (c *Ctrie) ilookup(i *iNode, entry *Entry, lev uint, parent *iNode, startGen *generation) (interface{}, bool, bool) {
// Linearization point.
main := gcasRead(i, c)
switch {
case main.cNode != nil:
cn := main.cNode
flag, pos := flagPos(entry.hash, lev, cn.bmp)
if cn.bmp&flag == 0 {
// If the bitmap does not contain the relevant bit, a key with the
// required hashcode prefix is not present in the trie.
return nil, false, true
}
// Otherwise, the relevant branch at index pos is read from the array.
branch := cn.array[pos]
switch branch.(type) {
case *iNode:
// If the branch is an I-node, the ilookup procedure is called
// recursively at the next level.
in := branch.(*iNode)
if c.readOnly || startGen == in.gen {
return c.ilookup(in, entry, lev+w, i, startGen)
}
if gcas(i, main, &mainNode{cNode: cn.renewed(startGen, c)}, c) {
return c.ilookup(i, entry, lev, parent, startGen)
}
return nil, false, false
case *sNode:
// If the branch is an S-node, then the key within the S-node is
// compared with the key being searched – these two keys have the
// same hashcode prefixes, but they need not be equal. If they are
// equal, the corresponding value from the S-node is
// returned and a NOTFOUND value otherwise.
sn := branch.(*sNode)
if bytes.Equal(sn.Key, entry.Key) {
return sn.Value, true, true
}
return nil, false, true
default:
panic("Ctrie is in an invalid state")
}
case main.tNode != nil:
return cleanReadOnly(main.tNode, lev, parent, c, entry)
case main.lNode != nil:
// Hash collisions are handled using L-nodes, which are essentially
// persistent linked lists.
val, ok := main.lNode.lookup(entry)
return val, ok, true
default:
panic("Ctrie is in an invalid state")
}
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"ilookup",
"(",
"i",
"*",
"iNode",
",",
"entry",
"*",
"Entry",
",",
"lev",
"uint",
",",
"parent",
"*",
"iNode",
",",
"startGen",
"*",
"generation",
")",
"(",
"interface",
"{",
"}",
",",
"bool",
",",
"bool",
")",
"{",
"// Linearization point.",
"main",
":=",
"gcasRead",
"(",
"i",
",",
"c",
")",
"\n",
"switch",
"{",
"case",
"main",
".",
"cNode",
"!=",
"nil",
":",
"cn",
":=",
"main",
".",
"cNode",
"\n",
"flag",
",",
"pos",
":=",
"flagPos",
"(",
"entry",
".",
"hash",
",",
"lev",
",",
"cn",
".",
"bmp",
")",
"\n",
"if",
"cn",
".",
"bmp",
"&",
"flag",
"==",
"0",
"{",
"// If the bitmap does not contain the relevant bit, a key with the",
"// required hashcode prefix is not present in the trie.",
"return",
"nil",
",",
"false",
",",
"true",
"\n",
"}",
"\n",
"// Otherwise, the relevant branch at index pos is read from the array.",
"branch",
":=",
"cn",
".",
"array",
"[",
"pos",
"]",
"\n",
"switch",
"branch",
".",
"(",
"type",
")",
"{",
"case",
"*",
"iNode",
":",
"// If the branch is an I-node, the ilookup procedure is called",
"// recursively at the next level.",
"in",
":=",
"branch",
".",
"(",
"*",
"iNode",
")",
"\n",
"if",
"c",
".",
"readOnly",
"||",
"startGen",
"==",
"in",
".",
"gen",
"{",
"return",
"c",
".",
"ilookup",
"(",
"in",
",",
"entry",
",",
"lev",
"+",
"w",
",",
"i",
",",
"startGen",
")",
"\n",
"}",
"\n",
"if",
"gcas",
"(",
"i",
",",
"main",
",",
"&",
"mainNode",
"{",
"cNode",
":",
"cn",
".",
"renewed",
"(",
"startGen",
",",
"c",
")",
"}",
",",
"c",
")",
"{",
"return",
"c",
".",
"ilookup",
"(",
"i",
",",
"entry",
",",
"lev",
",",
"parent",
",",
"startGen",
")",
"\n",
"}",
"\n",
"return",
"nil",
",",
"false",
",",
"false",
"\n",
"case",
"*",
"sNode",
":",
"// If the branch is an S-node, then the key within the S-node is",
"// compared with the key being searched – these two keys have the",
"// same hashcode prefixes, but they need not be equal. If they are",
"// equal, the corresponding value from the S-node is",
"// returned and a NOTFOUND value otherwise.",
"sn",
":=",
"branch",
".",
"(",
"*",
"sNode",
")",
"\n",
"if",
"bytes",
".",
"Equal",
"(",
"sn",
".",
"Key",
",",
"entry",
".",
"Key",
")",
"{",
"return",
"sn",
".",
"Value",
",",
"true",
",",
"true",
"\n",
"}",
"\n",
"return",
"nil",
",",
"false",
",",
"true",
"\n",
"default",
":",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"case",
"main",
".",
"tNode",
"!=",
"nil",
":",
"return",
"cleanReadOnly",
"(",
"main",
".",
"tNode",
",",
"lev",
",",
"parent",
",",
"c",
",",
"entry",
")",
"\n",
"case",
"main",
".",
"lNode",
"!=",
"nil",
":",
"// Hash collisions are handled using L-nodes, which are essentially",
"// persistent linked lists.",
"val",
",",
"ok",
":=",
"main",
".",
"lNode",
".",
"lookup",
"(",
"entry",
")",
"\n",
"return",
"val",
",",
"ok",
",",
"true",
"\n",
"default",
":",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}"
] |
// ilookup attempts to fetch the entry from the Ctrie. The first two return
// values are the entry value and whether or not the entry was contained in the
// Ctrie. The last bool indicates if the operation succeeded. False means it
// should be retried.
|
[
"ilookup",
"attempts",
"to",
"fetch",
"the",
"entry",
"from",
"the",
"Ctrie",
".",
"The",
"first",
"two",
"return",
"values",
"are",
"the",
"entry",
"value",
"and",
"whether",
"or",
"not",
"the",
"entry",
"was",
"contained",
"in",
"the",
"Ctrie",
".",
"The",
"last",
"bool",
"indicates",
"if",
"the",
"operation",
"succeeded",
".",
"False",
"means",
"it",
"should",
"be",
"retried",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L538-L588
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
iremove
|
func (c *Ctrie) iremove(i *iNode, entry *Entry, lev uint, parent *iNode, startGen *generation) (interface{}, bool, bool) {
// Linearization point.
main := gcasRead(i, c)
switch {
case main.cNode != nil:
cn := main.cNode
flag, pos := flagPos(entry.hash, lev, cn.bmp)
if cn.bmp&flag == 0 {
// If the bitmap does not contain the relevant bit, a key with the
// required hashcode prefix is not present in the trie.
return nil, false, true
}
// Otherwise, the relevant branch at index pos is read from the array.
branch := cn.array[pos]
switch branch.(type) {
case *iNode:
// If the branch is an I-node, the iremove procedure is called
// recursively at the next level.
in := branch.(*iNode)
if startGen == in.gen {
return c.iremove(in, entry, lev+w, i, startGen)
}
if gcas(i, main, &mainNode{cNode: cn.renewed(startGen, c)}, c) {
return c.iremove(i, entry, lev, parent, startGen)
}
return nil, false, false
case *sNode:
// If the branch is an S-node, its key is compared against the key
// being removed.
sn := branch.(*sNode)
if !bytes.Equal(sn.Key, entry.Key) {
// If the keys are not equal, the NOTFOUND value is returned.
return nil, false, true
}
// If the keys are equal, a copy of the current node without the
// S-node is created. The contraction of the copy is then created
// using the toContracted procedure. A successful CAS will
// substitute the old C-node with the copied C-node, thus removing
// the S-node with the given key from the trie – this is the
// linearization point
ncn := cn.removed(pos, flag, i.gen)
cntr := toContracted(ncn, lev)
if gcas(i, main, cntr, c) {
if parent != nil {
main = gcasRead(i, c)
if main.tNode != nil {
cleanParent(parent, i, entry.hash, lev-w, c, startGen)
}
}
return sn.Value, true, true
}
return nil, false, false
default:
panic("Ctrie is in an invalid state")
}
case main.tNode != nil:
clean(parent, lev-w, c)
return nil, false, false
case main.lNode != nil:
nln := &mainNode{lNode: main.lNode.removed(entry)}
if nln.lNode.length() == 1 {
nln = entomb(nln.lNode.entry())
}
if gcas(i, main, nln, c) {
val, ok := main.lNode.lookup(entry)
return val, ok, true
}
return nil, false, true
default:
panic("Ctrie is in an invalid state")
}
}
|
go
|
func (c *Ctrie) iremove(i *iNode, entry *Entry, lev uint, parent *iNode, startGen *generation) (interface{}, bool, bool) {
// Linearization point.
main := gcasRead(i, c)
switch {
case main.cNode != nil:
cn := main.cNode
flag, pos := flagPos(entry.hash, lev, cn.bmp)
if cn.bmp&flag == 0 {
// If the bitmap does not contain the relevant bit, a key with the
// required hashcode prefix is not present in the trie.
return nil, false, true
}
// Otherwise, the relevant branch at index pos is read from the array.
branch := cn.array[pos]
switch branch.(type) {
case *iNode:
// If the branch is an I-node, the iremove procedure is called
// recursively at the next level.
in := branch.(*iNode)
if startGen == in.gen {
return c.iremove(in, entry, lev+w, i, startGen)
}
if gcas(i, main, &mainNode{cNode: cn.renewed(startGen, c)}, c) {
return c.iremove(i, entry, lev, parent, startGen)
}
return nil, false, false
case *sNode:
// If the branch is an S-node, its key is compared against the key
// being removed.
sn := branch.(*sNode)
if !bytes.Equal(sn.Key, entry.Key) {
// If the keys are not equal, the NOTFOUND value is returned.
return nil, false, true
}
// If the keys are equal, a copy of the current node without the
// S-node is created. The contraction of the copy is then created
// using the toContracted procedure. A successful CAS will
// substitute the old C-node with the copied C-node, thus removing
// the S-node with the given key from the trie – this is the
// linearization point
ncn := cn.removed(pos, flag, i.gen)
cntr := toContracted(ncn, lev)
if gcas(i, main, cntr, c) {
if parent != nil {
main = gcasRead(i, c)
if main.tNode != nil {
cleanParent(parent, i, entry.hash, lev-w, c, startGen)
}
}
return sn.Value, true, true
}
return nil, false, false
default:
panic("Ctrie is in an invalid state")
}
case main.tNode != nil:
clean(parent, lev-w, c)
return nil, false, false
case main.lNode != nil:
nln := &mainNode{lNode: main.lNode.removed(entry)}
if nln.lNode.length() == 1 {
nln = entomb(nln.lNode.entry())
}
if gcas(i, main, nln, c) {
val, ok := main.lNode.lookup(entry)
return val, ok, true
}
return nil, false, true
default:
panic("Ctrie is in an invalid state")
}
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"iremove",
"(",
"i",
"*",
"iNode",
",",
"entry",
"*",
"Entry",
",",
"lev",
"uint",
",",
"parent",
"*",
"iNode",
",",
"startGen",
"*",
"generation",
")",
"(",
"interface",
"{",
"}",
",",
"bool",
",",
"bool",
")",
"{",
"// Linearization point.",
"main",
":=",
"gcasRead",
"(",
"i",
",",
"c",
")",
"\n",
"switch",
"{",
"case",
"main",
".",
"cNode",
"!=",
"nil",
":",
"cn",
":=",
"main",
".",
"cNode",
"\n",
"flag",
",",
"pos",
":=",
"flagPos",
"(",
"entry",
".",
"hash",
",",
"lev",
",",
"cn",
".",
"bmp",
")",
"\n",
"if",
"cn",
".",
"bmp",
"&",
"flag",
"==",
"0",
"{",
"// If the bitmap does not contain the relevant bit, a key with the",
"// required hashcode prefix is not present in the trie.",
"return",
"nil",
",",
"false",
",",
"true",
"\n",
"}",
"\n",
"// Otherwise, the relevant branch at index pos is read from the array.",
"branch",
":=",
"cn",
".",
"array",
"[",
"pos",
"]",
"\n",
"switch",
"branch",
".",
"(",
"type",
")",
"{",
"case",
"*",
"iNode",
":",
"// If the branch is an I-node, the iremove procedure is called",
"// recursively at the next level.",
"in",
":=",
"branch",
".",
"(",
"*",
"iNode",
")",
"\n",
"if",
"startGen",
"==",
"in",
".",
"gen",
"{",
"return",
"c",
".",
"iremove",
"(",
"in",
",",
"entry",
",",
"lev",
"+",
"w",
",",
"i",
",",
"startGen",
")",
"\n",
"}",
"\n",
"if",
"gcas",
"(",
"i",
",",
"main",
",",
"&",
"mainNode",
"{",
"cNode",
":",
"cn",
".",
"renewed",
"(",
"startGen",
",",
"c",
")",
"}",
",",
"c",
")",
"{",
"return",
"c",
".",
"iremove",
"(",
"i",
",",
"entry",
",",
"lev",
",",
"parent",
",",
"startGen",
")",
"\n",
"}",
"\n",
"return",
"nil",
",",
"false",
",",
"false",
"\n",
"case",
"*",
"sNode",
":",
"// If the branch is an S-node, its key is compared against the key",
"// being removed.",
"sn",
":=",
"branch",
".",
"(",
"*",
"sNode",
")",
"\n",
"if",
"!",
"bytes",
".",
"Equal",
"(",
"sn",
".",
"Key",
",",
"entry",
".",
"Key",
")",
"{",
"// If the keys are not equal, the NOTFOUND value is returned.",
"return",
"nil",
",",
"false",
",",
"true",
"\n",
"}",
"\n",
"// If the keys are equal, a copy of the current node without the",
"// S-node is created. The contraction of the copy is then created",
"// using the toContracted procedure. A successful CAS will",
"// substitute the old C-node with the copied C-node, thus removing",
"// the S-node with the given key from the trie – this is the",
"// linearization point",
"ncn",
":=",
"cn",
".",
"removed",
"(",
"pos",
",",
"flag",
",",
"i",
".",
"gen",
")",
"\n",
"cntr",
":=",
"toContracted",
"(",
"ncn",
",",
"lev",
")",
"\n",
"if",
"gcas",
"(",
"i",
",",
"main",
",",
"cntr",
",",
"c",
")",
"{",
"if",
"parent",
"!=",
"nil",
"{",
"main",
"=",
"gcasRead",
"(",
"i",
",",
"c",
")",
"\n",
"if",
"main",
".",
"tNode",
"!=",
"nil",
"{",
"cleanParent",
"(",
"parent",
",",
"i",
",",
"entry",
".",
"hash",
",",
"lev",
"-",
"w",
",",
"c",
",",
"startGen",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"sn",
".",
"Value",
",",
"true",
",",
"true",
"\n",
"}",
"\n",
"return",
"nil",
",",
"false",
",",
"false",
"\n",
"default",
":",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"case",
"main",
".",
"tNode",
"!=",
"nil",
":",
"clean",
"(",
"parent",
",",
"lev",
"-",
"w",
",",
"c",
")",
"\n",
"return",
"nil",
",",
"false",
",",
"false",
"\n",
"case",
"main",
".",
"lNode",
"!=",
"nil",
":",
"nln",
":=",
"&",
"mainNode",
"{",
"lNode",
":",
"main",
".",
"lNode",
".",
"removed",
"(",
"entry",
")",
"}",
"\n",
"if",
"nln",
".",
"lNode",
".",
"length",
"(",
")",
"==",
"1",
"{",
"nln",
"=",
"entomb",
"(",
"nln",
".",
"lNode",
".",
"entry",
"(",
")",
")",
"\n",
"}",
"\n",
"if",
"gcas",
"(",
"i",
",",
"main",
",",
"nln",
",",
"c",
")",
"{",
"val",
",",
"ok",
":=",
"main",
".",
"lNode",
".",
"lookup",
"(",
"entry",
")",
"\n",
"return",
"val",
",",
"ok",
",",
"true",
"\n",
"}",
"\n",
"return",
"nil",
",",
"false",
",",
"true",
"\n",
"default",
":",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}"
] |
// iremove attempts to remove the entry from the Ctrie. The first two return
// values are the entry value and whether or not the entry was contained in the
// Ctrie. The last bool indicates if the operation succeeded. False means it
// should be retried.
|
[
"iremove",
"attempts",
"to",
"remove",
"the",
"entry",
"from",
"the",
"Ctrie",
".",
"The",
"first",
"two",
"return",
"values",
"are",
"the",
"entry",
"value",
"and",
"whether",
"or",
"not",
"the",
"entry",
"was",
"contained",
"in",
"the",
"Ctrie",
".",
"The",
"last",
"bool",
"indicates",
"if",
"the",
"operation",
"succeeded",
".",
"False",
"means",
"it",
"should",
"be",
"retried",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L594-L665
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
toContracted
|
func toContracted(cn *cNode, lev uint) *mainNode {
if lev > 0 && len(cn.array) == 1 {
branch := cn.array[0]
switch branch.(type) {
case *sNode:
return entomb(branch.(*sNode))
default:
return &mainNode{cNode: cn}
}
}
return &mainNode{cNode: cn}
}
|
go
|
func toContracted(cn *cNode, lev uint) *mainNode {
if lev > 0 && len(cn.array) == 1 {
branch := cn.array[0]
switch branch.(type) {
case *sNode:
return entomb(branch.(*sNode))
default:
return &mainNode{cNode: cn}
}
}
return &mainNode{cNode: cn}
}
|
[
"func",
"toContracted",
"(",
"cn",
"*",
"cNode",
",",
"lev",
"uint",
")",
"*",
"mainNode",
"{",
"if",
"lev",
">",
"0",
"&&",
"len",
"(",
"cn",
".",
"array",
")",
"==",
"1",
"{",
"branch",
":=",
"cn",
".",
"array",
"[",
"0",
"]",
"\n",
"switch",
"branch",
".",
"(",
"type",
")",
"{",
"case",
"*",
"sNode",
":",
"return",
"entomb",
"(",
"branch",
".",
"(",
"*",
"sNode",
")",
")",
"\n",
"default",
":",
"return",
"&",
"mainNode",
"{",
"cNode",
":",
"cn",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"&",
"mainNode",
"{",
"cNode",
":",
"cn",
"}",
"\n",
"}"
] |
// toContracted ensures that every I-node except the root points to a C-node
// with at least one branch. If a given C-Node has only a single S-node below
// it and is not at the root level, a T-node which wraps the S-node is
// returned.
|
[
"toContracted",
"ensures",
"that",
"every",
"I",
"-",
"node",
"except",
"the",
"root",
"points",
"to",
"a",
"C",
"-",
"node",
"with",
"at",
"least",
"one",
"branch",
".",
"If",
"a",
"given",
"C",
"-",
"Node",
"has",
"only",
"a",
"single",
"S",
"-",
"node",
"below",
"it",
"and",
"is",
"not",
"at",
"the",
"root",
"level",
"a",
"T",
"-",
"node",
"which",
"wraps",
"the",
"S",
"-",
"node",
"is",
"returned",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L671-L682
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
toCompressed
|
func toCompressed(cn *cNode, lev uint) *mainNode {
tmpArray := make([]branch, len(cn.array))
for i, sub := range cn.array {
switch sub.(type) {
case *iNode:
inode := sub.(*iNode)
mainPtr := (*unsafe.Pointer)(unsafe.Pointer(&inode.main))
main := (*mainNode)(atomic.LoadPointer(mainPtr))
tmpArray[i] = resurrect(inode, main)
case *sNode:
tmpArray[i] = sub
default:
panic("Ctrie is in an invalid state")
}
}
return toContracted(&cNode{bmp: cn.bmp, array: tmpArray}, lev)
}
|
go
|
func toCompressed(cn *cNode, lev uint) *mainNode {
tmpArray := make([]branch, len(cn.array))
for i, sub := range cn.array {
switch sub.(type) {
case *iNode:
inode := sub.(*iNode)
mainPtr := (*unsafe.Pointer)(unsafe.Pointer(&inode.main))
main := (*mainNode)(atomic.LoadPointer(mainPtr))
tmpArray[i] = resurrect(inode, main)
case *sNode:
tmpArray[i] = sub
default:
panic("Ctrie is in an invalid state")
}
}
return toContracted(&cNode{bmp: cn.bmp, array: tmpArray}, lev)
}
|
[
"func",
"toCompressed",
"(",
"cn",
"*",
"cNode",
",",
"lev",
"uint",
")",
"*",
"mainNode",
"{",
"tmpArray",
":=",
"make",
"(",
"[",
"]",
"branch",
",",
"len",
"(",
"cn",
".",
"array",
")",
")",
"\n",
"for",
"i",
",",
"sub",
":=",
"range",
"cn",
".",
"array",
"{",
"switch",
"sub",
".",
"(",
"type",
")",
"{",
"case",
"*",
"iNode",
":",
"inode",
":=",
"sub",
".",
"(",
"*",
"iNode",
")",
"\n",
"mainPtr",
":=",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"inode",
".",
"main",
")",
")",
"\n",
"main",
":=",
"(",
"*",
"mainNode",
")",
"(",
"atomic",
".",
"LoadPointer",
"(",
"mainPtr",
")",
")",
"\n",
"tmpArray",
"[",
"i",
"]",
"=",
"resurrect",
"(",
"inode",
",",
"main",
")",
"\n",
"case",
"*",
"sNode",
":",
"tmpArray",
"[",
"i",
"]",
"=",
"sub",
"\n",
"default",
":",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"toContracted",
"(",
"&",
"cNode",
"{",
"bmp",
":",
"cn",
".",
"bmp",
",",
"array",
":",
"tmpArray",
"}",
",",
"lev",
")",
"\n",
"}"
] |
// toCompressed compacts the C-node as a performance optimization.
|
[
"toCompressed",
"compacts",
"the",
"C",
"-",
"node",
"as",
"a",
"performance",
"optimization",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L685-L702
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
gcas
|
func gcas(in *iNode, old, n *mainNode, ct *Ctrie) bool {
prevPtr := (*unsafe.Pointer)(unsafe.Pointer(&n.prev))
atomic.StorePointer(prevPtr, unsafe.Pointer(old))
if atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&in.main)),
unsafe.Pointer(old), unsafe.Pointer(n)) {
gcasComplete(in, n, ct)
return atomic.LoadPointer(prevPtr) == nil
}
return false
}
|
go
|
func gcas(in *iNode, old, n *mainNode, ct *Ctrie) bool {
prevPtr := (*unsafe.Pointer)(unsafe.Pointer(&n.prev))
atomic.StorePointer(prevPtr, unsafe.Pointer(old))
if atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&in.main)),
unsafe.Pointer(old), unsafe.Pointer(n)) {
gcasComplete(in, n, ct)
return atomic.LoadPointer(prevPtr) == nil
}
return false
}
|
[
"func",
"gcas",
"(",
"in",
"*",
"iNode",
",",
"old",
",",
"n",
"*",
"mainNode",
",",
"ct",
"*",
"Ctrie",
")",
"bool",
"{",
"prevPtr",
":=",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"n",
".",
"prev",
")",
")",
"\n",
"atomic",
".",
"StorePointer",
"(",
"prevPtr",
",",
"unsafe",
".",
"Pointer",
"(",
"old",
")",
")",
"\n",
"if",
"atomic",
".",
"CompareAndSwapPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"in",
".",
"main",
")",
")",
",",
"unsafe",
".",
"Pointer",
"(",
"old",
")",
",",
"unsafe",
".",
"Pointer",
"(",
"n",
")",
")",
"{",
"gcasComplete",
"(",
"in",
",",
"n",
",",
"ct",
")",
"\n",
"return",
"atomic",
".",
"LoadPointer",
"(",
"prevPtr",
")",
"==",
"nil",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// gcas is a generation-compare-and-swap which has semantics similar to RDCSS,
// but it does not create the intermediate object except in the case of
// failures that occur due to the snapshot being taken. This ensures that the
// write occurs only if the Ctrie root generation has remained the same in
// addition to the I-node having the expected value.
|
[
"gcas",
"is",
"a",
"generation",
"-",
"compare",
"-",
"and",
"-",
"swap",
"which",
"has",
"semantics",
"similar",
"to",
"RDCSS",
"but",
"it",
"does",
"not",
"create",
"the",
"intermediate",
"object",
"except",
"in",
"the",
"case",
"of",
"failures",
"that",
"occur",
"due",
"to",
"the",
"snapshot",
"being",
"taken",
".",
"This",
"ensures",
"that",
"the",
"write",
"occurs",
"only",
"if",
"the",
"Ctrie",
"root",
"generation",
"has",
"remained",
"the",
"same",
"in",
"addition",
"to",
"the",
"I",
"-",
"node",
"having",
"the",
"expected",
"value",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L776-L786
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
gcasRead
|
func gcasRead(in *iNode, ctrie *Ctrie) *mainNode {
m := (*mainNode)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&in.main))))
prev := (*mainNode)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&m.prev))))
if prev == nil {
return m
}
return gcasComplete(in, m, ctrie)
}
|
go
|
func gcasRead(in *iNode, ctrie *Ctrie) *mainNode {
m := (*mainNode)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&in.main))))
prev := (*mainNode)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&m.prev))))
if prev == nil {
return m
}
return gcasComplete(in, m, ctrie)
}
|
[
"func",
"gcasRead",
"(",
"in",
"*",
"iNode",
",",
"ctrie",
"*",
"Ctrie",
")",
"*",
"mainNode",
"{",
"m",
":=",
"(",
"*",
"mainNode",
")",
"(",
"atomic",
".",
"LoadPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"in",
".",
"main",
")",
")",
")",
")",
"\n",
"prev",
":=",
"(",
"*",
"mainNode",
")",
"(",
"atomic",
".",
"LoadPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"m",
".",
"prev",
")",
")",
")",
")",
"\n",
"if",
"prev",
"==",
"nil",
"{",
"return",
"m",
"\n",
"}",
"\n",
"return",
"gcasComplete",
"(",
"in",
",",
"m",
",",
"ctrie",
")",
"\n",
"}"
] |
// gcasRead performs a GCAS-linearizable read of the I-node's main node.
|
[
"gcasRead",
"performs",
"a",
"GCAS",
"-",
"linearizable",
"read",
"of",
"the",
"I",
"-",
"node",
"s",
"main",
"node",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L789-L796
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
gcasComplete
|
func gcasComplete(i *iNode, m *mainNode, ctrie *Ctrie) *mainNode {
for {
if m == nil {
return nil
}
prev := (*mainNode)(atomic.LoadPointer(
(*unsafe.Pointer)(unsafe.Pointer(&m.prev))))
root := ctrie.rdcssReadRoot(true)
if prev == nil {
return m
}
if prev.failed != nil {
// Signals GCAS failure. Swap old value back into I-node.
fn := prev.failed
if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&i.main)),
unsafe.Pointer(m), unsafe.Pointer(fn)) {
return fn
}
m = (*mainNode)(atomic.LoadPointer(
(*unsafe.Pointer)(unsafe.Pointer(&i.main))))
continue
}
if root.gen == i.gen && !ctrie.readOnly {
// Commit GCAS.
if atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&m.prev)), unsafe.Pointer(prev), nil) {
return m
}
continue
}
// Generations did not match. Store failed node on prev to signal
// I-node's main node must be set back to the previous value.
atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&m.prev)),
unsafe.Pointer(prev),
unsafe.Pointer(&mainNode{failed: prev}))
m = (*mainNode)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&i.main))))
return gcasComplete(i, m, ctrie)
}
}
|
go
|
func gcasComplete(i *iNode, m *mainNode, ctrie *Ctrie) *mainNode {
for {
if m == nil {
return nil
}
prev := (*mainNode)(atomic.LoadPointer(
(*unsafe.Pointer)(unsafe.Pointer(&m.prev))))
root := ctrie.rdcssReadRoot(true)
if prev == nil {
return m
}
if prev.failed != nil {
// Signals GCAS failure. Swap old value back into I-node.
fn := prev.failed
if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&i.main)),
unsafe.Pointer(m), unsafe.Pointer(fn)) {
return fn
}
m = (*mainNode)(atomic.LoadPointer(
(*unsafe.Pointer)(unsafe.Pointer(&i.main))))
continue
}
if root.gen == i.gen && !ctrie.readOnly {
// Commit GCAS.
if atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&m.prev)), unsafe.Pointer(prev), nil) {
return m
}
continue
}
// Generations did not match. Store failed node on prev to signal
// I-node's main node must be set back to the previous value.
atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&m.prev)),
unsafe.Pointer(prev),
unsafe.Pointer(&mainNode{failed: prev}))
m = (*mainNode)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&i.main))))
return gcasComplete(i, m, ctrie)
}
}
|
[
"func",
"gcasComplete",
"(",
"i",
"*",
"iNode",
",",
"m",
"*",
"mainNode",
",",
"ctrie",
"*",
"Ctrie",
")",
"*",
"mainNode",
"{",
"for",
"{",
"if",
"m",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"prev",
":=",
"(",
"*",
"mainNode",
")",
"(",
"atomic",
".",
"LoadPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"m",
".",
"prev",
")",
")",
")",
")",
"\n",
"root",
":=",
"ctrie",
".",
"rdcssReadRoot",
"(",
"true",
")",
"\n",
"if",
"prev",
"==",
"nil",
"{",
"return",
"m",
"\n",
"}",
"\n\n",
"if",
"prev",
".",
"failed",
"!=",
"nil",
"{",
"// Signals GCAS failure. Swap old value back into I-node.",
"fn",
":=",
"prev",
".",
"failed",
"\n",
"if",
"atomic",
".",
"CompareAndSwapPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"i",
".",
"main",
")",
")",
",",
"unsafe",
".",
"Pointer",
"(",
"m",
")",
",",
"unsafe",
".",
"Pointer",
"(",
"fn",
")",
")",
"{",
"return",
"fn",
"\n",
"}",
"\n",
"m",
"=",
"(",
"*",
"mainNode",
")",
"(",
"atomic",
".",
"LoadPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"i",
".",
"main",
")",
")",
")",
")",
"\n",
"continue",
"\n",
"}",
"\n\n",
"if",
"root",
".",
"gen",
"==",
"i",
".",
"gen",
"&&",
"!",
"ctrie",
".",
"readOnly",
"{",
"// Commit GCAS.",
"if",
"atomic",
".",
"CompareAndSwapPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"m",
".",
"prev",
")",
")",
",",
"unsafe",
".",
"Pointer",
"(",
"prev",
")",
",",
"nil",
")",
"{",
"return",
"m",
"\n",
"}",
"\n",
"continue",
"\n",
"}",
"\n\n",
"// Generations did not match. Store failed node on prev to signal",
"// I-node's main node must be set back to the previous value.",
"atomic",
".",
"CompareAndSwapPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"m",
".",
"prev",
")",
")",
",",
"unsafe",
".",
"Pointer",
"(",
"prev",
")",
",",
"unsafe",
".",
"Pointer",
"(",
"&",
"mainNode",
"{",
"failed",
":",
"prev",
"}",
")",
")",
"\n",
"m",
"=",
"(",
"*",
"mainNode",
")",
"(",
"atomic",
".",
"LoadPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"i",
".",
"main",
")",
")",
")",
")",
"\n",
"return",
"gcasComplete",
"(",
"i",
",",
"m",
",",
"ctrie",
")",
"\n",
"}",
"\n",
"}"
] |
// gcasComplete commits the GCAS operation.
|
[
"gcasComplete",
"commits",
"the",
"GCAS",
"operation",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L799-L841
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
rdcssReadRoot
|
func (c *Ctrie) rdcssReadRoot(abort bool) *iNode {
r := (*iNode)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&c.root))))
if r.rdcss != nil {
return c.rdcssComplete(abort)
}
return r
}
|
go
|
func (c *Ctrie) rdcssReadRoot(abort bool) *iNode {
r := (*iNode)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&c.root))))
if r.rdcss != nil {
return c.rdcssComplete(abort)
}
return r
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"rdcssReadRoot",
"(",
"abort",
"bool",
")",
"*",
"iNode",
"{",
"r",
":=",
"(",
"*",
"iNode",
")",
"(",
"atomic",
".",
"LoadPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"c",
".",
"root",
")",
")",
")",
")",
"\n",
"if",
"r",
".",
"rdcss",
"!=",
"nil",
"{",
"return",
"c",
".",
"rdcssComplete",
"(",
"abort",
")",
"\n",
"}",
"\n",
"return",
"r",
"\n",
"}"
] |
// rdcssReadRoot performs a RDCSS-linearizable read of the Ctrie root with the
// given priority.
|
[
"rdcssReadRoot",
"performs",
"a",
"RDCSS",
"-",
"linearizable",
"read",
"of",
"the",
"Ctrie",
"root",
"with",
"the",
"given",
"priority",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L862-L868
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
rdcssRoot
|
func (c *Ctrie) rdcssRoot(old *iNode, expected *mainNode, nv *iNode) bool {
desc := &iNode{
rdcss: &rdcssDescriptor{
old: old,
expected: expected,
nv: nv,
},
}
if c.casRoot(old, desc) {
c.rdcssComplete(false)
return atomic.LoadInt32(&desc.rdcss.committed) == 1
}
return false
}
|
go
|
func (c *Ctrie) rdcssRoot(old *iNode, expected *mainNode, nv *iNode) bool {
desc := &iNode{
rdcss: &rdcssDescriptor{
old: old,
expected: expected,
nv: nv,
},
}
if c.casRoot(old, desc) {
c.rdcssComplete(false)
return atomic.LoadInt32(&desc.rdcss.committed) == 1
}
return false
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"rdcssRoot",
"(",
"old",
"*",
"iNode",
",",
"expected",
"*",
"mainNode",
",",
"nv",
"*",
"iNode",
")",
"bool",
"{",
"desc",
":=",
"&",
"iNode",
"{",
"rdcss",
":",
"&",
"rdcssDescriptor",
"{",
"old",
":",
"old",
",",
"expected",
":",
"expected",
",",
"nv",
":",
"nv",
",",
"}",
",",
"}",
"\n",
"if",
"c",
".",
"casRoot",
"(",
"old",
",",
"desc",
")",
"{",
"c",
".",
"rdcssComplete",
"(",
"false",
")",
"\n",
"return",
"atomic",
".",
"LoadInt32",
"(",
"&",
"desc",
".",
"rdcss",
".",
"committed",
")",
"==",
"1",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// rdcssRoot performs a RDCSS on the Ctrie root. This is used to create a
// snapshot of the Ctrie by copying the root I-node and setting it to a new
// generation.
|
[
"rdcssRoot",
"performs",
"a",
"RDCSS",
"on",
"the",
"Ctrie",
"root",
".",
"This",
"is",
"used",
"to",
"create",
"a",
"snapshot",
"of",
"the",
"Ctrie",
"by",
"copying",
"the",
"root",
"I",
"-",
"node",
"and",
"setting",
"it",
"to",
"a",
"new",
"generation",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L873-L886
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
rdcssComplete
|
func (c *Ctrie) rdcssComplete(abort bool) *iNode {
for {
r := (*iNode)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&c.root))))
if r.rdcss == nil {
return r
}
var (
desc = r.rdcss
ov = desc.old
exp = desc.expected
nv = desc.nv
)
if abort {
if c.casRoot(r, ov) {
return ov
}
continue
}
oldeMain := gcasRead(ov, c)
if oldeMain == exp {
// Commit the RDCSS.
if c.casRoot(r, nv) {
atomic.StoreInt32(&desc.committed, 1)
return nv
}
continue
}
if c.casRoot(r, ov) {
return ov
}
continue
}
}
|
go
|
func (c *Ctrie) rdcssComplete(abort bool) *iNode {
for {
r := (*iNode)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&c.root))))
if r.rdcss == nil {
return r
}
var (
desc = r.rdcss
ov = desc.old
exp = desc.expected
nv = desc.nv
)
if abort {
if c.casRoot(r, ov) {
return ov
}
continue
}
oldeMain := gcasRead(ov, c)
if oldeMain == exp {
// Commit the RDCSS.
if c.casRoot(r, nv) {
atomic.StoreInt32(&desc.committed, 1)
return nv
}
continue
}
if c.casRoot(r, ov) {
return ov
}
continue
}
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"rdcssComplete",
"(",
"abort",
"bool",
")",
"*",
"iNode",
"{",
"for",
"{",
"r",
":=",
"(",
"*",
"iNode",
")",
"(",
"atomic",
".",
"LoadPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"c",
".",
"root",
")",
")",
")",
")",
"\n",
"if",
"r",
".",
"rdcss",
"==",
"nil",
"{",
"return",
"r",
"\n",
"}",
"\n\n",
"var",
"(",
"desc",
"=",
"r",
".",
"rdcss",
"\n",
"ov",
"=",
"desc",
".",
"old",
"\n",
"exp",
"=",
"desc",
".",
"expected",
"\n",
"nv",
"=",
"desc",
".",
"nv",
"\n",
")",
"\n\n",
"if",
"abort",
"{",
"if",
"c",
".",
"casRoot",
"(",
"r",
",",
"ov",
")",
"{",
"return",
"ov",
"\n",
"}",
"\n",
"continue",
"\n",
"}",
"\n\n",
"oldeMain",
":=",
"gcasRead",
"(",
"ov",
",",
"c",
")",
"\n",
"if",
"oldeMain",
"==",
"exp",
"{",
"// Commit the RDCSS.",
"if",
"c",
".",
"casRoot",
"(",
"r",
",",
"nv",
")",
"{",
"atomic",
".",
"StoreInt32",
"(",
"&",
"desc",
".",
"committed",
",",
"1",
")",
"\n",
"return",
"nv",
"\n",
"}",
"\n",
"continue",
"\n",
"}",
"\n",
"if",
"c",
".",
"casRoot",
"(",
"r",
",",
"ov",
")",
"{",
"return",
"ov",
"\n",
"}",
"\n",
"continue",
"\n",
"}",
"\n",
"}"
] |
// rdcssComplete commits the RDCSS operation.
|
[
"rdcssComplete",
"commits",
"the",
"RDCSS",
"operation",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L889-L924
|
train
|
Workiva/go-datastructures
|
trie/ctrie/ctrie.go
|
casRoot
|
func (c *Ctrie) casRoot(ov, nv *iNode) bool {
c.assertReadWrite()
return atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&c.root)), unsafe.Pointer(ov), unsafe.Pointer(nv))
}
|
go
|
func (c *Ctrie) casRoot(ov, nv *iNode) bool {
c.assertReadWrite()
return atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&c.root)), unsafe.Pointer(ov), unsafe.Pointer(nv))
}
|
[
"func",
"(",
"c",
"*",
"Ctrie",
")",
"casRoot",
"(",
"ov",
",",
"nv",
"*",
"iNode",
")",
"bool",
"{",
"c",
".",
"assertReadWrite",
"(",
")",
"\n",
"return",
"atomic",
".",
"CompareAndSwapPointer",
"(",
"(",
"*",
"unsafe",
".",
"Pointer",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"c",
".",
"root",
")",
")",
",",
"unsafe",
".",
"Pointer",
"(",
"ov",
")",
",",
"unsafe",
".",
"Pointer",
"(",
"nv",
")",
")",
"\n",
"}"
] |
// casRoot performs a CAS on the Ctrie root.
|
[
"casRoot",
"performs",
"a",
"CAS",
"on",
"the",
"Ctrie",
"root",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/ctrie/ctrie.go#L927-L931
|
train
|
Workiva/go-datastructures
|
btree/immutable/node.go
|
copy
|
func (n *Node) copy() *Node {
cpValues := make([]interface{}, len(n.ChildValues))
copy(cpValues, n.ChildValues)
cpKeys := make(Keys, len(n.ChildKeys))
copy(cpKeys, n.ChildKeys)
return &Node{
ID: newID(),
IsLeaf: n.IsLeaf,
ChildValues: cpValues,
ChildKeys: cpKeys,
}
}
|
go
|
func (n *Node) copy() *Node {
cpValues := make([]interface{}, len(n.ChildValues))
copy(cpValues, n.ChildValues)
cpKeys := make(Keys, len(n.ChildKeys))
copy(cpKeys, n.ChildKeys)
return &Node{
ID: newID(),
IsLeaf: n.IsLeaf,
ChildValues: cpValues,
ChildKeys: cpKeys,
}
}
|
[
"func",
"(",
"n",
"*",
"Node",
")",
"copy",
"(",
")",
"*",
"Node",
"{",
"cpValues",
":=",
"make",
"(",
"[",
"]",
"interface",
"{",
"}",
",",
"len",
"(",
"n",
".",
"ChildValues",
")",
")",
"\n",
"copy",
"(",
"cpValues",
",",
"n",
".",
"ChildValues",
")",
"\n",
"cpKeys",
":=",
"make",
"(",
"Keys",
",",
"len",
"(",
"n",
".",
"ChildKeys",
")",
")",
"\n",
"copy",
"(",
"cpKeys",
",",
"n",
".",
"ChildKeys",
")",
"\n\n",
"return",
"&",
"Node",
"{",
"ID",
":",
"newID",
"(",
")",
",",
"IsLeaf",
":",
"n",
".",
"IsLeaf",
",",
"ChildValues",
":",
"cpValues",
",",
"ChildKeys",
":",
"cpKeys",
",",
"}",
"\n",
"}"
] |
// copy makes a deep copy of this node. Required before any mutation.
|
[
"copy",
"makes",
"a",
"deep",
"copy",
"of",
"this",
"node",
".",
"Required",
"before",
"any",
"mutation",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/node.go#L127-L139
|
train
|
Workiva/go-datastructures
|
btree/immutable/node.go
|
searchKey
|
func (n *Node) searchKey(comparator Comparator, value interface{}) (*Key, int) {
i := n.search(comparator, value)
if n.IsLeaf && i == len(n.ChildValues) { // not found
return nil, i
}
if n.IsLeaf { // equal number of ids and values
return n.ChildKeys[i], i
}
if i == len(n.ChildValues) { // we need to go to the farthest node to the write
return n.ChildKeys[len(n.ChildKeys)-1], i
}
return n.ChildKeys[i], i
}
|
go
|
func (n *Node) searchKey(comparator Comparator, value interface{}) (*Key, int) {
i := n.search(comparator, value)
if n.IsLeaf && i == len(n.ChildValues) { // not found
return nil, i
}
if n.IsLeaf { // equal number of ids and values
return n.ChildKeys[i], i
}
if i == len(n.ChildValues) { // we need to go to the farthest node to the write
return n.ChildKeys[len(n.ChildKeys)-1], i
}
return n.ChildKeys[i], i
}
|
[
"func",
"(",
"n",
"*",
"Node",
")",
"searchKey",
"(",
"comparator",
"Comparator",
",",
"value",
"interface",
"{",
"}",
")",
"(",
"*",
"Key",
",",
"int",
")",
"{",
"i",
":=",
"n",
".",
"search",
"(",
"comparator",
",",
"value",
")",
"\n\n",
"if",
"n",
".",
"IsLeaf",
"&&",
"i",
"==",
"len",
"(",
"n",
".",
"ChildValues",
")",
"{",
"// not found",
"return",
"nil",
",",
"i",
"\n",
"}",
"\n\n",
"if",
"n",
".",
"IsLeaf",
"{",
"// equal number of ids and values",
"return",
"n",
".",
"ChildKeys",
"[",
"i",
"]",
",",
"i",
"\n",
"}",
"\n\n",
"if",
"i",
"==",
"len",
"(",
"n",
".",
"ChildValues",
")",
"{",
"// we need to go to the farthest node to the write",
"return",
"n",
".",
"ChildKeys",
"[",
"len",
"(",
"n",
".",
"ChildKeys",
")",
"-",
"1",
"]",
",",
"i",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"ChildKeys",
"[",
"i",
"]",
",",
"i",
"\n",
"}"
] |
// searchKey returns the key associated with the provided value. If the
// provided value is greater than the highest value in this node and this
// node is an internal node, this method returns the last ID and an index
// equal to lenValues.
|
[
"searchKey",
"returns",
"the",
"key",
"associated",
"with",
"the",
"provided",
"value",
".",
"If",
"the",
"provided",
"value",
"is",
"greater",
"than",
"the",
"highest",
"value",
"in",
"this",
"node",
"and",
"this",
"node",
"is",
"an",
"internal",
"node",
"this",
"method",
"returns",
"the",
"last",
"ID",
"and",
"an",
"index",
"equal",
"to",
"lenValues",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/node.go#L145-L161
|
train
|
Workiva/go-datastructures
|
btree/immutable/node.go
|
insert
|
func (n *Node) insert(comparator Comparator, key *Key) *Key {
var overwrittenKey *Key
i := n.search(comparator, key.Value)
if i == len(n.ChildValues) {
n.ChildValues = append(n.ChildValues, key.Value)
} else {
if n.ChildValues[i] == key.Value {
overwrittenKey = n.ChildKeys[i]
n.ChildKeys[i] = key
return overwrittenKey
} else {
n.ChildValues = append(n.ChildValues, 0)
copy(n.ChildValues[i+1:], n.ChildValues[i:])
n.ChildValues[i] = key.Value
}
}
if n.IsLeaf && i == len(n.ChildKeys) {
n.ChildKeys = append(n.ChildKeys, key)
} else {
n.ChildKeys = append(n.ChildKeys, nil)
copy(n.ChildKeys[i+1:], n.ChildKeys[i:])
n.ChildKeys[i] = key
}
return overwrittenKey
}
|
go
|
func (n *Node) insert(comparator Comparator, key *Key) *Key {
var overwrittenKey *Key
i := n.search(comparator, key.Value)
if i == len(n.ChildValues) {
n.ChildValues = append(n.ChildValues, key.Value)
} else {
if n.ChildValues[i] == key.Value {
overwrittenKey = n.ChildKeys[i]
n.ChildKeys[i] = key
return overwrittenKey
} else {
n.ChildValues = append(n.ChildValues, 0)
copy(n.ChildValues[i+1:], n.ChildValues[i:])
n.ChildValues[i] = key.Value
}
}
if n.IsLeaf && i == len(n.ChildKeys) {
n.ChildKeys = append(n.ChildKeys, key)
} else {
n.ChildKeys = append(n.ChildKeys, nil)
copy(n.ChildKeys[i+1:], n.ChildKeys[i:])
n.ChildKeys[i] = key
}
return overwrittenKey
}
|
[
"func",
"(",
"n",
"*",
"Node",
")",
"insert",
"(",
"comparator",
"Comparator",
",",
"key",
"*",
"Key",
")",
"*",
"Key",
"{",
"var",
"overwrittenKey",
"*",
"Key",
"\n",
"i",
":=",
"n",
".",
"search",
"(",
"comparator",
",",
"key",
".",
"Value",
")",
"\n",
"if",
"i",
"==",
"len",
"(",
"n",
".",
"ChildValues",
")",
"{",
"n",
".",
"ChildValues",
"=",
"append",
"(",
"n",
".",
"ChildValues",
",",
"key",
".",
"Value",
")",
"\n",
"}",
"else",
"{",
"if",
"n",
".",
"ChildValues",
"[",
"i",
"]",
"==",
"key",
".",
"Value",
"{",
"overwrittenKey",
"=",
"n",
".",
"ChildKeys",
"[",
"i",
"]",
"\n",
"n",
".",
"ChildKeys",
"[",
"i",
"]",
"=",
"key",
"\n",
"return",
"overwrittenKey",
"\n",
"}",
"else",
"{",
"n",
".",
"ChildValues",
"=",
"append",
"(",
"n",
".",
"ChildValues",
",",
"0",
")",
"\n",
"copy",
"(",
"n",
".",
"ChildValues",
"[",
"i",
"+",
"1",
":",
"]",
",",
"n",
".",
"ChildValues",
"[",
"i",
":",
"]",
")",
"\n",
"n",
".",
"ChildValues",
"[",
"i",
"]",
"=",
"key",
".",
"Value",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"n",
".",
"IsLeaf",
"&&",
"i",
"==",
"len",
"(",
"n",
".",
"ChildKeys",
")",
"{",
"n",
".",
"ChildKeys",
"=",
"append",
"(",
"n",
".",
"ChildKeys",
",",
"key",
")",
"\n",
"}",
"else",
"{",
"n",
".",
"ChildKeys",
"=",
"append",
"(",
"n",
".",
"ChildKeys",
",",
"nil",
")",
"\n",
"copy",
"(",
"n",
".",
"ChildKeys",
"[",
"i",
"+",
"1",
":",
"]",
",",
"n",
".",
"ChildKeys",
"[",
"i",
":",
"]",
")",
"\n",
"n",
".",
"ChildKeys",
"[",
"i",
"]",
"=",
"key",
"\n",
"}",
"\n\n",
"return",
"overwrittenKey",
"\n",
"}"
] |
// insert adds the provided key to this node and returns any ID that has
// been overwritten. This method should only be called on leaf nodes.
|
[
"insert",
"adds",
"the",
"provided",
"key",
"to",
"this",
"node",
"and",
"returns",
"any",
"ID",
"that",
"has",
"been",
"overwritten",
".",
"This",
"method",
"should",
"only",
"be",
"called",
"on",
"leaf",
"nodes",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/node.go#L165-L191
|
train
|
Workiva/go-datastructures
|
btree/immutable/node.go
|
delete
|
func (n *Node) delete(comparator Comparator, key *Key) *Key {
i := n.search(comparator, key.Value)
if i == len(n.ChildValues) {
return nil
}
n.deleteValueAt(i)
n.deleteKeyAt(i)
return key
}
|
go
|
func (n *Node) delete(comparator Comparator, key *Key) *Key {
i := n.search(comparator, key.Value)
if i == len(n.ChildValues) {
return nil
}
n.deleteValueAt(i)
n.deleteKeyAt(i)
return key
}
|
[
"func",
"(",
"n",
"*",
"Node",
")",
"delete",
"(",
"comparator",
"Comparator",
",",
"key",
"*",
"Key",
")",
"*",
"Key",
"{",
"i",
":=",
"n",
".",
"search",
"(",
"comparator",
",",
"key",
".",
"Value",
")",
"\n",
"if",
"i",
"==",
"len",
"(",
"n",
".",
"ChildValues",
")",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"n",
".",
"deleteValueAt",
"(",
"i",
")",
"\n",
"n",
".",
"deleteKeyAt",
"(",
"i",
")",
"\n\n",
"return",
"key",
"\n",
"}"
] |
// delete removes the provided key from the node and returns any key that
// was deleted. Returns nil of the key could not be found.
|
[
"delete",
"removes",
"the",
"provided",
"key",
"from",
"the",
"node",
"and",
"returns",
"any",
"key",
"that",
"was",
"deleted",
".",
"Returns",
"nil",
"of",
"the",
"key",
"could",
"not",
"be",
"found",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/node.go#L195-L205
|
train
|
Workiva/go-datastructures
|
btree/immutable/node.go
|
replaceKeyAt
|
func (n *Node) replaceKeyAt(key *Key, i int) {
n.ChildKeys[i] = key
}
|
go
|
func (n *Node) replaceKeyAt(key *Key, i int) {
n.ChildKeys[i] = key
}
|
[
"func",
"(",
"n",
"*",
"Node",
")",
"replaceKeyAt",
"(",
"key",
"*",
"Key",
",",
"i",
"int",
")",
"{",
"n",
".",
"ChildKeys",
"[",
"i",
"]",
"=",
"key",
"\n",
"}"
] |
// replaceKeyAt replaces the key at index i with the provided id. This does
// not do any bounds checking.
|
[
"replaceKeyAt",
"replaces",
"the",
"key",
"at",
"index",
"i",
"with",
"the",
"provided",
"id",
".",
"This",
"does",
"not",
"do",
"any",
"bounds",
"checking",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/node.go#L245-L247
|
train
|
Workiva/go-datastructures
|
btree/immutable/node.go
|
iter
|
func (n *Node) iter(comparator Comparator, start, stop interface{}) iterator {
pointer := n.search(comparator, start)
pointer--
return &sliceIterator{
stop: stop,
n: n,
pointer: pointer,
comparator: comparator,
}
}
|
go
|
func (n *Node) iter(comparator Comparator, start, stop interface{}) iterator {
pointer := n.search(comparator, start)
pointer--
return &sliceIterator{
stop: stop,
n: n,
pointer: pointer,
comparator: comparator,
}
}
|
[
"func",
"(",
"n",
"*",
"Node",
")",
"iter",
"(",
"comparator",
"Comparator",
",",
"start",
",",
"stop",
"interface",
"{",
"}",
")",
"iterator",
"{",
"pointer",
":=",
"n",
".",
"search",
"(",
"comparator",
",",
"start",
")",
"\n",
"pointer",
"--",
"\n",
"return",
"&",
"sliceIterator",
"{",
"stop",
":",
"stop",
",",
"n",
":",
"n",
",",
"pointer",
":",
"pointer",
",",
"comparator",
":",
"comparator",
",",
"}",
"\n",
"}"
] |
// iter returns an iterator that will iterate through the provided Morton
// numbers as they exist in this node.
|
[
"iter",
"returns",
"an",
"iterator",
"that",
"will",
"iterate",
"through",
"the",
"provided",
"Morton",
"numbers",
"as",
"they",
"exist",
"in",
"this",
"node",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/node.go#L256-L265
|
train
|
Workiva/go-datastructures
|
btree/immutable/node.go
|
splitAt
|
func (n *Node) splitAt(i int) (interface{}, *Node) {
if n.IsLeaf {
return n.splitLeafAt(i)
}
return n.splitInternalAt(i)
}
|
go
|
func (n *Node) splitAt(i int) (interface{}, *Node) {
if n.IsLeaf {
return n.splitLeafAt(i)
}
return n.splitInternalAt(i)
}
|
[
"func",
"(",
"n",
"*",
"Node",
")",
"splitAt",
"(",
"i",
"int",
")",
"(",
"interface",
"{",
"}",
",",
"*",
"Node",
")",
"{",
"if",
"n",
".",
"IsLeaf",
"{",
"return",
"n",
".",
"splitLeafAt",
"(",
"i",
")",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"splitInternalAt",
"(",
"i",
")",
"\n",
"}"
] |
// splitAt breaks this node into two parts and conceptually
// returns the left part
|
[
"splitAt",
"breaks",
"this",
"node",
"into",
"two",
"parts",
"and",
"conceptually",
"returns",
"the",
"left",
"part"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/node.go#L352-L358
|
train
|
Workiva/go-datastructures
|
btree/immutable/node.go
|
nodeFromBytes
|
func nodeFromBytes(t *Tr, data []byte) (*Node, error) {
n := &Node{}
_, err := n.UnmarshalMsg(data)
if err != nil {
panic(err)
return nil, err
}
return n, nil
}
|
go
|
func nodeFromBytes(t *Tr, data []byte) (*Node, error) {
n := &Node{}
_, err := n.UnmarshalMsg(data)
if err != nil {
panic(err)
return nil, err
}
return n, nil
}
|
[
"func",
"nodeFromBytes",
"(",
"t",
"*",
"Tr",
",",
"data",
"[",
"]",
"byte",
")",
"(",
"*",
"Node",
",",
"error",
")",
"{",
"n",
":=",
"&",
"Node",
"{",
"}",
"\n",
"_",
",",
"err",
":=",
"n",
".",
"UnmarshalMsg",
"(",
"data",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"err",
")",
"\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"n",
",",
"nil",
"\n",
"}"
] |
// nodeFromBytes returns a new node struct deserialized from the provided
// bytes. An error is returned for any deserialization errors.
|
[
"nodeFromBytes",
"returns",
"a",
"new",
"node",
"struct",
"deserialized",
"from",
"the",
"provided",
"bytes",
".",
"An",
"error",
"is",
"returned",
"for",
"any",
"deserialization",
"errors",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/node.go#L420-L429
|
train
|
Workiva/go-datastructures
|
bitarray/encoding.go
|
Marshal
|
func Marshal(ba BitArray) ([]byte, error) {
if eba, ok := ba.(*bitArray); ok {
return eba.Serialize()
} else if sba, ok := ba.(*sparseBitArray); ok {
return sba.Serialize()
} else {
return nil, errors.New("not a valid BitArray")
}
}
|
go
|
func Marshal(ba BitArray) ([]byte, error) {
if eba, ok := ba.(*bitArray); ok {
return eba.Serialize()
} else if sba, ok := ba.(*sparseBitArray); ok {
return sba.Serialize()
} else {
return nil, errors.New("not a valid BitArray")
}
}
|
[
"func",
"Marshal",
"(",
"ba",
"BitArray",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"if",
"eba",
",",
"ok",
":=",
"ba",
".",
"(",
"*",
"bitArray",
")",
";",
"ok",
"{",
"return",
"eba",
".",
"Serialize",
"(",
")",
"\n",
"}",
"else",
"if",
"sba",
",",
"ok",
":=",
"ba",
".",
"(",
"*",
"sparseBitArray",
")",
";",
"ok",
"{",
"return",
"sba",
".",
"Serialize",
"(",
")",
"\n",
"}",
"else",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}"
] |
// Marshal takes a dense or sparse bit array and serializes it to a
// byte slice.
|
[
"Marshal",
"takes",
"a",
"dense",
"or",
"sparse",
"bit",
"array",
"and",
"serializes",
"it",
"to",
"a",
"byte",
"slice",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/encoding.go#L28-L36
|
train
|
Workiva/go-datastructures
|
bitarray/encoding.go
|
Unmarshal
|
func Unmarshal(input []byte) (BitArray, error) {
if len(input) == 0 {
return nil, errors.New("no data in input")
}
if input[0] == 'B' {
ret := newBitArray(0)
err := ret.Deserialize(input)
if err != nil {
return nil, err
}
return ret, nil
} else if input[0] == 'S' {
ret := newSparseBitArray()
err := ret.Deserialize(input)
if err != nil {
return nil, err
}
return ret, nil
} else {
return nil, errors.New("unrecognized encoding")
}
}
|
go
|
func Unmarshal(input []byte) (BitArray, error) {
if len(input) == 0 {
return nil, errors.New("no data in input")
}
if input[0] == 'B' {
ret := newBitArray(0)
err := ret.Deserialize(input)
if err != nil {
return nil, err
}
return ret, nil
} else if input[0] == 'S' {
ret := newSparseBitArray()
err := ret.Deserialize(input)
if err != nil {
return nil, err
}
return ret, nil
} else {
return nil, errors.New("unrecognized encoding")
}
}
|
[
"func",
"Unmarshal",
"(",
"input",
"[",
"]",
"byte",
")",
"(",
"BitArray",
",",
"error",
")",
"{",
"if",
"len",
"(",
"input",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"input",
"[",
"0",
"]",
"==",
"'B'",
"{",
"ret",
":=",
"newBitArray",
"(",
"0",
")",
"\n",
"err",
":=",
"ret",
".",
"Deserialize",
"(",
"input",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"ret",
",",
"nil",
"\n",
"}",
"else",
"if",
"input",
"[",
"0",
"]",
"==",
"'S'",
"{",
"ret",
":=",
"newSparseBitArray",
"(",
")",
"\n",
"err",
":=",
"ret",
".",
"Deserialize",
"(",
"input",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"ret",
",",
"nil",
"\n",
"}",
"else",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}"
] |
// Unmarshal takes a byte slice, of the same format produced by Marshal,
// and returns a BitArray.
|
[
"Unmarshal",
"takes",
"a",
"byte",
"slice",
"of",
"the",
"same",
"format",
"produced",
"by",
"Marshal",
"and",
"returns",
"a",
"BitArray",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/encoding.go#L40-L61
|
train
|
Workiva/go-datastructures
|
bitarray/encoding.go
|
Serialize
|
func (ba *sparseBitArray) Serialize() ([]byte, error) {
w := new(bytes.Buffer)
var identifier uint8 = 'S'
err := binary.Write(w, binary.LittleEndian, identifier)
if err != nil {
return nil, err
}
blocksLen := uint64(len(ba.blocks))
indexLen := uint64(len(ba.indices))
err = binary.Write(w, binary.LittleEndian, blocksLen)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.blocks)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, indexLen)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.indices)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
|
go
|
func (ba *sparseBitArray) Serialize() ([]byte, error) {
w := new(bytes.Buffer)
var identifier uint8 = 'S'
err := binary.Write(w, binary.LittleEndian, identifier)
if err != nil {
return nil, err
}
blocksLen := uint64(len(ba.blocks))
indexLen := uint64(len(ba.indices))
err = binary.Write(w, binary.LittleEndian, blocksLen)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.blocks)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, indexLen)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.indices)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
|
[
"func",
"(",
"ba",
"*",
"sparseBitArray",
")",
"Serialize",
"(",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"w",
":=",
"new",
"(",
"bytes",
".",
"Buffer",
")",
"\n\n",
"var",
"identifier",
"uint8",
"=",
"'S'",
"\n",
"err",
":=",
"binary",
".",
"Write",
"(",
"w",
",",
"binary",
".",
"LittleEndian",
",",
"identifier",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"blocksLen",
":=",
"uint64",
"(",
"len",
"(",
"ba",
".",
"blocks",
")",
")",
"\n",
"indexLen",
":=",
"uint64",
"(",
"len",
"(",
"ba",
".",
"indices",
")",
")",
"\n\n",
"err",
"=",
"binary",
".",
"Write",
"(",
"w",
",",
"binary",
".",
"LittleEndian",
",",
"blocksLen",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"binary",
".",
"Write",
"(",
"w",
",",
"binary",
".",
"LittleEndian",
",",
"ba",
".",
"blocks",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"binary",
".",
"Write",
"(",
"w",
",",
"binary",
".",
"LittleEndian",
",",
"indexLen",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"binary",
".",
"Write",
"(",
"w",
",",
"binary",
".",
"LittleEndian",
",",
"ba",
".",
"indices",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"w",
".",
"Bytes",
"(",
")",
",",
"nil",
"\n",
"}"
] |
// Serialize converts the sparseBitArray to a byte slice
|
[
"Serialize",
"converts",
"the",
"sparseBitArray",
"to",
"a",
"byte",
"slice"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/encoding.go#L64-L96
|
train
|
Workiva/go-datastructures
|
bitarray/encoding.go
|
Uint64FromBytes
|
func Uint64FromBytes(b []byte) (uint64, int) {
if len(b) < 8 {
return 0, -1
}
val := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
return val, 8
}
|
go
|
func Uint64FromBytes(b []byte) (uint64, int) {
if len(b) < 8 {
return 0, -1
}
val := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
return val, 8
}
|
[
"func",
"Uint64FromBytes",
"(",
"b",
"[",
"]",
"byte",
")",
"(",
"uint64",
",",
"int",
")",
"{",
"if",
"len",
"(",
"b",
")",
"<",
"8",
"{",
"return",
"0",
",",
"-",
"1",
"\n",
"}",
"\n\n",
"val",
":=",
"uint64",
"(",
"b",
"[",
"0",
"]",
")",
"|",
"uint64",
"(",
"b",
"[",
"1",
"]",
")",
"<<",
"8",
"|",
"uint64",
"(",
"b",
"[",
"2",
"]",
")",
"<<",
"16",
"|",
"uint64",
"(",
"b",
"[",
"3",
"]",
")",
"<<",
"24",
"|",
"uint64",
"(",
"b",
"[",
"4",
"]",
")",
"<<",
"32",
"|",
"uint64",
"(",
"b",
"[",
"5",
"]",
")",
"<<",
"40",
"|",
"uint64",
"(",
"b",
"[",
"6",
"]",
")",
"<<",
"48",
"|",
"uint64",
"(",
"b",
"[",
"7",
"]",
")",
"<<",
"56",
"\n",
"return",
"val",
",",
"8",
"\n",
"}"
] |
// This function is a copy from the binary package, with some added error
// checking to avoid panics. The function will return the value, and the number
// of bytes read from the buffer. If the number of bytes is negative, then
// not enough bytes were passed in and the return value will be zero.
|
[
"This",
"function",
"is",
"a",
"copy",
"from",
"the",
"binary",
"package",
"with",
"some",
"added",
"error",
"checking",
"to",
"avoid",
"panics",
".",
"The",
"function",
"will",
"return",
"the",
"value",
"and",
"the",
"number",
"of",
"bytes",
"read",
"from",
"the",
"buffer",
".",
"If",
"the",
"number",
"of",
"bytes",
"is",
"negative",
"then",
"not",
"enough",
"bytes",
"were",
"passed",
"in",
"and",
"the",
"return",
"value",
"will",
"be",
"zero",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/encoding.go#L102-L110
|
train
|
Workiva/go-datastructures
|
bitarray/encoding.go
|
Deserialize
|
func (ret *sparseBitArray) Deserialize(incoming []byte) error {
var intsize = uint64(s / 8)
var curLoc = uint64(1) // Ignore the identifier byte
var intsToRead uint64
var bytesRead int
intsToRead, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
}
curLoc += intsize
var nextblock uint64
ret.blocks = make([]block, intsToRead)
for i := uint64(0); i < intsToRead; i++ {
nextblock, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
}
ret.blocks[i] = block(nextblock)
curLoc += intsize
}
intsToRead, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
}
curLoc += intsize
var nextuint uint64
ret.indices = make(uintSlice, intsToRead)
for i := uint64(0); i < intsToRead; i++ {
nextuint, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
}
ret.indices[i] = nextuint
curLoc += intsize
}
return nil
}
|
go
|
func (ret *sparseBitArray) Deserialize(incoming []byte) error {
var intsize = uint64(s / 8)
var curLoc = uint64(1) // Ignore the identifier byte
var intsToRead uint64
var bytesRead int
intsToRead, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
}
curLoc += intsize
var nextblock uint64
ret.blocks = make([]block, intsToRead)
for i := uint64(0); i < intsToRead; i++ {
nextblock, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
}
ret.blocks[i] = block(nextblock)
curLoc += intsize
}
intsToRead, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
}
curLoc += intsize
var nextuint uint64
ret.indices = make(uintSlice, intsToRead)
for i := uint64(0); i < intsToRead; i++ {
nextuint, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
}
ret.indices[i] = nextuint
curLoc += intsize
}
return nil
}
|
[
"func",
"(",
"ret",
"*",
"sparseBitArray",
")",
"Deserialize",
"(",
"incoming",
"[",
"]",
"byte",
")",
"error",
"{",
"var",
"intsize",
"=",
"uint64",
"(",
"s",
"/",
"8",
")",
"\n",
"var",
"curLoc",
"=",
"uint64",
"(",
"1",
")",
"// Ignore the identifier byte",
"\n\n",
"var",
"intsToRead",
"uint64",
"\n",
"var",
"bytesRead",
"int",
"\n",
"intsToRead",
",",
"bytesRead",
"=",
"Uint64FromBytes",
"(",
"incoming",
"[",
"curLoc",
":",
"curLoc",
"+",
"intsize",
"]",
")",
"\n",
"if",
"bytesRead",
"<",
"0",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"curLoc",
"+=",
"intsize",
"\n\n",
"var",
"nextblock",
"uint64",
"\n",
"ret",
".",
"blocks",
"=",
"make",
"(",
"[",
"]",
"block",
",",
"intsToRead",
")",
"\n",
"for",
"i",
":=",
"uint64",
"(",
"0",
")",
";",
"i",
"<",
"intsToRead",
";",
"i",
"++",
"{",
"nextblock",
",",
"bytesRead",
"=",
"Uint64FromBytes",
"(",
"incoming",
"[",
"curLoc",
":",
"curLoc",
"+",
"intsize",
"]",
")",
"\n",
"if",
"bytesRead",
"<",
"0",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"ret",
".",
"blocks",
"[",
"i",
"]",
"=",
"block",
"(",
"nextblock",
")",
"\n",
"curLoc",
"+=",
"intsize",
"\n",
"}",
"\n\n",
"intsToRead",
",",
"bytesRead",
"=",
"Uint64FromBytes",
"(",
"incoming",
"[",
"curLoc",
":",
"curLoc",
"+",
"intsize",
"]",
")",
"\n",
"if",
"bytesRead",
"<",
"0",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"curLoc",
"+=",
"intsize",
"\n\n",
"var",
"nextuint",
"uint64",
"\n",
"ret",
".",
"indices",
"=",
"make",
"(",
"uintSlice",
",",
"intsToRead",
")",
"\n",
"for",
"i",
":=",
"uint64",
"(",
"0",
")",
";",
"i",
"<",
"intsToRead",
";",
"i",
"++",
"{",
"nextuint",
",",
"bytesRead",
"=",
"Uint64FromBytes",
"(",
"incoming",
"[",
"curLoc",
":",
"curLoc",
"+",
"intsize",
"]",
")",
"\n",
"if",
"bytesRead",
"<",
"0",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"ret",
".",
"indices",
"[",
"i",
"]",
"=",
"nextuint",
"\n",
"curLoc",
"+=",
"intsize",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Deserialize takes the incoming byte slice, and populates the sparseBitArray
// with data in the bytes. Note that this will overwrite any capacity
// specified when creating the sparseBitArray. Also note that if an error
// is returned, the sparseBitArray this is called on might be populated
// with partial data.
|
[
"Deserialize",
"takes",
"the",
"incoming",
"byte",
"slice",
"and",
"populates",
"the",
"sparseBitArray",
"with",
"data",
"in",
"the",
"bytes",
".",
"Note",
"that",
"this",
"will",
"overwrite",
"any",
"capacity",
"specified",
"when",
"creating",
"the",
"sparseBitArray",
".",
"Also",
"note",
"that",
"if",
"an",
"error",
"is",
"returned",
"the",
"sparseBitArray",
"this",
"is",
"called",
"on",
"might",
"be",
"populated",
"with",
"partial",
"data",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/encoding.go#L117-L157
|
train
|
Workiva/go-datastructures
|
bitarray/encoding.go
|
Serialize
|
func (ba *bitArray) Serialize() ([]byte, error) {
w := new(bytes.Buffer)
var identifier uint8 = 'B'
err := binary.Write(w, binary.LittleEndian, identifier)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.lowest)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.highest)
if err != nil {
return nil, err
}
var encodedanyset uint8
if ba.anyset {
encodedanyset = 1
} else {
encodedanyset = 0
}
err = binary.Write(w, binary.LittleEndian, encodedanyset)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.blocks)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
|
go
|
func (ba *bitArray) Serialize() ([]byte, error) {
w := new(bytes.Buffer)
var identifier uint8 = 'B'
err := binary.Write(w, binary.LittleEndian, identifier)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.lowest)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.highest)
if err != nil {
return nil, err
}
var encodedanyset uint8
if ba.anyset {
encodedanyset = 1
} else {
encodedanyset = 0
}
err = binary.Write(w, binary.LittleEndian, encodedanyset)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.blocks)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"Serialize",
"(",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"w",
":=",
"new",
"(",
"bytes",
".",
"Buffer",
")",
"\n\n",
"var",
"identifier",
"uint8",
"=",
"'B'",
"\n",
"err",
":=",
"binary",
".",
"Write",
"(",
"w",
",",
"binary",
".",
"LittleEndian",
",",
"identifier",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"binary",
".",
"Write",
"(",
"w",
",",
"binary",
".",
"LittleEndian",
",",
"ba",
".",
"lowest",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"err",
"=",
"binary",
".",
"Write",
"(",
"w",
",",
"binary",
".",
"LittleEndian",
",",
"ba",
".",
"highest",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"var",
"encodedanyset",
"uint8",
"\n",
"if",
"ba",
".",
"anyset",
"{",
"encodedanyset",
"=",
"1",
"\n",
"}",
"else",
"{",
"encodedanyset",
"=",
"0",
"\n",
"}",
"\n",
"err",
"=",
"binary",
".",
"Write",
"(",
"w",
",",
"binary",
".",
"LittleEndian",
",",
"encodedanyset",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"binary",
".",
"Write",
"(",
"w",
",",
"binary",
".",
"LittleEndian",
",",
"ba",
".",
"blocks",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"w",
".",
"Bytes",
"(",
")",
",",
"nil",
"\n",
"}"
] |
// Serialize converts the bitArray to a byte slice.
|
[
"Serialize",
"converts",
"the",
"bitArray",
"to",
"a",
"byte",
"slice",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/encoding.go#L160-L194
|
train
|
Workiva/go-datastructures
|
bitarray/encoding.go
|
Deserialize
|
func (ret *bitArray) Deserialize(incoming []byte) error {
r := bytes.NewReader(incoming[1:]) // Discard identifier
err := binary.Read(r, binary.LittleEndian, &ret.lowest)
if err != nil {
return err
}
err = binary.Read(r, binary.LittleEndian, &ret.highest)
if err != nil {
return err
}
var encodedanyset uint8
err = binary.Read(r, binary.LittleEndian, &encodedanyset)
if err != nil {
return err
}
// anyset defaults to false so we don't need an else statement
if encodedanyset == 1 {
ret.anyset = true
}
var nextblock block
err = binary.Read(r, binary.LittleEndian, &nextblock)
for err == nil {
ret.blocks = append(ret.blocks, nextblock)
err = binary.Read(r, binary.LittleEndian, &nextblock)
}
if err != io.EOF {
return err
}
return nil
}
|
go
|
func (ret *bitArray) Deserialize(incoming []byte) error {
r := bytes.NewReader(incoming[1:]) // Discard identifier
err := binary.Read(r, binary.LittleEndian, &ret.lowest)
if err != nil {
return err
}
err = binary.Read(r, binary.LittleEndian, &ret.highest)
if err != nil {
return err
}
var encodedanyset uint8
err = binary.Read(r, binary.LittleEndian, &encodedanyset)
if err != nil {
return err
}
// anyset defaults to false so we don't need an else statement
if encodedanyset == 1 {
ret.anyset = true
}
var nextblock block
err = binary.Read(r, binary.LittleEndian, &nextblock)
for err == nil {
ret.blocks = append(ret.blocks, nextblock)
err = binary.Read(r, binary.LittleEndian, &nextblock)
}
if err != io.EOF {
return err
}
return nil
}
|
[
"func",
"(",
"ret",
"*",
"bitArray",
")",
"Deserialize",
"(",
"incoming",
"[",
"]",
"byte",
")",
"error",
"{",
"r",
":=",
"bytes",
".",
"NewReader",
"(",
"incoming",
"[",
"1",
":",
"]",
")",
"// Discard identifier",
"\n\n",
"err",
":=",
"binary",
".",
"Read",
"(",
"r",
",",
"binary",
".",
"LittleEndian",
",",
"&",
"ret",
".",
"lowest",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"binary",
".",
"Read",
"(",
"r",
",",
"binary",
".",
"LittleEndian",
",",
"&",
"ret",
".",
"highest",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"var",
"encodedanyset",
"uint8",
"\n",
"err",
"=",
"binary",
".",
"Read",
"(",
"r",
",",
"binary",
".",
"LittleEndian",
",",
"&",
"encodedanyset",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"// anyset defaults to false so we don't need an else statement",
"if",
"encodedanyset",
"==",
"1",
"{",
"ret",
".",
"anyset",
"=",
"true",
"\n",
"}",
"\n\n",
"var",
"nextblock",
"block",
"\n",
"err",
"=",
"binary",
".",
"Read",
"(",
"r",
",",
"binary",
".",
"LittleEndian",
",",
"&",
"nextblock",
")",
"\n",
"for",
"err",
"==",
"nil",
"{",
"ret",
".",
"blocks",
"=",
"append",
"(",
"ret",
".",
"blocks",
",",
"nextblock",
")",
"\n",
"err",
"=",
"binary",
".",
"Read",
"(",
"r",
",",
"binary",
".",
"LittleEndian",
",",
"&",
"nextblock",
")",
"\n",
"}",
"\n",
"if",
"err",
"!=",
"io",
".",
"EOF",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Deserialize takes the incoming byte slice, and populates the bitArray
// with data in the bytes. Note that this will overwrite any capacity
// specified when creating the bitArray. Also note that if an error is returned,
// the bitArray this is called on might be populated with partial data.
|
[
"Deserialize",
"takes",
"the",
"incoming",
"byte",
"slice",
"and",
"populates",
"the",
"bitArray",
"with",
"data",
"in",
"the",
"bytes",
".",
"Note",
"that",
"this",
"will",
"overwrite",
"any",
"capacity",
"specified",
"when",
"creating",
"the",
"bitArray",
".",
"Also",
"note",
"that",
"if",
"an",
"error",
"is",
"returned",
"the",
"bitArray",
"this",
"is",
"called",
"on",
"might",
"be",
"populated",
"with",
"partial",
"data",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/encoding.go#L200-L234
|
train
|
Workiva/go-datastructures
|
btree/plus/btree.go
|
Iter
|
func (tree *btree) Iter(key Key) Iterator {
if tree.root == nil {
return nilIterator()
}
return tree.root.find(key)
}
|
go
|
func (tree *btree) Iter(key Key) Iterator {
if tree.root == nil {
return nilIterator()
}
return tree.root.find(key)
}
|
[
"func",
"(",
"tree",
"*",
"btree",
")",
"Iter",
"(",
"key",
"Key",
")",
"Iterator",
"{",
"if",
"tree",
".",
"root",
"==",
"nil",
"{",
"return",
"nilIterator",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"tree",
".",
"root",
".",
"find",
"(",
"key",
")",
"\n",
"}"
] |
// Iter returns an iterator that can be used to traverse the b-tree
// starting from the specified key or its successor.
|
[
"Iter",
"returns",
"an",
"iterator",
"that",
"can",
"be",
"used",
"to",
"traverse",
"the",
"b",
"-",
"tree",
"starting",
"from",
"the",
"specified",
"key",
"or",
"its",
"successor",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/plus/btree.go#L87-L93
|
train
|
Workiva/go-datastructures
|
btree/palm/tree.go
|
Get
|
func (ptree *ptree) Get(keys ...common.Comparator) common.Comparators {
ga := newGetAction(keys)
ptree.checkAndRun(ga)
ga.completer.Wait()
return ga.result
}
|
go
|
func (ptree *ptree) Get(keys ...common.Comparator) common.Comparators {
ga := newGetAction(keys)
ptree.checkAndRun(ga)
ga.completer.Wait()
return ga.result
}
|
[
"func",
"(",
"ptree",
"*",
"ptree",
")",
"Get",
"(",
"keys",
"...",
"common",
".",
"Comparator",
")",
"common",
".",
"Comparators",
"{",
"ga",
":=",
"newGetAction",
"(",
"keys",
")",
"\n",
"ptree",
".",
"checkAndRun",
"(",
"ga",
")",
"\n",
"ga",
".",
"completer",
".",
"Wait",
"(",
")",
"\n",
"return",
"ga",
".",
"result",
"\n",
"}"
] |
// Get will retrieve a list of keys from the provided keys.
|
[
"Get",
"will",
"retrieve",
"a",
"list",
"of",
"keys",
"from",
"the",
"provided",
"keys",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/palm/tree.go#L503-L508
|
train
|
Workiva/go-datastructures
|
btree/palm/tree.go
|
Query
|
func (ptree *ptree) Query(start, stop common.Comparator) common.Comparators {
cmps := make(common.Comparators, 0, 32)
aa := newApplyAction(func(cmp common.Comparator) bool {
cmps = append(cmps, cmp)
return true
}, start, stop)
ptree.checkAndRun(aa)
aa.completer.Wait()
return cmps
}
|
go
|
func (ptree *ptree) Query(start, stop common.Comparator) common.Comparators {
cmps := make(common.Comparators, 0, 32)
aa := newApplyAction(func(cmp common.Comparator) bool {
cmps = append(cmps, cmp)
return true
}, start, stop)
ptree.checkAndRun(aa)
aa.completer.Wait()
return cmps
}
|
[
"func",
"(",
"ptree",
"*",
"ptree",
")",
"Query",
"(",
"start",
",",
"stop",
"common",
".",
"Comparator",
")",
"common",
".",
"Comparators",
"{",
"cmps",
":=",
"make",
"(",
"common",
".",
"Comparators",
",",
"0",
",",
"32",
")",
"\n",
"aa",
":=",
"newApplyAction",
"(",
"func",
"(",
"cmp",
"common",
".",
"Comparator",
")",
"bool",
"{",
"cmps",
"=",
"append",
"(",
"cmps",
",",
"cmp",
")",
"\n",
"return",
"true",
"\n",
"}",
",",
"start",
",",
"stop",
")",
"\n",
"ptree",
".",
"checkAndRun",
"(",
"aa",
")",
"\n",
"aa",
".",
"completer",
".",
"Wait",
"(",
")",
"\n",
"return",
"cmps",
"\n",
"}"
] |
// Query will return a list of Comparators that fall within the
// provided start and stop Comparators. Start is inclusive while
// stop is exclusive, ie [start, stop).
|
[
"Query",
"will",
"return",
"a",
"list",
"of",
"Comparators",
"that",
"fall",
"within",
"the",
"provided",
"start",
"and",
"stop",
"Comparators",
".",
"Start",
"is",
"inclusive",
"while",
"stop",
"is",
"exclusive",
"ie",
"[",
"start",
"stop",
")",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/palm/tree.go#L518-L527
|
train
|
Workiva/go-datastructures
|
rtree/hilbert/hilbert.go
|
chunkRectangles
|
func chunkRectangles(slice rtree.Rectangles, numParts int64) []rtree.Rectangles {
parts := make([]rtree.Rectangles, numParts)
for i := int64(0); i < numParts; i++ {
parts[i] = slice[i*int64(len(slice))/numParts : (i+1)*int64(len(slice))/numParts]
}
return parts
}
|
go
|
func chunkRectangles(slice rtree.Rectangles, numParts int64) []rtree.Rectangles {
parts := make([]rtree.Rectangles, numParts)
for i := int64(0); i < numParts; i++ {
parts[i] = slice[i*int64(len(slice))/numParts : (i+1)*int64(len(slice))/numParts]
}
return parts
}
|
[
"func",
"chunkRectangles",
"(",
"slice",
"rtree",
".",
"Rectangles",
",",
"numParts",
"int64",
")",
"[",
"]",
"rtree",
".",
"Rectangles",
"{",
"parts",
":=",
"make",
"(",
"[",
"]",
"rtree",
".",
"Rectangles",
",",
"numParts",
")",
"\n",
"for",
"i",
":=",
"int64",
"(",
"0",
")",
";",
"i",
"<",
"numParts",
";",
"i",
"++",
"{",
"parts",
"[",
"i",
"]",
"=",
"slice",
"[",
"i",
"*",
"int64",
"(",
"len",
"(",
"slice",
")",
")",
"/",
"numParts",
":",
"(",
"i",
"+",
"1",
")",
"*",
"int64",
"(",
"len",
"(",
"slice",
")",
")",
"/",
"numParts",
"]",
"\n",
"}",
"\n",
"return",
"parts",
"\n",
"}"
] |
// chunkRectangles takes a slice of rtree.Rectangle values and chunks it into `numParts` subslices.
|
[
"chunkRectangles",
"takes",
"a",
"slice",
"of",
"rtree",
".",
"Rectangle",
"values",
"and",
"chunks",
"it",
"into",
"numParts",
"subslices",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rtree/hilbert/hilbert.go#L73-L79
|
train
|
Workiva/go-datastructures
|
sort/sort.go
|
MultithreadedSortComparators
|
func MultithreadedSortComparators(comparators Comparators) Comparators {
toBeSorted := make(Comparators, len(comparators))
copy(toBeSorted, comparators)
var wg sync.WaitGroup
numCPU := int64(runtime.NumCPU())
if numCPU%2 == 1 { // single core machine
numCPU++
}
chunks := chunk(toBeSorted, numCPU)
wg.Add(len(chunks))
for i := 0; i < len(chunks); i++ {
go func(i int) {
sortBucket(chunks[i])
wg.Done()
}(i)
}
wg.Wait()
todo := make([]Comparators, len(chunks)/2)
for {
todo = todo[:len(chunks)/2]
wg.Add(len(chunks) / 2)
for i := 0; i < len(chunks); i += 2 {
go func(i int) {
todo[i/2] = SymMerge(chunks[i], chunks[i+1])
wg.Done()
}(i)
}
wg.Wait()
chunks = copyChunk(todo)
if len(chunks) == 1 {
break
}
}
return chunks[0]
}
|
go
|
func MultithreadedSortComparators(comparators Comparators) Comparators {
toBeSorted := make(Comparators, len(comparators))
copy(toBeSorted, comparators)
var wg sync.WaitGroup
numCPU := int64(runtime.NumCPU())
if numCPU%2 == 1 { // single core machine
numCPU++
}
chunks := chunk(toBeSorted, numCPU)
wg.Add(len(chunks))
for i := 0; i < len(chunks); i++ {
go func(i int) {
sortBucket(chunks[i])
wg.Done()
}(i)
}
wg.Wait()
todo := make([]Comparators, len(chunks)/2)
for {
todo = todo[:len(chunks)/2]
wg.Add(len(chunks) / 2)
for i := 0; i < len(chunks); i += 2 {
go func(i int) {
todo[i/2] = SymMerge(chunks[i], chunks[i+1])
wg.Done()
}(i)
}
wg.Wait()
chunks = copyChunk(todo)
if len(chunks) == 1 {
break
}
}
return chunks[0]
}
|
[
"func",
"MultithreadedSortComparators",
"(",
"comparators",
"Comparators",
")",
"Comparators",
"{",
"toBeSorted",
":=",
"make",
"(",
"Comparators",
",",
"len",
"(",
"comparators",
")",
")",
"\n",
"copy",
"(",
"toBeSorted",
",",
"comparators",
")",
"\n\n",
"var",
"wg",
"sync",
".",
"WaitGroup",
"\n\n",
"numCPU",
":=",
"int64",
"(",
"runtime",
".",
"NumCPU",
"(",
")",
")",
"\n",
"if",
"numCPU",
"%",
"2",
"==",
"1",
"{",
"// single core machine",
"numCPU",
"++",
"\n",
"}",
"\n\n",
"chunks",
":=",
"chunk",
"(",
"toBeSorted",
",",
"numCPU",
")",
"\n",
"wg",
".",
"Add",
"(",
"len",
"(",
"chunks",
")",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"chunks",
")",
";",
"i",
"++",
"{",
"go",
"func",
"(",
"i",
"int",
")",
"{",
"sortBucket",
"(",
"chunks",
"[",
"i",
"]",
")",
"\n",
"wg",
".",
"Done",
"(",
")",
"\n",
"}",
"(",
"i",
")",
"\n",
"}",
"\n\n",
"wg",
".",
"Wait",
"(",
")",
"\n",
"todo",
":=",
"make",
"(",
"[",
"]",
"Comparators",
",",
"len",
"(",
"chunks",
")",
"/",
"2",
")",
"\n",
"for",
"{",
"todo",
"=",
"todo",
"[",
":",
"len",
"(",
"chunks",
")",
"/",
"2",
"]",
"\n",
"wg",
".",
"Add",
"(",
"len",
"(",
"chunks",
")",
"/",
"2",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"chunks",
")",
";",
"i",
"+=",
"2",
"{",
"go",
"func",
"(",
"i",
"int",
")",
"{",
"todo",
"[",
"i",
"/",
"2",
"]",
"=",
"SymMerge",
"(",
"chunks",
"[",
"i",
"]",
",",
"chunks",
"[",
"i",
"+",
"1",
"]",
")",
"\n",
"wg",
".",
"Done",
"(",
")",
"\n",
"}",
"(",
"i",
")",
"\n",
"}",
"\n\n",
"wg",
".",
"Wait",
"(",
")",
"\n\n",
"chunks",
"=",
"copyChunk",
"(",
"todo",
")",
"\n",
"if",
"len",
"(",
"chunks",
")",
"==",
"1",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"chunks",
"[",
"0",
"]",
"\n",
"}"
] |
// MultithreadedSortComparators will take a list of comparators
// and sort it using as many threads as are available. The list
// is split into buckets for a bucket sort and then recursively
// merged using SymMerge.
|
[
"MultithreadedSortComparators",
"will",
"take",
"a",
"list",
"of",
"comparators",
"and",
"sort",
"it",
"using",
"as",
"many",
"threads",
"as",
"are",
"available",
".",
"The",
"list",
"is",
"split",
"into",
"buckets",
"for",
"a",
"bucket",
"sort",
"and",
"then",
"recursively",
"merged",
"using",
"SymMerge",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/sort/sort.go#L23-L64
|
train
|
Workiva/go-datastructures
|
rtree/hilbert/tree.go
|
Search
|
func (tree *tree) Search(rect rtree.Rectangle) rtree.Rectangles {
ga := newGetAction(rect)
tree.checkAndRun(ga)
ga.completer.Wait()
return ga.result
}
|
go
|
func (tree *tree) Search(rect rtree.Rectangle) rtree.Rectangles {
ga := newGetAction(rect)
tree.checkAndRun(ga)
ga.completer.Wait()
return ga.result
}
|
[
"func",
"(",
"tree",
"*",
"tree",
")",
"Search",
"(",
"rect",
"rtree",
".",
"Rectangle",
")",
"rtree",
".",
"Rectangles",
"{",
"ga",
":=",
"newGetAction",
"(",
"rect",
")",
"\n",
"tree",
".",
"checkAndRun",
"(",
"ga",
")",
"\n",
"ga",
".",
"completer",
".",
"Wait",
"(",
")",
"\n",
"return",
"ga",
".",
"result",
"\n",
"}"
] |
// Search will return a list of rectangles that intersect the provided
// rectangle.
|
[
"Search",
"will",
"return",
"a",
"list",
"of",
"rectangles",
"that",
"intersect",
"the",
"provided",
"rectangle",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/rtree/hilbert/tree.go#L403-L408
|
train
|
Workiva/go-datastructures
|
list/persistent.go
|
Length
|
func (l *list) Length() uint {
curr := l
length := uint(0)
for {
length += 1
tail, _ := curr.Tail()
if tail.IsEmpty() {
return length
}
curr = tail.(*list)
}
}
|
go
|
func (l *list) Length() uint {
curr := l
length := uint(0)
for {
length += 1
tail, _ := curr.Tail()
if tail.IsEmpty() {
return length
}
curr = tail.(*list)
}
}
|
[
"func",
"(",
"l",
"*",
"list",
")",
"Length",
"(",
")",
"uint",
"{",
"curr",
":=",
"l",
"\n",
"length",
":=",
"uint",
"(",
"0",
")",
"\n",
"for",
"{",
"length",
"+=",
"1",
"\n",
"tail",
",",
"_",
":=",
"curr",
".",
"Tail",
"(",
")",
"\n",
"if",
"tail",
".",
"IsEmpty",
"(",
")",
"{",
"return",
"length",
"\n",
"}",
"\n",
"curr",
"=",
"tail",
".",
"(",
"*",
"list",
")",
"\n",
"}",
"\n",
"}"
] |
// Length returns the number of items in the list.
|
[
"Length",
"returns",
"the",
"number",
"of",
"items",
"in",
"the",
"list",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/list/persistent.go#L169-L180
|
train
|
Workiva/go-datastructures
|
list/persistent.go
|
Get
|
func (l *list) Get(pos uint) (interface{}, bool) {
if pos == 0 {
return l.head, true
}
return l.tail.Get(pos - 1)
}
|
go
|
func (l *list) Get(pos uint) (interface{}, bool) {
if pos == 0 {
return l.head, true
}
return l.tail.Get(pos - 1)
}
|
[
"func",
"(",
"l",
"*",
"list",
")",
"Get",
"(",
"pos",
"uint",
")",
"(",
"interface",
"{",
"}",
",",
"bool",
")",
"{",
"if",
"pos",
"==",
"0",
"{",
"return",
"l",
".",
"head",
",",
"true",
"\n",
"}",
"\n",
"return",
"l",
".",
"tail",
".",
"Get",
"(",
"pos",
"-",
"1",
")",
"\n",
"}"
] |
// Get returns the item at the given position or an error if the position is
// invalid.
|
[
"Get",
"returns",
"the",
"item",
"at",
"the",
"given",
"position",
"or",
"an",
"error",
"if",
"the",
"position",
"is",
"invalid",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/list/persistent.go#L202-L207
|
train
|
Workiva/go-datastructures
|
list/persistent.go
|
Remove
|
func (l *list) Remove(pos uint) (PersistentList, error) {
if pos == 0 {
nl, _ := l.Tail()
return nl, nil
}
nl, err := l.tail.Remove(pos - 1)
if err != nil {
return nil, err
}
return &list{l.head, nl}, nil
}
|
go
|
func (l *list) Remove(pos uint) (PersistentList, error) {
if pos == 0 {
nl, _ := l.Tail()
return nl, nil
}
nl, err := l.tail.Remove(pos - 1)
if err != nil {
return nil, err
}
return &list{l.head, nl}, nil
}
|
[
"func",
"(",
"l",
"*",
"list",
")",
"Remove",
"(",
"pos",
"uint",
")",
"(",
"PersistentList",
",",
"error",
")",
"{",
"if",
"pos",
"==",
"0",
"{",
"nl",
",",
"_",
":=",
"l",
".",
"Tail",
"(",
")",
"\n",
"return",
"nl",
",",
"nil",
"\n",
"}",
"\n\n",
"nl",
",",
"err",
":=",
"l",
".",
"tail",
".",
"Remove",
"(",
"pos",
"-",
"1",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"&",
"list",
"{",
"l",
".",
"head",
",",
"nl",
"}",
",",
"nil",
"\n",
"}"
] |
// Remove will remove the item at the given position, returning the new list or
// an error if the position is invalid.
|
[
"Remove",
"will",
"remove",
"the",
"item",
"at",
"the",
"given",
"position",
"returning",
"the",
"new",
"list",
"or",
"an",
"error",
"if",
"the",
"position",
"is",
"invalid",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/list/persistent.go#L211-L222
|
train
|
Workiva/go-datastructures
|
list/persistent.go
|
Find
|
func (l *list) Find(pred func(interface{}) bool) (interface{}, bool) {
if pred(l.head) {
return l.head, true
}
return l.tail.Find(pred)
}
|
go
|
func (l *list) Find(pred func(interface{}) bool) (interface{}, bool) {
if pred(l.head) {
return l.head, true
}
return l.tail.Find(pred)
}
|
[
"func",
"(",
"l",
"*",
"list",
")",
"Find",
"(",
"pred",
"func",
"(",
"interface",
"{",
"}",
")",
"bool",
")",
"(",
"interface",
"{",
"}",
",",
"bool",
")",
"{",
"if",
"pred",
"(",
"l",
".",
"head",
")",
"{",
"return",
"l",
".",
"head",
",",
"true",
"\n",
"}",
"\n",
"return",
"l",
".",
"tail",
".",
"Find",
"(",
"pred",
")",
"\n",
"}"
] |
// Find applies the predicate function to the list and returns the first item
// which matches.
|
[
"Find",
"applies",
"the",
"predicate",
"function",
"to",
"the",
"list",
"and",
"returns",
"the",
"first",
"item",
"which",
"matches",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/list/persistent.go#L226-L231
|
train
|
Workiva/go-datastructures
|
list/persistent.go
|
FindIndex
|
func (l *list) FindIndex(pred func(interface{}) bool) int {
curr := l
idx := 0
for {
if pred(curr.head) {
return idx
}
tail, _ := curr.Tail()
if tail.IsEmpty() {
return -1
}
curr = tail.(*list)
idx += 1
}
}
|
go
|
func (l *list) FindIndex(pred func(interface{}) bool) int {
curr := l
idx := 0
for {
if pred(curr.head) {
return idx
}
tail, _ := curr.Tail()
if tail.IsEmpty() {
return -1
}
curr = tail.(*list)
idx += 1
}
}
|
[
"func",
"(",
"l",
"*",
"list",
")",
"FindIndex",
"(",
"pred",
"func",
"(",
"interface",
"{",
"}",
")",
"bool",
")",
"int",
"{",
"curr",
":=",
"l",
"\n",
"idx",
":=",
"0",
"\n",
"for",
"{",
"if",
"pred",
"(",
"curr",
".",
"head",
")",
"{",
"return",
"idx",
"\n",
"}",
"\n",
"tail",
",",
"_",
":=",
"curr",
".",
"Tail",
"(",
")",
"\n",
"if",
"tail",
".",
"IsEmpty",
"(",
")",
"{",
"return",
"-",
"1",
"\n",
"}",
"\n",
"curr",
"=",
"tail",
".",
"(",
"*",
"list",
")",
"\n",
"idx",
"+=",
"1",
"\n",
"}",
"\n",
"}"
] |
// FindIndex applies the predicate function to the list and returns the index
// of the first item which matches or -1 if there is no match.
|
[
"FindIndex",
"applies",
"the",
"predicate",
"function",
"to",
"the",
"list",
"and",
"returns",
"the",
"index",
"of",
"the",
"first",
"item",
"which",
"matches",
"or",
"-",
"1",
"if",
"there",
"is",
"no",
"match",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/list/persistent.go#L235-L249
|
train
|
Workiva/go-datastructures
|
list/persistent.go
|
Map
|
func (l *list) Map(f func(interface{}) interface{}) []interface{} {
return append(l.tail.Map(f), f(l.head))
}
|
go
|
func (l *list) Map(f func(interface{}) interface{}) []interface{} {
return append(l.tail.Map(f), f(l.head))
}
|
[
"func",
"(",
"l",
"*",
"list",
")",
"Map",
"(",
"f",
"func",
"(",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
")",
"[",
"]",
"interface",
"{",
"}",
"{",
"return",
"append",
"(",
"l",
".",
"tail",
".",
"Map",
"(",
"f",
")",
",",
"f",
"(",
"l",
".",
"head",
")",
")",
"\n",
"}"
] |
// Map applies the function to each entry in the list and returns the resulting
// slice.
|
[
"Map",
"applies",
"the",
"function",
"to",
"each",
"entry",
"in",
"the",
"list",
"and",
"returns",
"the",
"resulting",
"slice",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/list/persistent.go#L253-L255
|
train
|
Workiva/go-datastructures
|
numerics/hilbert/hilbert.go
|
Encode
|
func Encode(x, y int32) int64 {
var rx, ry int32
var d int64
for s := int32(n / 2); s > 0; s /= 2 {
rx = boolToInt(x&s > 0)
ry = boolToInt(y&s > 0)
d += int64(int64(s) * int64(s) * int64(((3 * rx) ^ ry)))
rotate(s, rx, ry, &x, &y)
}
return d
}
|
go
|
func Encode(x, y int32) int64 {
var rx, ry int32
var d int64
for s := int32(n / 2); s > 0; s /= 2 {
rx = boolToInt(x&s > 0)
ry = boolToInt(y&s > 0)
d += int64(int64(s) * int64(s) * int64(((3 * rx) ^ ry)))
rotate(s, rx, ry, &x, &y)
}
return d
}
|
[
"func",
"Encode",
"(",
"x",
",",
"y",
"int32",
")",
"int64",
"{",
"var",
"rx",
",",
"ry",
"int32",
"\n",
"var",
"d",
"int64",
"\n",
"for",
"s",
":=",
"int32",
"(",
"n",
"/",
"2",
")",
";",
"s",
">",
"0",
";",
"s",
"/=",
"2",
"{",
"rx",
"=",
"boolToInt",
"(",
"x",
"&",
"s",
">",
"0",
")",
"\n",
"ry",
"=",
"boolToInt",
"(",
"y",
"&",
"s",
">",
"0",
")",
"\n",
"d",
"+=",
"int64",
"(",
"int64",
"(",
"s",
")",
"*",
"int64",
"(",
"s",
")",
"*",
"int64",
"(",
"(",
"(",
"3",
"*",
"rx",
")",
"^",
"ry",
")",
")",
")",
"\n",
"rotate",
"(",
"s",
",",
"rx",
",",
"ry",
",",
"&",
"x",
",",
"&",
"y",
")",
"\n",
"}",
"\n\n",
"return",
"d",
"\n",
"}"
] |
// Encode will encode the provided x and y coordinates into a Hilbert
// distance.
|
[
"Encode",
"will",
"encode",
"the",
"provided",
"x",
"and",
"y",
"coordinates",
"into",
"a",
"Hilbert",
"distance",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/hilbert/hilbert.go#L62-L73
|
train
|
Workiva/go-datastructures
|
numerics/hilbert/hilbert.go
|
Decode
|
func Decode(h int64) (int32, int32) {
var ry, rx int64
var x, y int32
t := h
for s := int64(1); s < int64(n); s *= 2 {
rx = 1 & (t / 2)
ry = 1 & (t ^ rx)
rotate(int32(s), int32(rx), int32(ry), &x, &y)
x += int32(s * rx)
y += int32(s * ry)
t /= 4
}
return x, y
}
|
go
|
func Decode(h int64) (int32, int32) {
var ry, rx int64
var x, y int32
t := h
for s := int64(1); s < int64(n); s *= 2 {
rx = 1 & (t / 2)
ry = 1 & (t ^ rx)
rotate(int32(s), int32(rx), int32(ry), &x, &y)
x += int32(s * rx)
y += int32(s * ry)
t /= 4
}
return x, y
}
|
[
"func",
"Decode",
"(",
"h",
"int64",
")",
"(",
"int32",
",",
"int32",
")",
"{",
"var",
"ry",
",",
"rx",
"int64",
"\n",
"var",
"x",
",",
"y",
"int32",
"\n",
"t",
":=",
"h",
"\n\n",
"for",
"s",
":=",
"int64",
"(",
"1",
")",
";",
"s",
"<",
"int64",
"(",
"n",
")",
";",
"s",
"*=",
"2",
"{",
"rx",
"=",
"1",
"&",
"(",
"t",
"/",
"2",
")",
"\n",
"ry",
"=",
"1",
"&",
"(",
"t",
"^",
"rx",
")",
"\n",
"rotate",
"(",
"int32",
"(",
"s",
")",
",",
"int32",
"(",
"rx",
")",
",",
"int32",
"(",
"ry",
")",
",",
"&",
"x",
",",
"&",
"y",
")",
"\n",
"x",
"+=",
"int32",
"(",
"s",
"*",
"rx",
")",
"\n",
"y",
"+=",
"int32",
"(",
"s",
"*",
"ry",
")",
"\n",
"t",
"/=",
"4",
"\n",
"}",
"\n\n",
"return",
"x",
",",
"y",
"\n",
"}"
] |
// Decode will decode the provided Hilbert distance into a corresponding
// x and y value, respectively.
|
[
"Decode",
"will",
"decode",
"the",
"provided",
"Hilbert",
"distance",
"into",
"a",
"corresponding",
"x",
"and",
"y",
"value",
"respectively",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/numerics/hilbert/hilbert.go#L77-L92
|
train
|
Workiva/go-datastructures
|
trie/yfast/entries.go
|
insert
|
func (entries *Entries) insert(entry Entry) Entry {
i := entries.search(entry.Key())
if i == len(*entries) {
*entries = append(*entries, entry)
return nil
}
if (*entries)[i].Key() == entry.Key() {
oldEntry := (*entries)[i]
(*entries)[i] = entry
return oldEntry
}
(*entries) = append(*entries, nil)
copy((*entries)[i+1:], (*entries)[i:])
(*entries)[i] = entry
return nil
}
|
go
|
func (entries *Entries) insert(entry Entry) Entry {
i := entries.search(entry.Key())
if i == len(*entries) {
*entries = append(*entries, entry)
return nil
}
if (*entries)[i].Key() == entry.Key() {
oldEntry := (*entries)[i]
(*entries)[i] = entry
return oldEntry
}
(*entries) = append(*entries, nil)
copy((*entries)[i+1:], (*entries)[i:])
(*entries)[i] = entry
return nil
}
|
[
"func",
"(",
"entries",
"*",
"Entries",
")",
"insert",
"(",
"entry",
"Entry",
")",
"Entry",
"{",
"i",
":=",
"entries",
".",
"search",
"(",
"entry",
".",
"Key",
"(",
")",
")",
"\n\n",
"if",
"i",
"==",
"len",
"(",
"*",
"entries",
")",
"{",
"*",
"entries",
"=",
"append",
"(",
"*",
"entries",
",",
"entry",
")",
"\n",
"return",
"nil",
"\n",
"}",
"\n\n",
"if",
"(",
"*",
"entries",
")",
"[",
"i",
"]",
".",
"Key",
"(",
")",
"==",
"entry",
".",
"Key",
"(",
")",
"{",
"oldEntry",
":=",
"(",
"*",
"entries",
")",
"[",
"i",
"]",
"\n",
"(",
"*",
"entries",
")",
"[",
"i",
"]",
"=",
"entry",
"\n",
"return",
"oldEntry",
"\n",
"}",
"\n\n",
"(",
"*",
"entries",
")",
"=",
"append",
"(",
"*",
"entries",
",",
"nil",
")",
"\n",
"copy",
"(",
"(",
"*",
"entries",
")",
"[",
"i",
"+",
"1",
":",
"]",
",",
"(",
"*",
"entries",
")",
"[",
"i",
":",
"]",
")",
"\n",
"(",
"*",
"entries",
")",
"[",
"i",
"]",
"=",
"entry",
"\n",
"return",
"nil",
"\n",
"}"
] |
// insert will insert the provided entry into this list of
// entries. Returned is an entry if an entry already exists
// for the provided key. If nothing is overwritten, Entry
// will be nil.
|
[
"insert",
"will",
"insert",
"the",
"provided",
"entry",
"into",
"this",
"list",
"of",
"entries",
".",
"Returned",
"is",
"an",
"entry",
"if",
"an",
"entry",
"already",
"exists",
"for",
"the",
"provided",
"key",
".",
"If",
"nothing",
"is",
"overwritten",
"Entry",
"will",
"be",
"nil",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/entries.go#L53-L71
|
train
|
Workiva/go-datastructures
|
trie/yfast/entries.go
|
delete
|
func (entries *Entries) delete(key uint64) Entry {
i := entries.search(key)
if i == len(*entries) { // key not found
return nil
}
if (*entries)[i].Key() != key {
return nil
}
oldEntry := (*entries)[i]
copy((*entries)[i:], (*entries)[i+1:])
(*entries)[len(*entries)-1] = nil // GC
*entries = (*entries)[:len(*entries)-1]
return oldEntry
}
|
go
|
func (entries *Entries) delete(key uint64) Entry {
i := entries.search(key)
if i == len(*entries) { // key not found
return nil
}
if (*entries)[i].Key() != key {
return nil
}
oldEntry := (*entries)[i]
copy((*entries)[i:], (*entries)[i+1:])
(*entries)[len(*entries)-1] = nil // GC
*entries = (*entries)[:len(*entries)-1]
return oldEntry
}
|
[
"func",
"(",
"entries",
"*",
"Entries",
")",
"delete",
"(",
"key",
"uint64",
")",
"Entry",
"{",
"i",
":=",
"entries",
".",
"search",
"(",
"key",
")",
"\n",
"if",
"i",
"==",
"len",
"(",
"*",
"entries",
")",
"{",
"// key not found",
"return",
"nil",
"\n",
"}",
"\n\n",
"if",
"(",
"*",
"entries",
")",
"[",
"i",
"]",
".",
"Key",
"(",
")",
"!=",
"key",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"oldEntry",
":=",
"(",
"*",
"entries",
")",
"[",
"i",
"]",
"\n",
"copy",
"(",
"(",
"*",
"entries",
")",
"[",
"i",
":",
"]",
",",
"(",
"*",
"entries",
")",
"[",
"i",
"+",
"1",
":",
"]",
")",
"\n",
"(",
"*",
"entries",
")",
"[",
"len",
"(",
"*",
"entries",
")",
"-",
"1",
"]",
"=",
"nil",
"// GC",
"\n",
"*",
"entries",
"=",
"(",
"*",
"entries",
")",
"[",
":",
"len",
"(",
"*",
"entries",
")",
"-",
"1",
"]",
"\n",
"return",
"oldEntry",
"\n",
"}"
] |
// delete will remove the provided key from this list of entries.
// Returned is a deleted Entry. This will be nil if the key
// cannot be found.
|
[
"delete",
"will",
"remove",
"the",
"provided",
"key",
"from",
"this",
"list",
"of",
"entries",
".",
"Returned",
"is",
"a",
"deleted",
"Entry",
".",
"This",
"will",
"be",
"nil",
"if",
"the",
"key",
"cannot",
"be",
"found",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/entries.go#L76-L91
|
train
|
Workiva/go-datastructures
|
trie/yfast/entries.go
|
max
|
func (entries Entries) max() (uint64, bool) {
if len(entries) == 0 {
return 0, false
}
return entries[len(entries)-1].Key(), true
}
|
go
|
func (entries Entries) max() (uint64, bool) {
if len(entries) == 0 {
return 0, false
}
return entries[len(entries)-1].Key(), true
}
|
[
"func",
"(",
"entries",
"Entries",
")",
"max",
"(",
")",
"(",
"uint64",
",",
"bool",
")",
"{",
"if",
"len",
"(",
"entries",
")",
"==",
"0",
"{",
"return",
"0",
",",
"false",
"\n",
"}",
"\n\n",
"return",
"entries",
"[",
"len",
"(",
"entries",
")",
"-",
"1",
"]",
".",
"Key",
"(",
")",
",",
"true",
"\n",
"}"
] |
// max returns the value of the highest key in this list
// of entries. The bool indicates if it's a valid key, that
// is if there is more than zero entries in this list.
|
[
"max",
"returns",
"the",
"value",
"of",
"the",
"highest",
"key",
"in",
"this",
"list",
"of",
"entries",
".",
"The",
"bool",
"indicates",
"if",
"it",
"s",
"a",
"valid",
"key",
"that",
"is",
"if",
"there",
"is",
"more",
"than",
"zero",
"entries",
"in",
"this",
"list",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/entries.go#L96-L102
|
train
|
Workiva/go-datastructures
|
trie/yfast/entries.go
|
get
|
func (entries Entries) get(key uint64) Entry {
i := entries.search(key)
if i == len(entries) {
return nil
}
if entries[i].Key() == key {
return entries[i]
}
return nil
}
|
go
|
func (entries Entries) get(key uint64) Entry {
i := entries.search(key)
if i == len(entries) {
return nil
}
if entries[i].Key() == key {
return entries[i]
}
return nil
}
|
[
"func",
"(",
"entries",
"Entries",
")",
"get",
"(",
"key",
"uint64",
")",
"Entry",
"{",
"i",
":=",
"entries",
".",
"search",
"(",
"key",
")",
"\n",
"if",
"i",
"==",
"len",
"(",
"entries",
")",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"if",
"entries",
"[",
"i",
"]",
".",
"Key",
"(",
")",
"==",
"key",
"{",
"return",
"entries",
"[",
"i",
"]",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// get will perform a lookup over this list of entries
// and return an Entry if it exists. Returns nil if the
// entry does not exist.
|
[
"get",
"will",
"perform",
"a",
"lookup",
"over",
"this",
"list",
"of",
"entries",
"and",
"return",
"an",
"Entry",
"if",
"it",
"exists",
".",
"Returns",
"nil",
"if",
"the",
"entry",
"does",
"not",
"exist",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/entries.go#L107-L118
|
train
|
Workiva/go-datastructures
|
trie/yfast/entries.go
|
successor
|
func (entries Entries) successor(key uint64) (Entry, int) {
i := entries.search(key)
if i == len(entries) {
return nil, -1
}
return entries[i], i
}
|
go
|
func (entries Entries) successor(key uint64) (Entry, int) {
i := entries.search(key)
if i == len(entries) {
return nil, -1
}
return entries[i], i
}
|
[
"func",
"(",
"entries",
"Entries",
")",
"successor",
"(",
"key",
"uint64",
")",
"(",
"Entry",
",",
"int",
")",
"{",
"i",
":=",
"entries",
".",
"search",
"(",
"key",
")",
"\n",
"if",
"i",
"==",
"len",
"(",
"entries",
")",
"{",
"return",
"nil",
",",
"-",
"1",
"\n",
"}",
"\n\n",
"return",
"entries",
"[",
"i",
"]",
",",
"i",
"\n",
"}"
] |
// successor will return the first entry that has a key
// greater than or equal to provided key. Also returned
// is the index of the find. Returns nil, -1 if a successor does
// not exist.
|
[
"successor",
"will",
"return",
"the",
"first",
"entry",
"that",
"has",
"a",
"key",
"greater",
"than",
"or",
"equal",
"to",
"provided",
"key",
".",
"Also",
"returned",
"is",
"the",
"index",
"of",
"the",
"find",
".",
"Returns",
"nil",
"-",
"1",
"if",
"a",
"successor",
"does",
"not",
"exist",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/entries.go#L124-L131
|
train
|
Workiva/go-datastructures
|
trie/yfast/entries.go
|
predecessor
|
func (entries Entries) predecessor(key uint64) (Entry, int) {
if len(entries) == 0 {
return nil, -1
}
i := entries.search(key)
if i == len(entries) {
return entries[i-1], i - 1
}
if entries[i].Key() == key {
return entries[i], i
}
i--
if i < 0 {
return nil, -1
}
return entries[i], i
}
|
go
|
func (entries Entries) predecessor(key uint64) (Entry, int) {
if len(entries) == 0 {
return nil, -1
}
i := entries.search(key)
if i == len(entries) {
return entries[i-1], i - 1
}
if entries[i].Key() == key {
return entries[i], i
}
i--
if i < 0 {
return nil, -1
}
return entries[i], i
}
|
[
"func",
"(",
"entries",
"Entries",
")",
"predecessor",
"(",
"key",
"uint64",
")",
"(",
"Entry",
",",
"int",
")",
"{",
"if",
"len",
"(",
"entries",
")",
"==",
"0",
"{",
"return",
"nil",
",",
"-",
"1",
"\n",
"}",
"\n\n",
"i",
":=",
"entries",
".",
"search",
"(",
"key",
")",
"\n",
"if",
"i",
"==",
"len",
"(",
"entries",
")",
"{",
"return",
"entries",
"[",
"i",
"-",
"1",
"]",
",",
"i",
"-",
"1",
"\n",
"}",
"\n\n",
"if",
"entries",
"[",
"i",
"]",
".",
"Key",
"(",
")",
"==",
"key",
"{",
"return",
"entries",
"[",
"i",
"]",
",",
"i",
"\n",
"}",
"\n\n",
"i",
"--",
"\n\n",
"if",
"i",
"<",
"0",
"{",
"return",
"nil",
",",
"-",
"1",
"\n",
"}",
"\n\n",
"return",
"entries",
"[",
"i",
"]",
",",
"i",
"\n",
"}"
] |
// predecessor will return the first entry that has a key
// less than or equal to the provided key. Also returned
// is the index of the find. Returns nil, -1 if a predecessor
// does not exist.
|
[
"predecessor",
"will",
"return",
"the",
"first",
"entry",
"that",
"has",
"a",
"key",
"less",
"than",
"or",
"equal",
"to",
"the",
"provided",
"key",
".",
"Also",
"returned",
"is",
"the",
"index",
"of",
"the",
"find",
".",
"Returns",
"nil",
"-",
"1",
"if",
"a",
"predecessor",
"does",
"not",
"exist",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/yfast/entries.go#L137-L158
|
train
|
Workiva/go-datastructures
|
queue/priority_queue.go
|
Put
|
func (pq *PriorityQueue) Put(items ...Item) error {
if len(items) == 0 {
return nil
}
pq.lock.Lock()
defer pq.lock.Unlock()
if pq.disposed {
return ErrDisposed
}
for _, item := range items {
if pq.allowDuplicates {
pq.items.push(item)
} else if _, ok := pq.itemMap[item]; !ok {
pq.itemMap[item] = struct{}{}
pq.items.push(item)
}
}
for {
sema := pq.waiters.get()
if sema == nil {
break
}
sema.response.Add(1)
sema.ready <- true
sema.response.Wait()
if len(pq.items) == 0 {
break
}
}
return nil
}
|
go
|
func (pq *PriorityQueue) Put(items ...Item) error {
if len(items) == 0 {
return nil
}
pq.lock.Lock()
defer pq.lock.Unlock()
if pq.disposed {
return ErrDisposed
}
for _, item := range items {
if pq.allowDuplicates {
pq.items.push(item)
} else if _, ok := pq.itemMap[item]; !ok {
pq.itemMap[item] = struct{}{}
pq.items.push(item)
}
}
for {
sema := pq.waiters.get()
if sema == nil {
break
}
sema.response.Add(1)
sema.ready <- true
sema.response.Wait()
if len(pq.items) == 0 {
break
}
}
return nil
}
|
[
"func",
"(",
"pq",
"*",
"PriorityQueue",
")",
"Put",
"(",
"items",
"...",
"Item",
")",
"error",
"{",
"if",
"len",
"(",
"items",
")",
"==",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"pq",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"pq",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"pq",
".",
"disposed",
"{",
"return",
"ErrDisposed",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"item",
":=",
"range",
"items",
"{",
"if",
"pq",
".",
"allowDuplicates",
"{",
"pq",
".",
"items",
".",
"push",
"(",
"item",
")",
"\n",
"}",
"else",
"if",
"_",
",",
"ok",
":=",
"pq",
".",
"itemMap",
"[",
"item",
"]",
";",
"!",
"ok",
"{",
"pq",
".",
"itemMap",
"[",
"item",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"pq",
".",
"items",
".",
"push",
"(",
"item",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"for",
"{",
"sema",
":=",
"pq",
".",
"waiters",
".",
"get",
"(",
")",
"\n",
"if",
"sema",
"==",
"nil",
"{",
"break",
"\n",
"}",
"\n\n",
"sema",
".",
"response",
".",
"Add",
"(",
"1",
")",
"\n",
"sema",
".",
"ready",
"<-",
"true",
"\n",
"sema",
".",
"response",
".",
"Wait",
"(",
")",
"\n",
"if",
"len",
"(",
"pq",
".",
"items",
")",
"==",
"0",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// Put adds items to the queue.
|
[
"Put",
"adds",
"items",
"to",
"the",
"queue",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/priority_queue.go#L118-L154
|
train
|
Workiva/go-datastructures
|
queue/priority_queue.go
|
Get
|
func (pq *PriorityQueue) Get(number int) ([]Item, error) {
if number < 1 {
return nil, nil
}
pq.lock.Lock()
if pq.disposed {
pq.lock.Unlock()
return nil, ErrDisposed
}
var items []Item
// Remove references to popped items.
deleteItems := func(items []Item) {
for _, item := range items {
delete(pq.itemMap, item)
}
}
if len(pq.items) == 0 {
sema := newSema()
pq.waiters.put(sema)
pq.lock.Unlock()
<-sema.ready
if pq.Disposed() {
return nil, ErrDisposed
}
items = pq.items.get(number)
if !pq.allowDuplicates {
deleteItems(items)
}
sema.response.Done()
return items, nil
}
items = pq.items.get(number)
deleteItems(items)
pq.lock.Unlock()
return items, nil
}
|
go
|
func (pq *PriorityQueue) Get(number int) ([]Item, error) {
if number < 1 {
return nil, nil
}
pq.lock.Lock()
if pq.disposed {
pq.lock.Unlock()
return nil, ErrDisposed
}
var items []Item
// Remove references to popped items.
deleteItems := func(items []Item) {
for _, item := range items {
delete(pq.itemMap, item)
}
}
if len(pq.items) == 0 {
sema := newSema()
pq.waiters.put(sema)
pq.lock.Unlock()
<-sema.ready
if pq.Disposed() {
return nil, ErrDisposed
}
items = pq.items.get(number)
if !pq.allowDuplicates {
deleteItems(items)
}
sema.response.Done()
return items, nil
}
items = pq.items.get(number)
deleteItems(items)
pq.lock.Unlock()
return items, nil
}
|
[
"func",
"(",
"pq",
"*",
"PriorityQueue",
")",
"Get",
"(",
"number",
"int",
")",
"(",
"[",
"]",
"Item",
",",
"error",
")",
"{",
"if",
"number",
"<",
"1",
"{",
"return",
"nil",
",",
"nil",
"\n",
"}",
"\n\n",
"pq",
".",
"lock",
".",
"Lock",
"(",
")",
"\n\n",
"if",
"pq",
".",
"disposed",
"{",
"pq",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"nil",
",",
"ErrDisposed",
"\n",
"}",
"\n\n",
"var",
"items",
"[",
"]",
"Item",
"\n\n",
"// Remove references to popped items.",
"deleteItems",
":=",
"func",
"(",
"items",
"[",
"]",
"Item",
")",
"{",
"for",
"_",
",",
"item",
":=",
"range",
"items",
"{",
"delete",
"(",
"pq",
".",
"itemMap",
",",
"item",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"pq",
".",
"items",
")",
"==",
"0",
"{",
"sema",
":=",
"newSema",
"(",
")",
"\n",
"pq",
".",
"waiters",
".",
"put",
"(",
"sema",
")",
"\n",
"pq",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"<-",
"sema",
".",
"ready",
"\n\n",
"if",
"pq",
".",
"Disposed",
"(",
")",
"{",
"return",
"nil",
",",
"ErrDisposed",
"\n",
"}",
"\n\n",
"items",
"=",
"pq",
".",
"items",
".",
"get",
"(",
"number",
")",
"\n",
"if",
"!",
"pq",
".",
"allowDuplicates",
"{",
"deleteItems",
"(",
"items",
")",
"\n",
"}",
"\n",
"sema",
".",
"response",
".",
"Done",
"(",
")",
"\n",
"return",
"items",
",",
"nil",
"\n",
"}",
"\n\n",
"items",
"=",
"pq",
".",
"items",
".",
"get",
"(",
"number",
")",
"\n",
"deleteItems",
"(",
"items",
")",
"\n",
"pq",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"items",
",",
"nil",
"\n",
"}"
] |
// Get retrieves items from the queue. If the queue is empty,
// this call blocks until the next item is added to the queue. This
// will attempt to retrieve number of items.
|
[
"Get",
"retrieves",
"items",
"from",
"the",
"queue",
".",
"If",
"the",
"queue",
"is",
"empty",
"this",
"call",
"blocks",
"until",
"the",
"next",
"item",
"is",
"added",
"to",
"the",
"queue",
".",
"This",
"will",
"attempt",
"to",
"retrieve",
"number",
"of",
"items",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/priority_queue.go#L159-L203
|
train
|
Workiva/go-datastructures
|
queue/priority_queue.go
|
Peek
|
func (pq *PriorityQueue) Peek() Item {
pq.lock.Lock()
defer pq.lock.Unlock()
if len(pq.items) > 0 {
return pq.items[0]
}
return nil
}
|
go
|
func (pq *PriorityQueue) Peek() Item {
pq.lock.Lock()
defer pq.lock.Unlock()
if len(pq.items) > 0 {
return pq.items[0]
}
return nil
}
|
[
"func",
"(",
"pq",
"*",
"PriorityQueue",
")",
"Peek",
"(",
")",
"Item",
"{",
"pq",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"pq",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"if",
"len",
"(",
"pq",
".",
"items",
")",
">",
"0",
"{",
"return",
"pq",
".",
"items",
"[",
"0",
"]",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Peek will look at the next item without removing it from the queue.
|
[
"Peek",
"will",
"look",
"at",
"the",
"next",
"item",
"without",
"removing",
"it",
"from",
"the",
"queue",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/priority_queue.go#L206-L213
|
train
|
Workiva/go-datastructures
|
queue/priority_queue.go
|
Empty
|
func (pq *PriorityQueue) Empty() bool {
pq.lock.Lock()
defer pq.lock.Unlock()
return len(pq.items) == 0
}
|
go
|
func (pq *PriorityQueue) Empty() bool {
pq.lock.Lock()
defer pq.lock.Unlock()
return len(pq.items) == 0
}
|
[
"func",
"(",
"pq",
"*",
"PriorityQueue",
")",
"Empty",
"(",
")",
"bool",
"{",
"pq",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"pq",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"return",
"len",
"(",
"pq",
".",
"items",
")",
"==",
"0",
"\n",
"}"
] |
// Empty returns a bool indicating if there are any items left
// in the queue.
|
[
"Empty",
"returns",
"a",
"bool",
"indicating",
"if",
"there",
"are",
"any",
"items",
"left",
"in",
"the",
"queue",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/priority_queue.go#L217-L222
|
train
|
Workiva/go-datastructures
|
queue/priority_queue.go
|
Len
|
func (pq *PriorityQueue) Len() int {
pq.lock.Lock()
defer pq.lock.Unlock()
return len(pq.items)
}
|
go
|
func (pq *PriorityQueue) Len() int {
pq.lock.Lock()
defer pq.lock.Unlock()
return len(pq.items)
}
|
[
"func",
"(",
"pq",
"*",
"PriorityQueue",
")",
"Len",
"(",
")",
"int",
"{",
"pq",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"pq",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"return",
"len",
"(",
"pq",
".",
"items",
")",
"\n",
"}"
] |
// Len returns a number indicating how many items are in the queue.
|
[
"Len",
"returns",
"a",
"number",
"indicating",
"how",
"many",
"items",
"are",
"in",
"the",
"queue",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/priority_queue.go#L225-L230
|
train
|
Workiva/go-datastructures
|
queue/priority_queue.go
|
Disposed
|
func (pq *PriorityQueue) Disposed() bool {
pq.disposeLock.Lock()
defer pq.disposeLock.Unlock()
return pq.disposed
}
|
go
|
func (pq *PriorityQueue) Disposed() bool {
pq.disposeLock.Lock()
defer pq.disposeLock.Unlock()
return pq.disposed
}
|
[
"func",
"(",
"pq",
"*",
"PriorityQueue",
")",
"Disposed",
"(",
")",
"bool",
"{",
"pq",
".",
"disposeLock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"pq",
".",
"disposeLock",
".",
"Unlock",
"(",
")",
"\n\n",
"return",
"pq",
".",
"disposed",
"\n",
"}"
] |
// Disposed returns a bool indicating if this queue has been disposed.
|
[
"Disposed",
"returns",
"a",
"bool",
"indicating",
"if",
"this",
"queue",
"has",
"been",
"disposed",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/priority_queue.go#L233-L238
|
train
|
Workiva/go-datastructures
|
queue/priority_queue.go
|
NewPriorityQueue
|
func NewPriorityQueue(hint int, allowDuplicates bool) *PriorityQueue {
return &PriorityQueue{
items: make(priorityItems, 0, hint),
itemMap: make(map[Item]struct{}, hint),
allowDuplicates: allowDuplicates,
}
}
|
go
|
func NewPriorityQueue(hint int, allowDuplicates bool) *PriorityQueue {
return &PriorityQueue{
items: make(priorityItems, 0, hint),
itemMap: make(map[Item]struct{}, hint),
allowDuplicates: allowDuplicates,
}
}
|
[
"func",
"NewPriorityQueue",
"(",
"hint",
"int",
",",
"allowDuplicates",
"bool",
")",
"*",
"PriorityQueue",
"{",
"return",
"&",
"PriorityQueue",
"{",
"items",
":",
"make",
"(",
"priorityItems",
",",
"0",
",",
"hint",
")",
",",
"itemMap",
":",
"make",
"(",
"map",
"[",
"Item",
"]",
"struct",
"{",
"}",
",",
"hint",
")",
",",
"allowDuplicates",
":",
"allowDuplicates",
",",
"}",
"\n",
"}"
] |
// NewPriorityQueue is the constructor for a priority queue.
|
[
"NewPriorityQueue",
"is",
"the",
"constructor",
"for",
"a",
"priority",
"queue",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/priority_queue.go#L260-L266
|
train
|
Workiva/go-datastructures
|
btree/immutable/rt.go
|
contextOrCachedNode
|
func (t *Tr) contextOrCachedNode(id ID, cache bool) (*Node, error) {
if t.context != nil {
n := t.context.getNode(id)
if n != nil {
return n, nil
}
}
return t.cacher.getNode(t, id, cache)
}
|
go
|
func (t *Tr) contextOrCachedNode(id ID, cache bool) (*Node, error) {
if t.context != nil {
n := t.context.getNode(id)
if n != nil {
return n, nil
}
}
return t.cacher.getNode(t, id, cache)
}
|
[
"func",
"(",
"t",
"*",
"Tr",
")",
"contextOrCachedNode",
"(",
"id",
"ID",
",",
"cache",
"bool",
")",
"(",
"*",
"Node",
",",
"error",
")",
"{",
"if",
"t",
".",
"context",
"!=",
"nil",
"{",
"n",
":=",
"t",
".",
"context",
".",
"getNode",
"(",
"id",
")",
"\n",
"if",
"n",
"!=",
"nil",
"{",
"return",
"n",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"t",
".",
"cacher",
".",
"getNode",
"(",
"t",
",",
"id",
",",
"cache",
")",
"\n",
"}"
] |
// contextOrCachedNode is a convenience function for either fetching
// a node from the context or persistence.
|
[
"contextOrCachedNode",
"is",
"a",
"convenience",
"function",
"for",
"either",
"fetching",
"a",
"node",
"from",
"the",
"context",
"or",
"persistence",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/rt.go#L80-L89
|
train
|
Workiva/go-datastructures
|
btree/immutable/rt.go
|
toBytes
|
func (t *Tr) toBytes() []byte {
buf, err := t.MarshalMsg(nil)
if err != nil {
panic(`unable to encode tree`)
}
return buf
}
|
go
|
func (t *Tr) toBytes() []byte {
buf, err := t.MarshalMsg(nil)
if err != nil {
panic(`unable to encode tree`)
}
return buf
}
|
[
"func",
"(",
"t",
"*",
"Tr",
")",
"toBytes",
"(",
")",
"[",
"]",
"byte",
"{",
"buf",
",",
"err",
":=",
"t",
".",
"MarshalMsg",
"(",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"`unable to encode tree`",
")",
"\n",
"}",
"\n\n",
"return",
"buf",
"\n",
"}"
] |
// toBytes encodes this tree into a byte array. Panics if unable
// as this error has to be fixed in code.
|
[
"toBytes",
"encodes",
"this",
"tree",
"into",
"a",
"byte",
"array",
".",
"Panics",
"if",
"unable",
"as",
"this",
"error",
"has",
"to",
"be",
"fixed",
"in",
"code",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/rt.go#L97-L104
|
train
|
Workiva/go-datastructures
|
btree/immutable/rt.go
|
commit
|
func (t *Tr) commit() []*Payload {
items := make([]*Payload, 0, len(t.context.seenNodes))
for _, n := range t.context.seenNodes {
n.ChildValues, n.ChildKeys = n.flatten()
buf, err := n.MarshalMsg(nil)
if err != nil {
panic(`unable to encode node`)
}
n.ChildValues, n.ChildKeys = nil, nil
item := &Payload{n.ID, buf}
items = append(items, item)
}
return items
}
|
go
|
func (t *Tr) commit() []*Payload {
items := make([]*Payload, 0, len(t.context.seenNodes))
for _, n := range t.context.seenNodes {
n.ChildValues, n.ChildKeys = n.flatten()
buf, err := n.MarshalMsg(nil)
if err != nil {
panic(`unable to encode node`)
}
n.ChildValues, n.ChildKeys = nil, nil
item := &Payload{n.ID, buf}
items = append(items, item)
}
return items
}
|
[
"func",
"(",
"t",
"*",
"Tr",
")",
"commit",
"(",
")",
"[",
"]",
"*",
"Payload",
"{",
"items",
":=",
"make",
"(",
"[",
"]",
"*",
"Payload",
",",
"0",
",",
"len",
"(",
"t",
".",
"context",
".",
"seenNodes",
")",
")",
"\n",
"for",
"_",
",",
"n",
":=",
"range",
"t",
".",
"context",
".",
"seenNodes",
"{",
"n",
".",
"ChildValues",
",",
"n",
".",
"ChildKeys",
"=",
"n",
".",
"flatten",
"(",
")",
"\n",
"buf",
",",
"err",
":=",
"n",
".",
"MarshalMsg",
"(",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"`unable to encode node`",
")",
"\n",
"}",
"\n\n",
"n",
".",
"ChildValues",
",",
"n",
".",
"ChildKeys",
"=",
"nil",
",",
"nil",
"\n",
"item",
":=",
"&",
"Payload",
"{",
"n",
".",
"ID",
",",
"buf",
"}",
"\n",
"items",
"=",
"append",
"(",
"items",
",",
"item",
")",
"\n",
"}",
"\n\n",
"return",
"items",
"\n",
"}"
] |
// commit will gather up all created nodes and serialize them into
// items that can be persisted.
|
[
"commit",
"will",
"gather",
"up",
"all",
"created",
"nodes",
"and",
"serialize",
"them",
"into",
"items",
"that",
"can",
"be",
"persisted",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/rt.go#L114-L129
|
train
|
Workiva/go-datastructures
|
btree/immutable/rt.go
|
Load
|
func Load(p Persister, id []byte, comparator Comparator) (ReadableTree, error) {
items, err := p.Load(id)
if err != nil {
return nil, err
}
if len(items) == 0 || items[0] == nil {
return nil, ErrTreeNotFound
}
rt, err := treeFromBytes(p, items[0].Payload, comparator)
if err != nil {
return nil, err
}
return rt, nil
}
|
go
|
func Load(p Persister, id []byte, comparator Comparator) (ReadableTree, error) {
items, err := p.Load(id)
if err != nil {
return nil, err
}
if len(items) == 0 || items[0] == nil {
return nil, ErrTreeNotFound
}
rt, err := treeFromBytes(p, items[0].Payload, comparator)
if err != nil {
return nil, err
}
return rt, nil
}
|
[
"func",
"Load",
"(",
"p",
"Persister",
",",
"id",
"[",
"]",
"byte",
",",
"comparator",
"Comparator",
")",
"(",
"ReadableTree",
",",
"error",
")",
"{",
"items",
",",
"err",
":=",
"p",
".",
"Load",
"(",
"id",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"items",
")",
"==",
"0",
"||",
"items",
"[",
"0",
"]",
"==",
"nil",
"{",
"return",
"nil",
",",
"ErrTreeNotFound",
"\n",
"}",
"\n\n",
"rt",
",",
"err",
":=",
"treeFromBytes",
"(",
"p",
",",
"items",
"[",
"0",
"]",
".",
"Payload",
",",
"comparator",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"rt",
",",
"nil",
"\n",
"}"
] |
// Load returns a ReadableTree from persistence. The provided
// config should contain a persister that can be used for this purpose.
// An error is returned if the tree could not be found or an error
// occurred in the persistence layer.
|
[
"Load",
"returns",
"a",
"ReadableTree",
"from",
"persistence",
".",
"The",
"provided",
"config",
"should",
"contain",
"a",
"persister",
"that",
"can",
"be",
"used",
"for",
"this",
"purpose",
".",
"An",
"error",
"is",
"returned",
"if",
"the",
"tree",
"could",
"not",
"be",
"found",
"or",
"an",
"error",
"occurred",
"in",
"the",
"persistence",
"layer",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/rt.go#L210-L226
|
train
|
Workiva/go-datastructures
|
bitarray/bitmap.go
|
PopCount
|
func (b Bitmap32) PopCount() int {
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
b -= (b >> 1) & 0x55555555
b = (b>>2)&0x33333333 + b&0x33333333
b += b >> 4
b &= 0x0f0f0f0f
b *= 0x01010101
return int(byte(b >> 24))
}
|
go
|
func (b Bitmap32) PopCount() int {
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
b -= (b >> 1) & 0x55555555
b = (b>>2)&0x33333333 + b&0x33333333
b += b >> 4
b &= 0x0f0f0f0f
b *= 0x01010101
return int(byte(b >> 24))
}
|
[
"func",
"(",
"b",
"Bitmap32",
")",
"PopCount",
"(",
")",
"int",
"{",
"// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel",
"b",
"-=",
"(",
"b",
">>",
"1",
")",
"&",
"0x55555555",
"\n",
"b",
"=",
"(",
"b",
">>",
"2",
")",
"&",
"0x33333333",
"+",
"b",
"&",
"0x33333333",
"\n",
"b",
"+=",
"b",
">>",
"4",
"\n",
"b",
"&=",
"0x0f0f0f0f",
"\n",
"b",
"*=",
"0x01010101",
"\n",
"return",
"int",
"(",
"byte",
"(",
"b",
">>",
"24",
")",
")",
"\n",
"}"
] |
// PopCount returns the amount of bits set to 1 in the Bitmap32
|
[
"PopCount",
"returns",
"the",
"amount",
"of",
"bits",
"set",
"to",
"1",
"in",
"the",
"Bitmap32"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitmap.go#L50-L58
|
train
|
Workiva/go-datastructures
|
bitarray/bitmap.go
|
PopCount
|
func (b Bitmap64) PopCount() int {
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
b -= (b >> 1) & 0x5555555555555555
b = (b>>2)&0x3333333333333333 + b&0x3333333333333333
b += b >> 4
b &= 0x0f0f0f0f0f0f0f0f
b *= 0x0101010101010101
return int(byte(b >> 56))
}
|
go
|
func (b Bitmap64) PopCount() int {
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
b -= (b >> 1) & 0x5555555555555555
b = (b>>2)&0x3333333333333333 + b&0x3333333333333333
b += b >> 4
b &= 0x0f0f0f0f0f0f0f0f
b *= 0x0101010101010101
return int(byte(b >> 56))
}
|
[
"func",
"(",
"b",
"Bitmap64",
")",
"PopCount",
"(",
")",
"int",
"{",
"// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel",
"b",
"-=",
"(",
"b",
">>",
"1",
")",
"&",
"0x5555555555555555",
"\n",
"b",
"=",
"(",
"b",
">>",
"2",
")",
"&",
"0x3333333333333333",
"+",
"b",
"&",
"0x3333333333333333",
"\n",
"b",
"+=",
"b",
">>",
"4",
"\n",
"b",
"&=",
"0x0f0f0f0f0f0f0f0f",
"\n",
"b",
"*=",
"0x0101010101010101",
"\n",
"return",
"int",
"(",
"byte",
"(",
"b",
">>",
"56",
")",
")",
"\n",
"}"
] |
// PopCount returns the amount of bits set to 1 in the Bitmap64
|
[
"PopCount",
"returns",
"the",
"amount",
"of",
"bits",
"set",
"to",
"1",
"in",
"the",
"Bitmap64"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitmap.go#L79-L87
|
train
|
Workiva/go-datastructures
|
btree/immutable/cacher.go
|
clear
|
func (c *cacher) clear() {
c.lock.Lock()
defer c.lock.Unlock()
c.cache = make(map[string]*futures.Future, 10)
}
|
go
|
func (c *cacher) clear() {
c.lock.Lock()
defer c.lock.Unlock()
c.cache = make(map[string]*futures.Future, 10)
}
|
[
"func",
"(",
"c",
"*",
"cacher",
")",
"clear",
"(",
")",
"{",
"c",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"c",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"c",
".",
"cache",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"futures",
".",
"Future",
",",
"10",
")",
"\n",
"}"
] |
// clear deletes all items from the cache.
|
[
"clear",
"deletes",
"all",
"items",
"from",
"the",
"cache",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/cacher.go#L54-L59
|
train
|
Workiva/go-datastructures
|
btree/immutable/cacher.go
|
deleteFromCache
|
func (c *cacher) deleteFromCache(id ID) {
c.lock.Lock()
defer c.lock.Unlock()
delete(c.cache, string(id))
}
|
go
|
func (c *cacher) deleteFromCache(id ID) {
c.lock.Lock()
defer c.lock.Unlock()
delete(c.cache, string(id))
}
|
[
"func",
"(",
"c",
"*",
"cacher",
")",
"deleteFromCache",
"(",
"id",
"ID",
")",
"{",
"c",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"c",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"delete",
"(",
"c",
".",
"cache",
",",
"string",
"(",
"id",
")",
")",
"\n",
"}"
] |
// deleteFromCache will remove the provided ID from the cache. This
// is a threadsafe operation.
|
[
"deleteFromCache",
"will",
"remove",
"the",
"provided",
"ID",
"from",
"the",
"cache",
".",
"This",
"is",
"a",
"threadsafe",
"operation",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/cacher.go#L63-L68
|
train
|
Workiva/go-datastructures
|
btree/immutable/cacher.go
|
getNode
|
func (c *cacher) getNode(t *Tr, key ID, useCache bool) (*Node, error) {
if !useCache {
return c.loadNode(t, key)
}
c.lock.Lock()
future, ok := c.cache[string(key)]
if ok {
c.lock.Unlock()
ifc, err := future.GetResult()
if err != nil {
return nil, err
}
return ifc.(*Node), nil
}
completer := make(chan interface{}, 1)
future = futures.New(completer, 30*time.Second)
c.cache[string(key)] = future
c.lock.Unlock()
go c.asyncLoadNode(t, key, completer)
ifc, err := future.GetResult()
if err != nil {
c.deleteFromCache(key)
return nil, err
}
if err, ok := ifc.(error); ok {
c.deleteFromCache(key)
return nil, err
}
return ifc.(*Node), nil
}
|
go
|
func (c *cacher) getNode(t *Tr, key ID, useCache bool) (*Node, error) {
if !useCache {
return c.loadNode(t, key)
}
c.lock.Lock()
future, ok := c.cache[string(key)]
if ok {
c.lock.Unlock()
ifc, err := future.GetResult()
if err != nil {
return nil, err
}
return ifc.(*Node), nil
}
completer := make(chan interface{}, 1)
future = futures.New(completer, 30*time.Second)
c.cache[string(key)] = future
c.lock.Unlock()
go c.asyncLoadNode(t, key, completer)
ifc, err := future.GetResult()
if err != nil {
c.deleteFromCache(key)
return nil, err
}
if err, ok := ifc.(error); ok {
c.deleteFromCache(key)
return nil, err
}
return ifc.(*Node), nil
}
|
[
"func",
"(",
"c",
"*",
"cacher",
")",
"getNode",
"(",
"t",
"*",
"Tr",
",",
"key",
"ID",
",",
"useCache",
"bool",
")",
"(",
"*",
"Node",
",",
"error",
")",
"{",
"if",
"!",
"useCache",
"{",
"return",
"c",
".",
"loadNode",
"(",
"t",
",",
"key",
")",
"\n",
"}",
"\n\n",
"c",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"future",
",",
"ok",
":=",
"c",
".",
"cache",
"[",
"string",
"(",
"key",
")",
"]",
"\n",
"if",
"ok",
"{",
"c",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"ifc",
",",
"err",
":=",
"future",
".",
"GetResult",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"ifc",
".",
"(",
"*",
"Node",
")",
",",
"nil",
"\n",
"}",
"\n\n",
"completer",
":=",
"make",
"(",
"chan",
"interface",
"{",
"}",
",",
"1",
")",
"\n",
"future",
"=",
"futures",
".",
"New",
"(",
"completer",
",",
"30",
"*",
"time",
".",
"Second",
")",
"\n",
"c",
".",
"cache",
"[",
"string",
"(",
"key",
")",
"]",
"=",
"future",
"\n",
"c",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"go",
"c",
".",
"asyncLoadNode",
"(",
"t",
",",
"key",
",",
"completer",
")",
"\n\n",
"ifc",
",",
"err",
":=",
"future",
".",
"GetResult",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"c",
".",
"deleteFromCache",
"(",
"key",
")",
"\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"err",
",",
"ok",
":=",
"ifc",
".",
"(",
"error",
")",
";",
"ok",
"{",
"c",
".",
"deleteFromCache",
"(",
"key",
")",
"\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"ifc",
".",
"(",
"*",
"Node",
")",
",",
"nil",
"\n",
"}"
] |
// getNode will return a Node matching the provided id. An error is returned
// if the cacher could not go to the persister or the node could not be found.
// All found nodes are cached so subsequent calls should be faster than
// the initial. This blocks until the node is loaded, but is also threadsafe.
|
[
"getNode",
"will",
"return",
"a",
"Node",
"matching",
"the",
"provided",
"id",
".",
"An",
"error",
"is",
"returned",
"if",
"the",
"cacher",
"could",
"not",
"go",
"to",
"the",
"persister",
"or",
"the",
"node",
"could",
"not",
"be",
"found",
".",
"All",
"found",
"nodes",
"are",
"cached",
"so",
"subsequent",
"calls",
"should",
"be",
"faster",
"than",
"the",
"initial",
".",
"This",
"blocks",
"until",
"the",
"node",
"is",
"loaded",
"but",
"is",
"also",
"threadsafe",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/cacher.go#L88-L124
|
train
|
Workiva/go-datastructures
|
btree/immutable/cacher.go
|
newCacher
|
func newCacher(persister Persister) *cacher {
return &cacher{
persister: persister,
cache: make(map[string]*futures.Future, 10),
}
}
|
go
|
func newCacher(persister Persister) *cacher {
return &cacher{
persister: persister,
cache: make(map[string]*futures.Future, 10),
}
}
|
[
"func",
"newCacher",
"(",
"persister",
"Persister",
")",
"*",
"cacher",
"{",
"return",
"&",
"cacher",
"{",
"persister",
":",
"persister",
",",
"cache",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"futures",
".",
"Future",
",",
"10",
")",
",",
"}",
"\n",
"}"
] |
// newCacher is the constructor for a cacher that caches nodes for
// an indefinite period of time.
|
[
"newCacher",
"is",
"the",
"constructor",
"for",
"a",
"cacher",
"that",
"caches",
"nodes",
"for",
"an",
"indefinite",
"period",
"of",
"time",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/cacher.go#L128-L133
|
train
|
Workiva/go-datastructures
|
trie/dtrie/node.go
|
get
|
func get(n *node, keyHash uint32, key interface{}) Entry {
index := uint(mask(keyHash, n.level))
if n.dataMap.GetBit(index) {
return n.entries[index]
}
if n.nodeMap.GetBit(index) {
return get(n.entries[index].(*node), keyHash, key)
}
if n.level == 6 { // get from collisionNode
if n.entries[index] == nil {
return nil
}
cNode := n.entries[index].(*collisionNode)
for _, e := range cNode.entries {
if e.Key() == key {
return e
}
}
}
return nil
}
|
go
|
func get(n *node, keyHash uint32, key interface{}) Entry {
index := uint(mask(keyHash, n.level))
if n.dataMap.GetBit(index) {
return n.entries[index]
}
if n.nodeMap.GetBit(index) {
return get(n.entries[index].(*node), keyHash, key)
}
if n.level == 6 { // get from collisionNode
if n.entries[index] == nil {
return nil
}
cNode := n.entries[index].(*collisionNode)
for _, e := range cNode.entries {
if e.Key() == key {
return e
}
}
}
return nil
}
|
[
"func",
"get",
"(",
"n",
"*",
"node",
",",
"keyHash",
"uint32",
",",
"key",
"interface",
"{",
"}",
")",
"Entry",
"{",
"index",
":=",
"uint",
"(",
"mask",
"(",
"keyHash",
",",
"n",
".",
"level",
")",
")",
"\n",
"if",
"n",
".",
"dataMap",
".",
"GetBit",
"(",
"index",
")",
"{",
"return",
"n",
".",
"entries",
"[",
"index",
"]",
"\n",
"}",
"\n",
"if",
"n",
".",
"nodeMap",
".",
"GetBit",
"(",
"index",
")",
"{",
"return",
"get",
"(",
"n",
".",
"entries",
"[",
"index",
"]",
".",
"(",
"*",
"node",
")",
",",
"keyHash",
",",
"key",
")",
"\n",
"}",
"\n",
"if",
"n",
".",
"level",
"==",
"6",
"{",
"// get from collisionNode",
"if",
"n",
".",
"entries",
"[",
"index",
"]",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"cNode",
":=",
"n",
".",
"entries",
"[",
"index",
"]",
".",
"(",
"*",
"collisionNode",
")",
"\n",
"for",
"_",
",",
"e",
":=",
"range",
"cNode",
".",
"entries",
"{",
"if",
"e",
".",
"Key",
"(",
")",
"==",
"key",
"{",
"return",
"e",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// returns nil if not found
|
[
"returns",
"nil",
"if",
"not",
"found"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/dtrie/node.go#L128-L148
|
train
|
Subsets and Splits
SQL Console for semeru/code-text-go
Retrieves a limited set of code samples with their languages, with a specific case adjustment for 'Go' language.