diff --git a/hashmap/avlkeydup.go b/hashmap/avlkeydup.go index 2489fb5..2fba6a3 100644 --- a/hashmap/avlkeydup.go +++ b/hashmap/avlkeydup.go @@ -22,6 +22,11 @@ func avlNew(Compare compare.Compare) *avlTree { return &avlTree{Compare: Compare} } +func (tree *avlTree) Clear() { + tree.size = 0 + tree.root = nil +} + func (tree *avlTree) Size() int { return tree.size } @@ -222,7 +227,7 @@ const ( ) // Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r -func (tree *avlTree) Traversal(every func(k, v interface{}) bool, traversalMethod ...interface{}) { +func (tree *avlTree) Traversal(every func(node *avlNode) bool, traversalMethod ...interface{}) { if tree.root == nil { return } @@ -239,7 +244,7 @@ func (tree *avlTree) Traversal(every func(k, v interface{}) bool, traversalMetho if cur == nil { return true } - if !every(cur.key, cur.value) { + if !every(cur) { return false } if !traverasl(cur.children[0]) { @@ -260,7 +265,7 @@ func (tree *avlTree) Traversal(every func(k, v interface{}) bool, traversalMetho if !traverasl(cur.children[0]) { return false } - if !every(cur.key, cur.value) { + if !every(cur) { return false } if !traverasl(cur.children[1]) { @@ -281,7 +286,7 @@ func (tree *avlTree) Traversal(every func(k, v interface{}) bool, traversalMetho if !traverasl(cur.children[1]) { return false } - if !every(cur.key, cur.value) { + if !every(cur) { return false } return true @@ -293,7 +298,7 @@ func (tree *avlTree) Traversal(every func(k, v interface{}) bool, traversalMetho if cur == nil { return true } - if !every(cur.key, cur.value) { + if !every(cur) { return false } if !traverasl(cur.children[0]) { @@ -314,7 +319,7 @@ func (tree *avlTree) Traversal(every func(k, v interface{}) bool, traversalMetho if !traverasl(cur.children[1]) { return false } - if !every(cur.key, cur.value) { + if !every(cur) { return false } if !traverasl(cur.children[0]) { @@ -335,7 +340,7 @@ func (tree *avlTree) Traversal(every func(k, v interface{}) bool, traversalMetho if !traverasl(cur.children[0]) { return false } - if !every(cur.key, cur.value) { + if !every(cur) { return false } return true diff --git a/hashmap/hashmap.go b/hashmap/hashmap.go index 2124206..d6df96a 100644 --- a/hashmap/hashmap.go +++ b/hashmap/hashmap.go @@ -92,22 +92,38 @@ func (hm *HashMap) grow() { newsize := hm.size << 1 newtable := make([]*avlTree, newsize, newsize) + nodelist := make([]*avlNode, hm.size, hm.size) + i := 0 for _, cur := range hm.table { if cur != nil { - cur.Traversal(func(k, v interface{}) bool { - hash := hm.GetHash(k) - index := hash % newsize - bkt := newtable[index] - if bkt == nil { - bkt = avlNew(hm.Compare) - newtable[index] = bkt - } - bkt.Put(k, v) + cur.Traversal(func(node *avlNode) bool { + nodelist[i] = node + i++ + // hash := hm.GetHash(node.key) + // index := hash % newsize + // bkt := newtable[index] + // if bkt == nil { + // bkt = avlNew(hm.Compare) + // newtable[index] = bkt + // } + //bkt.Put(node.key, node.value) return true }) - } - } + } + cur.Clear() + } + var hash, index uint + for _, node := range nodelist { + hash = hm.GetHash(node.key) + index = hash % newsize + bkt := newtable[index] + if bkt == nil { + bkt = avlNew(hm.Compare) + newtable[index] = bkt + } + bkt.PutNode(node) + } hm.table = newtable hm.countNextGrow() }