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 48c7a5a commit 466e74e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
20 changes: 19 additions & 1 deletion smap/smap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,42 @@ func NewSmap() *List {
}
}

func (l *List) Set(k, v string) {
found := false
for i := range l.list {
if l.list[i].key == k {
l.list[i].val = v
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 {

Expand Down
9 changes: 8 additions & 1 deletion smap/smap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,24 @@ func Test_Get(t *testing.T) {
func Test_Len(t *testing.T) {
if smap.Len() != len(cases) {
t.Errorf("have %d, want %d", smap.Len(), len(cases))
// t.Error(smap.list, cases)
}
}

func Test_Update(t *testing.T) {
func Test_Set2(t *testing.T) {
hello := "hello_hello"
smap.Set("hi0", hello)
if smap.Get("hi0") != hello {
t.Errorf("have %s, want %s", smap.Get("hi"), hello)
}
}

func Test_Len2(t *testing.T) {
if smap.Len() != len(cases) {
//t.Errorf("have %d, want %d", smap.Len(), len(cases))
t.Error("\n", smap.list, "\n", cases)
}
}
func BenchmarkMap(t *testing.B) {

// keys := NewSmap()
Expand Down

0 comments on commit 466e74e

Please sign in to comment.