-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_ordered.go
361 lines (312 loc) · 10.8 KB
/
map_ordered.go
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package g
import (
"fmt"
"slices"
"github.com/enetx/g/cmp"
"github.com/enetx/g/f"
)
// NewMapOrd creates a new ordered Map with the specified size (if provided).
// An ordered Map is an Map that maintains the order of its key-value pairs based on the
// insertion order. If no size is provided, the default size will be used.
//
// Parameters:
//
// - size ...int: (Optional) The initial size of the ordered Map. If not provided, a default size
// will be used.
//
// Returns:
//
// - MapOrd[K, V]: Ordered Map with the specified initial size (or default
// size if not provided).
//
// Example usage:
//
// mapOrd := g.NewMapOrd[string, int](10)
//
// Creates a new ordered Map with an initial size of 10.
func NewMapOrd[K, V any](size ...Int) MapOrd[K, V] {
if len(size) == 0 {
return make(MapOrd[K, V], 0)
}
return make(MapOrd[K, V], 0, size[0])
}
// Iter returns an iterator (SeqMapOrd[K, V]) for the ordered Map, allowing for sequential iteration
// over its key-value pairs. It is commonly used in combination with higher-order functions,
// such as 'ForEach', to perform operations on each key-value pair of the ordered Map.
//
// Returns:
//
// A SeqMapOrd[K, V], which can be used for sequential iteration over the key-value pairs of the ordered Map.
//
// Example usage:
//
// iter := g.NewMapOrd[int, int]()
// iter.
// Set(1, 1).
// Set(2, 2).
// Set(3, 3).
// Iter()
//
// iter.ForEach(func(k, v int) {
// // Process key-value pair
// })
//
// The 'Iter' method provides a convenient way to traverse the key-value pairs of an ordered Map
// in a functional style, enabling operations like mapping or filtering.
func (mo MapOrd[K, V]) Iter() SeqMapOrd[K, V] { return ToSeqMapOrd(mo) }
// MapOrdFromMap converts a standard Map to an ordered Map.
// The resulting ordered Map will maintain the order of its key-value pairs based on the order of
// insertion.
// This function is useful when you want to create an ordered Map from an existing Map.
//
// Parameters:
//
// - m Map[K, V]: The input Map to be converted to an ordered Map.
//
// Returns:
//
// - MapOrd[K, V]: New ordered Map containing the same key-value pairs as the
// input Map.
//
// Example usage:
//
// mapOrd := g.MapOrdFromMap[string, int](hmap)
//
// Converts the standard Map 'hmap' to an ordered Map.
func MapOrdFromMap[K comparable, V any](m Map[K, V]) MapOrd[K, V] {
mo := NewMapOrd[K, V](m.Len())
for k, v := range m {
mo.Set(k, v)
}
return mo
}
// MapOrdFromStd converts a standard Go map to an ordered Map.
// The resulting ordered Map will maintain the order of its key-value pairs based on the order of
// insertion.
// This function is useful when you want to create an ordered Map from an existing Go map.
//
// Parameters:
//
// - m map[K]V: The input Go map to be converted to an ordered Map.
//
// Returns:
//
// - MapOrd[K, V]: New ordered Map containing the same key-value pairs as the
// input Go map.
//
// Example usage:
//
// mapOrd := g.MapOrdFromStd[string, int](goMap)
//
// Converts the standard Go map 'map[K]V' to an ordered Map.
func MapOrdFromStd[K comparable, V any](m map[K]V) MapOrd[K, V] { return MapOrdFromMap(MapFromStd(m)) }
// SortBy sorts the ordered Map by a custom comparison function.
//
// Parameters:
//
// - fn func(a, b Pair[K, V]) cmp.Ordering: The custom comparison function used for sorting the ordered Map.
//
// Returns:
//
// - MapOrd[K, V]: Same ordered Map, sorted according to the custom comparison
// function.
//
// Example usage:
//
// hmapo.SortBy(func(a, b g.Pair[g.String, g.Int]) cmp.Ordering { return a.Key.Cmp(b.Key) })
// hmapo.SortBy(func(a, b g.Pair[g.String, g.Int]) cmp.Ordering { return a.Value.Cmp(b.Value) })
func (mo MapOrd[K, V]) SortBy(fn func(a, b Pair[K, V]) cmp.Ordering) MapOrd[K, V] {
slices.SortFunc(mo, func(a, b Pair[K, V]) int { return int(fn(a, b)) })
return mo
}
// SortByKey sorts the ordered MapOrd[K, V] by the keys using a custom comparison function.
//
// Parameters:
//
// - fn func(a, b K) cmp.Ordering: The custom comparison function used for sorting the keys.
//
// Returns:
//
// - MapOrd[K, V]: The same ordered MapOrd, sorted by the keys according to the custom comparison function.
//
// Example usage:
//
// hmapo.SortByKey(func(a, b g.String) cmp.Ordering { return a.Cmp(b) })
func (mo MapOrd[K, V]) SortByKey(fn func(a, b K) cmp.Ordering) MapOrd[K, V] {
slices.SortFunc(mo, func(a, b Pair[K, V]) int { return int(fn(a.Key, b.Key)) })
return mo
}
// SortByValue sorts the ordered MapOrd[K, V] by the values using a custom comparison function.
//
// Parameters:
//
// - fn func(a, b V) cmp.Ordering: The custom comparison function used for sorting the values.
//
// Returns:
//
// - MapOrd[K, V]: The same ordered MapOrd, sorted by the values according to the custom comparison function.
//
// Example usage:
//
// hmapo.SortByValue(func(a, b g.Int) cmp.Ordering { return a.Cmp(b) })
func (mo MapOrd[K, V]) SortByValue(fn func(a, b V) cmp.Ordering) MapOrd[K, V] {
slices.SortFunc(mo, func(a, b Pair[K, V]) int { return int(fn(a.Value, b.Value)) })
return mo
}
// Clone creates a new ordered Map with the same key-value pairs.
func (mo MapOrd[K, V]) Clone() MapOrd[K, V] {
result := NewMapOrd[K, V](mo.Len())
mo.Iter().ForEach(func(k K, v V) { result.Set(k, v) })
return result
}
// Copy copies key-value pairs from the source ordered Map to the current ordered Map.
func (mo *MapOrd[K, V]) Copy(src MapOrd[K, V]) MapOrd[K, V] {
src.Iter().ForEach(func(k K, v V) { mo.Set(k, v) })
return *mo
}
// ToMap converts the ordered Map to a standard Map.
// func (mo MapOrd[K, V]) ToMap() Map[K, V] {
// m := NewMap[K, V](len(mo))
// mo.Iter().ForEach(func(k K, v V) { m.Set(k, v) })
// return m
// }
// Set sets the value for the specified key in the ordered Map.
func (mo *MapOrd[K, V]) Set(key K, value V) *MapOrd[K, V] {
if i := mo.index(key); i != -1 {
(*mo)[i].Value = value
return mo
}
mp := Pair[K, V]{key, value}
*mo = append(*mo, mp)
return mo
}
// Get retrieves the value for the specified key, along with a boolean indicating whether the key
// was found in the ordered Map. This function is useful when you want to access the value
// associated with a key in the ordered Map, and also check if the key exists in the map.
//
// Parameters:
//
// - key K: The key to search for in the ordered Map.
//
// Returns:
//
// - V: The value associated with the specified key if found, or the zero value for the value type
// if the key is not found.
//
// - bool: A boolean value indicating whether the key was found in the ordered Map.
//
// Example usage:
//
// value, found := mo.Get("some_key")
//
// Retrieves the value associated with the key "some_key" and checks if the key exists in the
// ordered Map.
func (mo MapOrd[K, V]) Get(key K) Option[V] {
if i := mo.index(key); i != -1 {
return Some(mo[i].Value)
}
return None[V]()
}
// GetOrSet returns the value for a key. If the key does not exist, it returns the default value
// instead and also sets the default value for the key in the ordered Map. This function is useful
// when you want to access the value associated with a key in the ordered Map, and if the key does
// not exist, you want to return a specified default value and set that default value for the key.
//
// Parameters:
//
// - key K: The key to search for in the ordered Map.
//
// - defaultValue V: The default value to return if the key is not found in the ordered Map.
// If the key is not found, this default value will also be set for the key in the ordered Map.
//
// Returns:
//
// - V: The value associated with the specified key if found, or the provided default value if the key is not found.
//
// Example usage:
//
// value := mo.GetOrSet("some_key", "default_value")
//
// Retrieves the value associated with the key "some_key" or returns "default_value" if the key is
// not found, and sets "default_value" as the value for "some_key" in the ordered Map if it's not
// present.
func (mo *MapOrd[K, V]) GetOrSet(key K, defaultValue V) V {
if value := mo.Get(key); value.IsSome() {
return value.Some()
}
mo.Set(key, defaultValue)
return defaultValue
}
// Invert inverts the key-value pairs in the ordered Map, creating a new ordered Map with the
// values as keys and the original keys as values.
func (mo MapOrd[K, V]) Invert() MapOrd[V, K] {
result := NewMapOrd[V, K](mo.Len())
mo.Iter().ForEach(func(k K, v V) { result.Set(v, k) })
return result
}
func (mo MapOrd[K, V]) index(key K) int {
if f.Comparable(key) {
for i, mp := range mo {
if f.Eq[any](mp.Key)(key) {
return i
}
}
return -1
}
for i, mp := range mo {
if f.Eqd(mp.Key)(key) {
return i
}
}
return -1
}
// Keys returns an Slice containing all the keys in the ordered Map.
func (mo MapOrd[K, V]) Keys() Slice[K] { return mo.Iter().Keys().Collect() }
// Values returns an Slice containing all the values in the ordered Map.
func (mo MapOrd[K, V]) Values() Slice[V] { return mo.Iter().Values().Collect() }
// Delete removes the specified keys from the ordered Map.
func (mo *MapOrd[K, V]) Delete(keys ...K) MapOrd[K, V] {
for _, key := range keys {
if i := mo.index(key); i != -1 {
*mo = append((*mo)[:i], (*mo)[i+1:]...)
}
}
return *mo
}
// Eq compares the current ordered Map to another ordered Map and returns true if they are equal.
func (mo MapOrd[K, V]) Eq(other MapOrd[K, V]) bool {
if len(mo) != len(other) || mo.Empty() {
return false
}
comparable := f.Comparable(mo[0].Value)
for _, mp := range mo {
value := other.Get(mp.Key)
if value.IsNone() || (comparable && !f.Eq[any](value.Some())(mp.Value)) ||
(!comparable && !f.Eqd(value.Some())(mp.Value)) {
return false
}
}
return true
}
// String returns a string representation of the ordered Map.
func (mo MapOrd[K, V]) String() string {
builder := NewBuilder()
mo.Iter().ForEach(func(k K, v V) { builder.Write(Sprintf("%v:%v, ", k, v)) })
return builder.String().TrimRight(", ").Format("MapOrd{%s}").Std()
}
// Clear removes all key-value pairs from the ordered Map.
func (mo *MapOrd[K, V]) Clear() MapOrd[K, V] { return mo.Delete(mo.Keys()...) }
// Contains checks if the ordered Map contains the specified key.
func (mo MapOrd[K, V]) Contains(key K) bool { return mo.index(key) >= 0 }
// Empty checks if the ordered Map is empty.
func (mo MapOrd[K, V]) Empty() bool { return len(mo) == 0 }
// Len returns the number of key-value pairs in the ordered Map.
func (mo MapOrd[K, V]) Len() Int { return Int(len(mo)) }
// Ne compares the current ordered Map to another ordered Map and returns true if they are not
// equal.
func (mo MapOrd[K, V]) Ne(other MapOrd[K, V]) bool { return !mo.Eq(other) }
// NotEmpty checks if the ordered Map is not empty.
func (mo MapOrd[K, V]) NotEmpty() bool { return !mo.Empty() }
// Print prints the key-value pairs of the MapOrd to the standard output (console)
// and returns the MapOrd pointer unchanged.
func (mo MapOrd[K, V]) Print() MapOrd[K, V] { fmt.Println(mo); return mo }