Debian Codegraph @

common/hmaps/ordered.go

raw
1 // Copyright 2026 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hmaps
15
16 import (
17 "slices"
18
19 "github.com/gohugoio/hugo/common/hashing"
20 )
21
22 // Ordered is a map that can be iterated in the order of insertion.
23 // Note that insertion order is not affected if a key is re-inserted into the map.
24 // In a nil map, all operations are no-ops.
25 // This is not thread safe.
26 type Ordered[K comparable, T any] struct {
27 // The keys in the order they were added.
28 keys []K
29 // The values.
30 values map[K]T
31 }
32
33 // NewOrdered creates a new Ordered map.
34 func NewOrdered[K comparable, T any]() *Ordered[K, T] {
35 return &Ordered[K, T]{values: make(map[K]T)}
36 }
37
38 // Set sets the value for the given key.
39 // Note that insertion order is not affected if a key is re-inserted into the map.
40 func (m *Ordered[K, T]) Set(key K, value T) {
41 if m == nil {
42 return
43 }
44 // Check if key already exists.
45 if _, found := m.values[key]; !found {
46 m.keys = append(m.keys, key)
47 }
48 m.values[key] = value
49 }
50
51 // Get gets the value for the given key.
52 func (m *Ordered[K, T]) Get(key K) (T, bool) {
53 if m == nil {
54 var v T
55 return v, false
56 }
57 value, found := m.values[key]
58 return value, found
59 }
60
61 // Has returns whether the given key exists in the map.
62 func (m *Ordered[K, T]) Has(key K) bool {
63 if m == nil {
64 return false
65 }
66 _, found := m.values[key]
67 return found
68 }
69
70 // Delete deletes the value for the given key.
71 func (m *Ordered[K, T]) Delete(key K) {
72 if m == nil {
73 return
74 }
75 delete(m.values, key)
76 for i, k := range m.keys {
77 if k == key {
78 m.keys = slices.Delete(m.keys, i, i+1)
79 break
80 }
81 }
82 }
83
84 // Clone creates a shallow copy of the map.
85 func (m *Ordered[K, T]) Clone() *Ordered[K, T] {
86 if m == nil {
87 return nil
88 }
89 clone := NewOrdered[K, T]()
90 for _, k := range m.keys {
91 clone.Set(k, m.values[k])
92 }
93 return clone
94 }
95
96 // Keys returns the keys in the order they were added.
97 func (m *Ordered[K, T]) Keys() []K {
98 if m == nil {
99 return nil
100 }
101 return m.keys
102 }
103
104 // Values returns the values in the order they were added.
105 func (m *Ordered[K, T]) Values() []T {
106 if m == nil {
107 return nil
108 }
109 var values []T
110 for _, k := range m.keys {
111 values = append(values, m.values[k])
112 }
113 return values
114 }
115
116 // Len returns the number of items in the map.
117 func (m *Ordered[K, T]) Len() int {
118 if m == nil {
119 return 0
120 }
121 return len(m.keys)
122 }
123
124 // Range calls f sequentially for each key and value present in the map.
125 // If f returns false, range stops the iteration.
126 // TODO(bep) replace with iter.Seq2 when we bump go Go 1.24.
127 func (m *Ordered[K, T]) Range(f func(key K, value T) bool) {
128 if m == nil {
129 return
130 }
131 for _, k := range m.keys {
132 if !f(k, m.values[k]) {
133 return
134 }
135 }
136 }
137
138 // Hash calculates a hash from the values.
139 func (m *Ordered[K, T]) Hash() (uint64, error) {
140 if m == nil {
141 return 0, nil
142 }
143 return hashing.Hash(m.values)
144 }