Convert Figma logo to code with AI

zyedidia logogeneric

A collection of generic data structures written in Go.

1,285
77
1,285
7

Top Related Projects

A simple, battle-tested and generic set type for the Go language. Trusted by Docker, 1Password, Ethereum and Hashicorp.

16,091

GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more

17,335

💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)

4,744

A modern Go utility library which provides helpers (map, find, contains, filter, ...)

3,500

.NET LINQ capabilities in Go

Quick Overview

The zyedidia/generic repository is a Go library that provides generic data structures and algorithms. It aims to offer efficient, type-safe implementations of common data structures like trees, heaps, and sets, as well as algorithms such as sorting and searching, all while leveraging Go's generics feature.

Pros

  • Type-safe implementations of common data structures and algorithms
  • Efficient implementations optimized for performance
  • Leverages Go's generics feature for improved code reusability
  • Well-documented with clear examples and usage instructions

Cons

  • Requires Go 1.18 or later due to the use of generics
  • May have a learning curve for developers unfamiliar with generic programming
  • Limited to structures and algorithms currently implemented in the library

Code Examples

  1. Creating and using a binary heap:
import "github.com/zyedidia/generic/heap"

h := heap.New(heap.Less[int])
h.Push(5)
h.Push(3)
h.Push(7)
fmt.Println(h.Pop()) // Output: 3
  1. Using a red-black tree:
import "github.com/zyedidia/generic/tree/rbtree"

tree := rbtree.New[int, string](rbtree.Less[int])
tree.Insert(5, "five")
tree.Insert(3, "three")
fmt.Println(tree.Get(3)) // Output: three
  1. Sorting a slice using quicksort:
import "github.com/zyedidia/generic/sort"

slice := []int{5, 2, 8, 1, 9}
sort.Quicksort(slice, sort.Less[int])
fmt.Println(slice) // Output: [1 2 5 8 9]

Getting Started

To use the zyedidia/generic library in your Go project:

  1. Ensure you have Go 1.18 or later installed.
  2. Add the library to your project:
    go get github.com/zyedidia/generic
    
  3. Import the desired package in your Go file:
    import "github.com/zyedidia/generic/heap"
    
  4. Use the data structures or algorithms as shown in the code examples above.

Competitor Comparisons

A simple, battle-tested and generic set type for the Go language. Trusted by Docker, 1Password, Ethereum and Hashicorp.

Pros of golang-set

  • Specialized for set operations, providing a focused and optimized implementation
  • Extensive set-specific methods like Union, Intersection, and Difference
  • Well-established project with a longer history and more contributors

Cons of golang-set

  • Limited to set data structures, less versatile for other generic use cases
  • Requires type assertions when working with interface{} elements
  • May have slightly higher memory usage due to map-based implementation

Code Comparison

golang-set:

set := mapset.NewSet()
set.Add("apple")
set.Add("banana")
fmt.Println(set.Contains("apple")) // true

generic:

set := generic.NewSet[string]()
set.Add("apple")
set.Add("banana")
fmt.Println(set.Has("apple")) // true

Key Differences

  • generic provides a more flexible approach, supporting various data structures beyond sets
  • golang-set focuses solely on set operations, offering more specialized functionality
  • generic uses Go 1.18+ generics, while golang-set uses interface{} for type flexibility
  • generic's API is more concise, with methods like Has instead of Contains
  • golang-set offers more set-specific operations out of the box

Both libraries have their merits, with golang-set being more suitable for projects requiring extensive set operations, while generic offers a broader range of generic data structure implementations.

16,091

GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more

Pros of gods

  • More comprehensive collection of data structures and algorithms
  • Better documentation with examples and benchmarks
  • Higher number of stars and contributors, indicating wider adoption

Cons of gods

  • Less focus on generic programming concepts
  • May have more overhead due to broader scope
  • Not as actively maintained (last commit over a year ago)

Code Comparison

gods:

set := treeset.NewWithIntComparator()
set.Add(1)
set.Add(2)
set.Add(3)

generic:

set := generic.NewSet[int]()
set.Add(1)
set.Add(2)
set.Add(3)

Summary

gods offers a more extensive collection of data structures and algorithms with better documentation, making it suitable for developers who need a wide range of ready-to-use implementations. It has gained more popularity in the Go community.

generic focuses on providing generic programming capabilities, which can lead to more flexible and reusable code. It has a more active development cycle and may be more suitable for projects that prioritize type safety and code reusability.

Both libraries offer similar functionality for common data structures, but their implementation approaches differ. The choice between them depends on specific project requirements and preferences for generic programming vs. a more traditional approach.

17,335

💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)

Pros of lo

  • More comprehensive set of utility functions, covering a wider range of use cases
  • Better documentation and examples, making it easier for developers to understand and use
  • Active development and maintenance, with frequent updates and improvements

