Merge pull request #82 from loxp/master

Add Floor and Ceiling method to Treemap
development
Emir Pasic 7 years ago committed by GitHub
commit 95d9cc1eea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -105,6 +105,28 @@ func (m *Map) Max() (key interface{}, value interface{}) {
return nil, nil return nil, nil
} }
// Floor searches the floor element in the map by key.
// Returns floor key, floor value, true if floor key is found.
// Returns nil, nil, false if floor key is not found.
func (m *Map) Floor(key interface{}) (retKey interface{}, retValue interface{}, found bool) {
ret, found := m.tree.Floor(key)
if !found {
return nil, nil, false
}
return ret.Key, ret.Value, true
}
// Ceiling searches the ceiling element in the map by key.
// Returns ceiling key, ceiling value, true if a ceiling key is found.
// Returns nil, nil, false if ceiling key is not found.
func (m *Map) Ceiling(key interface{}) (retKey interface{}, retValue interface{}, found bool) {
ret, found := m.tree.Ceiling(key)
if !found {
return nil, nil, false
}
return ret.Key, ret.Value, true
}
// String returns a string representation of container // String returns a string representation of container
func (m *Map) String() string { func (m *Map) String() string {
str := "TreeMap\nmap[" str := "TreeMap\nmap["

@ -289,6 +289,60 @@ func TestMapChaining(t *testing.T) {
} }
} }
func TestMapFloor(t *testing.T) {
m := NewWithIntComparator()
m.Put(7, "g")
m.Put(3, "c")
m.Put(1, "a")
// key,expectedKey,expectedValue,expectedFound
tests1 := [][]interface{}{
{-1, nil, nil, false},
{0, nil, nil, false},
{1, 1, "a", true},
{2, 1, "a", true},
{3, 3, "c", true},
{4, 3, "c", true},
{7, 7, "g", true},
{8, 7, "g", true},
}
for _, test := range tests1 {
// retrievals
actualKey, actualValue, actualFound := m.Floor(test[0])
if actualKey != test[1] || actualValue != test[2] || actualFound != test[3] {
t.Errorf("Got %v, %v, %v, expected %v, %v, %v", actualKey, actualValue, actualFound, test[1], test[2], test[3])
}
}
}
func TestMapCeiling(t *testing.T) {
m := NewWithIntComparator()
m.Put(7, "g")
m.Put(3, "c")
m.Put(1, "a")
// key,expectedKey,expectedValue,expectedFound
tests1 := [][]interface{}{
{-1, 1, "a", true},
{0, 1, "a", true},
{1, 1, "a", true},
{2, 3, "c", true},
{3, 3, "c", true},
{4, 7, "g", true},
{7, 7, "g", true},
{8, nil, nil, false},
}
for _, test := range tests1 {
// retrievals
actualKey, actualValue, actualFound := m.Ceiling(test[0])
if actualKey != test[1] || actualValue != test[2] || actualFound != test[3] {
t.Errorf("Got %v, %v, %v, expected %v, %v, %v", actualKey, actualValue, actualFound, test[1], test[2], test[3])
}
}
}
func TestMapIteratorNextOnEmpty(t *testing.T) { func TestMapIteratorNextOnEmpty(t *testing.T) {
m := NewWithStringComparator() m := NewWithStringComparator()
it := m.Iterator() it := m.Iterator()

Loading…
Cancel
Save