memory

module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0

README

memory

A high-performance collection of generic data structures and algorithms for Go.

memory provides a suite of specialized, type-safe in-memory data structures optimized for efficiency and specific use cases. Unlike the standard library's container packages, this repository offers advanced algorithms like Counting Bloom Filters, Fuzzy Search, and Ordered Maps, all built using Go 1.18+ generics.

Features

🧠 Probabilistic Data Structures
  • Bloom Filter (bloom): Space-efficient probabilistic data structure for checking if an element is in a set. Includes a Counting Bloom Filter that supports removals.
  • HyperLogLog (hll): Efficient cardinality estimation for large datasets.
🔍 Search & String Algo
  • Fuzzy Search (fuzzysearch): Fast, lightweight fuzzy matching. Supports simple subsequence matching and Levenshtein distance ranking.
📦 Containers & Caches
  • LRU Cache (lru): A generic, thread-safe Least Recently Used cache implementation.
  • Ordered Map (orderedmap): A map that maintains insertion order, supporting iteration and index-based access.
  • Registry (registry): A thread-safe structure for grouping items by ID and Category, useful for managing active sessions or grouped workers.
  • Stack (stack): A classic LIFO stack implementation with slice-based backing.
  • Sorted Set (sortedset): (Redis-like) ZSET implementation for storing unique elements with scores.

Installation

Bash

go get github.com/FrogoAI/memory

Usage Examples

1. Counting Bloom Filter

Check for existence with a defined false-positive rate, with support for removing items.

Go

package main

import (
	"fmt"
	"github.com/FrogoAI/memory/bloom"
)

func main() {
	// Initialize for 1000 items with 0.01 (1%) false positive rate
	filter, err := bloom.NewCounting(1000, 0.01)
	if err != nil {
		panic(err)
	}

	data := []byte("user_123")

	filter.Add(data)

	if filter.Test(data) {
		fmt.Println("Item exists!")
	}

	filter.Remove(data)
}
2. Fuzzy Search & Ranking

Find strings that approximately match a target, ranked by Levenshtein distance.

Go

package main

import (
	"fmt"
	"github.com/FrogoAI/memory/fuzzysearch"
)

func main() {
	targets := []string{"cartwheel", "foobar", "wheel", "baz"}
	
	// Find simple matches (subsequence)
	matches := fuzzysearch.Find("whl", targets)
	fmt.Println(matches) // ["cartwheel", "wheel"]

	// Rank by Levenshtein distance
	ranks := fuzzysearch.RankFind("wheel", targets)
	for _, r := range ranks {
		fmt.Printf("Target: %s, Distance: %d\n", r.Target, r.Distance)
	}
}
3. LRU Cache

A type-safe cache that automatically evicts the least recently used items.

Go

package main

import (
	"fmt"
	"github.com/FrogoAI/memory/lru"
)

func main() {
	// Create a cache with capacity 2
	cache := lru.NewLRUCache[string](2)

	cache.Put("a", "alpha")
	cache.Put("b", "beta")
	
	val, found := cache.Get("a") // "a" is now most recently used
	
	cache.Put("c", "charlie") // Evicts "b" (least recently used)

	_, foundB := cache.Get("b") // false
	fmt.Println(foundB)
}
4. Registry

Manage grouped resources (e.g., connections per user) safely.

Go

package main

import (
	"github.com/FrogoAI/memory/registry"
)

func main() {
	// Key=String (GroupID), Index=Int (ItemID), Value=String (Data)
	reg := registry.NewRegistry[string, int, string]()

	// Add item 101 to group "admins"
	reg.Add("admins", 101, "User Alice")
	
	// Iterate over all admins
	for user := range reg.Iterator("admins") {
		println(user)
	}
}

License

MIT

Directories

Path Synopsis
Package bloom provides a counting Bloom filter for probabilistic membership testing with element removal support.
Package bloom provides a counting Bloom filter for probabilistic membership testing with element removal support.
Package btree provides a B-tree for ordered key-value storage with configurable order.
Package btree provides a B-tree for ordered key-value storage with configurable order.
Package comparator provides stateless comparison functions that are safe for concurrent use.
Package comparator provides stateless comparison functions that are safe for concurrent use.
Package fuzzysearch provides fuzzy string matching with Levenshtein distance ranking, useful for filtering data quickly based on lightweight user input.
Package fuzzysearch provides fuzzy string matching with Levenshtein distance ranking, useful for filtering data quickly based on lightweight user input.
Package hll provides a HyperLogLog probabilistic cardinality estimator.
Package hll provides a HyperLogLog probabilistic cardinality estimator.
Package linkedlist provides a generic doubly linked list with ID-based indexing.
Package linkedlist provides a generic doubly linked list with ID-based indexing.
Package lru provides an LRU cache that evicts the least recently used entry when capacity is reached.
Package lru provides an LRU cache that evicts the least recently used entry when capacity is reached.
Package orderedmap provides an insertion-ordered map.
Package orderedmap provides an insertion-ordered map.
Package registry provides a thread-safe registry; all methods are protected by a read-write mutex.
Package registry provides a thread-safe registry; all methods are protected by a read-write mutex.
Package sortedset provides a sorted set backed by a skip list.
Package sortedset provides a sorted set backed by a skip list.
Package stack provides a generic LIFO stack backed by a slice.
Package stack provides a generic LIFO stack backed by a slice.
Package utils provides hashing, sorting, and string helpers (stateless, safe for concurrent use) as well as SafeMap and SafeList which are thread-safe via read-write mutexes.
Package utils provides hashing, sorting, and string helpers (stateless, safe for concurrent use) as well as SafeMap and SafeList which are thread-safe via read-write mutexes.

Jump to

Keyboard shortcuts

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