ds

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 2 Imported by: 10

Documentation

Overview

Package ds provides various data structure implementations with generics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

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

Heap is a data structure that orders items when inserted according to a specified ordering.

func NewHeap

func NewHeap[T any](initialCapacity int, better func(a, b T) bool) *Heap[T]

NewHeap creates a new Heap instance that is configured to use the specified better function to order items. When better returns true, the first argument will be placed higher in the heap. The specified initialCapacity is used to preallocate memory.

func (*Heap[T]) Clear

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

Clear removes all items from this Heap.

func (*Heap[T]) Clip

func (s *Heap[T]) Clip()

Clip removes unused capacity from the Heap.

func (*Heap[T]) IsEmpty

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

IsEmpty returns true if there are no items in this Heap and false otherwise.

func (*Heap[T]) Peek

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

Peek returns the top-most item from this Heap without removing it. This method panics if the Heap is empty so make sure to use IsEmpty beforehand.

func (*Heap[T]) Pop

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

Pop removes the top-most item from this Heap and returns it. This method panics if the Heap is empty so make sure to use IsEmpty beforehand.

func (*Heap[T]) Push

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

Push adds a new item to this Heap.

func (*Heap[T]) Size

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

Size returns the number of items stored in this Heap.

type List

type List[T comparable] struct {
	// contains filtered or unexported fields
}

List represents a sequence of items.

Currently a List can only store comparable items. This restriction allows for Remove, IndexOf and Contains operations.

func NewList

func NewList[T comparable](initialCapacity int) *List[T]

NewList creates a new List with the given capacity. The capacity can be used as a form of optimization. Regardless of the value, the initial size of the list is zero and the list can grow past the specified capacity.

func (*List[T]) Add

func (l *List[T]) Add(item T)

Add appends the specified item to the List.

func (*List[T]) Clear

func (l *List[T]) Clear()

Clear removes all items from this List.

func (*List[T]) Clip

func (l *List[T]) Clip()

Clip removes unused capacity from the List.

func (*List[T]) Contains

func (l *List[T]) Contains(item T) bool

Contains checks whether this List has the specified item and returns true if it is contained and false otherwise.

func (*List[T]) Each

func (l *List[T]) Each(iterator func(item T))

Each is a helper method allows one to iterate over all items in this List through a closure function.

func (*List[T]) Get

func (l *List[T]) Get(index int) T

Get returns the item in this list that is located at the specified index (starting from zero). This method will panic if the index is invalid.

func (*List[T]) IndexOf

func (l *List[T]) IndexOf(item T) int

IndexOf returns the index where the specified item is located in this List. If the item is not part of this list, this method returns -1.

func (*List[T]) IsEmpty

func (l *List[T]) IsEmpty() bool

IsEmpty returns whether this list has no elements.

func (*List[T]) Items

func (l *List[T]) Items() []T

Item returns all items stored in this List as a slice. The returned slice should not be mutated.

func (*List[T]) Remove

func (l *List[T]) Remove(item T) bool

Remove removes the specified item from this List and returns true. If the item is not contained by this List, then false is returned.

func (*List[T]) Size

func (l *List[T]) Size() int

Size returns the number of items contained in this List.

type Pool added in v0.8.0

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

Pool represents a storage structure that can preseve allocated objects for faster reuse.

func NewPool added in v0.8.0

func NewPool[T any]() *Pool[T]

NewPool creates a new Pool instance.

func (*Pool[T]) Clear added in v0.8.0

func (p *Pool[T]) Clear()

Clear removes any items that were stored for reuse.

func (*Pool[T]) Fetch added in v0.8.0

func (p *Pool[T]) Fetch() *T

Fetch retrieves an available item from the pool or creates a new one if one is not available.

func (*Pool[T]) IsEmpty added in v0.8.0

func (p *Pool[T]) IsEmpty() bool

IsEmpty returns true if there is nothing stored for reuse in this pool.

func (*Pool[T]) Restore added in v0.8.0

func (p *Pool[T]) Restore(v *T)

Restore returns an item to the pool to be reused.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set represents a set data structure, where only one instance of a given item is stored.

Note: Using the standard map[T]struct{} approach will likely yield faster performance and should be preferred in performance-critical code. This type makes usage of sets more human-readable.

func NewSet

