collections

package module
v0.0.0-...-62ce27e Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2020 License: MIT Imports: 3 Imported by: 0

README

collections

golang collections like python include ordered map and set, dequeue,queue,counter


implemented chainmap, counter, deque(thread safe), queue(thread safe), orderedmap and set datatypes so far. ##Basic usages

  • counter
counter := NewCounter("a", "b", "c", "d", "b") // init a counter with some elements
counter.Add(1) // add one
counter.Add(1) // now twice 1
top2 := counter.MostCommon(2) // get a slice like PairList{Pair{1, 2}, Pair{"b", 2}} that indicate the top 2 elements and their counts
counter.Elements() //return all elements
counter.Del(1)
len := counter.Len() // return length of counter 4
  • deque
import "container/list"

dq := NewDeque()
dq.AppendLeft(1) // push left
dq.Append("gensword") // push right
dq.Pop() // pop right
dq.PopLeft() // pop left
other := list.New()
other.PushBack(1)
other.PushBack(2)
other.PushFront(5)
dq.Extend(other) // extend a *list.List right
dq.ExtendLeft(other) // extend a *list.List left
dq.Rotate(1) // right step 1
dq.Rotate(-1) // left step 1
dq.Index(1, 0, dq.Size()) // return the first position index of element and true which value is 1 between 0 and len(dq), if not found, return zero and false
dq.Remove(1) // delete the first element satisfied with 1 and return remove nums(1) and true, if not found, return zero and false
dq.Clear() // clear all elements
  • queue
q := Newqueue()
q.Push(1)
q.push("hello")
q.Pop()
q.Size() // return 1
q.Pop()
q.Isempty() // return true
  • orderedMap
om := NewOrderedMap()
om.Set("name", "gensword", true) // set name, the last bool argument is a flag if the name should replace to the last position and update the key when key name already in om, if false, just update the name value
om.Set("age", 21)
v, ok := om.Get("age") // get the value of age, if not found, reutn nil and false
for item := range om.Iter() {
		fmt.Println(item.Key, item.Value) // print k-v order by set time
	}
om.Del("name") // delete the key name, if not exists, return false
  • set
s := NewSet(1, "hello") // init a set with two elements
s.Add('I') // add one
s.Exists(1) // true
s.Exists(2) // false
other := NewSet("you", "hello")
intersect := s.Intersect(other) // return a new set include "hello"
union := s.Union(other) // return a new set include 1, "hello", "you", 'I'
diff := s.Diff(other) // return a new set include 1 , 'I', and "you"
s.Elemetns // return []interface{}{1, "hello", 'I'}

some code inspired by another golang package collections.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

type Counter struct {
	Count map[interface{}]int
	// contains filtered or unexported fields
}

func NewCounter

func NewCounter(elems ...interface{}) (c *Counter)

func (*Counter) Add

func (c *Counter) Add(elem interface{})

add a elem(key) into the counter and incr the counts of the key

func (*Counter) Del

func (c *Counter) Del(key interface{}) bool

delete a key from a counter, if key exist return true else return false

func (*Counter) Elements

func (c *Counter) Elements() []interface{}

return elements of the counter

func (*Counter) Len

func (c *Counter) Len() int

return length of a counter

func (*Counter) MostCommon

func (c *Counter) MostCommon(top int) PairList

return top frequently keys and their counts

type Deque

type Deque struct {
	// contains filtered or unexported fields
}

func NewDeque

func NewDeque() (dq *Deque)

func (*Deque) Append

func (dq *Deque) Append(v interface{})

Append push back a item

func (*Deque) AppendLeft

func (dq *Deque) AppendLeft(v interface{})

push front a item

func (*Deque) Clear

func (dq *Deque) Clear()

Clear clear a dequeue

func (*Deque) Extend

func (dq *Deque) Extend(other *list.List)

Extend push back a container/list

func (*Deque) ExtendLeft

func (dq *Deque) ExtendLeft(other *list.List)

ExtendLeft push front a container/list

func (*Deque) Index

func (dq *Deque) Index(v interface{}, start int, end int) (position int, find bool)

Index return the first index between start(include) and end(not include) which match v, find will be false if can not find

func (*Deque) Pop

func (dq *Deque) Pop() (v interface{}, canPop bool)

Pop pop a item from back, if dequeue

func (*Deque) PopLeft

func (dq *Deque) PopLeft() (v interface{}, canGet bool)

PopLeft pop a item from front

func (*Deque) Remove

func (dq *Deque) Remove(v interface{}) (removed bool)

Remove remove the first elem match v

func (*Deque) Rotate

func (dq *Deque) Rotate(step int)

Rotate right shift step, if step < 0 then left shift step

func (*Deque) Size

func (dq *Deque) Size() int

return length of the deque

type LinkedList

type LinkedList struct {
	// contains filtered or unexported fields
}

func NewLinkdList

func NewLinkdList() *LinkedList

func (*LinkedList) Append

func (l *LinkedList) Append(key interface{}, value interface{}) *LinkedNode

func (*LinkedList) Iter

func (l *LinkedList) Iter() chan *LinkedNode

func (*LinkedList) Remove

func (l *LinkedList) Remove(n *LinkedNode) bool

type LinkedNode

type LinkedNode struct {
	// contains filtered or unexported fields
}

type OrderedMap

type OrderedMap struct {
	// contains filtered or unexported fields
}

func NewOrderedMap

func NewOrderedMap() *OrderedMap

func (*OrderedMap) Del

func (om *OrderedMap) Del(key interface{}) bool

Del del a key from ordered map, return false if key not exist

func (*OrderedMap) Get

func (om *OrderedMap) Get(key interface{}) (interface{}, bool)

Get

func (*OrderedMap) Iter

func (om *OrderedMap) Iter() chan *item

return k-v in order(set order)

func (*OrderedMap) Set

func (om *OrderedMap) Set(key interface{}, value interface{}, putEndIfExists bool)

Set set k-v and push back the k-v, the order will not change if the key already exist unless putEndIfExists is true

type Pair

type Pair struct {
	Key   interface{} // key add to counter
	Value int         // store counts of the key
}

type PairList

type PairList []Pair

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

func NewQueue

func NewQueue() (queue *Queue)

func (*Queue) IsEmpty

func (queue *Queue) IsEmpty() (isEmpty bool)

func (*Queue) Pop

func (queue *Queue) Pop() (v interface{}, canGet bool)

func (*Queue) Push

func (queue *Queue) Push(v interface{})

func (*Queue) Size

func (queue *Queue) Size() int

type Set

type Set struct {
	// contains filtered or unexported fields
}

func NewSet

func NewSet(elems ...interface{}) *Set

func (*Set) Add

func (s *Set) Add(elem interface{})

Add add a elem into set

func (*Set) Del

func (s *Set) Del(elem interface{}) bool

Del delete the elem from set

func (*Set) Diff

func (s *Set) Diff(other *Set) *Set

diff = union - intersect

func (*Set) Elements

func (s *Set) Elements() []interface{}

Elements return all elems

func (*Set) Exists

func (s *Set) Exists(elem interface{}) bool

Exists return true if elem exist else false

func (*Set) Intersect

func (s *Set) Intersect(other *Set) *Set

Intersect return the intersection between s and other

func (*Set) Len

func (s *Set) Len() int

Len return length of s

func (*Set) Union

func (s *Set) Union(other *Set) *Set

Union return the union of s and other

Jump to

Keyboard shortcuts

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