collection

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: MIT Imports: 1 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 (without Dictionary).

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

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

    • SyncSet: The thread safe implementation of Set based on sync.Map.

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

    • HashDict: The implementation of Dictionary based on Go built-in map structure.

    • SyncDict: The thread safe implementation of dictionary based on sync.Map.

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.

// import "github.com/ghosind/collection/set"

fruits := set.NewHashSet[string]()

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

log.Print(fruits.Contains("Banana")) // true
log.Print(fruits.Contains("Lemon"))
HashDictionary Examples
// import "github.com/ghosind/collection/dict"

languages := dict.NewHashDict[string, int]()

languages.Put("C", 1972)
languages.Put("Go", 2007)

log.Print(languages.GetDefault("C", 0)) // 1972

License

This project is licensed under the MIT License - see the LICENSE file for details.

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 {
	Iterable[T]

	// 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

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

	// 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

	// RemoveIf removes all of the elements of this collection that satisfy the given predicate.
	RemoveIf(f func(T) bool) bool

	// RetainAll retains only the elements in this collection that are contained in the specified
	// collection.
	RetainAll(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 Dict added in v0.3.0

type Dict[K comparable, V any] interface {
	Iterable2[K, V]

	// Clear removes all key-value pairs in this dictionary.
	Clear()

	// Clone returns a copy of this dictionary.
	Clone() Dict[K, V]

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

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

	// ForEach performs the given handler for each key-value pairs in the dictionary 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 dictionary contains no pair with the key.
	GetDefault(k K, defaultVal V) V

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

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

	// Put associate the specified value with the specified key in this dictionary.
	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 dictionary.
	Replace(k K, v V) (V, bool)

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

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

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

type Iterable added in v0.3.0

type Iterable[T any] interface {
	// Iter returns a channel of all elements in this collection.
	Iter() iter.Seq[T]
}

type Iterable2 added in v0.3.0

type Iterable2[K comparable, V any] interface {
	// Iter returns an iterator of all key-value pairs in this collection.
	Iter() iter.Seq2[K, V]
}

type List added in v0.3.0

type List[T any] interface {
	Collection[T]

	// AddAtIndex inserts the specified element to the specified position in this list.
	AddAtIndex(int, T)

	// Get returns the element at the specified position inn this list.
	Get(int) T

	// IndexOf returns the index of the first occurrence of the specified element in this list, or -1
	// if this list does not contain the element.
	IndexOf(T) int

	// LastIndexOf returns the index of the last occurrence of the specified element in this list, or
	// -1 if this list does not contain the element.
	LastIndexOf(T) int

	// RemoveAtIndex removes the element at the specified position in this list.
	RemoveAtIndex(int) T

	// Set replaces the element at the specified position in this list with the specified element.
	Set(int, T) T
}

List is an ordered collection.

type Set

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

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

	// 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
}

Set is a collection interface that contains no duplicate elements.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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