Skip to content

Commit

Permalink
feat: add map and mutil values map
Browse files Browse the repository at this point in the history
  • Loading branch information
qicz committed May 12, 2022
1 parent a95ebd5 commit bbcbb16
Show file tree
Hide file tree
Showing 7 changed files with 815 additions and 7 deletions.
17 changes: 13 additions & 4 deletions gox/containerx/listx/array_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

package listx

import "github.com/openingo/godkits/array"
import (
"reflect"

"github.com/openingo/godkits/array"
)

// DefaultArraylistSize the default array size 10
const DefaultArraylistSize = 10
Expand Down Expand Up @@ -52,8 +56,8 @@ func (lst *Arraylist) Add(element ...any) {
}
}

// Remove from array list at index
func (lst *Arraylist) Remove(index int) any {
// RemoveAtIndex from array list at index
func (lst *Arraylist) RemoveAtIndex(index int) any {
if index < 0 || index >= lst.size {
return nil
}
Expand All @@ -65,6 +69,11 @@ func (lst *Arraylist) Remove(index int) any {
return current
}

// Remove from array list
func (lst *Arraylist) Remove(element any) bool {
return reflect.DeepEqual(lst.RemoveAtIndex(lst.IndexOf(element)), element)
}

// Get element from array list at index
func (lst *Arraylist) Get(index int) any {
if index < 0 || index >= lst.size {
Expand All @@ -81,7 +90,7 @@ func (lst *Arraylist) IndexOf(element any) int {

// Empty array list
func (lst *Arraylist) Empty() bool {
return lst.Size() == 0 || array.Empty(lst.elements)
return lst == nil || lst.Size() == 0 || array.Empty(lst.elements)
}

// Size of array list
Expand Down
46 changes: 43 additions & 3 deletions gox/containerx/listx/array_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestArraylist_IndexOf(t *testing.T) {
}
}

func TestArraylist_Remove(t *testing.T) {
func TestArraylist_RemoveAtIndex(t *testing.T) {
arrayList := &Arraylist{
elements: []any{"hello", "world"},
size: 2,
Expand Down Expand Up @@ -253,8 +253,8 @@ func TestArraylist_Remove(t *testing.T) {
elements: tt.fields.elements,
size: tt.fields.size,
}
if got := lst.Remove(tt.args.index); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Remove() = %v, want %v", got, tt.want)
if got := lst.RemoveAtIndex(tt.args.index); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RemoveAtIndex() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -368,3 +368,43 @@ func TestNew(t *testing.T) {
})
}
}

func TestArraylist_Remove(t *testing.T) {
arrayList := &Arraylist{
elements: []any{"hello", "world"},
size: 2,
}
type fields struct {
elements []any
size int
}
type args struct {
element any
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
fields: fields{
elements: arrayList.elements,
size: arrayList.size,
},
args: args{element: "hello"},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lst := &Arraylist{
elements: tt.fields.elements,
size: tt.fields.size,
}
if got := lst.Remove(tt.args.element); got != tt.want {
t.Errorf("Remove() = %v, want %v", got, tt.want)
}
})
}
}
32 changes: 32 additions & 0 deletions gox/mapx/map_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2022, OpeningO
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mapx

import "log"

func checkMap(m *Map) {
if m == nil || m.kv == nil {
log.Println("the map illegal state, invoke the `NewMap` func first")
}
}

func checkMultiValuesMap(m *MultiValueMap) {
if m == nil || m.kvs == nil {
log.Println("the mutil values map illegal state, invoke the `NewMultiValueMap` func first")
}
}
80 changes: 80 additions & 0 deletions gox/mapx/mapx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2022, OpeningO
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mapx

// Map key value mapping
type Map struct {
size int
kv map[any]any
}

// NewMap new map
func NewMap(cap int) *Map {
m := Map{}
m.kv = make(map[any]any, cap)
m.size = 0
return &m
}

// ContainsKey check the key exist or not
func (m *Map) ContainsKey(key any) bool {
checkMap(m)
return m.kv[key] != nil
}

// Put key value to map
func (m *Map) Put(key any, value any) bool {
checkMap(m)
m.kv[key] = value
if !m.ContainsKey(key) {
m.size++
}
return true
}

// Get value by key
func (m *Map) Get(key any) any {
checkMap(m)
return m.kv[key]
}

// Value by key
func (m *Map) Value(key any) any {
checkMap(m)
return m.kv[key]
}

// Remove by key
func (m *Map) Remove(key any) bool {
checkMap(m)
delete(m.kv, key)
m.size--
return true
}

// Empty the map empty or not
func (m *Map) Empty() bool {
checkMap(m)
return m.size == 0
}

// Size map size
func (m *Map) Size() int {
checkMap(m)
return m.size
}
Loading

0 comments on commit bbcbb16

Please sign in to comment.