Source file
src/net/http/mapping.go
1
2
3
4
5 package http
6
7
8
9
10 type mapping[K comparable, V any] struct {
11 s []entry[K, V]
12 m map[K]V
13 }
14
15 type entry[K comparable, V any] struct {
16 key K
17 value V
18 }
19
20
21
22 var maxSlice int = 8
23
24
25 func (h *mapping[K, V]) add(k K, v V) {
26 if h.m == nil && len(h.s) < maxSlice {
27 h.s = append(h.s, entry[K, V]{k, v})
28 } else {
29 if h.m == nil {
30 h.m = map[K]V{}
31 for _, e := range h.s {
32 h.m[e.key] = e.value
33 }
34 h.s = nil
35 }
36 h.m[k] = v
37 }
38 }
39
40
41
42
43 func (h *mapping[K, V]) find(k K) (v V, found bool) {
44 if h == nil {
45 return v, false
46 }
47 if h.m != nil {
48 v, found = h.m[k]
49 return v, found
50 }
51 for _, e := range h.s {
52 if e.key == k {
53 return e.value, true
54 }
55 }
56 return v, false
57 }
58
59
60
61 func (h *mapping[K, V]) eachPair(f func(k K, v V) bool) {
62 if h == nil {
63 return
64 }
65 if h.m != nil {
66 for k, v := range h.m {
67 if !f(k, v) {
68 return
69 }
70 }
71 } else {
72 for _, e := range h.s {
73 if !f(e.key, e.value) {
74 return
75 }
76 }
77 }
78 }
79
View as plain text