diff --git a/README.md b/README.md index d1eefd6..4bd7dc9 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,13 @@ func main() { iter := pq.Iterator() // Next 小到大 从root节点起始 // log.Println(iter.Value()) 直接使用会报错, - iter.Next() // Next 从小到大 + iter.ToHead() log.Println(iter.Value()) // 起始最大值. true 5 + log.Println(iter.Prev(), iter.Value()) // false 5 - log.Println(iter.Next(), iter.Value()) // false 5 // Prev 大到小 - log.Println(iter.Prev(), iter.Value()) // true 4 + log.Println(iter.Next(), iter.Value()) // true 4 + + } ``` diff --git a/list/linked_list/iterator.go b/list/linked_list/iterator.go index 0b12219..2024612 100644 --- a/list/linked_list/iterator.go +++ b/list/linked_list/iterator.go @@ -25,11 +25,11 @@ func (iter *Iterator) Next() bool { return iter.cur != iter.ll.tail } -func (iter *Iterator) MoveToHead() { +func (iter *Iterator) ToHead() { iter.cur = iter.ll.head } -func (iter *Iterator) MoveToTail() { +func (iter *Iterator) ToTail() { iter.cur = iter.ll.tail } diff --git a/list/priority_list/iterator.go b/list/priority_list/iterator.go index cc1738d..44a3543 100644 --- a/list/priority_list/iterator.go +++ b/list/priority_list/iterator.go @@ -25,11 +25,11 @@ func (iter *Iterator) Next() bool { return iter.cur != iter.pl.tail } -func (iter *Iterator) MoveToHead() { +func (iter *Iterator) ToHead() { iter.cur = iter.pl.head } -func (iter *Iterator) MoveToTail() { +func (iter *Iterator) ToTail() { iter.cur = iter.pl.tail } @@ -78,10 +78,10 @@ func (iter *CircularIterator) Next() bool { return true } -func (iter *CircularIterator) MoveToHead() { +func (iter *CircularIterator) ToHead() { iter.cur = iter.pl.head } -func (iter *CircularIterator) MoveToTail() { +func (iter *CircularIterator) ToTail() { iter.cur = iter.pl.tail } diff --git a/list/priority_list/priority_list_test.go b/list/priority_list/priority_list_test.go index 2ea0129..4c5d0e9 100644 --- a/list/priority_list/priority_list_test.go +++ b/list/priority_list/priority_list_test.go @@ -102,7 +102,7 @@ func TestCircularIterator(t *testing.T) { } } - iter.MoveToTail() + iter.ToTail() for i := 0; i != 10; i++ { iter.Prev() if iter.Value() != i { diff --git a/priority_queue/iterator.go b/priority_queue/iterator.go index 41dcfee..a8feea8 100644 --- a/priority_queue/iterator.go +++ b/priority_queue/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,25 +54,7 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - -func (iter *Iterator) GetNext(cur *Node, idx int) *Node { +func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { // iter := NewIterator(cur) iter.SetNode(cur) @@ -93,7 +85,7 @@ func (iter *Iterator) GetNext(cur *Node, idx int) *Node { return cur } -func (iter *Iterator) Next() (result bool) { +func (iter *Iterator) Prev() (result bool) { if iter.dir > -1 { if iter.dir == 1 && iter.cur != nil { @@ -121,7 +113,7 @@ func (iter *Iterator) Next() (result bool) { return false } -func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { +func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) iter.SetNode(cur) @@ -152,7 +144,7 @@ func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { return cur } -func (iter *Iterator) Prev() (result bool) { +func (iter *Iterator) Next() (result bool) { if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev) if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈 diff --git a/priority_queue/priority_queue.go b/priority_queue/priority_queue.go index 400b7d8..ce52fe6 100644 --- a/priority_queue/priority_queue.go +++ b/priority_queue/priority_queue.go @@ -1,6 +1,9 @@ package pqueue -import "github.com/474420502/focus/compare" +import ( + "github.com/474420502/focus/compare" + "github.com/davecgh/go-spew/spew" +) type PriorityQueue struct { queue *vbTree @@ -78,3 +81,7 @@ func (pq *PriorityQueue) Remove(key interface{}) (interface{}, bool) { func (pq *PriorityQueue) Values() []interface{} { return pq.queue.Values() } + +func (pq *PriorityQueue) String() string { + return spew.Sprint(pq.queue.Values()) +} diff --git a/priority_queue/priority_queue_test.go b/priority_queue/priority_queue_test.go index c3bf3f5..b168708 100644 --- a/priority_queue/priority_queue_test.go +++ b/priority_queue/priority_queue_test.go @@ -242,6 +242,35 @@ func TestQueueIndex(t *testing.T) { } } +func TestPriorityQueue_Iterator(t *testing.T) { + pq := New(compare.Int) + for i := 0; i < 5; i++ { + pq.Push(i) + } + + pq.Push(-1) + pq.Push(10) + + result := pq.String() + if result != "[10 4 3 2 1 0 -1]" { + t.Error("should be [10 4 3 2 1 0 -1]") + } + + iter := pq.Iterator() + iter.ToHead() + + values := pq.Values() + for i := 0; ; i++ { + if values[i] != iter.Value() { + t.Error(values[i], " != ", iter.Value()) + } + + if !iter.Next() { + break + } + } +} + // func BenchmarkQueueGet(b *testing.B) { // l := loadTestData() diff --git a/priority_queuekey/iterator.go b/priority_queuekey/iterator.go index 4481862..4b56206 100644 --- a/priority_queuekey/iterator.go +++ b/priority_queuekey/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,25 +54,7 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - -func (iter *Iterator) GetNext(cur *Node, idx int) *Node { +func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { // iter := NewIterator(cur) iter.SetNode(cur) @@ -93,7 +85,7 @@ func (iter *Iterator) GetNext(cur *Node, idx int) *Node { return cur } -func (iter *Iterator) Next() (result bool) { +func (iter *Iterator) Prev() (result bool) { if iter.dir > -1 { if iter.dir == 1 && iter.cur != nil { @@ -121,7 +113,7 @@ func (iter *Iterator) Next() (result bool) { return false } -func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { +func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) iter.SetNode(cur) @@ -152,7 +144,7 @@ func (iter *Iterator) GetPrev(cur *Node, idx int) *Node { return cur } -func (iter *Iterator) Prev() (result bool) { +func (iter *Iterator) Next() (result bool) { if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev) if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈 diff --git a/priority_queuekey/priority_queuekey.go b/priority_queuekey/priority_queuekey.go index 0954043..b3d3779 100644 --- a/priority_queuekey/priority_queuekey.go +++ b/priority_queuekey/priority_queuekey.go @@ -1,6 +1,9 @@ package pqueuekey -import "github.com/474420502/focus/compare" +import ( + "github.com/474420502/focus/compare" + "github.com/davecgh/go-spew/spew" +) type PriorityQueue struct { queue *vbTree @@ -78,3 +81,7 @@ func (pq *PriorityQueue) Remove(key interface{}) (interface{}, bool) { func (pq *PriorityQueue) Values() []interface{} { return pq.queue.Values() } + +func (pq *PriorityQueue) String() string { + return spew.Sprint(pq.queue.Values()) +} diff --git a/priority_queuekey/priority_queuekey_test.go b/priority_queuekey/priority_queuekey_test.go index f91d378..1776d7d 100644 --- a/priority_queuekey/priority_queuekey_test.go +++ b/priority_queuekey/priority_queuekey_test.go @@ -1,11 +1,11 @@ package pqueuekey import ( + "log" "testing" - "github.com/davecgh/go-spew/spew" - "github.com/474420502/focus/compare" + "github.com/davecgh/go-spew/spew" ) func TestQueuePush(t *testing.T) { @@ -345,3 +345,68 @@ func TestQueueIndex(t *testing.T) { // pq.Pop() // } // } + +func TestPriorityQueue_Iterator(t *testing.T) { + pq := New(compare.Int) + for i := 0; i < 5; i++ { + pq.Push(i, i) + } + + pq.Push(-1, -1) + pq.Push(10, 10) + + result := pq.String() + if result != "[10 4 3 2 1 0 -1]" { + t.Error("should be [10 4 3 2 1 0 -1]") + } + + iter := pq.Iterator() + iter.ToHead() + + values := pq.Values() + for i := 0; ; i++ { + if values[i] != iter.Value() { + t.Error(values[i], " != ", iter.Value()) + } + + if !iter.Next() { + break + } + } +} + +func TestMain(t *testing.T) { + pq := New(compare.Int) + pq.Push(1, 1) + pq.Push(4, 4) + pq.Push(5, 5) + pq.Push(6, 6) + pq.Push(2, 2) // pq.Values() = [6 5 4 2 1] + log.Println(pq.Values()) + value, _ := pq.Pop() // value = 6 + log.Println(value) + value, _ = pq.Get(1) // value = 1 pq.Values() = [5 4 2 1] + log.Println(value) + value, _ = pq.Get(0) // value = nil , Get equal to Seach Key + log.Println(value) + value, _ = pq.Index(0) // value = 5, compare.Int the order from big to small + log.Println(value) + values := pq.GetRange(2, 5) // values = [2 4 5] + log.Println(values) + values = pq.GetRange(5, 2) // values = [5 4 2] + log.Println(values) + values = pq.GetRange(100, 2) // values = [5 4 2] + log.Println(values) + values3 := pq.GetAround(5) // values3 = [, 5, 4] + log.Println(values3) + + iter := pq.Iterator() // Next 大到小 从root节点起始 + log.Println(pq.String()) + // log.Println(iter.Value()) 直接使用会报错, + iter.ToHead() + log.Println(iter.Value()) // 起始最大值. true 5 + log.Println(iter.Prev(), iter.Value()) // false 5 + + // Prev 大到小 + log.Println(iter.Next(), iter.Value()) // true 4 +} diff --git a/set/treeset/treeset_test.go b/set/treeset/treeset_test.go index 6e3671b..5c2254a 100644 --- a/set/treeset/treeset_test.go +++ b/set/treeset/treeset_test.go @@ -151,7 +151,29 @@ func TestTreeSet_Iterator(t *testing.T) { set.Add(5, 4, 3, 5) iter := set.Iterator() - for iter.Prev() { + iter.ToHead() + // 3 4 5 + if iter.Value() != 3 { + t.Error(iter.Value()) + } + + iter.Next() + if iter.Value() != 4 { + t.Error(iter.Value()) + } + + iter.Next() + if iter.Value() != 5 { + t.Error(iter.Value()) + } + + iter.ToTail() + if iter.Value() != 5 { + t.Error(iter.Value()) + } + + iter.Prev() + if iter.Value() != 4 { t.Error(iter.Value()) } } diff --git a/tree/avl/avl_test.go b/tree/avl/avl_test.go index 154d5ec..0192966 100644 --- a/tree/avl/avl_test.go +++ b/tree/avl/avl_test.go @@ -22,6 +22,43 @@ func loadTestData() []int { return l } +func TestIteratorHeadTail(t *testing.T) { + tree := New(compare.Int) + for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} { + tree.Put(v) + } + // ` AVLTree + // │ ┌── 30 + // │ │ └── 21 + // │ ┌── 20 + // │ │ └── 15 + // └── 14 + // │ ┌── 7 + // │ ┌── 7 + // │ │ └── 6 + // └── 5 + // │ ┌── 4 + // │ │ └── 3 + // └── 2 + // └── 1` + + iter := tree.Iterator() + iter.Prev() + if iter.Value() != 14 { + t.Error("iter.Value() != ", 14, " value =", iter.Value()) + } + + iter.ToHead() + if iter.Value() != 1 { + t.Error("iter.Value() != ", 14, " value =", iter.Value()) + } + + iter.ToTail() + if iter.Value() != 30 { + t.Error("iter.Value() != ", 30, " value =", iter.Value()) + } +} + func TestIterator(t *testing.T) { tree := New(compare.Int) for _, v := range []int{1, 2, 7, 4, 5, 6, 7, 14, 15, 20, 30, 21, 3} { diff --git a/tree/avl/iterator.go b/tree/avl/iterator.go index 6436a96..5884f1d 100644 --- a/tree/avl/iterator.go +++ b/tree/avl/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) diff --git a/tree/avldup/iterator.go b/tree/avldup/iterator.go index 1ef37ea..1ec0716 100644 --- a/tree/avldup/iterator.go +++ b/tree/avldup/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) diff --git a/tree/avlkey/iterator.go b/tree/avlkey/iterator.go index 1de0bc2..ac6306b 100644 --- a/tree/avlkey/iterator.go +++ b/tree/avlkey/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) diff --git a/tree/avlkeydup/iterator.go b/tree/avlkeydup/iterator.go index 4369589..6511ee2 100644 --- a/tree/avlkeydup/iterator.go +++ b/tree/avlkeydup/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) diff --git a/tree/vbt/iterator.go b/tree/vbt/iterator.go index 3a09be1..3afdf0a 100644 --- a/tree/vbt/iterator.go +++ b/tree/vbt/iterator.go @@ -30,6 +30,16 @@ func NewIteratorWithCap(n *Node, cap int) *Iterator { return iter } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) GetNode() *Node { return iter.cur } @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur) diff --git a/tree/vbtkey/iterator.go b/tree/vbtkey/iterator.go index 2d7acc4..1cd3d31 100644 --- a/tree/vbtkey/iterator.go +++ b/tree/vbtkey/iterator.go @@ -34,6 +34,16 @@ func (iter *Iterator) GetNode() *Node { return iter.cur } +func (iter *Iterator) ToHead() { + for iter.Prev() { + } +} + +func (iter *Iterator) ToTail() { + for iter.Next() { + } +} + func (iter *Iterator) SetNode(n *Node) { iter.up = n iter.dir = 0 @@ -44,24 +54,6 @@ func (iter *Iterator) Value() interface{} { return iter.cur.value } -func (iter *Iterator) Left() bool { - if iter.cur.children[0] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[0] - return true - } - return false -} - -func (iter *Iterator) Right() bool { - if iter.cur.children[1] != nil { - iter.dir = 0 - iter.cur = iter.cur.children[1] - return true - } - return false -} - func (iter *Iterator) GetNext(cur *Node, idx int) *Node { // iter := NewIterator(cur)