Documentation ¶
Index ¶
- func CompareByte(a, b byte) int
- func CompareFloat32(a, b float32) int
- func CompareFloat64(a, b float64) int
- func CompareInt(a, b int) int
- func CompareInt16(a, b int16) int
- func CompareInt32(a, b int32) int
- func CompareInt64(a, b int64) int
- func CompareInt8(a, b int8) int
- func CompareRune(a, b rune) int
- func CompareString(a, b string) int
- func CompareUInt(a, b uint) int
- func CompareUInt16(a, b uint16) int
- func CompareUInt32(a, b uint32) int
- func CompareUInt64(a, b uint64) int
- func CompareUInt8(a, b uint8) int
- func LessByte(a, b byte) bool
- func LessFloat32(a, b float32) bool
- func LessFloat64(a, b float64) bool
- func LessInt(a, b int) bool
- func LessInt16(a, b int16) bool
- func LessInt32(a, b int32) bool
- func LessInt64(a, b int64) bool
- func LessInt8(a, b int8) bool
- func LessRune(a, b rune) bool
- func LessString(a, b string) bool
- func LessUint(a, b uint) bool
- func LessUint16(a, b uint16) bool
- func LessUint32(a, b uint32) bool
- func LessUint64(a, b uint64) bool
- func LessUint8(a, b uint8) bool
- type ArrayList
- func (al *ArrayList[T]) Add(vs ...T)
- func (al *ArrayList[T]) AddAll(ac Collection[T])
- func (al *ArrayList[T]) Cap() int
- func (al *ArrayList[T]) Clear()
- func (al *ArrayList[T]) Contains(vs ...T) bool
- func (al *ArrayList[T]) ContainsAll(ac Collection[T]) bool
- func (al *ArrayList[T]) Delete(vs ...T)
- func (al *ArrayList[T]) DeleteAll(ac Collection[T])
- func (al *ArrayList[T]) DeleteIf(f func(T) bool)
- func (al *ArrayList[T]) Each(f func(T))
- func (al *ArrayList[T]) Get(index int) T
- func (al *ArrayList[T]) Head() (v T)
- func (al *ArrayList[T]) Index(v T) int
- func (al *ArrayList[T]) IndexIf(f func(T) bool) int
- func (al *ArrayList[T]) Insert(index int, vs ...T)
- func (al *ArrayList[T]) InsertAll(index int, ac Collection[T])
- func (al *ArrayList[T]) IsEmpty() bool
- func (al *ArrayList[T]) Iterator() Iterator[T]
- func (al *ArrayList[T]) Len() int
- func (al *ArrayList[T]) MarshalJSON() ([]byte, error)
- func (al *ArrayList[T]) Peek() (v T, ok bool)
- func (al *ArrayList[T]) PeekHead() (v T, ok bool)
- func (al *ArrayList[T]) PeekTail() (v T, ok bool)
- func (al *ArrayList[T]) Poll() (T, bool)
- func (al *ArrayList[T]) PollHead() (v T, ok bool)
- func (al *ArrayList[T]) PollTail() (v T, ok bool)
- func (al *ArrayList[T]) Push(vs ...T)
- func (al *ArrayList[T]) PushHead(vs ...T)
- func (al *ArrayList[T]) PushHeadAll(ac Collection[T])
- func (al *ArrayList[T]) PushTail(vs ...T)
- func (al *ArrayList[T]) PushTailAll(ac Collection[T])
- func (al *ArrayList[T]) Remove(index int)
- func (al *ArrayList[T]) Reserve(n int)
- func (al *ArrayList[T]) Retain(vs ...T)
- func (al *ArrayList[T]) RetainAll(ac Collection[T])
- func (al *ArrayList[T]) ReverseEach(f func(T))
- func (al *ArrayList[T]) Set(index int, v T) (ov T)
- func (al *ArrayList[T]) Sort(less Less[T])
- func (al *ArrayList[T]) String() string
- func (al *ArrayList[T]) Swap(i, j int)
- func (al *ArrayList[T]) Tail() (v T)
- func (al *ArrayList[T]) UnmarshalJSON(data []byte) error
- func (al *ArrayList[T]) Values() []T
- type Collection
- type Compare
- type Container
- type Deque
- type Eachable
- type Eachable2
- type HashMap
- func (hm *HashMap[K, V]) Clear()
- func (hm *HashMap[K, V]) Contains(ks ...K) bool
- func (hm *HashMap[K, V]) Delete(ks ...K) (ov V, ok bool)
- func (hm *HashMap[K, V]) Each(f func(k K, v V))
- func (hm *HashMap[K, V]) Get(key K) (v V, ok bool)
- func (hm *HashMap[K, V]) HashMap() map[K]V
- func (hm *HashMap[K, V]) IsEmpty() bool
- func (hm *HashMap[K, V]) Keys() []K
- func (hm *HashMap[K, V]) Len() int
- func (hm *HashMap[K, V]) MarshalJSON() ([]byte, error)
- func (hm *HashMap[K, V]) Set(key K, value V) (ov V, ok bool)
- func (hm *HashMap[K, V]) SetAll(am Map[K, V])
- func (hm *HashMap[K, V]) SetIfAbsent(key K, value V) (ov V, ok bool)
- func (hm *HashMap[K, V]) SetPairs(pairs ...P[K, V])
- func (hm *HashMap[K, V]) String() string
- func (hm *HashMap[K, V]) UnmarshalJSON(data []byte) error
- func (hm *HashMap[K, V]) Values() []V
- type HashSet
- func (hs *HashSet[T]) Add(vs ...T)
- func (hs *HashSet[T]) AddAll(ac Collection[T])
- func (hs *HashSet[T]) Clear()
- func (hs *HashSet[T]) Contains(vs ...T) bool
- func (hs *HashSet[T]) ContainsAll(ac Collection[T]) bool
- func (hs *HashSet[T]) Delete(vs ...T)
- func (hs *HashSet[T]) DeleteAll(ac Collection[T])
- func (hs *HashSet[T]) DeleteIf(f func(T) bool)
- func (hs *HashSet[T]) Difference(a *HashSet[T]) *HashSet[T]
- func (hs *HashSet[T]) Each(f func(T))
- func (hs *HashSet[T]) Intersection(a *HashSet[T]) *HashSet[T]
- func (hs *HashSet[T]) IsEmpty() bool
- func (hs *HashSet[T]) Len() int
- func (hs *HashSet[T]) MarshalJSON() ([]byte, error)
- func (hs *HashSet[T]) Retain(vs ...T)
- func (hs *HashSet[T]) RetainAll(ac Collection[T])
- func (hs *HashSet[T]) String() string
- func (hs *HashSet[T]) UnmarshalJSON(data []byte) error
- func (hs *HashSet[T]) Values() []T
- type Iterable
- type Iterable2
- type IterableMap
- type Iterator
- type Iterator2
- type Less
- type LinkedHashMap
- func (lm *LinkedHashMap[K, V]) Clear()
- func (lm *LinkedHashMap[K, V]) Contains(ks ...K) bool
- func (lm *LinkedHashMap[K, V]) Delete(ks ...K) (ov V, ok bool)
- func (lm *LinkedHashMap[K, V]) Each(f func(k K, v V))
- func (lm *LinkedHashMap[K, V]) Get(key K) (V, bool)
- func (lm *LinkedHashMap[K, V]) Head() *LinkedMapNode[K, V]
- func (lm *LinkedHashMap[K, V]) IsEmpty() bool
- func (lm *LinkedHashMap[K, V]) Items() []*LinkedMapNode[K, V]
- func (lm *LinkedHashMap[K, V]) Iterator() Iterator2[K, V]
- func (lm *LinkedHashMap[K, V]) IteratorOf(k K) Iterator2[K, V]
- func (lm *LinkedHashMap[K, V]) Keys() []K
- func (lm *LinkedHashMap[K, V]) Len() int
- func (lm *LinkedHashMap[K, V]) MarshalJSON() ([]byte, error)
- func (lm *LinkedHashMap[K, V]) PollHead() *LinkedMapNode[K, V]
- func (lm *LinkedHashMap[K, V]) PollTail() *LinkedMapNode[K, V]
- func (lm *LinkedHashMap[K, V]) ReverseEach(f func(k K, v V))
- func (lm *LinkedHashMap[K, V]) Set(key K, value V) (ov V, ok bool)
- func (lm *LinkedHashMap[K, V]) SetAll(am Map[K, V])
- func (lm *LinkedHashMap[K, V]) SetIfAbsent(key K, value V) (ov V, ok bool)
- func (lm *LinkedHashMap[K, V]) SetPairs(pairs ...P[K, V])
- func (lm *LinkedHashMap[K, V]) String() string
- func (lm *LinkedHashMap[K, V]) Tail() *LinkedMapNode[K, V]
- func (lm *LinkedHashMap[K, V]) UnmarshalJSON(data []byte) error
- func (lm *LinkedHashMap[K, V]) Values() []V
- type LinkedHashSet
- func (ls *LinkedHashSet[T]) Add(vs ...T)
- func (ls *LinkedHashSet[T]) AddAll(ac Collection[T])
- func (ls *LinkedHashSet[T]) Clear()
- func (ls *LinkedHashSet[T]) Contains(vs ...T) bool
- func (ls *LinkedHashSet[T]) ContainsAll(ac Collection[T]) bool
- func (ls *LinkedHashSet[T]) Delete(vs ...T)
- func (ls *LinkedHashSet[T]) DeleteAll(ac Collection[T])
- func (ls *LinkedHashSet[T]) DeleteIf(f func(T) bool)
- func (ls *LinkedHashSet[T]) Each(f func(T))
- func (ls *LinkedHashSet[T]) Get(index int) T
- func (ls *LinkedHashSet[T]) Head() (v T)
- func (ls *LinkedHashSet[T]) Index(v T) int
- func (ls *LinkedHashSet[T]) Insert(index int, vs ...T)
- func (ls *LinkedHashSet[T]) InsertAll(index int, ac Collection[T])
- func (ls *LinkedHashSet[T]) IsEmpty() bool
- func (ls *LinkedHashSet[T]) Iterator() Iterator[T]
- func (ls *LinkedHashSet[T]) Len() int
- func (ls *LinkedHashSet[T]) MarshalJSON() ([]byte, error)
- func (ls *LinkedHashSet[T]) Peek() (v T, ok bool)
- func (ls *LinkedHashSet[T]) PeekHead() (v T, ok bool)
- func (ls *LinkedHashSet[T]) PeekTail() (v T, ok bool)
- func (ls *LinkedHashSet[T]) Poll() (T, bool)
- func (ls *LinkedHashSet[T]) PollHead() (v T, ok bool)
- func (ls *LinkedHashSet[T]) PollTail() (v T, ok bool)
- func (ls *LinkedHashSet[T]) Push(vs ...T)
- func (ls *LinkedHashSet[T]) PushHead(vs ...T)
- func (ls *LinkedHashSet[T]) PushHeadAll(ac Collection[T])
- func (ls *LinkedHashSet[T]) PushTail(vs ...T)
- func (ls *LinkedHashSet[T]) PushTailAll(ac Collection[T])
- func (ls *LinkedHashSet[T]) Remove(index int)
- func (ls *LinkedHashSet[T]) Retain(vs ...T)
- func (ls *LinkedHashSet[T]) RetainAll(ac Collection[T])
- func (ls *LinkedHashSet[T]) ReverseEach(f func(T))
- func (ls *LinkedHashSet[T]) Set(index int, v T) (ov T)
- func (ls *LinkedHashSet[T]) Sort(less Less[T])
- func (ls *LinkedHashSet[T]) String() string
- func (ls *LinkedHashSet[T]) Swap(i, j int)
- func (ls *LinkedHashSet[T]) Tail() (v T)
- func (ls *LinkedHashSet[T]) UnmarshalJSON(data []byte) error
- func (ls *LinkedHashSet[T]) Values() []T
- type LinkedList
- func (ll *LinkedList[T]) Add(vs ...T)
- func (ll *LinkedList[T]) AddAll(ac Collection[T])
- func (ll *LinkedList[T]) Clear()
- func (ll *LinkedList[T]) Contains(vs ...T) bool
- func (ll *LinkedList[T]) ContainsAll(ac Collection[T]) bool
- func (ll *LinkedList[T]) Delete(vs ...T)
- func (ll *LinkedList[T]) DeleteAll(ac Collection[T])
- func (ll *LinkedList[T]) DeleteIf(f func(T) bool)
- func (ll *LinkedList[T]) Each(f func(T))
- func (ll *LinkedList[T]) Get(index int) T
- func (ll *LinkedList[T]) Head() (v T)
- func (ll *LinkedList[T]) Index(v T) int
- func (ll *LinkedList[T]) IndexIf(f func(T) bool) int
- func (ll *LinkedList[T]) Insert(index int, vs ...T)
- func (ll *LinkedList[T]) InsertAll(index int, ac Collection[T])
- func (ll *LinkedList[T]) IsEmpty() bool
- func (ll *LinkedList[T]) Iterator() Iterator[T]
- func (ll *LinkedList[T]) LastIndex(v T) int
- func (ll *LinkedList[T]) Len() int
- func (ll *LinkedList[T]) MarshalJSON() ([]byte, error)
- func (ll *LinkedList[T]) Peek() (v T, ok bool)
- func (ll *LinkedList[T]) PeekHead() (v T, ok bool)
- func (ll *LinkedList[T]) PeekTail() (v T, ok bool)
- func (ll *LinkedList[T]) Poll() (T, bool)
- func (ll *LinkedList[T]) PollHead() (v T, ok bool)
- func (ll *LinkedList[T]) PollTail() (v T, ok bool)
- func (ll *LinkedList[T]) Push(vs ...T)
- func (ll *LinkedList[T]) PushHead(vs ...T)
- func (ll *LinkedList[T]) PushHeadAll(ac Collection[T])
- func (ll *LinkedList[T]) PushTail(vs ...T)
- func (ll *LinkedList[T]) PushTailAll(ac Collection[T])
- func (ll *LinkedList[T]) Remove(index int)
- func (ll *LinkedList[T]) Retain(vs ...T)
- func (ll *LinkedList[T]) RetainAll(ac Collection[T])
- func (ll *LinkedList[T]) ReverseEach(f func(T))
- func (ll *LinkedList[T]) Set(index int, v T) (ov T)
- func (ll *LinkedList[T]) Sort(less Less[T])
- func (ll *LinkedList[T]) String() string
- func (ll *LinkedList[T]) Swap(i, j int)
- func (ll *LinkedList[T]) Tail() (v T)
- func (ll *LinkedList[T]) UnmarshalJSON(data []byte) error
- func (ll *LinkedList[T]) Values() []T
- type LinkedMapNode
- type List
- type Map
- type P
- type Queue
- type ReverseEachable
- type ReverseEachable2
- type RingBuffer
- func (rb *RingBuffer[T]) Add(vs ...T)
- func (rb *RingBuffer[T]) AddAll(ac Collection[T])
- func (rb *RingBuffer[T]) Cap() int
- func (rb *RingBuffer[T]) Clear()
- func (rb *RingBuffer[T]) Contains(vs ...T) bool
- func (rb *RingBuffer[T]) ContainsAll(ac Collection[T]) bool
- func (rb *RingBuffer[T]) Delete(vs ...T)
- func (rb *RingBuffer[T]) DeleteAll(ac Collection[T])
- func (rb *RingBuffer[T]) DeleteIf(f func(T) bool)
- func (rb *RingBuffer[T]) Each(f func(T))
- func (rb *RingBuffer[T]) Get(index int) T
- func (rb *RingBuffer[T]) Head() (v T)
- func (rb *RingBuffer[T]) Index(v T) int
- func (rb *RingBuffer[T]) IndexIf(f func(T) bool) int
- func (rb *RingBuffer[T]) Insert(index int, vs ...T)
- func (rb *RingBuffer[T]) InsertAll(index int, ac Collection[T])
- func (rb *RingBuffer[T]) IsEmpty() bool
- func (rb *RingBuffer[T]) Iterator() Iterator[T]
- func (rb *RingBuffer[T]) Len() int
- func (rb *RingBuffer[T]) MarshalJSON() ([]byte, error)
- func (rb *RingBuffer[T]) MustPeek() T
- func (rb *RingBuffer[T]) MustPoll() T
- func (rb *RingBuffer[T]) Peek() (v T, ok bool)
- func (rb *RingBuffer[T]) PeekHead() (v T, ok bool)
- func (rb *RingBuffer[T]) PeekTail() (v T, ok bool)
- func (rb *RingBuffer[T]) Poll() (T, bool)
- func (rb *RingBuffer[T]) PollHead() (v T, ok bool)
- func (rb *RingBuffer[T]) PollTail() (v T, ok bool)
- func (rb *RingBuffer[T]) Push(vs ...T)
- func (rb *RingBuffer[T]) PushHead(vs ...T)
- func (rb *RingBuffer[T]) PushHeadAll(ac Collection[T])
- func (rb *RingBuffer[T]) PushTail(vs ...T)
- func (rb *RingBuffer[T]) PushTailAll(ac Collection[T])
- func (rb *RingBuffer[T]) Remove(index int)
- func (rb *RingBuffer[T]) Retain(vs ...T)
- func (rb *RingBuffer[T]) RetainAll(ac Collection[T])
- func (rb *RingBuffer[T]) ReverseEach(f func(T))
- func (rb *RingBuffer[T]) Set(index int, v T) (ov T)
- func (rb *RingBuffer[T]) Sort(less Less[T])
- func (rb *RingBuffer[T]) String() string
- func (rb *RingBuffer[T]) Swap(i, j int)
- func (rb *RingBuffer[T]) Tail() (v T)
- func (rb *RingBuffer[T]) UnmarshalJSON(data []byte) error
- func (rb *RingBuffer[T]) Values() []T
- type S
- type Set
- type Sortable
- type TreeMap
- func (tm *TreeMap[K, V]) Ceiling(key K) *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) Clear()
- func (tm *TreeMap[K, V]) Contains(ks ...K) bool
- func (tm *TreeMap[K, V]) Delete(ks ...K) (ov V, ok bool)
- func (tm *TreeMap[K, V]) Each(f func(k K, v V))
- func (tm *TreeMap[K, V]) Floor(key K) *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) Get(key K) (V, bool)
- func (tm *TreeMap[K, V]) Graph(value bool) string
- func (tm *TreeMap[K, V]) Head() *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) IsEmpty() bool
- func (tm *TreeMap[K, V]) Items() []*TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) Iterator() Iterator2[K, V]
- func (tm *TreeMap[K, V]) Keys() []K
- func (tm *TreeMap[K, V]) Len() int
- func (tm *TreeMap[K, V]) MarshalJSON() ([]byte, error)
- func (tm *TreeMap[K, V]) PollHead() *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) PollTail() *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) ReverseEach(f func(k K, v V))
- func (tm *TreeMap[K, V]) Set(key K, value V) (ov V, ok bool)
- func (tm *TreeMap[K, V]) SetAll(am Map[K, V])
- func (tm *TreeMap[K, V]) SetIfAbsent(key K, value V) (ov V, ok bool)
- func (tm *TreeMap[K, V]) SetPairs(pairs ...P[K, V])
- func (tm *TreeMap[K, V]) String() string
- func (tm *TreeMap[K, V]) Tail() *TreeMapNode[K, V]
- func (tm *TreeMap[K, V]) UnmarshalJSON(data []byte) error
- func (tm *TreeMap[K, V]) Values() []V
- type TreeMapNode
- type TreeSet
- func (ts *TreeSet[T]) Add(vs ...T)
- func (ts *TreeSet[T]) AddAll(ac Collection[T])
- func (ts *TreeSet[T]) Ceiling(v T) *T
- func (ts *TreeSet[T]) Clear()
- func (ts *TreeSet[T]) Contains(vs ...T) bool
- func (ts *TreeSet[T]) ContainsAll(ac Collection[T]) bool
- func (ts *TreeSet[T]) Delete(vs ...T)
- func (ts *TreeSet[T]) DeleteAll(ac Collection[T])
- func (ts *TreeSet[T]) DeleteIf(f func(T) bool)
- func (ts *TreeSet[T]) Each(f func(v T))
- func (ts *TreeSet[T]) Floor(v T) *T
- func (ts *TreeSet[T]) Graph() string
- func (ts *TreeSet[T]) Head() (v T)
- func (ts *TreeSet[T]) IsEmpty() bool
- func (ts *TreeSet[T]) Iterator() Iterator[T]
- func (ts *TreeSet[T]) Len() int
- func (ts *TreeSet[T]) MarshalJSON() ([]byte, error)
- func (ts *TreeSet[T]) PeekHead() (v T, ok bool)
- func (ts *TreeSet[T]) PeekTail() (v T, ok bool)
- func (ts *TreeSet[T]) PollHead() (v T, ok bool)
- func (ts *TreeSet[T]) PollTail() (v T, ok bool)
- func (ts *TreeSet[T]) Retain(vs ...T)
- func (ts *TreeSet[T]) RetainAll(ac Collection[T])
- func (ts *TreeSet[T]) ReverseEach(f func(v T))
- func (ts *TreeSet[T]) String() string
- func (ts *TreeSet[T]) Tail() (v T)
- func (ts *TreeSet[T]) UnmarshalJSON(data []byte) error
- func (ts *TreeSet[T]) Values() []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompareFloat32 ¶
CompareFloat32 provides a basic comparison on float32
func CompareFloat64 ¶
CompareFloat64 provides a basic comparison on float64
func CompareInt16 ¶
CompareInt16 provides a basic comparison on int16
func CompareInt32 ¶
CompareInt32 provides a basic comparison on int32
func CompareInt64 ¶
CompareInt64 provides a basic comparison on int64
func CompareString ¶
CompareString provides a fast comparison on strings
func CompareUInt16 ¶
CompareUInt16 provides a basic comparison on uint16
func CompareUInt32 ¶
CompareUInt32 provides a basic comparison on uint32
func CompareUInt64 ¶
CompareUInt64 provides a basic comparison on uint64
func CompareUInt8 ¶
CompareUInt8 provides a basic comparison on uint8
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.Add("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.Remove(2) // ["b","a"] list.Remove(1) // ["b"] list.Remove(0) // [] list.Remove(0) // [] (ignored) _ = 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 ¶
AsArrayList returns an initialized list. Example: AsArrayList([]T{1, 2, 3})
func NewArrayList ¶
NewArrayList returns an initialized list. Example: NewArrayList(1, 2, 3)
func (*ArrayList[T]) Add ¶
func (al *ArrayList[T]) Add(vs ...T)
Add adds all items of vs and returns the last added item.
func (*ArrayList[T]) AddAll ¶
func (al *ArrayList[T]) AddAll(ac Collection[T])
AddAll adds all items of another collection
func (*ArrayList[T]) ContainsAll ¶
func (al *ArrayList[T]) ContainsAll(ac Collection[T]) bool
ContainsAll Test to see if the collection contains all items of another collection
func (*ArrayList[T]) Delete ¶
func (al *ArrayList[T]) Delete(vs ...T)
Delete delete all items with associated value v of vs
func (*ArrayList[T]) DeleteAll ¶
func (al *ArrayList[T]) DeleteAll(ac Collection[T])
DeleteAll delete all of this collection's elements that are also contained in the specified collection
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 ¶
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]) Index ¶
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]) IndexIf ¶
IndexIf 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 ¶
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]) InsertAll ¶
func (al *ArrayList[T]) InsertAll(index int, ac Collection[T])
InsertAll 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]) MarshalJSON ¶
MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(al)
func (*ArrayList[T]) Push ¶
func (al *ArrayList[T]) Push(vs ...T)
Push inserts all items of vs at the tail of list al.
func (*ArrayList[T]) PushHead ¶
func (al *ArrayList[T]) PushHead(vs ...T)
PushHead inserts all items of vs at the head of list al.
func (*ArrayList[T]) PushHeadAll ¶
func (al *ArrayList[T]) PushHeadAll(ac Collection[T])
PushHeadAll 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]) PushTail ¶
func (al *ArrayList[T]) PushTail(vs ...T)
PushTail inserts all items of vs at the tail of list al.
func (*ArrayList[T]) PushTailAll ¶
func (al *ArrayList[T]) PushTailAll(ac Collection[T])
PushTailAll 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]) Retain ¶
func (al *ArrayList[T]) Retain(vs ...T)
Retain Retains only the elements in this collection that are contained in the argument array vs.
func (*ArrayList[T]) RetainAll ¶
func (al *ArrayList[T]) RetainAll(ac Collection[T])
RetainAll Retains only the elements in this collection that are contained in the specified collection.
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 ¶
Set set the v at the specified index in this list and returns the old value.
func (*ArrayList[T]) Sort ¶
Sort Sorts this list according to the order induced by the specified Comparator.
func (*ArrayList[T]) UnmarshalJSON ¶
UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, al)
type Collection ¶
type Collection[T any] interface { Container // Add adds items of vs to the collection Add(vs ...T) // AddAll adds all items of another collection AddAll(ac Collection[T]) // Delete delete all items of vs Delete(vs ...T) // DeleteIf delete all items that function f returns true DeleteIf(f func(T) bool) // DeleteAll delete all of this collection's elements that are also contained in the specified collection DeleteAll(ac Collection[T]) // Contains Test to see if the collection contains all items of vs Contains(vs ...T) bool // ContainsAll Test to see if the collection contains all items of another collection ContainsAll(ac Collection[T]) bool // Retain Retains only the elements in this collection that are contained in the argument array vs. Retain(vs ...T) // RetainAll Retains only the elements in this collection that are contained in the specified collection. RetainAll(ac Collection[T]) // Values returns a slice contains all the items of the collection Values() []T Eachable[T] }
Collection the base collection interface
type Compare ¶
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 adds items of vs to the head of queue PushHead(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 adds items of vs to the tail of queue PushTail(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]() // empty 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.Delete(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]) Contains ¶
Contains looks for the given key, and returns true if the key exists in the map.
func (*HashMap[K, V]) Delete ¶
Delete delete all items with key of ks, and returns what `Get` would have returned on that key prior to the call to `Set`.
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]) Get ¶
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]) MarshalJSON ¶
MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(lm)
func (*HashMap[K, V]) Set ¶
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]) SetIfAbsent ¶
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]) SetPairs ¶
SetPairs set items from key-value items array, override the existing items
func (*HashMap[K, V]) UnmarshalJSON ¶
UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, lm)
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]() // empty set.Add(1) // 1 set.Add(2, 2, 3, 4, 5) // 3, 1, 2, 4, 5 (random order, duplicates ignored) set.Delete(4) // 5, 3, 2, 1 (random order) set.Delete(2, 3) // 1, 5 (random order) set.Contains(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]) AddAll ¶
func (hs *HashSet[T]) AddAll(ac Collection[T])
AddAll adds all items of another collection
func (*HashSet[T]) ContainsAll ¶
func (hs *HashSet[T]) ContainsAll(ac Collection[T]) bool
ContainsAll Test to see if the collection contains all items of another collection
func (*HashSet[T]) DeleteAll ¶
func (hs *HashSet[T]) DeleteAll(ac Collection[T])
DeleteAll delete all of this collection's elements that are also contained in the specified collection
func (*HashSet[T]) Difference ¶
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 ¶
Intersection Find the intersection of two sets
func (*HashSet[T]) MarshalJSON ¶
MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(hs)
func (*HashSet[T]) Retain ¶
func (hs *HashSet[T]) Retain(vs ...T)
Retain Retains only the elements in this collection that are contained in the argument array vs.
func (*HashSet[T]) RetainAll ¶
func (hs *HashSet[T]) RetainAll(ac Collection[T])
RetainAll Retains only the elements in this collection that are contained in the specified collection.
func (*HashSet[T]) UnmarshalJSON ¶
UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, 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 ¶
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]() // empty (keys are of type int) 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.Delete(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]) 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]) Delete ¶
func (lm *LinkedHashMap[K, V]) Delete(ks ...K) (ov V, ok bool)
Delete delete all items with key of ks, and returns what `Get` would have returned on that key prior to the call to `Set`.
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]) 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]) 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]) 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]) SetAll ¶
func (lm *LinkedHashMap[K, V]) SetAll(am Map[K, V])
SetAll set items from another map am, 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]) SetPairs ¶
func (lm *LinkedHashMap[K, V]) SetPairs(pairs ...P[K, V])
SetPairs set items from key-value items array, override the existing items
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: NewLinkedHashSet(1, 2, 3)
Example ¶
set := NewLinkedHashSet[int]() // empty set.Add(5) // 5 set.Add(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.Delete(4) // 5, 3, 2, 1 (in insertion-order) set.Delete(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(vs ...T)
Add adds all items of vs and returns the last added item. Note: existing item's order will not change.
func (*LinkedHashSet[T]) AddAll ¶
func (ls *LinkedHashSet[T]) AddAll(ac Collection[T])
AddAll adds all items of another collection Note: existing item's order will not change.
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]) ContainsAll ¶
func (ls *LinkedHashSet[T]) ContainsAll(ac Collection[T]) bool
ContainsAll Test to see if the collection contains all items of another collection
func (*LinkedHashSet[T]) Delete ¶
func (ls *LinkedHashSet[T]) Delete(vs ...T)
Delete delete all items with associated value v of vs
func (*LinkedHashSet[T]) DeleteAll ¶
func (ls *LinkedHashSet[T]) DeleteAll(ac Collection[T])
DeleteAll delete all of this collection's elements that are also contained in the specified collection
func (*LinkedHashSet[T]) DeleteIf ¶
func (ls *LinkedHashSet[T]) DeleteIf(f func(T) bool)
DeleteIf delete all items that function f returns true
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, 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]) InsertAll ¶
func (ls *LinkedHashSet[T]) InsertAll(index int, ac Collection[T])
InsertAll 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]) 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(vs ...T)
Push inserts all items of vs at the tail of set al.
func (*LinkedHashSet[T]) PushHead ¶
func (ls *LinkedHashSet[T]) PushHead(vs ...T)
PushHead inserts all items of vs at the head of set ls.
func (*LinkedHashSet[T]) PushHeadAll ¶
func (ls *LinkedHashSet[T]) PushHeadAll(ac Collection[T])
PushHeadAll 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]) PushTail ¶
func (ls *LinkedHashSet[T]) PushTail(vs ...T)
PushTail inserts all items of vs at the tail of set ls.
func (*LinkedHashSet[T]) PushTailAll ¶
func (ls *LinkedHashSet[T]) PushTailAll(ac Collection[T])
PushTailAll 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]) Remove ¶
func (ls *LinkedHashSet[T]) Remove(index int)
Remove removes the element at the specified position in this set.
func (*LinkedHashSet[T]) Retain ¶
func (ls *LinkedHashSet[T]) Retain(vs ...T)
Retain Retains only the elements in this collection that are contained in the argument array vs.
func (*LinkedHashSet[T]) RetainAll ¶
func (ls *LinkedHashSet[T]) RetainAll(ac Collection[T])
RetainAll Retains only the elements in this collection that are contained in the specified collection.
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: NewLinkedList(1, 2, 3)
Example ¶
list := NewLinkedList[string]() list.Add("a") // ["a"] list.Add("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.Remove(2) // ["b","a"] list.Remove(1) // ["b"] list.Remove(0) // [] list.Remove(0) // [] (ignored) _ = 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(vs ...T)
Add adds all items of vs and returns the last added item.
func (*LinkedList[T]) AddAll ¶
func (ll *LinkedList[T]) AddAll(ac Collection[T])
AddAll adds all items of another collection
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]) ContainsAll ¶
func (ll *LinkedList[T]) ContainsAll(ac Collection[T]) bool
ContainsAll Test to see if the collection contains all items of another collection
func (*LinkedList[T]) Delete ¶
func (ll *LinkedList[T]) Delete(vs ...T)
Delete delete all items with associated value v of vs
func (*LinkedList[T]) DeleteAll ¶
func (ll *LinkedList[T]) DeleteAll(ac Collection[T])
DeleteAll delete all of this collection's elements that are also contained in the specified collection
func (*LinkedList[T]) DeleteIf ¶
func (ll *LinkedList[T]) DeleteIf(f func(T) bool)
DeleteIf delete all items that function f returns true
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]) 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]) IndexIf ¶
func (ll *LinkedList[T]) IndexIf(f func(T) bool) int
IndexIf 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, 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 (*LinkedList[T]) InsertAll ¶
func (ll *LinkedList[T]) InsertAll(index int, ac Collection[T])
InsertAll 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]) 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]) 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(vs ...T)
Push inserts all items of vs at the tail of list al.
func (*LinkedList[T]) PushHead ¶
func (ll *LinkedList[T]) PushHead(vs ...T)
PushHead inserts all items of vs at the head of list ll.
func (*LinkedList[T]) PushHeadAll ¶
func (ll *LinkedList[T]) PushHeadAll(ac Collection[T])
PushHeadAll 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]) PushTail ¶
func (ll *LinkedList[T]) PushTail(vs ...T)
PushTail inserts all items of vs at the tail of list ll.
func (*LinkedList[T]) PushTailAll ¶
func (ll *LinkedList[T]) PushTailAll(ac Collection[T])
PushTailAll 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]) Remove ¶
func (ll *LinkedList[T]) Remove(index int)
Remove removes the element at the specified position in this list.
func (*LinkedList[T]) Retain ¶
func (ll *LinkedList[T]) Retain(vs ...T)
Retain Retains only the elements in this collection that are contained in the argument array vs.
func (*LinkedList[T]) RetainAll ¶
func (ll *LinkedList[T]) RetainAll(ac Collection[T])
RetainAll Retains only the elements in this collection that are contained in the specified collection.
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]) Swap ¶
func (ll *LinkedList[T]) Swap(i, j int)
Swap swaps values of two items at the given index.
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 ¶
LinkedMapNode is a node of a linked hash map.
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
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 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. Insert(index int, vs ...T) // InsertAll 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. InsertAll(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 // IndexIf returns the index of the first true returned by function f in this list, or -1 if this list does not contain v. IndexIf(f func(T) bool) int // Remove delete the item at the specified position in this list Remove(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) // 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) // SetPairs set items from key-value items array, override the existing items SetPairs(pairs ...P[K, V]) // SetAll set items from another map am, override the existing items SetAll(am Map[K, V]) // 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) // Delete delete all items with key of ks, // and returns what `Get` would have returned // on that key prior to the call to `Set`. Delete(ks ...K) (ov V, ok 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 Eachable2[K, V] }
Map map interface
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 adds items of vs to the tail of queue Push(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: NewRingBuffer(1, 2, 3)
func (*RingBuffer[T]) Add ¶
func (rb *RingBuffer[T]) Add(vs ...T)
Add adds all items of vs and returns the last added item.
func (*RingBuffer[T]) AddAll ¶
func (rb *RingBuffer[T]) AddAll(ac Collection[T])
AddAll adds all items of another collection
func (*RingBuffer[T]) Cap ¶
func (rb *RingBuffer[T]) Cap() int
Cap returns the capcity of the buffer.
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]) ContainsAll ¶
func (rb *RingBuffer[T]) ContainsAll(ac Collection[T]) bool
ContainsAll Test to see if the collection contains all items of another collection
func (*RingBuffer[T]) Delete ¶
func (rb *RingBuffer[T]) Delete(vs ...T)
Delete delete all items with associated value v of vs
func (*RingBuffer[T]) DeleteAll ¶
func (rb *RingBuffer[T]) DeleteAll(ac Collection[T])
DeleteAll delete all of this collection's elements that are also contained in the specified collection
func (*RingBuffer[T]) DeleteIf ¶
func (rb *RingBuffer[T]) DeleteIf(f func(T) bool)
DeleteIf delete all items that function f returns true
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]) IndexIf ¶
func (rb *RingBuffer[T]) IndexIf(f func(T) bool) int
IndexIf 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, 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 RingBuffer's size Note: position equal to RingBuffer's size is valid, i.e. append.
func (*RingBuffer[T]) InsertAll ¶
func (rb *RingBuffer[T]) InsertAll(index int, ac Collection[T])
InsertAll 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]) 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(vs ...T)
Push inserts all items of vs at the tail of RingBuffer rb.
func (*RingBuffer[T]) PushHead ¶
func (rb *RingBuffer[T]) PushHead(vs ...T)
PushHead inserts all items of vs at the head of RingBuffer rb.
func (*RingBuffer[T]) PushHeadAll ¶
func (rb *RingBuffer[T]) PushHeadAll(ac Collection[T])
PushHeadAll 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]) PushTail ¶
func (rb *RingBuffer[T]) PushTail(vs ...T)
PushTail inserts all items of vs at the tail of RingBuffer rb.
func (*RingBuffer[T]) PushTailAll ¶
func (rb *RingBuffer[T]) PushTailAll(ac Collection[T])
PushTailAll 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]) Remove ¶
func (rb *RingBuffer[T]) Remove(index int)
Remove removes the item at the specified position in this RingBuffer.
func (*RingBuffer[T]) Retain ¶
func (rb *RingBuffer[T]) Retain(vs ...T)
Retain Retains only the elements in this collection that are contained in the argument array vs.
func (*RingBuffer[T]) RetainAll ¶
func (rb *RingBuffer[T]) RetainAll(ac Collection[T])
RetainAll Retains only the elements in this collection that are contained in the specified collection.
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 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 ¶
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) // empty (keys are of type int) 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.Delete(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 ¶
NewTreeMap creates a new TreeMap. Example: NewTreeMap(CompareString, []P{{"k1", "v1"}, {"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]) Contains ¶
Contains looks for the given key, and returns true if the key exists in the map.
func (*TreeMap[K, V]) Delete ¶
Delete delete all items with key of ks, and returns what `Get` would have returned on that key prior to the call to `Delete`.
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]) 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 ¶
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]) Head ¶
func (tm *TreeMap[K, V]) Head() *TreeMapNode[K, V]
Head returns a pointer to the minimum item.
func (*TreeMap[K, V]) Items ¶
func (tm *TreeMap[K, V]) Items() []*TreeMapNode[K, V]
Items returns the map item slice
func (*TreeMap[K, V]) MarshalJSON ¶
MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(tm)
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]) 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 ¶
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]) SetIfAbsent ¶
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]) SetPairs ¶
SetPairs set items from key-value items array, override the existing items
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 ¶
UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, tm)
type TreeMapNode ¶
TreeMapNode is a node of red-black tree
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
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 ¶
NewTreeSet creates a new TreeSet. Example: NewTreeSet(CompareString, "v1", "v2")
Example ¶
set := NewTreeSet(CompareInt) // empty (keys are of type int) set.Add(1) // 1 set.Add(2, 2, 3, 4, 5) // 1, 2, 3, 4, 5 (in order, duplicates ignored) set.Delete(4) // 1, 2, 3, 5 (in order) set.Delete(2, 3) // 1, 5 (in order) set.Contains(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(vs ...T)
Add adds all items of vs and returns the last added item.
func (*TreeSet[T]) AddAll ¶
func (ts *TreeSet[T]) AddAll(ac Collection[T])
AddAll adds all items of another collection
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]) ContainsAll ¶
func (ts *TreeSet[T]) ContainsAll(ac Collection[T]) bool
ContainsAll Test to see if the collection contains all items of another collection
func (*TreeSet[T]) Delete ¶
func (ts *TreeSet[T]) Delete(vs ...T)
Delete delete all items with associated value v of vs
func (*TreeSet[T]) DeleteAll ¶
func (ts *TreeSet[T]) DeleteAll(ac Collection[T])
DeleteAll delete all of this collection's elements that are also contained in the specified collection
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]) 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]) MarshalJSON ¶
MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(ts)
func (*TreeSet[T]) Retain ¶
func (ts *TreeSet[T]) Retain(vs ...T)
Retain Retains only the elements in this collection that are contained in the argument array vs.
func (*TreeSet[T]) RetainAll ¶
func (ts *TreeSet[T]) RetainAll(ac Collection[T])
RetainAll Retains only the elements in this collection that are contained in the specified collection.
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]) 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 ¶
UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, ts)
Source Files ¶
- array_list.go
- array_list_iter.go
- collection.go
- compare.go
- hash_map.go
- hash_set.go
- iterator.go
- json.go
- less.go
- linked_hash_map.go
- linked_hash_map_iter.go
- linked_hash_map_node.go
- linked_hash_set.go
- linked_hash_set_iter.go
- linked_hash_set_node.go
- linked_list.go
- linked_list_iter.go
- linked_list_node.go
- red_black_color.go
- ring_buffer.go
- ring_buffer_iter.go
- sorter.go
- tree_map.go
- tree_map_iter.go
- tree_map_node.go
- tree_set.go
- tree_set_iter.go
- tree_set_node.go
- util.go