@@ -3,13 +3,11 @@ package bikeymap
33import (
44 "errors"
55 "fmt"
6- "sync"
76)
87
98// BiKeyMap is a generic in-memory map with two independent keys for each value.
109// It implements container/Container.
1110type BiKeyMap [KeyA comparable , KeyB comparable , V any ] struct {
12- mu sync.RWMutex
1311 dataByKeyA map [KeyA ]V
1412 keyAByKeyB map [KeyB ]KeyA
1513 keyBByKeyA map [KeyA ]KeyB
@@ -25,123 +23,99 @@ func New[KeyA comparable, KeyB comparable, V any]() *BiKeyMap[KeyA, KeyB, V] {
2523}
2624
2725// Put stores a value with two keys. It only fails if one of the keys is already set without the other.
28- func (c * BiKeyMap [KeyA , KeyB , V ]) Put (keyA KeyA , keyB KeyB , value V ) error {
29- c .mu .Lock ()
30- defer c .mu .Unlock ()
31-
26+ func (m * BiKeyMap [KeyA , KeyB , V ]) Put (keyA KeyA , keyB KeyB , value V ) error {
3227 // Check if one key is set without the other, or if they do not point to each other.
33- if existingKeyA , keyBExists := c .keyAByKeyB [keyB ]; keyBExists && existingKeyA != keyA {
28+ if existingKeyA , keyBExists := m .keyAByKeyB [keyB ]; keyBExists && existingKeyA != keyA {
3429 return errors .New ("keyB is already set with a different keyA" )
3530 }
36- if existingKeyB , keyAExists := c .keyBByKeyA [keyA ]; keyAExists && existingKeyB != keyB {
31+ if existingKeyB , keyAExists := m .keyBByKeyA [keyA ]; keyAExists && existingKeyB != keyB {
3732 return errors .New ("keyA is already set with a different keyB" )
3833 }
3934
4035 // Put the new values for both keys.
41- c .dataByKeyA [keyA ] = value
42- c .keyAByKeyB [keyB ] = keyA
43- c .keyBByKeyA [keyA ] = keyB
36+ m .dataByKeyA [keyA ] = value
37+ m .keyAByKeyB [keyB ] = keyA
38+ m .keyBByKeyA [keyA ] = keyB
4439 return nil
4540}
4641
4742// GetByKeyA retrieves a value using the first key.
48- func (c * BiKeyMap [KeyA , KeyB , V ]) GetByKeyA (keyA KeyA ) (V , bool ) {
49- c .mu .RLock ()
50- defer c .mu .RUnlock ()
51-
52- value , exists := c .dataByKeyA [keyA ]
43+ func (m * BiKeyMap [KeyA , KeyB , V ]) GetByKeyA (keyA KeyA ) (V , bool ) {
44+ value , exists := m .dataByKeyA [keyA ]
5345 return value , exists
5446}
5547
5648// GetByKeyB retrieves a value using the second key.
57- func (c * BiKeyMap [KeyA , KeyB , V ]) GetByKeyB (keyB KeyB ) (V , bool ) {
58- c .mu .RLock ()
59- defer c .mu .RUnlock ()
60-
61- keyA , exists := c .keyAByKeyB [keyB ]
49+ func (m * BiKeyMap [KeyA , KeyB , V ]) GetByKeyB (keyB KeyB ) (V , bool ) {
50+ keyA , exists := m .keyAByKeyB [keyB ]
6251 if ! exists {
6352 var zero V
6453 return zero , false
6554 }
6655
67- value , exists := c .dataByKeyA [keyA ]
56+ value , exists := m .dataByKeyA [keyA ]
6857 return value , exists
6958}
7059
7160// RemoveByKeyA removes a value using the first key, ensuring the corresponding second key is also deleted.
72- func (c * BiKeyMap [KeyA , KeyB , V ]) RemoveByKeyA (keyA KeyA ) error {
73- c .mu .Lock ()
74- defer c .mu .Unlock ()
75-
61+ func (m * BiKeyMap [KeyA , KeyB , V ]) RemoveByKeyA (keyA KeyA ) error {
7662 // Verify that keyA exists and retrieve the associated keyB.
77- keyB , ok := c .keyBByKeyA [keyA ]
63+ keyB , ok := m .keyBByKeyA [keyA ]
7864 if ! ok {
7965 return errors .New ("keyA does not exist" )
8066 }
8167
8268 // Remove keyA, keyB, and the associated value.
83- delete (c .dataByKeyA , keyA )
84- delete (c .keyAByKeyB , keyB )
85- delete (c .keyBByKeyA , keyA )
69+ delete (m .dataByKeyA , keyA )
70+ delete (m .keyAByKeyB , keyB )
71+ delete (m .keyBByKeyA , keyA )
8672
8773 return nil
8874}
8975
9076// RemoveByKeyB removes a value using the second key, ensuring the corresponding first key is also deleted.
91- func (c * BiKeyMap [KeyA , KeyB , V ]) RemoveByKeyB (keyB KeyB ) error {
92- c .mu .Lock ()
93- defer c .mu .Unlock ()
94-
77+ func (m * BiKeyMap [KeyA , KeyB , V ]) RemoveByKeyB (keyB KeyB ) error {
9578 // Verify that keyB exists and retrieve the associated keyA.
96- keyA , ok := c .keyAByKeyB [keyB ]
79+ keyA , ok := m .keyAByKeyB [keyB ]
9780 if ! ok {
9881 return errors .New ("keyB does not exist" )
9982 }
10083
10184 // Remove keyA, keyB, and the associated value.
102- delete (c .dataByKeyA , keyA )
103- delete (c .keyAByKeyB , keyB )
104- delete (c .keyBByKeyA , keyA )
85+ delete (m .dataByKeyA , keyA )
86+ delete (m .keyAByKeyB , keyB )
87+ delete (m .keyBByKeyA , keyA )
10588
10689 return nil
10790}
10891
10992// Empty checks if the map is empty.
110- func (c * BiKeyMap [KeyA , KeyB , V ]) Empty () bool {
111- return len (c .dataByKeyA ) == 0
93+ func (m * BiKeyMap [KeyA , KeyB , V ]) Empty () bool {
94+ return len (m .dataByKeyA ) == 0
11295}
11396
11497// Size returns the number of elements in the map.
115- func (c * BiKeyMap [KeyA , KeyB , V ]) Size () int {
116- return len (c .dataByKeyA )
98+ func (m * BiKeyMap [KeyA , KeyB , V ]) Size () int {
99+ return len (m .dataByKeyA )
117100}
118101
119102// Values returns a slice of all values in the map.
120- func (c * BiKeyMap [KeyA , KeyB , V ]) Values () []V {
121- c .mu .RLock ()
122- defer c .mu .RUnlock ()
123-
124- values := make ([]V , 0 , len (c .dataByKeyA ))
125- for _ , value := range c .dataByKeyA {
103+ func (m * BiKeyMap [KeyA , KeyB , V ]) Values () []V {
104+ values := make ([]V , 0 , len (m .dataByKeyA ))
105+ for _ , value := range m .dataByKeyA {
126106 values = append (values , value )
127107 }
128108 return values
129109}
130110
131111// Clear removes all elements from the map.
132- func (c * BiKeyMap [KeyA , KeyB , V ]) Clear () {
133- c .mu .Lock ()
134- defer c .mu .Unlock ()
135-
136- c .dataByKeyA = make (map [KeyA ]V )
137- c .keyAByKeyB = make (map [KeyB ]KeyA )
138- c .keyBByKeyA = make (map [KeyA ]KeyB )
112+ func (m * BiKeyMap [KeyA , KeyB , V ]) Clear () {
113+ m .dataByKeyA = make (map [KeyA ]V )
114+ m .keyAByKeyB = make (map [KeyB ]KeyA )
115+ m .keyBByKeyA = make (map [KeyA ]KeyB )
139116}
140117
141118// String returns a string representation of the map.
142- func (c * BiKeyMap [KeyA , KeyB , V ]) String () string {
143- c .mu .RLock ()
144- defer c .mu .RUnlock ()
145-
146- return fmt .Sprintf ("BiKeyMap: %v" , c .dataByKeyA )
119+ func (m * BiKeyMap [KeyA , KeyB , V ]) String () string {
120+ return fmt .Sprintf ("BiKeyMap: %v" , m .dataByKeyA )
147121}
0 commit comments