container

package
v0.20.2 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2022 License: ISC Imports: 6 Imported by: 6

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container added in v0.11.1

type Container[T any] interface {
	HasValues[T]
	// Add an element to the Container.
	Add(t T)
	// AddAll elements to the Container.
	AddAll(t ...T)
}

Container is a minimal container that HasValues and accepts additional elements.

type Deque

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

Deque is a doubly ended implementation of Queue with default behavior of a Fifo but provides left and right access. Employs a List for storage.

func NewDeque

func NewDeque[T any](t ...T) (degue *Deque[T])

NewDeque creates a Deque containing any provided elements.

func (*Deque[T]) Add

func (d *Deque[T]) Add(t T)

Add an element to the right of the Deque.

func (*Deque[T]) AddAll

func (d *Deque[T]) AddAll(t ...T)

AddAll elements to the right of the Deque.

func (*Deque[T]) AddLeft

func (d *Deque[T]) AddLeft(t T)

AddLeft an element to the left of the Deque.

func (*Deque[T]) AddRight

func (d *Deque[T]) AddRight(t T)

AddRight an element to the right of the Deque.

func (*Deque[T]) Iterator added in v0.19.1

func (d *Deque[T]) Iterator() Iterator[T]

func (*Deque[T]) Len

func (d *Deque[T]) Len() (length int)

Len reports the length of the Deque.

func (*Deque[T]) Peek

func (d *Deque[T]) Peek() (value T)

Peek returns the left most element in the Deque without removing it.

func (*Deque[T]) PeekLeft

func (d *Deque[T]) PeekLeft() (value T)

PeekLeft returns the left most element in the Deque without removing it.

func (*Deque[T]) PeekRight

func (d *Deque[T]) PeekRight() (value T)

PeekRight returns the right most element in the Deque without removing it.

func (*Deque[T]) Remove

func (d *Deque[T]) Remove() (value T)

Remove and return the left most element in the Deque.

func (*Deque[T]) RemoveLeft

func (d *Deque[T]) RemoveLeft() (value T)

RemoveLeft and return the left most element in the Deque.

func (*Deque[T]) RemoveRight

func (d *Deque[T]) RemoveRight() (value T)

RemoveRight and return the right most element in the Deque.

func (*Deque[T]) Values

func (d *Deque[T]) Values() (values GSlice[T])

Values in the Deque returned in a new GSlice.

type GMap

type GMap[K comparable, V any] map[K]V

GMap is a generic type employing the standard Go map and implementation Map.

func (GMap[K, V]) All added in v0.10.0

func (m GMap[K, V]) All(predicate genfuncs.Function[V, bool]) (ok bool)

All returns true if all values in GMap satisfy the predicate.

func (GMap[K, V]) Any added in v0.10.0

func (m GMap[K, V]) Any(predicate genfuncs.Function[V, bool]) (ok bool)

Any returns true if any values in GMap satisfy the predicate.

func (GMap[K, V]) Contains

func (m GMap[K, V]) Contains(key K) (isTrue bool)

Contains returns true if the GMap contains the given key.

Example
package main

import (
	"fmt"
	"github.com/nwillc/genfuncs/container"
)

var wordPositions = container.GMap[string, int]{"hello": 1, "world": 2}

func main() {
	fmt.Println(wordPositions.Contains("hello"))
	fmt.Println(wordPositions.Contains("no"))
}
Output:

true
false

func (GMap[K, V]) Delete added in v0.13.0

func (m GMap[K, V]) Delete(key K)

Delete an entry in the GMap.

func (GMap[K, V]) Filter added in v0.10.0

func (m GMap[K, V]) Filter(predicate genfuncs.Function[V, bool]) (result GMap[K, V])

Filter a GMap by a predicate, returning a new GMap that contains only values that satisfy the predicate.

func (GMap[K, V]) FilterKeys added in v0.10.0

func (m GMap[K, V]) FilterKeys(predicate genfuncs.Function[K, bool]) (result GMap[K, V])

FilterKeys returns a new GMap that contains only values whose key satisfy the predicate.

func (GMap[K, V]) ForEach added in v0.11.0

func (m GMap[K, V]) ForEach(action func(k K, v V))

ForEach performs the given action on each entry in the GMap.

func (GMap[K, V]) Get added in v0.13.0

func (m GMap[K, V]) Get(key K) (v V, ok bool)

Get returns an entry from the Map. The returned bool indicates if the key is in the Map.

func (GMap[K, V]) GetOrElse added in v0.12.1

func (m GMap[K, V]) GetOrElse(k K, defaultValue func() V) (value V)

GetOrElse returns the value at the given key if it exists or returns the result of defaultValue.

