collections

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package collections provides several convenient data structures.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChainMap

type ChainMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ChainMap represents a data structure that allows chaining multiple maps together.

Example
d1 := dict.Dict[string, int]{"a": 1, "b": 2}
d2 := dict.Dict[string, int]{"a": 4, "c": 5}
cm := NewChainMap(&d1, &d2)
fmt.Println(cm.Get("a"))
fmt.Println(cm.Get("b"))
fmt.Println(cm.Get("c"))
fmt.Println(cm.Get("d"))
cm.Set("d", 3)
fmt.Println(cm.Get("d"))
Output:

1 true
2 true
5 true
0 false
3 true

func NewChainMap

func NewChainMap[K comparable, V any](maps ...*dict.Dict[K, V]) *ChainMap[K, V]

NewChainMap creates a new ChainMap with the provided maps as parent nodes.

func (*ChainMap[K, V]) Get

func (cm *ChainMap[K, V]) Get(key K) (value V, ok bool)

Get retrieves the value for the key from the current ChainMap or its parent nodes.

func (*ChainMap[K, V]) Items

func (cm *ChainMap[K, V]) Items() *dict.Dict[K, V]

Items returns the dictionary of the current ChainMap node.

func (*ChainMap[K, V]) Maps

func (cm *ChainMap[K, V]) Maps() []*dict.Dict[K, V]

Maps returns a slice of all the maps in the chain, including the current ChainMap and its parents.

func (*ChainMap[K, V]) NewChild

func (cm *ChainMap[K, V]) NewChild() *ChainMap[K, V]

NewChild creates a new child ChainMap with the current ChainMap as its parent.

func (*ChainMap[K, V]) Parent

func (cm *ChainMap[K, V]) Parent() *ChainMap[K, V]

Parent returns the parent ChainMap node.

func (*ChainMap[K, V]) Parents

func (cm *ChainMap[K, V]) Parents() []*ChainMap[K, V]

Parents returns a slice of all the parent ChainMap nodes in the chain.

func (*ChainMap[K, V]) Set

func (cm *ChainMap[K, V]) Set(key K, value V) *ChainMap[K, V]

Set sets the key-value pair in the current ChainMap and returns a pointer to the ChainMap.

type Counter

type Counter[T comparable] struct {
	// contains filtered or unexported fields
}

Counter represents a data structure for counting occurrences of items.

Example
l := arraylist.ArrayList[int]{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}
c := NewCounter(l, -1)
for i := 0; i < 10; i++ {
	fmt.Println(i, c.Get(i))
}
Output:

	0 -1
1 2
2 1
3 2
4 1
5 3
6 1
7 -1
8 -1
9 1

func MergeCounts

func MergeCounts[T comparable](counters ...*Counter[T]) *Counter[T]

MergeCounts merges counts from multiple Counters and returns a new Counter with combined counts.

Example
l1 := arraylist.ArrayList[int]{1, 2, 2, 2, 3, 3, 4, 4, 5}
c1 := NewCounter(l1)
l2 := arraylist.ArrayList[int]{1, 2, 7, 8, 9, 10}
c2 := NewCounter(l2)
mc := MergeCounts(c1, c2)
fmt.Println(c1.Get(2))
fmt.Println(c2.Get(2))
fmt.Println(mc.Get(2))
Output:

3
1
4

func NewCounter

func NewCounter[T comparable](items arraylist.ArrayList[T], defaultCounts ...int) *Counter[T]

NewCounter returns a new Counter initialized with given items and defaultCounts. It counts occurrences of items and initializes the Counter with groups and default count.

func (*Counter[T]) Clear

func (c *Counter[T]) Clear() *Counter[T]

Clear removes all items and counts from the Counter and returns the Counter.

func (Counter[T]) Copy

func (c Counter[T]) Copy() Counter[T]

Copy returns a copy of the current Counter.

func (*Counter[T]) Elements

func (c *Counter[T]) Elements() arraylist.ArrayList[T]

Elements returns all the unique elements (items) in the Counter as an ArrayList.

