orderedcontainers

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: 8 Imported by: 0

Documentation

Overview

Package orderedcontainers provides ordered Dict and Set implementations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OrderedChainDict

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

OrderedChainDict represents a dictionary with ordered keys, which is based on a Dict and a LinkedList.

Example
d := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
fmt.Println(d)
Output:

map[c:1 a:2 b:3]

func NewOrderedChainDict

func NewOrderedChainDict(entries ...[2]any) *OrderedChainDict

NewOrderedChainDict creates a new OrderedChainDict with the given key-value entries.

func (*OrderedChainDict) Clear

func (d *OrderedChainDict) Clear() *OrderedChainDict

Clear clears the OrderedChainDict and returns the modified instance.

func (OrderedChainDict) Copy

Copy creates a deep copy of the OrderedChainDict.

Example
d := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"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) Delete

func (d *OrderedChainDict) Delete(key any) bool

Delete deletes the key from the OrderedChainDict and returns true if successful, false otherwise.

Example
d := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"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) Equal

func (d OrderedChainDict) Equal(another OrderedChainDict) bool

Equal checks if two OrderedChainDict instances are equal.

Example
d1 := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
d2 := NewOrderedChainDict([2]any{"c", 1}, [2]any{"b", 3}, [2]any{"a", 2})
d3 := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
fmt.Println(d1.Equal(*d2))
fmt.Println(d1.Equal(*d3))
Output:

false
true

func (OrderedChainDict) IndexOf

func (d OrderedChainDict) IndexOf(key any) int

IndexOf returns the index of the specified key in the OrderedChainDict.

Example
d := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
index := d.IndexOf("a")
fmt.Println(index)
Output:

1

func (OrderedChainDict) Items

func (d OrderedChainDict) Items() [][2]any

Items returns the key-value pairs of the OrderedChainDict as an array of tuples.

Example
d := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
fmt.Println(d.Items())
Output:

[[c 1] [a 2] [b 3]]

func (OrderedChainDict) KeyAt

func (d OrderedChainDict) KeyAt(index int) (any, error)

KeyAt returns the key at the specified index in the OrderedChainDict.

Example
d := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
key, _ := d.KeyAt(1)
fmt.Println(key)
Output:

a

func (OrderedChainDict) Keys

func (d OrderedChainDict) Keys() []any

Keys returns the keys of the OrderedChainDict as an array.

Example
d := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
fmt.Println(d.Keys())
Output:

[c a b]

func (OrderedChainDict) MarshalJSON

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

MarshalJSON returns the JSON encoding of the OrderedChainDict.

Example
d := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
data, _ := json.Marshal(d)
fmt.Println(string(data))
Output:

[["c",1],["a",2],["b",3]]

func (*OrderedChainDict) Pop

func (d *OrderedChainDict) Pop(key any, args ...any) (value any, err error)

Pop removes the key and returns the corresponding value from the OrderedChainDict.

func (*OrderedChainDict) PopItem

func (d *OrderedChainDict) PopItem() (key, value any, err error)

PopItem removes and returns the last key-value pair from the OrderedChainDict.

Example
d := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"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) Set

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

Set sets the key-value pair in the OrderedChainDict and returns the modified instance.

Example
d := NewOrderedChainDict()
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) String

func (d OrderedChainDict) String() string

String returns the string representation of the OrderedChainDict.

func (*OrderedChainDict) UnmarshalJSON

func (d *OrderedChainDict) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON sets the OrderedChainDict from its JSON representation.

Example
d := NewOrderedChainDict()
_ = json.Unmarshal([]byte(`[["c",1],["a",2],["b",3]]`), &d)
fmt.Println(d)
Output:

map[c:1 a:2 b:3]

func (*OrderedChainDict) Update

Update merges an OrderedChainDict into the current OrderedChainDict and returns the modified instance.

