修改PriorityQueue顺序, 以大到小为默认

This commit is contained in:
huangsimin 2019-07-18 14:02:12 +08:00
parent 411533f1a4
commit 4d56ed3348
7 changed files with 47 additions and 36 deletions

View File

@ -7,6 +7,16 @@
``` golang
pq := pqueuekey.New(compare.Int)
pq.Push(1, 1)
pq.Push(2, 2)
v = pq.Pop() // v = 2
pq.Push(4, 4)
pq.Push(5, 5)
pq.Push(6, 6)
pq.Push(2, 2) // pq.Values() = [6 5 4 2 1]
value, _ := pq.Pop() // value = 6
t.Error(value)
value, _ = pq.Get(1) // value = 1 pq.Values() = [5 4 2 1]
value, _ = pq.Get(0) // value = nil , Get equal to Seach Key
value, _ = pq.Index(0) // value = 5, compare.Int the order from big to small
values := pq.GetRange(2, 5) // values = [2 4 5]
values = pq.GetRange(5, 2) // values = [5 4 2]
values3 := pq.GetAround(5) // values3 = [<nil>, 5, 4]
```

View File

@ -122,32 +122,32 @@ func TestQueueGetAround(t *testing.T) {
var result string
result = spew.Sprint(pq.GetAround(53))
if result != "[32 53 78]" {
if result != "[78 53 32]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(52))
if result != "[32 <nil> 53]" {
if result != "[53 <nil> 32]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(1))
if result != "[<nil> 1 4]" {
if result != "[4 1 <nil>]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(90))
if result != "[78 90 <nil>]" {
if result != "[<nil> 90 78]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(0))
if result != "[<nil> <nil> 1]" {
if result != "[1 <nil> <nil>]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(100))
if result != "[90 <nil> <nil>]" {
if result != "[<nil> <nil> 90]" {
t.Error(result)
}
}

View File

@ -273,7 +273,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
return []interface{}{}
}
result = make([]interface{}, 0, 16)
result = make([]interface{}, 0, 8)
// iter := NewIterator(min)
tree.iter.SeNode(min)
@ -300,7 +300,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
return []interface{}{}
}
result = make([]interface{}, 0, 16)
result = make([]interface{}, 0, 8)
// iter := NewIterator(max)
tree.iter.SeNode(max)
@ -329,11 +329,12 @@ func (tree *vbTree) Get(key interface{}) (interface{}, bool) {
return n, false
}
// GetAround 改成Big To Small
func (tree *vbTree) GetAround(key interface{}) (result [3]interface{}) {
an := tree.getArounNode(key)
for i, n := range an {
if n != nil {
result[i] = n.value
result[2-i] = n.value
}
}
return

View File

@ -93,56 +93,56 @@ func TestGetAround(t *testing.T) {
var Result string
Result = spew.Sprint(tree.GetAround(17))
if Result != "[16 17 20]" {
if Result != "[20 17 16]" {
t.Error(tree.Values())
t.Error("17 is root, tree.GetAround(17)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(3))
if Result != "[<nil> 3 7]" {
if Result != "[7 3 <nil>]" {
t.Error(tree.Values())
t.Error("tree.GetAround(3)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(40))
if Result != "[30 40 40]" {
if Result != "[40 40 30]" {
t.Error(tree.Values())
t.Error("tree.GetAround(40)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(50))
if Result != "[40 50 <nil>]" {
if Result != "[<nil> 50 40]" {
t.Error(tree.Values())
t.Error("tree.GetAround(50)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(18))
if Result != "[17 <nil> 20]" {
if Result != "[20 <nil> 17]" {
t.Error(tree.Values())
t.Error("18 is not in list, tree.GetAround(18)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(5))
if Result != "[3 <nil> 7]" {
if Result != "[7 <nil> 3]" {
t.Error(tree.Values())
t.Error("5 is not in list, tree.GetAround(5)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(2))
if Result != "[<nil> <nil> 3]" {
if Result != "[3 <nil> <nil>]" {
t.Error(tree.Values())
t.Error("2 is not in list, tree.GetAround(2)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(100))
if Result != "[50 <nil> <nil>]" {
if Result != "[<nil> <nil> 50]" {
t.Error(tree.Values())
t.Error("50 is not in list, tree.GetAround(50)) is error", Result)
t.Error(tree.debugString())

View File

@ -122,32 +122,32 @@ func TestQueueGetAround(t *testing.T) {
var result string
result = spew.Sprint(pq.GetAround(53))
if result != "[32 53 78]" {
if result != "[78 53 32]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(52))
if result != "[32 <nil> 53]" {
if result != "[53 <nil> 32]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(1))
if result != "[<nil> 1 4]" {
if result != "[4 1 <nil>]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(90))
if result != "[78 90 <nil>]" {
if result != "[<nil> 90 78]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(0))
if result != "[<nil> <nil> 1]" {
if result != "[1 <nil> <nil>]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(100))
if result != "[90 <nil> <nil>]" {
if result != "[<nil> <nil> 90]" {
t.Error(result)
}
}

View File

@ -273,7 +273,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
return []interface{}{}
}
result = make([]interface{}, 0, 16)
result = make([]interface{}, 0, 8)
// iter := NewIterator(min)
tree.iter.SeNode(min)
@ -300,7 +300,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
return []interface{}{}
}
result = make([]interface{}, 0, 16)
result = make([]interface{}, 0, 8)
// iter := NewIterator(max)
tree.iter.SeNode(max)
@ -333,7 +333,7 @@ func (tree *vbTree) GetAround(key interface{}) (result [3]interface{}) {
an := tree.getArounNode(key)
for i, n := range an {
if n != nil {
result[i] = n.value
result[2-i] = n.value
}
}
return

View File

@ -93,56 +93,56 @@ func TestGetAround(t *testing.T) {
var Result string
Result = spew.Sprint(tree.GetAround(17))
if Result != "[16 17 20]" {
if Result != "[20 17 16]" {
t.Error(tree.Values())
t.Error("17 is root, tree.GetAround(17)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(3))
if Result != "[<nil> 3 7]" {
if Result != "[7 3 <nil>]" {
t.Error(tree.Values())
t.Error("tree.GetAround(3)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(40))
if Result != "[30 40 40]" {
if Result != "[40 40 30]" {
t.Error(tree.Values())
t.Error("tree.GetAround(40)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(50))
if Result != "[40 50 <nil>]" {
if Result != "[<nil> 50 40]" {
t.Error(tree.Values())
t.Error("tree.GetAround(50)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(18))
if Result != "[17 <nil> 20]" {
if Result != "[20 <nil> 17]" {
t.Error(tree.Values())
t.Error("18 is not in list, tree.GetAround(18)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(5))
if Result != "[3 <nil> 7]" {
if Result != "[7 <nil> 3]" {
t.Error(tree.Values())
t.Error("5 is not in list, tree.GetAround(5)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(2))
if Result != "[<nil> <nil> 3]" {
if Result != "[3 <nil> <nil>]" {
t.Error(tree.Values())
t.Error("2 is not in list, tree.GetAround(2)) is error", Result)
t.Error(tree.debugString())
}
Result = spew.Sprint(tree.GetAround(100))
if Result != "[50 <nil> <nil>]" {
if Result != "[<nil> <nil> 50]" {
t.Error(tree.Values())
t.Error("50 is not in list, tree.GetAround(50)) is error", Result)
t.Error(tree.debugString())