hashtable

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package hashtable implements a hash table data structure using separate chaining for collision resolution. uses my doublyLinkedList package for the buckets

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hash

func Hash(key interface{}) int

Helper hash function

Types

type Entry

type Entry[K, V comparable] struct {
	Hash int
	// contains filtered or unexported fields
}

Entry represents a key-value pair.

func NewEntry

func NewEntry[K, V comparable](key K, value V) *Entry[K, V]

NewEntry creates a new key-value pair entry by hashing the key that is of type K comparable

func (*Entry[K, V]) Equals

func (e *Entry[K, V]) Equals(other *Entry[K, V]) bool

Equals checks if two entries are equal

func (Entry[K, V]) ToString

func (e Entry[K, V]) ToString() string

ToString returns a string representation of the entry

type HashTable

type HashTable[K, V comparable] struct {
	// contains filtered or unexported fields
}

HashTable represents a hash table data structure

func NewHashTable

func NewHashTable[K, V comparable](capacity int, maxLoadFactor float64) *HashTable[K, V]

NewHashTable creates a new hash table with the given capacity(optional) and load factor(optional) if no arguments are given, the default values of 0.75 and 16 are used

func (*HashTable[K, V]) Add

func (h *HashTable[K, V]) Add(key K, value V) V

Add inserts the given key-value pair into the hash table

func (*HashTable[K, V]) Clear

func (h *HashTable[K, V]) Clear()

Clear removes all elements from the hash table

func (*HashTable[K, V]) ContainsKey

func (h *HashTable[K, V]) ContainsKey(key K) bool

ContainsKey checks if the hash table contains the given key

func (*HashTable[K, V]) Get

func (h *HashTable[K, V]) Get(key K) *V

Get returns the value associated with the given key return nil if the key is not found

func (*HashTable[K, V]) Has

func (h *HashTable[K, V]) Has(key K) bool

Has checks if the hash table contains the given key

func (*HashTable[K, V]) Insert

func (h *HashTable[K, V]) Insert(key K, value V) V

Insert inserts the given key-value pair into the hash table

func (*HashTable[K, V]) IsEmpty

func (h *HashTable[K, V]) IsEmpty() bool

IsEmpty returns true if the hash table is empty

func (*HashTable[K, V]) Keys

func (h *HashTable[K, V]) Keys() []K

Keys returns a slice of all the keys in the hash table

func (*HashTable[K, V]) Put

func (h *HashTable[K, V]) Put(key K, value V) V

Put inserts the given key-value pair into the hash table

func (*HashTable[K, V]) Remove

func (h *HashTable[K, V]) Remove(key K) *V

Remove removes the entry with the given key from the hash table

func (*HashTable[K, V]) Size

func (h *HashTable[K, V]) Size() int

Size returns the number of elements in the hash table

func (*HashTable[K, V]) ToString

func (h *HashTable[K, V]) ToString() string

ToString returns a string representation of the hash table

func (*HashTable[K, V]) Values

func (h *HashTable[K, V]) Values() []V

Values returns a slice of all the values in the hash table

Jump to

Keyboard shortcuts

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