Debian Codegraph @

gopls/internal/util/immutable/immutable.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // The immutable package defines immutable wrappers around common data
6 // structures. These are used for additional type safety inside gopls.
7 //
8 // See the "persistent" package for copy-on-write data structures.
9 package immutable
10
11 import (
12 "iter"
13 "maps"
14 )
15
16 // Map is an immutable wrapper around an ordinary Go map.
17 type Map[K comparable, V any] struct {
18 m map[K]V
19 }
20
21 // MapOf wraps the given Go map.
22 //
23 // The caller must not subsequently mutate the map.
24 func MapOf[K comparable, V any](m map[K]V) Map[K, V] {
25 return Map[K, V]{m}
26 }
27
28 // Value returns the mapped value for k.
29 // It is equivalent to the commaok form of an ordinary go map, and returns
30 // (zero, false) if the key is not present.
31 func (m Map[K, V]) Value(k K) (V, bool) {
32 v, ok := m.m[k]
33 return v, ok
34 }
35
36 // Len returns the number of entries in the Map.
37 func (m Map[K, V]) Len() int {
38 return len(m.m)
39 }
40
41 // All returns an iterator over each mapped (key, value) pair.
42 func (m Map[K, V]) All() iter.Seq2[K, V] {
43 return maps.All(m.m)
44 }