func NewSet[T comparable](initialCapacity int) *Set[T]

NewSet creates a new Set instance with the specified initial capacity, which is only used to preallocate memory and does not act as an upper bound.

func SetDifference added in v0.6.0

func SetDifference[T comparable](first, second *Set[T]) *Set[T]

SetDifference creates a new Set that holds the difference between the first and the second specified sets.

func SetFromMapKeys added in v0.5.0

func SetFromMapKeys[T comparable, V any](m map[T]V) *Set[T]

SetFromMapKeys creates a new Set instance based on the keys of the provided map.

func SetFromMapValues added in v0.5.0

func SetFromMapValues[K comparable, V comparable](m map[K]V) *Set[V]

SetFromMapValues creates a new Set instance based on the values of the provided map.

func SetFromSlice added in v0.5.0

func SetFromSlice[T comparable](slice []T) *Set[T]

SetFromSlice creates a new Set instance based on the elements contained in the provided slice.

func SetIntersection added in v0.6.0

func SetIntersection[T comparable](first, second *Set[T]) *Set[T]

SetIntersection creates a new Set that holds the intersection of the items of the two specified sets.

func SetUnion added in v0.6.0

func SetUnion[T comparable](first, second *Set[T]) *Set[T]

SetUnion creates a new Set that is the union of the specified sets.

func (*Set[T]) Add

func (s *Set[T]) Add(item T) bool

Add adds the specified item to this Set if it was not present already. This method returns true if the operation was performed and false if the item was aleady present.

func (*Set[T]) AddSet added in v0.6.0

func (s *Set[T]) AddSet(other *Set[T]) bool

AddSet adds the items of another Set to this Set. The operation returns true if the operation resulted in a change and false otherwise.

func (*Set[T]) Clear

func (s *Set[T]) Clear()

Clear removes all items from this Set.

func (*Set[T]) Clip

func (s *Set[T]) Clip()

Clip removes unused capacity from the Set.

func (*Set[T]) Contains

func (s *Set[T]) Contains(item T) bool

Contains returns whether this Set holds the specified item.

func (*Set[T]) ContainsSet added in v0.6.0

func (s *Set[T]) ContainsSet(other *Set[T]) bool

ContainsSet returns whether this set fully contains another set.

func (*Set[T]) IsEmpty

func (s *Set[T]) IsEmpty() bool

IsEmpty returns whether this Set is empty.

func (*Set[T]) Items

func (s *Set[T]) Items() []T

Items returns a slice containing all of the items from this Set.

Note: The items are returned in a random order which can differ between subsequent calls.

func (*Set[T]) Remove

func (s *Set[T]) Remove(item T) bool

Remove removes the specified item from this Set. This method returns true if there was in fact such an item to be removed and false otherwise.

func (*Set[T]) RemoveSet added in v0.6.0

func (s *Set[T]) RemoveSet(other *Set[T]) bool

RemoveSet removes the items of another Set from this Set. The operation returns true if the operation resulted in a change and false otherwise.

func (*Set[T]) Size

func (s *Set[T]) Size() int

Size returns the number of items contained in this Set.

type Stack

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

Stack is an implementation of a stack data structure. The last inserted item is the first one to be removed (LIFO - last in, first out).

func NewStack

func NewStack[T any](initCapacity int) *Stack[T]

NewStack creates a new Stack instance with the specified initial capacity, which only serves to preallocate memory. Exceeding the initial capacity is allowed.

func (*Stack[T]) Clear

func (s *Stack[T]) Clear()

Clear removes all items from this Stack.

func (*Stack[T]) Clip

func (s *Stack[T]) Clip()

Clip removes unused capacity from the Stack.

func (*Stack[T]) IsEmpty

func (s *Stack[T]) IsEmpty() bool

IsEmpty returns true if there are no more items in this Stack.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() T

Peek returns the item that is at the top of the Stack without removing it. Make sure that the Stack is not empty, otherwise this method will panic.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() T

Pop removes the item from the top of this Stack and returns it. This function panics if there are no more items. Use IsEmpty to check for that.

func (*Stack[T]) Push

func (s *Stack[T]) Push(v T)

Push adds an item to the top of this Stack.

func (*Stack[T]) Size

func (s *Stack[T]) Size() int

Size returns the number of items stored in this Stack.

Jump to

Keyboard shortcuts

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