• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

enetx / g / 21455109854

28 Jan 2026 08:55PM UTC coverage: 89.873% (-0.09%) from 89.958%
21455109854

push

github

enetx
refactor(api): rename conversion methods for consistency

  - String: ToInt → TryInt, ToFloat → TryFloat, ToBigInt → TryBigInt
  - Containers: ToSlice → Slice, ToHeap → Heap
  - Map: ToMapOrd → Ordered, ToMapSafe → Safe
  - MapOrd: ToMap → Map, ToMapSafe → Safe
  - Iterators: ToChan → Chan
  - Remove: Slice.ToStringSlice (use TransformSlice instead)

  Naming convention:
  - Try* for fallible operations (parsing)
  - Direct type name for infallible conversions
  - From* for constructors

17 of 20 new or added lines in 13 files covered. (85.0%)

4 existing lines in 1 file now uncovered.

6425 of 7149 relevant lines covered (89.87%)

11863.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.06
/map_ordered.go
1
package g
2

3
import (
4
        "fmt"
5
        "math/rand/v2"
6
        "slices"
7

8
        "github.com/enetx/g/cmp"
9
        "github.com/enetx/g/f"
10
        "github.com/enetx/iter"
11
)
12

13
// Pair is a struct representing a key-value Pair for MapOrd.
14
type Pair[K, V any] = iter.Pair[K, V]
15

16
// MapOrd is an ordered map that maintains insertion order using a slice for pairs
17
// and a map for fast index lookups.
18
type MapOrd[K comparable, V any] []Pair[K, V] // ordered key-value pairs
19

20
// NewMapOrd creates a new ordered Map with the specified size (if provided).
21
// An ordered Map is an Map that maintains the order of its key-value pairs based on the
22
// insertion order. If no size is provided, the default size will be used.
23
//
24
// Parameters:
25
//
26
// - size ...int: (Optional) The initial size of the ordered Map. If not provided, a default size
27
// will be used.
28
//
29
// Returns:
30
//
31
// - MapOrd[K, V]: Ordered Map with the specified initial size (or default
32
// size if not provided).
33
//
34
// Example usage:
35
//
36
//        mapOrd := g.NewMapOrd[string, int](10)
37
//
38
// Creates a new ordered Map with an initial size of 10.
39
func NewMapOrd[K comparable, V any](size ...Int) MapOrd[K, V] {
211✔
40
        return make(MapOrd[K, V], 0, Slice[Int](size).Get(0).UnwrapOrDefault())
211✔
41
}
211✔
42

43
// Transform applies a transformation function to the MapOrd and returns the result.
44
func (mo MapOrd[K, V]) Transform(fn func(MapOrd[K, V]) MapOrd[K, V]) MapOrd[K, V] { return fn(mo) }
2✔
45

46
// Entry returns an OrdEntry for the given key.
47
func (mo *MapOrd[K, V]) Entry(key K) OrdEntry[K, V] {
45✔
48
        if i := mo.index(key); i != -1 {
66✔
49
                return OccupiedOrdEntry[K, V]{mo: mo, key: key, index: i}
21✔
50
        }
21✔
51

52
        return VacantOrdEntry[K, V]{mo: mo, key: key}
24✔
53
}
54

55
// Iter returns an iterator (SeqMapOrd[K, V]) for the ordered Map, allowing for sequential iteration
56
// over its key-value pairs. It is commonly used in combination with higher-order functions,
57
// such as 'ForEach', to perform operations on each key-value pair of the ordered Map.
58
//
59
// Returns:
60
//
61
// A SeqMapOrd[K, V], which can be used for sequential iteration over the key-value pairs of the ordered Map.
62
//
63
// Example usage:
64
//
65
//        m := g.NewMapOrd[int, int]()
66
//        m.Set(1, 1)
67
//        m.Set(2, 2)
68
//        m.Set(3, 3).
69
//
70
//        m.Iter().ForEach(func(k, v int) {
71
//            // Process key-value pair
72
//        })
73
//
74
// The 'Iter' method provides a convenient way to traverse the key-value pairs of an ordered Map
75
// in a functional style, enabling operations like mapping or filtering.
76
func (mo MapOrd[K, V]) Iter() SeqMapOrd[K, V] {
79✔
77
        return func(yield func(K, V) bool) {
163✔
78
                for _, v := range mo {
288✔
79
                        if !yield(v.Key, v.Value) {
221✔
80
                                return
17✔
81
                        }
17✔
82
                }
83
        }
84
}
85

