collections

package module
v0.0.0-...-688668c Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2023 License: Unlicense Imports: 3 Imported by: 0

README

Collections

Generics-based, collection based data structures. Some of the collections provide thread-safe versions.

These data structures are based on Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. All credit for the algorithms goes to the authors.

The package currently has the following data structures (all generics-based):

  • MaxPQ - A basic max heap.
  • ConcurrentMaxPQ - Thread-safe max heap.
  • RBTree - An implementation of a red-black tree.
  • Set - A basic implementation of a collection with properties of a Set and allowing Set operations.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Color

type Color int
const (
	Red Color = iota
	Black
)

type Comparable

type Comparable interface {
	int64 | int | float64 | uint | uint64 | string
}

type Comparator

type Comparator[T comparable] func(o1, o2 T) int

Comparator is a function that compares two objects o1 and o2 of type T and returns an integer. The comparator is used by the Heap to compare custom objects (non primitives).

It should return a -ve number if o1 < o2, 0 if o1 == o2 and +ve number if o1 > o2.

type CompareFn

type CompareFn[K Comparable] func(key1, key2 K) int

type ConcurrentMaxPQ

type ConcurrentMaxPQ[T comparable] struct {
	*MaxPQ[T]
	// contains filtered or unexported fields
}

ConcurrentMaxPQ is the thread-safe version of MaxPQ. All operations of this type are thread-safe.

func NewConcurrentMaxPQ

func NewConcurrentMaxPQ[T comparable](capacity uint, compareFn Comparator[T]) *ConcurrentMaxPQ[T]

func (*ConcurrentMaxPQ[T]) DelMax

func (pq *ConcurrentMaxPQ[T]) DelMax() T

DelMax returns the current max element and deletes it from the collection.

func (*ConcurrentMaxPQ[T]) Insert

func (pq *ConcurrentMaxPQ[T]) Insert(item T)

Insert inserts the given element in the collection.

func (*ConcurrentMaxPQ[T]) PeekMax

func (pq *ConcurrentMaxPQ[T]) PeekMax() T

PeekMax returns the current max/head element.

type MaxPQ

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

MaxPQ is an implementation of Max Heap with generics support.

The implementation is not thread-safe. For a thread-safe implementation, use ConcurentMaxPQ.

func NewMaxPQ

func NewMaxPQ[T comparable](capacity uint, compareFn Comparator[T]) *MaxPQ[T]

func (*MaxPQ[T]) DelMax

func (pq *MaxPQ[T]) DelMax() T

DelMax returns the current max element and deletes it from the collection.

func (*MaxPQ[T]) Insert

func (pq *MaxPQ[T]) Insert(item T)

Insert inserts a new element in the collection and moves it to the correct position.

func (*MaxPQ[T]) PeekMax

func (pq *MaxPQ[T]) PeekMax() T

PeekMax returns the current max/head element.

type Node

type Node[K Comparable, V any] struct {
	// contains filtered or unexported fields
}

func (*Node[K, V]) Key

func (n *Node[K, V]) Key() K

func (*Node[K, V]) Val

func (n *Node[K, V]) Val() V

type RBTree

type RBTree[K Comparable, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K Comparable, V any]() *RBTree[K, V]

func NewWithComparator

func NewWithComparator[K Comparable, V any](compareFn CompareFn[K]) *RBTree[K, V]

func (*RBTree[K, V]) Delete

func (t *RBTree[K, V]) Delete(key K)

func (*RBTree[K, V]) DeleteMax

func (t *RBTree[K, V]) DeleteMax()

func (*RBTree[K, V]) DeleteMin

func (t *RBTree[K, V]) DeleteMin()

func (*RBTree[K, V]) Get

func (t *RBTree[K, V]) Get(key K) (V, bool)

func (*RBTree[K, V]) Has

func (t *RBTree[K, V]) Has(key K) bool

func (*RBTree[K, V]) Height

func (t *RBTree[K, V]) Height() int

func (*RBTree[K, V]) IsEmpty

func (t *RBTree[K, V]) IsEmpty() bool

func (*RBTree[K, V]) Keys

func (t *RBTree[K, V]) Keys() []K

func (*RBTree[K, V]) Max

func (t *RBTree[K, V]) Max() *Node[K, V]

func (*RBTree[K, V]) Min

func (t *RBTree[K, V]) Min() *Node[K, V]

func (*RBTree[K, V]) Put

func (t *RBTree[K, V]) Put(key K, val V)

func (*RBTree[K, V]) Rank

func (t *RBTree[K, V]) Rank(key K) int

func (*RBTree[K, V]) Size

func (t *RBTree[K, V]) Size() int

type Set

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

Set provides a collection with no duplicates.

func FromArray

func FromArray[T comparable](a []T) *Set[T]

FromArray creates a new set from the given array/slice.

func NewSet

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

NewSet creates and returns a new set with the given initial capacity.

func (*Set[T]) Add

func (s *Set[T]) Add(e T)

Add adds the given element to the set. If the element already exists, it is a no-op.

func (*Set[T]) Contains

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

Contains returns true if the given element is already present in the current set, otherwise returns false.

func (*Set[T]) Difference

func (s *Set[T]) Difference(s2 *Set[T]) *Set[T]

Difference returns a new set with elements that are in the current set but not in s2.

func (*Set[T]) Index

func (s *Set[T]) Index(e T) int

Index returns the index of the given element in the set. Returns -1 if the element is not present.

func (*Set[T]) Intersection

func (s *Set[T]) Intersection(s2 *Set[T]) *Set[T]

Intersection returns a new set with elements that are present in both the sets.

func (*Set[T]) IsDisjoint

func (s *Set[T]) IsDisjoint(s2 *Set[T]) bool

IsDisjoint returns true if there are no common elements between this set and the given set, else returns true.

func (*Set[T]) IsSubsetOf

func (s *Set[T]) IsSubsetOf(s2 *Set[T]) bool

IsSubsetOf returns true if this set is a subset of the given set.

func (*Set[T]) Items

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

Items returns the elements of the set as a slice.

func (*Set[T]) Size

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

Size returns the number of elements in the current set.

func (*Set[T]) Union

func (s *Set[T]) Union(s2 *Set[T])

Union adds all elements from s2 to the current set which are not present in the current set.

Jump to

Keyboard shortcuts

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