/** * Copyright 2023 ByteDance Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ast import ( `sort` `unsafe` ) type nodeChunk [_DEFAULT_NODE_CAP]Node type linkedNodes struct { head nodeChunk tail []*nodeChunk size int } func (self *linkedNodes) Cap() int { if self == nil { return 0 } return (len(self.tail)+1)*_DEFAULT_NODE_CAP } func (self *linkedNodes) Len() int { if self == nil { return 0 } return self.size } func (self *linkedNodes) At(i int) (*Node) { if self == nil { return nil } if i >= 0 && i= _DEFAULT_NODE_CAP && i= 0 { 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) } (*n)[b] = v self.size++ } func (self *linkedNodes) ToSlice(con []Node) { if len(con) < self.size { return } i := (self.size-1) a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP if a < 0 { copy(con, self.head[:b+1]) return } else { copy(con, self.head[:]) con = con[_DEFAULT_NODE_CAP:] } for i:=0; i>_APPEND_GROW_SHIFT self.tail = make([]*nodeChunk, a+1, c) } self.tail = self.tail[:a+1] for i:=0; i= 0 && i < _DEFAULT_NODE_CAP && i= _DEFAULT_NODE_CAP && i= 0 { 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) } (*n)[b] = v self.size++ } // linear search func (self *linkedPairs) Get(key string) (*Pair, int) { for i:=0; i>_APPEND_GROW_SHIFT self.tail = make([]*pairChunk, a+1, c) } self.tail = self.tail[:a+1] for i:=0; i len(b) { l = len(b) } for i := d; i < l; i++ { if a[i] == b[i] { continue } return a[i] < b[i] } return len(a) < len(b) } type parseObjectStack struct { parser Parser v linkedPairs } type parseArrayStack struct { parser Parser v linkedNodes } func newLazyArray(p *Parser) Node { s := new(parseArrayStack) s.parser = *p return Node{ t: _V_ARRAY_LAZY, p: unsafe.Pointer(s), } } func newLazyObject(p *Parser) Node { s := new(parseObjectStack) s.parser = *p return Node{ t: _V_OBJECT_LAZY, p: unsafe.Pointer(s), } } func (self *Node) getParserAndArrayStack() (*Parser, *parseArrayStack) { stack := (*parseArrayStack)(self.p) return &stack.parser, stack } func (self *Node) getParserAndObjectStack() (*Parser, *parseObjectStack) { stack := (*parseObjectStack)(self.p) return &stack.parser, stack }