Skip to content

Commit

Permalink
Allow pre-allocating linked.Hashmap (#2918)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Apr 5, 2024
1 parent 88d304c commit 4a1d0bb
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions utils/linked/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ type Hashmap[K comparable, V any] struct {
}

func NewHashmap[K comparable, V any]() *Hashmap[K, V] {
return &Hashmap[K, V]{
entryMap: make(map[K]*ListElement[keyValue[K, V]]),
return NewHashmapWithSize[K, V](0)
}

func NewHashmapWithSize[K comparable, V any](initialSize int) *Hashmap[K, V] {
lh := &Hashmap[K, V]{
entryMap: make(map[K]*ListElement[keyValue[K, V]], initialSize),
entryList: NewList[keyValue[K, V]](),
freeList: make([]*ListElement[keyValue[K, V]], initialSize),
}
for i := range lh.freeList {
lh.freeList[i] = &ListElement[keyValue[K, V]]{}
}
return lh
}

func (lh *Hashmap[K, V]) Put(key K, value V) {
Expand Down

0 comments on commit 4a1d0bb

Please sign in to comment.