collection

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2023 License: MIT Imports: 3 Imported by: 0

README

collection

Test codecov Latest version License Badge Go Reference

Generics collections framework for Golang.

IMPORTANT NOTICE: This package requires Go version 1.18+.

Overview

This package provides the following data structure interfaces and implementations:

  • Collection: The root interface of most of the structures in this package.

  • Set: A collection interface that contains no duplicate elements.

    • HashSet: The implementation of Set based on Go built-in map structure.

    • ConcurrentHashSet: The thread safe implementation of Set based on Go built-in map structure.

  • Map: A object that maps keys to values, and it cannot contain duplicate key.

    • HashMap: The implementation of Map based on Go built-in map structure.

Installation

You can install this package by the following command.

go get -u github.com/ghosind/collection

After installation, you can import it by the following code.

import "github.com/ghosind/collection"

Examples

HashSet Examples

Create a string set, add and test elements in the set.

fruits := collection.NewHashSet[string]()

fruits.Add("Apple")
fruits.Add("Banana")

log.Print(fruits.Contains("Banana")) // true
log.Print(fruits.Contains("Lemon"))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection[T any] interface {
	// Add adds the specified element to this collection.
	Add(e T) bool

	// AddAll adds all of the elements in the this collection.
	AddAll(c ...T) bool

	// Clear removes all of the elements from this collection.
	Clear()

	// Contains returns true if this collection contains the specified element.
	Contains(e T) bool

	// ContainsAll returns true if this collection contains all of the elements in the specified collection.
	ContainsAll(c ...T) bool

	// Equals compares this collection with the object pass from parameter.
	Equals(o any) bool

	// ForEach performs the given handler for each elements in the collection until all elements have been processed or the handler returns an error.
	ForEach(func(e T) error) error

	// IsEmpty returns true if this collection contains no elements.
	IsEmpty() bool

	// Iter returns a channel of all elements in this collection.
	Iter() <-chan T

	// Remove removes the specified element from this collection.
	Remove(e T) bool

	// RemoveAll removes all of the elements in the specified collection from this collection.
	RemoveAll(c ...T) bool

	// Size returns the number of elements in this collection.
	Size() int

	// ToSlice returns a slice containing all of the elements in this collection.
	ToSlice() []T
}

Collection is the root interface for this collections framework hierarchy.

type ConcurrentHashSet added in v0.1.1

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

ConcurrentHashSet is a thread-safe set implementation that uses a Golang builtin map to store its elements.

func NewConcurrentHashSet added in v0.1.1

func NewConcurrentHashSet[T comparable]() *ConcurrentHashSet[T]

NewConcurrentHashSet creates and returns am empty ConcurrentHashSet with the specified type.

func (*ConcurrentHashSet[T]) Add added in v0.1.1

func (set *ConcurrentHashSet[T]) Add(e T) bool

Add adds the specified element to this set.

func (*ConcurrentHashSet[T]) AddAll added in v0.1.1

func (set *ConcurrentHashSet[T]) AddAll(c ...T) bool

AddAll adds all of the specified elements to this set.

func (*ConcurrentHashSet[T]) Clear added in v0.1.1

func (set *ConcurrentHashSet[T]) Clear()

Clear removes all of the elements from this set.

func (*ConcurrentHashSet[T]) Clone added in v0.1.1

func (set *ConcurrentHashSet[T]) Clone() Set[T]

Clone returns a copy of this set.

func (*ConcurrentHashSet[T]) Contains added in v0.1.1

func (set *ConcurrentHashSet[T]) Contains(e T) bool

Contains returns true if this set contains the specified element.

func (*ConcurrentHashSet[T]) ContainsAll added in v0.1.1

func (set *ConcurrentHashSet[T]) ContainsAll(c ...T) bool

ContainsAll returns true if this set contains all of the specified elements.

func (*ConcurrentHashSet[T]) Equals added in v0.1.1

func (set *ConcurrentHashSet[T]) Equals(o any) bool

Equals compares set with the object pass from parameter.

