slices

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToStr

func BytesToStr(bytes []byte) string

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

func DedupSlice[T any](slice *[]T, keyFn func(v T) string)

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 Int32ToStrSlice(arr []int32) []string

func Int64ToStrSlice

func Int64ToStrSlice(arr []int64) []string

func InterfaceSlice

func InterfaceSlice[T any](arr []T) []interface{}

func Last

func Last[T any](strSlice []T) (ret T)

LastString returns the last element in a slice.

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 Repeat

func Repeat[T any](n int, element T) []T

Repeat returns a slice with `n` repeated same `element` string.

func Reverse

func Reverse[T any](slice []T) []T

func StrToBytes

func StrToBytes(str string) []byte

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 (bs BitSet[T]) Difference(obj BitSet[T]) BitSet[T]

func (BitSet[T]) HasOverlap

func (bs BitSet[T]) HasOverlap(obj BitSet[T]) bool

func (BitSet[T]) Intersect

func (bs BitSet[T]) Intersect(obj BitSet[T]) BitSet[T]

func (BitSet[T]) Length

func (bs BitSet[T]) Length() int

func (BitSet[T]) Slices

func (bs BitSet[T]) Slices(bsr *BitSetRange[T]) []T

func (BitSet[T]) Union

func (bs BitSet[T]) Union(obj BitSet[T]) BitSet[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.

`

func (*Heap[T]) Cap

func (h *Heap[T]) Cap() int

Cap returns the capacity of the heap.

func (*Heap[T]) IsEmpty

func (h *Heap[T]) IsEmpty() bool

IsEmpty returns whether the heap is empty.

func (*Heap[T]) IsFull

func (h *Heap[T]) IsFull() bool

IsFull returns whether the heap is full.

func (*Heap[T]) Len

func (h *Heap[T]) Len() int

Len returns the actual number of elements in the heap.

func (*Heap[T]) Peek

func (h *Heap[T]) Peek() (T, error)

Peek returns the top item in heap. Equivalent to the `container[0]`

func (*Heap[T]) Pop

func (h *Heap[T]) Pop() (T, error)

Pop returns the top item, and removes it from the heap.

func (*Heap[T]) Push

func (h *Heap[T]) Push(element T) error

Push adds an element to the heap.

type IntRange

type IntRange struct {
	Min int
	Max int
}

func MergeIntRanges

func MergeIntRanges(rs ...*IntRange) []*IntRange

func ParseIntRange

func ParseIntRange(s string) (*IntRange, error)

func ParseIntRanges

func ParseIntRanges(s []string) ([]*IntRange, error)

func (*IntRange) Contains

func (r *IntRange) Contains(i int) bool

func (*IntRange) String

func (r *IntRange) String() string

type IntRanges

type IntRanges []*IntRange

func (IntRanges) Contains

func (rs IntRanges) Contains(i int) bool

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

type Ring[T any] struct {
	Data []T

	Lock sync.RWMutex
	// contains filtered or unexported fields
}

Ring is a Thread-safe string slice with fixed size. Only the last "capacity" strings are retained.

func NewRing

func NewRing[T any](cap int) *Ring[T]

NewRing returns a Ring object with capacity "cap".

func (*Ring[T]) Append

func (rs *Ring[T]) Append(ele ...T)

Append adds elements to the tail of ring.

func (*Ring[T]) Cap

func (rs *Ring[T]) Cap() int

Cap returns the capacity of the ring.

func (*Ring[T]) Get

func (rs *Ring[T]) Get(idx int) (T, error)

Get returns the idx'th element.

func (*Ring[T]) Len

func (rs *Ring[T]) Len() int

Len returns the actual number of elements stored in the ring.

func (*Ring[T]) Size

func (rs *Ring[T]) Size() int

Size returns the logical size of the appended data. Only the [Size-Cap, Size) elements are retained.

type Set

type Set[T comparable] map[T]struct{}

Set is a data structure to determine existency. Note: Set is not thread-safe.

func NewSet

func NewSet[T comparable](slices ...T) Set[T]

NewSet returns a Set.

func (Set[T]) Add

func (ss Set[T]) Add(strs ...T) Set[T]

Add pushs element elemento set. If already in, it's a no-op.

func (Set[T]) Contains

func (ss Set[T]) Contains(s T) bool

Contains return true if given element is in the set.

func (Set[T]) Delete

func (ss Set[T]) Delete(strs ...T) Set[T]

Delete remove element from set. If not in, it's a no-op.

func (Set[T]) Difference

func (ss Set[T]) Difference(obj Set[T]) Set[T]

Difference returns the difference of the main set exculding the objective set.

func (Set[T]) Exclude

func (ss Set[T]) Exclude(obj Set[T]) Set[T]

Exclude removes content in another element set from the current set.

func (Set[T]) HasOverlap

func (ss Set[T]) HasOverlap(obj Set[T]) bool

HasOverlap returns whether two elementSet have any common elements.

func (Set[T]) Intersect added in v1.0.2

func (ss Set[T]) Intersect(obj Set[T]) Set[T]

Intersect returns the intersection of two sets.

func (Set[T]) Length

func (ss Set[T]) Length() int

Length of the set elements number.

func (Set[T]) Merge

func (ss Set[T]) Merge(obj Set[T]) Set[T]

Merge adds all content in another element set to the current set.

func (Set[T]) Slice

func (ss Set[T]) Slice() []T

Slice returns a slices of elements.

func (Set[T]) Union

func (ss Set[T]) Union(obj Set[T]) Set[T]

Union returns the union of two sets.

Jump to

Keyboard shortcuts

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