Skip to content

Commit

Permalink
fix:(ast) implement soft delete for Unset() (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jan 2, 2024
1 parent ec2ab23 commit f806331
Show file tree
Hide file tree
Showing 8 changed files with 740 additions and 174 deletions.
148 changes: 114 additions & 34 deletions ast/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,89 @@ func (self *linkedNodes) At(i int) (*Node) {
return nil
}

func (self *linkedNodes) Add(v Node) {
if self.size < _DEFAULT_NODE_CAP {
self.head[self.size] = v
self.size++
func (self *linkedNodes) MoveOne(source int, target int) {
if source == target {
return
}
if source < 0 || source >= self.size || target < 0 || target >= self.size {
return
}
// reserve source
n := *self.At(source)
if source < target {
// move every element (source,target] one step back
for i:=source; i<target; i++ {
*self.At(i) = *self.At(i+1)
}
} else {
// move every element [target,source) one step forward
for i:=source; i>target; i-- {
*self.At(i) = *self.At(i-1)
}
}
// set target
*self.At(target) = n
}

func (self *linkedNodes) Pop() {
if self == nil || self.size == 0 {
return
}
self.Set(self.size-1, Node{})
self.size--
}

func (self *linkedPairs) Pop() {
if self == nil || self.size == 0 {
return
}
self.Set(self.size-1, Pair{})
self.size--
}

func (self *linkedNodes) Push(v Node) {
self.Set(self.size, v)
}

a, b, c := self.size/_DEFAULT_NODE_CAP-1 , self.size%_DEFAULT_NODE_CAP, cap(self.tail)
if a - c >= 0 {
func (self *linkedNodes) Set(i int, v Node) {
if i < _DEFAULT_NODE_CAP {
self.head[i] = v
if self.size <= i {
self.size = i+1
}
return
}
a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
if a < 0 {
self.head[b] = v
} else {
self.growTailLength(a+1)
var n = &self.tail[a]
if *n == nil {
*n = new(nodeChunk)
}
(*n)[b] = v
}
if self.size <= i {
self.size = i+1
}
}

func (self *linkedNodes) growTailLength(l int) {
if l <= len(self.tail) {
return
}
c := cap(self.tail)
for c < l {
c += 1 + c>>_APPEND_GROW_SHIFT
tmp := make([]*nodeChunk, a + 1, c)
copy(tmp, self.tail)
self.tail = tmp
} else if a >= len(self.tail) {
self.tail = self.tail[:a+1]
}

var n = &self.tail[a]
if *n == nil {
*n = new(nodeChunk)
if c == cap(self.tail) {
self.tail = self.tail[:l]
return
}
(*n)[b] = v
self.size++
tmp := make([]*nodeChunk, l, c)
copy(tmp, self.tail)
self.tail = tmp
}

func (self *linkedNodes) ToSlice(con []Node) {
Expand Down Expand Up @@ -169,29 +229,49 @@ func (self *linkedPairs) At(i int) *Pair {
return nil
}

func (self *linkedPairs) Add(v Pair) {
if self.size < _DEFAULT_NODE_CAP {
self.head[self.size] = v
self.size++
func (self *linkedPairs) Push(v Pair) {
self.Set(self.size, v)
}

func (self *linkedPairs) Set(i int, v Pair) {
if i < _DEFAULT_NODE_CAP {
self.head[i] = v
if self.size <= i {
self.size = i+1
}
return
}
a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
if a < 0 {
self.head[b] = v
} else {
self.growTailLength(a+1)
var n = &self.tail[a]
if *n == nil {
*n = new(pairChunk)
}
(*n)[b] = v
}
if self.size <= i {
self.size = i+1
}
}

a, b, c := self.size/_DEFAULT_NODE_CAP-1 , self.size%_DEFAULT_NODE_CAP, cap(self.tail)
if a - c >= 0 {
func (self *linkedPairs) growTailLength(l int) {
if l <= len(self.tail) {
return
}
c := cap(self.tail)
for c < l {
c += 1 + c>>_APPEND_GROW_SHIFT
tmp := make([]*pairChunk, a + 1, c)
copy(tmp, self.tail)
self.tail = tmp
} else if a >= len(self.tail) {
self.tail = self.tail[:a+1]
}

var n = &self.tail[a]
if *n == nil {
*n = new(pairChunk)
if c == cap(self.tail) {
self.tail = self.tail[:l]
return
}
(*n)[b] = v
self.size++
tmp := make([]*pairChunk, l, c)
copy(tmp, self.tail)
self.tail = tmp
}

// linear search
Expand Down

0 comments on commit f806331

Please sign in to comment.