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

lif0 / pkg / 18823681365

26 Oct 2025 09:03PM UTC coverage: 97.5% (-1.6%) from 99.132%
18823681365

Pull #27

github

web-flow
Merge 69598ec0c into 2cb9a23a8
Pull Request #27: [PKG-18] add OrderedMap

95 of 109 new or added lines in 2 files covered. (87.16%)

780 of 800 relevant lines covered (97.5%)

28427.34 hits per line

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

91.53
/utils/structx/ordered_map.go
1
package structx
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](cap ...int) *OrderedMap[K, V] {
12✔
20
        var dictCap int
12✔
21
        if len(cap) > 0 {
12✔
NEW
22
                dictCap = cap[0]
×
NEW
23
        }
×
24

25
        return &OrderedMap[K, V]{
12✔
26
                dict: make(map[K]*internal.Node[V], dictCap),
12✔
27
                list: internal.LinkedList[V]{},
12✔
28
        }
12✔
29
}
30

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

42
        var zeroVal V
7✔
43
        return zeroVal, false
7✔
44
}
45

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

1✔
59
                node.Val = value
1✔
60
        } else {
22✔
61
                node = &internal.Node[V]{Val: value}
21✔
62
                this.list.Append(node)
21✔
63
                this.dict[key] = node
21✔
64
        }
21✔
65
}
66

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

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

12✔
89
        if cap(result) == 0 {
16✔
90
                return result
4✔
91
        }
4✔
92

93
        if cap(result) == 1 {
9✔
94
                result[0] = this.list.GetHead().Val
1✔
95
        }
1✔
96

97
        for i, v := range this.list.Iter() {
25✔
98
                result[i] = v
17✔
99
        }
17✔
100

101
        return result
8✔
102
}
103

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

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

130
        if m.list.Len() == 0 {
5✔
131
                return
1✔
132
        }
1✔
133

134
        if node, ok := m.dict[key]; ok {
4✔
135
                m.list.Remove(node)
1✔
136
                delete(m.dict, key)
1✔
137
        }
1✔
138
}
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

© 2025 Coveralls, Inc