Skip to content

Commit bbcbb16

Browse files
committed
feat: add map and mutil values map
1 parent a95ebd5 commit bbcbb16

File tree

7 files changed

+815
-7
lines changed

7 files changed

+815
-7
lines changed

gox/containerx/listx/array_list.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
package listx
1919

20-
import "github.com/openingo/godkits/array"
20+
import (
21+
"reflect"
22+
23+
"github.com/openingo/godkits/array"
24+
)
2125

2226
// DefaultArraylistSize the default array size 10
2327
const DefaultArraylistSize = 10
@@ -52,8 +56,8 @@ func (lst *Arraylist) Add(element ...any) {
5256
}
5357
}
5458

55-
// Remove from array list at index
56-
func (lst *Arraylist) Remove(index int) any {
59+
// RemoveAtIndex from array list at index
60+
func (lst *Arraylist) RemoveAtIndex(index int) any {
5761
if index < 0 || index >= lst.size {
5862
return nil
5963
}
@@ -65,6 +69,11 @@ func (lst *Arraylist) Remove(index int) any {
6569
return current
6670
}
6771

72+
// Remove from array list
73+
func (lst *Arraylist) Remove(element any) bool {
74+
return reflect.DeepEqual(lst.RemoveAtIndex(lst.IndexOf(element)), element)
75+
}
76+
6877
// Get element from array list at index
6978
func (lst *Arraylist) Get(index int) any {
7079
if index < 0 || index >= lst.size {
@@ -81,7 +90,7 @@ func (lst *Arraylist) IndexOf(element any) int {
8190

8291
// Empty array list
8392
func (lst *Arraylist) Empty() bool {
84-
return lst.Size() == 0 || array.Empty(lst.elements)
93+
return lst == nil || lst.Size() == 0 || array.Empty(lst.elements)
8594
}
8695

8796
// Size of array list

gox/containerx/listx/array_list_test.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func TestArraylist_IndexOf(t *testing.T) {
220220
}
221221
}
222222

223-
func TestArraylist_Remove(t *testing.T) {
223+
func TestArraylist_RemoveAtIndex(t *testing.T) {
224224
arrayList := &Arraylist{
225225
elements: []any{"hello", "world"},
226226
size: 2,
@@ -253,8 +253,8 @@ func TestArraylist_Remove(t *testing.T) {
253253
elements: tt.fields.elements,
254254
size: tt.fields.size,
255255
}
256-
if got := lst.Remove(tt.args.index); !reflect.DeepEqual(got, tt.want) {
257-
t.Errorf("Remove() = %v, want %v", got, tt.want)
256+
if got := lst.RemoveAtIndex(tt.args.index); !reflect.DeepEqual(got, tt.want) {
257+
t.Errorf("RemoveAtIndex() = %v, want %v", got, tt.want)
258258
}
259259
})
260260
}
@@ -368,3 +368,43 @@ func TestNew(t *testing.T) {
368368
})
369369
}
370370
}
371+
372+
func TestArraylist_Remove(t *testing.T) {
373+
arrayList := &Arraylist{
374+
elements: []any{"hello", "world"},
375+
size: 2,
376+
}
377+
type fields struct {
378+
elements []any
379+
size int
380+
}
381+
type args struct {
382+
element any
383+
}
384+
tests := []struct {
385+
name string
386+
fields fields
387+
args args
388+
want bool
389+
}{
390+
{
391+
fields: fields{
392+
elements: arrayList.elements,
393+
size: arrayList.size,
394+
},
395+
args: args{element: "hello"},
396+
want: true,
397+
},
398+
}
399+
for _, tt := range tests {
400+
t.Run(tt.name, func(t *testing.T) {
401+
lst := &Arraylist{
402+
elements: tt.fields.elements,
403+
size: tt.fields.size,
404+
}
405+
if got := lst.Remove(tt.args.element); got != tt.want {
406+
t.Errorf("Remove() = %v, want %v", got, tt.want)
407+
}
408+
})
409+
}
410+
}

gox/mapx/map_check.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2022, OpeningO
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package mapx
19+
20+
import "log"
21+
22+
func checkMap(m *Map) {
23+
if m == nil || m.kv == nil {
24+
log.Println("the map illegal state, invoke the `NewMap` func first")
25+
}
26+
}
27+
28+
func checkMultiValuesMap(m *MultiValueMap) {
29+
if m == nil || m.kvs == nil {
30+
log.Println("the mutil values map illegal state, invoke the `NewMultiValueMap` func first")
31+
}
32+
}

gox/mapx/mapx.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2022, OpeningO
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package mapx
19+
20+
// Map key value mapping
21+
type Map struct {
22+
size int
23+
kv map[any]any
24+
}
25+
26+
// NewMap new map
27+
func NewMap(cap int) *Map {
28+
m := Map{}
29+
m.kv = make(map[any]any, cap)
30+
m.size = 0
31+
return &m
32+
}
33+
34+
// ContainsKey check the key exist or not
35+
func (m *Map) ContainsKey(key any) bool {
36+
checkMap(m)
37+
return m.kv[key] != nil
38+
}
39+
40+
// Put key value to map
41+
func (m *Map) Put(key any, value any) bool {
42+
checkMap(m)
43+
m.kv[key] = value
44+
if !m.ContainsKey(key) {
45+
m.size++
46+
}
47+
return true
48+
}
49+
50+
// Get value by key
51+
func (m *Map) Get(key any) any {
52+
checkMap(m)
53+
return m.kv[key]
54+
}
55+
56+
// Value by key
57+
func (m *Map) Value(key any) any {
58+
checkMap(m)
59+
return m.kv[key]
60+
}
61+
62+
// Remove by key
63+
func (m *Map) Remove(key any) bool {
64+
checkMap(m)
65+
delete(m.kv, key)
66+
m.size--
67+
return true
68+
}
69+
70+
// Empty the map empty or not
71+
func (m *Map) Empty() bool {
72+
checkMap(m)
73+
return m.size == 0
74+
}
75+
76+
// Size map size
77+
func (m *Map) Size() int {
78+
checkMap(m)
79+
return m.size
80+
}

0 commit comments

Comments
 (0)