2023-09-07 20:53:51 +00:00
|
|
|
package main
|
|
|
|
|
2023-09-12 11:06:48 +00:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2023-09-07 20:53:51 +00:00
|
|
|
type node struct {
|
|
|
|
prev, next, end *node
|
2023-09-08 10:05:44 +00:00
|
|
|
directParent *node
|
|
|
|
indirectParent *node
|
|
|
|
collapsed *node
|
2023-09-07 20:53:51 +00:00
|
|
|
depth uint8
|
|
|
|
key []byte
|
|
|
|
value []byte
|
2023-09-11 11:16:10 +00:00
|
|
|
chunk []byte
|
2023-09-07 20:53:51 +00:00
|
|
|
comma bool
|
2023-09-11 16:27:54 +00:00
|
|
|
index int
|
2023-09-07 20:53:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 14:12:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 11:16:10 +00:00
|
|
|
func (n *node) insertChild(child *node) {
|
|
|
|
if n.end == nil {
|
|
|
|
n.insertAfter(child)
|
|
|
|
} else {
|
|
|
|
n.end.insertAfter(child)
|
|
|
|
}
|
|
|
|
n.end = child
|
|
|
|
}
|
|
|
|
|
|
|
|
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.end == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n.chunk = nil
|
|
|
|
|
|
|
|
n.next = n.end.next
|
|
|
|
if n.next != nil {
|
|
|
|
n.next.prev = n
|
|
|
|
}
|
|
|
|
|
|
|
|
n.end = nil
|
|
|
|
}
|
|
|
|
|
2023-09-10 14:03:51 +00:00
|
|
|
func (n *node) hasChildren() bool {
|
|
|
|
return n.end != nil
|
|
|
|
}
|
|
|
|
|
2023-09-08 10:05:44 +00:00
|
|
|
func (n *node) parent() *node {
|
|
|
|
if n.directParent == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
parent := n.directParent
|
|
|
|
if parent.indirectParent != nil {
|
|
|
|
parent = parent.indirectParent
|
|
|
|
}
|
|
|
|
return parent
|
|
|
|
}
|
|
|
|
|
2023-09-10 14:12:38 +00:00
|
|
|
func (n *node) isCollapsed() bool {
|
|
|
|
return n.collapsed != nil
|
2023-09-07 20:53:51 +00:00
|
|
|
}
|
2023-09-08 09:14:48 +00:00
|
|
|
|
2023-09-08 22:14:46 +00:00
|
|
|
func (n *node) collapse() *node {
|
2023-09-10 14:11:42 +00:00
|
|
|
if n.end != nil && !n.isCollapsed() {
|
2023-09-08 10:05:44 +00:00
|
|
|
n.collapsed = n.next
|
2023-09-08 09:14:48 +00:00
|
|
|
n.next = n.end.next
|
|
|
|
if n.next != nil {
|
|
|
|
n.next.prev = n
|
|
|
|
}
|
2023-09-08 10:05:44 +00:00
|
|
|
}
|
2023-09-08 22:14:46 +00:00
|
|
|
return n
|
2023-09-08 10:05:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 15:50:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 10:05:44 +00:00
|
|
|
func (n *node) expand() {
|
2023-09-10 13:57:12 +00:00
|
|
|
if n.isCollapsed() {
|
|
|
|
if n.next != nil {
|
|
|
|
n.next.prev = n.end
|
|
|
|
}
|
2023-09-08 10:05:44 +00:00
|
|
|
n.next = n.collapsed
|
|
|
|
n.collapsed = nil
|
2023-09-08 09:14:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-11 15:50:38 +00:00
|
|
|
|
|
|
|
func (n *node) expandRecursively() {
|
|
|
|
at := n
|
|
|
|
for at != nil && at != n.end {
|
|
|
|
at.expand()
|
|
|
|
at = at.next
|
|
|
|
}
|
|
|
|
}
|
2023-09-12 11:06:48 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|