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: 9 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 struct {
	// contains filtered or unexported fields
}

ChainMap struct represents a chain of Dict where a new Dict is linked to its parent.

Example
d1 := dict.Dict{"a": 1, "b": 2}
d2 := dict.Dict{"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
<nil> false
3 true

func NewChainMap

func NewChainMap(maps ...*dict.Dict) *ChainMap

NewChainMap creates and returns a new ChainMap with the provided dictionaries linked as parents.

func (*ChainMap) Get

func (cm *ChainMap) Get(key any) (value any, ok bool)

Get retrieves the value of the key from the current level or its parent dictionaries.

func (*ChainMap) Items

func (cm *ChainMap) Items() *dict.Dict

Items returns the dictionary of key-value pairs for the current level.

func (*ChainMap) Maps

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

Maps returns a slice of all the dictionaries in the chain, including the current and parent dictionaries.

func (*ChainMap) NewChild

func (cm *ChainMap) NewChild() *ChainMap

NewChild creates and returns a new ChainMap with the current level's dictionary linked as the parent.

func (*ChainMap) Parent

func (cm *ChainMap) Parent() *ChainMap

Parent returns the parent ChainMap.

func (*ChainMap) Parents

func (cm *ChainMap) Parents() []*ChainMap

Parents returns a slice of all the parent ChainMaps in the chain.

func (*ChainMap) Set

func (cm *ChainMap) Set(key, value any) *ChainMap

Set sets the key-value pair in the current level's dictionary and returns the ChainMap.

type Counter

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

Counter struct represents a counter that counts the occurrences of items and provides various operations based on these counts.

Example
l := arraylist.ArrayList{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(counters ...*Counter) *Counter

MergeCounts merges multiple Counters into a single Counter based on their item counts.

Example
l1 := arraylist.ArrayList{1, 2, 2, 2, 3, 3, 4, 4, 5}
c1 := NewCounter(l1)
l2 := arraylist.ArrayList{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(items arraylist.ArrayList, defaultCounts ...int) *Counter

NewCounter creates and initializes a new Counter with the given items and default count value.

func (*Counter) Clear

func (c *Counter) Clear() *Counter

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

func (Counter) Copy

func (c Counter) Copy() Counter

Copy creates a deep copy of the Counter.

func (*Counter) Elements

func (c *Counter) Elements() arraylist.ArrayList

Elements returns a list of all distinct items in the Counter.

func (Counter) Equal

func (c Counter) Equal(another Counter) bool

Equal checks if the Counter is equal to another Counter based on their item counts.

func (*Counter) Get

func (c *Counter) Get(item any) int

Get gets the count of the given item in the Counter. If the item is not in the Counter, the default count value is returned.

func (*Counter) Increment

func (c *Counter) Increment(item any, counts ...int) *Counter

Increment increments the count of the given item in the Counter by the specified amount and returns the updated Counter.

Example
l := arraylist.ArrayList{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) LeastCommon

func (c *Counter) LeastCommon() arraylist.ArrayList

LeastCommon returns a list of items with the lowest count in the Counter.

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

[5]

func (*Counter) MostCommon

func (c *Counter) MostCommon() arraylist.ArrayList

MostCommon returns a list of items with the highest count in the Counter.

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

[2]

func (*Counter) Remove

func (c *Counter) Remove(item any) (exist bool)

Remove removes the given item from the Counter and returns true if the item was in the Counter, false otherwise.

Example
l := arraylist.ArrayList{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) Reset

func (c *Counter) Reset() *Counter

Reset resets the counts of all items in the Counter to the default count value and returns the updated Counter.

Example
l := arraylist.ArrayList{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) Set

func (c *Counter) Set(item any, count int) *Counter

Set sets the count of the given item in the Counter and returns the updated Counter.

func (*Counter) SetDefault

func (c *Counter) SetDefault(count int) *Counter

SetDefault sets the default count value of the Counter and returns the updated Counter.

func (*Counter) Subtract

func (c *Counter) Subtract(item any, counts ...int) *Counter

Subtract decrements the count of the given item in the Counter by the specified amount and returns the updated Counter.

Example
l := arraylist.ArrayList{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) Total

func (c *Counter) Total() (total int)

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

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

9

type DefaultDict

type DefaultDict struct {
	dict.Dict
	// contains filtered or unexported fields
}

DefaultDict is a Dict with a default value.

Example
d := dict.Dict{"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(items dict.Dict, defaultVal any) *DefaultDict

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

func (*DefaultDict) Clear

func (d *DefaultDict) Clear() *DefaultDict

Clear removes all items from the DefaultDict.

func (DefaultDict) Copy

func (d DefaultDict) Copy() DefaultDict

Copy creates a copy of the DefaultDict.

func (DefaultDict) Equal

func (d DefaultDict) Equal(another DefaultDict) bool

Equal checks if two DefaultDicts are equal.

func (*DefaultDict) Get

func (d *DefaultDict) Get(key any) (value any)

Get retrieves the value for the specified key. If the key does not exist, it returns the default value.

func (DefaultDict) MarshalJSON

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

MarshalJSON returns the JSON encoding of the DefaultDict.

func (*DefaultDict) Pop

func (d *DefaultDict) Pop(key any) (value any)

Pop removes the specified key and returns its corresponding value. If the key does not exist, it returns the default value.

func (*DefaultDict) Set

func (d *DefaultDict) Set(key, value any) *DefaultDict

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

func (*DefaultDict) SetDefault

func (d *DefaultDict) SetDefault(value any) *DefaultDict

SetDefault sets the default value for the DefaultDict.

func (DefaultDict) String

func (d DefaultDict) String() string

String returns the string representation of the DefaultDict.

func (*DefaultDict) UnmarshalJSON

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

UnmarshalJSON sets the value of the DefaultDict based on the input JSON data.

func (*DefaultDict) Update

func (d *DefaultDict) Update(another DefaultDict) *DefaultDict

Update updates the DefaultDict with the key-value pairs from another DefaultDict.

type Stack

type Stack interface {
	// Push adds an element to the top of the stack if it's not full
	Push(element any) (ok bool)
	// Pop removes and returns the top element from the stack if it's not empty
	Pop() (element any, ok bool)
	// Peek returns the top element of the stack without removing it if the stack is not empty
	Peek() (element any, ok bool)
	// Empty checks if the stack is empty
	Empty() bool
	// Full checks if the stack is full based on its capacity
	Full() bool
}

Stack interface represents a stack data structure

Example
s := NewStack(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(capacity int) Stack

NewStack creates a new stack with the specified capacity

type Translator

type Translator struct {
	dict.Dict
}

Translator is a struct that represents a dictionary for translating words or characters.

Example
trans := NewTranslator([2]string{"a", "A"}, [2]string{"b", "B"}, [2]string{"c", "C"})
fmt.Println(trans.Translate("abcdefg"))
Output:

ABCdefg

func NewTranslator

func NewTranslator(entries ...[2]string) *Translator

NewTranslator creates a new Translator with the provided entries.

func (*Translator) Clear

func (t *Translator) Clear() *Translator

Clear removes all entries from the Translator.

func (Translator) Copy

func (t Translator) Copy() Translator

Copy creates and returns a copy of the Translator.

func (*Translator) Delete

func (t *Translator) Delete(key string) bool

Delete removes the entry with the specified key from the Translator.

func (Translator) Get

func (t Translator) Get(key string) (value string)

Get retrieves the value associated with the specified key from the Translator.

func (Translator) Has

func (t Translator) Has(key string) bool

Has checks if the Translator contains the specified key.

func (Translator) Items

func (t Translator) Items() [][2]string

Items returns all key-value pairs from the Translator as a slice of [2]string arrays.

func (Translator) Keys

func (t Translator) Keys() []string

Keys returns all keys from the Translator as a slice of strings.

func (*Translator) Pop

func (t *Translator) Pop(key string) (value string, err error)

Pop removes the entry with the specified key from the Translator and returns its value.

func (*Translator) PopItem

func (t *Translator) PopItem() (key, value string, err error)

PopItem removes and returns an arbitrary entry from the Translator as a key-value pair.

func (*Translator) Set

func (t *Translator) Set(key, value string) *Translator

Set adds or updates the entry with the specified key and value in the Translator.

func (Translator) Translate

func (t Translator) Translate(entry string) (result string)

Translate translates the input string using the mappings in the Translator and returns the result.

func (*Translator) Update

func (t *Translator) Update(another Translator) *Translator

Update merges the entries from another Translator into the current Translator.

func (Translator) Values

func (t Translator) Values() []string

Values returns all values from the Translator as a slice of strings.

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 list provides functions for working with lists.
Package list provides functions for working with lists.
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.

Jump to

Keyboard shortcuts

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