Documentation
¶
Index ¶
- type LessFunc
- type Map
- func (m *Map[K, V]) Clear()
- func (m Map[K, V]) Contains(key K) bool
- func (m *Map[K, V]) Erase(iter *MapIterator[K, V]) bool
- func (m Map[K, V]) Find(key K) *MapIterator[K, V]
- func (m Map[K, V]) First() *MapIterator[K, V]
- func (m Map[K, V]) Get(key K) V
- func (m *Map[K, V]) Insert(key K, value V) (*MapIterator[K, V], bool)
- func (m *Map[K, V]) Keys() []K
- func (m Map[K, V]) Last() *MapIterator[K, V]
- func (m Map[K, V]) Len() int
- func (m *Map[K, V]) Remove(key K) bool
- func (m Map[K, V]) String() string
- func (m Map[K, V]) Stringify(options *tree.Options) string
- func (m *Map[K, V]) Values() []V
- type MapIterator
- type Set
- func (s *Set[K]) Clear()
- func (s Set[K]) Contains(key K) bool
- func (s *Set[K]) Erase(iter *SetIterator[K]) bool
- func (s Set[K]) Find(key K) *SetIterator[K]
- func (s Set[K]) First() *SetIterator[K]
- func (s *Set[K]) Insert(key K) (*SetIterator[K], bool)
- func (s *Set[K]) Keys() []K
- func (s Set[K]) Last() *SetIterator[K]
- func (s Set[K]) Len() int
- func (s *Set[K]) Remove(key K) bool
- func (s Set[K]) String() string
- func (s Set[K]) Stringify(options *tree.Options) string
- type SetIterator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map[K comparable, V any] rbtree.RBTree[K, V]
Map represents an ordered map
Example ¶
package main
import (
"fmt"
"github.com/gopherd/doge/container/ordered"
"github.com/gopherd/doge/container/tree"
)
func main() {
m := ordered.NewMap[int, int]()
fmt.Print("empty:\n" + m.Stringify(nil))
m.Insert(1, 2)
m.Insert(2, 4)
m.Insert(4, 8)
iter, ok := m.Insert(8, 16)
if !ok {
fmt.Println("insert fail")
} else {
fmt.Println("inserted:", iter.Key(), iter.Value())
}
fmt.Print("default:\n" + m.Stringify(nil))
fmt.Print("custom:\n" + m.Stringify(&tree.Options{
Prefix: "... ",
Parent: "| ",
Branch: "|-- ",
LastBranch: "+-- ",
}))
fmt.Println("plain:\n" + m.String())
}
Output: empty: <nil> inserted: 8 16 default: 2:4 ├── 1:2 └── 4:8 └── 8:16 custom: ... 2:4 ... |-- 1:2 ... +-- 4:8 ... +-- 8:16 plain: [1:2 2:4 4:8 8:16]
func NewMap ¶
func NewMap[K constraints.Ordered, V any]() *Map[K, V]
New creates an ordered map for ordered K
func NewMapFunc ¶
func NewMapFunc[K comparable, V any](less LessFunc[K]) *Map[K, V]
NewMapFunc creates an ordered map with custom less function
func (*Map[K, V]) Erase ¶
func (m *Map[K, V]) Erase(iter *MapIterator[K, V]) bool
Erase deletes the node, false returned if the node not found.
func (Map[K, V]) Find ¶
func (m Map[K, V]) Find(key K) *MapIterator[K, V]
Find finds node by key, nil returned if the key not found.
func (Map[K, V]) First ¶
func (m Map[K, V]) First() *MapIterator[K, V]
First returns the first node.
usage:
iter := m.First()
for iter != nil {
// hint: do something here using iter
// hint: iter.Key(), iter.Value(), iter.SetValue(newValue)
iter = iter.Next()
}
func (*Map[K, V]) Insert ¶
func (m *Map[K, V]) Insert(key K, value V) (*MapIterator[K, V], bool)
Insert inserts a key-value pair, inserted node and true returned if the key not found, otherwise, existed node and false returned.
func (*Map[K, V]) Keys ¶
func (m *Map[K, V]) Keys() []K
Keys collects all keys of the map as a slice
func (Map[K, V]) Last ¶
func (m Map[K, V]) Last() *MapIterator[K, V]
Last returns the last node.
usage:
iter := m.Last()
for iter != nil {
// hint: do something here using iter
// hint: iter.Key(), iter.Value(), iter.SetValue(newValue)
iter = iter.Prev()
}
type MapIterator ¶
type MapIterator[K comparable, V any] rbtree.Node[K, V]
MapIterator represents an iterator of map to iterate nodes
func (*MapIterator[K, V]) Next ¶
func (iter *MapIterator[K, V]) Next() *MapIterator[K, V]
Next returns next node
func (*MapIterator[K, V]) Prev ¶
func (iter *MapIterator[K, V]) Prev() *MapIterator[K, V]
Prev returns previous node
func (*MapIterator[K, V]) SetValue ¶
func (iter *MapIterator[K, V]) SetValue(value V)
SetValue sets node's value
func (*MapIterator[K, V]) Value ¶
func (iter *MapIterator[K, V]) Value() V
Value returns node's value
type Set ¶
type Set[K comparable] rbtree.RBTree[K, empty]
Set represents an ordered set
Example ¶
package main
import (
"fmt"
"github.com/gopherd/doge/container/ordered"
"github.com/gopherd/doge/container/tree"
)
func main() {
s := ordered.NewSet[int]()
fmt.Print("empty:\n" + s.Stringify(nil))
s.Insert(1)
s.Insert(2)
s.Insert(4)
iter, ok := s.Insert(8)
if !ok {
fmt.Println("insert fail")
} else {
fmt.Println("inserted:", iter.Key())
}
fmt.Print("default:\n" + s.Stringify(nil))
fmt.Print("custom:\n" + s.Stringify(&tree.Options{
Prefix: "... ",
Parent: "| ",
Branch: "|-- ",
LastBranch: "+-- ",
}))
fmt.Println("plain:\n" + s.String())
}
Output: empty: <nil> inserted: 8 default: 2 ├── 1 └── 4 └── 8 custom: ... 2 ... |-- 1 ... +-- 4 ... +-- 8 plain: [1 2 4 8]
func NewSetFunc ¶
func NewSetFunc[K comparable](less LessFunc[K]) *Set[K]
NewSetFunc creates an ordered set with custom less function
func (*Set[K]) Erase ¶
func (s *Set[K]) Erase(iter *SetIterator[K]) bool
Erase deletes the node, false returned if the node not found.
func (Set[K]) Find ¶
func (s Set[K]) Find(key K) *SetIterator[K]
Find finds node by key, nil returned if the key not found.
func (Set[K]) First ¶
func (s Set[K]) First() *SetIterator[K]
First returns the first node.
usage:
iter := s.First()
for iter != nil {
// hint: do something here using iter
// hint: iter.Key()
iter = iter.Next()
}
func (*Set[K]) Insert ¶
func (s *Set[K]) Insert(key K) (*SetIterator[K], bool)
Insert inserts a key-value pair, inserted node and true returned if the key not found, otherwise, existed node and false returned.
func (Set[K]) Last ¶
func (s Set[K]) Last() *SetIterator[K]
Last returns the last node.
usage:
iter := s.Last()
for iter != nil {
// hint: do something here using iter
// hint: iter.Key()
iter = iter.Prev()
}
type SetIterator ¶
type SetIterator[K comparable] rbtree.Node[K, empty]
SetIterator represents an iterator of OrderedSet to iterate nodes
func (*SetIterator[K]) Next ¶
func (iter *SetIterator[K]) Next() *SetIterator[K]
Next returns next node
func (*SetIterator[K]) Prev ¶
func (iter *SetIterator[K]) Prev() *SetIterator[K]
Prev returns previous node