func (GMap[K, V]) Iterator added in v0.18.0

func (m GMap[K, V]) Iterator() Iterator[V]

Iterator creates an iterator for the values in the GMap.

func (GMap[K, V]) Keys

func (m GMap[K, V]) Keys() (keys GSlice[K])

Keys return a GSlice containing the keys of the GMap.

Example
package main

import (
	"fmt"
	"github.com/nwillc/genfuncs"
	"github.com/nwillc/genfuncs/container"
)

var wordPositions = container.GMap[string, int]{"hello": 1, "world": 2}

func main() {
	fmt.Println(wordPositions.Keys().SortBy(genfuncs.OrderedLess[string]))
}
Output:

[hello world]

func (GMap[K, V]) Len added in v0.11.1

func (m GMap[K, V]) Len() (length int)

Len is the number of elements in the GMap.

func (GMap[K, V]) Put added in v0.13.0

func (m GMap[K, V]) Put(key K, value V)

Put a key and value in the Map.

func (GMap[K, V]) Values

func (m GMap[K, V]) Values() (values GSlice[V])

Values returns a GSlice of all the values in the GMap.

Example
package main

import (
	"fmt"
	"github.com/nwillc/genfuncs/container"
)

var wordPositions = container.GMap[string, int]{"hello": 1, "world": 2}

func main() {
	wordPositions.Values().ForEach(func(_, i int) { fmt.Println(i) })
}
Output:

1
2

type GSlice

type GSlice[T any] []T

GSlice is a generic type corresponding to a standard Go slice that implements HasValues.

func (GSlice[T]) Filter

func (s GSlice[T]) Filter(predicate genfuncs.Function[T, bool]) GSlice[T]

Filter returns a slice containing only elements matching the given predicate.

Example
package main

import (
	"fmt"
	"github.com/nwillc/genfuncs"
	"github.com/nwillc/genfuncs/container"
)

var isGreaterThanZero = genfuncs.OrderedGreaterThan(0)

func main() {
	var values container.GSlice[int] = []int{1, -2, 2, -3}
	values.Filter(isGreaterThanZero).ForEach(func(_, i int) {
		fmt.Println(i)
	})
}
Output:

1
2

func (GSlice[T]) ForEach

func (s GSlice[T]) ForEach(action func(i int, t T))

ForEach element of the GSlice invoke given function with the element. Syntactic sugar for a range that intends to traverse all the elements, i.e. no exiting midway through.

func (GSlice[T]) Iterator added in v0.18.0

func (s GSlice[T]) Iterator() Iterator[T]

Iterator returns an Iterator that will iterate over the GSlice.

func (GSlice[T]) Len added in v0.11.1

func (s GSlice[T]) Len() int

Len is the number of elements in the GSlice.

func (GSlice[T]) Random

func (s GSlice[T]) Random() (t T)

Random returns a random element of the GSlice.

func (GSlice[T]) SortBy

func (s GSlice[T]) SortBy(order genfuncs.BiFunction[T, T, bool]) (sorted GSlice[T])

SortBy copies a slice, sorts the copy applying the Ordered and returns it. This is not a pure function, the GSlice is sorted in place, the returned slice is to allow for fluid calls in chains.

Example
package main

import (
	"fmt"
	"github.com/nwillc/genfuncs"
	"github.com/nwillc/genfuncs/container"
)

func main() {
	var numbers container.GSlice[int] = []int{1, 0, 9, 6, 0}
	fmt.Println(numbers)
	fmt.Println(numbers.SortBy(genfuncs.OrderedLess[int]))
}
Output:

[1 0 9 6 0]
[0 0 1 6 9]

func (GSlice[T]) Swap

func (s GSlice[T]) Swap(i, j int)

Swap two values in the slice.

Example
package main

import (
	"fmt"
	"github.com/nwillc/genfuncs"
	"github.com/nwillc/genfuncs/container"
)

var words container.GSlice[string] = []string{"hello", "world"}

func main() {
	words = words.SortBy(genfuncs.OrderedLess[string])
	words.Swap(0, 1)
	fmt.Println(words)
}
Output:

[world hello]

func (GSlice[T]) Values added in v0.11.1

func (s GSlice[T]) Values() (values GSlice[T])

Values is the GSlice itself.

type HasValues added in v0.11.1

type HasValues[T any] interface {
	// Len returns length of the Container.
	Len() int
	// Values returns a copy of the current values in the Container without modifying the contents.
	Values() GSlice[T]
}

HasValues is an interface that indicates a struct contains values that can counted and be retrieved.

type Heap

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

Heap implements an ordered heap of any type which can be min heap or max heap depending on the compare provided. Heap implements Queue.

