Skip to content

Commit

Permalink
SortedMap: set multiple items at once
Browse files Browse the repository at this point in the history
  • Loading branch information
laher committed Dec 28, 2022
1 parent 1fed1a3 commit 48d927a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions immutable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 48d927a

Please sign in to comment.