### B-Tree Usage Example in Go Source: https://github.com/arthur-zhang/letsddia-go/blob/main/btree/b-tree-on-disk/README.md Demonstrates how to use the persistent B-Tree implementation in Go. It covers creating a new B-tree, inserting items with fixed-size keys and values, and searching for existing and non-existing keys. ```go btree := NewBtree("/tmp/btree.db", 2) arr := []byte{'F', 'S', 'Q', 'K', 'C', 'L', 'H', 'T', 'V', 'W', 'M', 'R', 'N', 'P', 'A', 'B', 'X', 'Y', 'D', 'Z', 'E'} for _, v := range arr { item := &Item{ key: [N]byte{v}, value: [N]byte{v, byte('0'), byte('1')}, } bTree.Insert(item) } key := Item{key: [N]byte{'A'}} item := btree.Search(&key) assert.True(t, item != nil) assert.Equal(t, "A", decodeString(item.key)) assert.Equal(t, "A01", decodeString(item.value)) key = Item{key: [N]byte{'a'}} item = btree.Search(&key) assert.True(t, item == nil) ``` -------------------------------- ### Bloom Filter Go Usage Example Source: https://github.com/arthur-zhang/letsddia-go/blob/main/bloom-filter/README.md Demonstrates how to create, insert into, and look up elements in a Bloom Filter implemented in Go. It initializes a Bloom Filter with a specified capacity and false-positive rate, inserts an item, and verifies its presence. ```go func TestSingleElement(t *testing.T) { bf := NewBloomFilter[Key](10000, 0.10) item := Key("Hello") bf.Insert(item) if !bf.LookUp(item) { t.Errorf("Expected item to exist in the bloom filter") } } ``` -------------------------------- ### Test Rabin-Karp Algorithm Implementation in Go Source: https://github.com/arthur-zhang/letsddia-go/blob/main/algorithms/rabin-karp/README.md This Go code snippet demonstrates how to test the RabinKarpSearch function. It includes various test cases covering different scenarios such as finding patterns, patterns not present, empty strings, and partial matches. The tests use the standard Go testing package and the assert library for assertions. ```go func TestRabinKarp(t *testing.T) { tests := []struct { s string pattern string wantIdx int wantOk bool }{ {"hello world", "world", 6, true}, {"hello world", "world!", 0, false}, {"hello world", "o", 4, true}, {"hello world", "hello world", 0, true}, {"hello world", "hello world!", 0, false}, {"", "", 0, true}, {"", "a", 0, false}, {"a", "", 0, true}, {"abc", "abc", 0, true}, {"abcabc", "abc", 0, true}, {"abcabc", "acb", 0, false}, } for _, test := range tests { gotIdx, gotOk := RabinKarpSearch(test.s, test.pattern) assert.Equal(t, test.wantIdx, gotIdx) assert.Equal(t, test.wantOk, gotOk) } } ``` -------------------------------- ### Cuckoo Hashing Usage and Test Cases in Go Source: https://github.com/arthur-zhang/letsddia-go/blob/main/algorithms/cuckoo-hashing/README.md Demonstrates the usage of the Cuckoo Hashing implementation in Go through unit tests. It covers inserting keys, looking up existing and non-existent keys, and erasing keys. ```go func TestCuckooHashing(t *testing.T) { cht := New(4, hash0, hash1) // insert key t.Run("Insert", func(t *testing.T) { keys := []int{10, 20, 30, 40, 50} for _, key := range keys { cht.Insert(key) } // ensure all keys are present for _, key := range keys { if ok := cht.Lookup(key); !ok { t.Errorf("Expected key %d to be present, but it was not", key) } } }) // lookup key t.Run("Lookup", func(t *testing.T) { if ok := cht.Lookup(10); !ok { t.Error("Expected key 10 to be present, but it was not") } if ok := cht.Lookup(100); ok { t.Error("Expected key 100 to be absent, but it was found") } }) // delete key t.Run("Erase", func(t *testing.T) { cht.Erase(30) if ok := cht.Lookup(30); ok { t.Error("Expected key 30 to be absent after erasing, but it was found") } }) // delete nonexistent key t.Run("Erase nonexistent key", func(t *testing.T) { cht.Erase(100) if ok := cht.Lookup(100); ok { t.Error("Expected key 100 to be absent, but it was found") } }) } ``` -------------------------------- ### Implement HyperLogLog in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt A probabilistic cardinality estimator that approximates the count of distinct elements using minimal space. The implementation includes corrections for small and large ranges to improve accuracy. ```go package main import "hyperloglog" func main() { // Create HyperLogLog with b bits for bucket indexing // m = 2^b buckets, standard error ~= 1.04/sqrt(m) hll := hyperloglog.New(14) // 16384 buckets, ~0.81% standard error // Add elements (strings) hll.Add("user:alice") hll.Add("user:bob") hll.Add("user:charlie") hll.Add("user:alice") // duplicate, won't increase cardinality // Or add integers hll.AddInt(12345) hll.AddInt(67890) // Get cardinality estimate count := hll.Cardinality() // ~3.0 for unique users } ``` -------------------------------- ### In-Memory B-Tree Implementation in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt An in-memory B-Tree implementation based on Introduction to Algorithms. Provides O(log n) search and insertion with automatic node splitting when full. Requires the 'b_tree' package. ```go package main import "b_tree" func main() { // Create B-Tree with degree T=2 (max 3 keys per node) btree := b_tree.NewBtree() // Insert keys keys := []int{10, 20, 5, 6, 12, 30, 7, 17} for _, key := range keys { btree.Insert(key) } // Search for key node, idx := btree.Search(12) if node != nil { // Found at node.keys[idx] } // Search for non-existent key node, _ = btree.Search(100) // node == nil } ``` -------------------------------- ### B-Tree Disk Layout Description Source: https://github.com/arthur-zhang/letsddia-go/blob/main/btree/b-tree-on-disk/README.md Describes the on-disk layout of the persistent B-Tree, specifying the size and order of fields like 'isLeaf', 'numKeys', and key-value pairs. ```text |isLeaf 1-byte| |numKeys 8-bytes| |Key#0 10-bytes | |Value#0 10-bytes | ``` -------------------------------- ### Rabin-Karp String Search in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt A rolling hash algorithm for efficient string pattern matching. Uses polynomial hashing to achieve O(n+m) average-case complexity for finding a pattern in text. Requires the 'rabin_karp' package. ```go package main import "rabin_karp" func main() { text := "hello world, hello universe" pattern := "world" // Search for pattern in text idx, found := rabin_karp.RabinKarpSearch(text, pattern) if found { // idx = 6, pattern found at position 6 } // Search for non-existent pattern idx, found = rabin_karp.RabinKarpSearch(text, "galaxy") // found = false, idx = 0 } ``` -------------------------------- ### Implement LSM Tree Disk Store in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt A Log-Structured Merge Tree implementation using block-based disk files. It optimizes for efficient sequential writes with periodic compaction and provides iterator-based range scans. ```go package main import "lsm" func main() { // Create disk store in specified directory store := lsm.NewDiskStore("/tmp/lsm_data") defer store.Close() // Iterate over all key-value pairs across multiple disk files iter := store.Iter() iter.SeekToFirst() for iter.Valid() { key := iter.Key() value := iter.Value() // Process key-value pair iter.Next() } } ``` -------------------------------- ### Implement Bitcask Storage Engine in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt A log-structured key-value store based on the Bitcask paper. It offers fast reads via an in-memory hash index and append-only writes for durability. Supports compaction to reclaim space. ```go package main import "bitcask" func main() { // Open or create a Bitcask database opts := bitcask.Opts{} // Use defaults db := bitcask.Open("/tmp/mydb", opts) // Put key-value pairs (append-only writes) db.Put("user:1001", []byte(`{"name":"Alice","age":30}`)) db.Put("user:1002", []byte(`{"name":"Bob","age":25}`)) // Get value by key (O(1) via in-memory index) value, err := db.Get("user:1001") if err != nil { panic(err) } // value = []byte(`{"name":"Alice","age":30}`) // Delete key (writes tombstone marker) db.Delete("user:1002") // Merge/compact to reclaim space from old entries db.Merge() } ``` -------------------------------- ### Cuckoo Hashing for O(1) Worst-Case Lookup (Go) Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt Demonstrates Cuckoo Hashing in Go, a hash table implementation with O(1) worst-case lookup time. It uses two hash functions and handles collisions via displacement chains. Requires the 'cuckoo_hashing' Go package. ```go package main import "cuckoo_hashing" func main() { // Define two hash functions hash1 := func(key int) int { return key % 11 } hash2 := func(key int) int { return (key / 11) % 11 } // Create cuckoo hash table with size 11 cht := cuckoo_hashing.New(11, hash1, hash2) // Insert keys (may trigger displacement chain) cht.Insert(20) cht.Insert(50) cht.Insert(53) cht.Insert(75) // O(1) lookup found := cht.Lookup(50) // true found = cht.Lookup(99) // false // Delete cht.Erase(50) } ``` -------------------------------- ### On-Disk B-Tree Implementation in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt A persistent B-Tree implementation that stores nodes on disk with fixed-size keys and values. Supports page-based I/O for durability. Requires the 'b_tree_on_disk' package. ```go package main import "b_tree_on_disk" func main() { // Create persistent B-Tree with minimum degree t=2 btree := b_tree_on_disk.NewBtree("/tmp/btree.db", 2) // Keys and values are fixed-size byte arrays (10 bytes each) const N = 10 // Insert items item := &b_tree_on_disk.Item{ key: [N]byte{'U', 'S', 'E', 'R', '1'}, value: [N]byte{'A', 'L', 'I', 'C', 'E'}, } btree.Insert(item) // Search by key searchKey := &b_tree_on_disk.Item{key: [N]byte{'U', 'S', 'E', 'R', '1'}} result := btree.Search(searchKey) if result != nil { // Found: result.value contains the data } } ``` -------------------------------- ### T-Digest for Streaming Quantile Estimation (Go) Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt Shows how to use T-Digest, a streaming quantile estimation algorithm in Go, for accurate percentile calculations on streaming data. Useful for P50, P95, P99. Requires the 't_digest' Go package. ```go package main import "t_digest" func main() { // Create T-Digest td := t_digest.NewTDigestV2() // Insert streaming values values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} for _, v := range values { td.Insert(v) } // Query quantiles p50 := td.Quantile(0.5) // Median ~5.5 p90 := td.Quantile(0.9) // 90th percentile ~9.0 p99 := td.Quantile(0.99) // 99th percentile ~10.0 } ``` -------------------------------- ### SimHash for Near-Duplicate Document Detection (Go) Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt Implements SimHash, a locality-sensitive hashing algorithm to detect near-duplicate documents. It generates similar hash values for similar input strings, enabling efficient similarity detection. Requires the 'simhash' Go package. ```go package main import "simhash" func main() { // Generate SimHash for documents doc1 := "the quick brown fox jumps over the lazy dog" doc2 := "the quick brown fox jumps over the lazy cat" doc3 := "completely different text about programming" hash1 := simhash.SimHash(doc1) hash2 := simhash.SimHash(doc2) hash3 := simhash.SimHash(doc3) // Similar documents have similar hashes (low Hamming distance) // hash1 and hash2 will be similar // hash1 and hash3 will be very different } ``` -------------------------------- ### K-Way Merge Algorithm using Min-Heap (Go) Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt Implements the K-Way Merge algorithm in Go using a min-heap. This algorithm is essential for external sorting, efficiently merging K sorted files with bounded memory usage, suitable for datasets larger than RAM. Uses Go's standard 'container/heap' package. ```go package main import ( "container/heap" ) // MinHeap implementation for merge entries type Entry struct { value int iterIdx int } type MinHeap []Entry func (h MinHeap) Len() int { return len(h) } func (h MinHeap) Less(i, j int) bool { return h[i].value < h[j].value } func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *MinHeap) Push(x interface{}) { *h = append(*h, x.(Entry)) } func (h *MinHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func main() { // Create iterators for each sorted file iters := make([]Iterator, K) // K sorted file iterators // Initialize min-heap with first element from each iterator pq := &MinHeap{} heap.Init(pq) for i := 0; i < len(iters); i++ { if iters[i].Next() { heap.Push(pq, Entry{value: iters[i].Value(), iterIdx: i}) } } // Merge: always extract minimum and replenish from same iterator for pq.Len() > 0 { item := heap.Pop(pq).(Entry) // Output item.value (next smallest element) // Replenish from the same iterator if iters[item.iterIdx].Next() { heap.Push(pq, Entry{ value: iters[item.iterIdx].Value(), iterIdx: item.iterIdx, }) } } } ``` -------------------------------- ### Roaring Bitmap for Compressed Integer Sets (Go) Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt Demonstrates the use of Roaring Bitmap, a compressed bitmap data structure in Go. It automatically selects optimal storage based on data density, making it efficient for storing large sets of integers. Requires the 'roaring_bitmap' Go package. ```go package main import "roaring_bitmap" func main() { // Create roaring bitmap rb := roaring_bitmap.New() // Add 32-bit integers rb.Add(1) rb.Add(2) rb.Add(3) rb.Add(1000000) rb.Add(1000001) // Container type is chosen automatically: // - ArrayContainer for sparse data (<4096 elements) // - BitmapContainer for dense data // - RunContainer for sequential runs // Remove elements rb.Remove(2) // Get total count count := rb.Len() // 4 } ``` -------------------------------- ### Implement Count-Min Sketch in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt A probabilistic data structure for frequency estimation in data streams. It uses multiple hash functions and a 2D counter table to provide frequency estimates with bounded error. Estimates may overestimate but never underestimate. ```go package main import "count_min_sketch" func main() { // Create Count-Min Sketch with d rows (hash functions) and w columns // Error bound: epsilon = e/w, probability: delta = 1/e^d cms := count_min_sketch.New(5, 1000) // 5 hash functions, 1000 columns // Update counts cms.Update("apple", 1) cms.Update("banana", 3) cms.Update("apple", 2) // Estimate frequency (may overestimate, never underestimate) appleCount := cms.Estimate("apple") // ~3 bananaCount := cms.Estimate("banana") // ~3 } ``` -------------------------------- ### Hash Ring (Consistent Hashing) in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt A consistent hashing implementation for distributing resources across nodes with minimal redistribution when nodes are added or removed. Essential for distributed caching and load balancing. Requires the 'hash_ring' package. ```go package main import "hash_ring" func main() { // Create hash ring with k bits (ring size = 2^k) ring := hash_ring.NewHashRing(10) // Ring size 0-1023 // Add nodes (servers) at specific positions ring.AddNode(100) ring.AddNode(400) ring.AddNode(700) // Add resources (keys) - automatically assigned to closest node ring.AddResource(150) // Goes to node at 400 ring.AddResource(350) // Goes to node at 400 ring.AddResource(800) // Goes to node at 100 (wraps around) // Remove a node - resources automatically migrate to next node ring.DeleteNode(400) // Debug output shows node assignments ring.DebugPrint() } ``` -------------------------------- ### Inverted Index Implementation in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt Implements a full-text search index that maps terms to document occurrences with frequency tracking. Useful for search engines and text retrieval systems. Requires the 'inverted_index' package. ```go package main import "inverted_index" func main() { // Create inverted index idx := inverted_index.New() // Index documents idx.IndexDoc(&inverted_index.Doc{ Id: 1, Content: "The quick brown fox jumps over the lazy dog", }) idx.IndexDoc(&inverted_index.Doc{ Id: 2, Content: "A quick brown dog runs in the park", }) // Search for terms results := idx.Lookup("quick brown") // Returns map[string][]Appearance with document IDs and term frequencies // results["quick"] = [{DocId: 1, Freq: 1}, {DocId: 2, Freq: 1}] // results["brown"] = [{DocId: 1, Freq: 1}, {DocId: 2, Freq: 1}] } ``` -------------------------------- ### Implement Bloom Filter in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt A space-efficient probabilistic data structure for membership testing. It uses multiple hash functions to set bits in a bitmap, providing O(k) insertion and lookup. The implementation automatically calculates optimal bitmap size and hash function count. ```go package main import "bloom_filter" // Key type must implement the Byteable interface type Key string func (k Key) Bytes() []byte { return []byte(k) } func main() { // Create bloom filter for 10000 items with 10% false positive rate // Optimal bitmap size (m) and hash function count (k) are calculated automatically bf := bloom_filter.NewBloomFilter[Key](10000, 0.10) // Insert items bf.Insert(Key("user:1001")) bf.Insert(Key("user:1002")) bf.Insert(Key("user:1003")) // Lookup - may return false positive but never false negative exists := bf.LookUp(Key("user:1001")) // true exists = bf.LookUp(Key("user:9999")) // false (or rarely true - false positive) } ``` -------------------------------- ### Skip List Implementation in Go Source: https://context7.com/arthur-zhang/letsddia-go/llms.txt A probabilistic data structure providing O(log n) average-case search, insertion, and deletion. Implements a multi-level linked list with random level assignment. Requires the 'skiplist' package. ```go package main import "skiplist" func main() { // Create generic skip list list := skiplist.NewSkipList[int, string]() // Insert key-value pairs key1, val1 := 10, "ten" key2, val2 := 5, "five" key3, val3 := 15, "fifteen" list.Insert(&key1, &val1) list.Insert(&key2, &val2) list.Insert(&key3, &val3) // Check membership searchKey := 10 exists := list.Contains(&searchKey) // true // Find operations node := list.FindGreaterThanOrEqual(&searchKey) nodeLT := list.FindLessThan(&searchKey) lastNode := list.FindLast() // Delete list.Delete(&key1) // Iterator pattern iter := list.Iterator() iter.SeekToFirst() for iter.Valid() { key := iter.Key() value := iter.Value() // Process key-value iter.Next() } iter.Close() } ``` -------------------------------- ### Define Documents for Inverted Index (Go) Source: https://github.com/arthur-zhang/letsddia-go/blob/main/inverted-index/README.md This Go code snippet defines a slice of strings, where each string represents a document to be indexed. These documents serve as the input data for building the inverted index. ```go var docs = []string{ "new home sales top forecasts.", "home sales rise in july!", "increase in home sales in july...", "july new home sales rise,", } ``` -------------------------------- ### Calculate Cardinality - HyperLogLog Go Source: https://github.com/arthur-zhang/letsddia-go/blob/main/algorithms/hyperloglog/README.md Calculates the approximate number of distinct elements using the HyperLogLog algorithm. It applies different correction strategies based on the raw estimate and the number of zero registers. This function is essential for the count-distinct problem. ```go func (hll *HyperLogLog) Cardinality() float64 { sum := 0.0 zeros := 0 for _, it := range hll.registers { sum += 1.0 / float64(int(1)< 2^32/30, Large range correction return -math.Pow(2, 32) * math.Log(1-E/math.Pow(2, 32)) } } ``` -------------------------------- ### Cuckoo Hashing Hash Functions in Go Source: https://github.com/arthur-zhang/letsddia-go/blob/main/algorithms/cuckoo-hashing/README.md Defines two hash functions, hash0 and hash1, used in the Cuckoo Hashing implementation. These functions are crucial for determining the potential locations of keys within the hash table. ```go func hash0(key int) int { return key % 10 } func hash1(key int) int { return key / 10 } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.