diff --git a/README.md b/README.md index 9a9c4f7..9134a24 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,28 @@ added := myMap.AddOK("key1", 1) fmt.Println(added) // true ``` +### AddValueFunc +Adds a key-value pair to the map using a function to determine the key from the given value. + +```Go +myMap := &gomap.Map[string, int]{} +myMap.AddValueFunc(5, func(value int) string { + return strconv.Itoa(value) +}) +fmt.Println(myMap) // &map[5:5] +``` + +### AddValuesFunc +Adds multiple key-value pairs to the map using a function to determine keys from the given values. + +```Go +myMap := &gomap.Map[string, int]{} +myMap.AddValuesFunc([]int{5, 10, 15}, func(i int, value int) string { + return strconv.Itoa(value) + strconv.Itoa(i) +}) +fmt.Println(myMap) // &map[50:5 101:10 152:15] +``` + ### Contains Checks if the given value is present in the hash table and returns the corresponding key along with a boolean indicating existence. diff --git a/gomap.go b/gomap.go index fd5bb61..e631f79 100644 --- a/gomap.go +++ b/gomap.go @@ -98,6 +98,42 @@ func (gomap *Map[K, V]) AddManyOK(values ...map[K]V) *slice.Slice[bool] { return &successfulInsertions } +// AddValueFunc adds a key-value pair to the map using a function to determine the key from the given value. +// It takes a value and a key calculation function as input. The function calculates the key based on the provided value +// and adds the key-value pair to the map. +// +// // Create a new Map instance. +// newMap := make(gomap.Map[string, int]) +// +// // Function to calculate key from value (for example, using string conversion) +// gomap.AddValueFunc(5, func(value int) string { +// return strconv.Itoa(value) +// }) // Adds key "5" with value 5 to the map +func (gomap *Map[K, V]) AddValueFunc(value V, fn func(value V) K) *Map[K, V] { + return gomap.Add(fn(value), value) +} + +// AddValuesFunc adds multiple key-value pairs to the map using a function to determine keys from the given values. +// It takes a slice of values and a key calculation function as input. The function calculates the key for each value +// based on its index and the provided value, and adds the corresponding key-value pairs to the map. +// +// // Create a new Map instance. +// newMap := make(gomap.Map[string, int]) +// +// values := []int{5, 10, 15} +// // Function to calculate key from value and index (for example, using string conversion and appending index) +// gomap.AddValuesFunc(values, func(i int, value int) string { +// return strconv.Itoa(value) + strconv.Itoa(i) +// }) // Adds key "50", "101" and "152" with values 5, 10, and 15 to the map +func (gomap *Map[K, V]) AddValuesFunc(values []V, fn func(i int, value V) K) *Map[K, V] { + for i, value := range values { + gomap.AddValueFunc(value, func(value V) K { + return fn(i, value) + }) + } + return gomap +} + // AddOK inserts a new key-value pair into the map only if the key does not already exist in the map. // If the key already exists, the insertion fails, and false is returned. If the key is new, a new key-value pair is added to the gomap, // and true is returned to indicate a successful insertion. diff --git a/gomap_test.go b/gomap_test.go index 3395625..5897e85 100644 --- a/gomap_test.go +++ b/gomap_test.go @@ -4,6 +4,7 @@ import ( "math" "reflect" "sort" + "strconv" "strings" "testing" @@ -271,6 +272,57 @@ func TestAddOK(t *testing.T) { } } +// TestAddValueFunc tests Map.AddValueFunc. +func TestAddValueFunc(t *testing.T) { + // Create a new Map instance. + newMap := &gomap.Map[string, int]{} // Change the data types accordingly. + + // Function to calculate key from value using string conversion. + keyCalculationFunc := func(value int) string { + return strconv.Itoa(value) + } + + // Test case: Add a key-value pair using AddValueFunc. + valueToAdd := 5 + newMap.AddValueFunc(valueToAdd, keyCalculationFunc) + + // Verify that the key-value pair is added correctly. + expectedKey := "5" + expectedValue := 5 + retrievedValue, ok := newMap.Get(expectedKey) + + if !ok || retrievedValue != expectedValue { + t.Errorf("Expected key '%s' with value %d, but got key '%s' with value %d", expectedKey, expectedValue, expectedKey, retrievedValue) + } +} + +// TestAddValuesFunc tests the AddValuesFunc method of the Map. +func TestAddValuesFunc(t *testing.T) { + // Test case 1: Adding multiple key-value pairs to the map using a slice of values and a key calculation function. + newMap := &gomap.Map[string, int]{} // Create an empty map. + values := []int{5, 10, 15} + keyCalculationFunc := func(i int, value int) string { + return strconv.Itoa(value) + strconv.Itoa(i) + } + newMap.AddValuesFunc(values, keyCalculationFunc) // Adds keys "50", "101", and "152" with values 5, 10, and 15 to the map. + + // Verify that the key-value pairs have been added correctly. + expectedKeyValues := map[string]int{ + "50": 5, + "101": 10, + "152": 15, + } + + for key, expectedValue := range expectedKeyValues { + actualValue, ok := newMap.Get(key) + if !ok { + t.Errorf("Expected key '%s' to be in the map, but it was not found.", key) + } else if actualValue != expectedValue { + t.Errorf("Expected value %d for key '%s', but got %d", expectedValue, key, actualValue) + } + } +} + // TestContains tests Map.Contains. func TestContains(t *testing.T) { // Test case 1: Check for a value in an empty gomap.