FedP2P/ordered.go

45 lines
937 B
Go
Raw Normal View History

2014-07-09 22:26:58 +08:00
package torrent
import (
"container/list"
)
type OrderedList struct {
list *list.List
lessFunc func(a, b interface{}) bool
}
func (me *OrderedList) Len() int {
return me.list.Len()
}
func NewList(lessFunc func(a, b interface{}) bool) *OrderedList {
return &OrderedList{
list: list.New(),
lessFunc: lessFunc,
}
}
func (me *OrderedList) ValueChanged(e *list.Element) {
for prev := e.Prev(); prev != nil && me.lessFunc(e.Value, prev.Value); prev = e.Prev() {
me.list.MoveBefore(e, prev)
}
for next := e.Next(); next != nil && me.lessFunc(next.Value, e.Value); next = e.Next() {
me.list.MoveAfter(e, next)
}
}
func (me *OrderedList) Insert(value interface{}) (ret *list.Element) {
ret = me.list.PushFront(value)
me.ValueChanged(ret)
return
}
func (me *OrderedList) Front() *list.Element {
return me.list.Front()
}
func (me *OrderedList) Remove(e *list.Element) interface{} {
return me.list.Remove(e)
}