diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-12 16:17:02 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-12 16:17:02 -0700 |
commit | cfcc753dc8906817e15b1b7c36b4dc12462d12e4 (patch) | |
tree | f5d2aa0caaa4cb336017ba7595c3425f4aa00bfc /lib/containers/maputil.go | |
parent | 29b6b9f997913f13a0bff8bb1278a61302413615 (diff) | |
parent | f76faa4b8debd9c94751a03dd65e46c80a340a82 (diff) |
Merge branch 'lukeshu/fast'
Diffstat (limited to 'lib/containers/maputil.go')
-rw-r--r-- | lib/containers/maputil.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/containers/maputil.go b/lib/containers/maputil.go new file mode 100644 index 0000000..4d49d2a --- /dev/null +++ b/lib/containers/maputil.go @@ -0,0 +1,32 @@ +// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package containers + +type Map[K comparable, V any] interface { + Store(K, V) + Load(K) (V, bool) + Has(K) bool + Delete(K) + Len() int +} + +type RangeMap[K comparable, V any] interface { + Map[K, V] + Range(func(K, V) bool) +} + +type SubrangeMap[K comparable, V any] interface { + RangeMap[K, V] + Subrange(rangeFn func(K, V) int, handleFn func(K, V) bool) +} + +func LoadOrElse[K comparable, V any](m Map[K, V], k K, vFn func(K) V) V { + if v, ok := m.Load(k); ok { + return v + } + v := vFn(k) + m.Store(k, v) + return v +} |