This commit is contained in:
eson 2019-04-22 12:53:23 +08:00
parent 4ccb4f4016
commit 905dcf5f8f
2 changed files with 39 additions and 18 deletions

View File

@ -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

View File

@ -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
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.Put(k, v)
return true
})
bkt.PutNode(node)
}
}
hm.table = newtable
hm.countNextGrow()
}