Skip to content

Commit

Permalink
Support nil maps
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanh committed Nov 22, 2023
1 parent 3b4fe39 commit cb87ad4
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func NewMap[K any, V any](equal func(k1, k2 K) bool, hash func(k K) uint64, buck

// returns the number of elements in the map.
func (m *Map[K, V]) Len() int {
if m == nil {
return 0
}
return m.len
}

Expand All @@ -59,6 +62,9 @@ func (m *Map[K, V]) Clear() {

// returns the value associated with the given key.
func (m *Map[K, V]) Get(key K) (V, bool) {
if m == nil {
return *new(V), false
}
hash := m.hash(key)
bucketID := hash % uint64(len(m.buckets))
bucket := m.buckets[bucketID]
Expand Down Expand Up @@ -260,6 +266,9 @@ type MapIterator[K any, V any] struct {

// Next advances the iterator and returns true if there is another element
func (it *MapIterator[K, V]) Next() bool {
if it.m == nil {
return false
}
if it.ready {
// ensure the cursor is moved
it.pos++
Expand Down

0 comments on commit cb87ad4

Please sign in to comment.