Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsaygelle committed Oct 17, 2023
1 parent dc5aeb7 commit e722ed1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
44 changes: 44 additions & 0 deletions hashtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
57 changes: 53 additions & 4 deletions hashtable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hashtable_test
import (
"reflect"
"sort"
"strings"
"testing"

"github.com/lindsaygelle/hashtable"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"}
Expand All @@ -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)
}
}

0 comments on commit e722ed1

Please sign in to comment.