From d91f8ce17a6fc165fafd9dc921911233a69c34d2 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 9 Jan 2023 14:23:23 -0700 Subject: tree-wide: Migrate to the new ARCache --- lib/containers/maputil.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/containers/maputil.go (limited to 'lib/containers/maputil.go') 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 +// +// 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 +} -- cgit v1.2.3-54-g00ecf