diff options
author | Am Laher <amir.laher@tixtrack.com> | 2022-12-28 20:59:28 +1300 |
---|---|---|
committer | Am Laher <amir.laher@tixtrack.com> | 2022-12-28 20:59:28 +1300 |
commit | 48d927aaaff3fd21098623414c2307b7e92d8f58 (patch) | |
tree | b8bcdeb60447a14f8f0659632daa240ef6ea025d | |
parent | Fix SortedSet.Set. NewMapOf. docs (diff) | |
download | pds-48d927aaaff3fd21098623414c2307b7e92d8f58.tar.gz pds-48d927aaaff3fd21098623414c2307b7e92d8f58.tar.xz |
SortedMap: set multiple items at once
-rw-r--r-- | immutable.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/immutable.go b/immutable.go index daf433c..b3d7dcb 100644 --- a/immutable.go +++ b/immutable.go @@ -1608,6 +1608,20 @@ func NewSortedMap[K comparable, V any](comparer Comparer[K]) *SortedMap[K, V] { } } +// NewSortedMapOf returns a new instance of SortedMap, containing a map of provided entries. +// +// If comparer is nil then a default comparer is set after the first key is inserted. Default comparers +// exist for int, string, and byte slice keys. +func NewSortedMapOf[K comparable, V any](comparer Comparer[K], entries map[K]V) *SortedMap[K, V] { + m := &SortedMap[K, V]{ + comparer: comparer, + } + for k, v := range entries { + m.set(k, v, true) + } + return m +} + // Len returns the number of elements in the sorted map. func (m *SortedMap[K, V]) Len() int { return m.size @@ -1628,6 +1642,15 @@ func (m *SortedMap[K, V]) Set(key K, value V) *SortedMap[K, V] { return m.set(key, value, false) } +// SetMany returns a map with the keys set to the new values. +func (m *SortedMap[K, V]) SetMany(entries map[K]V) *SortedMap[K, V] { + n := m.clone() + for k, v := range entries { + n.set(k, v, true) + } + return n +} + func (m *SortedMap[K, V]) set(key K, value V, mutable bool) *SortedMap[K, V] { // Set a comparer on the first value if one does not already exist. comparer := m.comparer |