func (Counter[T]) Equal

func (c Counter[T]) Equal(another Counter[T]) bool

Equal checks if the current Counter is equal to another Counter by comparing their records.

func (*Counter[T]) Get

func (c *Counter[T]) Get(item T) int

Get returns the count of the specified item in the Counter.

func (*Counter[T]) Increment

func (c *Counter[T]) Increment(item T, counts ...int) *Counter[T]

Increment increments the count of the specified item in the Counter and returns the Counter.

Example
l := arraylist.ArrayList[int]{1, 2, 2, 2, 3, 3, 4, 4, 5}
c := NewCounter(l, -1)
fmt.Println(c.Get(2))
c.Increment(2, 2)
fmt.Println(c.Get(2))
Output:

3
5

func (*Counter[T]) LeastCommon

func (c *Counter[T]) LeastCommon() arraylist.ArrayList[T]

LeastCommon returns the least common item/s in the Counter as an ArrayList.

Example
l := arraylist.ArrayList[int]{1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5}
c := NewCounter(l, -1)
fmt.Println(c.LeastCommon())
Output:

[5]

func (*Counter[T]) MostCommon

func (c *Counter[T]) MostCommon() arraylist.ArrayList[T]

MostCommon returns the most common item/s in the Counter as an ArrayList.

Example
l := arraylist.ArrayList[int]{1, 2, 2, 2, 3, 3, 4, 4, 5}
c := NewCounter(l, -1)
fmt.Println(c.MostCommon())
Output:

[2]

func (*Counter[T]) Remove

func (c *Counter[T]) Remove(item T) (exist bool)

Remove removes the specified item from the Counter and returns a boolean indicating its existence.

Example
l := arraylist.ArrayList[int]{1, 2, 2, 2, 3, 3, 4, 4, 5}
c := NewCounter(l, -1)
fmt.Println(c.Get(2))
c.Remove(2)
fmt.Println(c.Get(2))
Output:

3
-1

func (*Counter[T]) Reset

func (c *Counter[T]) Reset() *Counter[T]

Reset resets all counts in the Counter to the default count and returns the Counter.

Example
l := arraylist.ArrayList[int]{1, 2, 2, 2, 3, 3, 4, 4, 5}
c := NewCounter(l, -1)
fmt.Println(c.Get(2))
c.Reset()
fmt.Println(c.Get(2))
Output:

3
-1

func (*Counter[T]) Set

func (c *Counter[T]) Set(item T, count int) *Counter[T]

Set sets the count of the specified item in the Counter and returns the Counter.

func (*Counter[T]) SetDefault

func (c *Counter[T]) SetDefault(count int) *Counter[T]

SetDefault sets the default count for the Counter and returns the Counter.

func (*Counter[T]) Subtract

func (c *Counter[T]) Subtract(item T, counts ...int) *Counter[T]

Subtract decrements the count of the specified item in the Counter and returns the Counter.

Example
l := arraylist.ArrayList[int]{1, 2, 2, 2, 3, 3, 4, 4, 5}
c := NewCounter(l, -1)
fmt.Println(c.Get(2))
c.Subtract(2, 2)
fmt.Println(c.Get(2))
Output:

3
1

func (*Counter[T]) Total

func (c *Counter[T]) Total() (total int)

Total returns the total count of all items in the Counter.

Example
l := arraylist.ArrayList[int]{1, 2, 2, 2, 3, 3, 4, 4, 5}
c := NewCounter(l, -1)
fmt.Println(c.Total())
Output:

9

type DefaultDict

type DefaultDict[K comparable, V any] struct {
	dict.Dict[K, V]
	// contains filtered or unexported fields
}

DefaultDict is a wrapper around dict.Dict with a default value for keys which are not present.

Example
d := dict.Dict[string, int]{"a": 1, "b": 2, "c": 3}
d1 := NewDefaultDict(d, -1)
fmt.Println(d1.Get("a"))
fmt.Println(d1.Get("b"))
fmt.Println(d1.Get("c"))
fmt.Println(d1.Get("d"))
Output:

1
2
3
-1

func NewDefaultDict

func NewDefaultDict[K comparable, V any](items dict.Dict[K, V], defaultVal V) *DefaultDict[K, V]

NewDefaultDict creates a new DefaultDict with the given items and default value.

func (*DefaultDict[K, V]) Clear

func (d *DefaultDict[K, V]) Clear() *DefaultDict[K, V]

Clear removes all items from the DefaultDict.

func (DefaultDict[K, V]) Copy

func (d DefaultDict[K, V]) Copy() DefaultDict[K, V]

Copy creates a copy of the DefaultDict.

func (DefaultDict[K, V]) Equal

func (d DefaultDict[K, V]) Equal(another DefaultDict[K, V]) bool

Equal checks whether two DefaultDicts are equal.

func (*DefaultDict[K, V]) Get

func (d *DefaultDict[K, V]) Get(key K) (value V)

Get retrieves the value for the given key; creates and sets default value if key is not present.

func (DefaultDict[K, V]) MarshalJSON

func (d DefaultDict[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the DefaultDict.

func (*DefaultDict[K, V]) Pop

func (d *DefaultDict[K, V]) Pop(key K) (value V)

Pop removes the value for the given key and returns it; creates and sets default value if key is not present.

func (*DefaultDict[K, V]) Set

func (d *DefaultDict[K, V]) Set(key K, value V) *DefaultDict[K, V]

Set sets the value for the given key in the DefaultDict.

func (*DefaultDict[K, V]) SetDefault

func (d *DefaultDict[K, V]) SetDefault(value V) *DefaultDict[K, V]

SetDefault sets the default value for keys not present in the DefaultDict.

func (DefaultDict[K, V]) String

func (d DefaultDict[K, V]) String() string

String returns the string representation of the DefaultDict.

func (*DefaultDict[K, V]) UnmarshalJSON

func (d *DefaultDict[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON sets the DefaultDict to the value represented by the JSON encoding.

func (*DefaultDict[K, V]) Update

func (d *DefaultDict[K, V]) Update(another DefaultDict[K, V]) *DefaultDict[K, V]

Update updates the DefaultDict with the items from another DefaultDict.

type Stack

type Stack[T any] interface {
	// Push adds an element to the stack if it is not full, and returns true if successful.
	Push(element T) (ok bool)
	// Pop removes and returns the top element from the stack if it is not empty, and returns true if successful.
	Pop() (element T, ok bool)
	// Peek returns the top element from the stack without removing it if the stack is not empty, and returns true if successful.
	Peek() (element T, ok bool)
	// Empty checks if the stack is empty and returns true if it contains no elements.
	Empty() bool
	// Full checks if the stack is full based on its capacity and returns true if it has reached its capacity.
	Full() bool
}

Stack represents a generic stack data structure with methods for push, pop, peek, check if empty or full.

Example
s := NewStack[int](3)
for i := 0; i < 3; i++ {
	s.Push(i)
}
for i := 0; i < 3; i++ {
	fmt.Println(s.Pop())
}
Output:

2 true
1 true
0 true

func NewStack

func NewStack[T any](capacity int) Stack[T]

NewStack creates a new instance of a generic stack with the given capacity.

Directories

Path Synopsis
Package arraylist provides a resizable array implementation.
Package arraylist provides a resizable array implementation.
Package dict provides a flexible hash map implementation.
Package dict provides a flexible hash map implementation.
Package linkedlist provides a doubly linked list implementation.
Package linkedlist provides a doubly linked list implementation.
Package orderedcontainers provides ordered Dict and Set implementations.
Package orderedcontainers provides ordered Dict and Set implementations.
Package queue provides several implementations of a queue data structure.
Package queue provides several implementations of a queue data structure.
Package set provides a set data structure.
Package set provides a set data structure.
Package sortedcontainers provides sotred data structures.
Package sortedcontainers provides sotred data structures.
sortedlist
Package sortedlist provides a sorted list data structure.
Package sortedlist provides a sorted list data structure.

Jump to

Keyboard shortcuts

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