Documentation
¶
Overview ¶
Package orderedcontainers provides ordered Dict and Set implementations.
Index ¶
- type OrderedChainDict
- func (d *OrderedChainDict[K, V]) Clear() *OrderedChainDict[K, V]
- func (d OrderedChainDict[K, V]) Copy() OrderedChainDict[K, V]
- func (d *OrderedChainDict[K, V]) Delete(key K) bool
- func (d OrderedChainDict[K, V]) Equal(another OrderedChainDict[K, V]) bool
- func (d OrderedChainDict[K, V]) IndexOf(key K) int
- func (d OrderedChainDict[K, V]) Items() []*dict.DictItem[K, V]
- func (d OrderedChainDict[K, V]) KeyAt(index int) (K, error)
- func (d OrderedChainDict[K, V]) Keys() []K
- func (d OrderedChainDict[K, V]) MarshalJSON() ([]byte, error)
- func (d *OrderedChainDict[K, V]) Pop(key K, args ...V) (value V, err error)
- func (d *OrderedChainDict[K, V]) PopItem() (key K, value V, err error)
- func (d *OrderedChainDict[K, V]) Set(key K, value V) *OrderedChainDict[K, V]
- func (d OrderedChainDict[K, V]) String() string
- func (d *OrderedChainDict[K, V]) UnmarshalJSON(data []byte) (err error)
- func (d *OrderedChainDict[K, V]) Update(another OrderedChainDict[K, V]) *OrderedChainDict[K, V]
- func (d OrderedChainDict[K, V]) Values() []V
- type OrderedChainSet
- func (s *OrderedChainSet[T]) Add(element T) *OrderedChainSet[T]
- func (s OrderedChainSet[T]) At(index int) (T, error)
- func (s *OrderedChainSet[T]) Clear() *OrderedChainSet[T]
- func (s *OrderedChainSet[T]) Copy() OrderedChainSet[T]
- func (s *OrderedChainSet[T]) Discard(element T) bool
- func (s *OrderedChainSet[T]) Elements() []T
- func (s OrderedChainSet[T]) Equal(another OrderedChainSet[T]) bool
- func (s OrderedChainSet[T]) IndexOf(element T) int
- func (s OrderedChainSet[T]) MarshalJSON() ([]byte, error)
- func (s *OrderedChainSet[T]) Pop() (element T, err error)
- func (s OrderedChainSet[T]) String() string
- func (s OrderedChainSet[T]) ToList() linkedlist.LinkedList[T]
- func (s *OrderedChainSet[T]) UnmarshalJSON(data []byte) (err error)
- func (s *OrderedChainSet[T]) Update(another OrderedChainSet[T]) *OrderedChainSet[T]
- type OrderedDict
- func (d *OrderedDict[K, V]) Clear() *OrderedDict[K, V]
- func (d OrderedDict[K, V]) Copy() OrderedDict[K, V]
- func (d *OrderedDict[K, V]) Delete(key K) bool
- func (d OrderedDict[K, V]) Equal(another OrderedDict[K, V]) bool
- func (d OrderedDict[K, V]) IndexOf(key K) int
- func (d OrderedDict[K, V]) Items() []*dict.DictItem[K, V]
- func (d OrderedDict[K, V]) KeyAt(index int) (K, error)
- func (d OrderedDict[K, V]) Keys() []K
- func (d OrderedDict[K, V]) MarshalJSON() ([]byte, error)
- func (d *OrderedDict[K, V]) Pop(key K, args ...V) (value V, err error)
- func (d *OrderedDict[K, V]) PopItem() (key K, value V, err error)
- func (d *OrderedDict[K, V]) Set(key K, value V) *OrderedDict[K, V]
- func (d OrderedDict[K, V]) String() string
- func (d *OrderedDict[K, V]) UnmarshalJSON(data []byte) (err error)
- func (d *OrderedDict[K, V]) Update(another OrderedDict[K, V]) *OrderedDict[K, V]
- func (d OrderedDict[K, V]) Values() []V
- type OrderedSet
- func (s *OrderedSet[T]) Add(element T) *OrderedSet[T]
- func (s OrderedSet[T]) At(index int) (T, error)
- func (s *OrderedSet[T]) Clear() *OrderedSet[T]
- func (s OrderedSet[T]) Copy() OrderedSet[T]
- func (s *OrderedSet[T]) Discard(element T) bool
- func (s OrderedSet[T]) Elements() []T
- func (s OrderedSet[T]) Equal(another OrderedSet[T]) bool
- func (s OrderedSet[T]) IndexOf(element T) int
- func (s OrderedSet[T]) MarshalJSON() ([]byte, error)
- func (s *OrderedSet[T]) Pop() (element T, err error)
- func (s OrderedSet[T]) String() string
- func (s OrderedSet[T]) ToList() arraylist.ArrayList[T]
- func (s *OrderedSet[T]) UnmarshalJSON(data []byte) (err error)
- func (s *OrderedSet[T]) Update(another OrderedSet[T]) *OrderedSet[T]
Examples ¶
- OrderedChainDict
- OrderedChainDict.Copy
- OrderedChainDict.Delete
- OrderedChainDict.Equal
- OrderedChainDict.IndexOf
- OrderedChainDict.Items
- OrderedChainDict.KeyAt
- OrderedChainDict.Keys
- OrderedChainDict.MarshalJSON
- OrderedChainDict.PopItem
- OrderedChainDict.Set
- OrderedChainDict.UnmarshalJSON
- OrderedChainDict.Update
- OrderedChainDict.Values
- OrderedChainSet
- OrderedChainSet.Add
- OrderedChainSet.At
- OrderedChainSet.Copy
- OrderedChainSet.Discard
- OrderedChainSet.Elements
- OrderedChainSet.Equal
- OrderedChainSet.IndexOf
- OrderedChainSet.MarshalJSON
- OrderedChainSet.Pop
- OrderedChainSet.ToList
- OrderedChainSet.UnmarshalJSON
- OrderedChainSet.Update
- OrderedDict
- OrderedDict.Copy
- OrderedDict.Delete
- OrderedDict.Equal
- OrderedDict.IndexOf
- OrderedDict.Items
- OrderedDict.KeyAt
- OrderedDict.Keys
- OrderedDict.MarshalJSON
- OrderedDict.PopItem
- OrderedDict.Set
- OrderedDict.UnmarshalJSON
- OrderedDict.Update
- OrderedDict.Values
- OrderedSet
- OrderedSet.Add
- OrderedSet.At
- OrderedSet.Copy
- OrderedSet.Discard
- OrderedSet.Elements
- OrderedSet.Equal
- OrderedSet.IndexOf
- OrderedSet.MarshalJSON
- OrderedSet.Pop
- OrderedSet.ToList
- OrderedSet.UnmarshalJSON
- OrderedSet.Update
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OrderedChainDict ¶
type OrderedChainDict[K comparable, V any] struct { dict.Dict[K, V] // contains filtered or unexported fields }
OrderedChainDict represents a dictionary with ordered keys and values.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d)
Output: map[c:1 a:2 b:3]
func NewOrderedChainDict ¶
func NewOrderedChainDict[K comparable, V any]() *OrderedChainDict[K, V]
NewOrderedChainDict creates and initializes a new OrderedChainDict.
func (*OrderedChainDict[K, V]) Clear ¶
func (d *OrderedChainDict[K, V]) Clear() *OrderedChainDict[K, V]
Clear removes all elements from the dictionary and sequence.
func (OrderedChainDict[K, V]) Copy ¶
func (d OrderedChainDict[K, V]) Copy() OrderedChainDict[K, V]
Copy creates a new OrderedChainDict with a copy of the dictionary and sequence.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d) d2 := d.Copy() fmt.Println(d2)
Output: map[c:1 a:2 b:3] map[c:1 a:2 b:3]
func (*OrderedChainDict[K, V]) Delete ¶
func (d *OrderedChainDict[K, V]) Delete(key K) bool
Delete removes the specified key and its value from the dictionary and sequence.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d) d.Delete("a") fmt.Println(d)
Output: map[c:1 a:2 b:3] map[c:1 b:3]
func (OrderedChainDict[K, V]) Equal ¶
func (d OrderedChainDict[K, V]) Equal(another OrderedChainDict[K, V]) bool
Equal checks if the current dictionary is equal to another dictionary in terms of keys and values.
Example ¶
d1 := NewOrderedChainDict[string, int]() d1.Set("c", 1) d1.Set("a", 2) d1.Set("b", 3) d2 := NewOrderedChainDict[string, int]() d2.Set("c", 1) d2.Set("b", 3) d2.Set("a", 2) d3 := NewOrderedChainDict[string, int]() d3.Set("c", 1) d3.Set("a", 2) d3.Set("b", 3) fmt.Println(d1.Equal(*d2)) fmt.Println(d1.Equal(*d3))
Output: false true
func (OrderedChainDict[K, V]) IndexOf ¶
func (d OrderedChainDict[K, V]) IndexOf(key K) int
IndexOf returns the index of the specified key in the sequence.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) index := d.IndexOf("a") fmt.Println(index)
Output: 1
func (OrderedChainDict[K, V]) Items ¶
func (d OrderedChainDict[K, V]) Items() []*dict.DictItem[K, V]
Items returns a slice of dict.DictItem in the order of insertion.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) for _, item := range d.Items() { fmt.Println(*item) }
Output: {c 1} {a 2} {b 3}
func (OrderedChainDict[K, V]) KeyAt ¶
func (d OrderedChainDict[K, V]) KeyAt(index int) (K, error)
KeyAt returns the key at the specified index in the sequence.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) key, _ := d.KeyAt(1) fmt.Println(key)
Output: a
func (OrderedChainDict[K, V]) Keys ¶
func (d OrderedChainDict[K, V]) Keys() []K
Keys returns a slice of all keys in the order of insertion.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d.Keys())
Output: [c a b]
func (OrderedChainDict[K, V]) MarshalJSON ¶
func (d OrderedChainDict[K, V]) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of the ordered dictionary.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) data, _ := json.Marshal(d) fmt.Println(string(data))
Output: [["c",1],["a",2],["b",3]]
func (*OrderedChainDict[K, V]) Pop ¶
func (d *OrderedChainDict[K, V]) Pop(key K, args ...V) (value V, err error)
Pop removes the specified key and returns its value from the dictionary and sequence.
func (*OrderedChainDict[K, V]) PopItem ¶
func (d *OrderedChainDict[K, V]) PopItem() (key K, value V, err error)
PopItem removes and returns the last key-value pair from the dictionary and sequence.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d) key, value, _ := d.PopItem() fmt.Println(key, value) fmt.Println(d)
Output: map[c:1 a:2 b:3] b 3 map[c:1 a:2]
func (*OrderedChainDict[K, V]) Set ¶
func (d *OrderedChainDict[K, V]) Set(key K, value V) *OrderedChainDict[K, V]
Set inserts or updates the value for the specified key and updates the sequence.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) fmt.Println(d) d.Set("c", 3) fmt.Println(d)
Output: map[c:1 a:2] map[c:3 a:2]
func (OrderedChainDict[K, V]) String ¶
func (d OrderedChainDict[K, V]) String() string
String returns the string representation of the dictionary in the form of a map.
func (*OrderedChainDict[K, V]) UnmarshalJSON ¶
func (d *OrderedChainDict[K, V]) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON sets the contents of the ordered dictionary to the JSON encoding.
Example ¶
d := NewOrderedChainDict[string, int]() _ = json.Unmarshal([]byte(`[["c",1],["a",2],["b",3]]`), &d) fmt.Println(d)
Output: map[c:1 a:2 b:3]
func (*OrderedChainDict[K, V]) Update ¶
func (d *OrderedChainDict[K, V]) Update(another OrderedChainDict[K, V]) *OrderedChainDict[K, V]
Update merges the elements from another dictionary into the current dictionary and updates the sequence.
Example ¶
d1 := NewOrderedChainDict[string, int]() d1.Set("c", 1) d1.Set("a", 2) d1.Set("b", 3) d2 := NewOrderedChainDict[string, int]() d2.Set("d", 5) d2.Set("b", 4) d2.Set("e", 6) fmt.Println(d1) d1.Update(*d2) fmt.Println(d1)
Output: map[c:1 a:2 b:3] map[c:1 a:2 b:4 d:5 e:6]
func (OrderedChainDict[K, V]) Values ¶
func (d OrderedChainDict[K, V]) Values() []V
Values returns a slice of all values in the order of insertion.
Example ¶
d := NewOrderedChainDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d.Values())
Output: [1 2 3]
type OrderedChainSet ¶
type OrderedChainSet[T comparable] struct { set.Set[T] // contains filtered or unexported fields }
OrderedChainSet represents a set data structure with maintained insertion order.
Example ¶
s := NewOrderedChainSet(1, 3, 2, 4, 5, 2, 3, 6, 9) fmt.Println(s)
Output: {1 3 2 4 5 6 9}
func NewOrderedChainSet ¶
func NewOrderedChainSet[T comparable](entries ...T) *OrderedChainSet[T]
NewOrderedChainSet creates a new OrderedChainSet with the given elements and returns a pointer to it.
func (*OrderedChainSet[T]) Add ¶
func (s *OrderedChainSet[T]) Add(element T) *OrderedChainSet[T]
Add inserts the given element into the OrderedChainSet.
Example ¶
s := NewOrderedChainSet(1, 2, 3) fmt.Println(s) s.Add(4) fmt.Println(s)
Output: {1 2 3} {1 2 3 4}
func (OrderedChainSet[T]) At ¶
func (s OrderedChainSet[T]) At(index int) (T, error)
At returns the element at the specified index in the OrderedChainSet.
Example ¶
s := NewOrderedChainSet(1, 2, 3) fmt.Println(s.At(0)) fmt.Println(s.At(-2)) fmt.Println(s.At(6))
Output: 1 <nil> 2 <nil> 0 the index is out of range
func (*OrderedChainSet[T]) Clear ¶
func (s *OrderedChainSet[T]) Clear() *OrderedChainSet[T]
Clear removes all elements from the OrderedChainSet.
func (*OrderedChainSet[T]) Copy ¶
func (s *OrderedChainSet[T]) Copy() OrderedChainSet[T]
Copy creates a shallow copy of the current OrderedChainSet and returns it.
Example ¶
s := NewOrderedChainSet(1, 2, 3) fmt.Println(s) s2 := s.Copy() fmt.Println(s2)
Output: {1 2 3} {1 2 3}
func (*OrderedChainSet[T]) Discard ¶
func (s *OrderedChainSet[T]) Discard(element T) bool
Discard removes the given element from the OrderedChainSet.
Example ¶
s := NewOrderedChainSet(1, 2, 3) fmt.Println(s) s.Discard(2) fmt.Println(s)
Output: {1 2 3} {1 3}
func (*OrderedChainSet[T]) Elements ¶
func (s *OrderedChainSet[T]) Elements() []T
Elements returns a slice of all elements in the OrderedChainSet.
Example ¶
s := NewOrderedChainSet(1, 2, 3) fmt.Println(s) fmt.Println(s.Elements())
Output: {1 2 3} [1 2 3]
func (OrderedChainSet[T]) Equal ¶
func (s OrderedChainSet[T]) Equal(another OrderedChainSet[T]) bool
Equal checks if the current OrderedChainSet is equal to another OrderedChainSet.
Example ¶
s := NewOrderedChainSet(1, 2, 3) s2 := NewOrderedChainSet(1, 2, 3) fmt.Println(s.Equal(*s2)) s3 := NewOrderedChainSet(1, 3, 2) fmt.Println(s.Equal(*s3))
Output: true false
func (OrderedChainSet[T]) IndexOf ¶
func (s OrderedChainSet[T]) IndexOf(element T) int
IndexOf returns the index of the given element in the OrderedChainSet.
Example ¶
s := NewOrderedChainSet(1, 2, 3) fmt.Println(s.IndexOf(2)) fmt.Println(s.IndexOf(4))
Output: 1 -1
func (OrderedChainSet[T]) MarshalJSON ¶
func (s OrderedChainSet[T]) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of the OrderedChainSet.
Example ¶
s := NewOrderedChainSet(1, 2, 3) data, _ := json.Marshal(&s) fmt.Println(string(data))
Output: [1,2,3]
func (*OrderedChainSet[T]) Pop ¶
func (s *OrderedChainSet[T]) Pop() (element T, err error)
Pop removes and returns the first element from the OrderedChainSet.
Example ¶
ExampleOrderedChainSet_Pop demonstrates how to pop the last element from the OrderedSet.
s := NewOrderedChainSet(1, 2, 3) fmt.Println(s) element, _ := s.Pop() fmt.Println(element) fmt.Println(s)
Output: {1 2 3} 3 {1 2}
func (OrderedChainSet[T]) String ¶
func (s OrderedChainSet[T]) String() string
String returns the string representation of the OrderedChainSet.
func (OrderedChainSet[T]) ToList ¶
func (s OrderedChainSet[T]) ToList() linkedlist.LinkedList[T]
ToList returns a copy of the sequence as a linked list.
Example ¶
s := NewOrderedChainSet(1, 2, 3) fmt.Println(s) fmt.Println(s.ToList())
Output: {1 2 3} [1 2 3]
func (*OrderedChainSet[T]) UnmarshalJSON ¶
func (s *OrderedChainSet[T]) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON parses the JSON-encoded data and updates the OrderedChainSet with the parsed elements.
Example ¶
s := NewOrderedChainSet[int]() _ = json.Unmarshal([]byte("[1,2,3]"), &s) fmt.Println(s)
Output: {1 2 3}
func (*OrderedChainSet[T]) Update ¶
func (s *OrderedChainSet[T]) Update(another OrderedChainSet[T]) *OrderedChainSet[T]
Update adds all elements from another OrderedChainSet to the current OrderedChainSet.
Example ¶
s := NewOrderedChainSet(1, 2, 3) fmt.Println(s) as := NewOrderedChainSet(4, 3, 5) s.Update(*as) fmt.Println(s)
Output: {1 2 3} {1 2 3 4 5}
type OrderedDict ¶
type OrderedDict[K comparable, V any] struct { dict.Dict[K, V] // contains filtered or unexported fields }
OrderedDict represents a dictionary with preserved insertion order.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d)
Output: map[c:1 a:2 b:3]
func NewOrderedDict ¶
func NewOrderedDict[K comparable, V any]() *OrderedDict[K, V]
NewOrderedDict creates and returns a new instance of OrderedDict.
func (*OrderedDict[K, V]) Clear ¶
func (d *OrderedDict[K, V]) Clear() *OrderedDict[K, V]
Clear removes all key-value pairs from the OrderedDict.
func (OrderedDict[K, V]) Copy ¶
func (d OrderedDict[K, V]) Copy() OrderedDict[K, V]
Copy creates and returns a deep copy of the OrderedDict.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d) d2 := d.Copy() fmt.Println(d2)
Output: map[c:1 a:2 b:3] map[c:1 a:2 b:3]
func (*OrderedDict[K, V]) Delete ¶
func (d *OrderedDict[K, V]) Delete(key K) bool
Delete removes the specified key and its associated value from the OrderedDict.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d) d.Delete("a") fmt.Println(d)
Output: map[c:1 a:2 b:3] map[c:1 b:3]
func (OrderedDict[K, V]) Equal ¶
func (d OrderedDict[K, V]) Equal(another OrderedDict[K, V]) bool
Equal checks if two OrderedDict instances are equal in terms of key-value pairs and insertion order.
Example ¶
d1 := NewOrderedDict[string, int]() d1.Set("c", 1) d1.Set("a", 2) d1.Set("b", 3) d2 := NewOrderedDict[string, int]() d2.Set("c", 1) d2.Set("b", 3) d2.Set("a", 2) d3 := NewOrderedDict[string, int]() d3.Set("c", 1) d3.Set("a", 2) d3.Set("b", 3) fmt.Println(d1.Equal(*d2)) fmt.Println(d1.Equal(*d3))
Output: false true
func (OrderedDict[K, V]) IndexOf ¶
func (d OrderedDict[K, V]) IndexOf(key K) int
IndexOf returns the index of the specified key in the insertion order.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) index := d.IndexOf("a") fmt.Println(index)
Output: 1
func (OrderedDict[K, V]) Items ¶
func (d OrderedDict[K, V]) Items() []*dict.DictItem[K, V]
Items returns a slice of dict.DictItem pointers, containing all key-value pairs in the OrderedDict.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) for _, item := range d.Items() { fmt.Println(*item) }
Output: {c 1} {a 2} {b 3}
func (OrderedDict[K, V]) KeyAt ¶
func (d OrderedDict[K, V]) KeyAt(index int) (K, error)
KeyAt returns the key at the specified index in the insertion order.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) key, _ := d.KeyAt(1) fmt.Println(key)
Output: a
func (OrderedDict[K, V]) Keys ¶
func (d OrderedDict[K, V]) Keys() []K
Keys returns a slice containing all the keys in the OrderedDict.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d.Keys())
Output: [c a b]
func (OrderedDict[K, V]) MarshalJSON ¶
func (d OrderedDict[K, V]) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of the OrderedDict.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) data, _ := json.Marshal(d) fmt.Println(string(data))
Output: [["c",1],["a",2],["b",3]]
func (*OrderedDict[K, V]) Pop ¶
func (d *OrderedDict[K, V]) Pop(key K, args ...V) (value V, err error)
Pop removes the specified key and returns its associated value from the OrderedDict.
func (*OrderedDict[K, V]) PopItem ¶
func (d *OrderedDict[K, V]) PopItem() (key K, value V, err error)
PopItem removes and returns the key-value pair at the insertion end of the OrderedDict.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d) key, value, _ := d.PopItem() fmt.Println(key, value) fmt.Println(d)
Output: map[c:1 a:2 b:3] b 3 map[c:1 a:2]
func (*OrderedDict[K, V]) Set ¶
func (d *OrderedDict[K, V]) Set(key K, value V) *OrderedDict[K, V]
Set adds or updates a key-value pair in the OrderedDict.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) fmt.Println(d) d.Set("c", 3) fmt.Println(d)
Output: map[c:1 a:2] map[c:3 a:2]
func (OrderedDict[K, V]) String ¶
func (d OrderedDict[K, V]) String() string
String returns the string representation of the OrderedDict in the format of a map.
func (*OrderedDict[K, V]) UnmarshalJSON ¶
func (d *OrderedDict[K, V]) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON parses the JSON-encoded data and stores the result in the OrderedDict.
Example ¶
d := NewOrderedDict[string, int]() _ = json.Unmarshal([]byte(`[["c",1],["a",2],["b",3]]`), &d) fmt.Println(d)
Output: map[c:1 a:2 b:3]
func (*OrderedDict[K, V]) Update ¶
func (d *OrderedDict[K, V]) Update(another OrderedDict[K, V]) *OrderedDict[K, V]
Update adds or updates the key-value pairs from another OrderedDict into the current OrderedDict.
Example ¶
d1 := NewOrderedDict[string, int]() d1.Set("c", 1) d1.Set("a", 2) d1.Set("b", 3) d2 := NewOrderedDict[string, int]() d2.Set("d", 5) d2.Set("b", 4) d2.Set("e", 6) fmt.Println(d1) d1.Update(*d2) fmt.Println(d1)
Output: map[c:1 a:2 b:3] map[c:1 a:2 b:4 d:5 e:6]
func (OrderedDict[K, V]) Values ¶
func (d OrderedDict[K, V]) Values() []V
Values returns a slice containing all the values in the OrderedDict, in the same order as the keys.
Example ¶
d := NewOrderedDict[string, int]() d.Set("c", 1) d.Set("a", 2) d.Set("b", 3) fmt.Println(d.Values())
Output: [1 2 3]
type OrderedSet ¶
type OrderedSet[T comparable] struct { set.Set[T] // contains filtered or unexported fields }
OrderedSet represents a set with elements in a specific order.
Example ¶
s := NewOrderedSet(1, 3, 2, 4, 5, 2, 3, 6, 9) fmt.Println(s)
Output: {1 3 2 4 5 6 9}
func NewOrderedSet ¶
func NewOrderedSet[T comparable](entries ...T) *OrderedSet[T]
NewOrderedSet creates a new OrderedSet with the provided elements, eliminating duplicates and maintaining the order of insertion.
func (*OrderedSet[T]) Add ¶
func (s *OrderedSet[T]) Add(element T) *OrderedSet[T]
Add adds the specified element to the OrderedSet if it does not exist and returns the updated OrderedSet.
Example ¶
s := NewOrderedSet(1, 2, 3) fmt.Println(s) s.Add(4) fmt.Println(s)
Output: {1 2 3} {1 2 3 4}
func (OrderedSet[T]) At ¶
func (s OrderedSet[T]) At(index int) (T, error)
At returns the element at the specified index in the sequence and any error encountered during the operation.
Example ¶
s := NewOrderedSet(1, 2, 3) fmt.Println(s.At(0)) fmt.Println(s.At(-2)) fmt.Println(s.At(6))
Output: 1 <nil> 2 <nil> 0 the index is out of range
func (*OrderedSet[T]) Clear ¶
func (s *OrderedSet[T]) Clear() *OrderedSet[T]
Clear removes all elements from the OrderedSet and returns the updated empty set.
func (OrderedSet[T]) Copy ¶
func (s OrderedSet[T]) Copy() OrderedSet[T]
Copy creates a new copy of the OrderedSet with the same elements and sequence.
Example ¶
s := NewOrderedSet(1, 2, 3) fmt.Println(s) s2 := s.Copy() fmt.Println(s2)
Output: {1 2 3} {1 2 3}
func (*OrderedSet[T]) Discard ¶
func (s *OrderedSet[T]) Discard(element T) bool
Discard removes the specified element from the OrderedSet and returns true if the removal was successful, false otherwise.
Example ¶
s := NewOrderedSet(1, 2, 3) fmt.Println(s) s.Discard(2) fmt.Println(s)
Output: {1 2 3} {1 3}
func (OrderedSet[T]) Elements ¶
func (s OrderedSet[T]) Elements() []T
Elements returns a copy of the elements in the OrderedSet as a slice.
Example ¶
s := NewOrderedSet(1, 2, 3) fmt.Println(s) fmt.Println(s.Elements())
Output: {1 2 3} [1 2 3]
func (OrderedSet[T]) Equal ¶
func (s OrderedSet[T]) Equal(another OrderedSet[T]) bool
Equal checks if the elements and their order in the current and another OrderedSet are the same.
Example ¶
s := NewOrderedSet(1, 2, 3) s2 := NewOrderedSet(1, 2, 3) fmt.Println(s.Equal(*s2)) s3 := NewOrderedSet(1, 3, 2) fmt.Println(s.Equal(*s3))
Output: true false
func (OrderedSet[T]) IndexOf ¶
func (s OrderedSet[T]) IndexOf(element T) int
IndexOf returns the first index of the specified element in the sequence, or -1 if the element is not found.
Example ¶
s := NewOrderedSet(1, 2, 3) fmt.Println(s.IndexOf(2)) fmt.Println(s.IndexOf(4))
Output: 1 -1
func (OrderedSet[T]) MarshalJSON ¶
func (s OrderedSet[T]) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of the sequence within the OrderedSet.
Example ¶
s := NewOrderedSet(1, 2, 3) data, _ := json.Marshal(&s) fmt.Println(string(data))
Output: [1,2,3]
func (*OrderedSet[T]) Pop ¶
func (s *OrderedSet[T]) Pop() (element T, err error)
Pop removes and returns the last element from the OrderedSet and any error encountered during the operation.
Example ¶
ExampleOrderedSet_Pop demonstrates how to pop the last element from the OrderedSet.
s := NewOrderedSet(1, 2, 3) fmt.Println(s) element, _ := s.Pop() fmt.Println(element) fmt.Println(s)
Output: {1 2 3} 3 {1 2}
func (OrderedSet[T]) String ¶
func (s OrderedSet[T]) String() string
String returns the string representation of the sequence within the OrderedSet.
func (OrderedSet[T]) ToList ¶
func (s OrderedSet[T]) ToList() arraylist.ArrayList[T]
ToList returns a copy of the sequence in the form of an ArrayList.
Example ¶
s := NewOrderedSet(1, 2, 3) fmt.Println(s) fmt.Println(s.ToList())
Output: {1 2 3} [1 2 3]
func (*OrderedSet[T]) UnmarshalJSON ¶
func (s *OrderedSet[T]) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON sets the elements and sequence in the OrderedSet based on the provided JSON data.
Example ¶
s := NewOrderedSet[int]() _ = json.Unmarshal([]byte("[1,2,3]"), &s) fmt.Println(s)
Output: {1 2 3}
func (*OrderedSet[T]) Update ¶
func (s *OrderedSet[T]) Update(another OrderedSet[T]) *OrderedSet[T]
Update adds all elements from another OrderedSet to the current OrderedSet and returns the updated set.
Example ¶
s := NewOrderedSet(1, 2, 3) fmt.Println(s) as := NewOrderedSet(4, 3, 5) s.Update(*as) fmt.Println(s)
Output: {1 2 3} {1 2 3 4 5}