Skip to content

Commit

Permalink
Add function to get length of Map
Browse files Browse the repository at this point in the history
Signed-off-by: m-murad <[email protected]>
  • Loading branch information
m-murad committed Aug 13, 2021
1 parent ff33133 commit bbe8ac6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
26 changes: 19 additions & 7 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ func New() *Map {
// The ok result indicates whether value was found in the map.
func (m *Map) Get(key interface{}) (interface{}, bool) {
m.mu.RLock()
defer m.mu.RUnlock()

v, ok := m.mp[key]
if !ok {
m.mu.RUnlock()
return nil, false
}
m.mu.RUnlock()

me := v.Value.(mapElement)
return me.value, ok
}
Expand All @@ -45,27 +46,28 @@ func (m *Map) Get(key interface{}) (interface{}, bool) {
// even if the values are same.
func (m *Map) Put(key interface{}, val interface{}) {
m.mu.Lock()
defer m.mu.Unlock()

if e, ok := m.mp[key]; !ok {
m.mp[key] = m.dll.PushFront(mapElement{key: key, value: val})
} else {
e.Value = mapElement{key: key, value: val}
}
m.mu.Unlock()
}

// Delete deletes the value for a key.
// It returns a boolean indicating weather the key existed and it was deleted.
func (m *Map) Delete(key interface{}) bool {
m.mu.Lock()
defer m.mu.Unlock()

e, ok := m.mp[key]
if !ok {
m.mu.Unlock()
return false
}

m.dll.Remove(e)
delete(m.mp, key)
m.mu.Unlock()
return true
}

Expand All @@ -75,10 +77,11 @@ func (m *Map) Delete(key interface{}) bool {
// It will cause a deadlock
func (m *Map) UnorderedRange(f func(key interface{}, value interface{})) {
m.mu.RLock()
defer m.mu.RUnlock()

for k, v := range m.mp {
f(k, v.Value.(mapElement).value)
}
m.mu.RUnlock()
}

// OrderedRange will range over the map in ab ordered sequence.
Expand All @@ -89,11 +92,20 @@ func (m *Map) UnorderedRange(f func(key interface{}, value interface{})) {
// It will cause a deadlock
func (m *Map) OrderedRange(f func(key interface{}, value interface{})) {
m.mu.RLock()
defer m.mu.RUnlock()

cur := m.dll.Back()
for cur != nil {
me := cur.Value.(mapElement)
f(me.key, me.value)
cur = cur.Prev()
}
m.mu.RUnlock()
}

// Length will return the length of Map
func (m *Map) Length() int {
m.mu.RLock()
defer m.mu.RUnlock()

return m.dll.Len()
}
44 changes: 44 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,47 @@ func TestOrderedRange(t *testing.T) {
}
m.OrderedRange(rangeFunc)
}

func TestLength(t *testing.T) {
m := initMap()

m.Put("a", 1)
m.Put("b", 2)
if m.Length() != 2 {
t.FailNow()
}

m.Put("c", 2)
m.Put("d", 2)
m.Put("e", 2)
if m.Length() != 5 {
t.FailNow()
}

m.Put("e", 3)
if m.Length() != 5 {
t.FailNow()
}

m.Delete("a")
if m.Length() != 4 {
t.FailNow()
}

m.Delete("does_not_exist")
if m.Length() != 4 {
t.FailNow()
}

m.Delete("b")
m.Delete("c")
m.Delete("d")
if m.Length() != 1 {
t.FailNow()
}

m.Delete("e")
if m.Length() != 0 {
t.FailNow()
}
}
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ func main() {
m.OrderedRange(func(key interface{}, val interface{}) {
log.Println("Key - %v, Value - %v", key, val)
})
// Length will return the length of the Map.
m.Length()
}
```

0 comments on commit bbe8ac6

Please sign in to comment.