86
// IterReverse returns an iterator (SeqMapOrd[K, V]) for the ordered Map that allows for sequential iteration
87
// over its key-value pairs in reverse order. This method is useful when you need to process the elements
88
// from the last to the first.
89
//
90
// Returns:
91
//
92
// A SeqMapOrd[K, V], which can be used for sequential iteration over the key-value pairs of the ordered Map in reverse order.
93
//
94
// Example usage:
95
//
96
//        m := g.NewMapOrd[int, int]()
97
//        m.Set(1, 1)
98
//        m.Set(2, 2)
99
//        m.Set(3, 3)
100
//
101
//        m.IterReverse().ForEach(func(k, v int) {
102
//            // Process key-value pair in reverse order
103
//            fmt.Println("Key:", k, "Value:", v)
104
//        })
105
//
106
// The 'IterReverse' method complements the 'Iter' method by providing a way to access the elements
107
// in a reverse sequence, offering additional flexibility in data processing scenarios.
108
func (mo MapOrd[K, V]) IterReverse() SeqMapOrd[K, V] {
4✔
109
        return func(yield func(K, V) bool) {
8✔
110
                for i := len(mo) - 1; i >= 0; i-- {
8✔
111
                        v := mo[i]
4✔
112
                        if !yield(v.Key, v.Value) {
4✔
113
                                return
×
114
                        }
×
115
                }
116
        }
117
}
118

119
// MapOrdFromStd converts a standard Go map to an ordered Map.
120
// The resulting ordered Map will maintain the order of its key-value pairs based on the order of
121
// insertion.
122
// This function is useful when you want to create an ordered Map from an existing Go map.
123
//
124
// Parameters:
125
//
126
// - m map[K]V: The input Go map to be converted to an ordered Map.
127
//
128
// Returns:
129
//
130
// - MapOrd[K, V]: New ordered Map containing the same key-value pairs as the
131
// input Go map.
132
//
133
// Example usage:
134
//
135
//        mapOrd := g.MapOrdFromStd[string, int](goMap)
136
//
137
// Converts the standard Go map 'map[K]V' to an ordered Map.
138
func MapOrdFromStd[K comparable, V any](m map[K]V) MapOrd[K, V] { return Map[K, V](m).Ordered() }
2✔
139

140
// SortBy sorts the ordered Map by a custom comparison function.
141
//
142
// Parameters:
143
//
144
// - fn func(a, b Pair[K, V]) cmp.Ordering: The custom comparison function used for sorting the ordered Map.
145
//
146
// Example usage:
147
//
148
//        hmapo.SortBy(func(a, b g.Pair[g.String, g.Int]) cmp.Ordering { return a.Key.Cmp(b.Key) })
149
//        hmapo.SortBy(func(a, b g.Pair[g.String, g.Int]) cmp.Ordering { return a.Value.Cmp(b.Value) })
150
func (mo MapOrd[K, V]) SortBy(fn func(a, b Pair[K, V]) cmp.Ordering) {
3✔
151
        slices.SortFunc(mo, func(a, b Pair[K, V]) int { return int(fn(a, b)) })
11✔
152
}
153

154
// SortByKey sorts the ordered MapOrd[K, V] by the keys using a custom comparison function.
155
//
156
// Parameters:
157
//
158
// - fn func(a, b K) cmp.Ordering: The custom comparison function used for sorting the keys.
159
//
160
// Example usage:
161
//
162
//        hmapo.SortByKey(func(a, b g.String) cmp.Ordering { return a.Cmp(b) })
163
func (mo MapOrd[K, V]) SortByKey(fn func(a, b K) cmp.Ordering) {
6✔
164
        slices.SortFunc(mo, func(a, b Pair[K, V]) int { return int(fn(a.Key, b.Key)) })
32✔
165
}
166

167
// SortByValue sorts the ordered MapOrd[K, V] by the values using a custom comparison function.
168
//
169
// Parameters:
170
//
171
// - fn func(a, b V) cmp.Ordering: The custom comparison function used for sorting the values.
172
//
173
// Example usage:
174
//
175
//        hmapo.SortByValue(func(a, b g.Int) cmp.Ordering { return a.Cmp(b) })
176
func (mo MapOrd[K, V]) SortByValue(fn func(a, b V) cmp.Ordering) {
2✔
177
        slices.SortFunc(mo, func(a, b Pair[K, V]) int { return int(fn(a.Value, b.Value)) })
7✔
178
}
179

180
// IsSortedBy checks if the ordered Map is sorted according to a custom comparison function.
181
//
182
// Parameters:
183
//
184
// - fn func(a, b Pair[K, V]) cmp.Ordering: The custom comparison function used for checking sort order.
185
//
186
// Returns:
187
//
188
// - bool: true if the map is sorted according to the comparison function, false otherwise.
189
//
190
// Example usage:
191
//
192
//        sorted := hmapo.IsSortedBy(func(a, b g.Pair[g.String, g.Int]) cmp.Ordering { return a.Key.Cmp(b.Key) })
193
func (mo MapOrd[K, V]) IsSortedBy(fn func(a, b Pair[K, V]) cmp.Ordering) bool {
4✔
194
        if len(mo) <= 1 {
6✔
195
                return true
2✔
196
        }
2✔
197

198
        for i := 1; i < len(mo); i++ {
5✔
199
                if fn(mo[i-1], mo[i]).IsGt() {
4✔
200
                        return false
1✔
201
                }
1✔
202
        }
203

204
        return true
1✔
205
}
206

207
// IsSortedByKey checks if the ordered MapOrd[K, V] is sorted by the keys using a custom comparison function.
208
//
209
// Parameters:
210
//
211
// - fn func(a, b K) cmp.Ordering: The custom comparison function used for checking key sort order.
212
//
213
// Returns:
214
//
215
// - bool: true if the map is sorted by keys according to the comparison function, false otherwise.
216
//
217
// Example usage:
218
//
219
//        sorted := hmapo.IsSortedByKey(func(a, b g.String) cmp.Ordering { return a.Cmp(b) })
220
func (mo MapOrd[K, V]) IsSortedByKey(fn func(a, b K) cmp.Ordering) bool {
6✔
221
        if len(mo) <= 1 {
8✔
222
                return true
2✔
223
        }
2✔
224

225
        for i := 1; i < len(mo); i++ {
10✔
226
                if fn(mo[i-1].Key, mo[i].Key).IsGt() {
8✔
227
                        return false
2✔
228
                }
2✔
229
        }
230

231
        return true
2✔
232
}
233

234
// IsSortedByValue checks if the ordered MapOrd[K, V] is sorted by the values using a custom comparison function.
235
//
236
// Parameters:
237
//
238
// - fn func(a, b V) cmp.Ordering: The custom comparison function used for checking value sort order.
239
//
240
// Returns:
241
//
242
// - bool: true if the map is sorted by values according to the comparison function, false otherwise.
243
//
244
// Example usage:
245
//
246
//        sorted := hmapo.IsSortedByValue(func(a, b g.Int) cmp.Ordering { return a.Cmp(b) })
247
func (mo MapOrd[K, V]) IsSortedByValue(fn func(a, b V) cmp.Ordering) bool {
6✔
248
        if len(mo) <= 1 {
8✔
249
                return true
2✔
250
        }
2✔
251

252
        for i := 1; i < len(mo); i++ {
10✔
253
                if fn(mo[i-1].Value, mo[i].Value).IsGt() {
8✔
254
                        return false
2✔
255
                }
2✔
256
        }
257

258
        return true
2✔
259
}
260

261
// Clone creates a new ordered Map with the same key-value pairs.
262
func (mo MapOrd[K, V]) Clone() MapOrd[K, V] {
4✔
263
        nmo := NewMapOrd[K, V](mo.Len())
4✔
264
        nmo.Copy(mo)
4✔
265

4✔
266
        return nmo
4✔
267
}
4✔
268

