Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #17

Merged
merged 4 commits into from
Oct 17, 2023
Merged

Dev #17

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .codespellignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
readded
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ repos:
rev: v2.1.0
hooks:
- id: codespell
args: [--ignore-words=.codespellignore]
149 changes: 149 additions & 0 deletions hashtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ func (hashtable *Hashtable[K, V]) AddMany(values ...map[K]V) *Hashtable[K, V] {
return hashtable
}

// AddOK inserts a new key-value pair into the hashtable if the key does not already exist.
// It returns a boolean value indicating whether the key was added successfully (true) or if the key already existed (false).
//
// Example:
// ht := make(hashtable.Hashtable[string, int])
//
// // Attempt to add key-value pairs.
// added := ht.AddOK("apple", 5) // added is true, "apple" is added with value 5.
// reAdded := ht.AddOK("apple", 10) // reAdded is false, "apple" already exists with value 5, no change is made.
// addedNew := ht.AddOK("banana", 3) // addedNew is true, "banana" is added with value 3.
func (hashtable *Hashtable[K, V]) AddOK(key K, value V) bool {
ok := !hashtable.Has(key)
if ok {
hashtable.Add(key, value)
}
return ok
}

// Delete removes a key-value pair from the hashtable based on the provided key.
//
// Example:
Expand All @@ -81,6 +99,137 @@ func (hashtable *Hashtable[K, V]) Delete(key K) *Hashtable[K, V] {
return hashtable
}

// DeleteFunc removes key-value pairs from the hashtable based on the evaluation performed by the provided function.
// For each key-value pair in the hashtable, the function fn is called with the key and value.
// If the function returns true, the key-value pair is removed from the hashtable.
//
// Example:
// ht := make(hashtable.Hashtable[string, int])
// ht.Add("apple", 5)
// ht.Add("orange", 7)
// ht.Add("kiwi", 6)
//
// // Delete key-value pairs where the value is less than 7.
// ht.DeleteFunc(func(key string, value int) bool {
// return value < 7
// }) // Removes the key-value pair with key "kiwi" from the hashtable.
func (hashtable *Hashtable[K, V]) DeleteFunc(fn func(key K, value V) bool) *Hashtable[K, V] {
for key, value := range *hashtable {
if fn(key, value) {
hashtable.Delete(key)
}
}
return hashtable
}

// DeleteMany removes multiple key-value pairs from the hashtable based on the provided keys.
// If a key doesn't exist in the hashtable, it is ignored.
//
// Example:
// ht := make(hashtable.Hashtable[string, int])
// ht.Add("apple", 5)
// ht.Add("orange", 7)
// ht.Add("kiwi", 6)
// ht.DeleteMany("apple", "kiwi") // Removes key-value pairs with keys "apple" and "kiwi" from the hashtable.
func (hashtable *Hashtable[K, V]) DeleteMany(keys ...K) *Hashtable[K, V] {
for _, key := range keys {
hashtable.Delete(key)
}
return hashtable
}

// Each iterates over the key-value pairs in the hashtable and applies a function to each pair.
//
// Example:
// ht := make(hashtable.Hashtable[string, int])
// ht.Add("apple", 5)
// ht.Add("banana", 3)
// ht.Add("cherry", 8)
//
// // Function to print all key-value pairs.
// printKeyValue := func(key string, value int) {
// fmt.Println(key, value)
// }
//
// // Iterate over the hashtable and print all key-value pairs.
// ht.Each(printKeyValue)
// // Output: "apple 5", "banana 3", "cherry 8"
func (hashtable *Hashtable[K, V]) Each(fn func(key K, value V)) *Hashtable[K, V] {
return hashtable.EachBreak(func(key K, value V) bool {
fn(key, value)
return true
})
}

// EachBreak iterates over the key-value pairs in the hashtable and applies a function to each pair.
// If the function returns false at any point, the iteration breaks.
//
// Example:
// ht := make(hashtable.Hashtable[string, int])
// ht.Add("apple", 5)
// ht.Add("banana", 3)
// ht.Add("cherry", 8)
//
// // Function to print key-value pairs until finding "banana".
// stopPrinting := ht.EachBreak(func(key string, value int) bool {
// fmt.Println(key, value)
// return key != "banana" // Continue printing until "banana" is encountered.
// })
// // Output: "apple 5", "banana 3"
func (hashtable *Hashtable[K, V]) EachBreak(fn func(key K, value V) bool) *Hashtable[K, V] {
for key, value := range *hashtable {
if !fn(key, value) {
break
}
}
return hashtable
}

// EachKey iterates over the keys in the hashtable and applies a function to each key.
//
// Example:
// ht := make(hashtable.Hashtable[string, int])
// ht.Add("apple", 5)
// ht.Add("banana", 3)
// ht.Add("cherry", 8)
//
// // Function to print each key.
// printKey := func(key string) {
// fmt.Println(key)
// }
//
// // Iterate over the hashtable and print each key.
// ht.EachKey(printKey)
// // Output: "apple", "banana", "cherry"
func (hashtable *Hashtable[K, V]) EachKey(fn func(key K)) *Hashtable[K, V] {
return hashtable.Each(func(key K, _ V) {
fn(key)
})
}

// EachKeyBreak iterates over the keys in the hashtable and applies a function to each key. It allows breaking the iteration early if the provided function returns false.
//
// Example:
// ht := make(hashtable.Hashtable[string, int])
// ht.Add("apple", 5)
// ht.Add("banana", 3)
// ht.Add("cherry", 8)
//
// // Function to print each key and break the iteration if the key is "banana".
// printAndBreak := func(key string) bool {
// fmt.Println(key)
// return key != "banana"
// }
//
// // Iterate over the hashtable keys, print them, and break when "banana" is encountered.
// ht.EachKeyBreak(printAndBreak)
// // Output: "apple", "banana"
func (hashtable *Hashtable[K, V]) EachKeyBreak(fn func(key K) bool) *Hashtable[K, V] {
return hashtable.EachBreak(func(key K, _ V) bool {
return fn(key)
})
}

// Get retrieves the value associated with the provided key from the hashtable.
// If the key exists, it returns the associated value and true. Otherwise, it returns the zero value for the value type and false.
//
Expand Down
Loading
Loading