Documentation ¶
Overview ¶
Package sortedcontainers provides sotred data structures.
Index ¶
- type SortedDict
- func (d *SortedDict[K, V]) Clear() *SortedDict[K, V]
- func (d SortedDict[K, V]) Copy() SortedDict[K, V]
- func (d *SortedDict[K, V]) Delete(key K) bool
- func (d SortedDict[K, V]) Equal(another SortedDict[K, V]) bool
- func (d SortedDict[K, V]) IndexOf(key K) int
- func (d SortedDict[K, V]) Items() []*dict.DictItem[K, V]
- func (d SortedDict[K, V]) KeyAt(index int) (K, error)
- func (d SortedDict[K, V]) Keys() []K
- func (d *SortedDict[K, V]) Pop(key K, args ...V) (value V, err error)
- func (d *SortedDict[K, V]) PopItem() (key K, value V, err error)
- func (d *SortedDict[K, V]) Set(key K, value V) *SortedDict[K, V]
- func (d *SortedDict[K, V]) Update(another dict.Dict[K, V]) *SortedDict[K, V]
- func (d SortedDict[K, V]) Values() []V
- type SortedSet
- func (s *SortedSet[T]) Add(element T) *SortedSet[T]
- func (s SortedSet[T]) At(index int) (T, error)
- func (s *SortedSet[T]) Clear() *SortedSet[T]
- func (s SortedSet[T]) Copy() SortedSet[T]
- func (s *SortedSet[T]) Discard(element T) bool
- func (s SortedSet[T]) Elements() []T
- func (s SortedSet[T]) Equal(another SortedSet[T]) bool
- func (s SortedSet[T]) IndexOf(element T) int
- func (s *SortedSet[T]) Pop() (element T, err error)
- func (s SortedSet[T]) ToList() sortedlist.SortedList[T]
- func (s *SortedSet[T]) Update(another set.Set[T]) *SortedSet[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SortedDict ¶
type SortedDict[K cmp.Ordered, V any] struct { dict.Dict[K, V] // contains filtered or unexported fields }
SortedDict is a data structure that represents a dictionary with sorted keys.
Example ¶
d := NewSortedDict(nil, dict.Dict[string, int]{"c": 1, "a": 2, "b": 3, "d": 4}) fmt.Println(d.Keys()) fmt.Println(d.Values()) for _, item := range d.Items() { fmt.Println(*item) }
Output: [a b c d] [2 3 1 4] {a 2} {b 3} {c 1} {d 4}
func NewSortedDict ¶
NewSortedDict creates a new SortedDict instance with the provided comparison function and initializes it with the given dictionary or an empty one if not provided.
func (*SortedDict[K, V]) Clear ¶
func (d *SortedDict[K, V]) Clear() *SortedDict[K, V]
Clear removes all key-value pairs from the SortedDict.
func (SortedDict[K, V]) Copy ¶
func (d SortedDict[K, V]) Copy() SortedDict[K, V]
Copy creates and returns a new SortedDict instance that is an exact copy of the original SortedDict.
func (*SortedDict[K, V]) Delete ¶
func (d *SortedDict[K, V]) Delete(key K) bool
Delete removes the key-value pair corresponding to the specified key from the SortedDict.
func (SortedDict[K, V]) Equal ¶
func (d SortedDict[K, V]) Equal(another SortedDict[K, V]) bool
Equal compares the SortedDict with another SortedDict to check if they are equal in terms of contents and order.
func (SortedDict[K, V]) IndexOf ¶
func (d SortedDict[K, V]) IndexOf(key K) int
IndexOf returns the index of the specified key in the sorted sequence of keys stored in the SortedDict.
func (SortedDict[K, V]) Items ¶
func (d SortedDict[K, V]) Items() []*dict.DictItem[K, V]
Items returns an array containing all the key-value pairs in the SortedDict in the sorted key order.
func (SortedDict[K, V]) KeyAt ¶
func (d SortedDict[K, V]) KeyAt(index int) (K, error)
KeyAt returns the key at the specified index in the sorted sequence of keys stored in the SortedDict.
func (SortedDict[K, V]) Keys ¶
func (d SortedDict[K, V]) Keys() []K
Keys returns a slice containing all the keys in the SortedDict in sorted order.
func (*SortedDict[K, V]) Pop ¶
func (d *SortedDict[K, V]) Pop(key K, args ...V) (value V, err error)
Pop removes and returns the value associated with the specified key from the SortedDict.
func (*SortedDict[K, V]) PopItem ¶
func (d *SortedDict[K, V]) PopItem() (key K, value V, err error)
PopItem removes and returns the last key-value pair from the SortedDict based on the sorted keys.
func (*SortedDict[K, V]) Set ¶
func (d *SortedDict[K, V]) Set(key K, value V) *SortedDict[K, V]
Set adds or updates a key-value pair in the SortedDict and ensures that the key is maintained in the sorting sequence.
Example ¶
d := NewSortedDict[string, int](nil, nil) d.Set("c", 1) d.Set("a", 2) fmt.Println(d.Keys()) fmt.Println(d.Values()) d.Set("c", 3) fmt.Println(d.Keys()) fmt.Println(d.Values())
Output: [a c] [2 1] [a c] [2 3]
func (*SortedDict[K, V]) Update ¶
func (d *SortedDict[K, V]) Update(another dict.Dict[K, V]) *SortedDict[K, V]
Update adds or updates multiple key-value pairs from another dictionary into the SortedDict.
Example ¶
d1 := NewSortedDict[string, int](nil, dict.Dict[string, int]{"c": 1, "a": 2, "b": 3}) fmt.Println(d1.Keys()) fmt.Println(d1.Values()) d2 := dict.Dict[string, int]{"d": 5, "b": 4, "e": 6} d1.Update(d2) fmt.Println(d1.Keys()) fmt.Println(d1.Values())
Output: [a b c] [2 3 1] [a b c d e] [2 4 1 5 6]
func (SortedDict[K, V]) Values ¶
func (d SortedDict[K, V]) Values() []V
Values returns a slice containing all the values in the SortedDict in the corresponding key order.
type SortedSet ¶
SortedSet represents a set of elements sorted according to the specified comparison function.
Example ¶
s := NewSortedSet(nil, 1, 3, 2, 4, 5, 2, 3, 6, 9) fmt.Println(s.Elements())
Output: [1 2 3 4 5 6 9]
func NewSortedSet ¶
NewSortedSet creates a new SortedSet instance with the given comparison function and initial entries.
func (*SortedSet[T]) Add ¶
Add adds the given element to the SortedSet if it does not already exist.
Example ¶
s := NewSortedSet(nil, 1, 5, 8) fmt.Println(s.Elements()) s.Add(3) fmt.Println(s.Elements())
Output: [1 5 8] [1 3 5 8]
func (SortedSet[T]) At ¶
At returns the element at the specified index in the SortedSet's sorted sequence.
func (SortedSet[T]) Elements ¶
func (s SortedSet[T]) Elements() []T
Elements returns all elements in the SortedSet as a slice.
func (SortedSet[T]) IndexOf ¶
IndexOf returns the index of the specified element in the SortedSet's sorted sequence.
func (SortedSet[T]) ToList ¶
func (s SortedSet[T]) ToList() sortedlist.SortedList[T]
ToList returns a copy of the SortedSet's sorted sequence as a SortedList.
Directories ¶
Path | Synopsis |
---|---|
Package sortedlist provides a sorted list data structure.
|
Package sortedlist provides a sorted list data structure. |