269
// Copy copies key-value pairs from the source ordered Map to the current ordered Map.
270
func (mo *MapOrd[K, V]) Copy(src MapOrd[K, V]) {
6✔
271
        idx := mo.indexMap()
6✔
272

6✔
273
        for _, p := range src {
18✔
274
                if i, ok := idx[p.Key]; ok {
12✔
275
                        (*mo)[i].Value = p.Value
×
276
                } else {
12✔
277
                        *mo = append(*mo, p)
12✔
278
                        idx[p.Key] = len(*mo) - 1
12✔
279
                }
12✔
280
        }
281
}
282

283
// Map converts the ordered Map to a standard Map.
NEW
284
func (mo MapOrd[K, V]) Map() Map[K, V] {
×
285
        m := NewMap[K, V](mo.Len())
×
286
        mo.Iter().ForEach(func(k K, v V) { m[k] = v })
×
287

288
        return m
×
289
}
290

291
// Safe converts a ordered Map to a thread-safe Map.
NEW
292
func (mo MapOrd[K, V]) Safe() *MapSafe[K, V] {
×
293
        ms := NewMapSafe[K, V]()
×
294
        mo.Iter().ForEach(func(k K, v V) { ms.Insert(k, v) })
×
295

296
        return ms
×
297
}
298

299
// Insert sets the value for the specified key in the ordered Map,
300
// and returns the previous value if it existed.
301
func (mo *MapOrd[K, V]) Insert(key K, value V) Option[V] {
461✔
302
        if i := mo.index(key); i != -1 {
469✔
303
                prev := (*mo)[i].Value
8✔
304
                (*mo)[i].Value = value
8✔
305

8✔
306
                return Some(prev)
8✔
307
        }
8✔
308

309
        mp := Pair[K, V]{Key: key, Value: value}
453✔
310
        *mo = append(*mo, mp)
453✔
311

453✔
312
        return None[V]()
453✔
313
}
314

315
// Get returns the value associated with the given key, wrapped in Option[V].
316
//
317
// It returns Some(value) if the key exists, or None if it does not.
318
func (mo MapOrd[K, V]) Get(key K) Option[V] {
37✔
319
        if i := mo.index(key); i != -1 {
74✔
320
                return Some(mo[i].Value)
37✔
321
        }
37✔
322

323
        return None[V]()
×
324
}
325

326
// Shuffle randomly reorders the elements of the ordered Map.
327
// It operates in place and affects the original order of the map's entries.
328
//
329
// The function uses the crypto/rand package to generate random indices.
330
func (mo MapOrd[K, V]) Shuffle() {
1✔
331
        for i := mo.Len() - 1; i > 0; i-- {
5✔
332
                j := rand.N(i + 1)
4✔
333
                mo[i], mo[j] = mo[j], mo[i]
4✔
334
        }
4✔
335
}
336

337
func (mo MapOrd[K, V]) index(key K) int {
549✔
338
        for i, mp := range mo {
1,208✔
339
                if mp.Key == key {
728✔
340
                        return i
69✔
341
                }
69✔
342
        }
343

344
        return -1
480✔
345
}
346

347
// Keys returns an Slice containing all the keys in the ordered Map.
348
func (mo MapOrd[K, V]) Keys() Slice[K] { return mo.Iter().Keys().Collect() }
9✔
349

350
// Values returns an Slice containing all the values in the ordered Map.
351
func (mo MapOrd[K, V]) Values() Slice[V] { return mo.Iter().Values().Collect() }
2✔
352

353
// Remove removes the specified key from the ordered Map and returns the removed value.
354
func (mo *MapOrd[K, V]) Remove(key K) Option[V] {
7✔
355
        if mo.IsEmpty() {
8✔
356
                return None[V]()
1✔
357
        }
1✔
358

359
        for i, p := range *mo {
17✔
360
                if p.Key == key {
16✔
361
                        *mo = append((*mo)[:i], (*mo)[i+1:]...)
5✔
362
                        return Some(p.Value)
5✔
363
                }
5✔
364
        }
365

366
        return None[V]()
1✔
367
}
368

