FedP2P/ordered.go

48 lines
1.0 KiB
Go
Raw Normal View History

2014-07-09 22:26:58 +08:00
package torrent
import (
"container/list"
)
2015-03-08 14:28:14 +08:00
// This was used to maintain pieces in order of bytes left to download. I
// don't think it's currently in use.
type orderedList struct {
2014-07-09 22:26:58 +08:00
list *list.List
lessFunc func(a, b interface{}) bool
}
2015-03-08 14:28:14 +08:00
func (me *orderedList) Len() int {
2014-07-09 22:26:58 +08:00
return me.list.Len()
}
2015-03-08 14:28:14 +08:00
func newOrderedList(lessFunc func(a, b interface{}) bool) *orderedList {
return &orderedList{
2014-07-09 22:26:58 +08:00
list: list.New(),
lessFunc: lessFunc,
}
}
2015-03-08 14:28:14 +08:00
func (me *orderedList) ValueChanged(e *list.Element) {
2014-07-09 22:26:58 +08:00
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)
}
}
2015-03-08 14:28:14 +08:00
func (me *orderedList) Insert(value interface{}) (ret *list.Element) {
2014-07-09 22:26:58 +08:00
ret = me.list.PushFront(value)
me.ValueChanged(ret)
return
}
2015-03-08 14:28:14 +08:00
func (me *orderedList) Front() *list.Element {
2014-07-09 22:26:58 +08:00
return me.list.Front()
}
2015-03-08 14:28:14 +08:00
func (me *orderedList) Remove(e *list.Element) interface{} {
return me.list.Remove(e)
}