buffer.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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) MoveOne(source int, target int) {
  54. if source == target {
  55. return
  56. }
  57. if source < 0 || source >= self.size || target < 0 || target >= self.size {
  58. return
  59. }
  60. // reserve source
  61. n := *self.At(source)
  62. if source < target {
  63. // move every element (source,target] one step back
  64. for i:=source; i<target; i++ {
  65. *self.At(i) = *self.At(i+1)
  66. }
  67. } else {
  68. // move every element [target,source) one step forward
  69. for i:=source; i>target; i-- {
  70. *self.At(i) = *self.At(i-1)
  71. }
  72. }
  73. // set target
  74. *self.At(target) = n
  75. }
  76. func (self *linkedNodes) Pop() {
  77. if self == nil || self.size == 0 {
  78. return
  79. }
  80. self.Set(self.size-1, Node{})
  81. self.size--
  82. }
  83. func (self *linkedPairs) Pop() {
  84. if self == nil || self.size == 0 {
  85. return
  86. }
  87. self.Set(self.size-1, Pair{})
  88. self.size--
  89. }
  90. func (self *linkedNodes) Push(v Node) {
  91. self.Set(self.size, v)
  92. }
  93. func (self *linkedNodes) Set(i int, v Node) {
  94. if i < _DEFAULT_NODE_CAP {
  95. self.head[i] = v
  96. if self.size <= i {
  97. self.size = i+1
  98. }
  99. return
  100. }
  101. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  102. if a < 0 {
  103. self.head[b] = v
  104. } else {
  105. self.growTailLength(a+1)
  106. var n = &self.tail[a]
  107. if *n == nil {
  108. *n = new(nodeChunk)
  109. }
  110. (*n)[b] = v
  111. }
  112. if self.size <= i {
  113. self.size = i+1
  114. }
  115. }
  116. func (self *linkedNodes) growTailLength(l int) {
  117. if l <= len(self.tail) {
  118. return
  119. }
  120. c := cap(self.tail)
  121. for c < l {
  122. c += 1 + c>>_APPEND_GROW_SHIFT
  123. }
  124. if c == cap(self.tail) {
  125. self.tail = self.tail[:l]
  126. return
  127. }
  128. tmp := make([]*nodeChunk, l, c)
  129. copy(tmp, self.tail)
  130. self.tail = tmp
  131. }
  132. func (self *linkedNodes) ToSlice(con []Node) {
  133. if len(con) < self.size {
  134. return
  135. }
  136. i := (self.size-1)
  137. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  138. if a < 0 {
  139. copy(con, self.head[:b+1])
  140. return
  141. } else {
  142. copy(con, self.head[:])
  143. con = con[_DEFAULT_NODE_CAP:]
  144. }
  145. for i:=0; i<a; i++ {
  146. copy(con, self.tail[i][:])
  147. con = con[_DEFAULT_NODE_CAP:]
  148. }
  149. copy(con, self.tail[a][:b+1])
  150. }
  151. func (self *linkedNodes) FromSlice(con []Node) {
  152. self.size = len(con)
  153. i := self.size-1
  154. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  155. if a < 0 {
  156. copy(self.head[:b+1], con)
  157. return
  158. } else {
  159. copy(self.head[:], con)
  160. con = con[_DEFAULT_NODE_CAP:]
  161. }
  162. if cap(self.tail) <= a {
  163. c := (a+1) + (a+1)>>_APPEND_GROW_SHIFT
  164. self.tail = make([]*nodeChunk, a+1, c)
  165. }
  166. self.tail = self.tail[:a+1]
  167. for i:=0; i<a; i++ {
  168. self.tail[i] = new(nodeChunk)
  169. copy(self.tail[i][:], con)
  170. con = con[_DEFAULT_NODE_CAP:]
  171. }
  172. self.tail[a] = new(nodeChunk)
  173. copy(self.tail[a][:b+1], con)
  174. }
  175. type pairChunk [_DEFAULT_NODE_CAP]Pair
  176. type linkedPairs struct {
  177. head pairChunk
  178. tail []*pairChunk
  179. size int
  180. }
  181. func (self *linkedPairs) Cap() int {
  182. if self == nil {
  183. return 0
  184. }
  185. return (len(self.tail)+1)*_DEFAULT_NODE_CAP
  186. }
  187. func (self *linkedPairs) Len() int {
  188. if self == nil {
  189. return 0
  190. }
  191. return self.size
  192. }
  193. func (self *linkedPairs) At(i int) *Pair {
  194. if self == nil {
  195. return nil
  196. }
  197. if i >= 0 && i < _DEFAULT_NODE_CAP && i<self.size {
  198. return &self.head[i]
  199. } else if i >= _DEFAULT_NODE_CAP && i<self.size {
  200. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  201. if a < len(self.tail) {
  202. return &self.tail[a][b]
  203. }
  204. }
  205. return nil
  206. }
  207. func (self *linkedPairs) Push(v Pair) {
  208. self.Set(self.size, v)
  209. }
  210. func (self *linkedPairs) Set(i int, v Pair) {
  211. if i < _DEFAULT_NODE_CAP {
  212. self.head[i] = v
  213. if self.size <= i {
  214. self.size = i+1
  215. }
  216. return
  217. }
  218. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  219. if a < 0 {
  220. self.head[b] = v
  221. } else {
  222. self.growTailLength(a+1)
  223. var n = &self.tail[a]
  224. if *n == nil {
  225. *n = new(pairChunk)
  226. }
  227. (*n)[b] = v
  228. }
  229. if self.size <= i {
  230. self.size = i+1
  231. }
  232. }
  233. func (self *linkedPairs) growTailLength(l int) {
  234. if l <= len(self.tail) {
  235. return
  236. }
  237. c := cap(self.tail)
  238. for c < l {
  239. c += 1 + c>>_APPEND_GROW_SHIFT
  240. }
  241. if c == cap(self.tail) {
  242. self.tail = self.tail[:l]
  243. return
  244. }
  245. tmp := make([]*pairChunk, l, c)
  246. copy(tmp, self.tail)
  247. self.tail = tmp
  248. }
  249. // linear search
  250. func (self *linkedPairs) Get(key string) (*Pair, int) {
  251. for i:=0; i<self.size; i++ {
  252. if n := self.At(i); n.Key == key {
  253. return n, i
  254. }
  255. }
  256. return nil, -1
  257. }
  258. func (self *linkedPairs) ToSlice(con []Pair) {
  259. if len(con) < self.size {
  260. return
  261. }
  262. i := self.size-1
  263. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  264. if a < 0 {
  265. copy(con, self.head[:b+1])
  266. return
  267. } else {
  268. copy(con, self.head[:])
  269. con = con[_DEFAULT_NODE_CAP:]
  270. }
  271. for i:=0; i<a; i++ {
  272. copy(con, self.tail[i][:])
  273. con = con[_DEFAULT_NODE_CAP:]
  274. }
  275. copy(con, self.tail[a][:b+1])
  276. }
  277. func (self *linkedPairs) ToMap(con map[string]Node) {
  278. for i:=0; i<self.size; i++ {
  279. n := self.At(i)
  280. con[n.Key] = n.Value
  281. }
  282. }
  283. func (self *linkedPairs) FromSlice(con []Pair) {
  284. self.size = len(con)
  285. i := self.size-1
  286. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  287. if a < 0 {
  288. copy(self.head[:b+1], con)
  289. return
  290. } else {
  291. copy(self.head[:], con)
  292. con = con[_DEFAULT_NODE_CAP:]
  293. }
  294. if cap(self.tail) <= a {
  295. c := (a+1) + (a+1)>>_APPEND_GROW_SHIFT
  296. self.tail = make([]*pairChunk, a+1, c)
  297. }
  298. self.tail = self.tail[:a+1]
  299. for i:=0; i<a; i++ {
  300. self.tail[i] = new(pairChunk)
  301. copy(self.tail[i][:], con)
  302. con = con[_DEFAULT_NODE_CAP:]
  303. }
  304. self.tail[a] = new(pairChunk)
  305. copy(self.tail[a][:b+1], con)
  306. }
  307. func (self *linkedPairs) Less(i, j int) bool {
  308. return lessFrom(self.At(i).Key, self.At(j).Key, 0)
  309. }
  310. func (self *linkedPairs) Swap(i, j int) {
  311. a, b := self.At(i), self.At(j)
  312. *a, *b = *b, *a
  313. }
  314. func (self *linkedPairs) Sort() {
  315. sort.Stable(self)
  316. }
  317. // Compare two strings from the pos d.
  318. func lessFrom(a, b string, d int) bool {
  319. l := len(a)
  320. if l > len(b) {
  321. l = len(b)
  322. }
  323. for i := d; i < l; i++ {
  324. if a[i] == b[i] {
  325. continue
  326. }
  327. return a[i] < b[i]
  328. }
  329. return len(a) < len(b)
  330. }
  331. type parseObjectStack struct {
  332. parser Parser
  333. v linkedPairs
  334. }
  335. type parseArrayStack struct {
  336. parser Parser
  337. v linkedNodes
  338. }
  339. func newLazyArray(p *Parser) Node {
  340. s := new(parseArrayStack)
  341. s.parser = *p
  342. return Node{
  343. t: _V_ARRAY_LAZY,
  344. p: unsafe.Pointer(s),
  345. }
  346. }
  347. func newLazyObject(p *Parser) Node {
  348. s := new(parseObjectStack)
  349. s.parser = *p
  350. return Node{
  351. t: _V_OBJECT_LAZY,
  352. p: unsafe.Pointer(s),
  353. }
  354. }
  355. func (self *Node) getParserAndArrayStack() (*Parser, *parseArrayStack) {
  356. stack := (*parseArrayStack)(self.p)
  357. return &stack.parser, stack
  358. }
  359. func (self *Node) getParserAndObjectStack() (*Parser, *parseObjectStack) {
  360. stack := (*parseObjectStack)(self.p)
  361. return &stack.parser, stack
  362. }