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