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
|
bitarray/util.go
|
maxInt64
|
func maxInt64(ints ...int64) int64 {
maxInt := ints[0]
for i := 1; i < len(ints); i++ {
if ints[i] > maxInt {
maxInt = ints[i]
}
}
return maxInt
}
|
go
|
func maxInt64(ints ...int64) int64 {
maxInt := ints[0]
for i := 1; i < len(ints); i++ {
if ints[i] > maxInt {
maxInt = ints[i]
}
}
return maxInt
}
|
[
"func",
"maxInt64",
"(",
"ints",
"...",
"int64",
")",
"int64",
"{",
"maxInt",
":=",
"ints",
"[",
"0",
"]",
"\n",
"for",
"i",
":=",
"1",
";",
"i",
"<",
"len",
"(",
"ints",
")",
";",
"i",
"++",
"{",
"if",
"ints",
"[",
"i",
"]",
">",
"maxInt",
"{",
"maxInt",
"=",
"ints",
"[",
"i",
"]",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"maxInt",
"\n",
"}"
] |
// maxInt64 returns the highest integer in the provided list of int64s
|
[
"maxInt64",
"returns",
"the",
"highest",
"integer",
"in",
"the",
"provided",
"list",
"of",
"int64s"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/util.go#L20-L29
|
train
|
Workiva/go-datastructures
|
bitarray/util.go
|
maxUint64
|
func maxUint64(ints ...uint64) uint64 {
maxInt := ints[0]
for i := 1; i < len(ints); i++ {
if ints[i] > maxInt {
maxInt = ints[i]
}
}
return maxInt
}
|
go
|
func maxUint64(ints ...uint64) uint64 {
maxInt := ints[0]
for i := 1; i < len(ints); i++ {
if ints[i] > maxInt {
maxInt = ints[i]
}
}
return maxInt
}
|
[
"func",
"maxUint64",
"(",
"ints",
"...",
"uint64",
")",
"uint64",
"{",
"maxInt",
":=",
"ints",
"[",
"0",
"]",
"\n",
"for",
"i",
":=",
"1",
";",
"i",
"<",
"len",
"(",
"ints",
")",
";",
"i",
"++",
"{",
"if",
"ints",
"[",
"i",
"]",
">",
"maxInt",
"{",
"maxInt",
"=",
"ints",
"[",
"i",
"]",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"maxInt",
"\n",
"}"
] |
// maxUint64 returns the highest integer in the provided list of uint64s
|
[
"maxUint64",
"returns",
"the",
"highest",
"integer",
"in",
"the",
"provided",
"list",
"of",
"uint64s"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/util.go#L32-L41
|
train
|
Workiva/go-datastructures
|
bitarray/util.go
|
minUint64
|
func minUint64(ints ...uint64) uint64 {
minInt := ints[0]
for i := 1; i < len(ints); i++ {
if ints[i] < minInt {
minInt = ints[i]
}
}
return minInt
}
|
go
|
func minUint64(ints ...uint64) uint64 {
minInt := ints[0]
for i := 1; i < len(ints); i++ {
if ints[i] < minInt {
minInt = ints[i]
}
}
return minInt
}
|
[
"func",
"minUint64",
"(",
"ints",
"...",
"uint64",
")",
"uint64",
"{",
"minInt",
":=",
"ints",
"[",
"0",
"]",
"\n",
"for",
"i",
":=",
"1",
";",
"i",
"<",
"len",
"(",
"ints",
")",
";",
"i",
"++",
"{",
"if",
"ints",
"[",
"i",
"]",
"<",
"minInt",
"{",
"minInt",
"=",
"ints",
"[",
"i",
"]",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"minInt",
"\n",
"}"
] |
// minUint64 returns the lowest integer in the provided list of int32s
|
[
"minUint64",
"returns",
"the",
"lowest",
"integer",
"in",
"the",
"provided",
"list",
"of",
"int32s"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/util.go#L44-L53
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
ToNums
|
func (ba *bitArray) ToNums() []uint64 {
nums := make([]uint64, 0, ba.highest-ba.lowest/4)
for i, block := range ba.blocks {
block.toNums(uint64(i)*s, &nums)
}
return nums
}
|
go
|
func (ba *bitArray) ToNums() []uint64 {
nums := make([]uint64, 0, ba.highest-ba.lowest/4)
for i, block := range ba.blocks {
block.toNums(uint64(i)*s, &nums)
}
return nums
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"ToNums",
"(",
")",
"[",
"]",
"uint64",
"{",
"nums",
":=",
"make",
"(",
"[",
"]",
"uint64",
",",
"0",
",",
"ba",
".",
"highest",
"-",
"ba",
".",
"lowest",
"/",
"4",
")",
"\n",
"for",
"i",
",",
"block",
":=",
"range",
"ba",
".",
"blocks",
"{",
"block",
".",
"toNums",
"(",
"uint64",
"(",
"i",
")",
"*",
"s",
",",
"&",
"nums",
")",
"\n",
"}",
"\n\n",
"return",
"nums",
"\n",
"}"
] |
// ToNums converts this bitarray to a list of numbers contained within it.
|
[
"ToNums",
"converts",
"this",
"bitarray",
"to",
"a",
"list",
"of",
"numbers",
"contained",
"within",
"it",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L75-L82
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
SetBit
|
func (ba *bitArray) SetBit(k uint64) error {
if k >= ba.Capacity() {
return OutOfRangeError(k)
}
if !ba.anyset {
ba.lowest = k
ba.highest = k
ba.anyset = true
} else {
if k < ba.lowest {
ba.lowest = k
} else if k > ba.highest {
ba.highest = k
}
}
i, pos := getIndexAndRemainder(k)
ba.blocks[i] = ba.blocks[i].insert(pos)
return nil
}
|
go
|
func (ba *bitArray) SetBit(k uint64) error {
if k >= ba.Capacity() {
return OutOfRangeError(k)
}
if !ba.anyset {
ba.lowest = k
ba.highest = k
ba.anyset = true
} else {
if k < ba.lowest {
ba.lowest = k
} else if k > ba.highest {
ba.highest = k
}
}
i, pos := getIndexAndRemainder(k)
ba.blocks[i] = ba.blocks[i].insert(pos)
return nil
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"SetBit",
"(",
"k",
"uint64",
")",
"error",
"{",
"if",
"k",
">=",
"ba",
".",
"Capacity",
"(",
")",
"{",
"return",
"OutOfRangeError",
"(",
"k",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"ba",
".",
"anyset",
"{",
"ba",
".",
"lowest",
"=",
"k",
"\n",
"ba",
".",
"highest",
"=",
"k",
"\n",
"ba",
".",
"anyset",
"=",
"true",
"\n",
"}",
"else",
"{",
"if",
"k",
"<",
"ba",
".",
"lowest",
"{",
"ba",
".",
"lowest",
"=",
"k",
"\n",
"}",
"else",
"if",
"k",
">",
"ba",
".",
"highest",
"{",
"ba",
".",
"highest",
"=",
"k",
"\n",
"}",
"\n",
"}",
"\n\n",
"i",
",",
"pos",
":=",
"getIndexAndRemainder",
"(",
"k",
")",
"\n",
"ba",
".",
"blocks",
"[",
"i",
"]",
"=",
"ba",
".",
"blocks",
"[",
"i",
"]",
".",
"insert",
"(",
"pos",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// SetBit sets a bit at the given index to true.
|
[
"SetBit",
"sets",
"a",
"bit",
"at",
"the",
"given",
"index",
"to",
"true",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L85-L105
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
GetBit
|
func (ba *bitArray) GetBit(k uint64) (bool, error) {
if k >= ba.Capacity() {
return false, OutOfRangeError(k)
}
i, pos := getIndexAndRemainder(k)
result := ba.blocks[i]&block(1<<pos) != 0
return result, nil
}
|
go
|
func (ba *bitArray) GetBit(k uint64) (bool, error) {
if k >= ba.Capacity() {
return false, OutOfRangeError(k)
}
i, pos := getIndexAndRemainder(k)
result := ba.blocks[i]&block(1<<pos) != 0
return result, nil
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"GetBit",
"(",
"k",
"uint64",
")",
"(",
"bool",
",",
"error",
")",
"{",
"if",
"k",
">=",
"ba",
".",
"Capacity",
"(",
")",
"{",
"return",
"false",
",",
"OutOfRangeError",
"(",
"k",
")",
"\n",
"}",
"\n\n",
"i",
",",
"pos",
":=",
"getIndexAndRemainder",
"(",
"k",
")",
"\n",
"result",
":=",
"ba",
".",
"blocks",
"[",
"i",
"]",
"&",
"block",
"(",
"1",
"<<",
"pos",
")",
"!=",
"0",
"\n",
"return",
"result",
",",
"nil",
"\n",
"}"
] |
// GetBit returns a bool indicating if the value at the given
// index has been set.
|
[
"GetBit",
"returns",
"a",
"bool",
"indicating",
"if",
"the",
"value",
"at",
"the",
"given",
"index",
"has",
"been",
"set",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L109-L117
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
ClearBit
|
func (ba *bitArray) ClearBit(k uint64) error {
if k >= ba.Capacity() {
return OutOfRangeError(k)
}
if !ba.anyset { // nothing is set, might as well bail
return nil
}
i, pos := getIndexAndRemainder(k)
ba.blocks[i] &^= block(1 << pos)
if k == ba.highest {
ba.setHighest()
} else if k == ba.lowest {
ba.setLowest()
}
return nil
}
|
go
|
func (ba *bitArray) ClearBit(k uint64) error {
if k >= ba.Capacity() {
return OutOfRangeError(k)
}
if !ba.anyset { // nothing is set, might as well bail
return nil
}
i, pos := getIndexAndRemainder(k)
ba.blocks[i] &^= block(1 << pos)
if k == ba.highest {
ba.setHighest()
} else if k == ba.lowest {
ba.setLowest()
}
return nil
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"ClearBit",
"(",
"k",
"uint64",
")",
"error",
"{",
"if",
"k",
">=",
"ba",
".",
"Capacity",
"(",
")",
"{",
"return",
"OutOfRangeError",
"(",
"k",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"ba",
".",
"anyset",
"{",
"// nothing is set, might as well bail",
"return",
"nil",
"\n",
"}",
"\n\n",
"i",
",",
"pos",
":=",
"getIndexAndRemainder",
"(",
"k",
")",
"\n",
"ba",
".",
"blocks",
"[",
"i",
"]",
"&^=",
"block",
"(",
"1",
"<<",
"pos",
")",
"\n\n",
"if",
"k",
"==",
"ba",
".",
"highest",
"{",
"ba",
".",
"setHighest",
"(",
")",
"\n",
"}",
"else",
"if",
"k",
"==",
"ba",
".",
"lowest",
"{",
"ba",
".",
"setLowest",
"(",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
//ClearBit will unset a bit at the given index if it is set.
|
[
"ClearBit",
"will",
"unset",
"a",
"bit",
"at",
"the",
"given",
"index",
"if",
"it",
"is",
"set",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L120-L138
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
Or
|
func (ba *bitArray) Or(other BitArray) BitArray {
if dba, ok := other.(*bitArray); ok {
return orDenseWithDenseBitArray(ba, dba)
}
return orSparseWithDenseBitArray(other.(*sparseBitArray), ba)
}
|
go
|
func (ba *bitArray) Or(other BitArray) BitArray {
if dba, ok := other.(*bitArray); ok {
return orDenseWithDenseBitArray(ba, dba)
}
return orSparseWithDenseBitArray(other.(*sparseBitArray), ba)
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"Or",
"(",
"other",
"BitArray",
")",
"BitArray",
"{",
"if",
"dba",
",",
"ok",
":=",
"other",
".",
"(",
"*",
"bitArray",
")",
";",
"ok",
"{",
"return",
"orDenseWithDenseBitArray",
"(",
"ba",
",",
"dba",
")",
"\n",
"}",
"\n\n",
"return",
"orSparseWithDenseBitArray",
"(",
"other",
".",
"(",
"*",
"sparseBitArray",
")",
",",
"ba",
")",
"\n",
"}"
] |
// Or will bitwise or two bit arrays and return a new bit array
// representing the result.
|
[
"Or",
"will",
"bitwise",
"or",
"two",
"bit",
"arrays",
"and",
"return",
"a",
"new",
"bit",
"array",
"representing",
"the",
"result",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L142-L148
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
And
|
func (ba *bitArray) And(other BitArray) BitArray {
if dba, ok := other.(*bitArray); ok {
return andDenseWithDenseBitArray(ba, dba)
}
return andSparseWithDenseBitArray(other.(*sparseBitArray), ba)
}
|
go
|
func (ba *bitArray) And(other BitArray) BitArray {
if dba, ok := other.(*bitArray); ok {
return andDenseWithDenseBitArray(ba, dba)
}
return andSparseWithDenseBitArray(other.(*sparseBitArray), ba)
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"And",
"(",
"other",
"BitArray",
")",
"BitArray",
"{",
"if",
"dba",
",",
"ok",
":=",
"other",
".",
"(",
"*",
"bitArray",
")",
";",
"ok",
"{",
"return",
"andDenseWithDenseBitArray",
"(",
"ba",
",",
"dba",
")",
"\n",
"}",
"\n\n",
"return",
"andSparseWithDenseBitArray",
"(",
"other",
".",
"(",
"*",
"sparseBitArray",
")",
",",
"ba",
")",
"\n",
"}"
] |
// And will bitwise and two bit arrays and return a new bit array
// representing the result.
|
[
"And",
"will",
"bitwise",
"and",
"two",
"bit",
"arrays",
"and",
"return",
"a",
"new",
"bit",
"array",
"representing",
"the",
"result",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L152-L158
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
Reset
|
func (ba *bitArray) Reset() {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] &= block(0)
}
ba.anyset = false
}
|
go
|
func (ba *bitArray) Reset() {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] &= block(0)
}
ba.anyset = false
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"Reset",
"(",
")",
"{",
"for",
"i",
":=",
"uint64",
"(",
"0",
")",
";",
"i",
"<",
"uint64",
"(",
"len",
"(",
"ba",
".",
"blocks",
")",
")",
";",
"i",
"++",
"{",
"ba",
".",
"blocks",
"[",
"i",
"]",
"&=",
"block",
"(",
"0",
")",
"\n",
"}",
"\n",
"ba",
".",
"anyset",
"=",
"false",
"\n",
"}"
] |
// Reset clears out the bit array.
|
[
"Reset",
"clears",
"out",
"the",
"bit",
"array",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L171-L176
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
Equals
|
func (ba *bitArray) Equals(other BitArray) bool {
if other.Capacity() == 0 && ba.highest > 0 {
return false
}
if other.Capacity() == 0 && !ba.anyset {
return true
}
var selfIndex uint64
for iter := other.Blocks(); iter.Next(); {
toIndex, otherBlock := iter.Value()
if toIndex > selfIndex {
for i := selfIndex; i < toIndex; i++ {
if ba.blocks[i] > 0 {
return false
}
}
}
selfIndex = toIndex
if !ba.blocks[selfIndex].equals(otherBlock) {
return false
}
selfIndex++
}
lastIndex, _ := getIndexAndRemainder(ba.highest)
if lastIndex >= selfIndex {
return false
}
return true
}
|
go
|
func (ba *bitArray) Equals(other BitArray) bool {
if other.Capacity() == 0 && ba.highest > 0 {
return false
}
if other.Capacity() == 0 && !ba.anyset {
return true
}
var selfIndex uint64
for iter := other.Blocks(); iter.Next(); {
toIndex, otherBlock := iter.Value()
if toIndex > selfIndex {
for i := selfIndex; i < toIndex; i++ {
if ba.blocks[i] > 0 {
return false
}
}
}
selfIndex = toIndex
if !ba.blocks[selfIndex].equals(otherBlock) {
return false
}
selfIndex++
}
lastIndex, _ := getIndexAndRemainder(ba.highest)
if lastIndex >= selfIndex {
return false
}
return true
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"Equals",
"(",
"other",
"BitArray",
")",
"bool",
"{",
"if",
"other",
".",
"Capacity",
"(",
")",
"==",
"0",
"&&",
"ba",
".",
"highest",
">",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"if",
"other",
".",
"Capacity",
"(",
")",
"==",
"0",
"&&",
"!",
"ba",
".",
"anyset",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"var",
"selfIndex",
"uint64",
"\n",
"for",
"iter",
":=",
"other",
".",
"Blocks",
"(",
")",
";",
"iter",
".",
"Next",
"(",
")",
";",
"{",
"toIndex",
",",
"otherBlock",
":=",
"iter",
".",
"Value",
"(",
")",
"\n",
"if",
"toIndex",
">",
"selfIndex",
"{",
"for",
"i",
":=",
"selfIndex",
";",
"i",
"<",
"toIndex",
";",
"i",
"++",
"{",
"if",
"ba",
".",
"blocks",
"[",
"i",
"]",
">",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"selfIndex",
"=",
"toIndex",
"\n",
"if",
"!",
"ba",
".",
"blocks",
"[",
"selfIndex",
"]",
".",
"equals",
"(",
"otherBlock",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"selfIndex",
"++",
"\n",
"}",
"\n\n",
"lastIndex",
",",
"_",
":=",
"getIndexAndRemainder",
"(",
"ba",
".",
"highest",
")",
"\n",
"if",
"lastIndex",
">=",
"selfIndex",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"return",
"true",
"\n",
"}"
] |
// Equals returns a bool indicating if these two bit arrays are equal.
|
[
"Equals",
"returns",
"a",
"bool",
"indicating",
"if",
"these",
"two",
"bit",
"arrays",
"are",
"equal",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L179-L212
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
Intersects
|
func (ba *bitArray) Intersects(other BitArray) bool {
if other.Capacity() > ba.Capacity() {
return false
}
if sba, ok := other.(*sparseBitArray); ok {
return ba.intersectsSparseBitArray(sba)
}
return ba.intersectsDenseBitArray(other.(*bitArray))
}
|
go
|
func (ba *bitArray) Intersects(other BitArray) bool {
if other.Capacity() > ba.Capacity() {
return false
}
if sba, ok := other.(*sparseBitArray); ok {
return ba.intersectsSparseBitArray(sba)
}
return ba.intersectsDenseBitArray(other.(*bitArray))
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"Intersects",
"(",
"other",
"BitArray",
")",
"bool",
"{",
"if",
"other",
".",
"Capacity",
"(",
")",
">",
"ba",
".",
"Capacity",
"(",
")",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"if",
"sba",
",",
"ok",
":=",
"other",
".",
"(",
"*",
"sparseBitArray",
")",
";",
"ok",
"{",
"return",
"ba",
".",
"intersectsSparseBitArray",
"(",
"sba",
")",
"\n",
"}",
"\n\n",
"return",
"ba",
".",
"intersectsDenseBitArray",
"(",
"other",
".",
"(",
"*",
"bitArray",
")",
")",
"\n",
"}"
] |
// Intersects returns a bool indicating if the supplied bitarray intersects
// this bitarray. This will check for intersection up to the length of the supplied
// bitarray. If the supplied bitarray is longer than this bitarray, this
// function returns false.
|
[
"Intersects",
"returns",
"a",
"bool",
"indicating",
"if",
"the",
"supplied",
"bitarray",
"intersects",
"this",
"bitarray",
".",
"This",
"will",
"check",
"for",
"intersection",
"up",
"to",
"the",
"length",
"of",
"the",
"supplied",
"bitarray",
".",
"If",
"the",
"supplied",
"bitarray",
"is",
"longer",
"than",
"this",
"bitarray",
"this",
"function",
"returns",
"false",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L218-L228
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
complement
|
func (ba *bitArray) complement() {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] = ^ba.blocks[i]
}
ba.setLowest()
if ba.anyset {
ba.setHighest()
}
}
|
go
|
func (ba *bitArray) complement() {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] = ^ba.blocks[i]
}
ba.setLowest()
if ba.anyset {
ba.setHighest()
}
}
|
[
"func",
"(",
"ba",
"*",
"bitArray",
")",
"complement",
"(",
")",
"{",
"for",
"i",
":=",
"uint64",
"(",
"0",
")",
";",
"i",
"<",
"uint64",
"(",
"len",
"(",
"ba",
".",
"blocks",
")",
")",
";",
"i",
"++",
"{",
"ba",
".",
"blocks",
"[",
"i",
"]",
"=",
"^",
"ba",
".",
"blocks",
"[",
"i",
"]",
"\n",
"}",
"\n\n",
"ba",
".",
"setLowest",
"(",
")",
"\n",
"if",
"ba",
".",
"anyset",
"{",
"ba",
".",
"setHighest",
"(",
")",
"\n",
"}",
"\n",
"}"
] |
// complement flips all bits in this array.
|
[
"complement",
"flips",
"all",
"bits",
"in",
"this",
"array",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L240-L249
|
train
|
Workiva/go-datastructures
|
bitarray/bitarray.go
|
newBitArray
|
func newBitArray(size uint64, args ...bool) *bitArray {
i, r := getIndexAndRemainder(size)
if r > 0 {
i++
}
ba := &bitArray{
blocks: make([]block, i),
anyset: false,
}
if len(args) > 0 && args[0] == true {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] = maximumBlock
}
ba.lowest = 0
ba.highest = i*s - 1
ba.anyset = true
}
return ba
}
|
go
|
func newBitArray(size uint64, args ...bool) *bitArray {
i, r := getIndexAndRemainder(size)
if r > 0 {
i++
}
ba := &bitArray{
blocks: make([]block, i),
anyset: false,
}
if len(args) > 0 && args[0] == true {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] = maximumBlock
}
ba.lowest = 0
ba.highest = i*s - 1
ba.anyset = true
}
return ba
}
|
[
"func",
"newBitArray",
"(",
"size",
"uint64",
",",
"args",
"...",
"bool",
")",
"*",
"bitArray",
"{",
"i",
",",
"r",
":=",
"getIndexAndRemainder",
"(",
"size",
")",
"\n",
"if",
"r",
">",
"0",
"{",
"i",
"++",
"\n",
"}",
"\n\n",
"ba",
":=",
"&",
"bitArray",
"{",
"blocks",
":",
"make",
"(",
"[",
"]",
"block",
",",
"i",
")",
",",
"anyset",
":",
"false",
",",
"}",
"\n\n",
"if",
"len",
"(",
"args",
")",
">",
"0",
"&&",
"args",
"[",
"0",
"]",
"==",
"true",
"{",
"for",
"i",
":=",
"uint64",
"(",
"0",
")",
";",
"i",
"<",
"uint64",
"(",
"len",
"(",
"ba",
".",
"blocks",
")",
")",
";",
"i",
"++",
"{",
"ba",
".",
"blocks",
"[",
"i",
"]",
"=",
"maximumBlock",
"\n",
"}",
"\n\n",
"ba",
".",
"lowest",
"=",
"0",
"\n",
"ba",
".",
"highest",
"=",
"i",
"*",
"s",
"-",
"1",
"\n",
"ba",
".",
"anyset",
"=",
"true",
"\n",
"}",
"\n\n",
"return",
"ba",
"\n",
"}"
] |
// newBitArray returns a new dense BitArray at the specified size. This is a
// separate private constructor so unit tests don't have to constantly cast the
// BitArray interface to the concrete type.
|
[
"newBitArray",
"returns",
"a",
"new",
"dense",
"BitArray",
"at",
"the",
"specified",
"size",
".",
"This",
"is",
"a",
"separate",
"private",
"constructor",
"so",
"unit",
"tests",
"don",
"t",
"have",
"to",
"constantly",
"cast",
"the",
"BitArray",
"interface",
"to",
"the",
"concrete",
"type",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/bitarray/bitarray.go#L285-L307
|
train
|
Workiva/go-datastructures
|
btree/plus/iterator.go
|
exhaust
|
func (iter *iterator) exhaust() keys {
keys := make(keys, 0, 10)
for iter := iter; iter.Next(); {
keys = append(keys, iter.Value())
}
return keys
}
|
go
|
func (iter *iterator) exhaust() keys {
keys := make(keys, 0, 10)
for iter := iter; iter.Next(); {
keys = append(keys, iter.Value())
}
return keys
}
|
[
"func",
"(",
"iter",
"*",
"iterator",
")",
"exhaust",
"(",
")",
"keys",
"{",
"keys",
":=",
"make",
"(",
"keys",
",",
"0",
",",
"10",
")",
"\n",
"for",
"iter",
":=",
"iter",
";",
"iter",
".",
"Next",
"(",
")",
";",
"{",
"keys",
"=",
"append",
"(",
"keys",
",",
"iter",
".",
"Value",
"(",
")",
")",
"\n",
"}",
"\n\n",
"return",
"keys",
"\n",
"}"
] |
// exhaust is a test function that's not exported
|
[
"exhaust",
"is",
"a",
"test",
"function",
"that",
"s",
"not",
"exported"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/plus/iterator.go#L55-L62
|
train
|
Workiva/go-datastructures
|
cache/cache.go
|
setElementIfNotNil
|
func (c *cached) setElementIfNotNil(element *list.Element) {
if element != nil {
c.element = element
}
}
|
go
|
func (c *cached) setElementIfNotNil(element *list.Element) {
if element != nil {
c.element = element
}
}
|
[
"func",
"(",
"c",
"*",
"cached",
")",
"setElementIfNotNil",
"(",
"element",
"*",
"list",
".",
"Element",
")",
"{",
"if",
"element",
"!=",
"nil",
"{",
"c",
".",
"element",
"=",
"element",
"\n",
"}",
"\n",
"}"
] |
// Sets the provided list element on the cached item if it is not nil
|
[
"Sets",
"the",
"provided",
"list",
"element",
"on",
"the",
"cached",
"item",
"if",
"it",
"is",
"not",
"nil"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/cache/cache.go#L37-L41
|
train
|
Workiva/go-datastructures
|
cache/cache.go
|
EvictionPolicy
|
func EvictionPolicy(policy Policy) CacheOption {
return func(c *cache) {
switch policy {
case LeastRecentlyAdded:
c.recordAccess = c.noop
c.recordAdd = c.record
case LeastRecentlyUsed:
c.recordAccess = c.record
c.recordAdd = c.noop
}
}
}
|
go
|
func EvictionPolicy(policy Policy) CacheOption {
return func(c *cache) {
switch policy {
case LeastRecentlyAdded:
c.recordAccess = c.noop
c.recordAdd = c.record
case LeastRecentlyUsed:
c.recordAccess = c.record
c.recordAdd = c.noop
}
}
}
|
[
"func",
"EvictionPolicy",
"(",
"policy",
"Policy",
")",
"CacheOption",
"{",
"return",
"func",
"(",
"c",
"*",
"cache",
")",
"{",
"switch",
"policy",
"{",
"case",
"LeastRecentlyAdded",
":",
"c",
".",
"recordAccess",
"=",
"c",
".",
"noop",
"\n",
"c",
".",
"recordAdd",
"=",
"c",
".",
"record",
"\n",
"case",
"LeastRecentlyUsed",
":",
"c",
".",
"recordAccess",
"=",
"c",
".",
"record",
"\n",
"c",
".",
"recordAdd",
"=",
"c",
".",
"noop",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// EvictionPolicy sets the eviction policy to be used to make room for new items.
// If not provided, default is LeastRecentlyUsed.
|
[
"EvictionPolicy",
"sets",
"the",
"eviction",
"policy",
"to",
"be",
"used",
"to",
"make",
"room",
"for",
"new",
"items",
".",
"If",
"not",
"provided",
"default",
"is",
"LeastRecentlyUsed",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/cache/cache.go#L69-L80
|
train
|
Workiva/go-datastructures
|
cache/cache.go
|
New
|
func New(capacity uint64, options ...CacheOption) Cache {
c := &cache{
cap: capacity,
keyList: list.New(),
items: map[string]*cached{},
}
// Default LRU eviction policy
EvictionPolicy(LeastRecentlyUsed)(c)
for _, option := range options {
option(c)
}
return c
}
|
go
|
func New(capacity uint64, options ...CacheOption) Cache {
c := &cache{
cap: capacity,
keyList: list.New(),
items: map[string]*cached{},
}
// Default LRU eviction policy
EvictionPolicy(LeastRecentlyUsed)(c)
for _, option := range options {
option(c)
}
return c
}
|
[
"func",
"New",
"(",
"capacity",
"uint64",
",",
"options",
"...",
"CacheOption",
")",
"Cache",
"{",
"c",
":=",
"&",
"cache",
"{",
"cap",
":",
"capacity",
",",
"keyList",
":",
"list",
".",
"New",
"(",
")",
",",
"items",
":",
"map",
"[",
"string",
"]",
"*",
"cached",
"{",
"}",
",",
"}",
"\n",
"// Default LRU eviction policy",
"EvictionPolicy",
"(",
"LeastRecentlyUsed",
")",
"(",
"c",
")",
"\n\n",
"for",
"_",
",",
"option",
":=",
"range",
"options",
"{",
"option",
"(",
"c",
")",
"\n",
"}",
"\n\n",
"return",
"c",
"\n",
"}"
] |
// New returns a cache with the requested options configured.
// The cache consumes memory bounded by a fixed capacity,
// plus tracking overhead linear in the number of items.
|
[
"New",
"returns",
"a",
"cache",
"with",
"the",
"requested",
"options",
"configured",
".",
"The",
"cache",
"consumes",
"memory",
"bounded",
"by",
"a",
"fixed",
"capacity",
"plus",
"tracking",
"overhead",
"linear",
"in",
"the",
"number",
"of",
"items",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/cache/cache.go#L85-L99
|
train
|
Workiva/go-datastructures
|
cache/cache.go
|
ensureCapacity
|
func (c *cache) ensureCapacity(toAdd uint64) {
mustRemove := int64(c.size+toAdd) - int64(c.cap)
for mustRemove > 0 {
key := c.keyList.Back().Value.(string)
mustRemove -= int64(c.items[key].item.Size())
c.remove(key)
}
}
|
go
|
func (c *cache) ensureCapacity(toAdd uint64) {
mustRemove := int64(c.size+toAdd) - int64(c.cap)
for mustRemove > 0 {
key := c.keyList.Back().Value.(string)
mustRemove -= int64(c.items[key].item.Size())
c.remove(key)
}
}
|
[
"func",
"(",
"c",
"*",
"cache",
")",
"ensureCapacity",
"(",
"toAdd",
"uint64",
")",
"{",
"mustRemove",
":=",
"int64",
"(",
"c",
".",
"size",
"+",
"toAdd",
")",
"-",
"int64",
"(",
"c",
".",
"cap",
")",
"\n",
"for",
"mustRemove",
">",
"0",
"{",
"key",
":=",
"c",
".",
"keyList",
".",
"Back",
"(",
")",
".",
"Value",
".",
"(",
"string",
")",
"\n",
"mustRemove",
"-=",
"int64",
"(",
"c",
".",
"items",
"[",
"key",
"]",
".",
"item",
".",
"Size",
"(",
")",
")",
"\n",
"c",
".",
"remove",
"(",
"key",
")",
"\n",
"}",
"\n",
"}"
] |
// Given the need to add some number of new bytes to the cache,
// evict items according to the eviction policy until there is room.
// The caller should hold the cache lock.
|
[
"Given",
"the",
"need",
"to",
"add",
"some",
"number",
"of",
"new",
"bytes",
"to",
"the",
"cache",
"evict",
"items",
"according",
"to",
"the",
"eviction",
"policy",
"until",
"there",
"is",
"room",
".",
"The",
"caller",
"should",
"hold",
"the",
"cache",
"lock",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/cache/cache.go#L156-L163
|
train
|
Workiva/go-datastructures
|
cache/cache.go
|
remove
|
func (c *cache) remove(key string) {
if cached, ok := c.items[key]; ok {
delete(c.items, key)
c.size -= cached.item.Size()
c.keyList.Remove(cached.element)
}
}
|
go
|
func (c *cache) remove(key string) {
if cached, ok := c.items[key]; ok {
delete(c.items, key)
c.size -= cached.item.Size()
c.keyList.Remove(cached.element)
}
}
|
[
"func",
"(",
"c",
"*",
"cache",
")",
"remove",
"(",
"key",
"string",
")",
"{",
"if",
"cached",
",",
"ok",
":=",
"c",
".",
"items",
"[",
"key",
"]",
";",
"ok",
"{",
"delete",
"(",
"c",
".",
"items",
",",
"key",
")",
"\n",
"c",
".",
"size",
"-=",
"cached",
".",
"item",
".",
"Size",
"(",
")",
"\n",
"c",
".",
"keyList",
".",
"Remove",
"(",
"cached",
".",
"element",
")",
"\n",
"}",
"\n",
"}"
] |
// Remove the item associated with the given key.
// The caller should hold the cache lock.
|
[
"Remove",
"the",
"item",
"associated",
"with",
"the",
"given",
"key",
".",
"The",
"caller",
"should",
"hold",
"the",
"cache",
"lock",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/cache/cache.go#L167-L173
|
train
|
Workiva/go-datastructures
|
cache/cache.go
|
record
|
func (c *cache) record(key string) *list.Element {
if item, ok := c.items[key]; ok {
c.keyList.MoveToFront(item.element)
return item.element
}
return c.keyList.PushFront(key)
}
|
go
|
func (c *cache) record(key string) *list.Element {
if item, ok := c.items[key]; ok {
c.keyList.MoveToFront(item.element)
return item.element
}
return c.keyList.PushFront(key)
}
|
[
"func",
"(",
"c",
"*",
"cache",
")",
"record",
"(",
"key",
"string",
")",
"*",
"list",
".",
"Element",
"{",
"if",
"item",
",",
"ok",
":=",
"c",
".",
"items",
"[",
"key",
"]",
";",
"ok",
"{",
"c",
".",
"keyList",
".",
"MoveToFront",
"(",
"item",
".",
"element",
")",
"\n",
"return",
"item",
".",
"element",
"\n",
"}",
"\n",
"return",
"c",
".",
"keyList",
".",
"PushFront",
"(",
"key",
")",
"\n",
"}"
] |
// A function to record the given key and mark it as last to be evicted
|
[
"A",
"function",
"to",
"record",
"the",
"given",
"key",
"and",
"mark",
"it",
"as",
"last",
"to",
"be",
"evicted"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/cache/cache.go#L179-L185
|
train
|
Workiva/go-datastructures
|
set/dict.go
|
Add
|
func (set *Set) Add(items ...interface{}) {
set.lock.Lock()
defer set.lock.Unlock()
set.flattened = nil
for _, item := range items {
set.items[item] = struct{}{}
}
}
|
go
|
func (set *Set) Add(items ...interface{}) {
set.lock.Lock()
defer set.lock.Unlock()
set.flattened = nil
for _, item := range items {
set.items[item] = struct{}{}
}
}
|
[
"func",
"(",
"set",
"*",
"Set",
")",
"Add",
"(",
"items",
"...",
"interface",
"{",
"}",
")",
"{",
"set",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"set",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"set",
".",
"flattened",
"=",
"nil",
"\n",
"for",
"_",
",",
"item",
":=",
"range",
"items",
"{",
"set",
".",
"items",
"[",
"item",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n",
"}"
] |
// Add will add the provided items to the set.
|
[
"Add",
"will",
"add",
"the",
"provided",
"items",
"to",
"the",
"set",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/set/dict.go#L42-L50
|
train
|
Workiva/go-datastructures
|
set/dict.go
|
Remove
|
func (set *Set) Remove(items ...interface{}) {
set.lock.Lock()
defer set.lock.Unlock()
set.flattened = nil
for _, item := range items {
delete(set.items, item)
}
}
|
go
|
func (set *Set) Remove(items ...interface{}) {
set.lock.Lock()
defer set.lock.Unlock()
set.flattened = nil
for _, item := range items {
delete(set.items, item)
}
}
|
[
"func",
"(",
"set",
"*",
"Set",
")",
"Remove",
"(",
"items",
"...",
"interface",
"{",
"}",
")",
"{",
"set",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"set",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"set",
".",
"flattened",
"=",
"nil",
"\n",
"for",
"_",
",",
"item",
":=",
"range",
"items",
"{",
"delete",
"(",
"set",
".",
"items",
",",
"item",
")",
"\n",
"}",
"\n",
"}"
] |
// Remove will remove the given items from the set.
|
[
"Remove",
"will",
"remove",
"the",
"given",
"items",
"from",
"the",
"set",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/set/dict.go#L53-L61
|
train
|
Workiva/go-datastructures
|
set/dict.go
|
Exists
|
func (set *Set) Exists(item interface{}) bool {
set.lock.RLock()
_, ok := set.items[item]
set.lock.RUnlock()
return ok
}
|
go
|
func (set *Set) Exists(item interface{}) bool {
set.lock.RLock()
_, ok := set.items[item]
set.lock.RUnlock()
return ok
}
|
[
"func",
"(",
"set",
"*",
"Set",
")",
"Exists",
"(",
"item",
"interface",
"{",
"}",
")",
"bool",
"{",
"set",
".",
"lock",
".",
"RLock",
"(",
")",
"\n\n",
"_",
",",
"ok",
":=",
"set",
".",
"items",
"[",
"item",
"]",
"\n\n",
"set",
".",
"lock",
".",
"RUnlock",
"(",
")",
"\n\n",
"return",
"ok",
"\n",
"}"
] |
// Exists returns a bool indicating if the given item exists in the set.
|
[
"Exists",
"returns",
"a",
"bool",
"indicating",
"if",
"the",
"given",
"item",
"exists",
"in",
"the",
"set",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/set/dict.go#L64-L72
|
train
|
Workiva/go-datastructures
|
set/dict.go
|
Flatten
|
func (set *Set) Flatten() []interface{} {
set.lock.Lock()
defer set.lock.Unlock()
if set.flattened != nil {
return set.flattened
}
set.flattened = make([]interface{}, 0, len(set.items))
for item := range set.items {
set.flattened = append(set.flattened, item)
}
return set.flattened
}
|
go
|
func (set *Set) Flatten() []interface{} {
set.lock.Lock()
defer set.lock.Unlock()
if set.flattened != nil {
return set.flattened
}
set.flattened = make([]interface{}, 0, len(set.items))
for item := range set.items {
set.flattened = append(set.flattened, item)
}
return set.flattened
}
|
[
"func",
"(",
"set",
"*",
"Set",
")",
"Flatten",
"(",
")",
"[",
"]",
"interface",
"{",
"}",
"{",
"set",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"set",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"set",
".",
"flattened",
"!=",
"nil",
"{",
"return",
"set",
".",
"flattened",
"\n",
"}",
"\n\n",
"set",
".",
"flattened",
"=",
"make",
"(",
"[",
"]",
"interface",
"{",
"}",
",",
"0",
",",
"len",
"(",
"set",
".",
"items",
")",
")",
"\n",
"for",
"item",
":=",
"range",
"set",
".",
"items",
"{",
"set",
".",
"flattened",
"=",
"append",
"(",
"set",
".",
"flattened",
",",
"item",
")",
"\n",
"}",
"\n",
"return",
"set",
".",
"flattened",
"\n",
"}"
] |
// Flatten will return a list of the items in the set.
|
[
"Flatten",
"will",
"return",
"a",
"list",
"of",
"the",
"items",
"in",
"the",
"set",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/set/dict.go#L75-L88
|
train
|
Workiva/go-datastructures
|
set/dict.go
|
Len
|
func (set *Set) Len() int64 {
set.lock.RLock()
size := int64(len(set.items))
set.lock.RUnlock()
return size
}
|
go
|
func (set *Set) Len() int64 {
set.lock.RLock()
size := int64(len(set.items))
set.lock.RUnlock()
return size
}
|
[
"func",
"(",
"set",
"*",
"Set",
")",
"Len",
"(",
")",
"int64",
"{",
"set",
".",
"lock",
".",
"RLock",
"(",
")",
"\n\n",
"size",
":=",
"int64",
"(",
"len",
"(",
"set",
".",
"items",
")",
")",
"\n\n",
"set",
".",
"lock",
".",
"RUnlock",
"(",
")",
"\n\n",
"return",
"size",
"\n",
"}"
] |
// Len returns the number of items in the set.
|
[
"Len",
"returns",
"the",
"number",
"of",
"items",
"in",
"the",
"set",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/set/dict.go#L91-L99
|
train
|
Workiva/go-datastructures
|
set/dict.go
|
Clear
|
func (set *Set) Clear() {
set.lock.Lock()
set.items = map[interface{}]struct{}{}
set.lock.Unlock()
}
|
go
|
func (set *Set) Clear() {
set.lock.Lock()
set.items = map[interface{}]struct{}{}
set.lock.Unlock()
}
|
[
"func",
"(",
"set",
"*",
"Set",
")",
"Clear",
"(",
")",
"{",
"set",
".",
"lock",
".",
"Lock",
"(",
")",
"\n\n",
"set",
".",
"items",
"=",
"map",
"[",
"interface",
"{",
"}",
"]",
"struct",
"{",
"}",
"{",
"}",
"\n\n",
"set",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"}"
] |
// Clear will remove all items from the set.
|
[
"Clear",
"will",
"remove",
"all",
"items",
"from",
"the",
"set",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/set/dict.go#L102-L108
|
train
|
Workiva/go-datastructures
|
set/dict.go
|
All
|
func (set *Set) All(items ...interface{}) bool {
set.lock.RLock()
defer set.lock.RUnlock()
for _, item := range items {
if _, ok := set.items[item]; !ok {
return false
}
}
return true
}
|
go
|
func (set *Set) All(items ...interface{}) bool {
set.lock.RLock()
defer set.lock.RUnlock()
for _, item := range items {
if _, ok := set.items[item]; !ok {
return false
}
}
return true
}
|
[
"func",
"(",
"set",
"*",
"Set",
")",
"All",
"(",
"items",
"...",
"interface",
"{",
"}",
")",
"bool",
"{",
"set",
".",
"lock",
".",
"RLock",
"(",
")",
"\n",
"defer",
"set",
".",
"lock",
".",
"RUnlock",
"(",
")",
"\n\n",
"for",
"_",
",",
"item",
":=",
"range",
"items",
"{",
"if",
"_",
",",
"ok",
":=",
"set",
".",
"items",
"[",
"item",
"]",
";",
"!",
"ok",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"true",
"\n",
"}"
] |
// All returns a bool indicating if all of the supplied items exist in the set.
|
[
"All",
"returns",
"a",
"bool",
"indicating",
"if",
"all",
"of",
"the",
"supplied",
"items",
"exist",
"in",
"the",
"set",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/set/dict.go#L111-L122
|
train
|
Workiva/go-datastructures
|
set/dict.go
|
Dispose
|
func (set *Set) Dispose() {
set.lock.Lock()
defer set.lock.Unlock()
for k := range set.items {
delete(set.items, k)
}
//this is so we don't hang onto any references
for i := 0; i < len(set.flattened); i++ {
set.flattened[i] = nil
}
set.flattened = set.flattened[:0]
pool.Put(set)
}
|
go
|
func (set *Set) Dispose() {
set.lock.Lock()
defer set.lock.Unlock()
for k := range set.items {
delete(set.items, k)
}
//this is so we don't hang onto any references
for i := 0; i < len(set.flattened); i++ {
set.flattened[i] = nil
}
set.flattened = set.flattened[:0]
pool.Put(set)
}
|
[
"func",
"(",
"set",
"*",
"Set",
")",
"Dispose",
"(",
")",
"{",
"set",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"set",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"for",
"k",
":=",
"range",
"set",
".",
"items",
"{",
"delete",
"(",
"set",
".",
"items",
",",
"k",
")",
"\n",
"}",
"\n\n",
"//this is so we don't hang onto any references",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"set",
".",
"flattened",
")",
";",
"i",
"++",
"{",
"set",
".",
"flattened",
"[",
"i",
"]",
"=",
"nil",
"\n",
"}",
"\n\n",
"set",
".",
"flattened",
"=",
"set",
".",
"flattened",
"[",
":",
"0",
"]",
"\n",
"pool",
".",
"Put",
"(",
"set",
")",
"\n",
"}"
] |
// Dispose will add this set back into the pool.
|
[
"Dispose",
"will",
"add",
"this",
"set",
"back",
"into",
"the",
"pool",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/set/dict.go#L125-L140
|
train
|
Workiva/go-datastructures
|
set/dict.go
|
New
|
func New(items ...interface{}) *Set {
set := pool.Get().(*Set)
for _, item := range items {
set.items[item] = struct{}{}
}
if len(items) > 0 {
set.flattened = nil
}
return set
}
|
go
|
func New(items ...interface{}) *Set {
set := pool.Get().(*Set)
for _, item := range items {
set.items[item] = struct{}{}
}
if len(items) > 0 {
set.flattened = nil
}
return set
}
|
[
"func",
"New",
"(",
"items",
"...",
"interface",
"{",
"}",
")",
"*",
"Set",
"{",
"set",
":=",
"pool",
".",
"Get",
"(",
")",
".",
"(",
"*",
"Set",
")",
"\n",
"for",
"_",
",",
"item",
":=",
"range",
"items",
"{",
"set",
".",
"items",
"[",
"item",
"]",
"=",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n",
"if",
"len",
"(",
"items",
")",
">",
"0",
"{",
"set",
".",
"flattened",
"=",
"nil",
"\n",
"}",
"\n\n",
"return",
"set",
"\n",
"}"
] |
// New is the constructor for sets. It will pull from a reuseable memory pool if it can.
// Takes a list of items to initialize the set with.
|
[
"New",
"is",
"the",
"constructor",
"for",
"sets",
".",
"It",
"will",
"pull",
"from",
"a",
"reuseable",
"memory",
"pool",
"if",
"it",
"can",
".",
"Takes",
"a",
"list",
"of",
"items",
"to",
"initialize",
"the",
"set",
"with",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/set/dict.go#L144-L155
|
train
|
Workiva/go-datastructures
|
augmentedtree/intervals.go
|
Dispose
|
func (ivs *Intervals) Dispose() {
for i := 0; i < len(*ivs); i++ {
(*ivs)[i] = nil
}
*ivs = (*ivs)[:0]
intervalsPool.Put(*ivs)
}
|
go
|
func (ivs *Intervals) Dispose() {
for i := 0; i < len(*ivs); i++ {
(*ivs)[i] = nil
}
*ivs = (*ivs)[:0]
intervalsPool.Put(*ivs)
}
|
[
"func",
"(",
"ivs",
"*",
"Intervals",
")",
"Dispose",
"(",
")",
"{",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"*",
"ivs",
")",
";",
"i",
"++",
"{",
"(",
"*",
"ivs",
")",
"[",
"i",
"]",
"=",
"nil",
"\n",
"}",
"\n\n",
"*",
"ivs",
"=",
"(",
"*",
"ivs",
")",
"[",
":",
"0",
"]",
"\n",
"intervalsPool",
".",
"Put",
"(",
"*",
"ivs",
")",
"\n",
"}"
] |
// Dispose will free any consumed resources and allow this list to be
// re-allocated.
|
[
"Dispose",
"will",
"free",
"any",
"consumed",
"resources",
"and",
"allow",
"this",
"list",
"to",
"be",
"re",
"-",
"allocated",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/augmentedtree/intervals.go#L32-L39
|
train
|
Workiva/go-datastructures
|
slice/skip/iterator.go
|
Next
|
func (iter *iterator) Next() bool {
if iter.first {
iter.first = false
return iter.n != nil
}
if iter.n == nil {
return false
}
iter.n = iter.n.forward[0]
return iter.n != nil
}
|
go
|
func (iter *iterator) Next() bool {
if iter.first {
iter.first = false
return iter.n != nil
}
if iter.n == nil {
return false
}
iter.n = iter.n.forward[0]
return iter.n != nil
}
|
[
"func",
"(",
"iter",
"*",
"iterator",
")",
"Next",
"(",
")",
"bool",
"{",
"if",
"iter",
".",
"first",
"{",
"iter",
".",
"first",
"=",
"false",
"\n",
"return",
"iter",
".",
"n",
"!=",
"nil",
"\n",
"}",
"\n\n",
"if",
"iter",
".",
"n",
"==",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"iter",
".",
"n",
"=",
"iter",
".",
"n",
".",
"forward",
"[",
"0",
"]",
"\n",
"return",
"iter",
".",
"n",
"!=",
"nil",
"\n",
"}"
] |
// Next returns a bool indicating if there are any further values
// in this iterator.
|
[
"Next",
"returns",
"a",
"bool",
"indicating",
"if",
"there",
"are",
"any",
"further",
"values",
"in",
"this",
"iterator",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/iterator.go#L33-L45
|
train
|
Workiva/go-datastructures
|
slice/skip/iterator.go
|
Value
|
func (iter *iterator) Value() common.Comparator {
if iter.n == nil {
return nil
}
return iter.n.entry
}
|
go
|
func (iter *iterator) Value() common.Comparator {
if iter.n == nil {
return nil
}
return iter.n.entry
}
|
[
"func",
"(",
"iter",
"*",
"iterator",
")",
"Value",
"(",
")",
"common",
".",
"Comparator",
"{",
"if",
"iter",
".",
"n",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"return",
"iter",
".",
"n",
".",
"entry",
"\n",
"}"
] |
// Value returns a Comparator representing the iterator's present
// position in the query. Returns nil if no values remain to iterate.
|
[
"Value",
"returns",
"a",
"Comparator",
"representing",
"the",
"iterator",
"s",
"present",
"position",
"in",
"the",
"query",
".",
"Returns",
"nil",
"if",
"no",
"values",
"remain",
"to",
"iterate",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/iterator.go#L49-L55
|
train
|
Workiva/go-datastructures
|
slice/skip/iterator.go
|
exhaust
|
func (iter *iterator) exhaust() common.Comparators {
entries := make(common.Comparators, 0, 10)
for i := iter; i.Next(); {
entries = append(entries, i.Value())
}
return entries
}
|
go
|
func (iter *iterator) exhaust() common.Comparators {
entries := make(common.Comparators, 0, 10)
for i := iter; i.Next(); {
entries = append(entries, i.Value())
}
return entries
}
|
[
"func",
"(",
"iter",
"*",
"iterator",
")",
"exhaust",
"(",
")",
"common",
".",
"Comparators",
"{",
"entries",
":=",
"make",
"(",
"common",
".",
"Comparators",
",",
"0",
",",
"10",
")",
"\n",
"for",
"i",
":=",
"iter",
";",
"i",
".",
"Next",
"(",
")",
";",
"{",
"entries",
"=",
"append",
"(",
"entries",
",",
"i",
".",
"Value",
"(",
")",
")",
"\n",
"}",
"\n\n",
"return",
"entries",
"\n",
"}"
] |
// exhaust is a helper method to exhaust this iterator and return
// all remaining entries.
|
[
"exhaust",
"is",
"a",
"helper",
"method",
"to",
"exhaust",
"this",
"iterator",
"and",
"return",
"all",
"remaining",
"entries",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/slice/skip/iterator.go#L59-L66
|
train
|
Workiva/go-datastructures
|
futures/selectable.go
|
WaitChan
|
func (f *Selectable) WaitChan() <-chan struct{} {
if atomic.LoadUint32(&f.filled) == 1 {
return closed
}
return f.wchan()
}
|
go
|
func (f *Selectable) WaitChan() <-chan struct{} {
if atomic.LoadUint32(&f.filled) == 1 {
return closed
}
return f.wchan()
}
|
[
"func",
"(",
"f",
"*",
"Selectable",
")",
"WaitChan",
"(",
")",
"<-",
"chan",
"struct",
"{",
"}",
"{",
"if",
"atomic",
".",
"LoadUint32",
"(",
"&",
"f",
".",
"filled",
")",
"==",
"1",
"{",
"return",
"closed",
"\n",
"}",
"\n",
"return",
"f",
".",
"wchan",
"(",
")",
"\n",
"}"
] |
// WaitChan returns channel, which is closed when future is fullfilled.
|
[
"WaitChan",
"returns",
"channel",
"which",
"is",
"closed",
"when",
"future",
"is",
"fullfilled",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/futures/selectable.go#L60-L65
|
train
|
Workiva/go-datastructures
|
futures/selectable.go
|
GetResult
|
func (f *Selectable) GetResult() (interface{}, error) {
if atomic.LoadUint32(&f.filled) == 0 {
<-f.wchan()
}
return f.val, f.err
}
|
go
|
func (f *Selectable) GetResult() (interface{}, error) {
if atomic.LoadUint32(&f.filled) == 0 {
<-f.wchan()
}
return f.val, f.err
}
|
[
"func",
"(",
"f",
"*",
"Selectable",
")",
"GetResult",
"(",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"if",
"atomic",
".",
"LoadUint32",
"(",
"&",
"f",
".",
"filled",
")",
"==",
"0",
"{",
"<-",
"f",
".",
"wchan",
"(",
")",
"\n",
"}",
"\n",
"return",
"f",
".",
"val",
",",
"f",
".",
"err",
"\n",
"}"
] |
// GetResult waits for future to be fullfilled and returns value or error,
// whatever is set first
|
[
"GetResult",
"waits",
"for",
"future",
"to",
"be",
"fullfilled",
"and",
"returns",
"value",
"or",
"error",
"whatever",
"is",
"set",
"first"
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/futures/selectable.go#L69-L74
|
train
|
Workiva/go-datastructures
|
futures/selectable.go
|
Fill
|
func (f *Selectable) Fill(v interface{}, e error) error {
f.m.Lock()
if f.filled == 0 {
f.val = v
f.err = e
atomic.StoreUint32(&f.filled, 1)
w := f.wait
f.wait = closed
if w != nil {
close(w)
}
}
f.m.Unlock()
return f.err
}
|
go
|
func (f *Selectable) Fill(v interface{}, e error) error {
f.m.Lock()
if f.filled == 0 {
f.val = v
f.err = e
atomic.StoreUint32(&f.filled, 1)
w := f.wait
f.wait = closed
if w != nil {
close(w)
}
}
f.m.Unlock()
return f.err
}
|
[
"func",
"(",
"f",
"*",
"Selectable",
")",
"Fill",
"(",
"v",
"interface",
"{",
"}",
",",
"e",
"error",
")",
"error",
"{",
"f",
".",
"m",
".",
"Lock",
"(",
")",
"\n",
"if",
"f",
".",
"filled",
"==",
"0",
"{",
"f",
".",
"val",
"=",
"v",
"\n",
"f",
".",
"err",
"=",
"e",
"\n",
"atomic",
".",
"StoreUint32",
"(",
"&",
"f",
".",
"filled",
",",
"1",
")",
"\n",
"w",
":=",
"f",
".",
"wait",
"\n",
"f",
".",
"wait",
"=",
"closed",
"\n",
"if",
"w",
"!=",
"nil",
"{",
"close",
"(",
"w",
")",
"\n",
"}",
"\n",
"}",
"\n",
"f",
".",
"m",
".",
"Unlock",
"(",
")",
"\n",
"return",
"f",
".",
"err",
"\n",
"}"
] |
// Fill sets value for future, if it were not already fullfilled
// Returns error, if it were already set to future.
|
[
"Fill",
"sets",
"value",
"for",
"future",
"if",
"it",
"were",
"not",
"already",
"fullfilled",
"Returns",
"error",
"if",
"it",
"were",
"already",
"set",
"to",
"future",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/futures/selectable.go#L78-L92
|
train
|
Workiva/go-datastructures
|
queue/queue.go
|
Put
|
func (q *Queue) Put(items ...interface{}) error {
if len(items) == 0 {
return nil
}
q.lock.Lock()
if q.disposed {
q.lock.Unlock()
return ErrDisposed
}
q.items = append(q.items, items...)
for {
sema := q.waiters.get()
if sema == nil {
break
}
sema.response.Add(1)
select {
case sema.ready <- true:
sema.response.Wait()
default:
// This semaphore timed out.
}
if len(q.items) == 0 {
break
}
}
q.lock.Unlock()
return nil
}
|
go
|
func (q *Queue) Put(items ...interface{}) error {
if len(items) == 0 {
return nil
}
q.lock.Lock()
if q.disposed {
q.lock.Unlock()
return ErrDisposed
}
q.items = append(q.items, items...)
for {
sema := q.waiters.get()
if sema == nil {
break
}
sema.response.Add(1)
select {
case sema.ready <- true:
sema.response.Wait()
default:
// This semaphore timed out.
}
if len(q.items) == 0 {
break
}
}
q.lock.Unlock()
return nil
}
|
[
"func",
"(",
"q",
"*",
"Queue",
")",
"Put",
"(",
"items",
"...",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"len",
"(",
"items",
")",
"==",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"q",
".",
"lock",
".",
"Lock",
"(",
")",
"\n\n",
"if",
"q",
".",
"disposed",
"{",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"ErrDisposed",
"\n",
"}",
"\n\n",
"q",
".",
"items",
"=",
"append",
"(",
"q",
".",
"items",
",",
"items",
"...",
")",
"\n",
"for",
"{",
"sema",
":=",
"q",
".",
"waiters",
".",
"get",
"(",
")",
"\n",
"if",
"sema",
"==",
"nil",
"{",
"break",
"\n",
"}",
"\n",
"sema",
".",
"response",
".",
"Add",
"(",
"1",
")",
"\n",
"select",
"{",
"case",
"sema",
".",
"ready",
"<-",
"true",
":",
"sema",
".",
"response",
".",
"Wait",
"(",
")",
"\n",
"default",
":",
"// This semaphore timed out.",
"}",
"\n",
"if",
"len",
"(",
"q",
".",
"items",
")",
"==",
"0",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n\n",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Put will add the specified items to the queue.
|
[
"Put",
"will",
"add",
"the",
"specified",
"items",
"to",
"the",
"queue",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/queue.go#L170-L202
|
train
|
Workiva/go-datastructures
|
queue/queue.go
|
Poll
|
func (q *Queue) Poll(number int64, timeout time.Duration) ([]interface{}, error) {
if number < 1 {
// thanks again go
return []interface{}{}, nil
}
q.lock.Lock()
if q.disposed {
q.lock.Unlock()
return nil, ErrDisposed
}
var items []interface{}
if len(q.items) == 0 {
sema := newSema()
q.waiters.put(sema)
q.lock.Unlock()
var timeoutC <-chan time.Time
if timeout > 0 {
timeoutC = time.After(timeout)
}
select {
case <-sema.ready:
// we are now inside the put's lock
if q.disposed {
return nil, ErrDisposed
}
items = q.items.get(number)
sema.response.Done()
return items, nil
case <-timeoutC:
// cleanup the sema that was added to waiters
select {
case sema.ready <- true:
// we called this before Put() could
// Remove sema from waiters.
q.lock.Lock()
q.waiters.remove(sema)
q.lock.Unlock()
default:
// Put() got it already, we need to call Done() so Put() can move on
sema.response.Done()
}
return nil, ErrTimeout
}
}
items = q.items.get(number)
q.lock.Unlock()
return items, nil
}
|
go
|
func (q *Queue) Poll(number int64, timeout time.Duration) ([]interface{}, error) {
if number < 1 {
// thanks again go
return []interface{}{}, nil
}
q.lock.Lock()
if q.disposed {
q.lock.Unlock()
return nil, ErrDisposed
}
var items []interface{}
if len(q.items) == 0 {
sema := newSema()
q.waiters.put(sema)
q.lock.Unlock()
var timeoutC <-chan time.Time
if timeout > 0 {
timeoutC = time.After(timeout)
}
select {
case <-sema.ready:
// we are now inside the put's lock
if q.disposed {
return nil, ErrDisposed
}
items = q.items.get(number)
sema.response.Done()
return items, nil
case <-timeoutC:
// cleanup the sema that was added to waiters
select {
case sema.ready <- true:
// we called this before Put() could
// Remove sema from waiters.
q.lock.Lock()
q.waiters.remove(sema)
q.lock.Unlock()
default:
// Put() got it already, we need to call Done() so Put() can move on
sema.response.Done()
}
return nil, ErrTimeout
}
}
items = q.items.get(number)
q.lock.Unlock()
return items, nil
}
|
[
"func",
"(",
"q",
"*",
"Queue",
")",
"Poll",
"(",
"number",
"int64",
",",
"timeout",
"time",
".",
"Duration",
")",
"(",
"[",
"]",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"if",
"number",
"<",
"1",
"{",
"// thanks again go",
"return",
"[",
"]",
"interface",
"{",
"}",
"{",
"}",
",",
"nil",
"\n",
"}",
"\n\n",
"q",
".",
"lock",
".",
"Lock",
"(",
")",
"\n\n",
"if",
"q",
".",
"disposed",
"{",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"nil",
",",
"ErrDisposed",
"\n",
"}",
"\n\n",
"var",
"items",
"[",
"]",
"interface",
"{",
"}",
"\n\n",
"if",
"len",
"(",
"q",
".",
"items",
")",
"==",
"0",
"{",
"sema",
":=",
"newSema",
"(",
")",
"\n",
"q",
".",
"waiters",
".",
"put",
"(",
"sema",
")",
"\n",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"var",
"timeoutC",
"<-",
"chan",
"time",
".",
"Time",
"\n",
"if",
"timeout",
">",
"0",
"{",
"timeoutC",
"=",
"time",
".",
"After",
"(",
"timeout",
")",
"\n",
"}",
"\n",
"select",
"{",
"case",
"<-",
"sema",
".",
"ready",
":",
"// we are now inside the put's lock",
"if",
"q",
".",
"disposed",
"{",
"return",
"nil",
",",
"ErrDisposed",
"\n",
"}",
"\n",
"items",
"=",
"q",
".",
"items",
".",
"get",
"(",
"number",
")",
"\n",
"sema",
".",
"response",
".",
"Done",
"(",
")",
"\n",
"return",
"items",
",",
"nil",
"\n",
"case",
"<-",
"timeoutC",
":",
"// cleanup the sema that was added to waiters",
"select",
"{",
"case",
"sema",
".",
"ready",
"<-",
"true",
":",
"// we called this before Put() could",
"// Remove sema from waiters.",
"q",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"q",
".",
"waiters",
".",
"remove",
"(",
"sema",
")",
"\n",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"default",
":",
"// Put() got it already, we need to call Done() so Put() can move on",
"sema",
".",
"response",
".",
"Done",
"(",
")",
"\n",
"}",
"\n",
"return",
"nil",
",",
"ErrTimeout",
"\n",
"}",
"\n",
"}",
"\n\n",
"items",
"=",
"q",
".",
"items",
".",
"get",
"(",
"number",
")",
"\n",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"items",
",",
"nil",
"\n",
"}"
] |
// Poll retrieves items from the queue. If there are some items in the queue,
// Poll will return a number UP TO the number passed in as a parameter. If no
// items are in the queue, this method will pause until items are added to the
// queue or the provided timeout is reached. A non-positive timeout will block
// until items are added. If a timeout occurs, ErrTimeout is returned.
|
[
"Poll",
"retrieves",
"items",
"from",
"the",
"queue",
".",
"If",
"there",
"are",
"some",
"items",
"in",
"the",
"queue",
"Poll",
"will",
"return",
"a",
"number",
"UP",
"TO",
"the",
"number",
"passed",
"in",
"as",
"a",
"parameter",
".",
"If",
"no",
"items",
"are",
"in",
"the",
"queue",
"this",
"method",
"will",
"pause",
"until",
"items",
"are",
"added",
"to",
"the",
"queue",
"or",
"the",
"provided",
"timeout",
"is",
"reached",
".",
"A",
"non",
"-",
"positive",
"timeout",
"will",
"block",
"until",
"items",
"are",
"added",
".",
"If",
"a",
"timeout",
"occurs",
"ErrTimeout",
"is",
"returned",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/queue.go#L217-L270
|
train
|
Workiva/go-datastructures
|
queue/queue.go
|
Peek
|
func (q *Queue) Peek() (interface{}, error) {
q.lock.Lock()
defer q.lock.Unlock()
if q.disposed {
return nil, ErrDisposed
}
peekItem, ok := q.items.peek()
if !ok {
return nil, ErrEmptyQueue
}
return peekItem, nil
}
|
go
|
func (q *Queue) Peek() (interface{}, error) {
q.lock.Lock()
defer q.lock.Unlock()
if q.disposed {
return nil, ErrDisposed
}
peekItem, ok := q.items.peek()
if !ok {
return nil, ErrEmptyQueue
}
return peekItem, nil
}
|
[
"func",
"(",
"q",
"*",
"Queue",
")",
"Peek",
"(",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"q",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"q",
".",
"disposed",
"{",
"return",
"nil",
",",
"ErrDisposed",
"\n",
"}",
"\n\n",
"peekItem",
",",
"ok",
":=",
"q",
".",
"items",
".",
"peek",
"(",
")",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"ErrEmptyQueue",
"\n",
"}",
"\n\n",
"return",
"peekItem",
",",
"nil",
"\n",
"}"
] |
// Peek returns a the first item in the queue by value
// without modifying the queue.
|
[
"Peek",
"returns",
"a",
"the",
"first",
"item",
"in",
"the",
"queue",
"by",
"value",
"without",
"modifying",
"the",
"queue",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/queue.go#L274-L288
|
train
|
Workiva/go-datastructures
|
queue/queue.go
|
TakeUntil
|
func (q *Queue) TakeUntil(checker func(item interface{}) bool) ([]interface{}, error) {
if checker == nil {
return nil, nil
}
q.lock.Lock()
if q.disposed {
q.lock.Unlock()
return nil, ErrDisposed
}
result := q.items.getUntil(checker)
q.lock.Unlock()
return result, nil
}
|
go
|
func (q *Queue) TakeUntil(checker func(item interface{}) bool) ([]interface{}, error) {
if checker == nil {
return nil, nil
}
q.lock.Lock()
if q.disposed {
q.lock.Unlock()
return nil, ErrDisposed
}
result := q.items.getUntil(checker)
q.lock.Unlock()
return result, nil
}
|
[
"func",
"(",
"q",
"*",
"Queue",
")",
"TakeUntil",
"(",
"checker",
"func",
"(",
"item",
"interface",
"{",
"}",
")",
"bool",
")",
"(",
"[",
"]",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"if",
"checker",
"==",
"nil",
"{",
"return",
"nil",
",",
"nil",
"\n",
"}",
"\n\n",
"q",
".",
"lock",
".",
"Lock",
"(",
")",
"\n\n",
"if",
"q",
".",
"disposed",
"{",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"nil",
",",
"ErrDisposed",
"\n",
"}",
"\n\n",
"result",
":=",
"q",
".",
"items",
".",
"getUntil",
"(",
"checker",
")",
"\n",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"result",
",",
"nil",
"\n",
"}"
] |
// TakeUntil takes a function and returns a list of items that
// match the checker until the checker returns false. This does not
// wait if there are no items in the queue.
|
[
"TakeUntil",
"takes",
"a",
"function",
"and",
"returns",
"a",
"list",
"of",
"items",
"that",
"match",
"the",
"checker",
"until",
"the",
"checker",
"returns",
"false",
".",
"This",
"does",
"not",
"wait",
"if",
"there",
"are",
"no",
"items",
"in",
"the",
"queue",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/queue.go#L293-L308
|
train
|
Workiva/go-datastructures
|
queue/queue.go
|
Empty
|
func (q *Queue) Empty() bool {
q.lock.Lock()
defer q.lock.Unlock()
return len(q.items) == 0
}
|
go
|
func (q *Queue) Empty() bool {
q.lock.Lock()
defer q.lock.Unlock()
return len(q.items) == 0
}
|
[
"func",
"(",
"q",
"*",
"Queue",
")",
"Empty",
"(",
")",
"bool",
"{",
"q",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"return",
"len",
"(",
"q",
".",
"items",
")",
"==",
"0",
"\n",
"}"
] |
// Empty returns a bool indicating if this bool is empty.
|
[
"Empty",
"returns",
"a",
"bool",
"indicating",
"if",
"this",
"bool",
"is",
"empty",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/queue.go#L311-L316
|
train
|
Workiva/go-datastructures
|
queue/queue.go
|
Len
|
func (q *Queue) Len() int64 {
q.lock.Lock()
defer q.lock.Unlock()
return int64(len(q.items))
}
|
go
|
func (q *Queue) Len() int64 {
q.lock.Lock()
defer q.lock.Unlock()
return int64(len(q.items))
}
|
[
"func",
"(",
"q",
"*",
"Queue",
")",
"Len",
"(",
")",
"int64",
"{",
"q",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"return",
"int64",
"(",
"len",
"(",
"q",
".",
"items",
")",
")",
"\n",
"}"
] |
// Len returns the number of items in this queue.
|
[
"Len",
"returns",
"the",
"number",
"of",
"items",
"in",
"this",
"queue",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/queue.go#L319-L324
|
train
|
Workiva/go-datastructures
|
queue/queue.go
|
Disposed
|
func (q *Queue) Disposed() bool {
q.lock.Lock()
defer q.lock.Unlock()
return q.disposed
}
|
go
|
func (q *Queue) Disposed() bool {
q.lock.Lock()
defer q.lock.Unlock()
return q.disposed
}
|
[
"func",
"(",
"q",
"*",
"Queue",
")",
"Disposed",
"(",
")",
"bool",
"{",
"q",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"return",
"q",
".",
"disposed",
"\n",
"}"
] |
// Disposed returns a bool indicating if this queue
// has had disposed called on it.
|
[
"Disposed",
"returns",
"a",
"bool",
"indicating",
"if",
"this",
"queue",
"has",
"had",
"disposed",
"called",
"on",
"it",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/queue.go#L328-L333
|
train
|
Workiva/go-datastructures
|
queue/queue.go
|
Dispose
|
func (q *Queue) Dispose() []interface{} {
q.lock.Lock()
defer q.lock.Unlock()
q.disposed = true
for _, waiter := range q.waiters {
waiter.response.Add(1)
select {
case waiter.ready <- true:
// release Poll immediately
default:
// ignore if it's a timeout or in the get
}
}
disposedItems := q.items
q.items = nil
q.waiters = nil
return disposedItems
}
|
go
|
func (q *Queue) Dispose() []interface{} {
q.lock.Lock()
defer q.lock.Unlock()
q.disposed = true
for _, waiter := range q.waiters {
waiter.response.Add(1)
select {
case waiter.ready <- true:
// release Poll immediately
default:
// ignore if it's a timeout or in the get
}
}
disposedItems := q.items
q.items = nil
q.waiters = nil
return disposedItems
}
|
[
"func",
"(",
"q",
"*",
"Queue",
")",
"Dispose",
"(",
")",
"[",
"]",
"interface",
"{",
"}",
"{",
"q",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"q",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n\n",
"q",
".",
"disposed",
"=",
"true",
"\n",
"for",
"_",
",",
"waiter",
":=",
"range",
"q",
".",
"waiters",
"{",
"waiter",
".",
"response",
".",
"Add",
"(",
"1",
")",
"\n",
"select",
"{",
"case",
"waiter",
".",
"ready",
"<-",
"true",
":",
"// release Poll immediately",
"default",
":",
"// ignore if it's a timeout or in the get",
"}",
"\n",
"}",
"\n\n",
"disposedItems",
":=",
"q",
".",
"items",
"\n\n",
"q",
".",
"items",
"=",
"nil",
"\n",
"q",
".",
"waiters",
"=",
"nil",
"\n\n",
"return",
"disposedItems",
"\n",
"}"
] |
// Dispose will dispose of this queue and returns
// the items disposed. Any subsequent calls to Get
// or Put will return an error.
|
[
"Dispose",
"will",
"dispose",
"of",
"this",
"queue",
"and",
"returns",
"the",
"items",
"disposed",
".",
"Any",
"subsequent",
"calls",
"to",
"Get",
"or",
"Put",
"will",
"return",
"an",
"error",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/queue/queue.go#L338-L359
|
train
|
Workiva/go-datastructures
|
trie/dtrie/dtrie.go
|
New
|
func New(hasher func(v interface{}) uint32) *Dtrie {
if hasher == nil {
hasher = defaultHasher
}
return &Dtrie{
root: emptyNode(0, 32),
hasher: hasher,
}
}
|
go
|
func New(hasher func(v interface{}) uint32) *Dtrie {
if hasher == nil {
hasher = defaultHasher
}
return &Dtrie{
root: emptyNode(0, 32),
hasher: hasher,
}
}
|
[
"func",
"New",
"(",
"hasher",
"func",
"(",
"v",
"interface",
"{",
"}",
")",
"uint32",
")",
"*",
"Dtrie",
"{",
"if",
"hasher",
"==",
"nil",
"{",
"hasher",
"=",
"defaultHasher",
"\n",
"}",
"\n",
"return",
"&",
"Dtrie",
"{",
"root",
":",
"emptyNode",
"(",
"0",
",",
"32",
")",
",",
"hasher",
":",
"hasher",
",",
"}",
"\n",
"}"
] |
// New creates an empty DTrie with the given hashing function.
// If nil is passed in, the default hashing function will be used.
|
[
"New",
"creates",
"an",
"empty",
"DTrie",
"with",
"the",
"given",
"hashing",
"function",
".",
"If",
"nil",
"is",
"passed",
"in",
"the",
"default",
"hashing",
"function",
"will",
"be",
"used",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/dtrie/dtrie.go#L62-L70
|
train
|
Workiva/go-datastructures
|
trie/dtrie/dtrie.go
|
Size
|
func (d *Dtrie) Size() (size int) {
for _ = range iterate(d.root, nil) {
size++
}
return size
}
|
go
|
func (d *Dtrie) Size() (size int) {
for _ = range iterate(d.root, nil) {
size++
}
return size
}
|
[
"func",
"(",
"d",
"*",
"Dtrie",
")",
"Size",
"(",
")",
"(",
"size",
"int",
")",
"{",
"for",
"_",
"=",
"range",
"iterate",
"(",
"d",
".",
"root",
",",
"nil",
")",
"{",
"size",
"++",
"\n",
"}",
"\n",
"return",
"size",
"\n",
"}"
] |
// Size returns the number of entries in the Dtrie.
|
[
"Size",
"returns",
"the",
"number",
"of",
"entries",
"in",
"the",
"Dtrie",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/dtrie/dtrie.go#L73-L78
|
train
|
Workiva/go-datastructures
|
trie/dtrie/dtrie.go
|
Get
|
func (d *Dtrie) Get(key interface{}) interface{} {
return get(d.root, d.hasher(key), key).Value()
}
|
go
|
func (d *Dtrie) Get(key interface{}) interface{} {
return get(d.root, d.hasher(key), key).Value()
}
|
[
"func",
"(",
"d",
"*",
"Dtrie",
")",
"Get",
"(",
"key",
"interface",
"{",
"}",
")",
"interface",
"{",
"}",
"{",
"return",
"get",
"(",
"d",
".",
"root",
",",
"d",
".",
"hasher",
"(",
"key",
")",
",",
"key",
")",
".",
"Value",
"(",
")",
"\n",
"}"
] |
// Get returns the value for the associated key or returns nil if the
// key does not exist.
|
[
"Get",
"returns",
"the",
"value",
"for",
"the",
"associated",
"key",
"or",
"returns",
"nil",
"if",
"the",
"key",
"does",
"not",
"exist",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/dtrie/dtrie.go#L82-L84
|
train
|
Workiva/go-datastructures
|
trie/dtrie/dtrie.go
|
Insert
|
func (d *Dtrie) Insert(key, value interface{}) *Dtrie {
root := insert(d.root, &entry{d.hasher(key), key, value})
return &Dtrie{root, d.hasher}
}
|
go
|
func (d *Dtrie) Insert(key, value interface{}) *Dtrie {
root := insert(d.root, &entry{d.hasher(key), key, value})
return &Dtrie{root, d.hasher}
}
|
[
"func",
"(",
"d",
"*",
"Dtrie",
")",
"Insert",
"(",
"key",
",",
"value",
"interface",
"{",
"}",
")",
"*",
"Dtrie",
"{",
"root",
":=",
"insert",
"(",
"d",
".",
"root",
",",
"&",
"entry",
"{",
"d",
".",
"hasher",
"(",
"key",
")",
",",
"key",
",",
"value",
"}",
")",
"\n",
"return",
"&",
"Dtrie",
"{",
"root",
",",
"d",
".",
"hasher",
"}",
"\n",
"}"
] |
// Insert adds a key value pair to the Dtrie, replacing the existing value if
// the key already exists and returns the resulting Dtrie.
|
[
"Insert",
"adds",
"a",
"key",
"value",
"pair",
"to",
"the",
"Dtrie",
"replacing",
"the",
"existing",
"value",
"if",
"the",
"key",
"already",
"exists",
"and",
"returns",
"the",
"resulting",
"Dtrie",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/dtrie/dtrie.go#L88-L91
|
train
|
Workiva/go-datastructures
|
trie/dtrie/dtrie.go
|
Remove
|
func (d *Dtrie) Remove(key interface{}) *Dtrie {
root := remove(d.root, d.hasher(key), key)
return &Dtrie{root, d.hasher}
}
|
go
|
func (d *Dtrie) Remove(key interface{}) *Dtrie {
root := remove(d.root, d.hasher(key), key)
return &Dtrie{root, d.hasher}
}
|
[
"func",
"(",
"d",
"*",
"Dtrie",
")",
"Remove",
"(",
"key",
"interface",
"{",
"}",
")",
"*",
"Dtrie",
"{",
"root",
":=",
"remove",
"(",
"d",
".",
"root",
",",
"d",
".",
"hasher",
"(",
"key",
")",
",",
"key",
")",
"\n",
"return",
"&",
"Dtrie",
"{",
"root",
",",
"d",
".",
"hasher",
"}",
"\n",
"}"
] |
// Remove deletes the value for the associated key if it exists and returns
// the resulting Dtrie.
|
[
"Remove",
"deletes",
"the",
"value",
"for",
"the",
"associated",
"key",
"if",
"it",
"exists",
"and",
"returns",
"the",
"resulting",
"Dtrie",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/dtrie/dtrie.go#L95-L98
|
train
|
Workiva/go-datastructures
|
btree/immutable/query.go
|
filter
|
func (t *Tr) filter(start, stop interface{}, n *Node, fn func(key *Key) bool) bool {
for iter := n.iter(t.config.Comparator, start, stop); iter.next(); {
id, _ := iter.value()
if !fn(id) {
return false
}
}
return true
}
|
go
|
func (t *Tr) filter(start, stop interface{}, n *Node, fn func(key *Key) bool) bool {
for iter := n.iter(t.config.Comparator, start, stop); iter.next(); {
id, _ := iter.value()
if !fn(id) {
return false
}
}
return true
}
|
[
"func",
"(",
"t",
"*",
"Tr",
")",
"filter",
"(",
"start",
",",
"stop",
"interface",
"{",
"}",
",",
"n",
"*",
"Node",
",",
"fn",
"func",
"(",
"key",
"*",
"Key",
")",
"bool",
")",
"bool",
"{",
"for",
"iter",
":=",
"n",
".",
"iter",
"(",
"t",
".",
"config",
".",
"Comparator",
",",
"start",
",",
"stop",
")",
";",
"iter",
".",
"next",
"(",
")",
";",
"{",
"id",
",",
"_",
":=",
"iter",
".",
"value",
"(",
")",
"\n",
"if",
"!",
"fn",
"(",
"id",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"true",
"\n",
"}"
] |
// filter performs an after fetch filtering of the values in the provided node.
// Due to the nature of the UB-Tree, we may get results in the node that
// aren't in the provided range. The returned list of keys is not necessarily
// in the correct row-major order.
|
[
"filter",
"performs",
"an",
"after",
"fetch",
"filtering",
"of",
"the",
"values",
"in",
"the",
"provided",
"node",
".",
"Due",
"to",
"the",
"nature",
"of",
"the",
"UB",
"-",
"Tree",
"we",
"may",
"get",
"results",
"in",
"the",
"node",
"that",
"aren",
"t",
"in",
"the",
"provided",
"range",
".",
"The",
"returned",
"list",
"of",
"keys",
"is",
"not",
"necessarily",
"in",
"the",
"correct",
"row",
"-",
"major",
"order",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/query.go#L92-L101
|
train
|
Workiva/go-datastructures
|
btree/immutable/query.go
|
iterativeFind
|
func (t *Tr) iterativeFind(value interface{}, id ID) (*path, error) {
if len(id) == 0 { // can't find a matching node
return nil, nil
}
path := &path{}
var n *Node
var err error
var i int
var key *Key
for {
n, err = t.contextOrCachedNode(id, t.mutable)
if err != nil {
return nil, err
}
key, i = n.searchKey(t.config.Comparator, value)
pb := &pathBundle{i: i, n: n}
path.append(pb)
if n.IsLeaf {
return path, nil
}
id = key.ID()
}
return path, nil
}
|
go
|
func (t *Tr) iterativeFind(value interface{}, id ID) (*path, error) {
if len(id) == 0 { // can't find a matching node
return nil, nil
}
path := &path{}
var n *Node
var err error
var i int
var key *Key
for {
n, err = t.contextOrCachedNode(id, t.mutable)
if err != nil {
return nil, err
}
key, i = n.searchKey(t.config.Comparator, value)
pb := &pathBundle{i: i, n: n}
path.append(pb)
if n.IsLeaf {
return path, nil
}
id = key.ID()
}
return path, nil
}
|
[
"func",
"(",
"t",
"*",
"Tr",
")",
"iterativeFind",
"(",
"value",
"interface",
"{",
"}",
",",
"id",
"ID",
")",
"(",
"*",
"path",
",",
"error",
")",
"{",
"if",
"len",
"(",
"id",
")",
"==",
"0",
"{",
"// can't find a matching node",
"return",
"nil",
",",
"nil",
"\n",
"}",
"\n\n",
"path",
":=",
"&",
"path",
"{",
"}",
"\n",
"var",
"n",
"*",
"Node",
"\n",
"var",
"err",
"error",
"\n",
"var",
"i",
"int",
"\n",
"var",
"key",
"*",
"Key",
"\n\n",
"for",
"{",
"n",
",",
"err",
"=",
"t",
".",
"contextOrCachedNode",
"(",
"id",
",",
"t",
".",
"mutable",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"key",
",",
"i",
"=",
"n",
".",
"searchKey",
"(",
"t",
".",
"config",
".",
"Comparator",
",",
"value",
")",
"\n\n",
"pb",
":=",
"&",
"pathBundle",
"{",
"i",
":",
"i",
",",
"n",
":",
"n",
"}",
"\n",
"path",
".",
"append",
"(",
"pb",
")",
"\n",
"if",
"n",
".",
"IsLeaf",
"{",
"return",
"path",
",",
"nil",
"\n",
"}",
"\n",
"id",
"=",
"key",
".",
"ID",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"path",
",",
"nil",
"\n",
"}"
] |
// iterativeFind searches for the node with the provided value. This
// is an iterative function and returns an error if there was a problem
// with persistence.
|
[
"iterativeFind",
"searches",
"for",
"the",
"node",
"with",
"the",
"provided",
"value",
".",
"This",
"is",
"an",
"iterative",
"function",
"and",
"returns",
"an",
"error",
"if",
"there",
"was",
"a",
"problem",
"with",
"persistence",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/btree/immutable/query.go#L138-L166
|
train
|
Workiva/go-datastructures
|
trie/xfast/xfast.go
|
isInternal
|
func isInternal(n *node) bool {
if n == nil {
return false
}
return n.entry == nil
}
|
go
|
func isInternal(n *node) bool {
if n == nil {
return false
}
return n.entry == nil
}
|
[
"func",
"isInternal",
"(",
"n",
"*",
"node",
")",
"bool",
"{",
"if",
"n",
"==",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"n",
".",
"entry",
"==",
"nil",
"\n",
"}"
] |
// isInternal returns a bool indicating if the provided
// node is an internal node, that is, non-leaf node.
|
[
"isInternal",
"returns",
"a",
"bool",
"indicating",
"if",
"the",
"provided",
"node",
"is",
"an",
"internal",
"node",
"that",
"is",
"non",
"-",
"leaf",
"node",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/xfast.go#L51-L56
|
train
|
Workiva/go-datastructures
|
trie/xfast/xfast.go
|
hasInternal
|
func hasInternal(n *node) bool {
return isInternal(n.children[0]) || isInternal(n.children[1])
}
|
go
|
func hasInternal(n *node) bool {
return isInternal(n.children[0]) || isInternal(n.children[1])
}
|
[
"func",
"hasInternal",
"(",
"n",
"*",
"node",
")",
"bool",
"{",
"return",
"isInternal",
"(",
"n",
".",
"children",
"[",
"0",
"]",
")",
"||",
"isInternal",
"(",
"n",
".",
"children",
"[",
"1",
"]",
")",
"\n",
"}"
] |
// hasInternal returns a bool indicating if the provided
// node has a child that is an internal node.
|
[
"hasInternal",
"returns",
"a",
"bool",
"indicating",
"if",
"the",
"provided",
"node",
"has",
"a",
"child",
"that",
"is",
"an",
"internal",
"node",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/xfast.go#L60-L62
|
train
|
Workiva/go-datastructures
|
trie/xfast/xfast.go
|
newNode
|
func newNode(parent *node, entry Entry) *node {
return &node{
children: [2]*node{},
entry: entry,
parent: parent,
}
}
|
go
|
func newNode(parent *node, entry Entry) *node {
return &node{
children: [2]*node{},
entry: entry,
parent: parent,
}
}
|
[
"func",
"newNode",
"(",
"parent",
"*",
"node",
",",
"entry",
"Entry",
")",
"*",
"node",
"{",
"return",
"&",
"node",
"{",
"children",
":",
"[",
"2",
"]",
"*",
"node",
"{",
"}",
",",
"entry",
":",
"entry",
",",
"parent",
":",
"parent",
",",
"}",
"\n",
"}"
] |
// newNode will allocate and initialize a newNode with the provided
// parent and entry. Parent should never be nil, but entry may be
// if constructing an internal node.
|
[
"newNode",
"will",
"allocate",
"and",
"initialize",
"a",
"newNode",
"with",
"the",
"provided",
"parent",
"and",
"entry",
".",
"Parent",
"should",
"never",
"be",
"nil",
"but",
"entry",
"may",
"be",
"if",
"constructing",
"an",
"internal",
"node",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/xfast.go#L131-L137
|
train
|
Workiva/go-datastructures
|
trie/xfast/xfast.go
|
binarySearchHashMaps
|
func binarySearchHashMaps(layers []map[uint64]*node, key uint64) (int, *node) {
low, high := 0, len(layers)-1
diff := 64 - len(layers)
var mid int
var node *node
for low <= high {
mid = (low + high) / 2
n, ok := layers[mid][key&masks[diff+mid]]
if ok {
node = n
low = mid + 1
} else {
high = mid - 1
}
}
return low, node
}
|
go
|
func binarySearchHashMaps(layers []map[uint64]*node, key uint64) (int, *node) {
low, high := 0, len(layers)-1
diff := 64 - len(layers)
var mid int
var node *node
for low <= high {
mid = (low + high) / 2
n, ok := layers[mid][key&masks[diff+mid]]
if ok {
node = n
low = mid + 1
} else {
high = mid - 1
}
}
return low, node
}
|
[
"func",
"binarySearchHashMaps",
"(",
"layers",
"[",
"]",
"map",
"[",
"uint64",
"]",
"*",
"node",
",",
"key",
"uint64",
")",
"(",
"int",
",",
"*",
"node",
")",
"{",
"low",
",",
"high",
":=",
"0",
",",
"len",
"(",
"layers",
")",
"-",
"1",
"\n",
"diff",
":=",
"64",
"-",
"len",
"(",
"layers",
")",
"\n",
"var",
"mid",
"int",
"\n",
"var",
"node",
"*",
"node",
"\n",
"for",
"low",
"<=",
"high",
"{",
"mid",
"=",
"(",
"low",
"+",
"high",
")",
"/",
"2",
"\n",
"n",
",",
"ok",
":=",
"layers",
"[",
"mid",
"]",
"[",
"key",
"&",
"masks",
"[",
"diff",
"+",
"mid",
"]",
"]",
"\n",
"if",
"ok",
"{",
"node",
"=",
"n",
"\n",
"low",
"=",
"mid",
"+",
"1",
"\n",
"}",
"else",
"{",
"high",
"=",
"mid",
"-",
"1",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"low",
",",
"node",
"\n",
"}"
] |
// binarySearchHashMaps will perform a binary search of the provided
// maps to return a node that matches the longest prefix of the provided
// key. This will return nil if a match could not be found, which would
// also return layer 0. Layer information is useful when determining the
// distance from the provided node to the leaves.
|
[
"binarySearchHashMaps",
"will",
"perform",
"a",
"binary",
"search",
"of",
"the",
"provided",
"maps",
"to",
"return",
"a",
"node",
"that",
"matches",
"the",
"longest",
"prefix",
"of",
"the",
"provided",
"key",
".",
"This",
"will",
"return",
"nil",
"if",
"a",
"match",
"could",
"not",
"be",
"found",
"which",
"would",
"also",
"return",
"layer",
"0",
".",
"Layer",
"information",
"is",
"useful",
"when",
"determining",
"the",
"distance",
"from",
"the",
"provided",
"node",
"to",
"the",
"leaves",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/xfast.go#L144-L161
|
train
|
Workiva/go-datastructures
|
trie/xfast/xfast.go
|
init
|
func (xft *XFastTrie) init(intType interface{}) {
bits := uint8(0)
switch intType.(type) {
case uint8:
bits = 8
case uint16:
bits = 16
case uint32:
bits = 32
case uint, uint64:
bits = 64
default:
// we'll panic with a bad value to the constructor.
panic(`Invalid universe size provided.`)
}
xft.layers = make([]map[uint64]*node, bits)
xft.bits = bits
xft.diff = 64 - bits
for i := uint8(0); i < bits; i++ {
xft.layers[i] = make(map[uint64]*node, 50) // we can obviously be more intelligent about this.
}
xft.num = 0
xft.root = newNode(nil, nil)
}
|
go
|
func (xft *XFastTrie) init(intType interface{}) {
bits := uint8(0)
switch intType.(type) {
case uint8:
bits = 8
case uint16:
bits = 16
case uint32:
bits = 32
case uint, uint64:
bits = 64
default:
// we'll panic with a bad value to the constructor.
panic(`Invalid universe size provided.`)
}
xft.layers = make([]map[uint64]*node, bits)
xft.bits = bits
xft.diff = 64 - bits
for i := uint8(0); i < bits; i++ {
xft.layers[i] = make(map[uint64]*node, 50) // we can obviously be more intelligent about this.
}
xft.num = 0
xft.root = newNode(nil, nil)
}
|
[
"func",
"(",
"xft",
"*",
"XFastTrie",
")",
"init",
"(",
"intType",
"interface",
"{",
"}",
")",
"{",
"bits",
":=",
"uint8",
"(",
"0",
")",
"\n",
"switch",
"intType",
".",
"(",
"type",
")",
"{",
"case",
"uint8",
":",
"bits",
"=",
"8",
"\n",
"case",
"uint16",
":",
"bits",
"=",
"16",
"\n",
"case",
"uint32",
":",
"bits",
"=",
"32",
"\n",
"case",
"uint",
",",
"uint64",
":",
"bits",
"=",
"64",
"\n",
"default",
":",
"// we'll panic with a bad value to the constructor.",
"panic",
"(",
"`Invalid universe size provided.`",
")",
"\n",
"}",
"\n\n",
"xft",
".",
"layers",
"=",
"make",
"(",
"[",
"]",
"map",
"[",
"uint64",
"]",
"*",
"node",
",",
"bits",
")",
"\n",
"xft",
".",
"bits",
"=",
"bits",
"\n",
"xft",
".",
"diff",
"=",
"64",
"-",
"bits",
"\n",
"for",
"i",
":=",
"uint8",
"(",
"0",
")",
";",
"i",
"<",
"bits",
";",
"i",
"++",
"{",
"xft",
".",
"layers",
"[",
"i",
"]",
"=",
"make",
"(",
"map",
"[",
"uint64",
"]",
"*",
"node",
",",
"50",
")",
"// we can obviously be more intelligent about this.",
"\n",
"}",
"\n",
"xft",
".",
"num",
"=",
"0",
"\n",
"xft",
".",
"root",
"=",
"newNode",
"(",
"nil",
",",
"nil",
")",
"\n",
"}"
] |
// init will initialize the XFastTrie with the provided byte-size.
// I'd prefer generics here, but it is what it is. We expect uints
// here when ints would perform just as well, but the public methods
// on the XFastTrie all expect uint64, so we expect a uint in the
// constructor for consistency's sake.
|
[
"init",
"will",
"initialize",
"the",
"XFastTrie",
"with",
"the",
"provided",
"byte",
"-",
"size",
".",
"I",
"d",
"prefer",
"generics",
"here",
"but",
"it",
"is",
"what",
"it",
"is",
".",
"We",
"expect",
"uints",
"here",
"when",
"ints",
"would",
"perform",
"just",
"as",
"well",
"but",
"the",
"public",
"methods",
"on",
"the",
"XFastTrie",
"all",
"expect",
"uint64",
"so",
"we",
"expect",
"a",
"uint",
"in",
"the",
"constructor",
"for",
"consistency",
"s",
"sake",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/xfast.go#L216-L240
|
train
|
Workiva/go-datastructures
|
trie/xfast/xfast.go
|
walkUpSuccessor
|
func (xft *XFastTrie) walkUpSuccessor(root, node, successor *node) {
n := successor.parent
for n != nil && n != root {
// we don't really want to overwrite existing internal nodes,
// or where the child is a leaf that is the successor
if !isInternal(n.children[0]) && n.children[0] != successor {
n.children[0] = node
}
n = n.parent
}
}
|
go
|
func (xft *XFastTrie) walkUpSuccessor(root, node, successor *node) {
n := successor.parent
for n != nil && n != root {
// we don't really want to overwrite existing internal nodes,
// or where the child is a leaf that is the successor
if !isInternal(n.children[0]) && n.children[0] != successor {
n.children[0] = node
}
n = n.parent
}
}
|
[
"func",
"(",
"xft",
"*",
"XFastTrie",
")",
"walkUpSuccessor",
"(",
"root",
",",
"node",
",",
"successor",
"*",
"node",
")",
"{",
"n",
":=",
"successor",
".",
"parent",
"\n",
"for",
"n",
"!=",
"nil",
"&&",
"n",
"!=",
"root",
"{",
"// we don't really want to overwrite existing internal nodes,",
"// or where the child is a leaf that is the successor",
"if",
"!",
"isInternal",
"(",
"n",
".",
"children",
"[",
"0",
"]",
")",
"&&",
"n",
".",
"children",
"[",
"0",
"]",
"!=",
"successor",
"{",
"n",
".",
"children",
"[",
"0",
"]",
"=",
"node",
"\n",
"}",
"\n",
"n",
"=",
"n",
".",
"parent",
"\n",
"}",
"\n",
"}"
] |
// walkUpSuccessor will walk up the successor branch setting
// the predecessor where possible. This breaks when a common
// ancestor between successor and node is found, ie, the root.
|
[
"walkUpSuccessor",
"will",
"walk",
"up",
"the",
"successor",
"branch",
"setting",
"the",
"predecessor",
"where",
"possible",
".",
"This",
"breaks",
"when",
"a",
"common",
"ancestor",
"between",
"successor",
"and",
"node",
"is",
"found",
"ie",
"the",
"root",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/xfast.go#L383-L393
|
train
|
Workiva/go-datastructures
|
trie/xfast/xfast.go
|
walkUpPredecessor
|
func (xft *XFastTrie) walkUpPredecessor(root, node, predecessor *node) {
n := predecessor.parent
for n != nil && n != root {
if !isInternal(n.children[1]) && n.children[1] != predecessor {
n.children[1] = node
}
n = n.parent
}
}
|
go
|
func (xft *XFastTrie) walkUpPredecessor(root, node, predecessor *node) {
n := predecessor.parent
for n != nil && n != root {
if !isInternal(n.children[1]) && n.children[1] != predecessor {
n.children[1] = node
}
n = n.parent
}
}
|
[
"func",
"(",
"xft",
"*",
"XFastTrie",
")",
"walkUpPredecessor",
"(",
"root",
",",
"node",
",",
"predecessor",
"*",
"node",
")",
"{",
"n",
":=",
"predecessor",
".",
"parent",
"\n",
"for",
"n",
"!=",
"nil",
"&&",
"n",
"!=",
"root",
"{",
"if",
"!",
"isInternal",
"(",
"n",
".",
"children",
"[",
"1",
"]",
")",
"&&",
"n",
".",
"children",
"[",
"1",
"]",
"!=",
"predecessor",
"{",
"n",
".",
"children",
"[",
"1",
"]",
"=",
"node",
"\n",
"}",
"\n",
"n",
"=",
"n",
".",
"parent",
"\n",
"}",
"\n",
"}"
] |
// walkUpPredecessor will walk up the predecessor branch setting
// the successor where possible. This breaks when a common
// ancestor between predecessor and node is found, ie, the root.
|
[
"walkUpPredecessor",
"will",
"walk",
"up",
"the",
"predecessor",
"branch",
"setting",
"the",
"successor",
"where",
"possible",
".",
"This",
"breaks",
"when",
"a",
"common",
"ancestor",
"between",
"predecessor",
"and",
"node",
"is",
"found",
"ie",
"the",
"root",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/xfast.go#L398-L406
|
train
|
Workiva/go-datastructures
|
trie/xfast/xfast.go
|
walkUpNode
|
func (xft *XFastTrie) walkUpNode(root, node, predecessor, successor *node) {
n := node.parent
for n != nil && n != root {
if !isInternal(n.children[1]) && n.children[1] != successor && n.children[1] != node {
n.children[1] = successor
}
if !isInternal(n.children[0]) && n.children[0] != predecessor && n.children[0] != node {
n.children[0] = predecessor
}
n = n.parent
}
}
|
go
|
func (xft *XFastTrie) walkUpNode(root, node, predecessor, successor *node) {
n := node.parent
for n != nil && n != root {
if !isInternal(n.children[1]) && n.children[1] != successor && n.children[1] != node {
n.children[1] = successor
}
if !isInternal(n.children[0]) && n.children[0] != predecessor && n.children[0] != node {
n.children[0] = predecessor
}
n = n.parent
}
}
|
[
"func",
"(",
"xft",
"*",
"XFastTrie",
")",
"walkUpNode",
"(",
"root",
",",
"node",
",",
"predecessor",
",",
"successor",
"*",
"node",
")",
"{",
"n",
":=",
"node",
".",
"parent",
"\n",
"for",
"n",
"!=",
"nil",
"&&",
"n",
"!=",
"root",
"{",
"if",
"!",
"isInternal",
"(",
"n",
".",
"children",
"[",
"1",
"]",
")",
"&&",
"n",
".",
"children",
"[",
"1",
"]",
"!=",
"successor",
"&&",
"n",
".",
"children",
"[",
"1",
"]",
"!=",
"node",
"{",
"n",
".",
"children",
"[",
"1",
"]",
"=",
"successor",
"\n",
"}",
"\n",
"if",
"!",
"isInternal",
"(",
"n",
".",
"children",
"[",
"0",
"]",
")",
"&&",
"n",
".",
"children",
"[",
"0",
"]",
"!=",
"predecessor",
"&&",
"n",
".",
"children",
"[",
"0",
"]",
"!=",
"node",
"{",
"n",
".",
"children",
"[",
"0",
"]",
"=",
"predecessor",
"\n",
"}",
"\n",
"n",
"=",
"n",
".",
"parent",
"\n",
"}",
"\n",
"}"
] |
// walkUpNode will walk up the newly created branch and set predecessor
// and successor where possible. If predecessor or successor are nil,
// this will set nil where possible.
|
[
"walkUpNode",
"will",
"walk",
"up",
"the",
"newly",
"created",
"branch",
"and",
"set",
"predecessor",
"and",
"successor",
"where",
"possible",
".",
"If",
"predecessor",
"or",
"successor",
"are",
"nil",
"this",
"will",
"set",
"nil",
"where",
"possible",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/xfast.go#L411-L422
|
train
|
Workiva/go-datastructures
|
trie/xfast/xfast.go
|
predecessor
|
func (xft *XFastTrie) predecessor(key uint64) *node {
if xft.root == nil || xft.max == nil { // no successor if no nodes
return nil
}
if key >= xft.max.entry.Key() {
return xft.max
}
if key < xft.min.entry.Key() {
return nil
}
n := xft.layers[xft.bits-1][key]
if n != nil {
return n
}
layer, n := binarySearchHashMaps(xft.layers, key)
if n == nil && layer > 1 {
return nil
} else if n == nil {
n = xft.root
}
if isInternal(n.children[0]) && isLeaf(n.children[1]) {
return n.children[1].children[0]
}
return n.children[0]
}
|
go
|
func (xft *XFastTrie) predecessor(key uint64) *node {
if xft.root == nil || xft.max == nil { // no successor if no nodes
return nil
}
if key >= xft.max.entry.Key() {
return xft.max
}
if key < xft.min.entry.Key() {
return nil
}
n := xft.layers[xft.bits-1][key]
if n != nil {
return n
}
layer, n := binarySearchHashMaps(xft.layers, key)
if n == nil && layer > 1 {
return nil
} else if n == nil {
n = xft.root
}
if isInternal(n.children[0]) && isLeaf(n.children[1]) {
return n.children[1].children[0]
}
return n.children[0]
}
|
[
"func",
"(",
"xft",
"*",
"XFastTrie",
")",
"predecessor",
"(",
"key",
"uint64",
")",
"*",
"node",
"{",
"if",
"xft",
".",
"root",
"==",
"nil",
"||",
"xft",
".",
"max",
"==",
"nil",
"{",
"// no successor if no nodes",
"return",
"nil",
"\n",
"}",
"\n\n",
"if",
"key",
">=",
"xft",
".",
"max",
".",
"entry",
".",
"Key",
"(",
")",
"{",
"return",
"xft",
".",
"max",
"\n",
"}",
"\n\n",
"if",
"key",
"<",
"xft",
".",
"min",
".",
"entry",
".",
"Key",
"(",
")",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"n",
":=",
"xft",
".",
"layers",
"[",
"xft",
".",
"bits",
"-",
"1",
"]",
"[",
"key",
"]",
"\n",
"if",
"n",
"!=",
"nil",
"{",
"return",
"n",
"\n",
"}",
"\n\n",
"layer",
",",
"n",
":=",
"binarySearchHashMaps",
"(",
"xft",
".",
"layers",
",",
"key",
")",
"\n",
"if",
"n",
"==",
"nil",
"&&",
"layer",
">",
"1",
"{",
"return",
"nil",
"\n",
"}",
"else",
"if",
"n",
"==",
"nil",
"{",
"n",
"=",
"xft",
".",
"root",
"\n",
"}",
"\n\n",
"if",
"isInternal",
"(",
"n",
".",
"children",
"[",
"0",
"]",
")",
"&&",
"isLeaf",
"(",
"n",
".",
"children",
"[",
"1",
"]",
")",
"{",
"return",
"n",
".",
"children",
"[",
"1",
"]",
".",
"children",
"[",
"0",
"]",
"\n",
"}",
"\n",
"return",
"n",
".",
"children",
"[",
"0",
"]",
"\n",
"}"
] |
// predecessor will find the node equal to or immediately less
// than the provided key.
|
[
"predecessor",
"will",
"find",
"the",
"node",
"equal",
"to",
"or",
"immediately",
"less",
"than",
"the",
"provided",
"key",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/xfast.go#L510-L539
|
train
|
Workiva/go-datastructures
|
trie/xfast/xfast.go
|
Iter
|
func (xft *XFastTrie) Iter(key uint64) *Iterator {
return &Iterator{
n: xft.successor(key),
first: true,
}
}
|
go
|
func (xft *XFastTrie) Iter(key uint64) *Iterator {
return &Iterator{
n: xft.successor(key),
first: true,
}
}
|
[
"func",
"(",
"xft",
"*",
"XFastTrie",
")",
"Iter",
"(",
"key",
"uint64",
")",
"*",
"Iterator",
"{",
"return",
"&",
"Iterator",
"{",
"n",
":",
"xft",
".",
"successor",
"(",
"key",
")",
",",
"first",
":",
"true",
",",
"}",
"\n",
"}"
] |
// Iter will return an iterator that will iterate over all values
// equal to or immediately greater than the provided key. Iterator
// will iterate successor relationships.
|
[
"Iter",
"will",
"return",
"an",
"iterator",
"that",
"will",
"iterate",
"over",
"all",
"values",
"equal",
"to",
"or",
"immediately",
"greater",
"than",
"the",
"provided",
"key",
".",
"Iterator",
"will",
"iterate",
"successor",
"relationships",
"."
] |
f07cbe3f82ca2fd6e5ab94afce65fe43319f675f
|
https://github.com/Workiva/go-datastructures/blob/f07cbe3f82ca2fd6e5ab94afce65fe43319f675f/trie/xfast/xfast.go#L601-L606
|
train
|
tucnak/telebot
|
api.go
|
Raw
|
func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
url := fmt.Sprintf("%s/bot%s/%s", b.URL, b.Token, method)
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(payload); err != nil {
return []byte{}, wrapSystem(err)
}
resp, err := b.client.Post(url, "application/json", &buf)
if err != nil {
return []byte{}, errors.Wrap(err, "http.Post failed")
}
resp.Close = true
defer resp.Body.Close()
json, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, wrapSystem(err)
}
return json, nil
}
|
go
|
func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) {
url := fmt.Sprintf("%s/bot%s/%s", b.URL, b.Token, method)
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(payload); err != nil {
return []byte{}, wrapSystem(err)
}
resp, err := b.client.Post(url, "application/json", &buf)
if err != nil {
return []byte{}, errors.Wrap(err, "http.Post failed")
}
resp.Close = true
defer resp.Body.Close()
json, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, wrapSystem(err)
}
return json, nil
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"Raw",
"(",
"method",
"string",
",",
"payload",
"interface",
"{",
"}",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"url",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"b",
".",
"URL",
",",
"b",
".",
"Token",
",",
"method",
")",
"\n\n",
"var",
"buf",
"bytes",
".",
"Buffer",
"\n",
"if",
"err",
":=",
"json",
".",
"NewEncoder",
"(",
"&",
"buf",
")",
".",
"Encode",
"(",
"payload",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"[",
"]",
"byte",
"{",
"}",
",",
"wrapSystem",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"resp",
",",
"err",
":=",
"b",
".",
"client",
".",
"Post",
"(",
"url",
",",
"\"",
"\"",
",",
"&",
"buf",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"[",
"]",
"byte",
"{",
"}",
",",
"errors",
".",
"Wrap",
"(",
"err",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"resp",
".",
"Close",
"=",
"true",
"\n",
"defer",
"resp",
".",
"Body",
".",
"Close",
"(",
")",
"\n",
"json",
",",
"err",
":=",
"ioutil",
".",
"ReadAll",
"(",
"resp",
".",
"Body",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"[",
"]",
"byte",
"{",
"}",
",",
"wrapSystem",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"json",
",",
"nil",
"\n",
"}"
] |
// Raw lets you call any method of Bot API manually.
|
[
"Raw",
"lets",
"you",
"call",
"any",
"method",
"of",
"Bot",
"API",
"manually",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/api.go#L21-L41
|
train
|
tucnak/telebot
|
message.go
|
IsForwarded
|
func (m *Message) IsForwarded() bool {
return m.OriginalSender != nil || m.OriginalChat != nil
}
|
go
|
func (m *Message) IsForwarded() bool {
return m.OriginalSender != nil || m.OriginalChat != nil
}
|
[
"func",
"(",
"m",
"*",
"Message",
")",
"IsForwarded",
"(",
")",
"bool",
"{",
"return",
"m",
".",
"OriginalSender",
"!=",
"nil",
"||",
"m",
".",
"OriginalChat",
"!=",
"nil",
"\n",
"}"
] |
// IsForwarded says whether message is forwarded copy of another
// message or not.
|
[
"IsForwarded",
"says",
"whether",
"message",
"is",
"forwarded",
"copy",
"of",
"another",
"message",
"or",
"not",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/message.go#L223-L225
|
train
|
tucnak/telebot
|
message.go
|
FromGroup
|
func (m *Message) FromGroup() bool {
return m.Chat.Type == ChatGroup || m.Chat.Type == ChatSuperGroup
}
|
go
|
func (m *Message) FromGroup() bool {
return m.Chat.Type == ChatGroup || m.Chat.Type == ChatSuperGroup
}
|
[
"func",
"(",
"m",
"*",
"Message",
")",
"FromGroup",
"(",
")",
"bool",
"{",
"return",
"m",
".",
"Chat",
".",
"Type",
"==",
"ChatGroup",
"||",
"m",
".",
"Chat",
".",
"Type",
"==",
"ChatSuperGroup",
"\n",
"}"
] |
// FromGroup returns true, if message came from a group OR
// a super group.
|
[
"FromGroup",
"returns",
"true",
"if",
"message",
"came",
"from",
"a",
"group",
"OR",
"a",
"super",
"group",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/message.go#L239-L241
|
train
|
tucnak/telebot
|
message.go
|
IsService
|
func (m *Message) IsService() bool {
fact := false
fact = fact || m.UserJoined != nil
fact = fact || len(m.UsersJoined) > 0
fact = fact || m.UserLeft != nil
fact = fact || m.NewGroupTitle != ""
fact = fact || m.NewGroupPhoto != nil
fact = fact || m.GroupPhotoDeleted
fact = fact || m.GroupCreated || m.SuperGroupCreated
fact = fact || (m.MigrateTo != m.MigrateFrom)
return fact
}
|
go
|
func (m *Message) IsService() bool {
fact := false
fact = fact || m.UserJoined != nil
fact = fact || len(m.UsersJoined) > 0
fact = fact || m.UserLeft != nil
fact = fact || m.NewGroupTitle != ""
fact = fact || m.NewGroupPhoto != nil
fact = fact || m.GroupPhotoDeleted
fact = fact || m.GroupCreated || m.SuperGroupCreated
fact = fact || (m.MigrateTo != m.MigrateFrom)
return fact
}
|
[
"func",
"(",
"m",
"*",
"Message",
")",
"IsService",
"(",
")",
"bool",
"{",
"fact",
":=",
"false",
"\n\n",
"fact",
"=",
"fact",
"||",
"m",
".",
"UserJoined",
"!=",
"nil",
"\n",
"fact",
"=",
"fact",
"||",
"len",
"(",
"m",
".",
"UsersJoined",
")",
">",
"0",
"\n",
"fact",
"=",
"fact",
"||",
"m",
".",
"UserLeft",
"!=",
"nil",
"\n",
"fact",
"=",
"fact",
"||",
"m",
".",
"NewGroupTitle",
"!=",
"\"",
"\"",
"\n",
"fact",
"=",
"fact",
"||",
"m",
".",
"NewGroupPhoto",
"!=",
"nil",
"\n",
"fact",
"=",
"fact",
"||",
"m",
".",
"GroupPhotoDeleted",
"\n",
"fact",
"=",
"fact",
"||",
"m",
".",
"GroupCreated",
"||",
"m",
".",
"SuperGroupCreated",
"\n",
"fact",
"=",
"fact",
"||",
"(",
"m",
".",
"MigrateTo",
"!=",
"m",
".",
"MigrateFrom",
")",
"\n\n",
"return",
"fact",
"\n",
"}"
] |
// IsService returns true, if message is a service message,
// returns false otherwise.
//
// Service messages are automatically sent messages, which
// typically occur on some global action. For instance, when
// anyone leaves the chat or chat title changes.
|
[
"IsService",
"returns",
"true",
"if",
"message",
"is",
"a",
"service",
"message",
"returns",
"false",
"otherwise",
".",
"Service",
"messages",
"are",
"automatically",
"sent",
"messages",
"which",
"typically",
"occur",
"on",
"some",
"global",
"action",
".",
"For",
"instance",
"when",
"anyone",
"leaves",
"the",
"chat",
"or",
"chat",
"title",
"changes",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/message.go#L254-L267
|
train
|
tucnak/telebot
|
media.go
|
UnmarshalJSON
|
func (p *Photo) UnmarshalJSON(jsonStr []byte) error {
var hq photoSize
if jsonStr[0] == '{' {
if err := json.Unmarshal(jsonStr, &hq); err != nil {
return err
}
} else {
var sizes []photoSize
if err := json.Unmarshal(jsonStr, &sizes); err != nil {
return err
}
hq = sizes[len(sizes)-1]
}
p.File = hq.File
p.Width = hq.Width
p.Height = hq.Height
return nil
}
|
go
|
func (p *Photo) UnmarshalJSON(jsonStr []byte) error {
var hq photoSize
if jsonStr[0] == '{' {
if err := json.Unmarshal(jsonStr, &hq); err != nil {
return err
}
} else {
var sizes []photoSize
if err := json.Unmarshal(jsonStr, &sizes); err != nil {
return err
}
hq = sizes[len(sizes)-1]
}
p.File = hq.File
p.Width = hq.Width
p.Height = hq.Height
return nil
}
|
[
"func",
"(",
"p",
"*",
"Photo",
")",
"UnmarshalJSON",
"(",
"jsonStr",
"[",
"]",
"byte",
")",
"error",
"{",
"var",
"hq",
"photoSize",
"\n\n",
"if",
"jsonStr",
"[",
"0",
"]",
"==",
"'{'",
"{",
"if",
"err",
":=",
"json",
".",
"Unmarshal",
"(",
"jsonStr",
",",
"&",
"hq",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"else",
"{",
"var",
"sizes",
"[",
"]",
"photoSize",
"\n\n",
"if",
"err",
":=",
"json",
".",
"Unmarshal",
"(",
"jsonStr",
",",
"&",
"sizes",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"hq",
"=",
"sizes",
"[",
"len",
"(",
"sizes",
")",
"-",
"1",
"]",
"\n",
"}",
"\n\n",
"p",
".",
"File",
"=",
"hq",
".",
"File",
"\n",
"p",
".",
"Width",
"=",
"hq",
".",
"Width",
"\n",
"p",
".",
"Height",
"=",
"hq",
".",
"Height",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// UnmarshalJSON is custom unmarshaller required to abstract
// away the hassle of treating different thumbnail sizes.
// Instead, Telebot chooses the hi-res one and just sticks to
// it.
//
// I really do find it a beautiful solution.
|
[
"UnmarshalJSON",
"is",
"custom",
"unmarshaller",
"required",
"to",
"abstract",
"away",
"the",
"hassle",
"of",
"treating",
"different",
"thumbnail",
"sizes",
".",
"Instead",
"Telebot",
"chooses",
"the",
"hi",
"-",
"res",
"one",
"and",
"just",
"sticks",
"to",
"it",
".",
"I",
"really",
"do",
"find",
"it",
"a",
"beautiful",
"solution",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/media.go#L50-L72
|
train
|
tucnak/telebot
|
webhook.go
|
ServeHTTP
|
func (h *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var update Update
err := json.NewDecoder(r.Body).Decode(&update)
if err != nil {
h.bot.debug(fmt.Errorf("cannot decode update: %v", err))
return
}
h.dest <- update
}
|
go
|
func (h *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var update Update
err := json.NewDecoder(r.Body).Decode(&update)
if err != nil {
h.bot.debug(fmt.Errorf("cannot decode update: %v", err))
return
}
h.dest <- update
}
|
[
"func",
"(",
"h",
"*",
"Webhook",
")",
"ServeHTTP",
"(",
"w",
"http",
".",
"ResponseWriter",
",",
"r",
"*",
"http",
".",
"Request",
")",
"{",
"var",
"update",
"Update",
"\n",
"err",
":=",
"json",
".",
"NewDecoder",
"(",
"r",
".",
"Body",
")",
".",
"Decode",
"(",
"&",
"update",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"h",
".",
"bot",
".",
"debug",
"(",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
")",
"\n",
"return",
"\n",
"}",
"\n",
"h",
".",
"dest",
"<-",
"update",
"\n",
"}"
] |
// The handler simply reads the update from the body of the requests
// and writes them to the update channel.
|
[
"The",
"handler",
"simply",
"reads",
"the",
"update",
"from",
"the",
"body",
"of",
"the",
"requests",
"and",
"writes",
"them",
"to",
"the",
"update",
"channel",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/webhook.go#L142-L150
|
train
|
tucnak/telebot
|
admin.go
|
AdminRights
|
func AdminRights() Rights {
return Rights{
true, true, true, true, true, // 1-5
true, true, true, true, true, // 6-10
true, true, true} // 11-13
}
|
go
|
func AdminRights() Rights {
return Rights{
true, true, true, true, true, // 1-5
true, true, true, true, true, // 6-10
true, true, true} // 11-13
}
|
[
"func",
"AdminRights",
"(",
")",
"Rights",
"{",
"return",
"Rights",
"{",
"true",
",",
"true",
",",
"true",
",",
"true",
",",
"true",
",",
"// 1-5",
"true",
",",
"true",
",",
"true",
",",
"true",
",",
"true",
",",
"// 6-10",
"true",
",",
"true",
",",
"true",
"}",
"// 11-13",
"\n",
"}"
] |
// AdminRights could be used to promote user to admin.
|
[
"AdminRights",
"could",
"be",
"used",
"to",
"promote",
"user",
"to",
"admin",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/admin.go#L45-L50
|
train
|
tucnak/telebot
|
admin.go
|
Ban
|
func (b *Bot) Ban(chat *Chat, member *ChatMember) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"user_id": member.User.Recipient(),
"until_date": strconv.FormatInt(member.RestrictedUntil, 10),
}
respJSON, err := b.Raw("kickChatMember", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
go
|
func (b *Bot) Ban(chat *Chat, member *ChatMember) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"user_id": member.User.Recipient(),
"until_date": strconv.FormatInt(member.RestrictedUntil, 10),
}
respJSON, err := b.Raw("kickChatMember", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"Ban",
"(",
"chat",
"*",
"Chat",
",",
"member",
"*",
"ChatMember",
")",
"error",
"{",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"chat",
".",
"Recipient",
"(",
")",
",",
"\"",
"\"",
":",
"member",
".",
"User",
".",
"Recipient",
"(",
")",
",",
"\"",
"\"",
":",
"strconv",
".",
"FormatInt",
"(",
"member",
".",
"RestrictedUntil",
",",
"10",
")",
",",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"extractOkResponse",
"(",
"respJSON",
")",
"\n",
"}"
] |
// Ban will ban user from chat until `member.RestrictedUntil`.
|
[
"Ban",
"will",
"ban",
"user",
"from",
"chat",
"until",
"member",
".",
"RestrictedUntil",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/admin.go#L58-L71
|
train
|
tucnak/telebot
|
admin.go
|
Unban
|
func (b *Bot) Unban(chat *Chat, user *User) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"user_id": user.Recipient(),
}
respJSON, err := b.Raw("unbanChatMember", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
go
|
func (b *Bot) Unban(chat *Chat, user *User) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"user_id": user.Recipient(),
}
respJSON, err := b.Raw("unbanChatMember", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"Unban",
"(",
"chat",
"*",
"Chat",
",",
"user",
"*",
"User",
")",
"error",
"{",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"chat",
".",
"Recipient",
"(",
")",
",",
"\"",
"\"",
":",
"user",
".",
"Recipient",
"(",
")",
",",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"extractOkResponse",
"(",
"respJSON",
")",
"\n",
"}"
] |
// Unban will unban user from chat, who would have thought eh?
|
[
"Unban",
"will",
"unban",
"user",
"from",
"chat",
"who",
"would",
"have",
"thought",
"eh?"
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/admin.go#L74-L86
|
train
|
tucnak/telebot
|
admin.go
|
AdminsOf
|
func (b *Bot) AdminsOf(chat *Chat) ([]ChatMember, error) {
params := map[string]string{
"chat_id": chat.Recipient(),
}
respJSON, err := b.Raw("getChatAdministrators", params)
if err != nil {
return nil, err
}
var resp struct {
Ok bool
Result []ChatMember
Description string `json:"description"`
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return nil, errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
}
return resp.Result, nil
}
|
go
|
func (b *Bot) AdminsOf(chat *Chat) ([]ChatMember, error) {
params := map[string]string{
"chat_id": chat.Recipient(),
}
respJSON, err := b.Raw("getChatAdministrators", params)
if err != nil {
return nil, err
}
var resp struct {
Ok bool
Result []ChatMember
Description string `json:"description"`
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return nil, errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
}
return resp.Result, nil
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"AdminsOf",
"(",
"chat",
"*",
"Chat",
")",
"(",
"[",
"]",
"ChatMember",
",",
"error",
")",
"{",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"chat",
".",
"Recipient",
"(",
")",
",",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"var",
"resp",
"struct",
"{",
"Ok",
"bool",
"\n",
"Result",
"[",
"]",
"ChatMember",
"\n",
"Description",
"string",
"`json:\"description\"`",
"\n",
"}",
"\n\n",
"err",
"=",
"json",
".",
"Unmarshal",
"(",
"respJSON",
",",
"&",
"resp",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"errors",
".",
"Wrap",
"(",
"err",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"resp",
".",
"Ok",
"{",
"return",
"nil",
",",
"errors",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"resp",
".",
"Description",
")",
"\n",
"}",
"\n\n",
"return",
"resp",
".",
"Result",
",",
"nil",
"\n",
"}"
] |
// AdminsOf return a member list of chat admins.
//
// On success, returns an Array of ChatMember objects that
// contains information about all chat administrators except other bots.
// If the chat is a group or a supergroup and
// no administrators were appointed, only the creator will be returned.
|
[
"AdminsOf",
"return",
"a",
"member",
"list",
"of",
"chat",
"admins",
".",
"On",
"success",
"returns",
"an",
"Array",
"of",
"ChatMember",
"objects",
"that",
"contains",
"information",
"about",
"all",
"chat",
"administrators",
"except",
"other",
"bots",
".",
"If",
"the",
"chat",
"is",
"a",
"group",
"or",
"a",
"supergroup",
"and",
"no",
"administrators",
"were",
"appointed",
"only",
"the",
"creator",
"will",
"be",
"returned",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/admin.go#L150-L176
|
train
|
tucnak/telebot
|
poller.go
|
NewMiddlewarePoller
|
func NewMiddlewarePoller(original Poller, filter func(*Update) bool) *MiddlewarePoller {
return &MiddlewarePoller{
Poller: original,
Filter: filter,
}
}
|
go
|
func NewMiddlewarePoller(original Poller, filter func(*Update) bool) *MiddlewarePoller {
return &MiddlewarePoller{
Poller: original,
Filter: filter,
}
}
|
[
"func",
"NewMiddlewarePoller",
"(",
"original",
"Poller",
",",
"filter",
"func",
"(",
"*",
"Update",
")",
"bool",
")",
"*",
"MiddlewarePoller",
"{",
"return",
"&",
"MiddlewarePoller",
"{",
"Poller",
":",
"original",
",",
"Filter",
":",
"filter",
",",
"}",
"\n",
"}"
] |
// NewMiddlewarePoller wait for it... constructs a new middleware poller.
|
[
"NewMiddlewarePoller",
"wait",
"for",
"it",
"...",
"constructs",
"a",
"new",
"middleware",
"poller",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/poller.go#L41-L46
|
train
|
tucnak/telebot
|
poller.go
|
Poll
|
func (p *MiddlewarePoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
cap := 1
if p.Capacity > 1 {
cap = p.Capacity
}
middle := make(chan Update, cap)
stopPoller := make(chan struct{})
go p.Poller.Poll(b, middle, stopPoller)
for {
select {
// call to stop
case <-stop:
stopPoller <- struct{}{}
// poller is done
case <-stopPoller:
close(stop)
return
case upd := <-middle:
if p.Filter(&upd) {
dest <- upd
}
}
}
}
|
go
|
func (p *MiddlewarePoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
cap := 1
if p.Capacity > 1 {
cap = p.Capacity
}
middle := make(chan Update, cap)
stopPoller := make(chan struct{})
go p.Poller.Poll(b, middle, stopPoller)
for {
select {
// call to stop
case <-stop:
stopPoller <- struct{}{}
// poller is done
case <-stopPoller:
close(stop)
return
case upd := <-middle:
if p.Filter(&upd) {
dest <- upd
}
}
}
}
|
[
"func",
"(",
"p",
"*",
"MiddlewarePoller",
")",
"Poll",
"(",
"b",
"*",
"Bot",
",",
"dest",
"chan",
"Update",
",",
"stop",
"chan",
"struct",
"{",
"}",
")",
"{",
"cap",
":=",
"1",
"\n",
"if",
"p",
".",
"Capacity",
">",
"1",
"{",
"cap",
"=",
"p",
".",
"Capacity",
"\n",
"}",
"\n\n",
"middle",
":=",
"make",
"(",
"chan",
"Update",
",",
"cap",
")",
"\n",
"stopPoller",
":=",
"make",
"(",
"chan",
"struct",
"{",
"}",
")",
"\n\n",
"go",
"p",
".",
"Poller",
".",
"Poll",
"(",
"b",
",",
"middle",
",",
"stopPoller",
")",
"\n\n",
"for",
"{",
"select",
"{",
"// call to stop",
"case",
"<-",
"stop",
":",
"stopPoller",
"<-",
"struct",
"{",
"}",
"{",
"}",
"\n\n",
"// poller is done",
"case",
"<-",
"stopPoller",
":",
"close",
"(",
"stop",
")",
"\n",
"return",
"\n\n",
"case",
"upd",
":=",
"<-",
"middle",
":",
"if",
"p",
".",
"Filter",
"(",
"&",
"upd",
")",
"{",
"dest",
"<-",
"upd",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Poll sieves updates through middleware filter.
|
[
"Poll",
"sieves",
"updates",
"through",
"middleware",
"filter",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/poller.go#L49-L77
|
train
|
tucnak/telebot
|
poller.go
|
Poll
|
func (p *LongPoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
go func(stop chan struct{}) {
<-stop
close(stop)
}(stop)
for {
updates, err := b.getUpdates(p.LastUpdateID+1, p.Timeout)
if err != nil {
b.debug(ErrCouldNotUpdate)
continue
}
for _, update := range updates {
p.LastUpdateID = update.ID
dest <- update
}
}
}
|
go
|
func (p *LongPoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
go func(stop chan struct{}) {
<-stop
close(stop)
}(stop)
for {
updates, err := b.getUpdates(p.LastUpdateID+1, p.Timeout)
if err != nil {
b.debug(ErrCouldNotUpdate)
continue
}
for _, update := range updates {
p.LastUpdateID = update.ID
dest <- update
}
}
}
|
[
"func",
"(",
"p",
"*",
"LongPoller",
")",
"Poll",
"(",
"b",
"*",
"Bot",
",",
"dest",
"chan",
"Update",
",",
"stop",
"chan",
"struct",
"{",
"}",
")",
"{",
"go",
"func",
"(",
"stop",
"chan",
"struct",
"{",
"}",
")",
"{",
"<-",
"stop",
"\n",
"close",
"(",
"stop",
")",
"\n",
"}",
"(",
"stop",
")",
"\n\n",
"for",
"{",
"updates",
",",
"err",
":=",
"b",
".",
"getUpdates",
"(",
"p",
".",
"LastUpdateID",
"+",
"1",
",",
"p",
".",
"Timeout",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"b",
".",
"debug",
"(",
"ErrCouldNotUpdate",
")",
"\n",
"continue",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"update",
":=",
"range",
"updates",
"{",
"p",
".",
"LastUpdateID",
"=",
"update",
".",
"ID",
"\n",
"dest",
"<-",
"update",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Poll does long polling.
|
[
"Poll",
"does",
"long",
"polling",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/poller.go#L87-L106
|
train
|
tucnak/telebot
|
inline.go
|
MarshalJSON
|
func (results Results) MarshalJSON() ([]byte, error) {
for _, result := range results {
if result.ResultID() == "" {
result.SetResultID(fmt.Sprintf("%d", &result))
}
if err := inferIQR(result); err != nil {
return nil, err
}
}
return json.Marshal([]Result(results))
}
|
go
|
func (results Results) MarshalJSON() ([]byte, error) {
for _, result := range results {
if result.ResultID() == "" {
result.SetResultID(fmt.Sprintf("%d", &result))
}
if err := inferIQR(result); err != nil {
return nil, err
}
}
return json.Marshal([]Result(results))
}
|
[
"func",
"(",
"results",
"Results",
")",
"MarshalJSON",
"(",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"for",
"_",
",",
"result",
":=",
"range",
"results",
"{",
"if",
"result",
".",
"ResultID",
"(",
")",
"==",
"\"",
"\"",
"{",
"result",
".",
"SetResultID",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"&",
"result",
")",
")",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"inferIQR",
"(",
"result",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"json",
".",
"Marshal",
"(",
"[",
"]",
"Result",
"(",
"results",
")",
")",
"\n",
"}"
] |
// MarshalJSON makes sure IQRs have proper IDs and Type variables set.
|
[
"MarshalJSON",
"makes",
"sure",
"IQRs",
"have",
"proper",
"IDs",
"and",
"Type",
"variables",
"set",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/inline.go#L75-L87
|
train
|
tucnak/telebot
|
filters.go
|
Add
|
func (c *Chain) Add(filter interface{}) {
switch filter.(type) {
case Filter:
break
case FilterFunc:
break
case func(*Update) bool:
break
default:
panic("telebot: unsupported filter type")
}
c.Filters = append(c.Filters, filter)
}
|
go
|
func (c *Chain) Add(filter interface{}) {
switch filter.(type) {
case Filter:
break
case FilterFunc:
break
case func(*Update) bool:
break
default:
panic("telebot: unsupported filter type")
}
c.Filters = append(c.Filters, filter)
}
|
[
"func",
"(",
"c",
"*",
"Chain",
")",
"Add",
"(",
"filter",
"interface",
"{",
"}",
")",
"{",
"switch",
"filter",
".",
"(",
"type",
")",
"{",
"case",
"Filter",
":",
"break",
"\n",
"case",
"FilterFunc",
":",
"break",
"\n",
"case",
"func",
"(",
"*",
"Update",
")",
"bool",
":",
"break",
"\n",
"default",
":",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"c",
".",
"Filters",
"=",
"append",
"(",
"c",
".",
"Filters",
",",
"filter",
")",
"\n",
"}"
] |
// Add accepts either Filter interface or FilterFunc
|
[
"Add",
"accepts",
"either",
"Filter",
"interface",
"or",
"FilterFunc"
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/filters.go#L53-L66
|
train
|
tucnak/telebot
|
bot.go
|
NewBot
|
func NewBot(pref Settings) (*Bot, error) {
if pref.Updates == 0 {
pref.Updates = 100
}
client := pref.Client
if client == nil {
client = http.DefaultClient
}
if pref.URL == "" {
pref.URL = DefaultApiURL
}
bot := &Bot{
Token: pref.Token,
URL: pref.URL,
Updates: make(chan Update, pref.Updates),
Poller: pref.Poller,
handlers: make(map[string]interface{}),
stop: make(chan struct{}),
reporter: pref.Reporter,
client: client,
}
user, err := bot.getMe()
if err != nil {
return nil, err
}
bot.Me = user
return bot, nil
}
|
go
|
func NewBot(pref Settings) (*Bot, error) {
if pref.Updates == 0 {
pref.Updates = 100
}
client := pref.Client
if client == nil {
client = http.DefaultClient
}
if pref.URL == "" {
pref.URL = DefaultApiURL
}
bot := &Bot{
Token: pref.Token,
URL: pref.URL,
Updates: make(chan Update, pref.Updates),
Poller: pref.Poller,
handlers: make(map[string]interface{}),
stop: make(chan struct{}),
reporter: pref.Reporter,
client: client,
}
user, err := bot.getMe()
if err != nil {
return nil, err
}
bot.Me = user
return bot, nil
}
|
[
"func",
"NewBot",
"(",
"pref",
"Settings",
")",
"(",
"*",
"Bot",
",",
"error",
")",
"{",
"if",
"pref",
".",
"Updates",
"==",
"0",
"{",
"pref",
".",
"Updates",
"=",
"100",
"\n",
"}",
"\n\n",
"client",
":=",
"pref",
".",
"Client",
"\n",
"if",
"client",
"==",
"nil",
"{",
"client",
"=",
"http",
".",
"DefaultClient",
"\n",
"}",
"\n\n",
"if",
"pref",
".",
"URL",
"==",
"\"",
"\"",
"{",
"pref",
".",
"URL",
"=",
"DefaultApiURL",
"\n",
"}",
"\n\n",
"bot",
":=",
"&",
"Bot",
"{",
"Token",
":",
"pref",
".",
"Token",
",",
"URL",
":",
"pref",
".",
"URL",
",",
"Updates",
":",
"make",
"(",
"chan",
"Update",
",",
"pref",
".",
"Updates",
")",
",",
"Poller",
":",
"pref",
".",
"Poller",
",",
"handlers",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
")",
",",
"stop",
":",
"make",
"(",
"chan",
"struct",
"{",
"}",
")",
",",
"reporter",
":",
"pref",
".",
"Reporter",
",",
"client",
":",
"client",
",",
"}",
"\n\n",
"user",
",",
"err",
":=",
"bot",
".",
"getMe",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"bot",
".",
"Me",
"=",
"user",
"\n",
"return",
"bot",
",",
"nil",
"\n",
"}"
] |
// NewBot does try to build a Bot with token `token`, which
// is a secret API key assigned to particular bot.
|
[
"NewBot",
"does",
"try",
"to",
"build",
"a",
"Bot",
"with",
"token",
"token",
"which",
"is",
"a",
"secret",
"API",
"key",
"assigned",
"to",
"particular",
"bot",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L18-L51
|
train
|
tucnak/telebot
|
bot.go
|
EditCaption
|
func (b *Bot) EditCaption(originalMsg Editable, caption string) (*Message, error) {
messageID, chatID := originalMsg.MessageSig()
params := map[string]string{"caption": caption}
// if inline message
if chatID == 0 {
params["inline_message_id"] = messageID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = messageID
}
respJSON, err := b.Raw("editMessageCaption", params)
if err != nil {
return nil, err
}
return extractMsgResponse(respJSON)
}
|
go
|
func (b *Bot) EditCaption(originalMsg Editable, caption string) (*Message, error) {
messageID, chatID := originalMsg.MessageSig()
params := map[string]string{"caption": caption}
// if inline message
if chatID == 0 {
params["inline_message_id"] = messageID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = messageID
}
respJSON, err := b.Raw("editMessageCaption", params)
if err != nil {
return nil, err
}
return extractMsgResponse(respJSON)
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"EditCaption",
"(",
"originalMsg",
"Editable",
",",
"caption",
"string",
")",
"(",
"*",
"Message",
",",
"error",
")",
"{",
"messageID",
",",
"chatID",
":=",
"originalMsg",
".",
"MessageSig",
"(",
")",
"\n\n",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"caption",
"}",
"\n\n",
"// if inline message",
"if",
"chatID",
"==",
"0",
"{",
"params",
"[",
"\"",
"\"",
"]",
"=",
"messageID",
"\n",
"}",
"else",
"{",
"params",
"[",
"\"",
"\"",
"]",
"=",
"strconv",
".",
"FormatInt",
"(",
"chatID",
",",
"10",
")",
"\n",
"params",
"[",
"\"",
"\"",
"]",
"=",
"messageID",
"\n",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"extractMsgResponse",
"(",
"respJSON",
")",
"\n",
"}"
] |
// EditCaption used to edit already sent photo caption with known recepient and message id.
//
// On success, returns edited message object
|
[
"EditCaption",
"used",
"to",
"edit",
"already",
"sent",
"photo",
"caption",
"with",
"known",
"recepient",
"and",
"message",
"id",
".",
"On",
"success",
"returns",
"edited",
"message",
"object"
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L718-L737
|
train
|
tucnak/telebot
|
bot.go
|
Notify
|
func (b *Bot) Notify(recipient Recipient, action ChatAction) error {
params := map[string]string{
"chat_id": recipient.Recipient(),
"action": string(action),
}
respJSON, err := b.Raw("sendChatAction", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
go
|
func (b *Bot) Notify(recipient Recipient, action ChatAction) error {
params := map[string]string{
"chat_id": recipient.Recipient(),
"action": string(action),
}
respJSON, err := b.Raw("sendChatAction", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"Notify",
"(",
"recipient",
"Recipient",
",",
"action",
"ChatAction",
")",
"error",
"{",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"recipient",
".",
"Recipient",
"(",
")",
",",
"\"",
"\"",
":",
"string",
"(",
"action",
")",
",",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"extractOkResponse",
"(",
"respJSON",
")",
"\n",
"}"
] |
// Notify updates the chat action for recipient.
//
// Chat action is a status message that recipient would see where
// you typically see "Harry is typing" status message. The only
// difference is that bots' chat actions live only for 5 seconds
// and die just once the client recieves a message from the bot.
//
// Currently, Telegram supports only a narrow range of possible
// actions, these are aligned as constants of this package.
|
[
"Notify",
"updates",
"the",
"chat",
"action",
"for",
"recipient",
".",
"Chat",
"action",
"is",
"a",
"status",
"message",
"that",
"recipient",
"would",
"see",
"where",
"you",
"typically",
"see",
"Harry",
"is",
"typing",
"status",
"message",
".",
"The",
"only",
"difference",
"is",
"that",
"bots",
"chat",
"actions",
"live",
"only",
"for",
"5",
"seconds",
"and",
"die",
"just",
"once",
"the",
"client",
"recieves",
"a",
"message",
"from",
"the",
"bot",
".",
"Currently",
"Telegram",
"supports",
"only",
"a",
"narrow",
"range",
"of",
"possible",
"actions",
"these",
"are",
"aligned",
"as",
"constants",
"of",
"this",
"package",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L900-L912
|
train
|
tucnak/telebot
|
bot.go
|
Accept
|
func (b *Bot) Accept(query *PreCheckoutQuery, errorMessage ...string) error {
params := map[string]string{
"pre_checkout_query_id": query.ID,
}
if len(errorMessage) == 0 {
params["ok"] = "True"
} else {
params["ok"] = "False"
params["error_message"] = errorMessage[0]
}
respJSON, err := b.Raw("answerPreCheckoutQuery", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
go
|
func (b *Bot) Accept(query *PreCheckoutQuery, errorMessage ...string) error {
params := map[string]string{
"pre_checkout_query_id": query.ID,
}
if len(errorMessage) == 0 {
params["ok"] = "True"
} else {
params["ok"] = "False"
params["error_message"] = errorMessage[0]
}
respJSON, err := b.Raw("answerPreCheckoutQuery", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"Accept",
"(",
"query",
"*",
"PreCheckoutQuery",
",",
"errorMessage",
"...",
"string",
")",
"error",
"{",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"query",
".",
"ID",
",",
"}",
"\n\n",
"if",
"len",
"(",
"errorMessage",
")",
"==",
"0",
"{",
"params",
"[",
"\"",
"\"",
"]",
"=",
"\"",
"\"",
"\n",
"}",
"else",
"{",
"params",
"[",
"\"",
"\"",
"]",
"=",
"\"",
"\"",
"\n",
"params",
"[",
"\"",
"\"",
"]",
"=",
"errorMessage",
"[",
"0",
"]",
"\n",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"extractOkResponse",
"(",
"respJSON",
")",
"\n",
"}"
] |
// Accept finalizes the deal.
|
[
"Accept",
"finalizes",
"the",
"deal",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L915-L933
|
train
|
tucnak/telebot
|
bot.go
|
Answer
|
func (b *Bot) Answer(query *Query, response *QueryResponse) error {
response.QueryID = query.ID
for _, result := range response.Results {
result.Process()
}
respJSON, err := b.Raw("answerInlineQuery", response)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
go
|
func (b *Bot) Answer(query *Query, response *QueryResponse) error {
response.QueryID = query.ID
for _, result := range response.Results {
result.Process()
}
respJSON, err := b.Raw("answerInlineQuery", response)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"Answer",
"(",
"query",
"*",
"Query",
",",
"response",
"*",
"QueryResponse",
")",
"error",
"{",
"response",
".",
"QueryID",
"=",
"query",
".",
"ID",
"\n\n",
"for",
"_",
",",
"result",
":=",
"range",
"response",
".",
"Results",
"{",
"result",
".",
"Process",
"(",
")",
"\n",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"response",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"extractOkResponse",
"(",
"respJSON",
")",
"\n",
"}"
] |
// Answer sends a response for a given inline query. A query can only
// be responded to once, subsequent attempts to respond to the same query
// will result in an error.
|
[
"Answer",
"sends",
"a",
"response",
"for",
"a",
"given",
"inline",
"query",
".",
"A",
"query",
"can",
"only",
"be",
"responded",
"to",
"once",
"subsequent",
"attempts",
"to",
"respond",
"to",
"the",
"same",
"query",
"will",
"result",
"in",
"an",
"error",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L938-L951
|
train
|
tucnak/telebot
|
bot.go
|
FileByID
|
func (b *Bot) FileByID(fileID string) (File, error) {
params := map[string]string{
"file_id": fileID,
}
respJSON, err := b.Raw("getFile", params)
if err != nil {
return File{}, err
}
var resp struct {
Ok bool
Description string
Result File
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return File{}, errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return File{}, errors.Errorf("api error: %s", resp.Description)
}
return resp.Result, nil
}
|
go
|
func (b *Bot) FileByID(fileID string) (File, error) {
params := map[string]string{
"file_id": fileID,
}
respJSON, err := b.Raw("getFile", params)
if err != nil {
return File{}, err
}
var resp struct {
Ok bool
Description string
Result File
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return File{}, errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return File{}, errors.Errorf("api error: %s", resp.Description)
}
return resp.Result, nil
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"FileByID",
"(",
"fileID",
"string",
")",
"(",
"File",
",",
"error",
")",
"{",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"fileID",
",",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"File",
"{",
"}",
",",
"err",
"\n",
"}",
"\n\n",
"var",
"resp",
"struct",
"{",
"Ok",
"bool",
"\n",
"Description",
"string",
"\n",
"Result",
"File",
"\n",
"}",
"\n\n",
"err",
"=",
"json",
".",
"Unmarshal",
"(",
"respJSON",
",",
"&",
"resp",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"File",
"{",
"}",
",",
"errors",
".",
"Wrap",
"(",
"err",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"resp",
".",
"Ok",
"{",
"return",
"File",
"{",
"}",
",",
"errors",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"resp",
".",
"Description",
")",
"\n\n",
"}",
"\n\n",
"return",
"resp",
".",
"Result",
",",
"nil",
"\n",
"}"
] |
// FileByID returns full file object including File.FilePath, allowing you to
// download the file from the server.
//
// Usually, Telegram-provided File objects miss FilePath so you might need to
// perform an additional request to fetch them.
|
[
"FileByID",
"returns",
"full",
"file",
"object",
"including",
"File",
".",
"FilePath",
"allowing",
"you",
"to",
"download",
"the",
"file",
"from",
"the",
"server",
".",
"Usually",
"Telegram",
"-",
"provided",
"File",
"objects",
"miss",
"FilePath",
"so",
"you",
"might",
"need",
"to",
"perform",
"an",
"additional",
"request",
"to",
"fetch",
"them",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L984-L1011
|
train
|
tucnak/telebot
|
bot.go
|
Download
|
func (b *Bot) Download(file *File, localFilename string) error {
reader, err := b.GetFile(file)
if err != nil {
return wrapSystem(err)
}
defer reader.Close()
out, err := os.Create(localFilename)
if err != nil {
return wrapSystem(err)
}
defer out.Close()
_, err = io.Copy(out, reader)
if err != nil {
return wrapSystem(err)
}
file.FileLocal = localFilename
return nil
}
|
go
|
func (b *Bot) Download(file *File, localFilename string) error {
reader, err := b.GetFile(file)
if err != nil {
return wrapSystem(err)
}
defer reader.Close()
out, err := os.Create(localFilename)
if err != nil {
return wrapSystem(err)
}
defer out.Close()
_, err = io.Copy(out, reader)
if err != nil {
return wrapSystem(err)
}
file.FileLocal = localFilename
return nil
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"Download",
"(",
"file",
"*",
"File",
",",
"localFilename",
"string",
")",
"error",
"{",
"reader",
",",
"err",
":=",
"b",
".",
"GetFile",
"(",
"file",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"wrapSystem",
"(",
"err",
")",
"\n",
"}",
"\n",
"defer",
"reader",
".",
"Close",
"(",
")",
"\n\n",
"out",
",",
"err",
":=",
"os",
".",
"Create",
"(",
"localFilename",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"wrapSystem",
"(",
"err",
")",
"\n",
"}",
"\n",
"defer",
"out",
".",
"Close",
"(",
")",
"\n\n",
"_",
",",
"err",
"=",
"io",
".",
"Copy",
"(",
"out",
",",
"reader",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"wrapSystem",
"(",
"err",
")",
"\n",
"}",
"\n",
"file",
".",
"FileLocal",
"=",
"localFilename",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// Download saves the file from Telegram servers locally.
//
// Maximum file size to download is 20 MB.
|
[
"Download",
"saves",
"the",
"file",
"from",
"Telegram",
"servers",
"locally",
".",
"Maximum",
"file",
"size",
"to",
"download",
"is",
"20",
"MB",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L1016-L1036
|
train
|
tucnak/telebot
|
bot.go
|
GetFile
|
func (b *Bot) GetFile(file *File) (io.ReadCloser, error) {
f, err := b.FileByID(file.FileID)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/file/bot%s/%s",
b.URL, b.Token, f.FilePath)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
// set FilePath
*file = f
return resp.Body, nil
}
|
go
|
func (b *Bot) GetFile(file *File) (io.ReadCloser, error) {
f, err := b.FileByID(file.FileID)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/file/bot%s/%s",
b.URL, b.Token, f.FilePath)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
// set FilePath
*file = f
return resp.Body, nil
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"GetFile",
"(",
"file",
"*",
"File",
")",
"(",
"io",
".",
"ReadCloser",
",",
"error",
")",
"{",
"f",
",",
"err",
":=",
"b",
".",
"FileByID",
"(",
"file",
".",
"FileID",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"url",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"b",
".",
"URL",
",",
"b",
".",
"Token",
",",
"f",
".",
"FilePath",
")",
"\n\n",
"resp",
",",
"err",
":=",
"http",
".",
"Get",
"(",
"url",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"// set FilePath",
"*",
"file",
"=",
"f",
"\n\n",
"return",
"resp",
".",
"Body",
",",
"nil",
"\n",
"}"
] |
// GetFile from Telegram servers
|
[
"GetFile",
"from",
"Telegram",
"servers"
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L1039-L1056
|
train
|
tucnak/telebot
|
bot.go
|
StopLiveLocation
|
func (b *Bot) StopLiveLocation(message Editable, options ...interface{}) (*Message, error) {
messageID, chatID := message.MessageSig()
params := map[string]string{
"chat_id": fmt.Sprintf("%d", chatID),
"message_id": messageID,
}
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
respJSON, err := b.Raw("stopMessageLiveLocation", params)
if err != nil {
return nil, err
}
return extractMsgResponse(respJSON)
}
|
go
|
func (b *Bot) StopLiveLocation(message Editable, options ...interface{}) (*Message, error) {
messageID, chatID := message.MessageSig()
params := map[string]string{
"chat_id": fmt.Sprintf("%d", chatID),
"message_id": messageID,
}
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
respJSON, err := b.Raw("stopMessageLiveLocation", params)
if err != nil {
return nil, err
}
return extractMsgResponse(respJSON)
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"StopLiveLocation",
"(",
"message",
"Editable",
",",
"options",
"...",
"interface",
"{",
"}",
")",
"(",
"*",
"Message",
",",
"error",
")",
"{",
"messageID",
",",
"chatID",
":=",
"message",
".",
"MessageSig",
"(",
")",
"\n\n",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"chatID",
")",
",",
"\"",
"\"",
":",
"messageID",
",",
"}",
"\n\n",
"sendOpts",
":=",
"extractOptions",
"(",
"options",
")",
"\n",
"embedSendOptions",
"(",
"params",
",",
"sendOpts",
")",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"extractMsgResponse",
"(",
"respJSON",
")",
"\n",
"}"
] |
// StopLiveLocation should be called to stop broadcasting live message location
// before Location.LivePeriod expires.
//
// It supports telebot.ReplyMarkup.
|
[
"StopLiveLocation",
"should",
"be",
"called",
"to",
"stop",
"broadcasting",
"live",
"message",
"location",
"before",
"Location",
".",
"LivePeriod",
"expires",
".",
"It",
"supports",
"telebot",
".",
"ReplyMarkup",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L1061-L1078
|
train
|
tucnak/telebot
|
bot.go
|
SetGroupTitle
|
func (b *Bot) SetGroupTitle(chat *Chat, newTitle string) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"title": newTitle,
}
respJSON, err := b.Raw("setChatTitle", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
go
|
func (b *Bot) SetGroupTitle(chat *Chat, newTitle string) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"title": newTitle,
}
respJSON, err := b.Raw("setChatTitle", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"SetGroupTitle",
"(",
"chat",
"*",
"Chat",
",",
"newTitle",
"string",
")",
"error",
"{",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"chat",
".",
"Recipient",
"(",
")",
",",
"\"",
"\"",
":",
"newTitle",
",",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"extractOkResponse",
"(",
"respJSON",
")",
"\n",
"}"
] |
// SetChatTitle should be used to update group title.
|
[
"SetChatTitle",
"should",
"be",
"used",
"to",
"update",
"group",
"title",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L1110-L1122
|
train
|
tucnak/telebot
|
bot.go
|
SetGroupPhoto
|
func (b *Bot) SetGroupPhoto(chat *Chat, p *Photo) error {
params := map[string]string{
"chat_id": chat.Recipient(),
}
respJSON, err := b.sendFiles("setChatPhoto", map[string]File{"photo": p.File}, params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
go
|
func (b *Bot) SetGroupPhoto(chat *Chat, p *Photo) error {
params := map[string]string{
"chat_id": chat.Recipient(),
}
respJSON, err := b.sendFiles("setChatPhoto", map[string]File{"photo": p.File}, params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"SetGroupPhoto",
"(",
"chat",
"*",
"Chat",
",",
"p",
"*",
"Photo",
")",
"error",
"{",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"chat",
".",
"Recipient",
"(",
")",
",",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"sendFiles",
"(",
"\"",
"\"",
",",
"map",
"[",
"string",
"]",
"File",
"{",
"\"",
"\"",
":",
"p",
".",
"File",
"}",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"extractOkResponse",
"(",
"respJSON",
")",
"\n",
"}"
] |
// SetGroupPhoto should be used to update group photo.
|
[
"SetGroupPhoto",
"should",
"be",
"used",
"to",
"update",
"group",
"photo",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L1140-L1151
|
train
|
tucnak/telebot
|
bot.go
|
Pin
|
func (b *Bot) Pin(message Editable, options ...interface{}) error {
messageID, chatID := message.MessageSig()
params := map[string]string{
"chat_id": strconv.FormatInt(chatID, 10),
"message_id": messageID,
}
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
respJSON, err := b.Raw("pinChatMessage", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
go
|
func (b *Bot) Pin(message Editable, options ...interface{}) error {
messageID, chatID := message.MessageSig()
params := map[string]string{
"chat_id": strconv.FormatInt(chatID, 10),
"message_id": messageID,
}
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
respJSON, err := b.Raw("pinChatMessage", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"Pin",
"(",
"message",
"Editable",
",",
"options",
"...",
"interface",
"{",
"}",
")",
"error",
"{",
"messageID",
",",
"chatID",
":=",
"message",
".",
"MessageSig",
"(",
")",
"\n\n",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"strconv",
".",
"FormatInt",
"(",
"chatID",
",",
"10",
")",
",",
"\"",
"\"",
":",
"messageID",
",",
"}",
"\n\n",
"sendOpts",
":=",
"extractOptions",
"(",
"options",
")",
"\n",
"embedSendOptions",
"(",
"params",
",",
"sendOpts",
")",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"extractOkResponse",
"(",
"respJSON",
")",
"\n",
"}"
] |
// Use this method to pin a message in a supergroup or a channel.
//
// It supports telebot.Silent option.
|
[
"Use",
"this",
"method",
"to",
"pin",
"a",
"message",
"in",
"a",
"supergroup",
"or",
"a",
"channel",
".",
"It",
"supports",
"telebot",
".",
"Silent",
"option",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L1213-L1230
|
train
|
tucnak/telebot
|
bot.go
|
ChatByID
|
func (b *Bot) ChatByID(id string) (*Chat, error) {
params := map[string]string{
"chat_id": id,
}
respJSON, err := b.Raw("getChat", params)
if err != nil {
return nil, err
}
var resp struct {
Ok bool
Description string
Result *Chat
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return nil, errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
}
if resp.Result.Type == ChatChannel && resp.Result.Username == "" {
//Channel is Private
resp.Result.Type = ChatChannelPrivate
}
return resp.Result, nil
}
|
go
|
func (b *Bot) ChatByID(id string) (*Chat, error) {
params := map[string]string{
"chat_id": id,
}
respJSON, err := b.Raw("getChat", params)
if err != nil {
return nil, err
}
var resp struct {
Ok bool
Description string
Result *Chat
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return nil, errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
}
if resp.Result.Type == ChatChannel && resp.Result.Username == "" {
//Channel is Private
resp.Result.Type = ChatChannelPrivate
}
return resp.Result, nil
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"ChatByID",
"(",
"id",
"string",
")",
"(",
"*",
"Chat",
",",
"error",
")",
"{",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"id",
",",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"var",
"resp",
"struct",
"{",
"Ok",
"bool",
"\n",
"Description",
"string",
"\n",
"Result",
"*",
"Chat",
"\n",
"}",
"\n\n",
"err",
"=",
"json",
".",
"Unmarshal",
"(",
"respJSON",
",",
"&",
"resp",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"errors",
".",
"Wrap",
"(",
"err",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"resp",
".",
"Ok",
"{",
"return",
"nil",
",",
"errors",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"resp",
".",
"Description",
")",
"\n",
"}",
"\n\n",
"if",
"resp",
".",
"Result",
".",
"Type",
"==",
"ChatChannel",
"&&",
"resp",
".",
"Result",
".",
"Username",
"==",
"\"",
"\"",
"{",
"//Channel is Private",
"resp",
".",
"Result",
".",
"Type",
"=",
"ChatChannelPrivate",
"\n",
"}",
"\n\n",
"return",
"resp",
".",
"Result",
",",
"nil",
"\n",
"}"
] |
// ChatByID fetches chat info of its ID.
//
// Including current name of the user for one-on-one conversations,
// current username of a user, group or channel, etc.
//
// Returns a Chat object on success.
|
[
"ChatByID",
"fetches",
"chat",
"info",
"of",
"its",
"ID",
".",
"Including",
"current",
"name",
"of",
"the",
"user",
"for",
"one",
"-",
"on",
"-",
"one",
"conversations",
"current",
"username",
"of",
"a",
"user",
"group",
"or",
"channel",
"etc",
".",
"Returns",
"a",
"Chat",
"object",
"on",
"success",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L1254-L1285
|
train
|
tucnak/telebot
|
bot.go
|
ProfilePhotosOf
|
func (b *Bot) ProfilePhotosOf(user *User) ([]Photo, error) {
params := map[string]string{
"user_id": user.Recipient(),
}
respJSON, err := b.Raw("getUserProfilePhotos", params)
if err != nil {
return nil, err
}
var resp struct {
Ok bool
Result struct {
Count int `json:"total_count"`
Photos []Photo `json:"photos"`
}
Description string `json:"description"`
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return nil, errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
}
return resp.Result.Photos, nil
}
|
go
|
func (b *Bot) ProfilePhotosOf(user *User) ([]Photo, error) {
params := map[string]string{
"user_id": user.Recipient(),
}
respJSON, err := b.Raw("getUserProfilePhotos", params)
if err != nil {
return nil, err
}
var resp struct {
Ok bool
Result struct {
Count int `json:"total_count"`
Photos []Photo `json:"photos"`
}
Description string `json:"description"`
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return nil, errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
}
return resp.Result.Photos, nil
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"ProfilePhotosOf",
"(",
"user",
"*",
"User",
")",
"(",
"[",
"]",
"Photo",
",",
"error",
")",
"{",
"params",
":=",
"map",
"[",
"string",
"]",
"string",
"{",
"\"",
"\"",
":",
"user",
".",
"Recipient",
"(",
")",
",",
"}",
"\n\n",
"respJSON",
",",
"err",
":=",
"b",
".",
"Raw",
"(",
"\"",
"\"",
",",
"params",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"var",
"resp",
"struct",
"{",
"Ok",
"bool",
"\n",
"Result",
"struct",
"{",
"Count",
"int",
"`json:\"total_count\"`",
"\n",
"Photos",
"[",
"]",
"Photo",
"`json:\"photos\"`",
"\n",
"}",
"\n\n",
"Description",
"string",
"`json:\"description\"`",
"\n",
"}",
"\n\n",
"err",
"=",
"json",
".",
"Unmarshal",
"(",
"respJSON",
",",
"&",
"resp",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"errors",
".",
"Wrap",
"(",
"err",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"if",
"!",
"resp",
".",
"Ok",
"{",
"return",
"nil",
",",
"errors",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"resp",
".",
"Description",
")",
"\n",
"}",
"\n\n",
"return",
"resp",
".",
"Result",
".",
"Photos",
",",
"nil",
"\n",
"}"
] |
// ProfilePhotosOf return list of profile pictures for a user.
|
[
"ProfilePhotosOf",
"return",
"list",
"of",
"profile",
"pictures",
"for",
"a",
"user",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L1288-L1318
|
train
|
tucnak/telebot
|
bot.go
|
FileURLByID
|
func (b *Bot) FileURLByID(fileID string) (string, error) {
f, err := b.FileByID(fileID)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/file/bot%s/%s", b.URL, b.Token, f.FilePath), nil
}
|
go
|
func (b *Bot) FileURLByID(fileID string) (string, error) {
f, err := b.FileByID(fileID)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/file/bot%s/%s", b.URL, b.Token, f.FilePath), nil
}
|
[
"func",
"(",
"b",
"*",
"Bot",
")",
"FileURLByID",
"(",
"fileID",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"f",
",",
"err",
":=",
"b",
".",
"FileByID",
"(",
"fileID",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"b",
".",
"URL",
",",
"b",
".",
"Token",
",",
"f",
".",
"FilePath",
")",
",",
"nil",
"\n",
"}"
] |
// FileURLByID returns direct url for files using FileId which you can get from File object
|
[
"FileURLByID",
"returns",
"direct",
"url",
"for",
"files",
"using",
"FileId",
"which",
"you",
"can",
"get",
"from",
"File",
"object"
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/bot.go#L1353-L1359
|
train
|
tucnak/telebot
|
file.go
|
OnDisk
|
func (f *File) OnDisk() bool {
if _, err := os.Stat(f.FileLocal); err != nil {
return false
}
return true
}
|
go
|
func (f *File) OnDisk() bool {
if _, err := os.Stat(f.FileLocal); err != nil {
return false
}
return true
}
|
[
"func",
"(",
"f",
"*",
"File",
")",
"OnDisk",
"(",
")",
"bool",
"{",
"if",
"_",
",",
"err",
":=",
"os",
".",
"Stat",
"(",
"f",
".",
"FileLocal",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"return",
"true",
"\n",
"}"
] |
// OnDisk will return true if file is present on disk.
|
[
"OnDisk",
"will",
"return",
"true",
"if",
"file",
"is",
"present",
"on",
"disk",
"."
] |
8c1c512262f2f97b1212eb1751efb498d2038a0c
|
https://github.com/tucnak/telebot/blob/8c1c512262f2f97b1212eb1751efb498d2038a0c/file.go#L81-L87
|
train
|
remind101/empire
|
pkg/constraints/constraints.go
|
NewCPUShare
|
func NewCPUShare(i int) (CPUShare, error) {
if i < 2 {
return 0, ErrInvalidCPUShare
}
return CPUShare(i), nil
}
|
go
|
func NewCPUShare(i int) (CPUShare, error) {
if i < 2 {
return 0, ErrInvalidCPUShare
}
return CPUShare(i), nil
}
|
[
"func",
"NewCPUShare",
"(",
"i",
"int",
")",
"(",
"CPUShare",
",",
"error",
")",
"{",
"if",
"i",
"<",
"2",
"{",
"return",
"0",
",",
"ErrInvalidCPUShare",
"\n",
"}",
"\n\n",
"return",
"CPUShare",
"(",
"i",
")",
",",
"nil",
"\n",
"}"
] |
// NewCPUShare casts i to a CPUShare and ensures its validity.
|
[
"NewCPUShare",
"casts",
"i",
"to",
"a",
"CPUShare",
"and",
"ensures",
"its",
"validity",
"."
] |
3daba389f6b07b5d219246bfc7d901fcec664161
|
https://github.com/remind101/empire/blob/3daba389f6b07b5d219246bfc7d901fcec664161/pkg/constraints/constraints.go#L44-L50
|
train
|
remind101/empire
|
pkg/constraints/constraints.go
|
ParseMemory
|
func ParseMemory(s string) (Memory, error) {
i, err := parseMemory(s)
return Memory(i), err
}
|
go
|
func ParseMemory(s string) (Memory, error) {
i, err := parseMemory(s)
return Memory(i), err
}
|
[
"func",
"ParseMemory",
"(",
"s",
"string",
")",
"(",
"Memory",
",",
"error",
")",
"{",
"i",
",",
"err",
":=",
"parseMemory",
"(",
"s",
")",
"\n",
"return",
"Memory",
"(",
"i",
")",
",",
"err",
"\n",
"}"
] |
// ParseMemory parses a string in memory format and returns the amount of memory
// in bytes.
|
[
"ParseMemory",
"parses",
"a",
"string",
"in",
"memory",
"format",
"and",
"returns",
"the",
"amount",
"of",
"memory",
"in",
"bytes",
"."
] |
3daba389f6b07b5d219246bfc7d901fcec664161
|
https://github.com/remind101/empire/blob/3daba389f6b07b5d219246bfc7d901fcec664161/pkg/constraints/constraints.go#L69-L72
|
train
|
remind101/empire
|
releases.go
|
BeforeCreate
|
func (r *Release) BeforeCreate() error {
t := timex.Now()
r.CreatedAt = &t
return nil
}
|
go
|
func (r *Release) BeforeCreate() error {
t := timex.Now()
r.CreatedAt = &t
return nil
}
|
[
"func",
"(",
"r",
"*",
"Release",
")",
"BeforeCreate",
"(",
")",
"error",
"{",
"t",
":=",
"timex",
".",
"Now",
"(",
")",
"\n",
"r",
".",
"CreatedAt",
"=",
"&",
"t",
"\n",
"return",
"nil",
"\n",
"}"
] |
// BeforeCreate sets created_at before inserting.
|
[
"BeforeCreate",
"sets",
"created_at",
"before",
"inserting",
"."
] |
3daba389f6b07b5d219246bfc7d901fcec664161
|
https://github.com/remind101/empire/blob/3daba389f6b07b5d219246bfc7d901fcec664161/releases.go#L61-L65
|
train
|
remind101/empire
|
releases.go
|
DefaultRange
|
func (q ReleasesQuery) DefaultRange() headerutil.Range {
sort, order := "version", "desc"
return headerutil.Range{
Sort: &sort,
Order: &order,
}
}
|
go
|
func (q ReleasesQuery) DefaultRange() headerutil.Range {
sort, order := "version", "desc"
return headerutil.Range{
Sort: &sort,
Order: &order,
}
}
|
[
"func",
"(",
"q",
"ReleasesQuery",
")",
"DefaultRange",
"(",
")",
"headerutil",
".",
"Range",
"{",
"sort",
",",
"order",
":=",
"\"",
"\"",
",",
"\"",
"\"",
"\n",
"return",
"headerutil",
".",
"Range",
"{",
"Sort",
":",
"&",
"sort",
",",
"Order",
":",
"&",
"order",
",",
"}",
"\n",
"}"
] |
// DefaultRange returns the default headerutil.Range used if values aren't
// provided.
|
[
"DefaultRange",
"returns",
"the",
"default",
"headerutil",
".",
"Range",
"used",
"if",
"values",
"aren",
"t",
"provided",
"."
] |
3daba389f6b07b5d219246bfc7d901fcec664161
|
https://github.com/remind101/empire/blob/3daba389f6b07b5d219246bfc7d901fcec664161/releases.go#L99-L105
|
train
|
remind101/empire
|
releases.go
|
CreateAndRelease
|
func (s *releasesService) CreateAndRelease(ctx context.Context, db *gorm.DB, r *Release, ss twelvefactor.StatusStream) (*Release, error) {
r, err := s.Create(ctx, db, r)
if err != nil {
return r, err
}
// Schedule the new release onto the cluster.
return r, s.Release(ctx, r, ss)
}
|
go
|
func (s *releasesService) CreateAndRelease(ctx context.Context, db *gorm.DB, r *Release, ss twelvefactor.StatusStream) (*Release, error) {
r, err := s.Create(ctx, db, r)
if err != nil {
return r, err
}
// Schedule the new release onto the cluster.
return r, s.Release(ctx, r, ss)
}
|
[
"func",
"(",
"s",
"*",
"releasesService",
")",
"CreateAndRelease",
"(",
"ctx",
"context",
".",
"Context",
",",
"db",
"*",
"gorm",
".",
"DB",
",",
"r",
"*",
"Release",
",",
"ss",
"twelvefactor",
".",
"StatusStream",
")",
"(",
"*",
"Release",
",",
"error",
")",
"{",
"r",
",",
"err",
":=",
"s",
".",
"Create",
"(",
"ctx",
",",
"db",
",",
"r",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"r",
",",
"err",
"\n",
"}",
"\n",
"// Schedule the new release onto the cluster.",
"return",
"r",
",",
"s",
".",
"Release",
"(",
"ctx",
",",
"r",
",",
"ss",
")",
"\n",
"}"
] |
// CreateAndRelease creates a new release then submits it to the scheduler.
|
[
"CreateAndRelease",
"creates",
"a",
"new",
"release",
"then",
"submits",
"it",
"to",
"the",
"scheduler",
"."
] |
3daba389f6b07b5d219246bfc7d901fcec664161
|
https://github.com/remind101/empire/blob/3daba389f6b07b5d219246bfc7d901fcec664161/releases.go#L113-L120
|
train
|
remind101/empire
|
releases.go
|
Create
|
func (s *releasesService) Create(ctx context.Context, db *gorm.DB, r *Release) (*Release, error) {
// Lock all releases for the given application to ensure that the
// release version is updated automically.
if err := db.Exec(`select 1 from releases where app_id = ? for update`, r.App.ID).Error; err != nil {
return r, err
}
// During rollbacks, we can just provide the existing Formation for the
// old release. For new releases, we need to create a new formation by
// merging the formation from the extracted Procfile, and the Formation
// from the existing release.
if r.Formation == nil {
if err := buildFormation(db, r); err != nil {
return r, err
}
}
return releasesCreate(db, r)
}
|
go
|
func (s *releasesService) Create(ctx context.Context, db *gorm.DB, r *Release) (*Release, error) {
// Lock all releases for the given application to ensure that the
// release version is updated automically.
if err := db.Exec(`select 1 from releases where app_id = ? for update`, r.App.ID).Error; err != nil {
return r, err
}
// During rollbacks, we can just provide the existing Formation for the
// old release. For new releases, we need to create a new formation by
// merging the formation from the extracted Procfile, and the Formation
// from the existing release.
if r.Formation == nil {
if err := buildFormation(db, r); err != nil {
return r, err
}
}
return releasesCreate(db, r)
}
|
[
"func",
"(",
"s",
"*",
"releasesService",
")",
"Create",
"(",
"ctx",
"context",
".",
"Context",
",",
"db",
"*",
"gorm",
".",
"DB",
",",
"r",
"*",
"Release",
")",
"(",
"*",
"Release",
",",
"error",
")",
"{",
"// Lock all releases for the given application to ensure that the",
"// release version is updated automically.",
"if",
"err",
":=",
"db",
".",
"Exec",
"(",
"`select 1 from releases where app_id = ? for update`",
",",
"r",
".",
"App",
".",
"ID",
")",
".",
"Error",
";",
"err",
"!=",
"nil",
"{",
"return",
"r",
",",
"err",
"\n",
"}",
"\n\n",
"// During rollbacks, we can just provide the existing Formation for the",
"// old release. For new releases, we need to create a new formation by",
"// merging the formation from the extracted Procfile, and the Formation",
"// from the existing release.",
"if",
"r",
".",
"Formation",
"==",
"nil",
"{",
"if",
"err",
":=",
"buildFormation",
"(",
"db",
",",
"r",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"r",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"releasesCreate",
"(",
"db",
",",
"r",
")",
"\n",
"}"
] |
// Create creates a new release.
|
[
"Create",
"creates",
"a",
"new",
"release",
"."
] |
3daba389f6b07b5d219246bfc7d901fcec664161
|
https://github.com/remind101/empire/blob/3daba389f6b07b5d219246bfc7d901fcec664161/releases.go#L123-L141
|
train
|
remind101/empire
|
releases.go
|
Rollback
|
func (s *releasesService) Rollback(ctx context.Context, db *gorm.DB, opts RollbackOpts) (*Release, error) {
app, version := opts.App, opts.Version
r, err := releasesFind(db, ReleasesQuery{App: app, Version: &version})
if err != nil {
return nil, err
}
desc := fmt.Sprintf("Rollback to v%d", version)
desc = appendMessageToDescription(desc, opts.User, opts.Message)
return s.CreateAndRelease(ctx, db, &Release{
App: app,
Config: r.Config,
Slug: r.Slug,
Formation: r.Formation,
Description: desc,
}, nil)
}
|
go
|
func (s *releasesService) Rollback(ctx context.Context, db *gorm.DB, opts RollbackOpts) (*Release, error) {
app, version := opts.App, opts.Version
r, err := releasesFind(db, ReleasesQuery{App: app, Version: &version})
if err != nil {
return nil, err
}
desc := fmt.Sprintf("Rollback to v%d", version)
desc = appendMessageToDescription(desc, opts.User, opts.Message)
return s.CreateAndRelease(ctx, db, &Release{
App: app,
Config: r.Config,
Slug: r.Slug,
Formation: r.Formation,
Description: desc,
}, nil)
}
|
[
"func",
"(",
"s",
"*",
"releasesService",
")",
"Rollback",
"(",
"ctx",
"context",
".",
"Context",
",",
"db",
"*",
"gorm",
".",
"DB",
",",
"opts",
"RollbackOpts",
")",
"(",
"*",
"Release",
",",
"error",
")",
"{",
"app",
",",
"version",
":=",
"opts",
".",
"App",
",",
"opts",
".",
"Version",
"\n",
"r",
",",
"err",
":=",
"releasesFind",
"(",
"db",
",",
"ReleasesQuery",
"{",
"App",
":",
"app",
",",
"Version",
":",
"&",
"version",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"desc",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"version",
")",
"\n",
"desc",
"=",
"appendMessageToDescription",
"(",
"desc",
",",
"opts",
".",
"User",
",",
"opts",
".",
"Message",
")",
"\n",
"return",
"s",
".",
"CreateAndRelease",
"(",
"ctx",
",",
"db",
",",
"&",
"Release",
"{",
"App",
":",
"app",
",",
"Config",
":",
"r",
".",
"Config",
",",
"Slug",
":",
"r",
".",
"Slug",
",",
"Formation",
":",
"r",
".",
"Formation",
",",
"Description",
":",
"desc",
",",
"}",
",",
"nil",
")",
"\n",
"}"
] |
// Rolls back to a specific release version.
|
[
"Rolls",
"back",
"to",
"a",
"specific",
"release",
"version",
"."
] |
3daba389f6b07b5d219246bfc7d901fcec664161
|
https://github.com/remind101/empire/blob/3daba389f6b07b5d219246bfc7d901fcec664161/releases.go#L144-L160
|
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.