Skip to content

Commit

Permalink
docs: document map read/write behaviour while ranging
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 8, 2020
1 parent e8c6e3c commit ff33133
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func (m *Map) Delete(key interface{}) bool {

// UnorderedRange will range over the map in an unordered sequence.
// This is same as ranging over a map using the "for range" syntax.
// Parameter func f should not call any method of the Map, eg Get, Put, Delete, UnorderedRange, OrderedRange etc
// It will cause a deadlock
func (m *Map) UnorderedRange(f func(key interface{}, value interface{})) {
m.mu.RLock()
for k, v := range m.mp {
Expand All @@ -83,6 +85,8 @@ func (m *Map) UnorderedRange(f func(key interface{}, value interface{})) {
// This is way faster than UnorderedRange. For a map containing 10_000_000 items
// UnorderedRange completes in ~1.7 seconds,
// OrderedRange completes in ~98 milli seconds.
// Parameter func f should not call any method of the Map, eg Get, Put, Delete, UnorderedRange, OrderedRange etc
// It will cause a deadlock
func (m *Map) OrderedRange(f func(key interface{}, value interface{})) {
m.mu.RLock()
cur := m.dll.Back()
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ func main() {
// UnorderedRange will iterate over the Map in a random sequence.
// This is same as ranging over a map using the "for range" syntax.
// Parameter function should not call any methods associated with the map.
m.UnorderedRange(func(key interface{}, val interface{}) {
log.Println("Key - %v, Value - %v", key, val)
})
// OrderedRange will iterate over the Map in the sequence in which
// elements were added.
// Parameter function should not call any methods associated with the map.
m.OrderedRange(func(key interface{}, val interface{}) {
log.Println("Key - %v, Value - %v", key, val)
})
Expand Down

0 comments on commit ff33133

Please sign in to comment.