Debian Codegraph @

common/hmaps/orderedintset.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 "fmt"
18 "slices"
19
20 "github.com/bits-and-blooms/bitset"
21 )
22
23 type OrderedIntSet struct {
24 keys []int
25 values *bitset.BitSet
26 }
27
28 // NewOrderedIntSet creates a new OrderedIntSet.
29 // Note that this is backed by https://github.com/bits-and-blooms/bitset
30 func NewOrderedIntSet(vals ...int) *OrderedIntSet {
31 m := &OrderedIntSet{
32 keys: make([]int, 0, len(vals)),
33 values: bitset.New(uint(len(vals))),
34 }
35 for _, v := range vals {
36 m.Set(v)
37 }
38 return m
39 }
40
41 // Set sets the value for the given key.
42 // Note that insertion order is not affected if a key is re-inserted into the set.
43 func (m *OrderedIntSet) Set(key int) {
44 if m == nil {
45 panic("nil OrderedIntSet")
46 }
47 keyu := uint(key)
48 if m.values.Test(keyu) {
49 return
50 }
51 m.values.Set(keyu)
52 m.keys = append(m.keys, key)
53 }
54
55 // SetFrom sets the values from another OrderedIntSet.
56 func (m *OrderedIntSet) SetFrom(other *OrderedIntSet) {
57 if m == nil || other == nil {
58 return
59 }
60 for _, key := range other.keys {
61 m.Set(key)
62 }
63 }
64
65 func (m *OrderedIntSet) Clone() *OrderedIntSet {
66 if m == nil {
67 return nil
68 }
69 newSet := &OrderedIntSet{
70 keys: slices.Clone(m.keys),
71 values: m.values.Clone(),
72 }
73 return newSet
74 }
75
76 // Next returns the next key in the set possibly including the given key.
77 // It returns -1 if the key is not found or if there are no keys greater than the given key.
78 func (m *OrderedIntSet) Next(i int) int {
79 n, ok := m.values.NextSet(uint(i))
80 if !ok {
81 return -1
82 }
83 return int(n)
84 }
85
86 // The reason we don't use iter.Seq is https://github.com/golang/go/issues/69015
87 // This is 70% faster than using iter.Seq2[int, int] for the keys.
88 // It returns false if the iteration was stopped early.
89 func (m *OrderedIntSet) ForEachKey(yield func(int) bool) bool {
90 if m == nil {
91 return true
92 }
93 for _, key := range m.keys {
94 if !yield(key) {
95 return false
96 }
97 }
98 return true
99 }
100
101 func (m *OrderedIntSet) Has(key int) bool {
102 if m == nil {
103 return false
104 }
105 return m.values.Test(uint(key))
106 }
107
108 func (m *OrderedIntSet) Len() int {
109 if m == nil {
110 return 0
111 }
112 return len(m.keys)
113 }
114
115 // KeysSorted returns the keys in sorted order.
116 func (m *OrderedIntSet) KeysSorted() []int {
117 if m == nil {
118 return nil
119 }
120 keys := slices.Clone(m.keys)
121 slices.Sort(keys)
122 return m.keys
123 }
124
125 func (m *OrderedIntSet) String() string {
126 if m == nil {
127 return "[]"
128 }
129 return fmt.Sprintf("%v", m.keys)
130 }
131
132 func (m *OrderedIntSet) Values() *bitset.BitSet {
133 if m == nil {
134 return nil
135 }
136 return m.values
137 }
138
139 func (m *OrderedIntSet) IsSuperSet(other *OrderedIntSet) bool {
140 if m == nil || other == nil {
141 return false
142 }
143 return m.values.IsSuperSet(other.values)
144 }
145
146 // Words returns the bitset as array of 64-bit words, giving direct access to the internal representation.
147 // It is not a copy, so changes to the returned slice will affect the bitset.
148 // It is meant for advanced users.
149 func (m *OrderedIntSet) Words() []uint64 {
150 if m == nil {
151 return nil
152 }
153 return m.values.Words()
154 }