func (*ConcurrentHashSet[T]) ForEach added in v0.1.1

func (set *ConcurrentHashSet[T]) ForEach(handler func(e T) error) error

ForEach performs the given handler for each elements in the set until all elements have been processed or the handler returns an error.

func (*ConcurrentHashSet[T]) IsEmpty added in v0.1.1

func (set *ConcurrentHashSet[T]) IsEmpty() bool

IsEmpty returns true if this set contains no elements.

func (*ConcurrentHashSet[T]) Iter added in v0.1.1

func (set *ConcurrentHashSet[T]) Iter() <-chan T

Iter returns a channel of all elements in this set.

func (*ConcurrentHashSet[T]) Remove added in v0.1.1

func (set *ConcurrentHashSet[T]) Remove(e T) bool

Remove removes the specified element from this set.

func (*ConcurrentHashSet[T]) RemoveAll added in v0.1.1

func (set *ConcurrentHashSet[T]) RemoveAll(c ...T) bool

RemoveAll removes all of the specified elements from this set.

func (*ConcurrentHashSet[T]) Size added in v0.1.1

func (set *ConcurrentHashSet[T]) Size() int

Size returns the number of elements in this set.

func (*ConcurrentHashSet[T]) ToSlice added in v0.1.1

func (set *ConcurrentHashSet[T]) ToSlice() []T

ToSlice returns a slice containing all of the elements in this set.

type HashMap added in v0.1.2

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

HashMap is a Golang builtin map wrapper.

func NewHashMap added in v0.1.2

func NewHashMap[K comparable, V any]() *HashMap[K, V]

NewHashMap creates a new HashMap.

func (*HashMap[K, V]) Clear added in v0.1.2

func (m *HashMap[K, V]) Clear()

Clear removes all key-value pairs in this map.

func (*HashMap[K, V]) Clone added in v0.1.2

func (m *HashMap[K, V]) Clone() Map[K, V]

Clone returns a copy of this map.

func (*HashMap[K, V]) ContainsKey added in v0.1.2

func (m *HashMap[K, V]) ContainsKey(k K) bool

ContainsKey returns true if this map contains a key-value pair with the specified key.

func (*HashMap[K, V]) Equals added in v0.1.2

func (m *HashMap[K, V]) Equals(o any) bool

Equals compares this map with the object pass from parameter.

func (*HashMap[K, V]) ForEach added in v0.1.2

func (m *HashMap[K, V]) ForEach(handler func(K, V) error) error

ForEach performs the given handler for each key-value pairs in the map until all pairs have been processed or the handler returns an error.

func (*HashMap[K, V]) Get added in v0.1.2

func (m *HashMap[K, V]) Get(k K) (V, bool)

Get returns the value which associated to the specified key.

func (*HashMap[K, V]) GetDefault added in v0.1.2

func (m *HashMap[K, V]) GetDefault(k K, defaultVal V) V

GetDefault returns the value associated with the specified key, and returns the default value if this map contains no pair with the key.

func (*HashMap[K, V]) IsEmpty added in v0.1.2

func (m *HashMap[K, V]) IsEmpty() bool

IsEmpty returns true if this map is empty.

func (*HashMap[K, V]) Keys added in v0.1.2

func (m *HashMap[K, V]) Keys() []K

Keys returns a slice that contains all the keys in this map.

func (*HashMap[K, V]) Put added in v0.1.2

func (m *HashMap[K, V]) Put(k K, v V) V

Put associate the specified value with the specified key in this map.

func (*HashMap[K, V]) Remove added in v0.1.2

func (m *HashMap[K, V]) Remove(k K) V

Remove removes the key-value pair with the specified key.

func (*HashMap[K, V]) Replace added in v0.1.2

func (m *HashMap[K, V]) Replace(k K, v V) (V, bool)

Replace replaces the value for the specified key only if it is currently in this map.

func (*HashMap[K, V]) Size added in v0.1.2

func (m *HashMap[K, V]) Size() int

Size returns the number of key-value pairs in this map.

func (*HashMap[K, V]) Values added in v0.1.2

func (m *HashMap[K, V]) Values() []V

Values returns a slice that contains all the values in this map.

type HashSet

type HashSet[T comparable] map[T]struct{}

HashSet is a set implementation that uses a Golang builtin map to store its elements.

func NewHashSet

func NewHashSet[T comparable]() *HashSet[T]

NewHashSet creates a new HashSet.

func (*HashSet[T]) Add

func (set *HashSet[T]) Add(e T) bool

Add adds the specified element to this set.

func (*HashSet[T]) AddAll

func (set *HashSet[T]) AddAll(c ...T) bool

AddAll adds all of the specified elements to this set.

func (*HashSet[T]) Clear

func (set *HashSet[T]) Clear()

Clear removes all of the elements from this set.

func (*HashSet[T]) Clone

func (set *HashSet[T]) Clone() Set[T]

Clone returns a copy of this set.

func (*HashSet[T]) Contains

func (set *HashSet[T]) Contains(e T) bool

Contains returns true if this set contains the specified element.

func (*HashSet[T]) ContainsAll

func (set *HashSet[T]) ContainsAll(c ...T) bool

ContainsAll returns true if this set contains all of the specified elements.

func (*HashSet[T]) Equals

func (set *HashSet[T]) Equals(o any) bool

Equals compares set with the object pass from parameter.

func (*HashSet[T]) ForEach

func (set *HashSet[T]) ForEach(handler func(e T) error) error

ForEach performs the given handler for each elements in the set until all elements have been processed or the handler returns an error.

func (*HashSet[T]) IsEmpty

func (set *HashSet[T]) IsEmpty() bool

IsEmpty returns true if this set contains no elements.

func (*HashSet[T]) Iter added in v0.1.1

func (set *HashSet[T]) Iter() <-chan T

Iter returns a channel of all elements in this set.

func (*HashSet[T]) Remove

func (set *HashSet[T]) Remove(e T) bool

Remove removes the specified element from this set.

func (*HashSet[T]) RemoveAll

func (set *HashSet[T]) RemoveAll(c ...T) bool

RemoveAll removes all of the specified elements from this set.

func (*HashSet[T]) Size

func (set *HashSet[T]) Size() int

Size returns the number of elements in this set.

func (*HashSet[T]) ToSlice

func (set *HashSet[T]) ToSlice() []T

ToSlice returns a slice containing all of the elements in this set.

type Map added in v0.1.2

type Map[K comparable, V any] interface {
	// Clear removes all key-value pairs in this map.
	Clear()

	// Clone returns a copy of this map.
	Clone() Map[K, V]

	// ContainsKey returns true if this map contains a key-value pair with the specified key.
	ContainsKey(k K) bool

	// Equals compares this map with the object pass from parameter.
	Equals(o any) bool

	// ForEach performs the given handler for each key-value pairs in the map until all pairs have
	// been processed or the handler returns an error.
	ForEach(handler func(k K, v V) error) error

	// Get returns the value which associated to the specified key.
	Get(k K) (V, bool)

	// GetDefault returns the value associated with the specified key, and returns the default value
	// if this map contains no pair with the key.
	GetDefault(k K, defaultVal V) V

	// IsEmpty returns true if this map is empty.
	IsEmpty() bool

	// Keys returns a slice that contains all the keys in this map.
	Keys() []K

	// Put associate the specified value with the specified key in this map.
	Put(k K, v V) V

	// Remove removes the key-value pair with the specified key.
	Remove(k K) V

	// Replace replaces the value for the specified key only if it is currently in this map.
	Replace(k K, v V) (V, bool)

	// Size returns the number of key-value pairs in this map.
	Size() int

	// Values returns a slice that contains all the values in this map.
	Values() []V
}

Map is a object that maps keys to values, and it cannot contain duplicate key.

type Set

type Set[T comparable] interface {
	Collection[T]

	// Clone returns a copy of this set.
	Clone() Set[T]
}

Set is a collection interface that contains no duplicate elements.

Jump to

Keyboard shortcuts

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