From b05cf14b986b49519d833eb789da704b66321f0b Mon Sep 17 00:00:00 2001 From: H1JK Date: Mon, 20 Nov 2023 23:48:07 +0800 Subject: [PATCH] chore: Replace stack collection with list --- common/collections/stack.go | 56 ------------------------------------- rules/logic/logic.go | 13 +++++---- 2 files changed, 7 insertions(+), 62 deletions(-) delete mode 100644 common/collections/stack.go diff --git a/common/collections/stack.go b/common/collections/stack.go deleted file mode 100644 index 74673f9a..00000000 --- a/common/collections/stack.go +++ /dev/null @@ -1,56 +0,0 @@ -package collections - -import "sync" - -type ( - stack struct { - top *node - length int - lock *sync.RWMutex - } - - node struct { - value interface{} - prev *node - } -) - -// NewStack Create a new stack -func NewStack() *stack { - return &stack{nil, 0, &sync.RWMutex{}} -} - -// Len Return the number of items in the stack -func (this *stack) Len() int { - return this.length -} - -// Peek View the top item on the stack -func (this *stack) Peek() interface{} { - if this.length == 0 { - return nil - } - return this.top.value -} - -// Pop the top item of the stack and return it -func (this *stack) Pop() interface{} { - this.lock.Lock() - defer this.lock.Unlock() - if this.length == 0 { - return nil - } - n := this.top - this.top = n.prev - this.length-- - return n.value -} - -// Push a value onto the top of the stack -func (this *stack) Push(value interface{}) { - this.lock.Lock() - defer this.lock.Unlock() - n := &node{value, this.top} - this.top = n - this.length++ -} diff --git a/rules/logic/logic.go b/rules/logic/logic.go index 4256a200..fde96e19 100644 --- a/rules/logic/logic.go +++ b/rules/logic/logic.go @@ -2,10 +2,10 @@ package logic import ( "fmt" + list "github.com/bahlo/generic-list-go" "regexp" "strings" - "github.com/metacubex/mihomo/common/collections" C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/rules/common" ) @@ -133,7 +133,7 @@ func (logic *Logic) payloadToRule(subPayload string, parseRule ParseRuleFunc) (C } func (logic *Logic) format(payload string) ([]Range, error) { - stack := collections.NewStack() + stack := list.New[Range]() num := 0 subRanges := make([]Range, 0) for i, c := range payload { @@ -144,15 +144,16 @@ func (logic *Logic) format(payload string) ([]Range, error) { } num++ - stack.Push(sr) + stack.PushBack(sr) } else if c == ')' { if stack.Len() == 0 { return nil, fmt.Errorf("missing '('") } - sr := stack.Pop().(Range) - sr.end = i - subRanges = append(subRanges, sr) + sr := stack.Back() + stack.Remove(sr) + sr.Value.end = i + subRanges = append(subRanges, sr.Value) } }