Skip to content

Commit

Permalink
improve small map
Browse files Browse the repository at this point in the history
  • Loading branch information
baxiry committed Jun 28, 2024
1 parent 466e74e commit 2322b70
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
31 changes: 6 additions & 25 deletions smap/smap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,13 @@ func (l *List) Set(k, v string) {
found = true
break
}

}
if !found {
l.list = append(l.list, Map{k, v})
}
}

/*
func (l *List) Set(k, v string) {
kv := Map{k, v}
if len(l.list) < 1 {
l.list = append(l.list, kv)
return
}
for i := 0; i < len(l.list); i++ {
if l.list[i].key == k {
l.list[i].val = v
break
} else if l.list[i].key == "" {
l.list[i].val = v
break
} else {
l.list = append(l.list, kv)
break
}
}
}
*/

func (l *List) Get(k string) string {

for i := 0; i < len(l.list); i++ {
Expand All @@ -63,11 +41,14 @@ func (l *List) Get(k string) string {
}

func (l *List) Delete(k string) {
list := []Map{}

for i := 0; i < len(l.list); i++ {
if l.list[i].key == k {
l.list[i].val = ""
if l.list[i].key != k {
list = append(list, l.list[i])
}
}
l.list = list
}

func (l *List) Len() int {
Expand Down
16 changes: 16 additions & 0 deletions smap/smap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ func Test_Len2(t *testing.T) {
t.Error("\n", smap.list, "\n", cases)
}
}

func Test_Delete(t *testing.T) {
hh := "hello_hello"
smap.Delete("hi0")
if smap.Get("hi0") == hh {
t.Errorf("have %s, want %s", smap.Get("hi0"), hh)
}
}

func Test_Len3(t *testing.T) {
if smap.Len() >= len(cases) {
t.Errorf("have %d, want %d", smap.Len(), len(cases)-1)
// t.Error("\n", smap.list, "\n", cases)
}
}

func BenchmarkMap(t *testing.B) {

// keys := NewSmap()
Expand Down

0 comments on commit 2322b70

Please sign in to comment.