golds

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 0 Imported by: 0

README

golds

Go(lang) Data Structures

Goal

Create a package with lots high-quality generic data structures in Go.

Status

Work in progress.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reverse

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

Reverse reverses the items of slice in place.

func SliceContains

func SliceContains[E comparable](slice []E, v E) bool

SliceContains returns true if the slice contains the specified element.

func SliceContainsFn

func SliceContainsFn[E any](slice []E, fn func(E) bool) bool

SliceContainsFn returns true, if fn-predicate returns true for any element of slice.

func SliceEq

func SliceEq[E comparable](a, b []E) bool

SliceEq compares two slices respecting element ordering. Empty slice is considered to be equal to nil slices.

func SliceEqWith

func SliceEqWith[A, B any](a []A, b []B, fn func(A, B) bool) bool

SliceEq compares two slices respecting element ordering using provided equality hook. Empty slice is considered to be equal to nil slices.

Types

type Heap

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

Heap data structure.

func NewHeap

func NewHeap[T any](size int, cmp func(a, b T) int) *Heap[T]

NewHeap instantiates a new heap with a given size.

func NewHeapFrom

func NewHeapFrom[T any](v []T, cmp func(a, b T) int) *Heap[T]

NewHeapFrom instantiates a new heap with a given size.

func (*Heap[T]) Build

func (h *Heap[T]) Build(values []T)

Build pushes all items from values to the heap

func (*Heap[T]) Pop

func (h *Heap[T]) Pop() (value T, ok bool)

Pop removes and returns top element of the heap

func (*Heap[T]) PopMany

func (h *Heap[T]) PopMany(k int) (values []T, ok bool)

PopMany removes and returns top k elements of the heap

func (*Heap[T]) Push

func (h *Heap[T]) Push(value T)

Push element to the heap.

func (*Heap[T]) PushMany

func (h *Heap[T]) PushMany(values ...T)

PushMany elements to the heap.

func (*Heap[T]) Reset

func (h *Heap[T]) Reset()

Reset the structure. Remove all the elements.

func (*Heap[T]) Size

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

Size of the heap.

func (*Heap[T]) Top

func (h *Heap[T]) Top() (value T, ok bool)

Top returns top element of the heap

func (*Heap[T]) Values

func (h *Heap[T]) Values() []T

Values that are in the heap.

type Slice

type Slice[E any] []E

Slice is a sequence of values. Basically it's an utility wrapper around a plain slice.

func NewSlice

func NewSlice[E any](vv ...E) Slice[E]

NewSlice puts provided values into resulting slice. export

func Repeat

func Repeat[E any](n int, v E) Slice[E]

Repeat returns a new Slice[E] with n copies of v.

func SliceIterFn

func SliceIterFn[E any](n int, fn func(int) E) Slice[E]

SliceIterFn returns a new Slice[E] with results of fn(i) for i := range [0, n].

func (*Slice[E]) Append

func (s *Slice[E]) Append(vv ...E)

Append new elements to the slice in place.

func (Slice[E]) Apply

func (s Slice[E]) Apply(fn func(v E) E) Slice[E]

Apply creates a new slice with mapped values.

func (Slice[E]) Cap

func (s Slice[E]) Cap() int

Cap returns capacity of underlying slice.

func (Slice[E]) ContainsFn

func (slice Slice[E]) ContainsFn(fn func(E) bool) bool

ContainsFn returns true, if fn-predicate returns true for any element of slice.

func (Slice[E]) Copy

func (s Slice[E]) Copy() Slice[E]

func (Slice[E]) CopyWith

func (s Slice[E]) CopyWith(fn func(E) E) Slice[E]

func (Slice[E]) Count

func (s Slice[E]) Count(fn func(v E) bool) int

Count returns the number of elements e, where fn(e) is true.

func (*Slice[E]) Delete

func (s *Slice[E]) Delete(i int)

Delete value at i-position, shifting elements to the begining of slice. Panics if the index is out of range.

Slice{1, 2, 3}.Delete(1) -> Slice{2, 3}

func (*Slice[E]) DeleteNoOrder

func (s *Slice[E]) DeleteNoOrder(i int)

DeleteNoOrder exchanges the i-th and the last element of the slice and cuts the last, now duplicated, element.

func (Slice[E]) EqWith

func (s Slice[E]) EqWith(other []E, fn func(E, E) bool) bool

EqWith compares slice with other one using provided hook.

func (Slice[E]) Fill

func (s Slice[E]) Fill(v E)

Fill slice using provided value.

func (Slice[E]) FillWith

func (s Slice[E]) FillWith(fn func() E)

FillWith uses results of fn to fill the slice.

func (Slice[E]) Filter

func (s Slice[E]) Filter(fn func(v E) bool) Slice[E]

Filter returns slice of elements e, where fn(e) is true. fn can be called multiple times for each element.

func (Slice[E]) FilterInPlace

func (s Slice[E]) FilterInPlace(fn func(v E) bool)

func (Slice[E]) Index

func (s Slice[E]) Index(i int) E

Index returns element by the index. If index is negative, then returns element len-index. Panics if index is out of [-len, len] range.

func (*Slice[E]) Insert

func (s *Slice[E]) Insert(i int, v E)

Insert value at i-position, shifting elements to the end of slice. Panics if the index is out of range.

Slice{1, 2, 3}.Insert(1, 100) -> Slice{1, 100, 2, 3}

func (Slice[E]) Len

func (s Slice[E]) Len() int

Len returns the number of elements in the slice.

func (*Slice[E]) Pop

func (s *Slice[E]) Pop() (E, bool)

Pop returns returns the last element and removes it from the slice. If the slice is empty, then returns false.

func (Slice[E]) Reverse

func (slice Slice[E]) Reverse()

Reverse reverses the items of slice in place.

func (Slice[E]) Select

func (s Slice[E]) Select(indexes ...int) Slice[E]

Select returns gets elements by indexes and puts them into a new slice. If index is negative, then len-index element will be used. Example:

Slice[int]{1, 2, 3}.Select(-1, 0, 2) -> Slice[int]{3, 1, 2}

func (Slice[E]) Swap

func (s Slice[E]) Swap(i, j int)

Swap two elements in the slice. If i or j are negative, then uses elements len-i(j).

Jump to

Keyboard shortcuts

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