bag

package
v2.0.0-...-8dcd6a7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2015 License: BSD-2-Clause Imports: 0 Imported by: 4

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 New

func New() *Bag

Creates a new empty bag.

func (*Bag) Count

func (b *Bag) Count(val interface{}) int

Counts the number of occurances of an element in the bag.

func (*Bag) Do

func (b *Bag) Do(f func(interface{}))

Executes a function for every element in the bag.

func (*Bag) Insert

func (b *Bag) Insert(val interface{})

Inserts an element into the bag.

func (*Bag) Remove

func (b *Bag) Remove(val interface{})

Removes an element from the bag. If none was present, nothing is done.

func (*Bag) Reset

func (b *Bag) Reset()

Clears the contents of a bag.

func (*Bag) Size

func (b *Bag) Size() int

Returns the total number of elemens in the bag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL