ordered

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LessFunc

type LessFunc[T any] func(x, y T) bool

LessFunc represents a comparation function which reports whether x "less" than y

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]) Clear

func (m *Map[K, V]) Clear()

Clear clears the set

func (Map[K, V]) Contains

func (m Map[K, V]) Contains(key K) bool

Contains reports whether the set contains the key

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]) Get

func (m Map[K, V]) Get(key K) V

Get retrives value by key

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()
}

func (Map[K, V]) Len

func (m Map[K, V]) Len() int

Len returns the number of elements

func (*Map[K, V]) Remove

func (m *Map[K, V]) Remove(key K) bool

Remove removes an element by key, false returned if the key not found.

func (Map[K, V]) String

func (m Map[K, V]) String() string

String returns content of the set as a plain string

func (Map[K, V]) Stringify

func (m Map[K, V]) Stringify(options *tree.Options) string

Stringify pretty stringifies the map with specified options or nil

func (*Map[K, V]) Values

func (m *Map[K, V]) Values() []V

Values collects all values of the map as a slice

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]) Key

func (iter *MapIterator[K, V]) Key() K

Key returns node's key

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 NewSet

func NewSet[K constraints.Ordered]() *Set[K]

New creates an ordered set for ordered K

func NewSetFunc

func NewSetFunc[K comparable](less LessFunc[K]) *Set[K]

NewSetFunc creates an ordered set with custom less function

func (*Set[K]) Clear

func (s *Set[K]) Clear()

Clear clears the set

func (Set[K]) Contains

func (s Set[K]) Contains(key K) bool

Contains reports whether the set contains the key

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]) Keys

func (s *Set[K]) Keys() []K

Keys collects all keys of the set as a slice

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()
}

func (Set[K]) Len

func (s Set[K]) Len() int

Len returns the number of elements

func (*Set[K]) Remove

func (s *Set[K]) Remove(key K) bool

Remove removes an element by key, false returned if the key not found.

func (Set[K]) String

func (s Set[K]) String() string

String returns content of the set as a plain string

func (Set[K]) Stringify

func (s Set[K]) Stringify(options *tree.Options) string

Stringify pretty stringifies the set with specified options or nil

type SetIterator

type SetIterator[K comparable] rbtree.Node[K, empty]

SetIterator represents an iterator of OrderedSet to iterate nodes

func (*SetIterator[K]) Key

func (iter *SetIterator[K]) Key() K

Key returns node's key

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

Jump to

Keyboard shortcuts

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