diff --git a/service/infra/dataStructure/linklist/linklist.go b/service/infra/dataStructure/linklist/linklist.go deleted file mode 100644 index c949588d..00000000 --- a/service/infra/dataStructure/linklist/linklist.go +++ /dev/null @@ -1,113 +0,0 @@ -package linklist - -// linklist with head node and tail node -type Node struct { - prior *Node - next *Node - Val interface{} -} - -func (p *Node) Next() *Node { - return p.next -} - -func (p *Node) Prior() *Node { - return p.prior -} - -type Linklist struct { - head *Node - tail *Node -} - -func NewLinklist() *Linklist { - head := new(Node) - tail := new(Node) - head.next = tail - tail.prior = head - return &Linklist{ - head: head, - tail: tail, - } -} - -func (l *Linklist) Front() *Node { - node := l.head.next - if node == l.tail { - return nil - } - return node -} - -func (l *Linklist) Back() *Node { - node := l.tail.prior - if node == l.head { - return nil - } - return node -} - -func (l *Linklist) Head() *Node { - return l.head -} - -func (l *Linklist) Tail() *Node { - return l.tail -} - -func (l *Linklist) Empty() bool { - return l.head.next == l.tail -} - -func (l *Linklist) InsertAfter(prior *Node, val interface{}) *Node { - if prior == l.tail { - return nil - } - p := new(Node) - p.Val = val - p.prior = prior - p.next = prior.next - prior.next.prior = p - prior.next = p - return p -} - -func (l *Linklist) PushFront(val interface{}) *Node { - return l.InsertAfter(l.head, val) -} - -func (l *Linklist) PushBack(val interface{}) *Node { - return l.InsertAfter(l.tail.prior, val) -} - -func (l *Linklist) Promote(p *Node) { - if p == l.Front() { - return - } - p.prior.next = p.next - p.next.prior = p.prior - p.prior = l.head - p.next = l.head.next - l.head.next.prior = p - l.head.next = p -} - -func (l *Linklist) Demote(p *Node) { - if p == l.Back() { - return - } - p.prior.next = p.next - p.next.prior = p.prior - p.prior = l.tail.prior - p.next = l.tail - l.tail.prior.next = p - l.tail.prior = p -} - -func (l *Linklist) Remove(p *Node) { - if p == l.head || p == l.tail { - return - } - p.prior.next = p.next - p.next.prior = p.prior -} diff --git a/service/infra/dataStructure/lru/lru.go b/service/infra/dataStructure/lru/lru.go index c47ebb40..83bc4ff2 100644 --- a/service/infra/dataStructure/lru/lru.go +++ b/service/infra/dataStructure/lru/lru.go @@ -1,7 +1,7 @@ package lru import ( - "github.com/v2rayA/v2rayA/infra/dataStructure/linklist" + "container/list" "sync" "time" ) @@ -14,9 +14,9 @@ const ( ) type LRU struct { - list *linklist.Linklist - index map[interface{}]*linklist.Node - reverseIndex map[*linklist.Node]interface{} + list *list.List + index map[interface{}]*list.Element + reverseIndex map[*list.Element]interface{} mutex sync.Mutex strategy LimitStrategy limit int64 @@ -29,9 +29,9 @@ type EncapsulatedValue struct { func New(strategy LimitStrategy, limit int64) *LRU { return &LRU{ - index: make(map[interface{}]*linklist.Node), - reverseIndex: make(map[*linklist.Node]interface{}), - list: linklist.NewLinklist(), + index: make(map[interface{}]*list.Element), + reverseIndex: make(map[*list.Element]interface{}), + list: list.New(), strategy: strategy, limit: limit, } @@ -59,8 +59,8 @@ func (l *LRU) get(key interface{}) (value interface{}) { if !ok { return nil } - l.list.Promote(v) - ev := v.Val.(*EncapsulatedValue) + l.list.PushFront(v) + ev := v.Value.(*EncapsulatedValue) ev.LastUseTime = time.Now() return ev.Value } @@ -83,7 +83,7 @@ func (l *LRU) insert(key interface{}, val interface{}) (removed []*EncapsulatedV case FixedLength: if int64(len(l.index)) > l.limit { back := l.list.Back() - removed = []*EncapsulatedValue{back.Val.(*EncapsulatedValue)} + removed = []*EncapsulatedValue{back.Value.(*EncapsulatedValue)} key := l.reverseIndex[back] l.list.Remove(back) delete(l.index, key) @@ -97,7 +97,7 @@ func (l *LRU) insert(key interface{}, val interface{}) (removed []*EncapsulatedV if back == nil { break } - ev := back.Val.(*EncapsulatedValue) + ev := back.Value.(*EncapsulatedValue) if int64(now.Sub(ev.LastUseTime)) < l.limit { break }