diff --git a/README.md b/README.md index a584a2ae..2d9b9929 100644 --- a/README.md +++ b/README.md @@ -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{} @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/maps/hashbidimap/hashbidimap.go b/maps/hashbidimap/hashbidimap.go index 5a386ec7..49d9bdab 100644 --- a/maps/hashbidimap/hashbidimap.go +++ b/maps/hashbidimap/hashbidimap.go @@ -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 { diff --git a/maps/hashbidimap/hashbidimap_test.go b/maps/hashbidimap/hashbidimap_test.go index dd911165..bd1bb917 100644 --- a/maps/hashbidimap/hashbidimap_test.go +++ b/maps/hashbidimap/hashbidimap_test.go @@ -7,6 +7,7 @@ package hashbidimap import ( "encoding/json" "fmt" + "reflect" "strings" "testing" ) @@ -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) diff --git a/maps/hashmap/hashmap.go b/maps/hashmap/hashmap.go index e945c39f..5d996f27 100644 --- a/maps/hashmap/hashmap.go +++ b/maps/hashmap/hashmap.go @@ -13,6 +13,7 @@ package hashmap import ( "fmt" + "github.com/emirpasic/gods/maps" ) @@ -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) diff --git a/maps/hashmap/hashmap_test.go b/maps/hashmap/hashmap_test.go index 91acca8d..d6d1de7c 100644 --- a/maps/hashmap/hashmap_test.go +++ b/maps/hashmap/hashmap_test.go @@ -7,6 +7,7 @@ package hashmap import ( "encoding/json" "fmt" + "reflect" "strings" "testing" ) @@ -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) diff --git a/maps/linkedhashmap/linkedhashmap.go b/maps/linkedhashmap/linkedhashmap.go index d625b3d7..81979cfc 100644 --- a/maps/linkedhashmap/linkedhashmap.go +++ b/maps/linkedhashmap/linkedhashmap.go @@ -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{}) { diff --git a/maps/linkedhashmap/linkedhashmap_test.go b/maps/linkedhashmap/linkedhashmap_test.go index ca44792c..a1db621c 100644 --- a/maps/linkedhashmap/linkedhashmap_test.go +++ b/maps/linkedhashmap/linkedhashmap_test.go @@ -7,6 +7,7 @@ package linkedhashmap import ( "encoding/json" "fmt" + "reflect" "strings" "testing" ) @@ -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) diff --git a/maps/maps.go b/maps/maps.go index cdce9f7b..edf5e825 100644 --- a/maps/maps.go +++ b/maps/maps.go @@ -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{} diff --git a/maps/treebidimap/treebidimap.go b/maps/treebidimap/treebidimap.go index 37af07e0..e1619e01 100644 --- a/maps/treebidimap/treebidimap.go +++ b/maps/treebidimap/treebidimap.go @@ -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 @@ -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) { diff --git a/maps/treebidimap/treebidimap_test.go b/maps/treebidimap/treebidimap_test.go index 75296e80..647fb456 100644 --- a/maps/treebidimap/treebidimap_test.go +++ b/maps/treebidimap/treebidimap_test.go @@ -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) { @@ -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 diff --git a/maps/treemap/treemap.go b/maps/treemap/treemap.go index a77d16d8..b4779da7 100644 --- a/maps/treemap/treemap.go +++ b/maps/treemap/treemap.go @@ -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 @@ -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{}) { diff --git a/maps/treemap/treemap_test.go b/maps/treemap/treemap_test.go index f0c96baa..cb2c702e 100644 --- a/maps/treemap/treemap_test.go +++ b/maps/treemap/treemap_test.go @@ -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) { @@ -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)