sortedcontainers

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package sortedcontainers provides sotred data structures.

Index

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

func NewSortedDict[K cmp.Ordered, V any](f func(a, b K) int, src dict.Dict[K, V]) *SortedDict[K, V]

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

type SortedSet[T cmp.Ordered] struct {
	set.Set[T]
	// contains filtered or unexported fields
}

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

func NewSortedSet[T cmp.Ordered](f func(a, b T) int, entries ...T) *SortedSet[T]

NewSortedSet creates a new SortedSet instance with the given comparison function and initial entries.

func (*SortedSet[T]) Add

func (s *SortedSet[T]) Add(element T) *SortedSet[T]

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

func (s SortedSet[T]) At(index int) (T, error)

At returns the element at the specified index in the SortedSet's sorted sequence.

func (*SortedSet[T]) Clear

func (s *SortedSet[T]) Clear() *SortedSet[T]

Clear removes all elements from the SortedSet.

func (SortedSet[T]) Copy

func (s SortedSet[T]) Copy() SortedSet[T]

Copy creates a shallow copy of the SortedSet.

func (*SortedSet[T]) Discard

func (s *SortedSet[T]) Discard(element T) bool

Discard removes the specified element from the SortedSet.

func (SortedSet[T]) Elements

func (s SortedSet[T]) Elements() []T

Elements returns all elements in the SortedSet as a slice.

func (SortedSet[T]) Equal

func (s SortedSet[T]) Equal(another SortedSet[T]) bool

Equal checks if the SortedSet is equal to the specified SortedSet.

func (SortedSet[T]) IndexOf

func (s SortedSet[T]) IndexOf(element T) int

IndexOf returns the index of the specified element in the SortedSet's sorted sequence.

func (*SortedSet[T]) Pop

func (s *SortedSet[T]) Pop() (element T, err error)

Pop removes and returns the first element from the SortedSet.

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.

func (*SortedSet[T]) Update

func (s *SortedSet[T]) Update(another set.Set[T]) *SortedSet[T]

Update adds all elements from the specified set to the SortedSet.

Example
s := NewSortedSet(nil, 1, 5, 8)
fmt.Println(s.Elements())
s2 := set.Of(3, 6, 9)
s.Update(s2)
fmt.Println(s.Elements())
Output:

[1 5 8]
[1 3 5 6 8 9]

Directories

Path Synopsis
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