Skip to content

Commit

Permalink
feat: add map methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
ghosind committed May 10, 2023
1 parent 2c478e8 commit 152444b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
44 changes: 44 additions & 0 deletions hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,32 @@ func (m *HashMap[K, V]) Get(k K) (V, bool) {
return v, ok
}

// GetDefault returns the value associated with the specified key, and returns the default value if
// this map contains no pair with the key.
func (m *HashMap[K, V]) GetDefault(k K, defaultVal V) V {
v, ok := (*m)[k]
if !ok {
return defaultVal
}

return v
}

// IsEmpty returns true if this map is empty.
func (m *HashMap[K, V]) IsEmpty() bool {
return m.Size() == 0
}

// Keys returns a slice that contains all the keys in this map.
func (m *HashMap[K, V]) Keys() []K {
keys := make([]K, 0, len(*m))
for k := range *m {
keys = append(keys, k)
}

return keys
}

// Put associate the specified value with the specified key in this map.
func (m *HashMap[K, V]) Put(k K, v V) V {
old := (*m)[k]
Expand All @@ -101,7 +122,30 @@ func (m *HashMap[K, V]) Remove(k K) V {
return old
}

// Replace replaces the value for the specified key only if it is currently in this map.
func (m *HashMap[K, V]) Replace(k K, v V) (V, bool) {
old, ok := (*m)[k]
if !ok {
return old, false // zero value
}

(*m)[k] = v

return old, true
}

// Size returns the number of key-value pairs in this map.
func (m *HashMap[K, V]) Size() int {
return len(*m)
}

// Values returns a slice that contains all the values in this map.
func (m *HashMap[K, V]) Values() []V {
arr := make([]V, 0, len(*m))

for _, v := range *m {
arr = append(arr, v)
}

return arr
}
13 changes: 13 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,28 @@ type Map[K comparable, V any] interface {
// Get returns the value which associated to the specified key.
Get(k K) (V, bool)

// GetDefault returns the value associated with the specified key, and returns the default value
// if this map contains no pair with the key.
GetDefault(k K, defaultVal V) V

// IsEmpty returns true if this map is empty.
IsEmpty() bool

// Keys returns a slice that contains all the keys in this map.
Keys() []K

// Put associate the specified value with the specified key in this map.
Put(k K, v V) V

// Remove removes the key-value pair with the specified key.
Remove(k K) V

// Replace replaces the value for the specified key only if it is currently in this map.
Replace(k K, v V) (V, bool)

// Size returns the number of key-value pairs in this map.
Size() int

// Values returns a slice that contains all the values in this map.
Values() []V
}
13 changes: 13 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ func testMapGet(t *testing.T, m Map[int, int], data []int) {
}
}
}

for i := 0; i < len(data)*2; i++ {
v := m.GetDefault(i, i+1)
if i < len(data) {
if v != data[i] {
t.Errorf("HashMap.GetDefault(%d) returns %d, expect %d", i, v, data[i])
}
} else {
if v != i+1 {
t.Errorf("HashMap.GetDefault(%d) returns %d, expect %d", i, v, i+1)
}
}
}
}

func testMapContains(t *testing.T, m Map[int, int], data []int) {
Expand Down

0 comments on commit 152444b

Please sign in to comment.