advanced

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2025 License: MIT Imports: 2 Imported by: 0

README

Advanced Data Structures Package

This package provides implementations of advanced data structures in Go, focusing on specialized use cases and optimized performance characteristics.

Features

LRU Cache
  • Least Recently Used (LRU) cache implementation
  • Generic key-value storage with interface{} type
  • Operations:
    • Put: Add or update key-value pairs
    • Get: Retrieve values with LRU update
    • Contains: Check key existence
    • Clear: Remove all entries
  • Internal structure:
    • Doubly linked list for order maintenance
    • Hash map for O(1) lookups
    • Automatic capacity management
Skip List
  • Probabilistic data structure for efficient searching
  • Configurable:
    • Maximum level (default: 16)
    • Probability factor (default: 0.5)
  • Operations:
    • Insert: Add key-value pairs
    • Search: Find values by key
    • Delete: Remove key-value pairs
  • Features:
    • Random level generation
    • Multiple layer links
    • O(log n) average time complexity
Disjoint Set (Union-Find)
  • Implementation with path compression and union by rank
  • Operations:
    • Find: Get set representative
    • Union: Merge two sets
    • Connected: Check if elements are in same set
  • Features:
    • Path compression optimization
    • Union by rank optimization
    • Set size tracking
    • Set count tracking

Usage Examples

LRU Cache
// Create a new LRU cache with capacity 3
cache := NewLRUCache(3)

// Add elements
cache.Put("key1", "value1")
cache.Put("key2", "value2")
cache.Put("key3", "value3")

// Get element (moves to most recently used)
value, exists := cache.Get("key1")
if exists {
    fmt.Println(value) // Outputs: value1
}

// Add new element when at capacity (removes least recently used)
cache.Put("key4", "value4") // "key2" will be removed if it's least recently used

// Check if key exists
exists = cache.Contains("key1") // returns: true
Skip List
// Create a new skip list
list := NewSkipList()

// Insert key-value pairs
list.Insert(10, "value1")
list.Insert(20, "value2")
list.Insert(5, "value3")

// Search for values
value, found := list.Search(20)
if found {
    fmt.Println(value) // Outputs: value2
}

// Delete elements
deleted := list.Delete(10) // returns: true
Disjoint Set
// Create a new disjoint set with size 5
ds := NewDisjointSet(5)

// Merge sets containing elements
ds.Union(0, 1)
ds.Union(2, 3)
ds.Union(1, 2)

// Check if elements are connected
connected := ds.Connected(0, 3) // returns: true

// Get total number of sets
setCount := ds.GetSetCount()

// Find set representative
root := ds.Find(0)

Implementation Details

Performance Characteristics
LRU Cache
  • Get: O(1)
  • Put: O(1)
  • Space complexity: O(capacity)
Skip List
  • Search: O(log n) average case
  • Insert: O(log n) average case
  • Delete: O(log n) average case
  • Space complexity: O(n log n) average case
Disjoint Set
  • Find: O(α(n)) amortized
  • Union: O(α(n)) amortized
  • Connected: O(α(n)) amortized
  • Space complexity: O(n)

Where α(n) is the inverse Ackermann function, which grows extremely slowly.

Memory Usage
  • LRU Cache: Uses doubly linked list nodes and hash map entries
  • Skip List: Uses nodes with variable-length forward arrays
  • Disjoint Set: Uses two arrays for parent pointers and ranks

Testing

Each data structure comes with comprehensive test coverage. Run tests using:

go test ./...

Contributing

Contributions are welcome! Please ensure that any new features or modifications come with:

  • Proper documentation
  • Comprehensive test cases
  • Example usage
  • Performance considerations

License

This package is distributed under the MIT license. See the LICENSE file for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DisjointSet

type DisjointSet struct {
	// contains filtered or unexported fields
}

DisjointSet represents a disjoint-set data structure

func NewDisjointSet

func NewDisjointSet(size int) *DisjointSet

NewDisjointSet creates a new disjoint-set data structure

func (*DisjointSet) Connected

func (ds *DisjointSet) Connected(x, y int) bool

Connected checks if two elements are in the same set

func (*DisjointSet) Find

func (ds *DisjointSet) Find(x int) int

Find finds the representative (root) of the set containing element x Uses path compression for optimization

func (*DisjointSet) GetSetCount

func (ds *DisjointSet) GetSetCount() int

GetSetCount returns the number of disjoint sets

func (*DisjointSet) GetSize

func (ds *DisjointSet) GetSize() int

GetSize returns the total number of elements in the disjoint set

func (*DisjointSet) Union

func (ds *DisjointSet) Union(x, y int) bool

Union merges the sets containing elements x and y Uses union by rank for optimization

type LRUCache

type LRUCache struct {
	// contains filtered or unexported fields
}

LRUCache represents the LRU cache data structure

func NewLRUCache

func NewLRUCache(capacity int) *LRUCache

NewLRUCache creates a new LRU cache with the given capacity

func (*LRUCache) Clear

func (lru *LRUCache) Clear()

Clear removes all items from the cache

func (*LRUCache) Contains

func (lru *LRUCache) Contains(key interface{}) bool

Contains checks if a key exists in the cache

func (*LRUCache) Get

func (lru *LRUCache) Get(key interface{}) (interface{}, bool)

Get retrieves a value from the cache

func (*LRUCache) GetCapacity

func (lru *LRUCache) GetCapacity() int

GetCapacity returns the capacity of the cache

func (*LRUCache) GetSize

func (lru *LRUCache) GetSize() int

GetSize returns the current size of the cache

func (*LRUCache) Put

func (lru *LRUCache) Put(key, value interface{})

Put adds or updates a value in the cache

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node represents a node in the doubly linked list

type SkipList

type SkipList struct {
	// contains filtered or unexported fields
}

SkipList represents the skip list data structure

func NewSkipList

func NewSkipList() *SkipList

NewSkipList creates a new skip list

func (*SkipList) Delete

func (sl *SkipList) Delete(key int) bool

Delete removes a key-value pair from the skip list

func (*SkipList) Insert

func (sl *SkipList) Insert(key int, value interface{})

Insert adds a new key-value pair to the skip list

func (*SkipList) Search

func (sl *SkipList) Search(key int) (interface{}, bool)

Search finds a value by key in the skip list

type SkipListNode

type SkipListNode struct {
	// contains filtered or unexported fields
}

SkipListNode represents a node in the skip list

Jump to

Keyboard shortcuts

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