From ee611860aa497ae52621bed17bafb350827720c5 Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Mon, 9 Dec 2024 22:06:05 +0300 Subject: [PATCH] create the map with capacity on copy --- orderedmap.go | 2 +- v2/orderedmap.go | 6 ++---- v3/orderedmap.go | 6 ++---- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/orderedmap.go b/orderedmap.go index d14f361..a39422e 100644 --- a/orderedmap.go +++ b/orderedmap.go @@ -109,7 +109,7 @@ func (m *OrderedMap) Back() *Element { // Copy returns a new OrderedMap with the same elements. // Using Copy while there are concurrent writes may mangle the result. func (m *OrderedMap) Copy() *OrderedMap { - m2 := NewOrderedMap() + m2 := NewOrderedMapWithCapacity(m.Len()) for el := m.Front(); el != nil; el = el.Next() { m2.Set(el.Key, el.Value) diff --git a/v2/orderedmap.go b/v2/orderedmap.go index f6ab5f3..3fa4f94 100644 --- a/v2/orderedmap.go +++ b/v2/orderedmap.go @@ -20,9 +20,7 @@ func NewOrderedMapWithCapacity[K comparable, V any](capacity int) *OrderedMap[K, } func NewOrderedMapWithElements[K comparable, V any](els ...*Element[K, V]) *OrderedMap[K, V] { - om := &OrderedMap[K, V]{ - kv: make(map[K]*Element[K, V], len(els)), - } + om := NewOrderedMapWithCapacity[K, V](len(els)) for _, el := range els { om.Set(el.Key, el.Value) } @@ -134,7 +132,7 @@ func (m *OrderedMap[K, V]) Back() *Element[K, V] { // Copy returns a new OrderedMap with the same elements. // Using Copy while there are concurrent writes may mangle the result. func (m *OrderedMap[K, V]) Copy() *OrderedMap[K, V] { - m2 := NewOrderedMap[K, V]() + m2 := NewOrderedMapWithCapacity[K, V](m.Len()) for el := m.Front(); el != nil; el = el.Next() { m2.Set(el.Key, el.Value) } diff --git a/v3/orderedmap.go b/v3/orderedmap.go index 7dbfbae..ec4049c 100644 --- a/v3/orderedmap.go +++ b/v3/orderedmap.go @@ -22,9 +22,7 @@ func NewOrderedMapWithCapacity[K comparable, V any](capacity int) *OrderedMap[K, } func NewOrderedMapWithElements[K comparable, V any](els ...*Element[K, V]) *OrderedMap[K, V] { - om := &OrderedMap[K, V]{ - kv: make(map[K]*Element[K, V], len(els)), - } + om := NewOrderedMapWithCapacity[K, V](len(els)) for _, el := range els { om.Set(el.Key, el.Value) } @@ -175,7 +173,7 @@ func (m *OrderedMap[K, V]) Back() *Element[K, V] { // Copy returns a new OrderedMap with the same elements. // Using Copy while there are concurrent writes may mangle the result. func (m *OrderedMap[K, V]) Copy() *OrderedMap[K, V] { - m2 := NewOrderedMap[K, V]() + m2 := NewOrderedMapWithCapacity[K, V](m.Len()) for el := m.Front(); el != nil; el = el.Next() { m2.Set(el.Key, el.Value) }