diff --git a/priority_list/priority_list_test.go b/priority_list/priority_list_test.go index fac9d69..4ccad2c 100644 --- a/priority_list/priority_list_test.go +++ b/priority_list/priority_list_test.go @@ -1,6 +1,8 @@ package plist -import "testing" +import ( + "testing" +) func TestPriority(t *testing.T) { pl := New() diff --git a/priority_queue/priority_list.go b/priority_queue/priority_list.go new file mode 100644 index 0000000..09fbd4f --- /dev/null +++ b/priority_queue/priority_list.go @@ -0,0 +1,134 @@ +package plist + +import ( + "474420502.top/eson/structure" +) + +type NodeList struct { + head, tail *Node +} + +// Node 节点结构 +type Node struct { + prev, next *Node + structure.NValue +} + +// PriorityList 跳表 +type PriorityList struct { + size int + compare func(v1, v2 interface{}) int + level *LevelNode +} + +type LevelSize struct { + size int +} + +// LevelNode 层级列表 +type LevelNode struct { + head *LevelNode + tail *LevelNode + sub interface{} + lsize *LevelSize + level int +} + +// New a node +func New() *PriorityList { + p := new(PriorityList) + p.level = new(LevelNode) + p.level.lsize = new(LevelSize) + + node := new(Node) + p.level.sub = node + + return p +} + +// String 展示需要的 +func (pl *PriorityList) String() string { + + content := "" + + return content +} + +// InsertValues 插入值 +func (pl *PriorityList) InsertValues(values ...interface{}) { + + ll := pl.level + + for _, value := range values { + node := new(Node) + node.IValue = value + + if ll.level == 0 { + + if ll.sub == nil { + ll.sub = node + continue + } + + cur := ll.sub.(*Node) + cur. + } + + if level.tail == nil { + level.head.lsize + node.prev = level.head + level.tail = node + continue + } + + cur := level.head + for cur != nil { + + if pl.compare(cur, node) < 0 { + + if cur.sub != nil { + cur = cur.sub + continue + } else { + temp := cur.next + cur.next = node + if temp != nil { + node.next = temp + temp.prev = node + } + pl.size++ + break + } + + } else { + + } + + cur = cur.next + } + } +} + +// Get 获取索引长度 +// func (pl *PriorityList) Get(idx int) INode { + +// if idx >= pl.size { +// return nil +// } +// cur := pl.head +// for i := 0; i < idx; i++ { +// cur = cur.GetNext() +// } +// return cur +// } + +// Size 长度 +func (pl *PriorityList) Size() int { + return pl.size +} + +// Clear 清空链表, 如果外部有引用其中一个节点 要把节点Prev Next值为nil, 三色标记 +func (pl *PriorityList) Clear() { + pl.level = nil + pl.size = 0 +} diff --git a/priority_queue/priority_list_test.go b/priority_queue/priority_list_test.go new file mode 100644 index 0000000..1f9f483 --- /dev/null +++ b/priority_queue/priority_list_test.go @@ -0,0 +1,18 @@ +package plist + +import "testing" + +func IntCompare(v1, v2 interface{}) int { + if v1.(*Node).ValueInt() > v2.(*Node).ValueInt() { + return 1 + } + return -1 +} + +func TestPriorityList_InsertValues(t *testing.T) { + pl := New() + pl.compare = IntCompare + + pl.InsertValues(1, 2, 5, 6, 2, 4, 3) + t.Error(pl.String()) +} diff --git a/structure.go b/structure.go index d0d30e7..53b5b31 100644 --- a/structure.go +++ b/structure.go @@ -1,3 +1,36 @@ package structure +// NValue Node节点的必备的结构 +type NValue struct { + IValue interface{} +} +// ValueInt 返回 Int +func (node *NValue) ValueInt() int { + return node.IValue.(int) +} + +// Value 返回 Value interface +func (node *NValue) Value() interface{} { + return node.IValue +} + +// ValueInt8 返回 Int +func (node *NValue) ValueInt8() int8 { + return node.IValue.(int8) +} + +// ValueInt16 返回 Int +func (node *NValue) ValueInt16() int16 { + return node.IValue.(int16) +} + +// ValueInt32 返回 Int32 +func (node *NValue) ValueInt32() int32 { + return node.IValue.(int32) +} + +// ValueInt64 返回 Int64 +func (node *NValue) ValueInt64() int64 { + return node.IValue.(int64) +}