From c8a741ada7946ddec39e1b3982ea01ea59c6c3af Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Fri, 21 Dec 2018 02:47:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E6=AF=94=E8=BE=83?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=80=BC=E7=9A=84=E6=96=B9=E6=B3=95=20GetCom?= =?UTF-8?q?pare...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- priority_list/priority_list.go | 46 +++++++++++++++++++++++++++++ priority_list/priority_list_test.go | 16 +++++++--- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/priority_list/priority_list.go b/priority_list/priority_list.go index f42eea3..943af0f 100644 --- a/priority_list/priority_list.go +++ b/priority_list/priority_list.go @@ -88,6 +88,52 @@ func (pl *PriorityList) Clear() { pl.size = 0 } +// GetCompare 获取比较的节点 compare > 11这个节点小的值 [15,12,9,8,6,1] = 9 +func (pl *PriorityList) GetCompare(cNode INode) INode { + cur := pl.head + for cur != nil { + if !cur.Compare(cNode) { + return cur + } + cur = cur.GetNext() + } + return nil +} + +// GetCompareReverse 逆序获取比较的节点 compare > 11这个节点大的值 [15,12,9,8,6,1] = 12 +func (pl *PriorityList) GetCompareReverse(cNode INode) INode { + cur := pl.tail + for cur != nil { + if cur.Compare(cNode) { + return cur + } + cur = cur.GetPrev() + } + return nil +} + +// ForEach 顺序遍历 +func (pl *PriorityList) ForEach(eachfunc func(INode) bool) { + cur := pl.head + for cur != nil { + if !eachfunc(cur) { + break + } + cur = cur.GetNext() + } +} + +// ForEachReverse 逆遍历 +func (pl *PriorityList) ForEachReverse(eachfunc func(INode) bool) { + cur := pl.tail + for cur != nil { + if !eachfunc(cur) { + break + } + cur = cur.GetPrev() + } +} + // Get 获取索引长度 func (pl *PriorityList) Get(idx int) INode { if idx >= pl.size { diff --git a/priority_list/priority_list_test.go b/priority_list/priority_list_test.go index 5816675..7531651 100644 --- a/priority_list/priority_list_test.go +++ b/priority_list/priority_list_test.go @@ -53,25 +53,33 @@ func TestPriority(t *testing.T) { t.Error("Remove error current is ", pl.String(), pl.Size()) } - t.Log("size is", pl.Size()) size := pl.Size() for i := 0; i < size; i++ { pl.RemoveReverseIndex(0) - t.Log(i) } if pl.String() != "[]" || pl.Size() != 0 { t.Error("Remove error current is ", pl.String(), pl.Size()) } pl.Insert(NewNodeInt(6)) - pl.Insert(NewNodeInt(5)) - pl.Insert(NewNodeInt(7)) + pl.Insert(NewNodeInt(1)) + pl.Insert(NewNodeInt(9)) + pl.Insert(NewNodeInt(15)) pl.Insert(NewNodeInt(12)) pl.Insert(NewNodeInt(8)) + if pl.GetCompare(NewNodeInt(11)).GetValue() != 9 { + t.Error(pl.String(), pl.GetCompare(NewNodeInt(11)).GetValue()) + } + + if pl.GetCompareReverse(NewNodeInt(11)).GetValue() != 12 { + t.Error(pl.String(), pl.GetCompareReverse(NewNodeInt(11)).GetValue()) + } + pl.Clear() if pl.String() != "[]" || pl.Size() != 0 { t.Error("Remove error current is ", pl.String(), pl.Size()) } + }