Example
d1 := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
d2 := NewOrderedChainDict([2]any{"d", 5}, [2]any{"b", 4}, [2]any{"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) Values

func (d OrderedChainDict) Values() []any

Values returns the values of the OrderedChainDict as an array.

Example
d := NewOrderedChainDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
fmt.Println(d.Values())
Output:

[1 2 3]

type OrderedChainSet

type OrderedChainSet struct {
	set.Set
	// contains filtered or unexported fields
}

OrderedChainSet represents a set data structure with elements kept in insertion order, which is based on a Set and a LinkedList.

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(entries ...any) *OrderedChainSet

NewOrderedChainSet creates and initializes a new OrderedChainSet with the specified elements.

func (*OrderedChainSet) Add

func (s *OrderedChainSet) Add(element any) *OrderedChainSet

Add adds the specified element to the OrderedChainSet if it is not already present.

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) At

func (s OrderedChainSet) At(index int) (any, error)

At returns the element at the specified index in the insertion order.

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>
<nil> the index is out of range

func (*OrderedChainSet) Clear

func (s *OrderedChainSet) Clear() *OrderedChainSet

Clear removes all elements from the OrderedChainSet.

func (*OrderedChainSet) Copy

func (s *OrderedChainSet) Copy() OrderedChainSet

Copy returns a new copy of the OrderedChainSet.

Example
s := NewOrderedChainSet(1, 2, 3)
fmt.Println(s)
s2 := s.Copy()
fmt.Println(s2)
Output:

{1 2 3}
{1 2 3}

func (*OrderedChainSet) Discard

func (s *OrderedChainSet) Discard(element any) bool

Discard removes the specified 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) Elements

func (s *OrderedChainSet) Elements() []any

Elements returns all elements of the OrderedChainSet in insertion order.

Example
s := NewOrderedChainSet(1, 2, 3)
fmt.Println(s)
fmt.Println(s.Elements())
Output:

{1 2 3}
[1 2 3]

func (OrderedChainSet) Equal

func (s OrderedChainSet) Equal(another OrderedChainSet) bool

Equal checks if this 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) IndexOf

func (s OrderedChainSet) IndexOf(element any) int

IndexOf returns the index of the specified element in the insertion order.

Example
s := NewOrderedChainSet(1, 2, 3)
fmt.Println(s.IndexOf(2))
fmt.Println(s.IndexOf(4))
Output:

1
-1

func (OrderedChainSet) MarshalJSON

func (s OrderedChainSet) MarshalJSON() ([]byte, error)

MarshalJSON converts the OrderedChainSet to JSON.

Example
s := NewOrderedChainSet(1, 2, 3)
data, _ := json.Marshal(&s)
fmt.Println(string(data))
Output:

[1,2,3]

func (*OrderedChainSet) Pop

func (s *OrderedChainSet) Pop() (element any, err error)

Pop removes and returns an element from the end of 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) String

func (s OrderedChainSet) String() string

String returns a string representation of the OrderedChainSet.

func (OrderedChainSet) ToList

ToList returns a linkedlist.LinkedList containing the elements in the insertion order.

Example
s := NewOrderedChainSet(1, 2, 3)
fmt.Println(s)
fmt.Println(s.ToList())
Output:

{1 2 3}
[1 2 3]

func (*OrderedChainSet) UnmarshalJSON

func (s *OrderedChainSet) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON parses JSON data and initializes the OrderedChainSet.

Example
s := NewOrderedChainSet()
_ = json.Unmarshal([]byte("[1,2,3]"), &s)
fmt.Println(s)
Output:

{1 2 3}

func (*OrderedChainSet) Update

func (s *OrderedChainSet) Update(another OrderedChainSet) *OrderedChainSet

Update adds all elements from another OrderedChainSet to this 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 struct {
	dict.Dict
	// contains filtered or unexported fields
}

OrderedDict represents an ordered dictionary, which is a collection of key-value pairs with a specific order based on the sequence of insertion, which is based on a Dict and an ArrayList.

Example
d := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
fmt.Println(d)
Output:

map[c:1 a:2 b:3]

func NewOrderedDict

func NewOrderedDict(entries ...[2]any) *OrderedDict

NewOrderedDict creates a new ordered dictionary with the given key-value entries. It returns a pointer to the created OrderedDict.

func (*OrderedDict) Clear

func (d *OrderedDict) Clear() *OrderedDict

Clear clears all the elements in the ordered dictionary and the insertion order sequence. It returns the updated OrderedDict.

func (OrderedDict) Copy

func (d OrderedDict) Copy() OrderedDict

Copy returns a copy of the ordered dictionary with the same key-value pairs and insertion order.

Example
d := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"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) Delete

func (d *OrderedDict) Delete(key any) bool

Delete deletes the key-value pair with the specified key from the ordered dictionary. It returns true if the key existed and was deleted; otherwise, it returns false.

Example
d := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"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) Equal

func (d OrderedDict) Equal(another OrderedDict) bool

Equal checks if the ordered dictionary is equal to another OrderedDict in terms of key-value pairs and insertion order.

Example
d1 := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
d2 := NewOrderedDict([2]any{"c", 1}, [2]any{"b", 3}, [2]any{"a", 2})
d3 := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
fmt.Println(d1.Equal(*d2))
fmt.Println(d1.Equal(*d3))
Output:

false
true

func (OrderedDict) IndexOf

func (d OrderedDict) IndexOf(key any) int

IndexOf returns the index of the specified key in the insertion order sequence.

Example
d := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
index := d.IndexOf("a")
fmt.Println(index)
Output:

1

func (OrderedDict) Items

func (d OrderedDict) Items() [][2]any

Items returns a slice of all key-value pairs in the ordered dictionary based on the insertion order of keys.

Example
d := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
fmt.Println(d.Items())
Output:

[[c 1] [a 2] [b 3]]

func (OrderedDict) KeyAt

func (d OrderedDict) KeyAt(index int) (any, error)

KeyAt returns the key at the specified index in the insertion order sequence.

Example
d := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
key, _ := d.KeyAt(1)
fmt.Println(key)
Output:

a

func (OrderedDict) Keys

func (d OrderedDict) Keys() []any

Keys returns a copy of all keys in the ordered dictionary based on the insertion order.

Example
d := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
fmt.Println(d.Keys())
Output:

[c a b]

func (OrderedDict) MarshalJSON

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

MarshalJSON returns the JSON encoding of the ordered dictionary.

Example
d := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
data, _ := json.Marshal(d)
fmt.Println(string(data))
Output:

[["c",1],["a",2],["b",3]]

func (*OrderedDict) Pop

func (d *OrderedDict) Pop(key any, args ...any) (value any, err error)

Pop removes the key-value pair with the specified key from the ordered dictionary. It also returns the value of the removed key-value pair and an error if applicable.

func (*OrderedDict) PopItem

func (d *OrderedDict) PopItem() (key, value any, err error)

PopItem removes and returns the last key-value pair from the ordered dictionary. It also returns an error if the dictionary is empty.

Example
d := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"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) Set

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

Set sets the key-value pair in the ordered dictionary. It maintains the insertion order of the keys and returns the updated OrderedDict.

Example
d := NewOrderedDict()
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) String

func (d OrderedDict) String() string

String returns a string representation of the ordered dictionary in the format of a map.

func (*OrderedDict) UnmarshalJSON

func (d *OrderedDict) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON sets the ordered dictionary from its JSON encoding.

Example
d := NewOrderedDict()
_ = json.Unmarshal([]byte(`[["c",1],["a",2],["b",3]]`), &d)
fmt.Println(d)
Output:

map[c:1 a:2 b:3]

func (*OrderedDict) Update

func (d *OrderedDict) Update(another OrderedDict) *OrderedDict

Update updates the ordered dictionary with the key-value pairs from another OrderedDict. It returns the updated OrderedDict.

Example
d1 := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
d2 := NewOrderedDict([2]any{"d", 5}, [2]any{"b", 4}, [2]any{"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) Values

func (d OrderedDict) Values() []any

Values returns a slice of all values in the ordered dictionary based on the insertion order of keys.

Example
d := NewOrderedDict([2]any{"c", 1}, [2]any{"a", 2}, [2]any{"b", 3})
fmt.Println(d.Values())
Output:

[1 2 3]

type OrderedSet

type OrderedSet struct {
	set.Set
	// contains filtered or unexported fields
}

OrderedSet represents a set data structure that maintains the order of elements, which is based on a Set and an ArrayList.

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(entries ...any) *OrderedSet

NewOrderedSet creates a new OrderedSet with the given initial entries

func (*OrderedSet) Add

func (s *OrderedSet) Add(element any) *OrderedSet

Add adds the given element to the 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) At

func (s OrderedSet) At(index int) (any, error)

At returns the element at the specified index in the OrderedSet

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>
<nil> the index is out of range

func (*OrderedSet) Clear

func (s *OrderedSet) Clear() *OrderedSet

Clear removes all elements from the OrderedSet

func (OrderedSet) Copy

func (s OrderedSet) Copy() OrderedSet

Copy returns a copy of the current OrderedSet

Example
s := NewOrderedSet(1, 2, 3)
fmt.Println(s)
s2 := s.Copy()
fmt.Println(s2)
Output:

{1 2 3}
{1 2 3}

func (*OrderedSet) Discard

func (s *OrderedSet) Discard(element any) bool

Discard removes the given element from the OrderedSet

Example
s := NewOrderedSet(1, 2, 3)
fmt.Println(s)
s.Discard(2)
fmt.Println(s)
Output:

{1 2 3}
{1 3}

func (OrderedSet) Elements

func (s OrderedSet) Elements() []any

Elements returns a slice containing all the elements of the OrderedSet

Example
s := NewOrderedSet(1, 2, 3)
fmt.Println(s)
fmt.Println(s.Elements())
Output:

{1 2 3}
[1 2 3]

func (OrderedSet) Equal

func (s OrderedSet) Equal(another OrderedSet) bool

Equal checks if the current OrderedSet is equal to another OrderedSet

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) IndexOf

func (s OrderedSet) IndexOf(element any) int

IndexOf returns the index of the specified element in the OrderedSet

Example
s := NewOrderedSet(1, 2, 3)
fmt.Println(s.IndexOf(2))
fmt.Println(s.IndexOf(4))
Output:

1
-1

func (OrderedSet) MarshalJSON

func (s OrderedSet) MarshalJSON() ([]byte, error)

MarshalJSON converts the sequence in the OrderedSet to JSON

Example
s := NewOrderedSet(1, 2, 3)
data, _ := json.Marshal(&s)
fmt.Println(string(data))
Output:

[1,2,3]

func (*OrderedSet) Pop

func (s *OrderedSet) Pop() (element any, err error)

Pop removes and returns the last element from the OrderedSet

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) String

func (s OrderedSet) String() string

String returns the string representation of the sequence in the OrderedSet

func (OrderedSet) ToList

func (s OrderedSet) ToList() arraylist.ArrayList

ToList returns a copy of the sequence as an ArrayList

Example
s := NewOrderedSet(1, 2, 3)
fmt.Println(s)
fmt.Println(s.ToList())
Output:

{1 2 3}
[1 2 3]

func (*OrderedSet) UnmarshalJSON

func (s *OrderedSet) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON decodes the JSON data to populate the OrderedSet sequence

Example
s := NewOrderedSet()
_ = json.Unmarshal([]byte("[1,2,3]"), &s)
fmt.Println(s)
Output:

{1 2 3}

func (*OrderedSet) Update

func (s *OrderedSet) Update(another OrderedSet) *OrderedSet

Update merges the elements from another OrderedSet into the current OrderedSet

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}

Jump to

Keyboard shortcuts

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