pslices

package
v0.4.81 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2023 License: ISC Imports: 8 Imported by: 2

Documentation

Overview

OrderedAny is a slice ordered by a function allowing duplicates. OrderedAny implements [parl.Ordered][E any].

OrderedPointers is an ordered list of *E pointers sorted by the referenced values. OrderedPointers implements [parl.OrderedPointers][E any].

Ordered is an ordered slice overwriting duplicates implementing [parl.Ordered][E any].

ResolveSlice removes one level of indirection from a slice of pointers.

SetLength adjusts the lenght of *slicep extending with append if necessary.

TrimLeft removes count bytes from the start of slicep, copying remaining bytes to its beginning.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IndexPartial added in v0.4.38

func IndexPartial(byts []byte, index int, key []byte) (newIndex int, notKnown bool)

IndexPartial returns scan-result in a byte-slice for a key byte-sequence, whether key was found or more bytes needs to be read to determine if key is present.

  • index is the first index to search at and it is checked against the length of byts
  • return value:
  • — notKnown: true, newIndex >= index: byts ended during a partial key match, newIndex is first byte of match
  • — notKnown: false, newIndex >= index: entire key was found at newIndex
  • — newIndex -1: no key sequence was found in byts
  • empty or nil key is found immediately, but only if index is less than length of byts
  • if byts is nil or empty, nothing is found
  • panic-free

func InsertOrdered

func InsertOrdered[E constraints.Ordered](slice0 []E, value E) (slice []E)

InsertOrdered inserts value into an ordered slice

  • duplicate values are allowed, new values are placed at the end
  • insert is O(log n)

func InsertOrderedFunc

func InsertOrderedFunc[E any](slice0 []E, value E, cmp func(a, b E) (result int)) (slice []E)

InsertOrderedFunc inserts a value into a slice making it ordered using a comparison function.

  • duplicate values are allowed, new values are placed at the end
  • insert is O(log n)
  • cmp function can be provided by E being a type with Cmp method.
  • cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b

func NewOrdered added in v0.4.29

func NewOrdered[E constraints.Ordered]() (list parli.Ordered[E])

NewOrdered returns a slice ordered by value.

func NewOrderedAny

func NewOrderedAny[E any](cmp func(a, b E) (result int)) (list parli.Ordered[E])

NewOrderedAny creates a list ordered by a comparison function.

  • The cmp comparison function can be provided by E being pslices.Comparable, ie. a type having a Cmp method.
  • cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b
  • duplicate values are allowed and inserted in order with later values last
  • if E is an interface-type and cmp is nil, every provided value must be checked to be comparable

func NewOrderedPointers

func NewOrderedPointers[E constraints.Ordered]() (list parli.OrderedPointers[E])

func ResolveSlice added in v0.4.29

func ResolveSlice[E any](slic []*E) (sList []E)

ResolveSlice removes one level of indirection from a slice of pointers.

func SetLength added in v0.4.48

func SetLength[E any](slicep *[]E, newLength int, noZero ...bool)

SetLength adjusts the lenght of *slicep extending with append if necessary.

  • slicep’s length is adjusted
  • if newLength > cap, slice may be reallocated

func TrimLeft added in v0.4.38

func TrimLeft[E any](slicep *[]E, count int, noZero ...bool)

TrimLeft removes count bytes from the start of slicep, copying remaining bytes to its beginning.

  • slicep’s length is adjusted
  • if count < 1 or slicep is empty or nil, nothing is done
  • if count >= len(slicep) slicep is emptied
  • no allocation or free is triggered

Types

type Ordered added in v0.4.29

type Ordered[E constraints.Ordered] struct {
	Slice[E]
}

Ordered is an ordered slice overwriting duplicates implementing [parl.Ordered][E any].

  • E must be a comparable and ordered type, ie. not slice func map.
  • Insert overwrites duplicates and is O(log n)
  • Delete removes the first occurrence O(log n)
  • For custom sort order or slice func map types, use NewOrderedAny

func (*Ordered[E]) Clone added in v0.4.29

func (o *Ordered[E]) Clone() (o2 parli.Ordered[E])

func (*Ordered[E]) Delete added in v0.4.29

func (o *Ordered[E]) Delete(element E)

Delete removes an element from an ordered slice.

  • if the element is not in the slice, the slice is not changed
  • is the slice has duplicates, the first occurrence is removed
  • O(log n)

func (*Ordered[E]) Index added in v0.4.29

func (o *Ordered[E]) Index(element E) (index int)

func (*Ordered[E]) Insert added in v0.4.29

func (o *Ordered[E]) Insert(element E)

Insert adds an element to an ordered slice.

  • Insert overwrites duplicates and is O(log n)

type OrderedAny

type OrderedAny[E any] struct {
	Slice[E] // Element() Length() List() Clear()
	// contains filtered or unexported fields
}

