Documentation ¶
Index ¶
- func BytesToStr(bytes []byte) string
- func Clone[T any](strSlice []T) []T
- func Compact[T comparable](slice *[]T)
- func Contain[T comparable](slice []T, s T) bool
- func Dedup[T comparable](slice []T) []T
- func DedupSlice[T any](slice *[]T, keyFn func(v T) string)
- func Equals[T comparable](a, b []T) bool
- func Filter[T comparable](slices *[]T, predictFn func(i int) bool)
- func FilterValues[T comparable](slice []T, needles ...T) []T
- func Find[T comparable](slice []T, s T) int
- func First[T any](strSlice []T) (ret T)
- func Int32ToStrSlice(arr []int32) []string
- func Int64ToStrSlice(arr []int64) []string
- func InterfaceSlice[T any](arr []T) []interface{}
- func Last[T any](strSlice []T) (ret T)
- func Map[T, K any](slice []T, fn func(v T) K) []K
- func Repeat[T any](n int, element T) []T
- func Reverse[T any](slice []T) []T
- func StrToBytes(str string) []byte
- type BitSet
- func (bs BitSet[T]) Add(bsr *BitSetRange[T], strs ...T) BitSet[T]
- func (bs BitSet[T]) Contains(bsr *BitSetRange[T], s T) bool
- func (bs BitSet[T]) Difference(obj BitSet[T]) BitSet[T]
- func (bs BitSet[T]) HasOverlap(obj BitSet[T]) bool
- func (bs BitSet[T]) Intersect(obj BitSet[T]) BitSet[T]
- func (bs BitSet[T]) Length() int
- func (bs BitSet[T]) Slices(bsr *BitSetRange[T]) []T
- func (bs BitSet[T]) Union(obj BitSet[T]) BitSet[T]
- type BitSetRange
- type Heap
- type IntRange
- type IntRanges
- type OrderedSet
- type Ring
- type Set
- func (ss Set[T]) Add(strs ...T) Set[T]
- func (ss Set[T]) Contains(s T) bool
- func (ss Set[T]) Delete(strs ...T) Set[T]
- func (ss Set[T]) Difference(obj Set[T]) Set[T]
- func (ss Set[T]) Exclude(obj Set[T]) Set[T]
- func (ss Set[T]) HasOverlap(obj Set[T]) bool
- func (ss Set[T]) Intersect(obj Set[T]) Set[T]
- func (ss Set[T]) Length() int
- func (ss Set[T]) Merge(obj Set[T]) Set[T]
- func (ss Set[T]) Slice() []T
- func (ss Set[T]) Union(obj Set[T]) Set[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToStr ¶
BytesToStr will return string using the byte array as underlying memory. Modifying the wrapped byte array will change the string content. NOTE: Should be used only to avoid copy for large string.
func Clone ¶
func Clone[T any](strSlice []T) []T
Clone copies the underlying array of given `strSlice` to a new slice.
func Compact ¶
func Compact[T comparable](slice *[]T)
Compact moves all non-nil elements in a slice to leftmost, returning the number of non-nil elements. If the element in slice is of type int, float, string, zero value will be regarded as nil. NOTE: This function performs in place modification. It has side-effect on slice. A common usage: ```
myslice := []string{"foo", nil, "bar"} slices.Compact(&myslice) // myslice = []string{"foo", "bar"}
```
func Contain ¶
func Contain[T comparable](slice []T, s T) bool
Contain return true if given string is in the string slice.
func Dedup ¶
func Dedup[T comparable](slice []T) []T
Dedup remove duplicated strings in slice. Note the given slice will be modified.
func DedupSlice ¶
DedupSlice remove duplicated elements in slice. User needs to provide a "key" function to determine key of each elements, The usage is similar to Compact() function: ```
myslice := []Object{a, b, a} slices.Dedup(&myslice, func(i Object) { return i.Key }) // myslice = []Object{a, b}
NOTE: This function performs in place modification. It has side-effect on slice. ```
func Equals ¶
func Equals[T comparable](a, b []T) bool
func Filter ¶
func Filter[T comparable](slices *[]T, predictFn func(i int) bool)
Filter removes the items failing predict function in a slice. For performance consideration, it's a in-place op to modify the slice.
func FilterValues ¶
func FilterValues[T comparable](slice []T, needles ...T) []T
FilterValues remove given strings from a slice. Be ware that the original slice is modifed in place.
func Find ¶
func Find[T comparable](slice []T, s T) int
Find return first index of given string. -1 for non existent.
func First ¶
func First[T any](strSlice []T) (ret T)
FirstString returns the first element in a slice.
func Int32ToStrSlice ¶
func Int64ToStrSlice ¶
func InterfaceSlice ¶
func InterfaceSlice[T any](arr []T) []interface{}
func Map ¶
func Map[T, K any](slice []T, fn func(v T) K) []K
Map values from a slice of objects.
Example usage: ` var slices []*SomeClass strs := Map(slices, func(i *SomeClass) string { return slices[i].String() }) `
func StrToBytes ¶
StrToBytes will return the same array in given string. Modifying the byte array will directly change underlying memory. NOTE: Should be used only to avoid copy for large string.
Types ¶
type BitSet ¶
type BitSet[T comparable] uint64
BitSet is a compressed StringSet that support limited number of categories, while having a much smaller memory footprint, and faster set operations, like union, intersection, etc. Usage:
bsr, err := NewBitSetRange("a", "b", "c") if err != nil { panic(err) } bs := bsr.NewBitSet("a", "b") bs.Contains(bsr, "a") // true bs.Contains(bsr, "c") // false bs = bs.Add(bsr, "c") // bs is now {a, b, c} bs.Slices(bsr) // []string{"a", "b", "c"} bs.Length() // 3 bs.HasOverlap(bsr.NewBitSet("a", "d")) // true bs.Intersect(bsr.NewBitSet("a", "d")) // {a} bs.Union(bsr.NewBitSet("a", "d")) // {a, b, c, d} bs.Difference(bsr.NewBitSet("a", "d")) // {b, c}
func (BitSet[T]) Add ¶
func (bs BitSet[T]) Add(bsr *BitSetRange[T], strs ...T) BitSet[T]
func (BitSet[T]) Contains ¶
func (bs BitSet[T]) Contains(bsr *BitSetRange[T], s T) bool
func (BitSet[T]) Difference ¶
func (BitSet[T]) HasOverlap ¶
func (BitSet[T]) Slices ¶
func (bs BitSet[T]) Slices(bsr *BitSetRange[T]) []T
type BitSetRange ¶
type BitSetRange[T comparable] struct { // contains filtered or unexported fields }
func NewBitSetRange ¶
func NewBitSetRange[T comparable](categories ...T) (*BitSetRange[T], error)
func (*BitSetRange[T]) NewBitSet ¶
func (bsr *BitSetRange[T]) NewBitSet(categories ...T) BitSet[T]
type Heap ¶
type Heap[T comparable] struct { // contains filtered or unexported fields }
Heap is a wrapper to standard built-in data structure container/heap.
func NewHeap ¶
func NewHeap[T comparable](heapSize int, cmpFunc func(i, j T) bool) *Heap[T]
NewHeap accepts a memory allocated slice, where the capacity of slice becomes the max size of the heap. The elements already stored in the given slice will become the initial elements in the heap. The second paramenter accepts a function to compare the elements in the container. Usage example: ```
cmpFunc := func(i, j *Item) { return i.Value < j.Value } h := NewHeap(maxSize, cmpFunc) err := h.Push(&Item{Value: "1234"}) // Load all the values from heap. var values []*Item for !h.IsEmpty() { v, _ := h.Pop() values = append(values, v) } // Now the values are the values in assending order.
`
type OrderedSet ¶
type OrderedSet[T comparable] map[T]int
OrderedSet provide fast way to find index of string in large slice.
func NewOrderedSet ¶
func NewOrderedSet[T comparable](slice ...T) OrderedSet[T]
NewOrderedSet return an OrderedSet object.
func (OrderedSet[T]) Contain ¶
func (os OrderedSet[T]) Contain(s T) bool
Contain return true if given string is in the set.
func (OrderedSet[T]) Find ¶
func (os OrderedSet[T]) Find(s T) int
Find return first index of given string. -1 for non existent.
type Ring ¶
Ring is a Thread-safe string slice with fixed size. Only the last "capacity" strings are retained.
func (*Ring[T]) Append ¶
func (rs *Ring[T]) Append(ele ...T)
Append adds elements to the tail of ring.
type Set ¶
type Set[T comparable] map[T]struct{}
Set is a data structure to determine existency. Note: Set is not thread-safe.
func (Set[T]) Difference ¶
Difference returns the difference of the main set exculding the objective set.
func (Set[T]) HasOverlap ¶
HasOverlap returns whether two elementSet have any common elements.