@@ -10,9 +10,8 @@ import (
10
10
// - Uses a slice to maintain insertion order and enable ordered iteration
11
11
// The data structure is safe for concurrent access through the use of a read-write mutex.
12
12
type OrderedSet [K comparable , V any ] struct {
13
- itemPos map [K ]int // position of the node in the list
13
+ itemPos map [K ]int // position of the item in the list
14
14
items []V
15
- size int
16
15
lock sync.RWMutex
17
16
}
18
17
@@ -22,46 +21,45 @@ func NewOrderedSet[K comparable, V any]() *OrderedSet[K, V] {
22
21
}
23
22
}
24
23
25
- func (ps * OrderedSet [K , V ]) Put (key K , value V ) {
26
- ps .lock .Lock ()
27
- defer ps .lock .Unlock ()
24
+ func (o * OrderedSet [K , V ]) Put (key K , value V ) {
25
+ o .lock .Lock ()
26
+ defer o .lock .Unlock ()
28
27
29
28
// Update existing entry
30
- if pos , exists := ps .itemPos [key ]; exists {
31
- ps .items [pos ] = value
29
+ if pos , exists := o .itemPos [key ]; exists {
30
+ o .items [pos ] = value
32
31
return
33
32
}
34
33
35
34
// Insert new entry
36
- ps .itemPos [key ] = len (ps .items )
37
- ps .items = append (ps .items , value )
38
- ps .size ++
35
+ o .itemPos [key ] = len (o .items )
36
+ o .items = append (o .items , value )
39
37
}
40
38
41
- func (ps * OrderedSet [K , V ]) Get (key K ) (V , bool ) {
42
- ps .lock .RLock ()
43
- defer ps .lock .RUnlock ()
39
+ func (o * OrderedSet [K , V ]) Get (key K ) (V , bool ) {
40
+ o .lock .RLock ()
41
+ defer o .lock .RUnlock ()
44
42
45
- if pos , ok := ps .itemPos [key ]; ok {
46
- return ps .items [pos ], true
43
+ if pos , ok := o .itemPos [key ]; ok {
44
+ return o .items [pos ], true
47
45
}
48
46
var zero V
49
47
return zero , false
50
48
}
51
49
52
- func (ps * OrderedSet [K , V ]) Size () int {
53
- ps .lock .RLock ()
54
- defer ps .lock .RUnlock ()
50
+ func (o * OrderedSet [K , V ]) Size () int {
51
+ o .lock .RLock ()
52
+ defer o .lock .RUnlock ()
55
53
56
- return ps . size
54
+ return len ( o . items )
57
55
}
58
56
59
57
// List returns a shallow copy of the proof set's value list.
60
- func (ps * OrderedSet [K , V ]) List () []V {
61
- ps .lock .RLock ()
62
- defer ps .lock .RUnlock ()
58
+ func (o * OrderedSet [K , V ]) List () []V {
59
+ o .lock .RLock ()
60
+ defer o .lock .RUnlock ()
63
61
64
- values := make ([]V , len (ps .items ))
65
- copy (values , ps .items )
62
+ values := make ([]V , len (o .items ))
63
+ copy (values , o .items )
66
64
return values
67
65
}
0 commit comments