orderedmap

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator[K comparable, V any] interface {
	// Prev returns previous iterator
	Prev() Iterator[K, V]
	// Next returns next node iterator
	Next() Iterator[K, V]
	// Key returns key of the node
	Key() K
	// Value returns value of the node
	Value() V
	// SetValue sets value of the node
	SetValue(V)
	// contains filtered or unexported methods
}

Iterator represents an iterator of OrderedMap to iterate nodes

type LessFunc

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

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

type OrderedMap

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap represents an ordered map

Example
package main

import (
	"fmt"

	"github.com/gopherd/doge/container"
	"github.com/gopherd/doge/container/orderedmap"
)

func main() {
	m := orderedmap.New[int, int]()
	fmt.Print("empty:\n" + m.Stringify(container.StringifyOptions{}))

	m.Insert(1, 2)
	m.Insert(2, 4)
	m.Insert(4, 8)
	m.Insert(8, 16)

	fmt.Print("default:\n" + m.Stringify(container.StringifyOptions{}))
	fmt.Print("custom:\n" + m.Stringify(container.StringifyOptions{
		Prefix:         "... ",
		IconParent:     "|  ",
		IconBranch:     "|--",
		IconLastBranch: "+--",
	}))
	fmt.Println("plain:\n" + m.String())

}
Output:

empty:
<nil>
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 New

func New[K constraints.Ordered, V any]() *OrderedMap[K, V]

New creates a OrderedMap for ordered K

func NewFunc

func NewFunc[K comparable, V any](less LessFunc[K]) *OrderedMap[K, V]

NewFunc creates a OrderedMap with compare function

func (*OrderedMap[K, V]) Clear

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

Clear clears the container

func (*OrderedMap[K, V]) Erase

func (m *OrderedMap[K, V]) Erase(iter Iterator[K, V]) bool

Erase deletes the node, false returned if the node not found.

func (*OrderedMap[K, V]) Find

func (m *OrderedMap[K, V]) Find(key K) Iterator[K, V]

Find finds node by key, nil returned if the key not found.

func (*OrderedMap[K, V]) First

func (m *OrderedMap[K, V]) First() Iterator[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 (*OrderedMap[K, V]) Insert

func (m *OrderedMap[K, V]) Insert(key K, value V) (Iterator[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 (*OrderedMap[K, V]) Last

func (m *OrderedMap[K, V]) Last() Iterator[K, V]

Last returns the first 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 (OrderedMap[K, V]) Len

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

Len returns the number of elements

func (*OrderedMap[K, V]) Remove

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

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

func (*OrderedMap[K, V]) String

func (m *OrderedMap[K, V]) String() string

String returns content of the map as a plain string

func (*OrderedMap[K, V]) Stringify added in v0.2.4

func (m *OrderedMap[K, V]) Stringify(options container.StringifyOptions) string

Stringify pretty stringify the map by specified options

Jump to

Keyboard shortcuts

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