Collections is a package that provides a collection of generic data structures, including Stack, Queue, List, Set, and their synchronized versions. This package is designed to be type-safe and easy to use with Go's generics.
To install the package, use the following command:
go get github.com/nandlabs/golly/collections
The Collection
interface is a generic interface that defines a collection of elements with various methods to manipulate them. It uses a type parameter T
to represent the type of elements stored in the collection.
Add(elem T) error
: Adds an element to the collection.AddAll(coll Collection[T]) error
: Adds all elements from another collection to this collection.Clear()
: Removes all elements from the collection.Contains(elem T) bool
: Checks if an element is in the collection.IsEmpty() bool
: Returns true if the collection is empty.Remove(elem T) bool
: Removes an element from the collection.Size() int
: Returns the number of elements in the collection.
A stack is a LIFO (Last In, First Out) data structure. The package provides both a basic and a synchronized stack implementation.
package main
import (
"fmt"
"github.com/nandlabs/golly/collections"
)
func main() {
stack := collections.NewStack[int]()
stack.Push(1)
stack.Push(2)
stack.Push(3)
fmt.Println(stack.Pop()) // Output: 3
fmt.Println(stack.Peek()) // Output: 2
}
package main
import (
"fmt"
"github.com/nandlabs/golly/collections"
)
func main() {
stack := collections.NewSyncStack[int]()
stack.Push(1)
stack.Push(2)
stack.Push(3)
fmt.Println(stack.Pop()) // Output: 3
fmt.Println(stack.Peek()) // Output: 2
}
A queue is a FIFO (First In, First Out) data structure. The package provides both a basic and a synchronized queue implementation.
package main
import (
"fmt"
"github.com/nandlabs/golly/collections"
)
func main() {
queue := collections.NewArrayQueue[int]()
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
fmt.Println(queue.Dequeue()) // Output: 1
fmt.Println(queue.Front()) // Output: 2
}
package main
import (
"fmt"
"github.com/nandlabs/golly/collections"
)
func main() {
queue := collections.NewSyncQueue[int]()
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
fmt.Println(queue.Dequeue()) // Output: 1
fmt.Println(queue.Front()) // Output: 2
}
A list is a collection of elements that can be accessed by index. The package provides both a basic and a synchronized list implementation.
package main
import (
"fmt"
"github.com/nandlabs/golly/collections"
)
func main() {
list := collections.NewArrayList[int]()
list.Add(1)
list.Add(2)
list.Add(3)
fmt.Println(list.Get(0)) // Output: 1
fmt.Println(list.GetLast()) // Output: 3
}
package main
import (
"fmt"
"github.com/nandlabs/golly/collections"
)
func main() {
list := collections.NewSyncedArrayList[int]()
list.Add(1)
list.Add(2)
list.Add(3)
fmt.Println(list.Get(0)) // Output: 1
fmt.Println(list.GetLast()) // Output: 3
}
A set is a collection of unique elements. The package provides both a basic and a synchronized set implementation.
package main
import (
"fmt"
"github.com/nandlabs/golly/collections"
)
func main() {
set := collections.NewHashSet[int]()
set.Add(1)
set.Add(2)
set.Add(3)
fmt.Println(set.Contains(2)) // Output: true
fmt.Println(set.Size()) // Output: 3
}
package main
import (
"fmt"
"github.com/nandlabs/golly/collections"
)
func main() {
set := collections.NewSyncSet[int]()
set.Add(1)
set.Add(2)
set.Add(3)
fmt.Println(set.Contains(2)) // Output: true
fmt.Println(set.Size()) // Output: 3
}
The ArrayList
is a generic list implementation using an array. It provides methods to add, remove, and access elements by index.
The LinkedList
is a generic list implementation using a linked list. It provides methods to add, remove, and access elements by index.
The HashSet
is a generic set implementation using a hash map. It provides methods to add, remove, and check for the presence of elements.
Each data structure has a synchronized version that is thread-safe. These synchronized versions use mutexes to ensure safe concurrent access.