369
// Eq compares the current ordered Map to another ordered Map and returns true if they are equal.
370
func (mo MapOrd[K, V]) Eq(other MapOrd[K, V]) bool {
30✔
371
        if len(mo) != len(other) {
32✔
372
                return false
2✔
373
        }
2✔
374
        if len(mo) == 0 {
31✔
375
                return true
3✔
376
        }
3✔
377

378
        idx := other.indexMap()
25✔
379

25✔
380
        var zero V
25✔
381
        comparable := f.IsComparable(zero)
25✔
382

25✔
383
        for i, mp := range mo {
94✔
384
                j, ok := idx[mp.Key]
69✔
385
                if !ok || j != i {
70✔
386
                        return false
1✔
387
                }
1✔
388

389
                value := other[j].Value
68✔
390

68✔
391
                if comparable && !f.Eq[any](value)(mp.Value) || !comparable && !f.Eqd(value)(mp.Value) {
69✔
392
                        return false
1✔
393
                }
1✔
394
        }
395

396
        return true
23✔
397
}
398

399
// String returns a string representation of the ordered Map.
400
func (mo MapOrd[K, V]) String() string {
5✔
401
        if len(mo) == 0 {
6✔
402
                return "MapOrd{}"
1✔
403
        }
1✔
404

405
        var b Builder
4✔
406
        b.WriteString("MapOrd{")
4✔
407

4✔
408
        first := true
4✔
409
        for _, pair := range mo {
12✔
410
                if !first {
12✔
411
                        b.WriteString(", ")
4✔
412
                }
4✔
413

414
                first = false
8✔
415
                b.WriteString(Format("{}:{}", pair.Key, pair.Value))
8✔
416
        }
417

418
        b.WriteString("}")
4✔
419

4✔
420
        return b.String().Std()
4✔
421
}
422

423
// Clear removes all key-value pairs from the ordered Map.
424
func (mo *MapOrd[K, V]) Clear() { *mo = (*mo)[:0] }
2✔
425

426
// Contains checks if the ordered Map contains the specified key.
427
func (mo MapOrd[K, V]) Contains(key K) bool { return mo.index(key) != -1 }
6✔
428

429
// Empty checks if the ordered Map is empty.
430
func (mo MapOrd[K, V]) IsEmpty() bool { return len(mo) == 0 }
11✔
431

432
// Len returns the number of key-value pairs in the ordered Map.
433
func (mo MapOrd[K, V]) Len() Int { return Int(len(mo)) }
26✔
434

435
// Ne compares the current ordered Map to another ordered Map and returns true if they are not equal.
436
func (mo MapOrd[K, V]) Ne(other MapOrd[K, V]) bool { return !mo.Eq(other) }
8✔
437

438
// Print writes the key-value pairs of the MapOrd to the standard output (console)
439
// and returns the MapOrd unchanged.
440
func (mo MapOrd[K, V]) Print() MapOrd[K, V] { fmt.Print(mo); return mo }
1✔
441

442
// Println writes the key-value pairs of the MapOrd to the standard output (console) with a newline
443
// and returns the MapOrd unchanged.
444
func (mo MapOrd[K, V]) Println() MapOrd[K, V] { fmt.Println(mo); return mo }
1✔
445

446
// indexMap builds a map from keys to their corresponding indices in the MapOrd.
447
//
448
// This function is used to create a temporary indexMap that maps each key in the
449
// ordered map to its position (insertion order) within the slice. It is useful
450
// for optimizing lookup operations such as Set, Delete, Copy, or Eq.
451
//
452
// Time complexity: O(n), where n is the number of key-value pairs in the MapOrd.
453
func (mo MapOrd[K, V]) indexMap() map[K]int {
31✔
454
        idx := make(map[K]int, len(mo))
31✔
455

31✔
456
        for i, p := range mo {
108✔
457
                idx[p.Key] = i
77✔
458
        }
77✔
459

460
        return idx
31✔
461
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc