You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fx/node.go

166 lines
2.5 KiB
Go

10 months ago
package main
10 months ago
import (
"strconv"
)
10 months ago
type node struct {
prev, next, end *node
10 months ago
directParent *node
indirectParent *node
collapsed *node
10 months ago
depth uint8
key []byte
value []byte
10 months ago
chunk []byte
chunkEnd *node
10 months ago
comma bool
10 months ago
index int
10 months ago
}
10 months ago
func (n *node) append(child *node) {
if n.end == nil {
n.end = n
}
n.end.next = child
child.prev = n.end
if child.end == nil {
n.end = child
} else {
n.end = child.end
}
}
func (n *node) insertChunk(chunk *node) {
if n.chunkEnd == nil {
n.insertAfter(chunk)
10 months ago
} else {
n.chunkEnd.insertAfter(chunk)
10 months ago
}
n.chunkEnd = chunk
10 months ago
}
func (n *node) insertAfter(child *node) {
if n.next == nil {
n.next = child
child.prev = n
} else {
old := n.next
n.next = child
child.prev = n
child.next = old
old.prev = child
}
}
func (n *node) dropChunks() {
if n.chunkEnd == nil {
10 months ago
return
}
n.chunk = nil
n.next = n.chunkEnd.next
10 months ago
if n.next != nil {
n.next.prev = n
}
n.chunkEnd = nil
10 months ago
}
10 months ago
func (n *node) hasChildren() bool {
return n.end != nil
}
10 months ago
func (n *node) parent() *node {
if n.directParent == nil {
return nil
}
parent := n.directParent
if parent.indirectParent != nil {
parent = parent.indirectParent
}
return parent
}
10 months ago
func (n *node) isCollapsed() bool {
return n.collapsed != nil
10 months ago
}
10 months ago
10 months ago
func (n *node) collapse() *node {
10 months ago
if n.end != nil && !n.isCollapsed() {
10 months ago
n.collapsed = n.next
10 months ago
n.next = n.end.next
if n.next != nil {
n.next.prev = n
}
10 months ago
}
10 months ago
return n
10 months ago
}
10 months ago
func (n *node) collapseRecursively() {
var at *node
if n.isCollapsed() {
at = n.collapsed
} else {
at = n.next
}
for at != nil && at != n.end {
if at.hasChildren() {
at.collapseRecursively()
at.collapse()
}
at = at.next
}
}
10 months ago
func (n *node) expand() {
10 months ago
if n.isCollapsed() {
if n.next != nil {
n.next.prev = n.end
}
10 months ago
n.next = n.collapsed
n.collapsed = nil
10 months ago
}
}
10 months ago
func (n *node) expandRecursively() {
at := n
for at != nil && at != n.end {
at.expand()
at = at.next
}
}
10 months ago
func (n *node) findChildByKey(key string) *node {
for at := n.next; at != nil && at != n.end; {
k, err := strconv.Unquote(string(at.key))
if err != nil {
return nil
}
if k == key {
return at
}
if at.end != nil {
at = at.end.next
} else {
at = at.next
}
}
return nil
}
func (n *node) findChildByIndex(index int) *node {
for at := n.next; at != nil && at != n.end; {
if at.index == index {
return at
}
if at.end != nil {
at = at.end.next
} else {
at = at.next
}
}
return nil
}