ds

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 2 Imported by: 8

Documentation

Overview

Package ds provides various data structure implementations with generics.

Prior to Go 1.18, such data structures could not be implemented in a satisfactory way, as one had to use interfaces, which ofter resulted in unreadable code, memory allocations and conditions for unexpected panics.

Now that Go has generics, this package provides some common data structures that make use of this new language feature.

Index

Examples

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.

Example
package main

import (
	"fmt"

	"github.com/mokiat/gog/ds"
)

func main() {
	heap := ds.NewHeap(0, func(a, b int) bool {
		return a < b
	})
	heap.Push(100)
	heap.Push(20)
	heap.Push(50)
	heap.Push(13)
	heap.Push(300)

	fmt.Println(heap.Pop())
	fmt.Println(heap.Pop())
	fmt.Println(heap.Pop())
	fmt.Println(heap.Pop())
	fmt.Println(heap.Pop())

}
Output:

13
20
50
100
300

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.

Example
package main

import (
	"fmt"

	"github.com/mokiat/gog/ds"
)

func main() {
	list := ds.NewList[string](0)
	list.Add("first")
	list.Add("second")
	list.Add("third")
	list.Add("fourth")
	list.Remove("second")
	list.Remove("third")
	fmt.Printf("%#v\n", list.Items())

}
Output:

[]string{"first", "fourth"}

func ListFromSlice added in v0.11.0

func ListFromSlice[T comparable](items []T) *List[T]

ListFromSlice constructs a new List that is based on the items from the specified slice.

It is safe to modify the slice afterwards, as the list creates its own internal copy.

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]) Equals added in v0.11.0

func (l *List[T]) Equals(other *List[T]) bool

Equals returns whether this list matches exactly the provided list.

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 outside the list bounds.

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

Items returns all items stored in this List as a slice. It is safe to mutate the returned slice as it is a copy of the inner representation.

If performance is needed, consider using Unbox method instead.

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]) Set added in v0.11.0

func (l *List[T]) Set(index int, value T)

Set modifies the item at the specified index.

This method will panic if the index is outside the list bounds.

func (*List[T]) Size

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

Size returns the number of items contained in this List.

func (*List[T]) Unbox added in v0.11.0

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

Unbox provides direct access to the inner representation of the list. The returned slice should not be modified, otherwise there is a risk that the List might not work correctly afterwards. Even if it works now, a future version might break that behavior.

This method should only be used when performance is critical and memory allocation is not desired.

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.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/mokiat/gog/ds"
)

func main() {
	pool := ds.NewPool[bytes.Buffer]()

	buffer := pool.Fetch()
	buffer.Reset()
	buffer.WriteString("this buffer is mine")
	pool.Restore(buffer)

	newBuffer := pool.Fetch()
	// newBuffer.Reset() // commented out to show that the instance is reused
	fmt.Println(newBuffer.String())

}
Output:

this buffer is mine

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.

Example
package main

import (
	"fmt"

	"github.com/mokiat/gog/ds"
)

func main() {
	set := ds.NewSet[int](0)
	set.Add(2)
	set.Add(3)
	set.Add(5)
	set.Add(7)
	fmt.Println(set.Contains(2))
	fmt.Println(set.Contains(10))
	fmt.Println(set.Contains(5))
	fmt.Println(set.Contains(8))

}
Output:

true
false
true
false

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]) Equals added in v0.11.0

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

Equals return whether this set is equal to the provided 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.

func (*Set[T]) Unbox added in v0.11.0

func (s *Set[T]) Unbox() map[T]struct{}

Unbox provides direct access to the inner representation of the set. The returned map should not be modified, otherwise there is a risk that the Set might not work correctly afterwards. Even if it works now, a future version might break that behavior.

This method should only be used when performance is critical and memory allocation is not desired.

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).

Example
package main

import (
	"fmt"

	"github.com/mokiat/gog/ds"
)

func main() {
	stack := ds.NewStack[string](3)
	stack.Push("first")
	stack.Push("second")
	stack.Push("third")
	fmt.Println(stack.Pop())
	fmt.Println(stack.Pop())
	fmt.Println(stack.Pop())

}
Output:

third
second
first

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