File: immutable.go

package info (click to toggle)
golang-github-cue-lang-cue 0.12.0.-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,072 kB
  • sloc: sh: 57; makefile: 17
file content (43 lines) | stat: -rw-r--r-- 1,240 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// The immutable package defines immutable wrappers around common data
// structures. These are used for additional type safety inside gopls.
//
// See the "persistent" package for copy-on-write data structures.
package immutable

// Map is an immutable wrapper around an ordinary Go map.
type Map[K comparable, V any] struct {
	m map[K]V
}

// MapOf wraps the given Go map.
//
// The caller must not subsequently mutate the map.
func MapOf[K comparable, V any](m map[K]V) Map[K, V] {
	return Map[K, V]{m}
}

// Value returns the mapped value for k.
// It is equivalent to the commaok form of an ordinary go map, and returns
// (zero, false) if the key is not present.
func (m Map[K, V]) Value(k K) (V, bool) {
	v, ok := m.m[k]
	return v, ok
}

// Len returns the number of entries in the Map.
func (m Map[K, V]) Len() int {
	return len(m.m)
}

// Range calls f for each mapped (key, value) pair.
// There is no way to break out of the loop.
// TODO: generalize when Go iterators (#61405) land.
func (m Map[K, V]) Range(f func(k K, v V)) {
	for k, v := range m.m {
		f(k, v)
	}
}