From e722ed1b665aebcf6dd8a3335bb4c59a40991fe7 Mon Sep 17 00:00:00 2001 From: lindsaygelle Date: Wed, 18 Oct 2023 10:13:44 +1100 Subject: [PATCH] Dev --- hashtable.go | 44 ++++++++++++++++++++++++++++++++++++ hashtable_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/hashtable.go b/hashtable.go index 35608df..a92f2dc 100644 --- a/hashtable.go +++ b/hashtable.go @@ -397,3 +397,47 @@ func (hashtable *Hashtable[K, V]) HasMany(keys ...K) *slice.Slice[bool] { } return &values } + +// Keys returns a slice containing all the keys present in the hashtable. +// +// Example: +// +// ht := make(hashtable.Hashtable[string, int]) +// ht.Add("apple", 5) +// ht.Add("banana", 3) +// ht.Add("cherry", 8) +// +// // Get all keys from the hashtable. +// keys := ht.Keys() // Result: {"apple", "banana", "cherry"} +func (hashtable *Hashtable[K, V]) Keys() *slice.Slice[K] { + keys := make(slice.Slice[K], 0) + hashtable.EachKey(func(key K) { + keys.Append(key) + }) + return &keys +} + +// KeysFunc returns a slice containing the keys from the hashtable for which the provided function returns true. +// The provided function `fn` should accept a key of type `K` and return a boolean value. +// +// Example: +// +// ht := make(hashtable.Hashtable[string, int]) +// ht.Add("apple", 5) +// ht.Add("banana", 3) +// ht.Add("cherry", 8) +// +// // Get keys from the hashtable where the key length is greater than 5. +// keys := ht.KeysFunc(func(key string) bool { +// return len(key) > 5 +// }) +// // Result: {"banana"} +func (hashtable *Hashtable[K, V]) KeysFunc(fn func(key K) bool) *slice.Slice[K] { + keys := make(slice.Slice[K], 0) + hashtable.EachKey(func(key K) { + if fn(key) { + keys.Append(key) + } + }) + return &keys +} diff --git a/hashtable_test.go b/hashtable_test.go index 78e6bfc..5c3a3f8 100644 --- a/hashtable_test.go +++ b/hashtable_test.go @@ -3,6 +3,7 @@ package hashtable_test import ( "reflect" "sort" + "strings" "testing" "github.com/lindsaygelle/hashtable" @@ -424,11 +425,12 @@ func TestEachValueBreak(t *testing.T) { ht.Add("banana", 3) ht.Add("cherry", 8) - // Sort the keys for consistent iteration order. keys := make([]string, 0, len(ht)) for key := range ht { keys = append(keys, key) } + + // Sort the keys for consistent iteration order. sort.Strings(keys) // Define a function to process each value. It returns false to break the iteration if the value is 3. @@ -529,9 +531,9 @@ func TestHas(t *testing.T) { func TestHasMany(t *testing.T) { // Create a new hashtable. ht := make(hashtable.Hashtable[string, int]) - ht.Add("apple", 5) - ht.Add("banana", 3) - ht.Add("cherry", 8) + ht["apple"] = 5 + ht["banana"] = 3 + ht["cherry"] = 8 // Keys to check existence. keysToCheck := []string{"apple", "orange", "banana"} @@ -547,3 +549,50 @@ func TestHasMany(t *testing.T) { t.Fatalf("Expected results: %v, but got: %v", expectedResults, results) } } + +// TestKeys tests Hashtable.Keys. +func TestKeys(t *testing.T) { + // Create a new hashtable. + ht := make(hashtable.Hashtable[string, int]) + ht["apple"] = 5 + ht["banana"] = 3 + ht["cherry"] = 8 + + // Get all keys from the hashtable. + keys := ht.Keys() + + // Sort the keys for consistent iteration order. + sort.Strings(*keys) + + // The expected keys slice: {"apple", "banana", "cherry"} + expectedKeys := &slice.Slice[string]{"apple", "banana", "cherry"} + + // Sort the keys for consistent iteration order. + sort.Strings(*expectedKeys) + + // Verify that the obtained keys match the expected keys. + if !reflect.DeepEqual(keys, expectedKeys) { + t.Errorf("Expected keys: %v, but got: %v", expectedKeys, keys) + } +} + +func TestKeysFunc(t *testing.T) { + // Create a new hashtable. + ht := make(hashtable.Hashtable[string, int]) + ht["apple"] = 5 + ht["banana"] = 3 + ht["cherry"] = 8 + + // Get keys from the hashtable where the key length is greater than 5. + keys := ht.KeysFunc(func(key string) bool { + return strings.HasPrefix(key, "b") + }) + + // The expected keys slice: {"banana"} + expectedKeys := &slice.Slice[string]{"banana"} + + // Verify that the obtained keys match the expected keys. + if !reflect.DeepEqual(keys, expectedKeys) { + t.Errorf("Expected keys: %v, but got: %v", expectedKeys, keys) + } +}