Skip to content

Commit c1d99fd

Browse files
committed
refactor: only use New function as constructor
1 parent 0098d28 commit c1d99fd

File tree

4 files changed

+40
-40
lines changed

4 files changed

+40
-40
lines changed

bikeymap/bikeymap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type BiKeyMap[KeyA comparable, KeyB comparable, V any] struct {
1515
keyBByKeyA map[KeyA]KeyB
1616
}
1717

18-
// NewBiKeyMap creates a new instance of BiKeyMap.
19-
func NewBiKeyMap[KeyA comparable, KeyB comparable, V any]() *BiKeyMap[KeyA, KeyB, V] {
18+
// New creates a new instance of BiKeyMap.
19+
func New[KeyA comparable, KeyB comparable, V any]() *BiKeyMap[KeyA, KeyB, V] {
2020
return &BiKeyMap[KeyA, KeyB, V]{
2121
dataByKeyA: make(map[KeyA]V),
2222
keyAByKeyB: make(map[KeyB]KeyA),

bikeymap/bikeymap_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/aeimer/go-multikeymap/container"
1010
)
1111

12-
func ExampleNewBiKeyMap() {
13-
bm := NewBiKeyMap[string, int, string]()
12+
func ExampleNew() {
13+
bm := New[string, int, string]()
1414
_ = bm.Put("keyA1", 1, "value1")
1515
value, exists := bm.GetByKeyA("keyA1")
1616
fmt.Printf("[Key A] value: %v, exists: %v\n", value, exists)
@@ -22,14 +22,14 @@ func ExampleNewBiKeyMap() {
2222
}
2323

2424
func TestBiKeyMap_ImplementsContainerInterface(t *testing.T) {
25-
instance := NewBiKeyMap[int, int, int]()
25+
instance := New[int, int, int]()
2626
if _, ok := any(instance).(container.Container[int]); !ok {
2727
t.Error("BiKeyMap does not implement the Container interface")
2828
}
2929
}
3030

3131
func TestBiKeyMap_SetAndGet(t *testing.T) {
32-
bm := NewBiKeyMap[string, int, string]()
32+
bm := New[string, int, string]()
3333

3434
err := bm.Put("keyA1", 1, "value1")
3535
if err != nil {
@@ -48,7 +48,7 @@ func TestBiKeyMap_SetAndGet(t *testing.T) {
4848
}
4949

5050
func TestBiKeyMap_SetDuplicateKeys(t *testing.T) {
51-
bm := NewBiKeyMap[string, int, string]()
51+
bm := New[string, int, string]()
5252

5353
err := bm.Put("keyA1", 1, "value1")
5454
if err != nil {
@@ -67,7 +67,7 @@ func TestBiKeyMap_SetDuplicateKeys(t *testing.T) {
6767
}
6868

6969
func TestBiKeyMap_RemoveByKeyA(t *testing.T) {
70-
bm := NewBiKeyMap[string, int, string]()
70+
bm := New[string, int, string]()
7171

7272
_ = bm.Put("keyA1", 1, "value1")
7373
err := bm.RemoveByKeyA("keyA1")
@@ -87,7 +87,7 @@ func TestBiKeyMap_RemoveByKeyA(t *testing.T) {
8787
}
8888

8989
func TestBiKeyMap_RemoveByKeyB(t *testing.T) {
90-
bm := NewBiKeyMap[string, int, string]()
90+
bm := New[string, int, string]()
9191

9292
_ = bm.Put("keyA1", 1, "value1")
9393
err := bm.RemoveByKeyB(1)
@@ -107,7 +107,7 @@ func TestBiKeyMap_RemoveByKeyB(t *testing.T) {
107107
}
108108

109109
func TestBiKeyMap_String(t *testing.T) {
110-
bm := NewBiKeyMap[string, int, string]()
110+
bm := New[string, int, string]()
111111
_ = bm.Put("keyA1", 1, "value1")
112112
expected := "BiKeyMap: map[keyA1:value1]"
113113
if bm.String() != expected {
@@ -116,22 +116,22 @@ func TestBiKeyMap_String(t *testing.T) {
116116
}
117117

118118
func TestBiKeyMap_RemoveByKeyA_NotFound(t *testing.T) {
119-
bm := NewBiKeyMap[string, int, string]()
119+
bm := New[string, int, string]()
120120
err := bm.RemoveByKeyA("nonExistentKey")
121121
if err == nil {
122122
t.Error("expected error, got nil")
123123
}
124124
}
125125

126126
func TestBiKeyMap_RemoveByKeyB_NotFound(t *testing.T) {
127-
bm := NewBiKeyMap[string, int, string]()
127+
bm := New[string, int, string]()
128128
err := bm.RemoveByKeyB(999)
129129
if err == nil {
130130
t.Error("expected error, got nil")
131131
}
132132
}
133133
func TestBiKeyMap_EmptyAndSize(t *testing.T) {
134-
bm := NewBiKeyMap[string, int, string]()
134+
bm := New[string, int, string]()
135135

136136
if !bm.Empty() {
137137
t.Error("expected map to be empty")
@@ -148,7 +148,7 @@ func TestBiKeyMap_EmptyAndSize(t *testing.T) {
148148
}
149149

150150
func TestBiKeyMap_Clear(t *testing.T) {
151-
bm := NewBiKeyMap[string, int, string]()
151+
bm := New[string, int, string]()
152152

153153
_ = bm.Put("keyA1", 1, "value1")
154154
_ = bm.Put("keyA2", 2, "value2")
@@ -164,7 +164,7 @@ func TestBiKeyMap_Clear(t *testing.T) {
164164
}
165165

166166
func TestBiKeyMap_Values(t *testing.T) {
167-
bm := NewBiKeyMap[string, int, string]()
167+
bm := New[string, int, string]()
168168

169169
_ = bm.Put("keyA1", 1, "value1")
170170
_ = bm.Put("keyA2", 2, "value2")
@@ -183,7 +183,7 @@ func TestBiKeyMap_Values(t *testing.T) {
183183
}
184184

185185
func TestBiKeyMap_ConcurrentAccess(t *testing.T) {
186-
bm := NewBiKeyMap[string, int, string]()
186+
bm := New[string, int, string]()
187187
var wg sync.WaitGroup
188188
const numGoroutines = 100
189189

@@ -249,7 +249,7 @@ func TestBiKeyMap_ConcurrentAccess(t *testing.T) {
249249
// Benchmarks
250250

251251
func benchmarkGet(b *testing.B, size int) {
252-
m := NewBiKeyMap[string, int, string]()
252+
m := New[string, int, string]()
253253
for n := 0; n < size; n++ {
254254
_ = m.Put(strconv.Itoa(n), n, strconv.Itoa(n))
255255
}
@@ -263,7 +263,7 @@ func benchmarkGet(b *testing.B, size int) {
263263
}
264264

265265
func benchmarkPut(b *testing.B, size int) {
266-
m := NewBiKeyMap[string, int, string]()
266+
m := New[string, int, string]()
267267
b.ResetTimer()
268268
for i := 0; i < b.N; i++ {
269269
for n := 0; n < size; n++ {
@@ -273,7 +273,7 @@ func benchmarkPut(b *testing.B, size int) {
273273
}
274274

275275
func benchmarkRemove(b *testing.B, size int) {
276-
m := NewBiKeyMap[string, int, string]()
276+
m := New[string, int, string]()
277277
for n := 0; n < size; n++ {
278278
_ = m.Put(strconv.Itoa(n), n, strconv.Itoa(n))
279279
}

multikeymap/multikeymap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type MultiKeyMap[K comparable, V any] struct {
1414
secondaryTo map[K]map[string]string // PrimaryKey -> Group -> SecondaryKey
1515
}
1616

17-
// NewMultiKeyMap creates a new MultiKeyMap instance.
18-
func NewMultiKeyMap[K comparable, V any]() *MultiKeyMap[K, V] {
17+
// New creates a new MultiKeyMap instance.
18+
func New[K comparable, V any]() *MultiKeyMap[K, V] {
1919
return &MultiKeyMap[K, V]{
2020
primary: make(map[K]V),
2121
secondary: make(map[string]map[string]K),

multikeymap/multikeymap_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/aeimer/go-multikeymap/container"
1010
)
1111

12-
func ExampleNewMultiKeyMap() {
13-
mm := NewMultiKeyMap[string, int]()
12+
func ExampleNew() {
13+
mm := New[string, int]()
1414
mm.Put("keyA1", 1)
1515
mm.PutSecondaryKeys("keyA1", "group1", "key1", "key2")
1616
mm.PutSecondaryKeys("keyA1", "group2", "key3", "key4")
@@ -25,14 +25,14 @@ func ExampleNewMultiKeyMap() {
2525
}
2626

2727
func TestMultiKeyMap_ImplementsContainerInterface(t *testing.T) {
28-
instance := NewMultiKeyMap[int, int]()
28+
instance := New[int, int]()
2929
if _, ok := any(instance).(container.Container[int]); !ok {
3030
t.Error("MultiKeyMap does not implement the Container interface")
3131
}
3232
}
3333

3434
func TestMultiKeyMap_SetAndGet(t *testing.T) {
35-
mm := NewMultiKeyMap[string, int]()
35+
mm := New[string, int]()
3636
mm.Put("key1", 1)
3737
value, exists := mm.Get("key1")
3838
if !exists || value != 1 {
@@ -41,7 +41,7 @@ func TestMultiKeyMap_SetAndGet(t *testing.T) {
4141
}
4242

4343
func TestMultiKeyMap_SetSecondaryKeys(t *testing.T) {
44-
mm := NewMultiKeyMap[string, int]()
44+
mm := New[string, int]()
4545
mm.Put("key1", 1)
4646
mm.PutSecondaryKeys("key1", "group1", "secKey1", "secKey2")
4747
value, exists := mm.GetBySecondaryKey("group1", "secKey1")
@@ -51,15 +51,15 @@ func TestMultiKeyMap_SetSecondaryKeys(t *testing.T) {
5151
}
5252

5353
func TestMultiKeyMap_HasPrimaryKey(t *testing.T) {
54-
mm := NewMultiKeyMap[string, int]()
54+
mm := New[string, int]()
5555
mm.Put("key1", 1)
5656
if !mm.HasPrimaryKey("key1") {
5757
t.Error("expected primary key 'key1' to exist")
5858
}
5959
}
6060

6161
func TestMultiKeyMap_HasSecondaryKey(t *testing.T) {
62-
mm := NewMultiKeyMap[string, int]()
62+
mm := New[string, int]()
6363
mm.Put("key1", 1)
6464
mm.PutSecondaryKeys("key1", "group1", "secKey1")
6565
if !mm.HasSecondaryKey("group1", "secKey1") {
@@ -68,7 +68,7 @@ func TestMultiKeyMap_HasSecondaryKey(t *testing.T) {
6868
}
6969

7070
func TestMultiKeyMap_Remove(t *testing.T) {
71-
mm := NewMultiKeyMap[string, int]()
71+
mm := New[string, int]()
7272
mm.Put("key1", 1)
7373
mm.PutSecondaryKeys("key1", "group1", "secKey1")
7474
mm.Remove("key1")
@@ -81,7 +81,7 @@ func TestMultiKeyMap_Remove(t *testing.T) {
8181
}
8282

8383
func TestMultiKeyMap_GetAllKeyGroups(t *testing.T) {
84-
mm := NewMultiKeyMap[string, int]()
84+
mm := New[string, int]()
8585
mm.Put("key1", 1)
8686
mm.PutSecondaryKeys("key1", "group1", "secKey1", "secKey2")
8787
mm.Put("key2", 2)
@@ -93,7 +93,7 @@ func TestMultiKeyMap_GetAllKeyGroups(t *testing.T) {
9393
}
9494

9595
func TestMultiKeyMap_GetBySecondaryKey_NotFound(t *testing.T) {
96-
mm := NewMultiKeyMap[string, int]()
96+
mm := New[string, int]()
9797
mm.Put("key1", 1)
9898
mm.PutSecondaryKeys("key1", "group1", "secKey1")
9999
if _, exists := mm.GetBySecondaryKey("group1", "nonExistentKey"); exists {
@@ -102,7 +102,7 @@ func TestMultiKeyMap_GetBySecondaryKey_NotFound(t *testing.T) {
102102
}
103103

104104
func TestMultiKeyMap_String(t *testing.T) {
105-
mm := NewMultiKeyMap[string, int]()
105+
mm := New[string, int]()
106106
mm.Put("key1", 1)
107107
expected := "MultiKeyMap: map[key1:1]"
108108
if mm.String() != expected {
@@ -111,7 +111,7 @@ func TestMultiKeyMap_String(t *testing.T) {
111111
}
112112

113113
func TestMultiKeyMap_Size(t *testing.T) {
114-
mm := NewMultiKeyMap[string, int]()
114+
mm := New[string, int]()
115115
mm.Put("key1", 1)
116116
mm.Put("key2", 2)
117117
if mm.Size() != 2 {
@@ -120,7 +120,7 @@ func TestMultiKeyMap_Size(t *testing.T) {
120120
}
121121

122122
func TestMultiKeyMap_Empty(t *testing.T) {
123-
mm := NewMultiKeyMap[string, int]()
123+
mm := New[string, int]()
124124
if !mm.Empty() {
125125
t.Error("expected map to be empty")
126126
}
@@ -131,7 +131,7 @@ func TestMultiKeyMap_Empty(t *testing.T) {
131131
}
132132

133133
func TestMultiKeyMap_Values(t *testing.T) {
134-
mm := NewMultiKeyMap[string, int]()
134+
mm := New[string, int]()
135135
mm.Put("key1", 1)
136136
mm.Put("key2", 2)
137137
values := mm.Values()
@@ -144,7 +144,7 @@ func TestMultiKeyMap_Values(t *testing.T) {
144144
}
145145

146146
func TestMultiKeyMap_Clear(t *testing.T) {
147-
mm := NewMultiKeyMap[string, int]()
147+
mm := New[string, int]()
148148
mm.Put("key1", 1)
149149
mm.Clear()
150150
if !mm.Empty() {
@@ -153,7 +153,7 @@ func TestMultiKeyMap_Clear(t *testing.T) {
153153
}
154154

155155
func TestMultiKeyMap_ConcurrentAccess(t *testing.T) {
156-
mm := NewMultiKeyMap[string, int]()
156+
mm := New[string, int]()
157157
var wg sync.WaitGroup
158158
const numGoroutines = 100
159159

@@ -205,7 +205,7 @@ func TestMultiKeyMap_ConcurrentAccess(t *testing.T) {
205205
// Benchmarks
206206

207207
func benchmarkGet(b *testing.B, size int) {
208-
m := NewMultiKeyMap[string, int]()
208+
m := New[string, int]()
209209
for n := 0; n < size; n++ {
210210
m.Put(strconv.Itoa(n), n)
211211
}
@@ -218,7 +218,7 @@ func benchmarkGet(b *testing.B, size int) {
218218
}
219219

220220
func benchmarkPut(b *testing.B, size int) {
221-
m := NewMultiKeyMap[string, int]()
221+
m := New[string, int]()
222222
b.ResetTimer()
223223
for i := 0; i < b.N; i++ {
224224
for n := 0; n < size; n++ {
@@ -228,7 +228,7 @@ func benchmarkPut(b *testing.B, size int) {
228228
}
229229

230230
func benchmarkRemove(b *testing.B, size int) {
231-
m := NewMultiKeyMap[string, int]()
231+
m := New[string, int]()
232232
for n := 0; n < size; n++ {
233233
m.Put(strconv.Itoa(n), n)
234234
}

0 commit comments

Comments
 (0)