buffer.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * Copyright 2023 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package ast
  17. import (
  18. `sort`
  19. `unsafe`
  20. )
  21. type nodeChunk [_DEFAULT_NODE_CAP]Node
  22. type linkedNodes struct {
  23. head nodeChunk
  24. tail []*nodeChunk
  25. size int
  26. }
  27. func (self *linkedNodes) Cap() int {
  28. if self == nil {
  29. return 0
  30. }
  31. return (len(self.tail)+1)*_DEFAULT_NODE_CAP
  32. }
  33. func (self *linkedNodes) Len() int {
  34. if self == nil {
  35. return 0
  36. }
  37. return self.size
  38. }
  39. func (self *linkedNodes) At(i int) (*Node) {
  40. if self == nil {
  41. return nil
  42. }
  43. if i >= 0 && i<self.size && i < _DEFAULT_NODE_CAP {
  44. return &self.head[i]
  45. } else if i >= _DEFAULT_NODE_CAP && i<self.size {
  46. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  47. if a < len(self.tail) {
  48. return &self.tail[a][b]
  49. }
  50. }
  51. return nil
  52. }
  53. func (self *linkedNodes) Add(v Node) {
  54. if self.size < _DEFAULT_NODE_CAP {
  55. self.head[self.size] = v
  56. self.size++
  57. return
  58. }
  59. a, b, c := self.size/_DEFAULT_NODE_CAP-1 , self.size%_DEFAULT_NODE_CAP, cap(self.tail)
  60. if a - c >= 0 {
  61. c += 1 + c>>_APPEND_GROW_SHIFT
  62. tmp := make([]*nodeChunk, a + 1, c)
  63. copy(tmp, self.tail)
  64. self.tail = tmp
  65. } else if a >= len(self.tail) {
  66. self.tail = self.tail[:a+1]
  67. }
  68. var n = &self.tail[a]
  69. if *n == nil {
  70. *n = new(nodeChunk)
  71. }
  72. (*n)[b] = v
  73. self.size++
  74. }
  75. func (self *linkedNodes) ToSlice(con []Node) {
  76. if len(con) < self.size {
  77. return
  78. }
  79. i := (self.size-1)
  80. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  81. if a < 0 {
  82. copy(con, self.head[:b+1])
  83. return
  84. } else {
  85. copy(con, self.head[:])
  86. con = con[_DEFAULT_NODE_CAP:]
  87. }
  88. for i:=0; i<a; i++ {
  89. copy(con, self.tail[i][:])
  90. con = con[_DEFAULT_NODE_CAP:]
  91. }
  92. copy(con, self.tail[a][:b+1])
  93. }
  94. func (self *linkedNodes) FromSlice(con []Node) {
  95. self.size = len(con)
  96. i := self.size-1
  97. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  98. if a < 0 {
  99. copy(self.head[:b+1], con)
  100. return
  101. } else {
  102. copy(self.head[:], con)
  103. con = con[_DEFAULT_NODE_CAP:]
  104. }
  105. if cap(self.tail) <= a {
  106. c := (a+1) + (a+1)>>_APPEND_GROW_SHIFT
  107. self.tail = make([]*nodeChunk, a+1, c)
  108. }
  109. self.tail = self.tail[:a+1]
  110. for i:=0; i<a; i++ {
  111. self.tail[i] = new(nodeChunk)
  112. copy(self.tail[i][:], con)
  113. con = con[_DEFAULT_NODE_CAP:]
  114. }
  115. self.tail[a] = new(nodeChunk)
  116. copy(self.tail[a][:b+1], con)
  117. }
  118. type pairChunk [_DEFAULT_NODE_CAP]Pair
  119. type linkedPairs struct {
  120. head pairChunk
  121. tail []*pairChunk
  122. size int
  123. }
  124. func (self *linkedPairs) Cap() int {
  125. if self == nil {
  126. return 0
  127. }
  128. return (len(self.tail)+1)*_DEFAULT_NODE_CAP
  129. }
  130. func (self *linkedPairs) Len() int {
  131. if self == nil {
  132. return 0
  133. }
  134. return self.size
  135. }
  136. func (self *linkedPairs) At(i int) *Pair {
  137. if self == nil {
  138. return nil
  139. }
  140. if i >= 0 && i < _DEFAULT_NODE_CAP && i<self.size {
  141. return &self.head[i]
  142. } else if i >= _DEFAULT_NODE_CAP && i<self.size {
  143. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  144. if a < len(self.tail) {
  145. return &self.tail[a][b]
  146. }
  147. }
  148. return nil
  149. }
  150. func (self *linkedPairs) Add(v Pair) {
  151. if self.size < _DEFAULT_NODE_CAP {
  152. self.head[self.size] = v
  153. self.size++
  154. return
  155. }
  156. a, b, c := self.size/_DEFAULT_NODE_CAP-1 , self.size%_DEFAULT_NODE_CAP, cap(self.tail)
  157. if a - c >= 0 {
  158. c += 1 + c>>_APPEND_GROW_SHIFT
  159. tmp := make([]*pairChunk, a + 1, c)
  160. copy(tmp, self.tail)
  161. self.tail = tmp
  162. } else if a >= len(self.tail) {
  163. self.tail = self.tail[:a+1]
  164. }
  165. var n = &self.tail[a]
  166. if *n == nil {
  167. *n = new(pairChunk)
  168. }
  169. (*n)[b] = v
  170. self.size++
  171. }
  172. // linear search
  173. func (self *linkedPairs) Get(key string) (*Pair, int) {
  174. for i:=0; i<self.size; i++ {
  175. if n := self.At(i); n.Key == key {
  176. return n, i
  177. }
  178. }
  179. return nil, -1
  180. }
  181. func (self *linkedPairs) ToSlice(con []Pair) {
  182. if len(con) < self.size {
  183. return
  184. }
  185. i := self.size-1
  186. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  187. if a < 0 {
  188. copy(con, self.head[:b+1])
  189. return
  190. } else {
  191. copy(con, self.head[:])
  192. con = con[_DEFAULT_NODE_CAP:]
  193. }
  194. for i:=0; i<a; i++ {
  195. copy(con, self.tail[i][:])
  196. con = con[_DEFAULT_NODE_CAP:]
  197. }
  198. copy(con, self.tail[a][:b+1])
  199. }
  200. func (self *linkedPairs) ToMap(con map[string]Node) {
  201. for i:=0; i<self.size; i++ {
  202. n := self.At(i)
  203. con[n.Key] = n.Value
  204. }
  205. }
  206. func (self *linkedPairs) FromSlice(con []Pair) {
  207. self.size = len(con)
  208. i := self.size-1
  209. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  210. if a < 0 {
  211. copy(self.head[:b+1], con)
  212. return
  213. } else {
  214. copy(self.head[:], con)
  215. con = con[_DEFAULT_NODE_CAP:]
  216. }
  217. if cap(self.tail) <= a {
  218. c := (a+1) + (a+1)>>_APPEND_GROW_SHIFT
  219. self.tail = make([]*pairChunk, a+1, c)
  220. }
  221. self.tail = self.tail[:a+1]
  222. for i:=0; i<a; i++ {
  223. self.tail[i] = new(pairChunk)
  224. copy(self.tail[i][:], con)
  225. con = con[_DEFAULT_NODE_CAP:]
  226. }
  227. self.tail[a] = new(pairChunk)
  228. copy(self.tail[a][:b+1], con)
  229. }
  230. func (self *linkedPairs) Less(i, j int) bool {
  231. return lessFrom(self.At(i).Key, self.At(j).Key, 0)
  232. }
  233. func (self *linkedPairs) Swap(i, j int) {
  234. a, b := self.At(i), self.At(j)
  235. *a, *b = *b, *a
  236. }
  237. func (self *linkedPairs) Sort() {
  238. sort.Sort(self)
  239. }
  240. // Compare two strings from the pos d.
  241. func lessFrom(a, b string, d int) bool {
  242. l := len(a)
  243. if l > len(b) {
  244. l = len(b)
  245. }
  246. for i := d; i < l; i++ {
  247. if a[i] == b[i] {
  248. continue
  249. }
  250. return a[i] < b[i]
  251. }
  252. return len(a) < len(b)
  253. }
  254. type parseObjectStack struct {
  255. parser Parser
  256. v linkedPairs
  257. }
  258. type parseArrayStack struct {
  259. parser Parser
  260. v linkedNodes
  261. }
  262. func newLazyArray(p *Parser) Node {
  263. s := new(parseArrayStack)
  264. s.parser = *p
  265. return Node{
  266. t: _V_ARRAY_LAZY,
  267. p: unsafe.Pointer(s),
  268. }
  269. }
  270. func newLazyObject(p *Parser) Node {
  271. s := new(parseObjectStack)
  272. s.parser = *p
  273. return Node{
  274. t: _V_OBJECT_LAZY,
  275. p: unsafe.Pointer(s),
  276. }
  277. }
  278. func (self *Node) getParserAndArrayStack() (*Parser, *parseArrayStack) {
  279. stack := (*parseArrayStack)(self.p)
  280. return &stack.parser, stack
  281. }
  282. func (self *Node) getParserAndObjectStack() (*Parser, *parseObjectStack) {
  283. stack := (*parseObjectStack)(self.p)
  284. return &stack.parser, stack
  285. }