OrderedAny is a slice ordered by a function allowing duplicates. OrderedAny implements [parl.Ordered][E any].

  • cmp allows for custom ordering or ordering of slice map and function types
  • Use E as value for small-sized data or interface values.
  • Use E as a pointer for larger sized structs.
  • Duplicates are allowed, Inssert places duplicates at end
  • Insert and Delete O(log n)
  • cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b

func (*OrderedAny[E]) Clone added in v0.4.29

func (o *OrderedAny[E]) Clone() (o2 parli.Ordered[E])

Length returns the number of elements

func (*OrderedAny[E]) Delete

func (o *OrderedAny[E]) Delete(element E)

Delete removes an element from the ordered slice.

  • if the element did not exist, the slice is not changed
  • if element exists in duplicates, a random element of those duplicates is removed
  • O(log n)

func (*OrderedAny[E]) Index added in v0.4.29

func (o *OrderedAny[E]) Index(element E) (index int)

func (*OrderedAny[E]) Insert

func (o *OrderedAny[E]) Insert(element E)

Insert adds a value to the ordered slice.

type OrderedPointers

type OrderedPointers[E constraints.Ordered] struct {
	OrderedAny[*E] // Element() Length() List() Clear() Insert() Delete() Index() Clone()
}

OrderedPointers is an ordered list of *E pointers sorted by the referenced values. OrderedPointers implements [parl.OrderedPointers][E any].

  • The OrderedPointers ordered list does not require a comparison function
  • E is used for large structs.
  • Insert overwrites duplicates.
  • for custom sort order, use NewOrderedAny

func (*OrderedPointers[E]) Cmp

func (o *OrderedPointers[E]) Cmp(a, b *E) (result int)

type Queue added in v0.4.43

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

func NewQueue added in v0.4.43

func NewQueue[T any]() (queue *Queue[T])

func (*Queue[T]) Add added in v0.4.43

func (q *Queue[T]) Add(value T)

func (*Queue[T]) AddSlice added in v0.4.43

func (q *Queue[T]) AddSlice(slice []T)

func (*Queue[T]) Remove added in v0.4.43

func (q *Queue[T]) Remove(value T, ok bool)

type RangeCh added in v0.4.58

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

RangeCh is a range-able channel based on a thread-safe slice

  • RangeCh.Ch() must be either read until close or RangeCh.Close must be invoked

func NewRangeCh added in v0.4.58

func NewRangeCh[T any](tss *ThreadSafeSlice[T]) (rangeChan *RangeCh[T])

NewRangeCh returns a range-able channel based on a ThreadSafeSlice

  • the channel must be either read until it closes or invoke Close

func (*RangeCh[T]) Ch added in v0.4.58

func (r *RangeCh[T]) Ch() (ch <-chan T)

Ch returns the channel, thread-safe

func (*RangeCh[T]) Close added in v0.4.58

func (r *RangeCh[T]) Close()

Close closes the channel, thread-safe, idempotent

type Slice added in v0.4.29

type Slice[E any] struct {
	// contains filtered or unexported fields
}

Slice implements basic slice methods for use by a slice embedded in a struct. Slice implements parl.Slice[E any].

func (*Slice[E]) Clear added in v0.4.29

func (o *Slice[E]) Clear()

func (*Slice[E]) Element added in v0.4.29

func (o *Slice[E]) Element(index int) (element E)

func (*Slice[E]) Length added in v0.4.29

func (o *Slice[E]) Length() (index int)

func (*Slice[E]) List added in v0.4.29

func (o *Slice[E]) List(n ...int) (list []E)

List returns a clone of the ordered slice.

type ThreadSafeSlice added in v0.4.58

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

func NewThreadSafeSlice added in v0.4.58

func NewThreadSafeSlice[T any]() (threadSafeSlice *ThreadSafeSlice[T])

func (*ThreadSafeSlice[T]) Append added in v0.4.58

func (t *ThreadSafeSlice[T]) Append(element T)

func (*ThreadSafeSlice[T]) Clear added in v0.4.58

func (t *ThreadSafeSlice[T]) Clear()

func (*ThreadSafeSlice[T]) Get added in v0.4.58

func (t *ThreadSafeSlice[T]) Get(index int) (element T, hasValue bool)

func (*ThreadSafeSlice[T]) Length added in v0.4.58

func (t *ThreadSafeSlice[T]) Length() (length int)

func (*ThreadSafeSlice[T]) Put added in v0.4.58

func (t *ThreadSafeSlice[T]) Put(element T, index int) (success bool)

func (*ThreadSafeSlice[T]) SetLength added in v0.4.58

func (t *ThreadSafeSlice[T]) SetLength(newLength int)

func (*ThreadSafeSlice[T]) SliceClone added in v0.4.58

func (t *ThreadSafeSlice[T]) SliceClone() (clone []T)

func (*ThreadSafeSlice[T]) TrimLeft added in v0.4.58

func (t *ThreadSafeSlice[T]) TrimLeft(count int)

Jump to

Keyboard shortcuts

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