set

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

README

Go Generic Set

A simple, lightweight, and type-safe implementation of Set data structures in Go, built with generics.

This repository provides two common set implementations: a standard unordered set for high performance and an ordered set that preserves the insertion order of elements.

✨ Features

  • Type-Safe & Generic: Built using Go 1.18+ generics, it works with any comparable type (e.g., string, int, float64).
  • Two Implementations:
    • GenericDataSet: An unordered set optimized for fast O(1) lookups, additions, and deletions.
    • GenericOrderedDataSet: An ordered set that maintains the original insertion order of its elements.
  • Core Set API: Provides essential methods like Add, Delete, Contains, Union (for unordered sets), Count, and ToSlice.
  • Well-Tested: Includes a comprehensive suite of unit and fuzz tests to ensure reliability.
  • Lightweight: Zero external dependencies.

🚀 Installation

go get github.com/your-username/go-generic-set

💡 Usage

Unordered Set (GenericDataSet)

Use this when you need maximum performance for membership testing and don't care about the order of elements.

package main

import (
	"fmt"
	
	"github.com/FrogoAI/set"
)

func main() {
	// Create a new set of integers
	s1 := set.NewGenericDataSet(1, 2, 3, 3, 4)

	// Add an element
	s1.Add(5)

	// Test for membership
	fmt.Printf("Set contains 3: %v\n", s1.Contains(3)) // true
	fmt.Printf("Set contains 99: %v\n", s1.Contains(99)) // false

	// Perform a union operation
	s2 := set.NewGenericDataSet(5, 6, 7)
	s1.Union(s2)

	// Convert to a slice (order is not guaranteed)
	fmt.Println("Slice:", s1.ToSlice()) // e.g., [1 2 3 4 5 6 7]
	fmt.Println("Count:", s1.Count())   // 7
}
Ordered Set (GenericOrderedDataSet)

Use this when you need to preserve the order in which elements were added to the set.

package main

import (
	"fmt"
	"github.com/FrogoAI/set"
)

func main() {
	// Create a new ordered set of strings
	orderedSet := set.NewGenericOrderedDataSet("apple", "banana", "cherry")

	// Adding a duplicate element does not change the order
	orderedSet.Add("banana")

	// Add a new element
	orderedSet.Add("date")

	// Delete an element
	orderedSet.Delete("banana")
    
	// Get the last element
	fmt.Println("Last element:", orderedSet.Last()) // date

	// The slice preserves the original insertion order
	// Output: [apple cherry date]
	fmt.Println("Ordered Slice:", orderedSet.ToSlice()) 
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GenericDataSet

type GenericDataSet[K comparable] map[K]struct{}

GenericDataSet data set support any string or number as key It can test for in O(1) time

func NewGenericDataSet

func NewGenericDataSet[K comparable](input ...K) GenericDataSet[K]

NewGenericDataSet returns a new GenericDataSet from the given data

func (GenericDataSet[K]) Add

func (set GenericDataSet[K]) Add(key K)

Add inserts the given value into the set

func (GenericDataSet[K]) Contains

func (set GenericDataSet[K]) Contains(key K) (exists bool)

Contains tests the membership of given key in the set

func (GenericDataSet[K]) Count

func (set GenericDataSet[K]) Count() int

Count return count of elements

func (GenericDataSet[K]) Delete

func (set GenericDataSet[K]) Delete(key K)

Delete delete value

func (GenericDataSet[K]) Intersection added in v1.1.0

func (set GenericDataSet[K]) Intersection(inter GenericDataSet[K]) GenericDataSet[K]

func (GenericDataSet[K]) IsEmpty

func (set GenericDataSet[K]) IsEmpty() bool

IsEmpty return if set is empty

func (GenericDataSet[K]) ToSlice

func (set GenericDataSet[K]) ToSlice() []K

ToSlice return slice of entities

func (GenericDataSet[K]) Union

func (set GenericDataSet[K]) Union(u GenericDataSet[K]) GenericDataSet[K]

type GenericOrderedDataSet

type GenericOrderedDataSet[K comparable] struct {
	// contains filtered or unexported fields
}

GenericOrderedDataSet is a set of strings or numbers It can test for in O(1) time

func NewGenericOrderedDataSet

func NewGenericOrderedDataSet[K comparable](input ...K) GenericOrderedDataSet[K]

NewGenericOrderedDataSet returns a new GenericOrderedDataSet from the given data

func (*GenericOrderedDataSet[K]) Add

func (set *GenericOrderedDataSet[K]) Add(key K)

Add inserts the given value into the set

func (*GenericOrderedDataSet[K]) Contains

func (set *GenericOrderedDataSet[K]) Contains(key K) (exists bool)

Contains tests the membership of given key in the set

func (*GenericOrderedDataSet[K]) Count

func (set *GenericOrderedDataSet[K]) Count() int

Count return count of elements

func (*GenericOrderedDataSet[K]) Delete

func (set *GenericOrderedDataSet[K]) Delete(key K)

Delete delete value

func (*GenericOrderedDataSet[K]) Last

func (set *GenericOrderedDataSet[K]) Last() K

Last last get lasts element

func (*GenericOrderedDataSet[K]) ToSlice

func (set *GenericOrderedDataSet[K]) ToSlice() []K

ToSlice return slice of entities

Jump to

Keyboard shortcuts

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