summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-maps/common.go
blob: f5877ee3abfd649d73418055e4a9512c0118c4b3 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package maps

import (
	"fmt"
	"reflect"

	"codeberg.org/gruf/go-byteutil"
	"codeberg.org/gruf/go-kv"
)

// ordered provides a common ordered hashmap base, storing order in a doubly-linked list.
type ordered[K comparable, V any] struct {
	hmap map[K]*elem[K, V]
	list list[K, V]
	pool []*elem[K, V]
	rnly bool
}

// write_check panics if map is not in a safe-state to write to.
func (m ordered[K, V]) write_check() {
	if m.rnly {
		panic("map write during read loop")
	}
}

// Has returns whether key exists in map.
func (m *ordered[K, V]) Has(key K) bool {
	_, ok := m.hmap[key]
	return ok
}

// Delete will delete given key from map, returns false if not found.
func (m *ordered[K, V]) Delete(key K) bool {
	// Ensure safe
	m.write_check()

	// Look for existing elem
	elem, ok := m.hmap[key]
	if !ok {
		return false
	}

	// Drop from list
	m.list.Unlink(elem)

	// Delete from map
	delete(m.hmap, key)

	// Return to pool
	m.free(elem)

	return true
}

// Range passes given function over the requested range of the map.
func (m *ordered[K, V]) Range(start, length int, fn func(int, K, V)) {
	// Disallow writes
	m.rnly = true
	defer func() {
		m.rnly = false
	}()

	// Nil check
	_ = fn

	switch end := start + length; {
	// No loop to iterate
	case length == 0:
		if start < 0 || (m.list.len > 0 && start >= m.list.len) {
			panic("index out of bounds")
		}

	// Step backwards
	case length < 0:
		// Check loop indices are within map bounds
		if end < -1 || start >= m.list.len || m.list.len == 0 {
			panic("index out of bounds")
		}

		// Get starting index elem
		elem := m.list.Index(start)

		for i := start; i > end; i-- {
			fn(i, elem.K, elem.V)
			elem = elem.prev
		}

	// Step forwards
	case length > 0:
		// Check loop indices are within map bounds
		if start < 0 || end > m.list.len || m.list.len == 0 {
			panic("index out of bounds")
		}

		// Get starting index elem
		elem := m.list.Index(start)

		for i := start; i < end; i++ {
			fn(i, elem.K, elem.V)
			elem = elem.next
		}
	}
}

// RangeIf passes given function over the requested range of the map. Returns early on 'fn' -> false.
func (m *ordered[K, V]) RangeIf(start, length int, fn func(int, K, V) bool) {
	// Disallow writes
	m.rnly = true
	defer func() {
		m.rnly = false
	}()

	// Nil check
	_ = fn

	switch end := start + length; {
	// No loop to iterate
	case length == 0:
		if start < 0 || (m.list.len > 0 && start >= m.list.len) {
			panic("index out of bounds")
		}

	// Step backwards
	case length < 0:
		// Check loop indices are within map bounds
		if end < -1 || start >= m.list.len || m.list.len == 0 {
			panic("index out of bounds")
		}

		// Get starting index elem
		elem := m.list.Index(start)

		for i := start; i > end; i-- {
			if !fn(i, elem.K, elem.V) {
				return
			}
			elem = elem.prev
		}

	// Step forwards
	case length > 0:
		// Check loop indices are within map bounds
		if start < 0 || end > m.list.len || m.list.len == 0 {
			panic("index out of bounds")
		}

		// Get starting index elem
		elem := m.list.Index(start)

		for i := start; i < end; i++ {
			if !fn(i, elem.K, elem.V) {
				return
			}
			elem = elem.next
		}
	}
}

// Truncate will truncate the map from the back by given amount, passing dropped elements to given function.
func (m *ordered[K, V]) Truncate(sz int, fn func(K, V)) {
	// Check size withing bounds
	if sz > m.list.len {
		panic("index out of bounds")
	}

	if fn == nil {
		// move nil check out of loop
		fn = func(K, V) {}
	}

	// Disallow writes
	m.rnly = true
	defer func() {
		m.rnly = false
	}()

	for i := 0; i < sz; i++ {
		// Pop current tail
		elem := m.list.tail
		m.list.Unlink(elem)

		// Delete from map
		delete(m.hmap, elem.K)

		// Pass dropped to func
		fn(elem.K, elem.V)

		// Release to pool
		m.free(elem)
	}
}

// Len returns the current length of the map.
func (m *ordered[K, V]) Len() int {
	return m.list.len
}

// format implements fmt.Formatter, allowing performant string formatting of map.
func (m *ordered[K, V]) format(rtype reflect.Type, state fmt.State, verb rune) {
	var (
		kvbuf byteutil.Buffer
		field kv.Field
		vbose bool
	)

	switch {
	// Only handle 'v' verb
	case verb != 'v':
		panic("invalid verb '" + string(verb) + "' for map")

	// Prefix with type when verbose
	case state.Flag('#'):
		state.Write([]byte(rtype.String()))
	}

	// Disallow writes
	m.rnly = true
	defer func() {
		m.rnly = false
	}()

	// Write map opening brace
	state.Write([]byte{'{'})

	if m.list.len > 0 {
		// Preallocate buffer
		kvbuf.Guarantee(64)

		// Start at index 0
		elem := m.list.head

		for i := 0; i < m.list.len-1; i++ {
			// Append formatted key-val pair to state
			field.K = fmt.Sprint(elem.K)
			field.V = elem.V
			field.AppendFormat(&kvbuf, vbose)
			_, _ = state.Write(kvbuf.B)
			kvbuf.Reset()

			// Prepare buffer with comma separator
			kvbuf.B = append(kvbuf.B, `, `...)

			// Jump to next in list
			elem = elem.next
		}

		// Append formatted key-val pair to state
		field.K = fmt.Sprint(elem.K)
		field.V = elem.V
		field.AppendFormat(&kvbuf, vbose)
		_, _ = state.Write(kvbuf.B)
	}

	// Write map closing brace
	state.Write([]byte{'}'})
}

// Std returns a clone of map's data in the standard library equivalent map type.
func (m *ordered[K, V]) Std() map[K]V {
	std := make(map[K]V, m.list.len)
	for _, elem := range m.hmap {
		std[elem.K] = elem.V
	}
	return std
}

// alloc will acquire list element from pool, or allocate new.
func (m *ordered[K, V]) alloc() *elem[K, V] {
	if len(m.pool) == 0 {
		return &elem[K, V]{}
	}
	idx := len(m.pool) - 1
	elem := m.pool[idx]
	m.pool = m.pool[:idx]
	return elem
}

// free will reset elem fields and place back in pool.
func (m *ordered[K, V]) free(elem *elem[K, V]) {
	var (
		zk K
		zv V
	)
	elem.K = zk
	elem.V = zv
	elem.next = nil
	elem.prev = nil
	m.pool = append(m.pool, elem)
}