compactchain

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

README

CompactMap

CompactChain is a Go library that provides a memory-efficient alternative to the standard map. It organizes entries into multiple buffers to optimize memory usage, making it suitable for applications where memory efficiency is critical. The library supports adding, getting, deleting, checking existence, counting, and iterating over key-value pairs.

Features

  • Memory-efficient storage for large datasets.
  • Supports adding, getting, and deleting key-value pairs.
  • Supports checking existence of key-value pairs.
  • Supports counting and iterating over key-value pairs.
  • Thread-safe operations with sync.Mutex.

Installation

To install the CompactChain library, use go get:

go get github.com/goupdate/compactchain

Usage

Creating a CompactChain

To create a new CompactChain, use the NewCompactChain function:

package main

import (
	"fmt"
	"github.com/goupdate/compactchain"
)

func main() {
	cm := compactchain.NewCompactChain[int, int]()
	cm.Add(1, 100)
	value, exists := cm.Get(1)
	if exists {
		fmt.Println("Value:", value)
	}
}
Adding Entries

To add entries to the CompactChain, use the Add method:

cm.Add(1, 100)
cm.Add(1, 200) // Adding another value to the same key
Getting Entries

To retrieve entries from the CompactChain, use the Get method:

values, exists := cm.Get(1)
if exists {
    fmt.Println("Values for key 1:", values)
}
Deleting Entries

To delete entries from the CompactChain, use the Delete method:

cm.Delete(1, 100)
Checking Existence of a Key-Value Pair

To check if a key-value pair exists in the CompactChain, use the Exist method:

exists := cm.Exist(1, 100)
fmt.Println("Exists:", exists)
Counting Entries

To get the number of entries in the CompactChain, use the Count method:

count := cm.Count()
fmt.Println("Count:", count)
Iterating Over Entries

To iterate over entries in the CompactChain, use the Iterate method:

cm.Iterate(func(key, value int) bool {
    fmt.Printf("Key: %d, Value: %d\n", key, value)
    return true // return false to stop iteration
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompactChain

type CompactChain[K constraints.Ordered, V constraints.Ordered] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

CompactChain is a map of slices of slices of empty structs

func NewCompactChain

func NewCompactChain[K, V constraints.Ordered]() *CompactChain[K, V]

NewCompactChain creates a new CompactChain

func (*CompactChain[K, V]) Add

func (m *CompactChain[K, V]) Add(key K, value V)

Add adds a key-value pair to the CompactChain

func (*CompactChain[K, V]) Count

func (m *CompactChain[K, V]) Count() int

Count returns the total number of key-value pairs in the CompactChain

func (*CompactChain[K, V]) Delete

func (m *CompactChain[K, V]) Delete(key K, value V)

Delete removes a key-value pair from the CompactChain

func (*CompactChain[K, V]) Exist

func (m *CompactChain[K, V]) Exist(key K, value V) bool

Exist checks if a key-value pair exists in the CompactChain

func (*CompactChain[K, V]) Get

func (m *CompactChain[K, V]) Get(key K) ([]V, bool)

Get retrieves the values associated with a key in the CompactChain

func (*CompactChain[K, V]) Iterate

func (m *CompactChain[K, V]) Iterate(fn func(key K, value V) bool)

Iterate iterates over all key-value pairs in the CompactChain

func (*CompactChain[K, V]) IterateKeys added in v0.0.4

func (m *CompactChain[K, V]) IterateKeys(fn func(key K, values []V) bool)

Iterate iterates over all keys in the CompactChain

type Entry

type Entry[K constraints.Ordered, V any] struct {
	Key   K
	Chain *[]V
}

Entry represents a key with its chain of values

Jump to

Keyboard shortcuts

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