mirror of
https://github.com/johnkerl/miller.git
synced 2026-01-23 02:14:13 +00:00
Switch to generics (#1763)
* gradually replace list.List with slices * gradually replace list.List with slices * more * more * more
This commit is contained in:
parent
7d51030b88
commit
9963df4090
5 changed files with 57 additions and 70 deletions
|
|
@ -26,7 +26,6 @@
|
|||
package runtime
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
|
||||
"github.com/johnkerl/miller/v6/pkg/lib"
|
||||
|
|
@ -68,7 +67,7 @@ func (sv *StackVariable) GetName() string {
|
|||
|
||||
type Stack struct {
|
||||
// list of *StackFrameSet
|
||||
stackFrameSets *list.List
|
||||
stackFrameSets []*StackFrameSet
|
||||
|
||||
// Invariant: equal to the head of the stackFrameSets list. This is cached
|
||||
// since all sets/gets in between frameset-push and frameset-pop will all
|
||||
|
|
@ -77,9 +76,9 @@ type Stack struct {
|
|||
}
|
||||
|
||||
func NewStack() *Stack {
|
||||
stackFrameSets := list.New()
|
||||
stackFrameSets := make([]*StackFrameSet, 1)
|
||||
head := newStackFrameSet()
|
||||
stackFrameSets.PushFront(head)
|
||||
stackFrameSets[0] = head
|
||||
return &Stack{
|
||||
stackFrameSets: stackFrameSets,
|
||||
head: head,
|
||||
|
|
@ -89,13 +88,13 @@ func NewStack() *Stack {
|
|||
// For when a user-defined function/subroutine is being entered
|
||||
func (stack *Stack) PushStackFrameSet() {
|
||||
stack.head = newStackFrameSet()
|
||||
stack.stackFrameSets.PushFront(stack.head)
|
||||
stack.stackFrameSets = append([]*StackFrameSet{stack.head}, stack.stackFrameSets...)
|
||||
}
|
||||
|
||||
// For when a user-defined function/subroutine is being exited
|
||||
func (stack *Stack) PopStackFrameSet() {
|
||||
stack.stackFrameSets.Remove(stack.stackFrameSets.Front())
|
||||
stack.head = stack.stackFrameSets.Front().Value.(*StackFrameSet)
|
||||
stack.stackFrameSets = stack.stackFrameSets[1:]
|
||||
stack.head = stack.stackFrameSets[0]
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
|
|
@ -180,9 +179,8 @@ func (stack *Stack) UnsetIndexed(
|
|||
}
|
||||
|
||||
func (stack *Stack) Dump() {
|
||||
fmt.Printf("STACK FRAMESETS (count %d):\n", stack.stackFrameSets.Len())
|
||||
for entry := stack.stackFrameSets.Front(); entry != nil; entry = entry.Next() {
|
||||
stackFrameSet := entry.Value.(*StackFrameSet)
|
||||
fmt.Printf("STACK FRAMESETS (count %d):\n", len(stack.stackFrameSets))
|
||||
for _, stackFrameSet := range stack.stackFrameSets {
|
||||
stackFrameSet.dump()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ type State struct {
|
|||
// This is necessary for the stateful semantics of `=~` and "\1", "\2", etc.
|
||||
// Those are avoided when the user calls `matchx`, which is newer, and
|
||||
// stateless. However, `=~` exists in the Miller DSL and we must support it.
|
||||
regexCapturesByFrame *list.List // list of []string
|
||||
regexCapturesByFrame [][]string
|
||||
|
||||
Options *cli.TOptions
|
||||
|
||||
|
|
@ -46,8 +46,8 @@ type State struct {
|
|||
func NewEmptyState(options *cli.TOptions, strictMode bool) *State {
|
||||
|
||||
// See lib.MakeEmptyCaptures for context.
|
||||
regexCapturesByFrame := list.New()
|
||||
regexCapturesByFrame.PushFront(lib.MakeEmptyCaptures())
|
||||
regexCapturesByFrame := make([][]string, 1)
|
||||
regexCapturesByFrame[0] = lib.MakeEmptyCaptures()
|
||||
|
||||
oosvars := mlrval.NewMlrmap()
|
||||
return &State{
|
||||
|
|
@ -72,25 +72,24 @@ func (state *State) Update(
|
|||
) {
|
||||
state.Inrec = inrec
|
||||
state.Context = context
|
||||
state.regexCapturesByFrame.Front().Value = lib.MakeEmptyCaptures()
|
||||
state.regexCapturesByFrame[0] = lib.MakeEmptyCaptures()
|
||||
}
|
||||
|
||||
func (state *State) SetRegexCaptures(
|
||||
captures []string,
|
||||
) {
|
||||
state.regexCapturesByFrame.Front().Value = lib.CopyStringArray(captures)
|
||||
state.regexCapturesByFrame[0] = lib.CopyStringArray(captures)
|
||||
}
|
||||
|
||||
func (state *State) GetRegexCaptures() []string {
|
||||
regexCaptures := state.regexCapturesByFrame.Front().Value.([]string)
|
||||
regexCaptures := state.regexCapturesByFrame[0]
|
||||
return lib.CopyStringArray(regexCaptures)
|
||||
}
|
||||
|
||||
func (state *State) PushRegexCapturesFrame() {
|
||||
state.regexCapturesByFrame.PushFront(lib.MakeEmptyCaptures())
|
||||
state.regexCapturesByFrame = append([][]string{lib.MakeEmptyCaptures()}, state.regexCapturesByFrame...)
|
||||
}
|
||||
|
||||
func (state *State) PopRegexCapturesFrame() {
|
||||
// There is no PopFront
|
||||
state.regexCapturesByFrame.Remove(state.regexCapturesByFrame.Front())
|
||||
state.regexCapturesByFrame = state.regexCapturesByFrame[1:]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue