linked_hashmap 完成, 90%+覆盖率

This commit is contained in:
huangsimin 2019-07-19 19:05:35 +08:00
parent 76ca4e2a0e
commit 79e6e955f5
4 changed files with 108 additions and 22 deletions

View File

@ -2,7 +2,6 @@ package linkedlist
import ( import (
"fmt" "fmt"
"log"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
) )
@ -197,9 +196,9 @@ func (l *LinkedList) Index(idx uint) (interface{}, bool) {
return nil, false return nil, false
} }
func (l *LinkedList) Insert(idx uint, values ...interface{}) { func (l *LinkedList) Insert(idx uint, values ...interface{}) bool {
if idx > l.size { // 插入的方式 可能导致size的范围判断不一样 if idx > l.size { // 插入的方式 可能导致size的范围判断不一样
return return false
} }
if idx > l.size/2 { if idx > l.size/2 {
@ -270,6 +269,7 @@ func (l *LinkedList) Insert(idx uint, values ...interface{}) {
} }
l.size += uint(len(values)) l.size += uint(len(values))
return true
} }
// InsertState InsertIf的every函数的枚举 从左到右 1 为前 2 为后 insert here(2) ->cur-> insert here(1) // InsertState InsertIf的every函数的枚举 从左到右 1 为前 2 为后 insert here(2) ->cur-> insert here(1)
@ -350,7 +350,7 @@ func remove(cur *Node) {
func (l *LinkedList) Remove(idx uint) (interface{}, bool) { func (l *LinkedList) Remove(idx uint) (interface{}, bool) {
if l.size <= idx { if l.size <= idx {
log.Printf("out of list range, size is %d, idx is %d\n", l.size, idx) // log.Printf("out of list range, size is %d, idx is %d\n", l.size, idx)
return nil, false return nil, false
} }

View File

@ -103,7 +103,9 @@ func TestInsert2(t *testing.T) {
t.Error(l.String()) t.Error(l.String())
} }
l.Insert(l.Size(), 5) if !l.Insert(l.Size(), 5) {
t.Error("should be true")
}
// step1: [0 0] -> step2: [4 0 3 0 2 0 1 0 0 0] front size is 10, but you can insert 11. equal to PushBack [4 0 3 0 2 0 1 0 0 0 5] // step1: [0 0] -> step2: [4 0 3 0 2 0 1 0 0 0] front size is 10, but you can insert 11. equal to PushBack [4 0 3 0 2 0 1 0 0 0 5]
if l.String() != "[4 0 3 0 2 0 1 0 0 0 5]" { if l.String() != "[4 0 3 0 2 0 1 0 0 0 5]" {

View File

@ -1,8 +1,6 @@
package linkedhashmap package linkedhashmap
import ( import (
"fmt"
linkedlist "github.com/474420502/focus/list/linked_list" linkedlist "github.com/474420502/focus/list/linked_list"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
) )
@ -19,7 +17,15 @@ func New() *LinkedHashmap {
return lhmap return lhmap
} }
// PushBack if key exists, push value replace the value is exists. size is unchanging // Put equal to PushBack
func (lhmap *LinkedHashmap) Put(key interface{}, value interface{}) {
if _, ok := lhmap.hmap[key]; !ok {
lhmap.list.PushBack(key)
}
lhmap.hmap[key] = value
}
// PushBack equal to Put, if key exists, push value replace the value is exists. size is unchanging
func (lhmap *LinkedHashmap) PushBack(key interface{}, value interface{}) { func (lhmap *LinkedHashmap) PushBack(key interface{}, value interface{}) {
if _, ok := lhmap.hmap[key]; !ok { if _, ok := lhmap.hmap[key]; !ok {
lhmap.list.PushBack(key) lhmap.list.PushBack(key)
@ -35,10 +41,11 @@ func (lhmap *LinkedHashmap) PushFront(key interface{}, value interface{}) {
lhmap.hmap[key] = value lhmap.hmap[key] = value
} }
// Insert 如果成功在该位置返回True, 否则返回false // Insert 如果成功在该位置返回True, 否则返回false 类似 linkedlist Size 可以 等于 idx
func (lhmap *LinkedHashmap) Insert(idx uint, key interface{}, value interface{}) bool { func (lhmap *LinkedHashmap) Insert(idx uint, key interface{}, value interface{}) bool {
if _, ok := lhmap.hmap[key]; !ok { if _, ok := lhmap.hmap[key]; !ok {
lhmap.list.Insert(idx, key)
lhmap.hmap[key] = value
return true return true
} }
return false return false
@ -73,21 +80,22 @@ func (lhmap *LinkedHashmap) Remove(key interface{}) (interface{}, bool) {
// RemoveIndex // RemoveIndex
func (lhmap *LinkedHashmap) RemoveIndex(idx uint) (interface{}, bool) { func (lhmap *LinkedHashmap) RemoveIndex(idx uint) (interface{}, bool) {
if lhmap.list.Size() <= idx { if lhmap.list.Size() >= idx {
panic(fmt.Sprintf("out of list range, size is %d, idx is %d", lhmap.list.Size(), idx)) // log.Printf("warn: out of list range, size is %d, idx is %d\n", lhmap.list.Size(), idx)
} if key, ok := lhmap.list.Remove(idx); ok {
result := lhmap.hmap[key]
if _, ok := lhmap.hmap[key]; ok { delete(lhmap.hmap, key)
delete(lhmap.hmap, key) return result, true
lhmap.list.RemoveIf(func(idx uint, lkey interface{}) linkedlist.RemoveState { }
if lkey == key {
return linkedlist.RemoveAndBreak
}
return linkedlist.UnremoveAndContinue
})
} }
return nil, false
} }
// RemoveIf 不是必须不实现这个
// func (lhmap *LinkedHashmap) RemoveIf(every func(idx uint, key interface{}) RemoveState) (interface{}, bool) {
// }
// Empty returns true if map does not contain any elements // Empty returns true if map does not contain any elements
func (lhmap *LinkedHashmap) Empty() bool { func (lhmap *LinkedHashmap) Empty() bool {
return lhmap.Size() == 0 return lhmap.Size() == 0

View File

@ -29,6 +29,11 @@ func TestPush(t *testing.T) {
if lhm.String() != "[4 1 2 3]" { if lhm.String() != "[4 1 2 3]" {
t.Error(lhm.String()) t.Error(lhm.String())
} }
lhm.Put(5, 5)
if lhm.String() != "[4 1 2 3 5]" {
t.Error(lhm.String())
}
} }
func TestBase(t *testing.T) { func TestBase(t *testing.T) {
@ -76,6 +81,33 @@ func TestGet(t *testing.T) {
} }
} }
func TestInsert(t *testing.T) {
lhm := New()
for i := 0; i < 5; i++ {
lhm.Insert(0, i, i)
}
if lhm.String() != "[4 3 2 1 0]" {
t.Error(lhm.String())
}
if !lhm.Insert(2, 5, 5) {
t.Error("Insert 2 5 5 error check it")
}
if lhm.String() != "[4 3 5 2 1 0]" {
t.Error(lhm.String())
}
if !lhm.Insert(lhm.Size(), 6, 6) {
t.Error("Insert Size() error check it")
}
if lhm.String() != "[4 3 5 2 1 0 6]" {
t.Error(lhm.String())
}
}
func TestRemove(t *testing.T) { func TestRemove(t *testing.T) {
lhm := New() lhm := New()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
@ -95,4 +127,48 @@ func TestRemove(t *testing.T) {
resultStr = resultStr[0:1] + resultStr[3:] resultStr = resultStr[0:1] + resultStr[3:]
} }
if lhm.Size() != 0 {
t.Error(lhm.Size())
}
for i := 0; i < 10; i++ {
lhm.PushFront(i, i)
}
for i := 0; i < 10; i++ {
if i >= 5 {
lhm.Remove(i)
}
}
if lhm.String() != "[4 3 2 1 0]" {
t.Error(lhm.String())
}
// RemoveIndex [4 3 2 1 0]
if value, _ := lhm.RemoveIndex(2); value != 2 {
t.Error("[4 3 2 1 0] remove index 2, value is 2, but now is", value)
}
// [4 3 1 0]
if value, _ := lhm.RemoveIndex(2); value != 1 {
t.Error("[4 3 1 0] remove index 2, value is 1, but now is", value)
}
// [4 3 0]
if value, _ := lhm.RemoveIndex(2); value != 0 {
t.Error("[4 3 0] remove index 2, value is 0, but now is", value)
}
// [4 3]
if value, _ := lhm.RemoveIndex(2); value != nil {
t.Error("[4 3] remove index 2, value is nil, but now is", value)
}
// [4 3]
if value, _ := lhm.RemoveIndex(0); value != 4 {
t.Error("[4 3] remove index 0, value is 4, but now is", value)
}
} }