Documentation
¶
Overview ¶
Package collections provides several convenient data structures.
Index ¶
- type ChainMap
- func (cm *ChainMap) Get(key any) (value any, ok bool)
- func (cm *ChainMap) Items() *dict.Dict
- func (cm *ChainMap) Maps() []*dict.Dict
- func (cm *ChainMap) NewChild() *ChainMap
- func (cm *ChainMap) Parent() *ChainMap
- func (cm *ChainMap) Parents() []*ChainMap
- func (cm *ChainMap) Set(key, value any) *ChainMap
- type Counter
- func (c *Counter) Clear() *Counter
- func (c Counter) Copy() Counter
- func (c *Counter) Elements() arraylist.ArrayList
- func (c Counter) Equal(another Counter) bool
- func (c *Counter) Get(item any) int
- func (c *Counter) Increment(item any, counts ...int) *Counter
- func (c *Counter) LeastCommon() arraylist.ArrayList
- func (c *Counter) MostCommon() arraylist.ArrayList
- func (c *Counter) Remove(item any) (exist bool)
- func (c *Counter) Reset() *Counter
- func (c *Counter) Set(item any, count int) *Counter
- func (c *Counter) SetDefault(count int) *Counter
- func (c *Counter) Subtract(item any, counts ...int) *Counter
- func (c *Counter) Total() (total int)
- type DefaultDict
- func (d *DefaultDict) Clear() *DefaultDict
- func (d DefaultDict) Copy() DefaultDict
- func (d DefaultDict) Equal(another DefaultDict) bool
- func (d *DefaultDict) Get(key any) (value any)
- func (d DefaultDict) MarshalJSON() ([]byte, error)
- func (d *DefaultDict) Pop(key any) (value any)
- func (d *DefaultDict) Set(key, value any) *DefaultDict
- func (d *DefaultDict) SetDefault(value any) *DefaultDict
- func (d DefaultDict) String() string
- func (d *DefaultDict) UnmarshalJSON(data []byte) error
- func (d *DefaultDict) Update(another DefaultDict) *DefaultDict
- type Stack
- type Translator
- func (t *Translator) Clear() *Translator
- func (t Translator) Copy() Translator
- func (t *Translator) Delete(key string) bool
- func (t Translator) Get(key string) (value string)
- func (t Translator) Has(key string) bool
- func (t Translator) Items() [][2]string
- func (t Translator) Keys() []string
- func (t *Translator) Pop(key string) (value string, err error)
- func (t *Translator) PopItem() (key, value string, err error)
- func (t *Translator) Set(key, value string) *Translator
- func (t Translator) Translate(entry string) (result string)
- func (t *Translator) Update(another Translator) *Translator
- func (t Translator) Values() []string
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 ¶
NewChainMap creates and returns a new ChainMap with the provided dictionaries linked as parents.
func (*ChainMap) Get ¶
Get retrieves the value of the key from the current level or its parent dictionaries.
func (*ChainMap) Maps ¶
Maps returns a slice of all the dictionaries in the chain, including the current and parent dictionaries.
func (*ChainMap) NewChild ¶
NewChild creates and returns a new ChainMap with the current level's dictionary linked as the parent.
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 ¶
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 ¶
NewCounter creates and initializes a new Counter with the given items and default count value.
func (Counter) Equal ¶
Equal checks if the Counter is equal to another Counter based on their item counts.
func (*Counter) Get ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Set sets the count of the given item in the Counter and returns the updated Counter.
func (*Counter) SetDefault ¶
SetDefault sets the default count value of the Counter and returns the updated Counter.
func (*Counter) Subtract ¶
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
type DefaultDict ¶
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
type Translator ¶
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. |