func NewHeap

func NewHeap[T any](compare genfuncs.BiFunction[T, T, bool], values ...T) (heap *Heap[T])

NewHeap return a heap ordered based on the compare and adds any values provided.

Example
package main

import (
	"fmt"

	"github.com/nwillc/genfuncs"
	"github.com/nwillc/genfuncs/container"
)

func main() {
	heap := container.NewHeap[int](genfuncs.OrderedLess[int], 3, 1, 4, 2)
	for heap.Len() > 0 {
		fmt.Print(heap.Remove())
	}
	fmt.Println()
}
Output:

1234

func (*Heap[T]) Add

func (h *Heap[T]) Add(v T)

Add a value onto the heap.

func (*Heap[T]) AddAll

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

AddAll the values onto the Heap.

func (*Heap[T]) Len

func (h *Heap[T]) Len() (length int)

Len returns current length of the heap.

func (*Heap[T]) Peek

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

Peek returns the next element without removing it.

func (*Heap[T]) Remove

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

Remove an item off the heap.

func (*Heap[T]) Values

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

Values returns a slice of the values in the Heap in no particular order.

type Iterator added in v0.18.0

type Iterator[T any] interface {
	HasNext() bool
	Next() T
}

func NewListIterator added in v0.18.0

func NewListIterator[T any](list *List[T]) Iterator[T]

func NewSliceIterator added in v0.18.0

func NewSliceIterator[T any](slice []T) Iterator[T]

func NewValuesIterator added in v0.18.0

func NewValuesIterator[T any](values ...T) Iterator[T]

type List added in v0.13.1

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

List is a doubly linked list, inspired by list.List but reworked to be generic. List implements Container.

func NewList added in v0.13.1

func NewList[T any](values ...T) (l *List[T])

NewList instantiates a new List containing any values provided.

func (*List[T]) Add added in v0.13.1

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

Add a value to the right of the List.

func (*List[T]) AddAll added in v0.13.1

func (l *List[T]) AddAll(values ...T)

AddAll values to the right of the List.

func (*List[T]) AddLeft added in v0.13.1

func (l *List[T]) AddLeft(value T) (e *ListElement[T])

AddLeft adds a value to the left of the List.

func (*List[T]) AddRight added in v0.13.1

func (l *List[T]) AddRight(v T) (e *ListElement[T])

AddRight adds a value to the right of the List.

func (*List[T]) ForEach added in v0.13.2

func (l *List[T]) ForEach(action func(value T))

ForEach invokes the action for each value in the list.

func (*List[T]) Iterator added in v0.18.0

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

Iterator creates an Iterator for the List.

func (*List[T]) Len added in v0.13.1

func (l *List[T]) Len() (length int)

Len returns the number of values in the List.

func (*List[T]) PeekLeft added in v0.13.1

func (l *List[T]) PeekLeft() (e *ListElement[T])

PeekLeft returns the leftmost value in the List or nil if empty.

func (*List[T]) PeekRight added in v0.13.1

func (l *List[T]) PeekRight() (e *ListElement[T])

PeekRight returns the rightmost value in the List or nil if empty.

func (*List[T]) Remove added in v0.13.1

func (l *List[T]) Remove(e *ListElement[T]) (t T)

Remove removes a given value from the List.

func (*List[T]) SortBy added in v0.13.2

func (l *List[T]) SortBy(order genfuncs.BiFunction[T, T, bool]) (result *List[T])

SortBy sorts the List by the order of the order function. This is not a pure function, the List is sorted, the List returned is to allow for fluid call chains. List does not provide efficient indexed access so a Bubble sort is employed.

func (*List[T]) Values added in v0.13.1

func (l *List[T]) Values() (values GSlice[T])

Values returns the values in the list as a GSlice.

type ListElement added in v0.13.2

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

ListElement is an element of List.

func (*ListElement[T]) Next added in v0.13.2

func (e *ListElement[T]) Next() (next *ListElement[T])

Next returns the next list element or nil.

func (*ListElement[T]) Prev added in v0.13.2

func (e *ListElement[T]) Prev() (prev *ListElement[T])

Prev returns the previous list element or nil.

func (*ListElement[T]) Swap added in v0.14.1

func (e *ListElement[T]) Swap(e2 *ListElement[T])

Swap the values of two ListElements.

type Map

type Map[K comparable, V any] interface {
	HasValues[V]
	Sequence[V]
	Contains(key K) bool
	Delete(key K)
	Get(key K) (value V, ok bool)
	Put(key K, value V)
	ForEach(f func(key K, value V))
	Keys() GSlice[K]
}

