cog

package
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: MIT Imports: 12 Imported by: 8

README

Pango Collection

A Go Collection/Container package.

Container

All data structures implement the container interface with the following methods:

// Container the base container interface
type Container interface {
	// Len returns the length of the container.
	Len() int

	// IsEmpty returns true if the container length == 0
	IsEmpty() bool

	// Clear clears the container
	Clear()
}

Containers are either ordered or unordered. All ordered containers provide stateful iterators.

Structure Ordered Iterator Sortable
ArrayList Y Y Y
LinkedList Y Y
HashSet
LinkedHashSet Y Y Y
TreeSet Y Y
HashMap
LinkedHashMap Y Y
TreeMap Y Y
Collection

Base interface for List and Set.

Extends Container interface.

// Collection the base collection interface
type Collection[T any] interface {
	Container

	// Add adds item v to the collection
	Add(v T)

	// Adds adds items of vs to the collection
	Adds(vs ...T)

	// AddCol adds all items of another collection
	AddCol(ac Collection[T])

	// Remove remove all items with associated value v
	Remove(v T)

	// Removes remove all items in the array vs
	Removes(vs ...T)

	// RemoveCol remove all of this collection's elements that are also contained in the specified collection
	RemoveCol(ac Collection[T])

	// RemoveIter remove all items in the iterator it
	RemoveIter(it Iterator[T])

	// RemoveFunc remove all items that function f returns true
	RemoveFunc(f func(T) bool)

	// Contain Test to see if the collection contains item v
	Contain(v T) bool

	// Contains Test to see if the collection contains all items of vs
	Contains(vs ...T) bool

	// ContainCol Test to see if the collection contains all items of another collection
	ContainCol(ac Collection[T]) bool

	// Retains Retains only the elements in this collection that are contained in the argument array vs.
	Retains(vs ...T)

	// RetainCol Retains only the elements in this collection that are contained in the specified collection.
	RetainCol(ac Collection[T])

	// Values returns a slice contains all the items of the collection
	Values() []T

	Eachable[T]
}
List

A list is a data structure that stores values and may have repeated values.

Extends Collection interface.

// List a doubly linked list interface
type List[T any] interface {
	Collection[T]

	ReverseEachable[T]

	Iterable[T]

	// Get returns the value at the specified index in this list. If the index is
	// invalid, the call will panic. This method accepts both positive and
	// negative index values. Index 0 refers to the first element, and
	// index -1 refers to the last.
	Get(index int) T

	// Set set the v at the specified index in this list and returns the old value.
	Set(index int, v T) T

	// Insert insert item v at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
	// Does not do anything if position is bigger than list's size
	// Note: position equal to list's size is valid, i.e. append.
	Insert(index int, v T)

	// Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
	// Does not do anything if position is bigger than list's size
	// Note: position equal to list's size is valid, i.e. append.
	Inserts(index int, vs ...T)

	// InsertCol inserts values of another collection ac at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
	// Does not do anything if position is bigger than list's size
	// Note: position equal to list's size is valid, i.e. append.
	InsertCol(index int, ac Collection[T])

	// Index returns the index of the first occurrence of the specified v in this list, or -1 if this list does not contain v.
	Index(v T) int

	// IndexFunc returns the index of the first true returned by function f in this list, or -1 if this list does not contain v.
	IndexFunc(f func(T) bool) int

	// DeleteAt remove the item at the specified position in this list
	DeleteAt(index int)

	// Swap swaps values of two items at the given index.
	Swap(i, j int)
}
ArrayList

A list backed by a dynamic array that grows implicitly.

Implements List, Iterator interfaces.

package main

import (
	"github.com/askasoft/pango/cog"
)

func main() {
	list := cog.NewArrayList[string]()
	list.Add("a")                         // ["a"]
	list.Adds("c", "b")                   // ["a","c","b"]
	list.Sort(cog.LessString)             // ["a","b","c"]
	_ = list.Get(0)                       // "a"
	_ = list.Get(100)                     // panic
	_ = list.Contain("a")                 // true
	_ = list.Contains("a", "b", "c")      // true
	_ = list.Contains("a", "b", "c", "d") // false
	list.Swap(0, 1)                       // ["b","a",c"]
	list.DeleteAt(2)                      // ["b","a"]
	list.DeleteAt(1)                      // ["b"]
	list.DeleteAt(0)                      // []
	_ = list.IsEmpty()                    // true
	_ = list.Len()                        // 0
	list.Add("a")                         // ["a"]
	list.Clear()                          // []
	list.Insert(0, "b")                   // ["b"]
	list.Insert(0, "a")                   // ["a","b"]
}
LinkedList

A list where each element points to the next and previous elements in the list.

Implements List, Iterator interfaces.

package main

import (
	"github.com/askasoft/pango/cog"
)

func main() {
	list := cog.NewLinkedList[string]()
	list.Add("a")                         // ["a"]
	list.Adds("c", "b")                   // ["a","c","b"]
	list.Sort(cog.LessString)             // ["a","b","c"]
	_ = list.Get(0)                       // "a"
	_ = list.Get(100)                     // panic
	_ = list.Contain("a")                 // true
	_ = list.Contains("a", "b", "c")      // true
	_ = list.Contains("a", "b", "c", "d") // false
	list.Swap(0, 1)                       // ["b","a",c"]
	list.DeleteAt(2)                      // ["b","a"]
	list.DeleteAt(1)                      // ["b"]
	list.DeleteAt(0)                      // []
	_ = list.IsEmpty()                    // true
	_ = list.Len()                        // 0
	list.Add("a")                         // ["a"]
	list.Clear()                          // []
	list.Insert(0, "b")                   // ["b"]
	list.Insert(0, "a")                   // ["a","b"]
}
Set

A set is a data structure that can store elements and has no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests an element for membership in a set. This structure is often used to ensure that no duplicates are present in a container.

// Set a set interface
type Set = Collection
HashSet

