hashtable

package module
v0.0.0-...-815a6f5 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2021 License: MIT Imports: 2 Imported by: 0

README

Hashtable in Go

Very simple, idiomatic and thread-safe implementation of Hashtables for Golang using Seperate chaining.

Installation

❯ go get -u github.com/HotPotatoC/hashtable

Usage

Set a value

ht := hashtable.New()

ht.Set("user", "John")

Get a value

ht.Get("user") // John

Remove a value

ht.Remove("user") // 1

Iterate through the table using Iter()

for entry := range ht.Iter() {
	fmt.Printf("key: %s | value: %v\n", entry.Key, entry.Value)
}

See more examples here

Methods

// Set inserts a new key-value pair item into the hash table
Set(k string, v interface{})
// Get returns the value of the given key
// and a boolean which returns false if the
// lookup result is nil otherwise true
Get(k string) (interface{}, bool)
// Remove deletes an item by the given key
// and returns the deleted count
Remove(k string) int
// Iter returns an iterator for the hashtable
Iter() <-chan *Entry
// Exist returns true if an item with the given key exists in the table
// otherwise returns false
Exist(k string) bool
// Len represents the size of the hash table
Len() int

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Support

Buy Me A Coffee

Documentation

Index

Constants

View Source
const (

	// DefaultSize is the default capacity of the table (16)
	DefaultSize = 16
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	Head *Entry
}

Bucket represents the hash table bucket

type Entry

type Entry struct {
	Key   string
	Value interface{}
	Next  *Entry
}

Entry represents an entry inside the bucket

type HashTable

type HashTable interface {
	// Set inserts a new key-value pair item into the hash table
	Set(k string, v interface{})
	// Get returns the value of the given key
	// and a boolean which returns false if the
	// lookup result is nil otherwise true
	Get(k string) (interface{}, bool)
	// Remove deletes an item by the given key
	// and returns the deleted count
	Remove(k string) int
	// Iter returns an iterator for the hashtable
	Iter() <-chan *Entry
	// Exist returns true if an item with the given key exists in the table
	// otherwise returns false
	Exist(k string) bool
	// Len represents the size of the hash table
	Len() int
}

HashTable data structure

func New

func New() HashTable

New creates a new hashtable

func NewWithSize

func NewWithSize(size uint) HashTable

NewWithSize creates a new hashtable with the given size

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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