Map interface to provide a polymorphic and generic interface to map implementations.

type MapSet

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

MapSet is a Set implementation based on a map. MapSet implements Set.

func (*MapSet[T]) Add

func (h *MapSet[T]) Add(t T)

Add element to MapSet.

func (*MapSet[T]) AddAll

func (h *MapSet[T]) AddAll(t ...T)

AddAll elements to MapSet.

func (*MapSet[T]) Contains

func (h *MapSet[T]) Contains(t T) (ok bool)

Contains returns true if MapSet contains element.

func (*MapSet[T]) Iterator added in v0.19.1

func (h *MapSet[T]) Iterator() Iterator[T]

Iterator returns an Iterator of the current state of the MapSet. This creates a copy of the data.

func (*MapSet[T]) Len

func (h *MapSet[T]) Len() (length int)

Len returns the length of the MapSet.

func (*MapSet[T]) Remove

func (h *MapSet[T]) Remove(t T)

Remove an element from the MapSet.

func (*MapSet[T]) Values

func (h *MapSet[T]) Values() (values GSlice[T])

Values returns the elements in the MapSet as a GSlice.

type Queue

type Queue[T any] interface {
	Container[T]
	// Peek returns the next element without removing it.
	Peek() T
	Remove() T
}

Queue is a container providing some define order when accessing elements. Queue implements Container.

type Sequence added in v0.18.0

type Sequence[T any] interface {
	Iterator() Iterator[T]
}

func NewIteratorSequence added in v0.18.0

func NewIteratorSequence[T any](iterator Iterator[T]) Sequence[T]

type Set

type Set[T comparable] interface {
	Container[T]
	Sequence[T]
	// Contains returns true if the Set contains a given element.
	Contains(t T) bool
	// Remove the element from the Set.
	Remove(T)
}

Set is a Container that contains no duplicate elements.

func NewMapSet

func NewMapSet[T comparable](t ...T) (set Set[T])

NewMapSet returns a new Set containing given values.

type SyncMap added in v0.13.0

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

SyncMap is a Map implementation employing sync.Map and is therefore GoRoutine safe.

func NewSyncMap added in v0.13.0

func NewSyncMap[K any, V any]() (syncMap *SyncMap[K, V])

NewSyncMap creates a new SyncMap instance.

func (*SyncMap[K, V]) Contains added in v0.13.0

func (s *SyncMap[K, V]) Contains(key K) (contains bool)

Contains returns true if the Map contains the given key.

func (*SyncMap[K, V]) Delete added in v0.13.0

func (s *SyncMap[K, V]) Delete(key K)

Delete an entry from the Map.

func (*SyncMap[K, V]) ForEach added in v0.13.0

func (s *SyncMap[K, V]) ForEach(f func(key K, value V))

ForEach traverses the Map applying the given function to all entries. The sync.Map's any types are cast to the appropriate types.

func (*SyncMap[K, V]) Get added in v0.13.0

func (s *SyncMap[K, V]) Get(key K) (value V, ok bool)

Get the value for the key. The sync.Map any type to cast to the appropriate type. The returned ok value will be false if the map is not contained in the Map.

func (*SyncMap[K, V]) GetAndDelete added in v0.13.0

func (s *SyncMap[K, V]) GetAndDelete(key K) (value V, ok bool)

GetAndDelete returns the value from the SyncMap corresponding to the key, returning it, and deletes it.

func (*SyncMap[K, V]) GetOrPut added in v0.13.0

func (s *SyncMap[K, V]) GetOrPut(key K, value V) (actual V, ok bool)

GetOrPut returns the existing value for the key if present. Otherwise, it puts and returns the given value. The ok result is true if the value was present, false if put.

func (*SyncMap[K, V]) Iterator added in v0.19.1

func (s *SyncMap[K, V]) Iterator() Iterator[V]

Iterator returns an iterator over a snapshot of the current values.

func (*SyncMap[K, V]) Keys added in v0.13.0

func (s *SyncMap[K, V]) Keys() (keys GSlice[K])

Keys returns the keys in the Map by traversing it and casting the sync.Map's any to the appropriate type.

func (*SyncMap[K, V]) Len added in v0.13.0

func (s *SyncMap[K, V]) Len() (length int)

Len returns the element count. This requires a traversal of the Map.

func (*SyncMap[K, V]) Put added in v0.13.0

func (s *SyncMap[K, V]) Put(key K, value V)

Put a key value pair into the Map.

func (*SyncMap[K, V]) Values added in v0.13.0

func (s *SyncMap[K, V]) Values() (values GSlice[V])

Values returns the values in the Map, The sync.Map any values is cast to the Map's type.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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