Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add GetDefault func for map interface #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ Implements [Container](#containers) interface.
type Map interface {
Put(key interface{}, value interface{})
Get(key interface{}) (value interface{}, found bool)
GetDefault(key interface{}, defaultValue interface{}) (value interface{})
Remove(key interface{})
Keys() []interface{}

Expand Down Expand Up @@ -476,6 +477,8 @@ func main() {
m.Put(1, "a") // 2->b, 1->a (random order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.GetDefault(2, "8") // b
_ = m.GetDefault(3, "8") // 8
_ = m.Values() // []interface {}{"b", "a"} (random order)
_ = m.Keys() // []interface {}{1, 2} (random order)
m.Remove(1) // 2->b
Expand Down Expand Up @@ -503,6 +506,8 @@ func main() {
m.Put(1, "a") // 1->a, 2->b (in order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.GetDefault(2, "8") // b
_ = m.GetDefault(3, "8") // 8
_ = m.Values() // []interface {}{"a", "b"} (in order)
_ = m.Keys() // []interface {}{1, 2} (in order)
m.Remove(1) // 2->b
Expand Down Expand Up @@ -534,6 +539,8 @@ func main() {
m.Put(1, "a") // 2->b, 1->a (insertion-order)
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.GetDefault(2, "8") // b
_ = m.GetDefault(3, "8") // 8
_ = m.Values() // []interface {}{"b", "a"} (insertion-order)
_ = m.Keys() // []interface {}{2, 1} (insertion-order)
m.Remove(1) // 2->b
Expand Down Expand Up @@ -564,6 +571,8 @@ func main() {
_, _ = m.GetKey("a") // 1, true
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.GetDefault(2, "8") // b
_ = m.GetDefault(3, "8") // 8
_ = m.Values() // []interface {}{"a", "b"} (random order)
_ = m.Keys() // []interface {}{1, 2} (random order)
m.Remove(1) // 2->b
Expand Down Expand Up @@ -596,6 +605,8 @@ func main() {
_, _ = m.GetKey("a") // 1, true
_, _ = m.Get(2) // b, true
_, _ = m.Get(3) // nil, false
_ = m.GetDefault(2, "8") // b
_ = m.GetDefault(3, "8") // 8
_ = m.Values() // []interface {}{"a", "b"} (ordered)
_ = m.Keys() // []interface {}{1, 2} (ordered)
m.Remove(1) // 2->b
Expand Down
10 changes: 10 additions & 0 deletions maps/hashbidimap/hashbidimap.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ func (m *Map) GetKey(value interface{}) (key interface{}, found bool) {
return m.inverseMap.Get(value)
}

// Get searches the element in the map by key and returns its value of default value given by second argument if key is not found in map.
func (m *Map) GetDefault(key interface{}, defaultValue interface{}) (value interface{}) {
var found bool = false
value, found = m.Get(key)
if !found {
value = defaultValue
}
return
}

// Remove removes the element from the map by key.
func (m *Map) Remove(key interface{}) {
if value, found := m.forwardMap.Get(key); found {
Expand Down
16 changes: 16 additions & 0 deletions maps/hashbidimap/hashbidimap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package hashbidimap
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -152,6 +153,21 @@ func TestMapGetKey(t *testing.T) {
}
}

func TestMapGetDefault(t *testing.T) {
m := New()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
val1 := m.GetDefault("c", 8)
if !reflect.DeepEqual(val1, 3) {
t.Errorf("Got %v expected %v", val1, 3)
}
val2 := m.GetDefault("e", 8)
if !reflect.DeepEqual(val2, 8) {
t.Errorf("Got %v expected %v", val2, 8)
}
}

func TestMapSerialization(t *testing.T) {
m := New()
m.Put("a", 1.0)
Expand Down
11 changes: 11 additions & 0 deletions maps/hashmap/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package hashmap

import (
"fmt"

"github.com/emirpasic/gods/maps"
)

Expand Down Expand Up @@ -41,6 +42,16 @@ func (m *Map) Get(key interface{}) (value interface{}, found bool) {
return
}

// Get searches the element in the map by key and returns its value of default value given by second argument if key is not found in map.
func (m *Map) GetDefault(key interface{}, defaultValue interface{}) (value interface{}) {
var found bool = false
value, found = m.Get(key)
if !found {
value = defaultValue
}
return
}

// Remove removes the element from the map by key.
func (m *Map) Remove(key interface{}) {
delete(m.m, key)
Expand Down
16 changes: 16 additions & 0 deletions maps/hashmap/hashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package hashmap
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -120,6 +121,21 @@ func TestMapRemove(t *testing.T) {
}
}

func TestMapGetDefault(t *testing.T) {
m := New()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
val1 := m.GetDefault("c", 8)
if !reflect.DeepEqual(val1, 3) {
t.Errorf("Got %v expected %v", val1, 3)
}
val2 := m.GetDefault("e", 8)
if !reflect.DeepEqual(val2, 8) {
t.Errorf("Got %v expected %v", val2, 8)
}
}

func TestMapSerialization(t *testing.T) {
m := New()
m.Put("a", 1.0)
Expand Down
10 changes: 10 additions & 0 deletions maps/linkedhashmap/linkedhashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func (m *Map) Get(key interface{}) (value interface{}, found bool) {
return
}

// Get searches the element in the map by key and returns its value of default value given by second argument if key is not found in map.
func (m *Map) GetDefault(key interface{}, defaultValue interface{}) (value interface{}) {
var found bool = false
value, found = m.Get(key)
if !found {
value = defaultValue
}
return
}

// Remove removes the element from the map by key.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (m *Map) Remove(key interface{}) {
Expand Down
16 changes: 16 additions & 0 deletions maps/linkedhashmap/linkedhashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package linkedhashmap
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -210,6 +211,21 @@ func TestMapSelect(t *testing.T) {
}
}

func TestMapGetDefault(t *testing.T) {
m := New()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
val1 := m.GetDefault("c", 8)
if !reflect.DeepEqual(val1, 3) {
t.Errorf("Got %v expected %v", val1, 3)
}
val2 := m.GetDefault("e", 8)
if !reflect.DeepEqual(val2, 8) {
t.Errorf("Got %v expected %v", val2, 8)
}
}

func TestMapAny(t *testing.T) {
m := New()
m.Put("c", 3)
Expand Down
1 change: 1 addition & 0 deletions maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import "github.com/emirpasic/gods/containers"
type Map interface {
Put(key interface{}, value interface{})
Get(key interface{}) (value interface{}, found bool)
GetDefault(key interface{}, defaultValue interface{}) (value interface{})
Remove(key interface{})
Keys() []interface{}

Expand Down
13 changes: 12 additions & 1 deletion maps/treebidimap/treebidimap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package treebidimap

import (
"fmt"
"strings"

"github.com/emirpasic/gods/maps"
"github.com/emirpasic/gods/trees/redblacktree"
"github.com/emirpasic/gods/utils"
"strings"
)

// Assert Map implementation
Expand Down Expand Up @@ -83,6 +84,16 @@ func (m *Map) Get(key interface{}) (value interface{}, found bool) {
return nil, false
}

// Get searches the element in the map by key and returns its value of default value given by second argument if key is not found in map.
func (m *Map) GetDefault(key interface{}, defaultValue interface{}) (value interface{}) {
var found bool = false
value, found = m.Get(key)
if !found {
value = defaultValue
}
return
}

// GetKey searches the element in the map by value and returns its key or nil if value is not found in map.
// Second return parameter is true if value was found, otherwise false.
func (m *Map) GetKey(value interface{}) (key interface{}, found bool) {
Expand Down
19 changes: 18 additions & 1 deletion maps/treebidimap/treebidimap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ package treebidimap
import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/utils"
"reflect"
"strings"
"testing"

"github.com/emirpasic/gods/utils"
)

func TestMapPut(t *testing.T) {
Expand Down Expand Up @@ -153,6 +155,21 @@ func TestMapGetKey(t *testing.T) {
}
}

func TestGetDefault(t *testing.T) {
m := NewWith(utils.StringComparator, utils.IntComparator)
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
val1 := m.GetDefault("c", 8)
if !reflect.DeepEqual(val1, 3) {
t.Errorf("Got %v expected %v", val1, 3)
}
val2 := m.GetDefault("e", 8)
if !reflect.DeepEqual(val2, 8) {
t.Errorf("Got %v expected %v", val2, 8)
}
}

func sameElements(a []interface{}, b []interface{}) bool {
if len(a) != len(b) {
return false
Expand Down
13 changes: 12 additions & 1 deletion maps/treemap/treemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ package treemap

import (
"fmt"
"strings"

"github.com/emirpasic/gods/maps"
rbt "github.com/emirpasic/gods/trees/redblacktree"
"github.com/emirpasic/gods/utils"
"strings"
)

// Assert Map implementation
Expand Down Expand Up @@ -55,6 +56,16 @@ func (m *Map) Get(key interface{}) (value interface{}, found bool) {
return m.tree.Get(key)
}

// Get searches the element in the map by key and returns its value of default value given by second argument if key is not found in map.
func (m *Map) GetDefault(key interface{}, defaultValue interface{}) (value interface{}) {
var found bool = false
value, found = m.Get(key)
if !found {
value = defaultValue
}
return
}

// Remove removes the element from the map by key.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (m *Map) Remove(key interface{}) {
Expand Down
19 changes: 18 additions & 1 deletion maps/treemap/treemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ package treemap
import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/utils"
"reflect"
"strings"
"testing"

"github.com/emirpasic/gods/utils"
)

func TestMapPut(t *testing.T) {
Expand Down Expand Up @@ -391,6 +393,21 @@ func TestMapFind(t *testing.T) {
}
}

func TestMapGetDefault(t *testing.T) {
m := NewWithStringComparator()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
val1 := m.GetDefault("c", 8)
if !reflect.DeepEqual(val1, 3) {
t.Errorf("Got %v expected %v", val1, 3)
}
val2 := m.GetDefault("e", 8)
if !reflect.DeepEqual(val2, 8) {
t.Errorf("Got %v expected %v", val2, 8)
}
}

func TestMapChaining(t *testing.T) {
m := NewWithStringComparator()
m.Put("c", 3)
Expand Down