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

lif0 / pkg / 18808341708

25 Oct 2025 08:50PM UTC coverage: 96.402%. First build
18808341708

Pull #19

github

web-flow
Merge dd01f6d04 into 41d9de232
Pull Request #19: [PKG-18] add OrderedMap

94 of 115 new or added lines in 3 files covered. (81.74%)

777 of 806 relevant lines covered (96.4%)

24799.19 hits per line

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

94.64
/utils/typex/ordered_map.go
1
package typex
2

3
import (
4
        "github.com/lif0/pkg/utils/internal"
5
)
6

7
// OrderedMap is a map[Type]Type1-like collection that preserves the order
8
// in which keys were inserted. It behaves like a regular map but
9
// allows deterministic iteration over its elements.
10
//
11
// OrderedMap is useful when both quick key-based access and
12
// predictable iteration order are desired.
13
type OrderedMap[K comparable, V any] struct {
14
        dict map[K]*internal.Node[V]
15
        list internal.LinkedList[V]
16
}
17

18
// NewOrderedMap returns a new empty OrderedMap.
19
func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V] {
12✔
20
        return &OrderedMap[K, V]{
12✔
21
                dict: make(map[K]*internal.Node[V]),
12✔
22
                list: internal.LinkedList[V]{},
12✔
23
        }
12✔
24
}
12✔
25

26
// Get retrieves the value stored under the given key.
27
// The second return value reports whether the key was present.
28
//
29
// Complexity:
30
// - time: O(1)
31
// - mem: O(1)
32
func (this *OrderedMap[K, V]) Get(key K) (V, bool) {
9✔
33
        if node, ok := this.dict[key]; ok {
11✔
34
                return node.Val, true
2✔
35
        }
2✔
36

37
        var zeroVal V
7✔
38
        return zeroVal, false
7✔
39
}
40

41
// Put sets the value for the given key.
42
// If the key already exists, its value is updated.
43
// Otherwise, a new entry is added to the end of the order.
44
//
45
// Complexity:
46
// - time: O(1)
47
// - mem: O(1)
48
func (this *OrderedMap[K, V]) Put(key K, value V) {
22✔
49
        if node, ok := this.dict[key]; ok {
23✔
50
                // this.removeNode(node)
1✔
51
                // node.Val = value
1✔
52
                // this.addNodeToTail(node)
1✔
53

1✔
54
                node.Val = value
1✔
55
        } else {
22✔
56
                node = &internal.Node[V]{Val: value}
21✔
57
                this.list.Append(node)
21✔
58
                this.dict[key] = node
21✔
59
        }
21✔
60
}
61

62
// Delete removes the element with the specified key.
63
// If the key does not exist, Delete does nothing.
64
//
65
// Complexity:
66
// - time: O(1)
67
// - mem: O(1)
68
func (this *OrderedMap[K, V]) Delete(key K) {
2✔
69
        if node, ok := this.dict[key]; ok {
3✔
70
                this.list.Remove(node)
1✔
71
                delete(this.dict, key)
1✔
72
        }
1✔
73
}
74

75
// GetValues returns all values in insertion order.
76
// The returned slice has the same length as the number of elements.
77
//
78
// Complexity:
79
// - time: O(N)
80
// - mem: O(N)
81
func (this *OrderedMap[K, V]) GetValues() []V {
12✔
82
        result := make([]V, this.list.Len())
12✔
83

12✔
84
        if cap(result) == 0 {
16✔
85
                return result
4✔
86
        }
4✔
87

88
        if cap(result) == 1 {
9✔
89
                result[0] = this.list.GetHead().Val
1✔
90
        }
1✔
91

92
        for i, v := range this.list.Iter() {
25✔
93
                result[i] = v
17✔
94
        }
17✔
95

96
        return result
8✔
97
}
98

99
// Iter iteration on map
100
//
101
// Example:
102
//
103
//        m := NewOrderedMap[int, string]()
104
//        for i, v := range m.Iter() {
105
//                fmt.Println(i,v)
106
//        }
NEW
107
func (this *OrderedMap[K, V]) Iter() func(func(int, V) bool) {
×
NEW
108
        return this.list.Iter()
×
NEW
109
}
×
110

111
// Delete built-in function deletes the element with the specified key
112
// (m[key]) from the OrderedMap. If m is nil or there is no such element, delete
113
// is a no-op.
114
//
115
// Example:
116
//
117
//        var om = NewOrderedMap[string, int]()
118
//        om.Put("x", 1)
119
//        typex.Delete(om, "x")
120
func Delete[Type comparable, Type1 any](m *OrderedMap[Type, Type1], key Type) {
5✔
121
        if m == nil {
6✔
122
                return
1✔
123
        }
1✔
124

125
        if m.list.Len() == 0 {
5✔
126
                return
1✔
127
        }
1✔
128

129
        if node, ok := m.dict[key]; ok {
4✔
130
                m.list.Remove(node)
1✔
131
                delete(m.dict, key)
1✔
132
        }
1✔
133
}
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