Cons of lo

  • Larger codebase and dependency footprint, potentially impacting project size
  • May have a steeper learning curve due to the extensive API surface
  • Potentially slower performance for some operations compared to generic's more focused approach

Code Comparison

lo:

result := lo.Map([]int{1, 2, 3, 4}, func(x int, _ int) string {
    return strconv.Itoa(x)
})

generic:

result := generic.Map([]int{1, 2, 3, 4}, func(x int) string {
    return strconv.Itoa(x)
})

Both libraries provide similar functionality for common operations like mapping, but lo offers a more extensive set of utilities beyond basic collection manipulation. generic focuses on core generic algorithms with a smaller, more focused API. The choice between the two depends on the specific needs of your project and the balance between functionality and simplicity you're aiming for.

4,744

A modern Go utility library which provides helpers (map, find, contains, filter, ...)

Pros of go-funk

  • More comprehensive set of utility functions, covering a wider range of use cases
  • Better documentation and examples for each function
  • Actively maintained with regular updates and contributions

Cons of go-funk

  • Relies on reflection, which can impact performance for large datasets
  • Less type-safe compared to generic's approach, as it uses interface{} for flexibility
  • May require type assertions when working with specific types

Code Comparison

go-funk:

result := funk.Filter([]int{1, 2, 3, 4}, func(x int) bool {
    return x%2 == 0
})

generic:

result := generic.Filter([]int{1, 2, 3, 4}, func(x int) bool {
    return x%2 == 0
})

Both libraries provide similar functionality, but generic uses Go's generics for type safety, while go-funk uses reflection. The generic approach offers better performance and compile-time type checking, whereas go-funk provides more flexibility at the cost of runtime type assertions and potential performance overhead.

go-funk offers a broader range of utility functions, making it suitable for projects requiring diverse operations on collections. generic, on the other hand, focuses on core functional programming concepts with a more streamlined API, prioritizing type safety and performance.

3,500

.NET LINQ capabilities in Go

Pros of go-linq

  • More mature and established project with a larger user base
  • Provides a wider range of LINQ-like operations for data manipulation
  • Offers better compatibility with existing Go code and idiomatic Go practices

Cons of go-linq

  • Relies on reflection, which can impact performance in some scenarios
  • Less type-safe compared to generic's approach, as it operates on interface{} types
  • May require more type assertions when working with specific data types

Code Comparison

go-linq:

From(users).Where(func(u interface{}) bool {
    return u.(User).Age > 18
}).Select(func(u interface{}) interface{} {
    return u.(User).Name
}).ToSlice(&result)

generic:

slice.Filter(users, func(u User) bool {
    return u.Age > 18
}).Map(func(u User) string {
    return u.Name
})

The generic library provides a more type-safe approach with less boilerplate, while go-linq offers a wider range of operations but requires type assertions. generic leverages Go's generics feature, resulting in cleaner and more readable code. However, go-linq's flexibility and extensive feature set may be preferred for complex data manipulation tasks.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Generic Data Structures

Test Workflow Go Report Card Go Reference MIT License

This package implements some generic data structures.

  • array2d: a 2-dimensional array.
  • avl: an AVL tree.
  • bimap: a bi-directional map; a map that allows lookups on both keys and values.
  • btree: a B-tree.
  • cache: a wrapper around map[K]V that uses a maximum size and evicts elements using LRU when full.
  • hashmap: a hashmap with linear probing. The main feature is that the hashmap can be efficiently copied, using copy-on-write under the hood.
  • hashset: a hashset that uses the hashmap as the underlying storage.
  • heap: a binary heap.
  • interval: an interval tree, implemented as an augmented AVL tree.
  • list: a doubly-linked list.
  • mapset: a set that uses Go's built-in map as the underlying storage.
  • multimap: an associative container that permits multiple entries with the same key.
  • queue: a First In First Out (FIFO) queue.
  • rope: a generic rope, which is similar to an array but supports efficient insertion and deletion from anywhere in the array. Ropes are typically used for arrays of bytes, but this rope is generic.
  • prope: a persistent version of the rope, which allows for keeping different versions of the rope with only a little extra time or memory.
  • stack: a LIFO stack.
  • trie: a ternary search trie.
  • ulist: an un-rolled doubly-linked list.

See each subpackage for documentation and examples. The top-level generic package provides some useful types and constraints. See DOC.md for documentation.

Contributing

If you would like to contribute a new feature, please let me know first what you would like to add (via email or issue tracker). Here are some ideas:

  • New data structures (bloom filters, graph structures, concurrent data structures, adaptive radix tree, or other kinds of search trees).
  • Benchmarks, and optimization of the existing data structures based on those benchmarks. The hashmap is an especially good target.
  • Design and implement a nice iterator API.
  • Improving tests (perhaps we can use Go's new fuzzing capabilities).