diff options
-rw-r--r-- | src/pds.go | 30 |
1 files changed, 16 insertions, 14 deletions
@@ -717,16 +717,18 @@ func NewMap[K, V any](hasher Hasher[K]) *Map[K, V] { // NewMapOf returns a new instance of Map, containing a map of provided entries. // -// If hasher is nil, a default hasher implementation will automatically be chosen based on the first key added. -// Default hasher implementations only exist for int, string, and byte slice types. -func NewMapOf[K comparable, V any](hasher Hasher[K], entries map[K]V) *Map[K, V] { - m := &Map[K, V]{ - hasher: hasher, - } +// If hasher is nil, a default hasher implementation will automatically be +// chosen based on the first key added. Default hasher implementations only +// exist for int, string, and byte slice types. +func NewMapOf[K comparable, V any]( + hasher Hasher[K], + entries map[K]V, +) *Map[K, V] { + m := NewMapBuilder[K, V](hasher) for k, v := range entries { - m.set(k, v, true) + m.Set(k, v) } - return m + return m.Map() } // Len returns the number of elements in the map. @@ -842,7 +844,7 @@ func NewMapBuilder[K, V any](hasher Hasher[K]) *MapBuilder[K, V] { // Map returns the underlying map. Only call once. // Builder is invalid after call. Will panic on second invocation. func (b *MapBuilder[K, V]) Map() *Map[K, V] { - assert(b.m != nil, "immutable.SortedMapBuilder.Map(): duplicate call to fetch map") + assert(b.m != nil, "duplicate call to builder.Map()") m := b.m b.m = nil return m @@ -850,31 +852,31 @@ func (b *MapBuilder[K, V]) Map() *Map[K, V] { // Len returns the number of elements in the underlying map. func (b *MapBuilder[K, V]) Len() int { - assert(b.m != nil, "immutable.MapBuilder: builder invalid after Map() invocation") + assert(b.m != nil, "builder is invalid after Map() invocation") return b.m.Len() } // Get returns the value for the given key. func (b *MapBuilder[K, V]) Get(key K) (value V, ok bool) { - assert(b.m != nil, "immutable.MapBuilder: builder invalid after Map() invocation") + assert(b.m != nil, "builder is invalid after Map() invocation") return b.m.Get(key) } // Set sets the value of the given key. See Map.Set() for additional details. func (b *MapBuilder[K, V]) Set(key K, value V) { - assert(b.m != nil, "immutable.MapBuilder: builder invalid after Map() invocation") + assert(b.m != nil, "builder is invalid after Map() invocation") b.m = b.m.set(key, value, true) } // Delete removes the given key. See Map.Delete() for additional details. func (b *MapBuilder[K, V]) Delete(key K) { - assert(b.m != nil, "immutable.MapBuilder: builder invalid after Map() invocation") + assert(b.m != nil, "builder is invalid after Map() invocation") b.m = b.m.delete(key, true) } // Iterator returns a new iterator for the underlying map. func (b *MapBuilder[K, V]) Iterator() *MapIterator[K, V] { - assert(b.m != nil, "immutable.MapBuilder: builder invalid after Map() invocation") + assert(b.m != nil, "builder is invalid after Map() invocation") return b.m.Iterator() } |