buffer.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. "github.com/bytedance/sonic/internal/caching"
  21. )
  22. type nodeChunk [_DEFAULT_NODE_CAP]Node
  23. type linkedNodes struct {
  24. head nodeChunk
  25. tail []*nodeChunk
  26. size int
  27. }
  28. func (self *linkedNodes) Cap() int {
  29. if self == nil {
  30. return 0
  31. }
  32. return (len(self.tail)+1)*_DEFAULT_NODE_CAP
  33. }
  34. func (self *linkedNodes) Len() int {
  35. if self == nil {
  36. return 0
  37. }
  38. return self.size
  39. }
  40. func (self *linkedNodes) At(i int) (*Node) {
  41. if self == nil {
  42. return nil
  43. }
  44. if i >= 0 && i<self.size && i < _DEFAULT_NODE_CAP {
  45. return &self.head[i]
  46. } else if i >= _DEFAULT_NODE_CAP && i<self.size {
  47. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  48. if a < len(self.tail) {
  49. return &self.tail[a][b]
  50. }
  51. }
  52. return nil
  53. }
  54. func (self *linkedNodes) MoveOne(source int, target int) {
  55. if source == target {
  56. return
  57. }
  58. if source < 0 || source >= self.size || target < 0 || target >= self.size {
  59. return
  60. }
  61. // reserve source
  62. n := *self.At(source)
  63. if source < target {
  64. // move every element (source,target] one step back
  65. for i:=source; i<target; i++ {
  66. *self.At(i) = *self.At(i+1)
  67. }
  68. } else {
  69. // move every element [target,source) one step forward
  70. for i:=source; i>target; i-- {
  71. *self.At(i) = *self.At(i-1)
  72. }
  73. }
  74. // set target
  75. *self.At(target) = n
  76. }
  77. func (self *linkedNodes) Pop() {
  78. if self == nil || self.size == 0 {
  79. return
  80. }
  81. self.Set(self.size-1, Node{})
  82. self.size--
  83. }
  84. func (self *linkedNodes) Push(v Node) {
  85. self.Set(self.size, v)
  86. }
  87. func (self *linkedNodes) Set(i int, v Node) {
  88. if i < _DEFAULT_NODE_CAP {
  89. self.head[i] = v
  90. if self.size <= i {
  91. self.size = i+1
  92. }
  93. return
  94. }
  95. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  96. if a < 0 {
  97. self.head[b] = v
  98. } else {
  99. self.growTailLength(a+1)
  100. var n = &self.tail[a]
  101. if *n == nil {
  102. *n = new(nodeChunk)
  103. }
  104. (*n)[b] = v
  105. }
  106. if self.size <= i {
  107. self.size = i+1
  108. }
  109. }
  110. func (self *linkedNodes) growTailLength(l int) {
  111. if l <= len(self.tail) {
  112. return
  113. }
  114. c := cap(self.tail)
  115. for c < l {
  116. c += 1 + c>>_APPEND_GROW_SHIFT
  117. }
  118. if c == cap(self.tail) {
  119. self.tail = self.tail[:l]
  120. return
  121. }
  122. tmp := make([]*nodeChunk, l, c)
  123. copy(tmp, self.tail)
  124. self.tail = tmp
  125. }
  126. func (self *linkedNodes) ToSlice(con []Node) {
  127. if len(con) < self.size {
  128. return
  129. }
  130. i := (self.size-1)
  131. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  132. if a < 0 {
  133. copy(con, self.head[:b+1])
  134. return
  135. } else {
  136. copy(con, self.head[:])
  137. con = con[_DEFAULT_NODE_CAP:]
  138. }
  139. for i:=0; i<a; i++ {
  140. copy(con, self.tail[i][:])
  141. con = con[_DEFAULT_NODE_CAP:]
  142. }
  143. copy(con, self.tail[a][:b+1])
  144. }
  145. func (self *linkedNodes) FromSlice(con []Node) {
  146. self.size = len(con)
  147. i := self.size-1
  148. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  149. if a < 0 {
  150. copy(self.head[:b+1], con)
  151. return
  152. } else {
  153. copy(self.head[:], con)
  154. con = con[_DEFAULT_NODE_CAP:]
  155. }
  156. if cap(self.tail) <= a {
  157. c := (a+1) + (a+1)>>_APPEND_GROW_SHIFT
  158. self.tail = make([]*nodeChunk, a+1, c)
  159. }
  160. self.tail = self.tail[:a+1]
  161. for i:=0; i<a; i++ {
  162. self.tail[i] = new(nodeChunk)
  163. copy(self.tail[i][:], con)
  164. con = con[_DEFAULT_NODE_CAP:]
  165. }
  166. self.tail[a] = new(nodeChunk)
  167. copy(self.tail[a][:b+1], con)
  168. }
  169. type pairChunk [_DEFAULT_NODE_CAP]Pair
  170. type linkedPairs struct {
  171. index map[uint64]int
  172. head pairChunk
  173. tail []*pairChunk
  174. size int
  175. }
  176. func (self *linkedPairs) BuildIndex() {
  177. if self.index == nil {
  178. self.index = make(map[uint64]int, self.size)
  179. }
  180. for i:=0; i<self.size; i++ {
  181. p := self.At(i)
  182. self.index[p.hash] = i
  183. }
  184. }
  185. func (self *linkedPairs) Cap() int {
  186. if self == nil {
  187. return 0
  188. }
  189. return (len(self.tail)+1)*_DEFAULT_NODE_CAP
  190. }
  191. func (self *linkedPairs) Len() int {
  192. if self == nil {
  193. return 0
  194. }
  195. return self.size
  196. }
  197. func (self *linkedPairs) At(i int) *Pair {
  198. if self == nil {
  199. return nil
  200. }
  201. if i >= 0 && i < _DEFAULT_NODE_CAP && i<self.size {
  202. return &self.head[i]
  203. } else if i >= _DEFAULT_NODE_CAP && i<self.size {
  204. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  205. if a < len(self.tail) {
  206. return &self.tail[a][b]
  207. }
  208. }
  209. return nil
  210. }
  211. func (self *linkedPairs) Push(v Pair) {
  212. self.Set(self.size, v)
  213. }
  214. func (self *linkedPairs) Pop() {
  215. if self == nil || self.size == 0 {
  216. return
  217. }
  218. self.Unset(self.size-1)
  219. self.size--
  220. }
  221. func (self *linkedPairs) Unset(i int) {
  222. if self.index != nil {
  223. p := self.At(i)
  224. delete(self.index, p.hash)
  225. }
  226. self.set(i, Pair{})
  227. }
  228. func (self *linkedPairs) Set(i int, v Pair) {
  229. if self.index != nil {
  230. h := v.hash
  231. self.index[h] = i
  232. }
  233. self.set(i, v)
  234. }
  235. func (self *linkedPairs) set(i int, v Pair) {
  236. if i < _DEFAULT_NODE_CAP {
  237. self.head[i] = v
  238. if self.size <= i {
  239. self.size = i+1
  240. }
  241. return
  242. }
  243. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  244. if a < 0 {
  245. self.head[b] = v
  246. } else {
  247. self.growTailLength(a+1)
  248. var n = &self.tail[a]
  249. if *n == nil {
  250. *n = new(pairChunk)
  251. }
  252. (*n)[b] = v
  253. }
  254. if self.size <= i {
  255. self.size = i+1
  256. }
  257. }
  258. func (self *linkedPairs) growTailLength(l int) {
  259. if l <= len(self.tail) {
  260. return
  261. }
  262. c := cap(self.tail)
  263. for c < l {
  264. c += 1 + c>>_APPEND_GROW_SHIFT
  265. }
  266. if c == cap(self.tail) {
  267. self.tail = self.tail[:l]
  268. return
  269. }
  270. tmp := make([]*pairChunk, l, c)
  271. copy(tmp, self.tail)
  272. self.tail = tmp
  273. }
  274. // linear search
  275. func (self *linkedPairs) Get(key string) (*Pair, int) {
  276. if self.index != nil {
  277. // fast-path
  278. i, ok := self.index[caching.StrHash(key)]
  279. if ok {
  280. n := self.At(i)
  281. if n.Key == key {
  282. return n, i
  283. }
  284. // hash conflicts
  285. goto linear_search
  286. } else {
  287. return nil, -1
  288. }
  289. }
  290. linear_search:
  291. for i:=0; i<self.size; i++ {
  292. if n := self.At(i); n.Key == key {
  293. return n, i
  294. }
  295. }
  296. return nil, -1
  297. }
  298. func (self *linkedPairs) ToSlice(con []Pair) {
  299. if len(con) < self.size {
  300. return
  301. }
  302. i := self.size-1
  303. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  304. if a < 0 {
  305. copy(con, self.head[:b+1])
  306. return
  307. } else {
  308. copy(con, self.head[:])
  309. con = con[_DEFAULT_NODE_CAP:]
  310. }
  311. for i:=0; i<a; i++ {
  312. copy(con, self.tail[i][:])
  313. con = con[_DEFAULT_NODE_CAP:]
  314. }
  315. copy(con, self.tail[a][:b+1])
  316. }
  317. func (self *linkedPairs) ToMap(con map[string]Node) {
  318. for i:=0; i<self.size; i++ {
  319. n := self.At(i)
  320. con[n.Key] = n.Value
  321. }
  322. }
  323. func (self *linkedPairs) copyPairs(to []Pair, from []Pair, l int) {
  324. copy(to, from)
  325. if self.index != nil {
  326. for i:=0; i<l; i++ {
  327. // NOTICE: in case of user not pass hash, just cal it
  328. h := caching.StrHash(from[i].Key)
  329. from[i].hash = h
  330. self.index[h] = i
  331. }
  332. }
  333. }
  334. func (self *linkedPairs) FromSlice(con []Pair) {
  335. self.size = len(con)
  336. i := self.size-1
  337. a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
  338. if a < 0 {
  339. self.copyPairs(self.head[:b+1], con, b+1)
  340. return
  341. } else {
  342. self.copyPairs(self.head[:], con, len(self.head))
  343. con = con[_DEFAULT_NODE_CAP:]
  344. }
  345. if cap(self.tail) <= a {
  346. c := (a+1) + (a+1)>>_APPEND_GROW_SHIFT
  347. self.tail = make([]*pairChunk, a+1, c)
  348. }
  349. self.tail = self.tail[:a+1]
  350. for i:=0; i<a; i++ {
  351. self.tail[i] = new(pairChunk)
  352. self.copyPairs(self.tail[i][:], con, len(self.tail[i]))
  353. con = con[_DEFAULT_NODE_CAP:]
  354. }
  355. self.tail[a] = new(pairChunk)
  356. self.copyPairs(self.tail[a][:b+1], con, b+1)
  357. }
  358. func (self *linkedPairs) Less(i, j int) bool {
  359. return lessFrom(self.At(i).Key, self.At(j).Key, 0)
  360. }
  361. func (self *linkedPairs) Swap(i, j int) {
  362. a, b := self.At(i), self.At(j)
  363. if self.index != nil {
  364. self.index[a.hash] = j
  365. self.index[b.hash] = i
  366. }
  367. *a, *b = *b, *a
  368. }
  369. func (self *linkedPairs) Sort() {
  370. sort.Stable(self)
  371. }
  372. // Compare two strings from the pos d.
  373. func lessFrom(a, b string, d int) bool {
  374. l := len(a)
  375. if l > len(b) {
  376. l = len(b)
  377. }
  378. for i := d; i < l; i++ {
  379. if a[i] == b[i] {
  380. continue
  381. }
  382. return a[i] < b[i]
  383. }
  384. return len(a) < len(b)
  385. }
  386. type parseObjectStack struct {
  387. parser Parser
  388. v linkedPairs
  389. }
  390. type parseArrayStack struct {
  391. parser Parser
  392. v linkedNodes
  393. }
  394. func newLazyArray(p *Parser) Node {
  395. s := new(parseArrayStack)
  396. s.parser = *p
  397. return Node{
  398. t: _V_ARRAY_LAZY,
  399. p: unsafe.Pointer(s),
  400. }
  401. }
  402. func newLazyObject(p *Parser) Node {
  403. s := new(parseObjectStack)
  404. s.parser = *p
  405. return Node{
  406. t: _V_OBJECT_LAZY,
  407. p: unsafe.Pointer(s),
  408. }
  409. }
  410. func (self *Node) getParserAndArrayStack() (*Parser, *parseArrayStack) {
  411. stack := (*parseArrayStack)(self.p)
  412. return &stack.parser, stack
  413. }
  414. func (self *Node) getParserAndObjectStack() (*Parser, *parseObjectStack) {
  415. stack := (*parseObjectStack)(self.p)
  416. return &stack.parser, stack
  417. }