A set backed by a hash table (actually a Go's map). It makes no guarantees as to the iteration order of the set.

Implements Set interfaces.

package main

import (
	"github.com/askasoft/pango/cog"
)

func main() {
	set := cog.NewHashSet[int]()
	set.Add(1)              // 1
	set.Adds(2, 2, 3, 4, 5) // 3, 1, 2, 4, 5 (random order, duplicates ignored)
	set.Remove(4)           // 5, 3, 2, 1 (random order)
	set.Removes(2, 3)       // 1, 5 (random order)
	set.Contain(1)          // true
	set.Contains(1, 5)      // true
	set.Contains(1, 6)      // false
	_ = set.Values()        // []int{5,1} (random order)
	set.Clear()             // empty
	set.IsEmpty()           // true
	set.Len()               // 0
}
LinkedHashSet

A set that preserves insertion-order. Data structure is backed by a hash table to store values and doubly-linked list to store insertion ordering.

Implements Set, Iterator interfaces.

package main

import (
	"github.com/askasoft/pango/cog"
)

func main() {
	set := cog.NewLinkedHashSet[int]()
	set.Add(5)              // 5
	set.Adds(4, 4, 3, 2, 1) // 5, 4, 3, 2, 1 (in insertion-order, duplicates ignored)
	set.Add(4)              // 5, 4, 3, 2, 1 (duplicates ignored, insertion-order unchanged)
	set.Remove(4)           // 5, 3, 2, 1 (in insertion-order)
	set.Removes(2, 3)       // 5, 1 (in insertion-order)
	set.Contain(1)          // true
	set.Contains(1, 5)      // true
	set.Contains(1, 6)      // false
	_ = set.Values()        // []int{5, 1} (in insertion-order)
	set.Clear()             // empty
	set.IsEmpty()           // true
	set.Len()               // 0
}
TreeSet

A set backed by a red-black-tree to keep the elements ordered with respect to the comparator.

Implements Set, Iterator interfaces.

package main

import (
	"github.com/askasoft/pango/cog"
)

func main() {
	set := cog.NewTreeSet(cog.CompareInt)
	set.Add(1)              // 1
	set.Adds(2, 2, 3, 4, 5) // 1, 2, 3, 4, 5 (in order, duplicates ignored)
	set.Remove(4)           // 1, 2, 3, 5 (in order)
	set.Removes(2, 3)       // 1, 5 (in order)
	set.Contain(1)          // true
	set.Contains(1, 5)      // true
	set.Contains(1, 6)      // false
	_ = set.Values()        // []int{1,5} (in order)
	set.Clear()             // empty
	set.IsEmpty()           // true
	set.Len()               // 0
}
Map

A Map is a data structure that maps keys to values. A map cannot contain duplicate keys and each key can map to at most one value.

Extends Container interface.

// Map map interface
type Map[K any, V any] interface {
	Container

	// Get looks for the given key, and returns the value associated with it,
	// or nil if not found. The boolean it returns says whether the key is ok in the map.
	Get(key K) (V, bool)

	// Set sets the paired key-value items, and returns what `Get` would have returned
	// on that key prior to the call to `Set`.
	// Example: lm.Set("k1", "v1", "k2", "v2")
	Set(key K, value V) (ov V, ok bool)

	// SetIfAbsent sets the key-value item if the key does not exists in the map,
	// and returns what `Get` would have returned
	// on that key prior to the call to `Set`.
	SetIfAbsent(key K, value V) (ov V, ok bool)

	// SetEntries set items from key-value items array, override the existing items
	SetEntries(pairs ...P[K, V])

	// Copy copy items from another map am, override the existing items
	Copy(am Map[K, V])

	// Remove remove the item with key k,
	// and returns what `Get` would have returned
	// on that key prior to the call to `Set`.
	Remove(k K) (ov V, ok bool)

	// Removes remove all items with key of ks.
	Removes(ks ...K)

	// Contain looks for the given key, and returns true if the key exists in the map.
	Contain(k K) bool

	// Contains looks for the given key, and returns true if the key exists in the map.
	Contains(ks ...K) bool

	// Keys returns the key slice
	Keys() []K

	// Values returns a slice contains all the items of the collection
	Values() []V

	// Entries returns the key-value pair slice
	Entries() []P[K, V]

	Eachable2[K, V]
}
HashMap

A map based on hash tables. Keys are unordered.

Implements Map interfaces.

package main

import (
	"github.com/askasoft/pango/cog"
)

func main() {
	m := cog.NewHashMap[int, string]()
	m.Set(1, "x")   // 1->x
	m.Set(2, "b")   // 2->b, 1->x (random order)
	m.Set(1, "a")   // 2->b, 1->a (random order)
	_, _ = m.Get(2) // b, true
	_, _ = m.Get(3) // nil, false
	_ = m.Values()  // []interface {}{"b", "a"} (random order)
	_ = m.Keys()    // []interface {}{1, 2} (random order)
	m.Remove(1)     // 2->b
	m.Clear()       // empty
	m.IsEmpty()     // true
	m.Len()         // 0
}
LinkedHashMap

A map that preserves insertion-order. It is backed by a hash table to store values and doubly-linked list to store ordering.

Implements Map, Iterator interfaces.

package main

import (
	"github.com/askasoft/pango/cog"
)

func main() {
	m := cog.NewLinkedHashMap[int, string]()
	m.Set(2, "b")   // 2->b
	m.Set(1, "x")   // 2->b, 1->x (insertion-order)
	m.Set(1, "a")   // 2->b, 1->a (insertion-order)
	_, _ = m.Get(2) // b, true
	_, _ = m.Get(3) // nil, false
	_ = m.Values()  // []interface {}{"b", "a"} (insertion-order)
	_ = m.Keys()    // []interface {}{2, 1} (insertion-order)
	m.Remove(1)     // 2->b
	m.Clear()       // empty
	m.IsEmpty()     // true
	m.Len()         // 0
}
TreeMap

A map based on red-black tree. Keys are ordered with respect to the comparator.

Implements Map, Iterator2 interfaces.

package main

import (
	"github.com/askasoft/pango/cog"
)

func main() {
	m := cog.NewTreeMap[int, string](cog.CompareInt)
	m.Set(1, "x")   // 1->x
	m.Set(2, "b")   // 1->x, 2->b (in order)
	m.Set(1, "a")   // 1->a, 2->b (in order)
	_, _ = m.Get(2) // b, true
	_, _ = m.Get(3) // nil, false
	_ = m.Values()  // []interface {}{"a", "b"} (in order)
	_ = m.Keys()    // []interface {}{1, 2} (in order)
	m.Remove(1)     // 2->b
	m.Clear()       // empty
	m.IsEmpty()     // true
	m.Len()         // 0

	// Other:
	m.Head()  // Returns the minimum key and its value from map.
	m.Tail()  // Returns the maximum key and its value from map.
}
Iterator

All ordered containers have stateful iterators. Typically an iterator is obtained by Iterator() function of an ordered container. Once obtained, iterator's Next() function moves the iterator to the next element and returns true if there was a next element. If there was an element, then element's can be obtained by iterator's Value() function.

Note: it is unsafe to use Iterator.Remove() element or Iterator.SetValue() while iterating, but the iterator's Prev()/Next() may be different after SetValue().

Typical usage:

// forward
for it := list.Iterator(); it.Next(); {
	value := it.Value()
	...
}

// backward
for it := list.Iterator(); it.Prev(); {
	value := it.Value()
	...
}
Iterator2

All ordered maps have stateful iterators. Typically an iterator is obtained by Iterator() function of an ordered map. Once obtained, iterator's Next() function moves the iterator to the next element and returns true if there was a next element. If there was an element, then element's can be obtained by iterator's Key(), Value() function.

Note: it is unsafe to use Iterator.Remove() element or Iterator.SetValue() while iterating.

Typical usage:

// forward
for it := list.Iterator(); it.Next(); {
	key, value := it.Key(), it.Value()
	...
}

// backward
for it := list.Iterator(); it.Prev(); {
	key, value := it.Key(), it.Value()
	...
}

Compare

Various helper functions used by Collection package.

Comparator

Some data structures (e.g. TreeMap, TreeSet) require a comparator function to automatically keep their elements sorted upon insertion. This comparator is necessary during the initalization.

Comparator is defined as:

// Should return a int:
//   negative : if a < b
//   zero     : if a == b
//   positive : if a > b

Comparator signature:

type Compare[T any] func(a, b T) int

All common comparators for builtin types are included in the package:

func CompareString(a, b string) int
func CompareInt(a, b int) int
func CompareInt8(a, b int8) int
func CompareInt16(a, b int16) int
func CompareInt32(a, b int32) int
func CompareInt64(a, b int64) int
func CompareUInt(a, b uint) int
func CompareUInt8(a, b uint8) int
func CompareUInt16(a, b uint16) int
func CompareUInt32(a, b uint32) int
func CompareUInt64(a, b uint64) int
func CompareFloat32(a, b float32) int
func CompareFloat64(a, b float64) int
func CompareByte(a, b byte) int
func CompareRune(a, b rune) int

Writing custom comparators is easy:

package main

import (
	"fmt"
	"github.com/askasoft/pango/cog"
)

type User struct {
	id   int
	name string
}

// Custom comparator (sort by IDs)
func byID(a, b any) int {

	// Type assertion, program will panic if this is not respected
	c1 := a.(User)
	c2 := b.(User)

	switch {
	case c1.id > c2.id:
		return 1
	case c1.id < c2.id:
		return -1
	default:
		return 0
	}
}

func main() {
	set := cog.NewTreeSet(byID)

	set.Add(User{2, "Second"})
	set.Add(User{3, "Third"})
	set.Add(User{1, "First"})
	set.Add(User{4, "Fourth"})

	fmt.Println(set) // {1 First}, {2 Second}, {3 Third}, {4 Fourth}
}
Less

Some data structures require a less compare function to sort it's elements (e.g. ArrayList.Sort()).

Less comparator is defined as:

// Should return a bool:
//    true : if a < b
//    false: if a >= b

Comparator signature:

type Less[T any] func(a, b T) bool

All common comparators for builtin types are included in the package:

func LessString(a, b string) bool
func LessByte(a, b byte) bool
func LessRune(a, b rune) bool
func LessInt(a, b int) bool
func LessInt8(a, b int8) bool
func LessInt16(a, b int16) bool
func LessInt32(a, b int32) bool
func LessInt64(a, b int64) bool
func LessUint(a, b uint) bool
func LessUint8(a, b uint8) bool
func LessUint16(a, b uint16) bool
func LessUint32(a, b uint32) bool
func LessUint64(a, b uint64) bool
func LessFloat32(a, b float32) bool
func LessFloat64(a, b float64) bool

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareByte

func CompareByte(a, b byte) int

CompareByte provides a basic comparison on byte

func CompareFloat32

func CompareFloat32(a, b float32) int

CompareFloat32 provides a basic comparison on float32

func CompareFloat64

func CompareFloat64(a, b float64) int

CompareFloat64 provides a basic comparison on float64

func CompareInt

func CompareInt(a, b int) int

CompareInt provides a basic comparison on int

func CompareInt16

func CompareInt16(a, b int16) int

CompareInt16 provides a basic comparison on int16

func CompareInt32

func CompareInt32(a, b int32) int

CompareInt32 provides a basic comparison on int32

func CompareInt64

func CompareInt64(a, b int64) int

CompareInt64 provides a basic comparison on int64

func CompareInt8

func CompareInt8(a, b int8) int

CompareInt8 provides a basic comparison on int8

func CompareRune

func CompareRune(a, b rune) int

CompareRune provides a basic comparison on rune

func CompareString

func CompareString(a, b string) int

CompareString provides a basic comparison on string

func CompareStringFold added in v1.0.11

func CompareStringFold(a, b string) int

CompareStringFold provides a basic case-insensitive comparison on string

func CompareUInt

func CompareUInt(a, b uint) int

CompareUInt provides a basic comparison on uint

func CompareUInt16

func CompareUInt16(a, b uint16) int

CompareUInt16 provides a basic comparison on uint16

func CompareUInt32

func CompareUInt32(a, b uint32) int

CompareUInt32 provides a basic comparison on uint32

func CompareUInt64

func CompareUInt64(a, b uint64) int

CompareUInt64 provides a basic comparison on uint64

func CompareUInt8

func CompareUInt8(a, b uint8) int

CompareUInt8 provides a basic comparison on uint8

func CopyMap added in v1.0.10

func CopyMap[K any, V any](dst, src Map[K, V])

CopyMap copy map 'src' to map 'des'

func LessByte

func LessByte(a, b byte) bool

LessByte byte less function

func LessFloat32

func LessFloat32(a, b float32) bool

LessFloat32 float32 less function

func LessFloat64

func LessFloat64(a, b float64) bool

LessFloat64 float64 less function

func LessInt

func LessInt(a, b int) bool

LessInt int less function

func LessInt16

func LessInt16(a, b int16) bool

LessInt16 int16 less function

func LessInt32

func LessInt32(a, b int32) bool

LessInt32 int32 less function

func LessInt64

func LessInt64(a, b int64) bool

LessInt64 int64 less function

func LessInt8

func LessInt8(a, b int8) bool

LessInt8 int8 less function

func LessRune

func LessRune(a, b rune) bool

LessRune rune less function

func LessString

func LessString(a, b string) bool

LessString string less function

func LessStringFold added in v1.0.11

func LessStringFold(a, b string) bool

LessStringFold string case-insensitive less function

func LessUint

func LessUint(a, b uint) bool

LessUint uint less function

func LessUint16

func LessUint16(a, b uint16) bool

LessUint16 uint16 less function

func LessUint32

func LessUint32(a, b uint32) bool

LessUint32 uint32 less function

func LessUint64

func LessUint64(a, b uint64) bool

LessUint64 uint64 less function

func LessUint8

func LessUint8(a, b uint8) bool

LessUint8 uint8 less function

Types

type ArrayList

type ArrayList[T any] struct {
	// contains filtered or unexported fields
}

ArrayList implements a list holdes the item in a array. The zero value for ArrayList is an empty list ready to use.

To iterate over a list (where al is a *ArrayList):

it := al.Iterator()
for it.Next() {
	// do something with it.Value()
}
Example
list := NewArrayList[string]()
list.Add("a")                         // ["a"]
list.Adds("c", "b")                   // ["a","c","b"]
list.Sort(LessString)                 // ["a","b","c"]
_ = list.Get(0)                       // "a"  //_ = list.Get(100)  --> panic
_ = list.Contain("a")                 // true
_ = list.Contains("a", "b", "c")      // true
_ = list.Contains("a", "b", "c", "d") // false
list.Swap(0, 1)                       // ["b","a",c"]
list.DeleteAt(2)                      // ["b","a"]
list.DeleteAt(1)                      // ["b"]
list.DeleteAt(0)                      // []
_ = list.IsEmpty()                    // true
_ = list.Len()                        // 0
list.Add("a")                         // ["a"]
list.Clear()                          // []
list.Insert(0, "b")                   // ["b"]
list.Insert(0, "a")                   // ["a","b"]
Output:

func AsArrayList

func AsArrayList[T any](vs []T) *ArrayList[T]

AsArrayList returns an initialized list. Example: AsArrayList([]T{1, 2, 3})

func NewArrayList

func NewArrayList[T any](vs ...T) *ArrayList[T]

NewArrayList returns an initialized list. Example: cog.NewArrayList(1, 2, 3)

func (*ArrayList[T]) Add

func (al *ArrayList[T]) Add(v T)

Add add the item v

func (*ArrayList[T]) AddCol added in v1.0.10

func (al *ArrayList[T]) AddCol(ac Collection[T])

AddCol adds all items of another collection

func (*ArrayList[T]) Adds added in v1.0.10

func (al *ArrayList[T]) Adds(vs ...T)

Adds adds all items of vs

func (*ArrayList[T]) Cap

func (al *ArrayList[T]) Cap() int

Cap returns the capcity of the list.

func (*ArrayList[T]) Clear

func (al *ArrayList[T]) Clear()

Clear clears list al.

func (*ArrayList[T]) Contain added in v1.0.10

func (al *ArrayList[T]) Contain(v T) bool

Contain Test to see if the list contains the value v

func (*ArrayList[T]) ContainCol added in v1.0.10

func (al *ArrayList[T]) ContainCol(ac Collection[T]) bool

ContainCol Test to see if the collection contains all items of another collection

func (*ArrayList[T]) ContainIter added in v1.0.12

func (al *ArrayList[T]) ContainIter(it Iterator[T]) bool

ContainIter Test to see if the collection contains all items of iterator 'it'

func (*ArrayList[T]) Contains

func (al *ArrayList[T]) Contains(vs ...T) bool

Contains Test to see if the collection contains all items of vs

func (*ArrayList[T]) DeleteAt added in v1.0.12

func (al *ArrayList[T]) DeleteAt(index int)

DeleteAt remove the item at the specified position in this list.

func (*ArrayList[T]) Each

func (al *ArrayList[T]) Each(f func(T))

Each call f for each item in the list

func (*ArrayList[T]) Get

func (al *ArrayList[T]) Get(index int) T

Get returns the item at the specified position in this list if i < -al.Len() or i >= al.Len(), panic if i < 0, returns al.Get(al.Len() + i)

func (*ArrayList[T]) Head

func (al *ArrayList[T]) Head() (v T)

Head get the first item of list.

func (*ArrayList[T]) Index

func (al *ArrayList[T]) Index(v T) int

Index returns the index of the first occurrence of the specified v in this list, or -1 if this list does not contain v.

func (*ArrayList[T]) IndexFunc added in v1.0.12

func (al *ArrayList[T]) IndexFunc(f func(T) bool) int

IndexFunc returns the index of the first true returned by function f in this list, or -1 if this list does not contain v.

func (*ArrayList[T]) Insert

func (al *ArrayList[T]) Insert(index int, v T)

Insert insert the item v at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than list's size Note: position equal to list's size is valid, i.e. append.

func (*ArrayList[T]) InsertCol added in v1.0.10

func (al *ArrayList[T]) InsertCol(index int, ac Collection[T])

InsertCol inserts values of another collection ac at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than list's size Note: position equal to list's size is valid, i.e. append.

func (*ArrayList[T]) Inserts added in v1.0.10

func (al *ArrayList[T]) Inserts(index int, vs ...T)

Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than list's size Note: position equal to list's size is valid, i.e. append.

func (*ArrayList[T]) IsEmpty

func (al *ArrayList[T]) IsEmpty() bool

IsEmpty returns true if the list length == 0

func (*ArrayList[T]) Iterator

func (al *ArrayList[T]) Iterator() Iterator[T]

Iterator returns a iterator for the list

func (*ArrayList[T]) Len

func (al *ArrayList[T]) Len() int

Len returns the length of the list.

func (*ArrayList[T]) MarshalJSON

func (al *ArrayList[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(al)

func (*ArrayList[T]) Peek

func (al *ArrayList[T]) Peek() (v T, ok bool)

Peek get the first item of list.

func (*ArrayList[T]) PeekHead

func (al *ArrayList[T]) PeekHead() (v T, ok bool)

PeekHead get the first item of list.

func (*ArrayList[T]) PeekTail

func (al *ArrayList[T]) PeekTail() (v T, ok bool)

PeekTail get the last item of list.

func (*ArrayList[T]) Poll

func (al *ArrayList[T]) Poll() (T, bool)

Poll get and remove the first item of list.

func (*ArrayList[T]) PollHead

func (al *ArrayList[T]) PollHead() (v T, ok bool)

PollHead get and remove the first item of list.

func (*ArrayList[T]) PollTail

func (al *ArrayList[T]) PollTail() (v T, ok bool)

PollTail get and remove the last item of list.

func (*ArrayList[T]) Push

func (al *ArrayList[T]) Push(v T)

Push insert item v at the tail of list al.

func (*ArrayList[T]) PushHead

func (al *ArrayList[T]) PushHead(v T)

PushHead inserts the item v at the head of list al.

func (*ArrayList[T]) PushHeadCol added in v1.0.10

func (al *ArrayList[T]) PushHeadCol(ac Collection[T])

PushHeadCol inserts a copy of another collection at the head of list al. The al and ac may be the same. They must not be nil.

func (*ArrayList[T]) PushHeads added in v1.0.10

func (al *ArrayList[T]) PushHeads(vs ...T)

PushHeads inserts all items of vs at the head of list al.

func (*ArrayList[T]) PushTail

func (al *ArrayList[T]) PushTail(v T)

PushTail inserts the item v at the tail of list al.

func (*ArrayList[T]) PushTailCol added in v1.0.10

func (al *ArrayList[T]) PushTailCol(ac Collection[T])

PushTailCol inserts a copy of another collection at the tail of list al. The al and ac may be the same. They must not be nil.

func (*ArrayList[T]) PushTails added in v1.0.10

func (al *ArrayList[T]) PushTails(vs ...T)

PushTails inserts all items of vs at the tail of list al.

func (*ArrayList[T]) Pushs added in v1.0.10

func (al *ArrayList[T]) Pushs(vs ...T)

Push inserts all items of vs at the tail of list al.

func (*ArrayList[T]) Remove

func (al *ArrayList[T]) Remove(v T)

Remove remove all items with associated value v of vs

func (*ArrayList[T]) RemoveCol added in v1.0.10

func (al *ArrayList[T]) RemoveCol(ac Collection[T])

RemoveCol remove all of this collection's elements that are also contained in the specified collection

func (*ArrayList[T]) RemoveFunc added in v1.0.12

func (al *ArrayList[T]) RemoveFunc(f func(T) bool)

RemoveFunc remove all items that function f returns true

func (*ArrayList[T]) RemoveIter added in v1.0.12

func (al *ArrayList[T]) RemoveIter(it Iterator[T])

RemoveIter remove all items in the iterator it

func (*ArrayList[T]) Removes added in v1.0.10

func (al *ArrayList[T]) Removes(vs ...T)

Removes remove all items in the array vs

func (*ArrayList[T]) Reserve

func (al *ArrayList[T]) Reserve(n int)

Reserve Increase the capacity of the underlying array.

func (*ArrayList[T]) RetainCol added in v1.0.10

func (al *ArrayList[T]) RetainCol(ac Collection[T])

RetainCol Retains only the elements in this collection that are contained in the specified collection.

func (*ArrayList[T]) RetainFunc added in v1.0.12

func (al *ArrayList[T]) RetainFunc(f func(T) bool)

RetainFunc Retains all items that function f returns true

func (*ArrayList[T]) Retains added in v1.0.10

func (al *ArrayList[T]) Retains(vs ...T)

Retains Retains only the elements in this collection that are contained in the argument array vs.

func (*ArrayList[T]) ReverseEach

func (al *ArrayList[T]) ReverseEach(f func(T))

ReverseEach call f for each item in the list with reverse order

func (*ArrayList[T]) Set

func (al *ArrayList[T]) Set(index int, v T) (ov T)

Set set the v at the specified index in this list and returns the old value.

func (*ArrayList[T]) Sort

func (al *ArrayList[T]) Sort(less Less[T])

Sort Sorts this list according to the order induced by the specified Comparator.

func (*ArrayList[T]) String

func (al *ArrayList[T]) String() string

String print list to string

func (*ArrayList[T]) Swap

func (al *ArrayList[T]) Swap(i, j int)

Swap swaps values of two items at the given index.

func (*ArrayList[T]) Tail

func (al *ArrayList[T]) Tail() (v T)

Tail get the last item of list.

func (*ArrayList[T]) UnmarshalJSON

func (al *ArrayList[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, al)

func (*ArrayList[T]) Values

func (al *ArrayList[T]) Values() []T

Values returns a slice contains all the items of the list al

type Collection

type Collection[T any] interface {
	Container

	// Add adds item v to the collection
	Add(v T)

	// Adds adds items of vs to the collection
	Adds(vs ...T)

	// AddCol adds all items of another collection
	AddCol(ac Collection[T])

	// Remove remove all items with associated value v
	Remove(v T)

	// Removes remove all items in the array vs
	Removes(vs ...T)

	// RemoveCol remove all of this collection's elements that are also contained in the specified collection
	RemoveCol(ac Collection[T])

	// RemoveIter remove all items in the iterator it
	RemoveIter(it Iterator[T])

	// RemoveFunc remove all items that function f returns true
	RemoveFunc(f func(T) bool)

	// Contain Test to see if the collection contains item v
	Contain(v T) bool

	// Contains Test to see if the collection contains all items of vs
	Contains(vs ...T) bool

	// ContainCol Test to see if the collection contains all items of another collection
	ContainCol(ac Collection[T]) bool

	// ContainIter Test to see if the collection contains all items of iterator 'it'
	ContainIter(it Iterator[T]) bool

	// Retains Retains only the elements in this collection that are contained in the argument array vs.
	Retains(vs ...T)

	// RetainCol Retains only the elements in this collection that are contained in the specified collection.
	RetainCol(ac Collection[T])

	// RetainFunc Retains all items that function f returns true
	RetainFunc(f func(T) bool)

	// Values returns a slice contains all the items of the collection
	Values() []T

	Eachable[T]
}

Collection the base collection interface

type Compare

type Compare[T any] func(a, b T) int

Compare will make type assertion (see CompareString(a,b) for example), which will panic if a or b are not of the asserted type.

Should return a int:

negative , if a < b
zero     , if a == b
positive , if a > b

type Container

type Container interface {
	// Len returns the length of the container.
	Len() int

	// IsEmpty returns true if the container length == 0
	IsEmpty() bool

	// Clear clears the container
	Clear()
}

Container the base container interface

type Deque

type Deque[T any] interface {
	// PeekHead Retrieves, but does not remove, the head of this queue, or returns (nil, false) if this queue is empty.
	PeekHead() (T, bool)

	// PollHead Retrieves and removes the head of this queue, or returns (nil, false) if this queue is empty.
	PollHead() (T, bool)

	// PushHead add item v to the head of queue
	PushHead(v T)

	// PushHeads adds items of vs to the head of queue
	PushHeads(vs ...T)

	// PeekTail Retrieves, but does not remove, the tail of this queue, or returns (nil, false) if this queue is empty.
	PeekTail() (T, bool)

	// PollTail Retrieves and removes the tail of this queue, or returns (nil, false) if this queue is empty.
	PollTail() (T, bool)

	// PushTail add item v to the tail of queue
	PushTail(v T)

	// PushTails adds items of vs to the tail of queue
	PushTails(vs ...T)
}

Deque A linear collection that supports element insertion and removal at both ends.

type Eachable

type Eachable[T any] interface {
	// Each call f for each item in the collection
	Each(f func(value T))
}

Eachable a value each interface for collection

type Eachable2

type Eachable2[K any, V any] interface {
	// Each call f for each key/value in the collection
	Each(f func(key K, value V))
}

Eachable2 a key/value each interface for collection

type HashMap

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

HashMap hash map type

Example
m := NewHashMap[int, string]()
m.Set(1, "x")   // 1->x
m.Set(2, "b")   // 2->b, 1->x (random order)
m.Set(1, "a")   // 2->b, 1->a (random order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values()  // []interface {}{"b", "a"} (random order)
_ = m.Keys()    // []interface {}{1, 2} (random order)
m.Remove(1)     // 2->b
m.Clear()       // empty
m.IsEmpty()     // true
m.Len()         // 0
Output:

func AsHashMap

func AsHashMap[K comparable, V any](m map[K]V) *HashMap[K, V]

AsHashMap creates a new HashMap from a map. Example: AsHashMap(map[K]V{"k1": "v1", "k2": "v2"})

func NewHashMap

func NewHashMap[K comparable, V any](kvs ...P[K, V]) *HashMap[K, V]

NewHashMap creates a new HashMap.

func (*HashMap[K, V]) Clear

func (hm *HashMap[K, V]) Clear()

Clear clears the map

func (*HashMap[K, V]) Contain added in v1.0.10

func (hm *HashMap[K, V]) Contain(k K) bool

Contain Test to see if the list contains the key k

func (*HashMap[K, V]) Contains

func (hm *HashMap[K, V]) Contains(ks ...K) bool

Contains looks for the given key, and returns true if the key exists in the map.

func (*HashMap[K, V]) Copy added in v1.0.10

func (hm *HashMap[K, V]) Copy(am Map[K, V])

Copy copy items from another map am, override the existing items

func (*HashMap[K, V]) Each

func (hm *HashMap[K, V]) Each(f func(k K, v V))

Each call f for each item(k,v) in the map

func (*HashMap[K, V]) Entries added in v1.0.12

func (hm *HashMap[K, V]) Entries() []P[K, V]

Entries returns the key-value pair slice

func (*HashMap[K, V]) Get

func (hm *HashMap[K, V]) Get(key K) (v V, ok bool)

Get looks for the given key, and returns the value associated with it, or nil if not found. The boolean it returns says whether the key is ok in the map.

func (*HashMap[K, V]) HashMap

func (hm *HashMap[K, V]) HashMap() map[K]V

HashMap returns underlying hash map

func (*HashMap[K, V]) IsEmpty

func (hm *HashMap[K, V]) IsEmpty() bool

IsEmpty returns true if the map has no items

func (*HashMap[K, V]) Keys

func (hm *HashMap[K, V]) Keys() []K

Keys returns the key slice

func (*HashMap[K, V]) Len

func (hm *HashMap[K, V]) Len() int

Len returns the length of the linked map.

func (*HashMap[K, V]) MarshalJSON

func (hm *HashMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(lm)

func (*HashMap[K, V]) MustGet added in v1.0.12

func (hm *HashMap[K, V]) MustGet(key K, defaults ...V) V

MustGet looks for the given key, and returns the value associated with it. If not found, return defaults[0] or panic if defaults is not supplied.

func (*HashMap[K, V]) Remove added in v1.0.10

func (hm *HashMap[K, V]) Remove(k K) (ov V, ok bool)

Remove remove the item with key k, and returns what `Get` would have returned on that key prior to the call to `Set`.

func (*HashMap[K, V]) Removes added in v1.0.10

func (hm *HashMap[K, V]) Removes(ks ...K)

Removes remove all items with key of ks.

func (*HashMap[K, V]) Set

func (hm *HashMap[K, V]) Set(key K, value V) (ov V, ok bool)

Set sets the paired key-value items, and returns what `Get` would have returned on that key prior to the call to `Set`.

func (*HashMap[K, V]) SetEntries added in v1.0.12

func (hm *HashMap[K, V]) SetEntries(pairs ...P[K, V])

SetEntries set items from key-value items array, override the existing items

func (*HashMap[K, V]) SetIfAbsent

func (hm *HashMap[K, V]) SetIfAbsent(key K, value V) (ov V, ok bool)

SetIfAbsent sets the key-value item if the key does not exists in the map, and returns what `Get` would have returned on that key prior to the call to `Set`.

func (*HashMap[K, V]) String

func (hm *HashMap[K, V]) String() string

String print map to string

func (*HashMap[K, V]) UnmarshalJSON

func (hm *HashMap[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, lm)

func (*HashMap[K, V]) Values

func (hm *HashMap[K, V]) Values() []V

Values returns the value slice

type HashSet

type HashSet[T comparable] struct {
	// contains filtered or unexported fields
}

HashSet an unordered collection of unique values. The zero value for HashSet is an empty set ready to use. http://en.wikipedia.org/wiki/Set_(computer_science%29)

Example
set := NewHashSet[int]()
set.Add(1)              // 1
set.Adds(2, 2, 3, 4, 5) // 3, 1, 2, 4, 5 (random order, duplicates ignored)
set.Remove(4)           // 5, 3, 2, 1 (random order)
set.Removes(2, 3)       // 1, 5 (random order)
set.Contain(1)          // true
set.Contains(1, 5)      // true
set.Contains(1, 6)      // false
_ = set.Values()        // []int{5,1} (random order)
set.Clear()             // empty
set.IsEmpty()           // true
set.Len()               // 0
Output:

func NewHashSet

func NewHashSet[T comparable](vs ...T) *HashSet[T]

NewHashSet Create a new hash set

func (*HashSet[T]) Add

func (hs *HashSet[T]) Add(v T)

Add Add the item v to the set

func (*HashSet[T]) AddCol added in v1.0.10

func (hs *HashSet[T]) AddCol(ac Collection[T])

AddCol adds all items of another collection

func (*HashSet[T]) Adds added in v1.0.10

func (hs *HashSet[T]) Adds(vs ...T)

Adds Adds all items of vs to the set

func (*HashSet[T]) Clear

func (hs *HashSet[T]) Clear()

Clear clears the hash set.

func (*HashSet[T]) Contain added in v1.0.10

func (hs *HashSet[T]) Contain(v T) bool

Contain Test to see if the list contains the value v

func (*HashSet[T]) ContainCol added in v1.0.10

func (hs *HashSet[T]) ContainCol(ac Collection[T]) bool

ContainCol Test to see if the collection contains all items of another collection

func (*HashSet[T]) ContainIter added in v1.0.12

func (hs *HashSet[T]) ContainIter(it Iterator[T]) bool

ContainIter Test to see if the collection contains all items of iterator 'it'

func (*HashSet[T]) Contains

func (hs *HashSet[T]) Contains(vs ...T) bool

Contains Test to see if the collection contains all items of vs

func (*HashSet[T]) Difference

func (hs *HashSet[T]) Difference(a *HashSet[T]) *HashSet[T]

Difference Find the difference btween two sets

func (*HashSet[T]) Each

func (hs *HashSet[T]) Each(f func(T))

Each Call f for each item in the set

func (*HashSet[T]) Intersection

func (hs *HashSet[T]) Intersection(a *HashSet[T]) *HashSet[T]

Intersection Find the intersection of two sets

func (*HashSet[T]) IsEmpty

func (hs *HashSet[T]) IsEmpty() bool

IsEmpty returns true if the set's length == 0

func (*HashSet[T]) Len

func (hs *HashSet[T]) Len() int

Len Return the number of items in the set

func (*HashSet[T]) MarshalJSON

func (hs *HashSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(hs)

func (*HashSet[T]) Remove added in v1.0.10

func (hs *HashSet[T]) Remove(v T)

Remove remove all items with associated value v of vs

func (*HashSet[T]) RemoveCol added in v1.0.10

func (hs *HashSet[T]) RemoveCol(ac Collection[T])

RemoveCol remove all of this collection's elements that are also contained in the specified collection

func (*HashSet[T]) RemoveFunc added in v1.0.12

func (hs *HashSet[T]) RemoveFunc(f func(T) bool)

RemoveFunc remove all items that function f returns true

func (*HashSet[T]) RemoveIter added in v1.0.12

func (hs *HashSet[T]) RemoveIter(it Iterator[T])

RemoveIter remove all items in the iterator it

func (*HashSet[T]) Removes added in v1.0.10

func (hs *HashSet[T]) Removes(vs ...T)

Removes delete items of vs

func (*HashSet[T]) RetainCol added in v1.0.10

func (hs *HashSet[T]) RetainCol(ac Collection[T])

RetainCol Retains only the elements in this collection that are contained in the specified collection.

func (*HashSet[T]) RetainFunc added in v1.0.12

func (hs *HashSet[T]) RetainFunc(f func(T) bool)

RetainFunc Retains all items that function f returns true

func (*HashSet[T]) Retains added in v1.0.10

func (hs *HashSet[T]) Retains(vs ...T)

Retains Retains only the elements in this collection that are contained in the argument array vs.

func (*HashSet[T]) String

func (hs *HashSet[T]) String() string

String print the set to string

func (*HashSet[T]) UnmarshalJSON

func (hs *HashSet[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, hs)

func (*HashSet[T]) Values

func (hs *HashSet[T]) Values() []T

Values returns a slice contains all the items of the set hs

type Iterable

type Iterable[T any] interface {
	// Iterator returns a iterator for collection
	Iterator() Iterator[T]
}

Iterable a value iterable interface for collection

type Iterable2

type Iterable2[K any, V any] interface {
	// Iterator returns a iterator for collection
	Iterator() Iterator2[K, V]
}

Iterable2 a key/value iterable interface for collection

type IterableMap

type IterableMap[K any, V any] interface {
	Map[K, V]

	ReverseEachable2[K, V]

	Iterable2[K, V]
}

IterableMap a iterable map interface

type Iterator

type Iterator[T any] interface {
	// Prev moves the iterator to the previous item and returns true if there was a previous item in collection.
	// If Prev() returns true, then previous item's index and value can be retrieved by Index() and Value().
	// Modifies the state of the iterator.
	Prev() bool

	// Next moves the iterator to the next item and returns true if there was a next item in the collection.
	// If Next() returns true, then next item's value can be retrieved by Value().
	// If Next() was called for the first time, then it will point the iterator to the first item if it exists.
	// Modifies the state of the iterator.
	Next() bool

	// Value returns the current item's value.
	Value() T

	// SetValue set the current item's value.
	SetValue(v T)

	// Remove remove the current item
	Remove()

	// Reset resets the iterator to its initial state (one-before-first/one-after-last)
	// Call Next()/Prev() to fetch the first/last item if any.
	Reset()
}

Iterator is stateful iterator for collection.

type Iterator2

type Iterator2[K any, V any] interface {
	Iterator[V]

	// Key returns the current item's key.
	Key() K
}

Iterator2 is stateful iterator for Key/Value paire.

type Less

type Less[T any] func(a, b T) bool

Less will make type assertion (see LessString(a,b) for example), which will panic if a or b are not of the asserted type.

Should return a bool:

true , if a < b
false, if a >= b

type LinkedHashMap

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

LinkedHashMap implements an linked map that keeps track of the order in which keys were inserted. The zero value for LinkedHashMap is an empty map ready to use.

To iterate over a linked map (where lm is a *LinkedHashMap):

it := lm.Iterator()
for it.Next() {
	// do something with it.Key(), it.Value()
}
Example
m := NewLinkedHashMap[int, string]()
m.Set(2, "b")   // 2->b
m.Set(1, "x")   // 2->b, 1->x (insertion-order)
m.Set(1, "a")   // 2->b, 1->a (insertion-order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values()  // []interface {}{"b", "a"} (insertion-order)
_ = m.Keys()    // []interface {}{2, 1} (insertion-order)
m.Remove(1)     // 2->b
m.Clear()       // empty
m.IsEmpty()     // true
m.Len()         // 0
Output:

func NewLinkedHashMap

func NewLinkedHashMap[K comparable, V any](kvs ...P[K, V]) *LinkedHashMap[K, V]

NewLinkedHashMap creates a new LinkedHashMap.

Example
// initialize from a list of key-value pairs
lm := NewLinkedHashMap([]P[string, any]{
	{"country", "United States"},
	{"countryCode", "US"},
	{"region", "CA"},
	{"regionName", "California"},
	{"city", "Mountain View"},
	{"zip", "94043"},
	{"lat", 37.4192},
	{"lon", -122.0574},
	{"timezone", "America/Los_Angeles"},
	{"isp", "Google Cloud"},
	{"org", "Google Cloud"},
	{"as", "AS15169 Google Inc."},
	{"mobile", true},
	{"proxy", false},
	{"query", "35.192.xx.xxx"},
}...)

for it := lm.Iterator(); it.Next(); {
	fmt.Printf("%-12s: %v\n", it.Key(), it.Value())
}
Output:

country     : United States
countryCode : US
region      : CA
regionName  : California
city        : Mountain View
zip         : 94043
lat         : 37.4192
lon         : -122.0574
timezone    : America/Los_Angeles
isp         : Google Cloud
org         : Google Cloud
as          : AS15169 Google Inc.
mobile      : true
proxy       : false
query       : 35.192.xx.xxx

func (*LinkedHashMap[K, V]) Clear

func (lm *LinkedHashMap[K, V]) Clear()

Clear clears the map

func (*LinkedHashMap[K, V]) Contain added in v1.0.10

func (lm *LinkedHashMap[K, V]) Contain(k K) bool

Contain Test to see if the list contains the key k

func (*LinkedHashMap[K, V]) Contains

func (lm *LinkedHashMap[K, V]) Contains(ks ...K) bool

Contains looks for the given key, and returns true if the key exists in the map.

func (*LinkedHashMap[K, V]) Copy added in v1.0.10

func (lm *LinkedHashMap[K, V]) Copy(am Map[K, V])

Copy copy items from another map am, override the existing items

func (*LinkedHashMap[K, V]) Each

func (lm *LinkedHashMap[K, V]) Each(f func(k K, v V))

Each call f for each item in the map

func (*LinkedHashMap[K, V]) Entries added in v1.0.12

func (lm *LinkedHashMap[K, V]) Entries() []P[K, V]

Entries returns the key-value pair slice

func (*LinkedHashMap[K, V]) Get

func (lm *LinkedHashMap[K, V]) Get(key K) (V, bool)

Get looks for the given key, and returns the value associated with it, or nil if not found. The boolean it returns says whether the key is ok in the map.

func (*LinkedHashMap[K, V]) Head

func (lm *LinkedHashMap[K, V]) Head() *LinkedMapNode[K, V]

Head returns the oldest key/value item.

func (*LinkedHashMap[K, V]) IsEmpty

func (lm *LinkedHashMap[K, V]) IsEmpty() bool

IsEmpty returns true if the map has no items

func (*LinkedHashMap[K, V]) Items

func (lm *LinkedHashMap[K, V]) Items() []*LinkedMapNode[K, V]

Items returns the map item slice

func (*LinkedHashMap[K, V]) Iterator

func (lm *LinkedHashMap[K, V]) Iterator() Iterator2[K, V]

Iterator returns a iterator for the map

func (*LinkedHashMap[K, V]) IteratorOf

func (lm *LinkedHashMap[K, V]) IteratorOf(k K) Iterator2[K, V]

IteratorOf returns a iterator at the specified key Returns nil if the map does not contains the key

func (*LinkedHashMap[K, V]) Keys

func (lm *LinkedHashMap[K, V]) Keys() []K

Keys returns the key slice

func (*LinkedHashMap[K, V]) Len

func (lm *LinkedHashMap[K, V]) Len() int

Len returns the length of the linked map.

func (*LinkedHashMap[K, V]) MarshalJSON

func (lm *LinkedHashMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(lm)

func (*LinkedHashMap[K, V]) MustGet added in v1.0.12

func (lm *LinkedHashMap[K, V]) MustGet(key K, defaults ...V) V

MustGet looks for the given key, and returns the value associated with it. If not found, return defaults[0] or panic if defaults is not supplied.

func (*LinkedHashMap[K, V]) PollHead

func (lm *LinkedHashMap[K, V]) PollHead() *LinkedMapNode[K, V]

PollHead remove the first item of map.

func (*LinkedHashMap[K, V]) PollTail

func (lm *LinkedHashMap[K, V]) PollTail() *LinkedMapNode[K, V]

PollTail remove the last item of map.

func (*LinkedHashMap[K, V]) Remove added in v1.0.10

func (lm *LinkedHashMap[K, V]) Remove(k K) (ov V, ok bool)

Remove remove the item with key k, and returns what `Get` would have returned on that key prior to the call to `Set`.

func (*LinkedHashMap[K, V]) Removes added in v1.0.10

func (lm *LinkedHashMap[K, V]) Removes(ks ...K)

Remove remove all items with key of ks.

func (*LinkedHashMap[K, V]) ReverseEach

func (lm *LinkedHashMap[K, V]) ReverseEach(f func(k K, v V))

ReverseEach call f for each item in the map with reverse order

func (*LinkedHashMap[K, V]) Set

func (lm *LinkedHashMap[K, V]) Set(key K, value V) (ov V, ok bool)

Set sets the paired key-value items, and returns what `Get` would have returned on that key prior to the call to `Set`.

func (*LinkedHashMap[K, V]) SetEntries added in v1.0.12

func (lm *LinkedHashMap[K, V]) SetEntries(pairs ...P[K, V])

SetEntries set items from key-value items array, override the existing items

func (*LinkedHashMap[K, V]) SetIfAbsent

func (lm *LinkedHashMap[K, V]) SetIfAbsent(key K, value V) (ov V, ok bool)

SetIfAbsent sets the key-value item if the key does not exists in the map, and returns what `Get` would have returned on that key prior to the call to `Set`. Example: lm.SetIfAbsent("k1", "v1", "k2", "v2")

func (*LinkedHashMap[K, V]) String

func (lm *LinkedHashMap[K, V]) String() string

String print map to string

func (*LinkedHashMap[K, V]) Tail

func (lm *LinkedHashMap[K, V]) Tail() *LinkedMapNode[K, V]

Tail returns the newest key/value item.

func (*LinkedHashMap[K, V]) UnmarshalJSON

func (lm *LinkedHashMap[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, lm)

Example
const jsonStream = `{
  "country"     : "United States",
  "countryCode" : "US",
  "region"      : "CA",
  "regionName"  : "California",
  "city"        : "Mountain View",
  "zip"         : "94043",
  "lat"         : "37.4192",
  "lon"         : "-122.0574",
  "timezone"    : "America/Los_Angeles",
  "isp"         : "Google Cloud",
  "org"         : "Google Cloud",
  "as"          : "AS15169 Google Inc.",
  "mobile"      : "true",
  "proxy"       : "false",
  "query"       : "35.192.xx.xxx"
}`

// compare with if using a regular generic map, the unmarshalled result
//  is a map with unpredictable order of keys
var m map[string]any
err := json.Unmarshal([]byte(jsonStream), &m)
if err != nil {
	fmt.Println("error:", err)
}
for key := range m {
	// fmt.Printf("%-12s: %v\n", key, m[key])
	_ = key
}

// use the LinkedHashMap to Unmarshal from JSON object
lm := NewLinkedHashMap[string, string]()
err = json.Unmarshal([]byte(jsonStream), lm)
if err != nil {
	fmt.Println("error:", err)
}

// loop over all key-value pairs,
// it is ok to call Set append-modify new key-value pairs,
// but not safe to call Delete during iteration.
for it := lm.Iterator(); it.Next(); {
	fmt.Printf("%-12s: %v\n", it.Key(), it.Value())
	if it.Key() == "city" {
		lm.Set("mobile", "false")
		lm.Set("extra", "42")
	}
}
Output:

country     : United States
countryCode : US
region      : CA
regionName  : California
city        : Mountain View
zip         : 94043
lat         : 37.4192
lon         : -122.0574
timezone    : America/Los_Angeles
isp         : Google Cloud
org         : Google Cloud
as          : AS15169 Google Inc.
mobile      : false
proxy       : false
query       : 35.192.xx.xxx
extra       : 42

func (*LinkedHashMap[K, V]) Values

func (lm *LinkedHashMap[K, V]) Values() []V

Values returns the value slice

type LinkedHashSet

type LinkedHashSet[T comparable] struct {
	// contains filtered or unexported fields
}

LinkedHashSet implements a doubly linked set. The zero value for LinkedHashSet is an empty set ready to use. Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.Add(e) is invoked when s.Contains(e) would return true immediately prior to the invocation.)

To iterate over a set (where ls is a *LinkedHashSet):

it := ls.Iterator()
for it.Next() {
	// do something with it.Value()
}

func NewLinkedHashSet

func NewLinkedHashSet[T comparable](vs ...T) *LinkedHashSet[T]

NewLinkedHashSet returns an initialized set. Example: cog.NewLinkedHashSet(1, 2, 3)

Example
set := NewLinkedHashSet[int]()
set.Add(5)              // 5
set.Adds(4, 4, 3, 2, 1) // 5, 4, 3, 2, 1 (in insertion-order, duplicates ignored)
set.Add(4)              // 5, 4, 3, 2, 1 (duplicates ignored, insertion-order unchanged)
set.Remove(4)           // 5, 3, 2, 1 (in insertion-order)
set.Removes(2, 3)       // 5, 1 (in insertion-order)
set.Contains(1)         // true
set.Contains(1, 5)      // true
set.Contains(1, 6)      // false
_ = set.Values()        // []int{5, 1} (in insertion-order)
set.Clear()             // empty
set.IsEmpty()           // true
set.Len()               // 0
Output:

func (*LinkedHashSet[T]) Add

func (ls *LinkedHashSet[T]) Add(v T)

Add add the item v. Note: existing item's order will not change.

func (*LinkedHashSet[T]) AddCol added in v1.0.10

func (ls *LinkedHashSet[T]) AddCol(ac Collection[T])

AddCol adds all items of another collection Note: existing item's order will not change.

func (*LinkedHashSet[T]) Adds added in v1.0.10

func (ls *LinkedHashSet[T]) Adds(vs ...T)

Adds adds all items of vs. Note: existing item's order will not change.

func (*LinkedHashSet[T]) Clear

func (ls *LinkedHashSet[T]) Clear()

Clear clears set ls.

func (*LinkedHashSet[T]) Contain added in v1.0.10

func (ls *LinkedHashSet[T]) Contain(v T) bool

Contain Test to see if the list contains the value v

func (*LinkedHashSet[T]) ContainCol added in v1.0.10

func (ls *LinkedHashSet[T]) ContainCol(ac Collection[T]) bool

ContainCol Test to see if the collection contains all items of another collection

func (*LinkedHashSet[T]) ContainIter added in v1.0.12

func (ls *LinkedHashSet[T]) ContainIter(it Iterator[T]) bool

ContainIter Test to see if the collection contains all items of iterator 'it'

func (*LinkedHashSet[T]) Contains

func (ls *LinkedHashSet[T]) Contains(vs ...T) bool

Contains Test to see if the collection contains all items of vs

func (*LinkedHashSet[T]) DeleteAt added in v1.0.12

func (ls *LinkedHashSet[T]) DeleteAt(index int)

DeleteAt removes the element at the specified position in this set.

func (*LinkedHashSet[T]) Each

func (ls *LinkedHashSet[T]) Each(f func(T))

Each call f for each item in the set

func (*LinkedHashSet[T]) Get

func (ls *LinkedHashSet[T]) Get(index int) T

Get returns the element at the specified position in this set if i < -ls.Len() or i >= ls.Len(), panic if i < 0, returns ls.Get(ls.Len() + i)

func (*LinkedHashSet[T]) Head

func (ls *LinkedHashSet[T]) Head() (v T)

Head get the first item of set.

func (*LinkedHashSet[T]) Index

func (ls *LinkedHashSet[T]) Index(v T) int

Index returns the index of the specified v in this set, or -1 if this set does not contain v.

func (*LinkedHashSet[T]) Insert

func (ls *LinkedHashSet[T]) Insert(index int, v T)

Insert insert value v at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than set's size Note: position equal to set's size is valid, i.e. append. Note: existing item's order will not change.

func (*LinkedHashSet[T]) InsertCol added in v1.0.10

func (ls *LinkedHashSet[T]) InsertCol(index int, ac Collection[T])

InsertCol inserts values of another collection ac at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than list's size Note: position equal to list's size is valid, i.e. append. Note: existing item's order will not change.

func (*LinkedHashSet[T]) Inserts added in v1.0.10

func (ls *LinkedHashSet[T]) Inserts(index int, vs ...T)

Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than set's size Note: position equal to set's size is valid, i.e. append. Note: existing item's order will not change.

func (*LinkedHashSet[T]) IsEmpty

func (ls *LinkedHashSet[T]) IsEmpty() bool

IsEmpty returns true if the set length == 0

func (*LinkedHashSet[T]) Iterator

func (ls *LinkedHashSet[T]) Iterator() Iterator[T]

Iterator returns a iterator for the set

func (*LinkedHashSet[T]) Len

func (ls *LinkedHashSet[T]) Len() int

Len returns the length of the set.

func (*LinkedHashSet[T]) MarshalJSON

func (ls *LinkedHashSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(ls)

func (*LinkedHashSet[T]) Peek

func (ls *LinkedHashSet[T]) Peek() (v T, ok bool)

Peek get the first item of set.

func (*LinkedHashSet[T]) PeekHead

func (ls *LinkedHashSet[T]) PeekHead() (v T, ok bool)

PeekHead get the first item of set.

func (*LinkedHashSet[T]) PeekTail

func (ls *LinkedHashSet[T]) PeekTail() (v T, ok bool)

PeekTail get the last item of set.

func (*LinkedHashSet[T]) Poll

func (ls *LinkedHashSet[T]) Poll() (T, bool)

Poll get and remove the first item of set.

func (*LinkedHashSet[T]) PollHead

func (ls *LinkedHashSet[T]) PollHead() (v T, ok bool)

PollHead remove the first item of set.

func (*LinkedHashSet[T]) PollTail

func (ls *LinkedHashSet[T]) PollTail() (v T, ok bool)

PollTail remove the last item of set.

func (*LinkedHashSet[T]) Push

func (ls *LinkedHashSet[T]) Push(v T)

Push insert the item v at the tail of set al.

func (*LinkedHashSet[T]) PushHead

func (ls *LinkedHashSet[T]) PushHead(v T)

PushHead insert the item v at the head of set ls.

func (*LinkedHashSet[T]) PushHeadCol added in v1.0.10

func (ls *LinkedHashSet[T]) PushHeadCol(ac Collection[T])

PushHeadCol inserts a copy of another collection at the head of set ls. The ls and ac may be the same. They must not be nil.

func (*LinkedHashSet[T]) PushHeads added in v1.0.10

func (ls *LinkedHashSet[T]) PushHeads(vs ...T)

PushHeads inserts all items of vs at the head of set ls.

func (*LinkedHashSet[T]) PushTail

func (ls *LinkedHashSet[T]) PushTail(v T)

PushTail insert the item v at the tail of set ls.

func (*LinkedHashSet[T]) PushTailCol added in v1.0.10

func (ls *LinkedHashSet[T]) PushTailCol(ac Collection[T])

PushTailCol inserts a copy of another collection at the tail of set ls. The ls and ac may be the same. They must not be nil.

func (*LinkedHashSet[T]) PushTails added in v1.0.10

func (ls *LinkedHashSet[T]) PushTails(vs ...T)

PushTails inserts all items of vs at the tail of set ls.

func (*LinkedHashSet[T]) Pushs added in v1.0.10

func (ls *LinkedHashSet[T]) Pushs(vs ...T)

Pushs inserts all items of vs at the tail of set al.

func (*LinkedHashSet[T]) Remove

func (ls *LinkedHashSet[T]) Remove(v T)

Remove remove all items with associated value v

func (*LinkedHashSet[T]) RemoveCol added in v1.0.10

func (ls *LinkedHashSet[T]) RemoveCol(ac Collection[T])

RemoveCol remove all of this collection's elements that are also contained in the specified collection

func (*LinkedHashSet[T]) RemoveFunc added in v1.0.12

func (ls *LinkedHashSet[T]) RemoveFunc(f func(T) bool)

RemoveFunc remove all items that function f returns true

func (*LinkedHashSet[T]) RemoveIter added in v1.0.12

func (ls *LinkedHashSet[T]) RemoveIter(it Iterator[T])

RemoveIter remove all items in the iterator it

func (*LinkedHashSet[T]) Removes added in v1.0.10

func (ls *LinkedHashSet[T]) Removes(vs ...T)

Removes remove all items in the array vs

func (*LinkedHashSet[T]) RetainCol added in v1.0.10

func (ls *LinkedHashSet[T]) RetainCol(ac Collection[T])

RetainCol Retains only the elements in this collection that are contained in the specified collection.

func (*LinkedHashSet[T]) RetainFunc added in v1.0.12

func (ls *LinkedHashSet[T]) RetainFunc(f func(T) bool)

RetainFunc Retains all items that function f returns true

func (*LinkedHashSet[T]) Retains added in v1.0.10

func (ls *LinkedHashSet[T]) Retains(vs ...T)

Retains Retains only the elements in this collection that are contained in the argument array vs.

func (*LinkedHashSet[T]) ReverseEach

func (ls *LinkedHashSet[T]) ReverseEach(f func(T))

ReverseEach call f for each item in the set with reverse order

func (*LinkedHashSet[T]) Set

func (ls *LinkedHashSet[T]) Set(index int, v T) (ov T)

Set set the v at the specified index in this set and returns the old value. Old item at index will be removed.

func (*LinkedHashSet[T]) Sort

func (ls *LinkedHashSet[T]) Sort(less Less[T])

Sort Sorts this set according to the order induced by the specified Comparator.

func (*LinkedHashSet[T]) String

func (ls *LinkedHashSet[T]) String() string

String print list to string

func (*LinkedHashSet[T]) Swap

func (ls *LinkedHashSet[T]) Swap(i, j int)

Swap swaps values of two items at the given index.

func (*LinkedHashSet[T]) Tail

func (ls *LinkedHashSet[T]) Tail() (v T)

Tail get the last item of set.

func (*LinkedHashSet[T]) UnmarshalJSON

func (ls *LinkedHashSet[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, ls)

func (*LinkedHashSet[T]) Values

func (ls *LinkedHashSet[T]) Values() []T

Values returns a slice contains all the items of the set ls

type LinkedList

type LinkedList[T any] struct {
	// contains filtered or unexported fields
}

LinkedList implements a doubly linked list. The zero value for LinkedList is an empty list ready to use.

To iterate over a list (where ll is a *LinkedList):

it := ll.Iterator()
for it.Next() {
	// do something with it.Value()
}

func NewLinkedList

func NewLinkedList[T any](vs ...T) *LinkedList[T]

NewLinkedList returns an initialized list. Example: cog.NewLinkedList(1, 2, 3)

Example
list := NewLinkedList[string]()
list.Add("a")                         // ["a"]
list.Adds("c", "b")                   // ["a","c","b"]
list.Sort(LessString)                 // ["a","b","c"]
_ = list.Get(0)                       // "a"  //_ = list.Get(100)  --> panic
_ = list.Contains("a", "b", "c")      // true
_ = list.Contains("a", "b", "c", "d") // false
list.Swap(0, 1)                       // ["b","a",c"]
list.DeleteAt(2)                      // ["b","a"]
list.DeleteAt(1)                      // ["b"]
list.DeleteAt(0)                      // []
_ = list.IsEmpty()                    // true
_ = list.Len()                        // 0
list.Add("a")                         // ["a"]
list.Clear()                          // []
list.Insert(0, "b")                   // ["b"]
list.Insert(0, "a")                   // ["a","b"]
Output:

func (*LinkedList[T]) Add

func (ll *LinkedList[T]) Add(v T)

Add add the item v.

func (*LinkedList[T]) AddCol added in v1.0.10

func (ll *LinkedList[T]) AddCol(ac Collection[T])

AddCol adds all items of another collection

func (*LinkedList[T]) Adds added in v1.0.10

func (ll *LinkedList[T]) Adds(vs ...T)

Adds adds all items of vs.

func (*LinkedList[T]) Clear

func (ll *LinkedList[T]) Clear()

Clear clears list ll.

func (*LinkedList[T]) Contain added in v1.0.10

func (ll *LinkedList[T]) Contain(v T) bool

Contain Test to see if the list contains the value v

func (*LinkedList[T]) ContainCol added in v1.0.10

func (ll *LinkedList[T]) ContainCol(ac Collection[T]) bool

ContainCol Test to see if the collection contains all items of another collection

func (*LinkedList[T]) ContainIter added in v1.0.12

func (ll *LinkedList[T]) ContainIter(it Iterator[T]) bool

ContainIter Test to see if the collection contains all items of iterator 'it'

func (*LinkedList[T]) Contains

func (ll *LinkedList[T]) Contains(vs ...T) bool

Contains Test to see if the collection contains all items of vs

func (*LinkedList[T]) DeleteAt added in v1.0.12

func (ll *LinkedList[T]) DeleteAt(index int)

DeleteAt remove the element at the specified position in this list.

func (*LinkedList[T]) Each

func (ll *LinkedList[T]) Each(f func(T))

Each call f for each item in the list

func (*LinkedList[T]) Get

func (ll *LinkedList[T]) Get(index int) T

Get returns the element at the specified position in this list if i < -ll.Len() or i >= ll.Len(), panic if i < 0, returns ll.Get(ll.Len() + i)

func (*LinkedList[T]) Head

func (ll *LinkedList[T]) Head() (v T)

Head get the first item of list.

func (*LinkedList[T]) Index

func (ll *LinkedList[T]) Index(v T) int

Index returns the index of the first occurrence of the specified v in this list, or -1 if this list does not contain v.

func (*LinkedList[T]) IndexFunc added in v1.0.12

func (ll *LinkedList[T]) IndexFunc(f func(T) bool) int

IndexFunc returns the index of the first true returned by function f in this list, or -1 if this list does not contain v.

func (*LinkedList[T]) Insert

func (ll *LinkedList[T]) Insert(index int, v T)

Insert insert value v at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than list's size Note: position equal to list's size is valid, i.e. append.

func (*LinkedList[T]) InsertCol added in v1.0.10

func (ll *LinkedList[T]) InsertCol(index int, ac Collection[T])

InsertCol inserts values of another collection ac at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than list's size Note: position equal to list's size is valid, i.e. append.

func (*LinkedList[T]) Inserts added in v1.0.10

func (ll *LinkedList[T]) Inserts(index int, vs ...T)

Inserts inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than list's size Note: position equal to list's size is valid, i.e. append.

func (*LinkedList[T]) IsEmpty

func (ll *LinkedList[T]) IsEmpty() bool

IsEmpty returns true if the list length == 0

func (*LinkedList[T]) Iterator

func (ll *LinkedList[T]) Iterator() Iterator[T]

Iterator returns a iterator for the list

func (*LinkedList[T]) LastIndex

func (ll *LinkedList[T]) LastIndex(v T) int

LastIndex returns the index of the last occurrence of the specified v in this list, or -1 if this list does not contain v.

func (*LinkedList[T]) Len

func (ll *LinkedList[T]) Len() int

Len returns the length of the list.

func (*LinkedList[T]) MarshalJSON

func (ll *LinkedList[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(ll)

func (*LinkedList[T]) Peek

func (ll *LinkedList[T]) Peek() (v T, ok bool)

Peek get the first item of list.

func (*LinkedList[T]) PeekHead

func (ll *LinkedList[T]) PeekHead() (v T, ok bool)

PeekHead get the first item of list.

func (*LinkedList[T]) PeekTail

func (ll *LinkedList[T]) PeekTail() (v T, ok bool)

PeekTail get the last item of list.

func (*LinkedList[T]) Poll

func (ll *LinkedList[T]) Poll() (T, bool)

Poll get and remove the first item of list.

func (*LinkedList[T]) PollHead

func (ll *LinkedList[T]) PollHead() (v T, ok bool)

PollHead remove the first item of list.

func (*LinkedList[T]) PollTail

func (ll *LinkedList[T]) PollTail() (v T, ok bool)

PollTail remove the last item of list.

func (*LinkedList[T]) Push

func (ll *LinkedList[T]) Push(v T)

Push insert the item v at the tail of list al.

func (*LinkedList[T]) PushHead

func (ll *LinkedList[T]) PushHead(v T)

PushHead insert the item v at the head of list ll.

func (*LinkedList[T]) PushHeadCol added in v1.0.10

func (ll *LinkedList[T]) PushHeadCol(ac Collection[T])

PushHeadCol inserts a copy of another collection at the head of list ll. The ll and ac may be the same. They must not be nil.

func (*LinkedList[T]) PushHeads added in v1.0.10

func (ll *LinkedList[T]) PushHeads(vs ...T)

PushHeads inserts all items of vs at the head of list ll.

func (*LinkedList[T]) PushTail

func (ll *LinkedList[T]) PushTail(v T)

PushTail insert the item v at the tail of list ll.

func (*LinkedList[T]) PushTailCol added in v1.0.10

func (ll *LinkedList[T]) PushTailCol(ac Collection[T])

PushTailCol inserts a copy of another collection at the tail of list ll. The ll and ac may be the same. They must not be nil.

func (*LinkedList[T]) PushTails added in v1.0.10

func (ll *LinkedList[T]) PushTails(vs ...T)

PushTails inserts all items of vs at the tail of list ll.

func (*LinkedList[T]) Pushs added in v1.0.10

func (ll *LinkedList[T]) Pushs(vs ...T)

Pushs inserts all items of vs at the tail of list al.

func (*LinkedList[T]) Remove

func (ll *LinkedList[T]) Remove(v T)

Remove remove all items with associated value v of vs

func (*LinkedList[T]) RemoveCol added in v1.0.10

func (ll *LinkedList[T]) RemoveCol(ac Collection[T])

RemoveCol remove all of this collection's elements that are also contained in the specified collection

func (*LinkedList[T]) RemoveFunc added in v1.0.12

func (ll *LinkedList[T]) RemoveFunc(f func(T) bool)

RemoveFunc remove all items that function f returns true

func (*LinkedList[T]) RemoveIter added in v1.0.12

func (ll *LinkedList[T]) RemoveIter(it Iterator[T])

RemoveIter remove all items in the iterator it

func (*LinkedList[T]) Removes added in v1.0.10

func (ll *LinkedList[T]) Removes(vs ...T)

Removes remove all items in the array vs

func (*LinkedList[T]) RetainCol added in v1.0.10

func (ll *LinkedList[T]) RetainCol(ac Collection[T])

RetainCol Retains only the elements in this collection that are contained in the specified collection.

func (*LinkedList[T]) RetainFunc added in v1.0.12

func (ll *LinkedList[T]) RetainFunc(f func(T) bool)

RetainFunc Retains all items that function f returns true

func (*LinkedList[T]) Retains added in v1.0.10

func (ll *LinkedList[T]) Retains(vs ...T)

Retains Retains only the elements in this collection that are contained in the argument array vs.

func (*LinkedList[T]) ReverseEach

func (ll *LinkedList[T]) ReverseEach(f func(T))

ReverseEach call f for each item in the list with reverse order

func (*LinkedList[T]) Set

func (ll *LinkedList[T]) Set(index int, v T) (ov T)

Set set the v at the specified index in this list and returns the old value.

func (*LinkedList[T]) Sort

func (ll *LinkedList[T]) Sort(less Less[T])

Sort Sorts this list according to the order induced by the specified Comparator.

func (*LinkedList[T]) String

func (ll *LinkedList[T]) String() string

String print list to string

func (*LinkedList[T]) Swap

func (ll *LinkedList[T]) Swap(i, j int)

Swap swaps values of two items at the given index.

func (*LinkedList[T]) Tail

func (ll *LinkedList[T]) Tail() (v T)

Tail get the last item of list.

func (*LinkedList[T]) UnmarshalJSON

func (ll *LinkedList[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, ll)

func (*LinkedList[T]) Values

func (ll *LinkedList[T]) Values() []T

Values returns a slice contains all the items of the list ll

type LinkedMapNode

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

LinkedMapNode is a node of a linked hash map.

func (*LinkedMapNode[K, V]) Key

func (ln *LinkedMapNode[K, V]) Key() K

Key returns the key

func (*LinkedMapNode[K, V]) SetValue

func (ln *LinkedMapNode[K, V]) SetValue(v V)

SetValue sets the value

func (*LinkedMapNode[K, V]) String

func (ln *LinkedMapNode[K, V]) String() string

String print the list item to string

func (*LinkedMapNode[K, V]) Value

func (ln *LinkedMapNode[K, V]) Value() V

Value returns the key

type List

type List[T any] interface {
	Collection[T]

	ReverseEachable[T]

	Iterable[T]

	// Get returns the value at the specified index in this list. If the index is
	// invalid, the call will panic. This method accepts both positive and
	// negative index values. Index 0 refers to the first element, and
	// index -1 refers to the last.
	Get(index int) T

	// Set set the v at the specified index in this list and returns the old value.
	Set(index int, v T) T

	// Insert insert item v at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
	// Does not do anything if position is bigger than list's size
	// Note: position equal to list's size is valid, i.e. append.
	Insert(index int, v T)

	// Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
	// Does not do anything if position is bigger than list's size
	// Note: position equal to list's size is valid, i.e. append.
	Inserts(index int, vs ...T)

	// InsertCol inserts values of another collection ac at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
	// Does not do anything if position is bigger than list's size
	// Note: position equal to list's size is valid, i.e. append.
	InsertCol(index int, ac Collection[T])

	// Index returns the index of the first occurrence of the specified v in this list, or -1 if this list does not contain v.
	Index(v T) int

	// IndexFunc returns the index of the first true returned by function f in this list, or -1 if this list does not contain v.
	IndexFunc(f func(T) bool) int

	// DeleteAt remove the item at the specified position in this list
	DeleteAt(index int)

	// Swap swaps values of two items at the given index.
	Swap(i, j int)
}

List a doubly linked list interface

type Map

type Map[K any, V any] interface {
	Container

	// Get looks for the given key, and returns the value associated with it,
	// or nil if not found. The boolean it returns says whether the key is ok in the map.
	Get(key K) (V, bool)

	// MustGet looks for the given key, and returns the value associated with it.
	// If not found, return defaults[0] or panic if defaults is not supplied.
	MustGet(key K, defaults ...V) V

	// Set sets the paired key-value items, and returns what `Get` would have returned
	// on that key prior to the call to `Set`.
	// Example: lm.Set("k1", "v1", "k2", "v2")
	Set(key K, value V) (ov V, ok bool)

	// SetIfAbsent sets the key-value item if the key does not exists in the map,
	// and returns what `Get` would have returned
	// on that key prior to the call to `Set`.
	SetIfAbsent(key K, value V) (ov V, ok bool)

	// SetEntries set items from key-value items array, override the existing items
	SetEntries(pairs ...P[K, V])

	// Copy copy items from another map am, override the existing items
	Copy(am Map[K, V])

	// Remove remove the item with key k,
	// and returns what `Get` would have returned
	// on that key prior to the call to `Set`.
	Remove(k K) (ov V, ok bool)

	// Removes remove all items with key of ks.
	Removes(ks ...K)

	// Contain looks for the given key, and returns true if the key exists in the map.
	Contain(k K) bool

	// Contains looks for the given key, and returns true if the key exists in the map.
	Contains(ks ...K) bool

	// Keys returns the key slice
	Keys() []K

	// Values returns a slice contains all the items of the collection
	Values() []V

	// Entries returns the key-value pair slice
	Entries() []P[K, V]

	Eachable2[K, V]
}

Map map interface

type P

type P[K any, V any] struct {
	Key   K
	Value V
}

P key/value pair

func KV added in v1.0.13

func KV[K any, V any](k K, v V) P[K, V]

KV create key/value pair

func (*P[K, V]) String added in v1.0.12

func (p *P[K, V]) String() string

type Queue

type Queue[T any] interface {
	// Peek Retrieves, but does not remove, the head of this queue, or returns (nil, false) if this queue is empty.
	Peek() (T, bool)

	// Poll Retrieves and removes the head of this queue, or returns (nil, false) if this queue is empty.
	Poll() (T, bool)

	// Push add item v to the tail of queue
	Push(v T)

	// Pushs adds items of vs to the tail of queue
	Pushs(vs ...T)
}

Queue A queue interface

type ReverseEachable

type ReverseEachable[T any] interface {
	// ReverseEach call f for each item in the collection with reverse order
	ReverseEach(f func(value T))
}

ReverseEachable a value each interface for collection

type ReverseEachable2

type ReverseEachable2[K any, V any] interface {
	// ReverseEach call f for each key/value in the collection with reverse order
	ReverseEach(f func(key K, value V))
}

ReverseEachable2 a key/value reverse each interface for collection

type RingBuffer

type RingBuffer[T any] struct {
	// contains filtered or unexported fields
}

RingBuffer A fast Golang queue using a ring-buffer, based on the version suggested by Dariusz Górecki. Using this instead of other, simpler, queue implementations (slice+append or linked list) provides substantial memory and time benefits, and fewer GC pauses. The queue implemented here is as fast as it is in part because it is not thread-safe.

func NewRingBuffer

func NewRingBuffer[T any](vs ...T) *RingBuffer[T]

NewRingBuffer constructs and returns a new RingBuffer. Example: cog.NewRingBuffer(1, 2, 3)

func (*RingBuffer[T]) Add

func (rb *RingBuffer[T]) Add(v T)

Add add item v.

func (*RingBuffer[T]) AddCol added in v1.0.10

func (rb *RingBuffer[T]) AddCol(ac Collection[T])

AddCol adds all items of another collection

func (*RingBuffer[T]) Adds added in v1.0.10

func (rb *RingBuffer[T]) Adds(vs ...T)

Adds adds all items of vs.

func (*RingBuffer[T]) Cap

func (rb *RingBuffer[T]) Cap() int

Cap returns the capcity of the buffer.

func (*RingBuffer[T]) Clear

func (rb *RingBuffer[T]) Clear()

Clear clears list al.

func (*RingBuffer[T]) Contain added in v1.0.10

func (rb *RingBuffer[T]) Contain(v T) bool

Contain Test to see if the list contains the value v

func (*RingBuffer[T]) ContainCol added in v1.0.10

func (rb *RingBuffer[T]) ContainCol(ac Collection[T]) bool

ContainCol Test to see if the collection contains all items of another collection

func (*RingBuffer[T]) ContainIter added in v1.0.12

func (rb *RingBuffer[T]) ContainIter(it Iterator[T]) bool

ContainIter Test to see if the collection contains all items of iterator 'it'

func (*RingBuffer[T]) Contains

func (rb *RingBuffer[T]) Contains(vs ...T) bool

Contains Test to see if the RingBuffer contains the value v

func (*RingBuffer[T]) DeleteAt added in v1.0.12

func (rb *RingBuffer[T]) DeleteAt(index int)

DeleteAt remove the item at the specified position in this RingBuffer.

func (*RingBuffer[T]) Each

func (rb *RingBuffer[T]) Each(f func(T))

Each call f for each item in the RingBuffer

func (*RingBuffer[T]) Get

func (rb *RingBuffer[T]) Get(index int) T

Get returns the item at the specified position in this RingBuffer if i < -rb.Len() or i >= rb.Len(), panic if i < 0, returns rb.Get(rb.Len() + i)

func (*RingBuffer[T]) Head

func (rb *RingBuffer[T]) Head() (v T)

Head get the first item of RingBuffer.

func (*RingBuffer[T]) Index

func (rb *RingBuffer[T]) Index(v T) int

Index returns the index of the first occurrence of the specified v in this RingBuffer, or -1 if this RingBuffer does not contain v.

func (*RingBuffer[T]) IndexFunc added in v1.0.12

func (rb *RingBuffer[T]) IndexFunc(f func(T) bool) int

IndexFunc returns the index of the first true returned by function f in this list, or -1 if this list does not contain v.

func (*RingBuffer[T]) Insert

func (rb *RingBuffer[T]) Insert(index int, v T)

Insert insert value v at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than RingBuffer's size Note: position equal to RingBuffer's size is valid, i.e. append.

func (*RingBuffer[T]) InsertCol added in v1.0.10

func (rb *RingBuffer[T]) InsertCol(index int, ac Collection[T])

InsertCol inserts values of another collection ac at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than RingBuffer's size Note: position equal to RingBuffer's size is valid, i.e. append.

func (*RingBuffer[T]) Inserts added in v1.0.10

func (rb *RingBuffer[T]) Inserts(index int, vs ...T)

Inserts inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Panic if position is bigger than RingBuffer's size Note: position equal to RingBuffer's size is valid, i.e. append.

func (*RingBuffer[T]) IsEmpty

func (rb *RingBuffer[T]) IsEmpty() bool

IsEmpty returns true if the container length == 0

func (*RingBuffer[T]) Iterator

func (rb *RingBuffer[T]) Iterator() Iterator[T]

Iterator returns a iterator for the RingBuffer

func (*RingBuffer[T]) Len

func (rb *RingBuffer[T]) Len() int

Len returns the number of elements currently stored in the buffer.

func (*RingBuffer[T]) MarshalJSON

func (rb *RingBuffer[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(rb)

func (*RingBuffer[T]) MustPeek

func (rb *RingBuffer[T]) MustPeek() T

MustPeek Retrieves, but does not remove, the head of this queue, panic if this queue is empty.

func (*RingBuffer[T]) MustPoll

func (rb *RingBuffer[T]) MustPoll() T

MustPoll Retrieves and removes the head of this queue, panic if this queue is empty.

func (*RingBuffer[T]) Peek

func (rb *RingBuffer[T]) Peek() (v T, ok bool)

Peek get the first item of RingBuffer.

func (*RingBuffer[T]) PeekHead

func (rb *RingBuffer[T]) PeekHead() (v T, ok bool)

PeekHead get the first item of RingBuffer.

func (*RingBuffer[T]) PeekTail

func (rb *RingBuffer[T]) PeekTail() (v T, ok bool)

PeekTail get the last item of RingBuffer.

func (*RingBuffer[T]) Poll

func (rb *RingBuffer[T]) Poll() (T, bool)

Poll get and remove the first item of RingBuffer.

func (*RingBuffer[T]) PollHead

func (rb *RingBuffer[T]) PollHead() (v T, ok bool)

PollHead get and remove the first item of RingBuffer.

func (*RingBuffer[T]) PollTail

func (rb *RingBuffer[T]) PollTail() (v T, ok bool)

PollTail get and remove the last item of RingBuffer.

func (*RingBuffer[T]) Push

func (rb *RingBuffer[T]) Push(v T)

Push insert item v at the tail of RingBuffer rb.

func (*RingBuffer[T]) PushHead

func (rb *RingBuffer[T]) PushHead(v T)

PushHead insert item v at the head of RingBuffer rb.

func (*RingBuffer[T]) PushHeadCol added in v1.0.10

func (rb *RingBuffer[T]) PushHeadCol(ac Collection[T])

PushHeadCol inserts a copy of another collection at the head of RingBuffer rb. The rb and ac may be the same. They must not be nil.

func (*RingBuffer[T]) PushHeads added in v1.0.10

func (rb *RingBuffer[T]) PushHeads(vs ...T)

PushHeads inserts all items of vs at the head of RingBuffer rb.

func (*RingBuffer[T]) PushTail

func (rb *RingBuffer[T]) PushTail(v T)

PushTail insert item v at the tail of RingBuffer rb.

func (*RingBuffer[T]) PushTailCol added in v1.0.10

func (rb *RingBuffer[T]) PushTailCol(ac Collection[T])

PushTailCol inserts a copy of another collection at the tail of RingBuffer rb. The rb and ac may be the same. They must not be nil.

func (*RingBuffer[T]) PushTails added in v1.0.10

func (rb *RingBuffer[T]) PushTails(vs ...T)

PushTails inserts all items of vs at the tail of RingBuffer rb.

func (*RingBuffer[T]) Pushs added in v1.0.10

func (rb *RingBuffer[T]) Pushs(vs ...T)

Pushs inserts all items of vs at the tail of RingBuffer rb.

func (*RingBuffer[T]) Remove

func (rb *RingBuffer[T]) Remove(v T)

Remove remove all items with associated value v of vs

func (*RingBuffer[T]) RemoveCol added in v1.0.10

func (rb *RingBuffer[T]) RemoveCol(ac Collection[T])

RemoveCol remove all of this collection's elements that are also contained in the specified collection

func (*RingBuffer[T]) RemoveFunc added in v1.0.12

func (rb *RingBuffer[T]) RemoveFunc(f func(T) bool)

RemoveFunc remove all items that function f returns true

func (*RingBuffer[T]) RemoveIter added in v1.0.12

func (rb *RingBuffer[T]) RemoveIter(it Iterator[T])

RemoveIter remove all items in the iterator it

func (*RingBuffer[T]) Removes added in v1.0.10

func (rb *RingBuffer[T]) Removes(vs ...T)

Removes remove all items in the array vs

func (*RingBuffer[T]) RetainCol added in v1.0.10

func (rb *RingBuffer[T]) RetainCol(ac Collection[T])

RetainCol Retains only the elements in this collection that are contained in the specified collection.

func (*RingBuffer[T]) RetainFunc added in v1.0.12

func (rb *RingBuffer[T]) RetainFunc(f func(T) bool)

RetainFunc Retains all items that function f returns true

func (*RingBuffer[T]) Retains added in v1.0.10

func (rb *RingBuffer[T]) Retains(vs ...T)

Retains Retains only the elements in this collection that are contained in the argument array vs.

func (*RingBuffer[T]) ReverseEach

func (rb *RingBuffer[T]) ReverseEach(f func(T))

ReverseEach call f for each item in the RingBuffer with reverse order

func (*RingBuffer[T]) Set

func (rb *RingBuffer[T]) Set(index int, v T) (ov T)

Set set the v at the specified index in this RingBuffer and returns the old value.

func (*RingBuffer[T]) Sort

func (rb *RingBuffer[T]) Sort(less Less[T])

Sort Sorts this RingBuffer according to the order induced by the specified Comparator.

func (*RingBuffer[T]) String

func (rb *RingBuffer[T]) String() string

String print RingBuffer to string

func (*RingBuffer[T]) Swap

func (rb *RingBuffer[T]) Swap(i, j int)

Swap swaps values of two items at the given index.

func (*RingBuffer[T]) Tail

func (rb *RingBuffer[T]) Tail() (v T)

Tail get the last item of RingBuffer.

func (*RingBuffer[T]) UnmarshalJSON

func (rb *RingBuffer[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, rb)

func (*RingBuffer[T]) Values

func (rb *RingBuffer[T]) Values() []T

Values returns a slice contains all the items of the RingBuffer rb

type Set

type Set[T any] Collection[T]

Set a set interface

type Sortable

type Sortable[T any] interface {
	// Sorts this container according to the order induced by the specified Comparator.
	Sort(less Less[T])
}

Sortable a value each interface for collection

type TreeMap

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

TreeMap implements an tree map that keeps the compare order of keys. The zero value for TreeMap is an empty map ready to use.

https://en.wikipedia.org/wiki/Red%E2%80%93black_tree

To iterate over a tree map (where tm is a *TreeMap):

for it := tm.Iterator(); it.Next(); {
	// do something with it.Key(), it.Value()
}
Example
m := NewTreeMap[int, string](CompareInt)
m.Set(1, "x")   // 1->x
m.Set(2, "b")   // 1->x, 2->b (in order)
m.Set(1, "a")   // 1->a, 2->b (in order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.Values()  // []interface {}{"a", "b"} (in order)
_ = m.Keys()    // []interface {}{1, 2} (in order)
m.Remove(1)     // 2->b
m.Clear()       // empty
m.IsEmpty()     // true
m.Len()         // 0

// Other:
m.Head() // Returns the minimum key and its value from map.
m.Tail() // Returns the maximum key and its value from map.
Output:

func NewTreeMap

func NewTreeMap[K any, V any](compare Compare[K], kvs ...P[K, V]) *TreeMap[K, V]

NewTreeMap creates a new TreeMap. Example: cog.NewTreeMap(cog.CompareString, cog.KV("k1", "v1"), cog.KV("k2", "v2"))

func (*TreeMap[K, V]) Ceiling

func (tm *TreeMap[K, V]) Ceiling(key K) *TreeMapNode[K, V]

Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling is found.

Ceiling node is defined as the smallest node that is larger than or equal to the given node. A ceiling node may not be found, either because the tree is empty, or because all nodes in the tree are smaller than the given node.

key should adhere to the comparator's type assertion, otherwise method panics.

func (*TreeMap[K, V]) Clear

func (tm *TreeMap[K, V]) Clear()

Clear clears the map

func (*TreeMap[K, V]) Contain added in v1.0.10

func (tm *TreeMap[K, V]) Contain(k K) bool

Contain Test to see if the list contains the key k

func (*TreeMap[K, V]) Contains

func (tm *TreeMap[K, V]) Contains(ks ...K) bool

Contains looks for the given key, and returns true if the key exists in the map.

func (*TreeMap[K, V]) Copy added in v1.0.10

func (tm *TreeMap[K, V]) Copy(am Map[K, V])

Copy copy items from another map am, override the existing items

func (*TreeMap[K, V]) Each

func (tm *TreeMap[K, V]) Each(f func(k K, v V))

Each call f for each item in the map

func (*TreeMap[K, V]) Entries added in v1.0.12

func (tm *TreeMap[K, V]) Entries() []P[K, V]

Entries returns the key-value pair slice

func (*TreeMap[K, V]) Floor

func (tm *TreeMap[K, V]) Floor(key K) *TreeMapNode[K, V]

Floor Finds floor node of the input key, return the floor node or nil if no floor is found.

Floor node is defined as the largest node that is smaller than or equal to the given node. A floor node may not be found, either because the tree is empty, or because all nodes in the tree are larger than the given node.

key should adhere to the comparator's type assertion, otherwise method panics.

func (*TreeMap[K, V]) Get

func (tm *TreeMap[K, V]) Get(key K) (V, bool)

Get looks for the given key, and returns the value associated with it, or nil if not found. The boolean it returns says whether the key is ok in the map.

func (*TreeMap[K, V]) Graph

func (tm *TreeMap[K, V]) Graph(value bool) string

Graph return the map's graph

func (*TreeMap[K, V]) Head

func (tm *TreeMap[K, V]) Head() *TreeMapNode[K, V]

Head returns a pointer to the minimum item.

func (*TreeMap[K, V]) IsEmpty

func (tm *TreeMap[K, V]) IsEmpty() bool

IsEmpty returns true if the map has no items

func (*TreeMap[K, V]) Items

func (tm *TreeMap[K, V]) Items() []*TreeMapNode[K, V]

Items returns the map item slice

func (*TreeMap[K, V]) Iterator

func (tm *TreeMap[K, V]) Iterator() Iterator2[K, V]

Iterator returns a iterator for the map

func (*TreeMap[K, V]) Keys

func (tm *TreeMap[K, V]) Keys() []K

Keys returns the key slice

func (*TreeMap[K, V]) Len

func (tm *TreeMap[K, V]) Len() int

Len returns the length of the tree map.

func (*TreeMap[K, V]) MarshalJSON

func (tm *TreeMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(tm)

func (*TreeMap[K, V]) MustGet added in v1.0.12

func (tm *TreeMap[K, V]) MustGet(key K, defaults ...V) V

MustGet looks for the given key, and returns the value associated with it. If not found, return defaults[0] or panic if defaults is not supplied.

func (*TreeMap[K, V]) PollHead

func (tm *TreeMap[K, V]) PollHead() *TreeMapNode[K, V]

PollHead remove the first item of map.

func (*TreeMap[K, V]) PollTail

func (tm *TreeMap[K, V]) PollTail() *TreeMapNode[K, V]

PollTail remove the last item of map.

func (*TreeMap[K, V]) Remove added in v1.0.10

func (tm *TreeMap[K, V]) Remove(k K) (ov V, ok bool)

Remove remove the item with key k, and returns what `Get` would have returned on that key prior to the call to `Set`.

func (*TreeMap[K, V]) Removes added in v1.0.10

func (tm *TreeMap[K, V]) Removes(ks ...K)

Removes remove all items with key of ks.

func (*TreeMap[K, V]) ReverseEach

func (tm *TreeMap[K, V]) ReverseEach(f func(k K, v V))

ReverseEach call f for each item in the map with reverse order

func (*TreeMap[K, V]) Set

func (tm *TreeMap[K, V]) Set(key K, value V) (ov V, ok bool)

Set sets the paired key-value item, and returns what `Get` would have returned on that key prior to the call to `Set`. key should adhere to the comparator's type assertion, otherwise method panics.

func (*TreeMap[K, V]) SetEntries added in v1.0.12

func (tm *TreeMap[K, V]) SetEntries(pairs ...P[K, V])

SetEntries set items from key-value items array, override the existing items

func (*TreeMap[K, V]) SetIfAbsent

func (tm *TreeMap[K, V]) SetIfAbsent(key K, value V) (ov V, ok bool)

SetIfAbsent sets the key-value item if the key does not exists in the map, and returns true if the tree is changed.

func (*TreeMap[K, V]) String

func (tm *TreeMap[K, V]) String() string

String print map to string

func (*TreeMap[K, V]) Tail

func (tm *TreeMap[K, V]) Tail() *TreeMapNode[K, V]

Tail returns a pointer to the maximum item.

func (*TreeMap[K, V]) UnmarshalJSON

func (tm *TreeMap[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, tm)

func (*TreeMap[K, V]) Values

func (tm *TreeMap[K, V]) Values() []V

Values returns the value slice

type TreeMapNode

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

TreeMapNode is a node of red-black tree

func (*TreeMapNode[K, V]) Key

func (tn *TreeMapNode[K, V]) Key() K

Key returns the key

func (*TreeMapNode[K, V]) SetValue

func (tn *TreeMapNode[K, V]) SetValue(v V)

SetValue sets the value

func (*TreeMapNode[K, V]) String

func (tn *TreeMapNode[K, V]) String() string

String print the key/value node to string

func (*TreeMapNode[K, V]) Value

func (tn *TreeMapNode[K, V]) Value() V

Value returns the key

type TreeSet

type TreeSet[T any] struct {
	// contains filtered or unexported fields
}

TreeSet implements an tree set that keeps the compare order of keys. The zero value for TreeSet is an empty set ready to use.

https://en.wikipedia.org/wiki/Red%E2%80%93black_tree

To iterate over a tree set (where ts is a *TreeSet):

for it := ts.Iterator(); it.Next(); {
	// do something with it.Value()
}

func NewTreeSet

func NewTreeSet[T any](compare Compare[T], vs ...T) *TreeSet[T]

NewTreeSet creates a new TreeSet. Example: cog.NewTreeSet(cog.CompareString, "v1", "v2")

Example
set := NewTreeSet(CompareInt)
set.Add(1)              // 1
set.Adds(2, 2, 3, 4, 5) // 1, 2, 3, 4, 5 (in order, duplicates ignored)
set.Remove(4)           // 1, 2, 3, 5 (in order)
set.Removes(2, 3)       // 1, 5 (in order)
set.Contain(1)          // true
set.Contains(1, 5)      // true
set.Contains(1, 6)      // false
_ = set.Values()        // []int{1,5} (in order)
set.Clear()             // empty
set.IsEmpty()           // true
set.Len()               // 0
Output:

func (*TreeSet[T]) Add

func (ts *TreeSet[T]) Add(v T)

Add add item v.

func (*TreeSet[T]) AddCol added in v1.0.10

func (ts *TreeSet[T]) AddCol(ac Collection[T])

AddCol adds all items of another collection

func (*TreeSet[T]) Adds added in v1.0.10

func (ts *TreeSet[T]) Adds(vs ...T)

Adds adds all items of vs.

func (*TreeSet[T]) Ceiling

func (ts *TreeSet[T]) Ceiling(v T) *T

Ceiling finds ceiling node of the input key, return the ceiling node's value or nil if no ceiling is found.

Ceiling node is defined as the smallest node that is larger than or equal to the given node. A ceiling node may not be found, either because the tree is empty, or because all nodes in the tree are smaller than the given node.

key should adhere to the comparator's type assertion, otherwise method panics.

func (*TreeSet[T]) Clear

func (ts *TreeSet[T]) Clear()

Clear clears the set

func (*TreeSet[T]) Contain added in v1.0.10

func (ts *TreeSet[T]) Contain(v T) bool

Contain Test to see if the list contains the value v

func (*TreeSet[T]) ContainCol added in v1.0.10

func (ts *TreeSet[T]) ContainCol(ac Collection[T]) bool

ContainCol Test to see if the collection contains all items of another collection

func (*TreeSet[T]) ContainIter added in v1.0.12

func (ts *TreeSet[T]) ContainIter(it Iterator[T]) bool

ContainIter Test to see if the collection contains all items of iterator 'it'

func (*TreeSet[T]) Contains

func (ts *TreeSet[T]) Contains(vs ...T) bool

Contains Test to see if the collection contains all items of vs

func (*TreeSet[T]) Each

func (ts *TreeSet[T]) Each(f func(v T))

Each call f for each item in the set

func (*TreeSet[T]) Floor

func (ts *TreeSet[T]) Floor(v T) *T

Floor Finds floor node of the input key, return the floor node's value or nil if no floor is found.

Floor node is defined as the largest node that is smaller than or equal to the given node. A floor node may not be found, either because the tree is empty, or because all nodes in the tree are larger than the given node.

key should adhere to the comparator's type assertion, otherwise method panics.

func (*TreeSet[T]) Graph

func (ts *TreeSet[T]) Graph() string

Graph return the set's graph

func (*TreeSet[T]) Head

func (ts *TreeSet[T]) Head() (v T)

Head returns the first item of set ts or nil if the set is empty.

func (*TreeSet[T]) IsEmpty

func (ts *TreeSet[T]) IsEmpty() bool

IsEmpty returns true if the set has no items

func (*TreeSet[T]) Iterator

func (ts *TreeSet[T]) Iterator() Iterator[T]

Iterator returns a iterator for the set

func (*TreeSet[T]) Len

func (ts *TreeSet[T]) Len() int

Len returns the length of the tree set.

func (*TreeSet[T]) MarshalJSON

func (ts *TreeSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(ts)

func (*TreeSet[T]) PeekHead

func (ts *TreeSet[T]) PeekHead() (v T, ok bool)

PeekHead get the first item of set.

func (*TreeSet[T]) PeekTail

func (ts *TreeSet[T]) PeekTail() (v T, ok bool)

PeekTail get the last item of set.

func (*TreeSet[T]) PollHead

func (ts *TreeSet[T]) PollHead() (v T, ok bool)

PollHead remove the first item of set.

func (*TreeSet[T]) PollTail

func (ts *TreeSet[T]) PollTail() (v T, ok bool)

PollTail remove the last item of set.

func (*TreeSet[T]) Remove added in v1.0.10

func (ts *TreeSet[T]) Remove(v T)

Remove remove all items with associated value v of vs

func (*TreeSet[T]) RemoveCol added in v1.0.10

func (ts *TreeSet[T]) RemoveCol(ac Collection[T])

RemoveCol remove all of this collection's elements that are also contained in the specified collection

func (*TreeSet[T]) RemoveFunc added in v1.0.12

func (ts *TreeSet[T]) RemoveFunc(f func(T) bool)

RemoveFunc remove all items that function f returns true

func (*TreeSet[T]) RemoveIter added in v1.0.12

func (ts *TreeSet[T]) RemoveIter(it Iterator[T])

RemoveIter remove all items in the iterator it

func (*TreeSet[T]) Removes added in v1.0.10

func (ts *TreeSet[T]) Removes(vs ...T)

Removes remove all items in the array vs

func (*TreeSet[T]) RetainCol added in v1.0.10

func (ts *TreeSet[T]) RetainCol(ac Collection[T])

RetainCol Retains only the elements in this collection that are contained in the specified collection.

func (*TreeSet[T]) RetainFunc added in v1.0.12

func (ts *TreeSet[T]) RetainFunc(f func(T) bool)

RetainFunc Retains all items that function f returns true

func (*TreeSet[T]) Retains added in v1.0.10

func (ts *TreeSet[T]) Retains(vs ...T)

Retains Retains only the elements in this collection that are contained in the argument array vs.

func (*TreeSet[T]) ReverseEach

func (ts *TreeSet[T]) ReverseEach(f func(v T))

ReverseEach call f for each item in the set with reverse order

func (*TreeSet[T]) String

func (ts *TreeSet[T]) String() string

String print set to string

func (*TreeSet[T]) Tail

func (ts *TreeSet[T]) Tail() (v T)

Tail returns the last item of set ts or nil if the set is empty.

func (*TreeSet[T]) UnmarshalJSON

func (ts *TreeSet[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, ts)

func (*TreeSet[T]) Values

func (ts *TreeSet[T]) Values() []T

Values returns the value slice

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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