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()) } + }