Documentation ¶
Overview ¶
Package bag implements a multi-set data structure supporting arbitrary types (even a mixture).
Internally it uses a simple map assigning counts to the different values present in the bag.
Example (Usage) ¶
Small demo of the common functions in the bag package.
package main import ( "fmt" "gopkg.in/karalabe/cookiejar.v2/collections/bag" ) func main() { // Create a new bag with some integers in it b := bag.New() for i := 0; i < 10; i++ { b.Insert(i) } b.Insert(8) // Remove every odd integer for i := 1; i < 10; i += 2 { b.Remove(i) } // Print the element count of all numbers for i := 0; i < 10; i++ { fmt.Printf("#%d: %d\n", i, b.Count(i)) } // Calculate the sum with a Do iteration sum := 0 b.Do(func(val interface{}) { sum += val.(int) }) fmt.Println("Sum:", sum) }
Output: #0: 1 #1: 0 #2: 1 #3: 0 #4: 1 #5: 0 #6: 1 #7: 0 #8: 2 #9: 0 Sum: 28
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bag ¶
type Bag struct {
// contains filtered or unexported fields
}
Bag data structure (multiset).
func (*Bag) Do ¶
func (b *Bag) Do(f func(interface{}))
Executes a function for every element in the bag.
Click to show internal directories.
Click to hide internal directories.