Skip to content

Commit

Permalink
Dev (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsaygelle authored Oct 27, 2023
1 parent 35383a1 commit 9d2c2b8
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
36 changes: 36 additions & 0 deletions gomap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
52 changes: 52 additions & 0 deletions gomap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"
"reflect"
"sort"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 9d2c2b8

Please sign in to comment.