Guest User

arrays.inc

a guest
Dec 12th, 2025
25
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.98 KB | None | 0 0
  1. ; SPDX-License-Identifier: MIT
  2.  
  3. /*
  4. TODO: support negative indexes? (for `array_get`, `array_set`, `array_delete`, `array_slice`)
  5.  
  6. Give this its own rgbds-arrays repo with arrays.inc and string-arrays.inc
  7. Make rgbds-control-structures repo for conditionals.inc and loops.inc
  8. rgbds-utils can have data.inc, code.inc, random.inc
  9. Also make enums.inc
  10. */
  11.  
  12. /**
  13. * To create an array:
  14. * def_array squares, 0, 1, 4, 9, 16, 25
  15. * (or use the other `def_array_*` macros)
  16. * To get the length of an array:
  17. * squares#len
  18. * To index an array with a hex value:
  19. * squares$1
  20. * To index an array with a variable:
  21. * def idx = 1
  22. * squares{idx}
  23. * To set the starting index for subsequent arrays:
  24. * use_array_base 1
  25. * To get the starting index of an array:
  26. * squares#base
  27. * To make subsequent arrays use constants or variables as values:
  28. * use_constant_arrays
  29. * use_variable_arrays
  30. * To operate on arrays, use the `array_*` macros:
  31. * array_println squares
  32. * array_reverse squares
  33. * array_append squares, 36, 49, 64
  34. * (...etc)
  35. */
  36.  
  37. if !def(ARRAYS_INC)
  38. def ARRAYS_INC equ 1
  39.  
  40. ; The current version of this library is 1.0.
  41. def ARRAYS_VERSION equs "1.0"
  42.  
  43. ; Check whether this library version meets a minimum required one.
  44. MACRO? check_arrays_inc_min_version ; major[.minor]
  45. pusho Q16
  46. if strfind("\1", ".") != -1
  47. def __min_ver equs "\1"
  48. else
  49. def __min_ver equs "\1.0"
  50. endc
  51. if {ARRAYS_VERSION} < {__min_ver}
  52. fail "arrays.inc version {ARRAYS_VERSION} is incompatible with minimum required version {__min_ver}"
  53. endc
  54. popo
  55. purge __min_ver
  56. ENDM
  57.  
  58. ; The base index for arrays.
  59. ; 0 by default, but 1 would match how RGBASM strings and macro arguments are indexed.
  60. ; Only modify this using `use_array_base`.
  61. def __arrays_base = 0
  62.  
  63. ; Set the base index for subsequent arrays.
  64. ; Typically 0 or 1, but higher non-negative bases are also technically valid.
  65. MACRO? use_array_base ; base
  66. def __arrays_base = \1
  67. __check_non_negative_for_array __arrays_base, "Array base", use_array_base
  68. ENDM
  69.  
  70. ; Whether array items are constants (if 1) or variables (if 0).
  71. ; Array items are constant by default.
  72. ; Only modify this using `use_constant_arrays` or `use_variable_arrays`.
  73. def __arrays_are_constant = 1
  74.  
  75. ; Set subsequent arrays to have constant items.
  76. MACRO? use_constant_arrays
  77. def __arrays_are_constant = 1
  78. ENDM
  79.  
  80. ; Set subsequent arrays to have variable items.
  81. MACRO? use_variable_arrays
  82. def __arrays_are_constant = 0
  83. ENDM
  84.  
  85. ; The pseudorandom number generator (PRNG) state
  86. ; for shuffling and picking from arrays.
  87. ; Only modify this using `array_reseed`.
  88. def __array_prng = ((((__UTC_SECOND__ * 60 + __UTC_MINUTE__) * 60 + __UTC_HOUR__) * 24 + \
  89. __UTC_DAY__ - 1) * 31 + __UTC_MONTH__ - 1) * 12 + __UTC_YEAR__ % 100
  90.  
  91. ; Reseed the PRNG used in randomized array macros.
  92. MACRO? array_reseed ; seed
  93. def __array_prng = \1
  94. ENDM
  95.  
  96. ; Generate the next PRNG value.
  97. ; Uses an xorshift32 algorithm.
  98. ; Meant for internal use.
  99. MACRO? __next_array_prng
  100. def __array_prng ^= __array_prng << 13
  101. def __array_prng ^= __array_prng >>> 17
  102. def __array_prng ^= __array_prng << 5
  103. ENDM
  104.  
  105. ; Causes assembly to fail if the specified value is negative.
  106. ; Meant for internal use.
  107. MACRO? __check_non_negative_for_array ; value [, value context], macro context
  108. if (\1) < 0
  109. if _NARG == 3
  110. fail strfmt("\3: %s must not be negative: %d", \2, \1)
  111. else
  112. fail strfmt("\2: \1 must not be negative: %d", \1)
  113. endc
  114. endc
  115. ENDM
  116.  
  117. ; Causes assembly to fail if the specified array is not defined.
  118. ; Meant for internal use.
  119. MACRO? __check_array_defined ; name, macro context
  120. if !def(\1#base) || !def(\1#len) || !def(\1#equ)
  121. fail "\2: Array \1 is not defined"
  122. endc
  123. __check_non_negative_for_array \1#base, \2
  124. __check_non_negative_for_array \1#len, \2
  125. ENDM
  126.  
  127. ; Causes assembly to fail if the specified array is already defined.
  128. ; Meant for internal use.
  129. MACRO? __check_array_undefined ; name, macro context
  130. if def(\1#base) || def(\1#len) || def(\1#equ)
  131. fail "\2: Array \1 is already defined"
  132. endc
  133. ENDM
  134.  
  135. ; Causes assembly to fail if the specified array index is out of bounds.
  136. ; Meant for internal use.
  137. MACRO? __check_array_index ; name, index, macro context
  138. if !(\1#base <= (\2) && (\2) < \1#base + \1#len)
  139. fail strfmt("\3: Invalid index for array \1: %d", \2)
  140. endc
  141. ENDM
  142.  
  143. ; Causes assembly to fail if the specified array position is out of bounds.
  144. ; (A position must be a valid index within the array, or an index 1 past the end of the array.)
  145. ; Meant for internal use.
  146. MACRO? __check_array_pos ; name, pos, macro context
  147. if !(\1#base <= (\2) && (\2) <= \1#base + \1#len)
  148. fail strfmt("\3: Invalid position for array \1: %d", \2)
  149. endc
  150. ENDM
  151.  
  152. ; Initializes an array's metadata, though not its items.
  153. ; Meant for internal use.
  154. MACRO? __init_array ; name, length, macro context
  155. __check_array_undefined \1, \3
  156. __check_non_negative_for_array \2, "Length", \3
  157. def \1#base equ __arrays_base
  158. def \1#len equ \2
  159. if __arrays_are_constant
  160. def \1#equ equs "equ"
  161. else
  162. def \1#equ equs "="
  163. endc
  164. redef def_array_item equs "array_append \1,"
  165. ENDM
  166.  
  167. ; Defines a new array, initializing it with zero or more values.
  168. ; Redefines `def_array_item <...values>` to append values to this array.
  169. MACRO? def_array ; name, ...values
  170. __init_array \1, {_NARG} - 1, def_array
  171. def __start = \1#base - 2
  172. for? __argi, 2, _NARG + 1
  173. def __idx = __start + __argi
  174. def \1{__idx} {\1#equ} \<__argi>
  175. endr
  176. purge __start, __argi
  177. if def(__idx)
  178. purge __idx
  179. endc
  180. ENDM
  181.  
  182. ; Defines a new array, initializing it to be filled with N of the same value,
  183. ; or with N zeros if the value is unspecified.
  184. ; Redefines `def_array_item <...values>` to append values to this array.
  185. MACRO? def_array_fill ; name, length [, value = 0]
  186. __init_array \1, \2, def_array_fill
  187. if _NARG == 3
  188. def __val = \3
  189. else
  190. def __val = 0
  191. endc
  192. for? __idx, \1#base, \1#base + \1#len
  193. def \1{__idx} {\1#equ} __va
  194. endr
  195. purge __val, __idx
  196. ENDM
  197.  
  198. ; Defines a new array, initializing it to be an arithmetic sequence.
  199. ; - `def_array_range <name>, <stop>`
  200. ; gives the half-open interval [0, <stop>).
  201. ; - `def_array_range <name>, <start>, <stop>`
  202. ; gives the half-open interval [<start>, <stop>).
  203. ; - `def_array_range <name>, <start>, <stop>, <step>`
  204. ; gives the half-open interval [<start>, <stop>), skipping by <step>.
  205. ; Redefines `def_array_item <...values>` to append values to this array.
  206. MACRO? def_array_range ; name [, start = 0], stop [, step = 1]
  207. if _NARG == 4
  208. def __start = \2
  209. def __stop = \3
  210. def __step = \4
  211. elif _NARG == 3
  212. def __start = \2
  213. def __stop = \3
  214. def __step = 1
  215. else
  216. def __start = 0
  217. def __stop = \2
  218. def __step = 1
  219. endc
  220. __check_non_negative_for_array __start, "Start", def_array_range
  221. __check_non_negative_for_array __stop, "Stop", def_array_range
  222. if __step == 0
  223. fail "def_array_range: Step must not be zero"
  224. endc
  225. if __step > 0 && __start < __stop
  226. def __len = (__stop - __start - 1) / __step + 1
  227. elif __step < 0 && __stop < __start
  228. def __len = (__start - __stop - 1) / -__step + 1
  229. else
  230. def __len = 0
  231. endc
  232. __init_array \1, __len, def_array_range
  233. def __val = __start
  234. for? __idx, \1#base, \1#base + \1#len
  235. def \1{__idx} {\1#equ} __val
  236. def __val += __step
  237. endr
  238. purge __start, __stop, __step, __len, __val, __idx
  239. ENDM
  240.  
  241. ; Defines a new array as a copy of another array.
  242. ; Redefines `def_array_item <...values>` to append values to this array.
  243. MACRO? def_array_copy ; name, other
  244. __check_array_defined \2, def_array_copy
  245. __init_array \1, \2#len, def_array_copy
  246. def __delta = \1#base - \2#base
  247. for? __idx, \2#base, \2#base + \2#len
  248. def __new = __idx + __delta
  249. def \1{__new} {\1#equ} \2{__idx}
  250. endr
  251. purge __delta, __idx
  252. if def (__new)
  253. purge __new
  254. endc
  255. ENDM
  256.  
  257. ; Purges an array and all its items.
  258. MACRO? array_purge ; name
  259. __check_array_defined \1, array_purge
  260. for? __idx, \1#base, \1#base + \1#len
  261. purge \1{__idx}
  262. endr
  263. purge \1#base, \1#len, \1#equ, __idx
  264. ENDM
  265.  
  266. ; Removes all items from an array, resetting its length to 0.
  267. MACRO? array_clear ; name
  268. __check_array_defined \1, array_clear
  269. for? __idx, \1#base, \1#base + \1#len
  270. purge \1{__idx}
  271. endr
  272. redef \1#len {\1#equ} 0
  273. purge __idx
  274. ENDM
  275.  
  276. ; Pads the end of an array up to a minimum length with a given value,
  277. ; or with 0 if the value is unspecified.
  278. MACRO? array_pad ; name, length [, value = 0]
  279. __check_array_defined \1, array_pad
  280. def __len = \2
  281. if __len > \1#len
  282. if _NARG == 3
  283. def __val = \3
  284. else
  285. def __val = 0
  286. endc
  287. for? __idx, \1#base + \1#len, \1#base + \1#len + __len
  288. def \1{__idx} {\1#equ} __val
  289. endr
  290. def \1#len equ __len
  291. endc
  292. purge __len
  293. if def (__val)
  294. purge __val, __idx
  295. endc
  296. ENDM
  297.  
  298. ; Pads the beginning of an array up to a minimum length with a given value,
  299. ; or with 0 if the value is unspecified.
  300. MACRO? array_lpad ; name, length [, value = 0]
  301. __check_array_defined \1, array_lpad
  302. def __len = \2
  303. if __len > \1#len
  304. if _NARG == 3
  305. def __val = \3
  306. else
  307. def __val = 0
  308. endc
  309. def __off = __len - \1#len
  310. for? __idx, \1#base + \1#len - 1, \1#base - 1, -1
  311. def __new = __idx + __off
  312. def \1{__new} {\1#equ} \1#{__idx}
  313. endr
  314. for? __idx, \1#base, \1#base + __off
  315. def \1{__idx} {\1#equ} __val
  316. endr
  317. def \1#len equ __len
  318. endc
  319. purge __len
  320. if def (__val)
  321. purge __val, __off, __idx
  322. if def(__new)
  323. purge __new
  324. endc
  325. endc
  326. ENDM
  327.  
  328. ; Prints the items of an array, comma-separated between brackets.
  329. MACRO? array_print ; name
  330. __check_array_defined \1, array_print
  331. print "["
  332. if \1#len > 0
  333. print \1{\1#base}
  334. endc
  335. for? __idx, \1#base + 1, \1#base + \1#len
  336. print ", ", \1{__idx}
  337. endr
  338. print "]"
  339. purge __idx
  340. ENDM
  341.  
  342. ; Prints the items of an array, comma-separated between brackets,
  343. ; followed by a newline.
  344. MACRO? array_println ; name
  345. __check_array_defined \1, array_println
  346. array_print \1
  347. println
  348. ENDM
  349.  
  350. ; Gets the value of an item in an array.
  351. MACRO? array_get ; result, name, index
  352. __check_array_defined \2, array_get
  353. def __idx = \3
  354. __check_array_index \2, __idx, array_get
  355. def \1 = \2{__idx}
  356. purge __idx
  357. ENDM
  358.  
  359. ; Sets the value of an item in an array.
  360. MACRO? array_set ; name, index, value
  361. __check_array_defined \1, array_set
  362. def __idx = \2
  363. __check_array_index \1, __idx, array_set
  364. redef \1{__idx} {\1#equ} \3
  365. purge __idx
  366. ENDM
  367.  
  368. ; Deletes an item from an array.
  369. MACRO? array_delete ; name, index
  370. __check_array_defined \1, array_delete
  371. def __del = \2
  372. __check_array_index \1, __del, array_delete
  373. for? __new, __del, \1#base + \1#len - 1
  374. def __idx = __new + 1
  375. redef \1{__new} {\1#equ} \1{__idx}
  376. endr
  377. purge \1{__new}
  378. redef \1#len equ \1#len - 1
  379. purge __del, __new
  380. if def(__idx)
  381. purge __idx
  382. endc
  383. ENDM
  384.  
  385. ; Inserts one or more values into an array at a given position.
  386. MACRO? array_insert ; name, index, ...values
  387. __check_array_defined \1, array_insert
  388. def __ins = \2
  389. __check_array_pos \1, __ins, array_insert
  390. for? __idx, \1#base + \1#len - 1, __ins - 1, -1
  391. def __new = __idx + _NARG - 2
  392. redef \1{__new} {\1#equ} \1{__idx}
  393. endr
  394. for? __argi, 3, _NARG + 1
  395. def __new = __ins + __argi - 3
  396. redef \1{__new} {\1#equ} \<__argi>
  397. endr
  398. redef \1#len equ \1#len + _NARG - 2
  399. purge __ins, __idx, __argi
  400. if def(__new)
  401. purge __new
  402. endc
  403. ENDM
  404.  
  405. ; Appends one or more values to the end of an array.
  406. MACRO? array_append ; name, ...values
  407. __check_array_defined \1, array_append
  408. def __start = \1#base + \1#len
  409. for? __argi, 2, _NARG + 1
  410. def __idx = __start + __argi - 2
  411. def \1{__idx} {\1#equ} \<__argi>
  412. endr
  413. redef \1#len equ \1#len + _NARG - 1
  414. purge __start, __argi
  415. if def(__idx)
  416. purge __idx
  417. endc
  418. ENDM
  419.  
  420. ; Prepends one or more values to the beginning of an array.
  421. MACRO? array_prepend ; name, ...values
  422. __check_array_defined \1, array_prepend
  423. def __base = \1#base
  424. for? __idx, \1#base + \1#len - 1, \1#base - 1, -1
  425. def __new = __idx + _NARG - 1
  426. redef \1{__new} {\1#equ} \1{__idx}
  427. endr
  428. for? __argi, 2, _NARG + 1
  429. def __new = __base + __argi - 2
  430. redef \1{__new} {\1#equ} \<__argi>
  431. endr
  432. redef \1#len equ \1#len + _NARG - 1
  433. purge __base, __idx, __argi
  434. if def(__new)
  435. purge __new
  436. endc
  437. ENDM
  438.  
  439. ; Extends an array by concatenating other arrays.
  440. MACRO? array_extend ; name, ...others
  441. __check_array_defined \1, array_extend
  442. for? __arr, 2, _NARG + 1
  443. __check_array_defined \<__arr>, array_extend
  444. def __delta = \1#base + \1#len - \<__arr>#base
  445. for? __idx, \<__arr>#base, \<__arr>#base + \<__arr>#len
  446. def __new = __idx + __delta
  447. def \1{__new} {\1#equ} \<__arr>{__idx}
  448. endr
  449. redef \1#len equ \1#len + \<__arr>#len
  450. endr
  451. purge _arr, __delta, __idx
  452. if def(__new)
  453. purge __new
  454. endc
  455. ENDM
  456.  
  457. ; Redefines an array to be a slice of itself starting at an index,
  458. ; and ending before a subsequent position if one is specified.
  459. MACRO? array_slice ; name, start index [, end pos]
  460. __check_array_defined \1, array_slice
  461. def __start = \2
  462. __check_array_index \1, __start, array_slice
  463. if _NARG == 3
  464. def __end = \3
  465. __check_array_pos \1, __end, array_slice
  466. else
  467. def __end = \1#base + \1#len
  468. endc
  469. if __start > __end
  470. fail "array_slice: Invalid slice for array \1: [{d:__start}, {d:__end})"
  471. endc
  472. def __delta = \1#base - __start
  473. for? __idx, __start, __end
  474. def __new = __idx + __delta
  475. redef \1{__new} {\1#equ} \1{__idx}
  476. endr
  477. for? __idx, __end + __delta, \1#base + \1#len
  478. purge \1{__idx}
  479. endr
  480. redef \1#len equ __end - __start
  481. purge __start, __end, __delta, __idx
  482. if def(__new)
  483. purge __new
  484. endc
  485. ENDM
  486.  
  487. ; Checks whether a value exists in an array,
  488. ; setting the result to 1 if it does or 0 if it does not.
  489. MACRO? array_contains ; result, name, value
  490. __check_array_defined \2, array_find
  491. def \1 = 0
  492. def __val = \3
  493. for? __idx, \2#base, \2#base + \2#len
  494. if \2{__idx} == __val
  495. def \1 = 1
  496. break
  497. endc
  498. endr
  499. purge __val, __idx
  500. ENDM
  501.  
  502. ; Find the first index of a value in an array,
  503. ; or -1 if the value is not in the array.
  504. MACRO? array_find ; result, name, value
  505. __check_array_defined \2, array_find
  506. def \1 = -1
  507. def __val = \3
  508. for? __idx, \2#base, \2#base + \2#len
  509. if \2{__idx} == __val
  510. def \1 = __idx
  511. break
  512. endc
  513. endr
  514. purge __val, __idx
  515. ENDM
  516.  
  517. ; Find the last index of a value in an array,
  518. ; or -1 if the value is not in the array.
  519. MACRO? array_rfind ; result, name, value
  520. __check_array_defined \2, array_rfind
  521. def \1 = -1
  522. def __val = \3
  523. for? __idx, \2#base + \2#len - 1, \2#base - 1, -1
  524. if \2{__idx} == __val
  525. def \1 = __idx
  526. break
  527. endc
  528. endr
  529. purge __val, __idx
  530. ENDM
  531.  
  532. ; Count the occurrences of a value in an array.
  533. MACRO? array_count ; result, name, value
  534. __check_array_defined \2, array_count
  535. def \1 = 0
  536. def __val = \3
  537. for? __idx, \2#base, \2#base + \2#len
  538. if \2{__idx} == __val
  539. def \1 += 1
  540. endc
  541. endr
  542. purge __val, __idx
  543. ENDM
  544.  
  545. ; Replace the first N occurrences of one value in an array with another,
  546. ; or replace all of them if N is unspecified.
  547. MACRO? array_replace ; name, old, new [, limit]
  548. __check_array_defined \1, array_replace
  549. def __old = \2
  550. def __new = \3
  551. if _NARG == 4
  552. def __limit = \4
  553. __check_non_negative_for_array __limit, "Limit", array_replace
  554. else
  555. def __limit = \1#len
  556. endc
  557. def __num = 0
  558. for? __idx, \1#base, \1#base + \1#len
  559. if \1{__idx} == __old
  560. redef \1{__idx} {\1#equ} __new
  561. def __num += 1
  562. if __num == __limit
  563. break
  564. endc
  565. endc
  566. endr
  567. purge __old, __new, __limit, __num, __idx
  568. ENDM
  569.  
  570. ; Replace the last N occurrences of one value in an array with another,
  571. ; or replace all of them if N is unspecified.
  572. MACRO? array_rreplace ; name, old, new [, limit]
  573. __check_array_defined \1, array_rreplace
  574. def __old = \2
  575. def __new = \3
  576. if _NARG == 4
  577. def __limit = \4
  578. __check_non_negative_for_array __limit, "Limit", array_rreplace
  579. else
  580. def __limit = \1#len
  581. endc
  582. def __num = 0
  583. for? __idx, \1#base + \1#len - 1, \1#base - 1, -1
  584. if \1{__idx} == __old
  585. redef \1{__idx} {\1#equ} __new
  586. def __num += 1
  587. if __num == __limit
  588. break
  589. endc
  590. endc
  591. endr
  592. purge __old, __new, __limit, __num, __idx
  593. ENDM
  594.  
  595. ; Remove the first N occurrences of a value in an array,
  596. ; or remove all of them if N is unspecified.
  597. MACRO? array_remove ; name, value [, limit]
  598. __check_array_defined \1, array_remove
  599. def __val = \2
  600. if _NARG == 3
  601. def __limit = \3
  602. __check_non_negative_for_array __limit, "Limit", array_remove
  603. else
  604. def __limit = \1#len
  605. endc
  606. def __num = 0
  607. for? __idx, \1#base, \1#base + \1#len
  608. if \1{__idx} == __val && __num < __limit
  609. def __num += 1
  610. elif __num > 0
  611. def __new = __idx - __num
  612. redef \1{__new} {\1#equ} \1{__idx}
  613. endc
  614. endr
  615. for? __idx, __idx - __num, __idx - 1
  616. purge \1{__idx}
  617. endr
  618. redef \1#len equ \1#len - __num
  619. purge __val, __limit, __num, __idx
  620. if def(__new)
  621. purge __new
  622. endc
  623. ENDM
  624.  
  625. ; Remove the last N occurrences of a value in an array,
  626. ; or remove all of them if N is unspecified.
  627. MACRO? array_rremove ; name, value [, limit]
  628. __check_array_defined \1, array_rremove
  629. def __val = \2
  630. if _NARG == 3
  631. def __limit = \3
  632. __check_non_negative_for_array __limit, "Limit", array_rremove
  633. else
  634. def __limit = \1#len
  635. endc
  636. def __end = \1#base + \1#len
  637. def __num = 0
  638. for? __idx, __end - 1, \1#base - 1, -1
  639. if \1{__idx} == __val && __num < __limit
  640. for? __next, __idx + 1, __end - __num
  641. def __new = __next - 1
  642. redef \1{__new} {\1#equ} \1{__next}
  643. endr
  644. def __num += 1
  645. if __num == __limit
  646. break
  647. endc
  648. endc
  649. endr
  650. for? __idx, __end - 1 - __num, __end - 1
  651. purge \1{__idx}
  652. endr
  653. redef \1#len equ \1#len - __num
  654. purge __val, __limit, __end, __num, __idx
  655. if def(__next)
  656. purge __next, __new
  657. endc
  658. ENDM
  659.  
  660. ; Reverses an array.
  661. MACRO? array_reverse ; name
  662. __check_array_defined \1, array_reverse
  663. def __base = \1#base
  664. def __last = \1#base + \1#len - 1
  665. for? __off, \1#len / 2
  666. def __i = __base + __off
  667. def __j = __last - __off
  668. def __tmp = \1{__i}
  669. redef \1{__i} {\1#equ} \1{__j}
  670. redef \1{__j} {\1#equ} __tmp
  671. endr
  672. purge __base, __last, __off
  673. if def(__i)
  674. purge __i, __j, __tmp
  675. endc
  676. ENDM
  677.  
  678. ; Sorts an array by a comparison operator.
  679. ; Uses a bottom-up merge sort algorithm.
  680. ; Meant for internal use.
  681. MACRO? __sort_array ; name, operator, macro context
  682. __check_array_defined \1, \3
  683. def __base = \1#base
  684. def __len = \1#len
  685. def __cap = __base + __len
  686. def __max = 0
  687. for? __p, 1, 32
  688. if __len >= 1 << __p
  689. def __max += 1
  690. endc
  691. endr
  692. for? __p, __max
  693. def __w = 2 ** __p
  694. for? __i, __base, __cap, 2 * __w
  695. if __i + __w >= __cap
  696. break
  697. endc
  698. def __l1 = __i
  699. def __r1 = __l1 + __w - 1
  700. def __l2 = __r1 + 1
  701. def __r2 = __l2 + __w - 1
  702. if __r2 >= __cap
  703. def __r2 = __cap - 1
  704. endc
  705. for? __t, __len
  706. if __l1 > __r1 || __l2 > __r2
  707. break
  708. endc
  709. if \1{__l1} \2 \1{__l2}
  710. def __tmp{u:__t} = \1{__l1}
  711. def __l1 += 1
  712. else
  713. def __tmp{u:__t} = \1{__l2}
  714. def __l2 += 1
  715. endc
  716. endr
  717. for? __j, __l1, __r1 + 1
  718. def __tmp{u:__t} = \1{__j}
  719. def __t += 1
  720. endr
  721. for? __j, __l2, __r2 + 1
  722. def __tmp{u:__t} = \1{__j}
  723. def __t += 1
  724. endr
  725. for? __j, __t
  726. def __k = __i + __j
  727. redef \1{__k} {\1#equ} __tmp{u:__j}
  728. purge __tmp{u:__j}
  729. endr
  730. endr
  731. endr
  732. purge __base, __len, __cap, __p, __max
  733. if def(__w)
  734. purge __w, __i
  735. if def(__l1)
  736. purge __l1, __r1, __l2, __r2, __t, __j, __k
  737. endc
  738. endc
  739. ENDM
  740.  
  741. ; Sorts an array in order from least to greatest.
  742. ; Uses an O(n log n) in-place merge sort algorithm.
  743. MACRO? array_sort ; name
  744. __sort_array \1, <=, array_sort
  745. ENDM
  746.  
  747. ; Sorts an array in order from greatest to least.
  748. ; Uses an O(n log n) in-place merge sort algorithm.
  749. MACRO? array_rsort ; name
  750. __sort_array \1, >=, array_rsort
  751. ENDM
  752.  
  753. ; Checks whether an array is sorted from least to greatest,
  754. ; setting the result to 1 if it is or 0 if it is not.
  755. MACRO? array_is_sorted ; result, name
  756. __check_array_defined \2, array_is_sorted
  757. def \1 = 1
  758. if \2#len > 1
  759. def __prev = \2{\2#base}
  760. for? __idx, \2#base + 1, \2#base + \2#len
  761. if \2{__idx} < __prev
  762. def \1 = 0
  763. break
  764. endc
  765. def __prev = \2{__idx}
  766. endr
  767. endc
  768. if def(__prev)
  769. purge __prev, __idx
  770. endc
  771. ENDM
  772.  
  773. ; Checks whether an array is sorted from greatest to least,
  774. ; setting the result to 1 if it is or 0 if it is not.
  775. MACRO? array_is_rsorted ; result, name
  776. __check_array_defined \2, array_is_rsorted
  777. def \1 = 1
  778. if \2#len > 1
  779. def __prev = \2{\2#base}
  780. for? __idx, \2#base + 1, \2#base + \2#len
  781. if \2{__idx} > __prev
  782. def \1 = 0
  783. break
  784. endc
  785. def __prev = \2{__idx}
  786. endr
  787. endc
  788. if def(__prev)
  789. purge __prev, __idx
  790. endc
  791. ENDM
  792.  
  793. ; Randomly shuffle an array
  794. ; Uses the O(n) Fisher-Yates aka Knuth algorithm.
  795. MACRO? array_shuffle ; name
  796. __check_array_defined \1, array_shuffle
  797. def __base = \1#base
  798. for? __i, \1#len - 1, 0, -1
  799. __next_array_prng
  800. def __j = __base + __array_prng % (__i + 1)
  801. def __i += __base
  802. def __tmp = \1{__i}
  803. redef \1{__i} {\1#equ} \1{__j}
  804. redef \1{__j} {\1#equ} __tmp
  805. endr
  806. purge __base, __i
  807. if def(__j)
  808. purge __j, __tmp
  809. endc
  810. ENDM
  811.  
  812. ; Randomly pick an item from an array.
  813. MACRO? array_pick ; result, name
  814. __check_array_defined \2, array_pick
  815. __next_array_prng
  816. def __idx = \2#base + __array_prng % \2#len
  817. def \1 = \2{__idx}
  818. purge __idx
  819. ENDM
  820.  
  821. ; Deduplicates an array by reducing runs of the same value to one item.
  822. MACRO? array_dedup ; name
  823. __check_array_defined \1, array_dedup
  824. def __num = 0
  825. for? __idx, \1#base + 1, \1#base + \1#len
  826. def __old = __idx - __num - 1
  827. if \1{__idx} == \1{__old}
  828. def __num += 1
  829. elif __num > 0
  830. def __new = __idx - __num
  831. redef \1{__new} {\1#equ} \1{__idx}
  832. endc
  833. endr
  834. for? __idx, __idx - __num, __idx - 1
  835. purge \1{__idx}
  836. endr
  837. redef \1#len equ \1#len - __num
  838. purge __num, __idx
  839. if def(__old)
  840. purge __old
  841. if def(__new)
  842. purge __new
  843. endc
  844. endc
  845. ENDM
  846.  
  847. ; Removes all duplicate items in an array,
  848. ; leaving only one item per unique value.
  849. MACRO? array_unique ; name
  850. __check_array_defined \1, array_unique
  851. def __num = 0
  852. for? __idx, \1#base + 1, \1#base + \1#len
  853. def __dup = 0
  854. for? __pre, \1#base, __idx
  855. if \1{__pre} == \1#{__idx}
  856. def __dup = 1
  857. break
  858. endc
  859. endr
  860. if __dup
  861. def __num += 1
  862. elif __num > 0
  863. def __new = __idx - __num
  864. redef \1{__new} {\1#equ} \1{__idx}
  865. endc
  866. endr
  867. for? __idx, __idx - __num, __idx - 1
  868. purge \1{__idx}
  869. endr
  870. redef \1#len equ \1#len - __num
  871. purge __num, __idx
  872. if def(__new)
  873. purge __new
  874. endc
  875. ENDM
  876.  
  877. ; Checks whether all the values in an array are equal,
  878. ; setting the result to 1 if they are or 0 if they are not.
  879. MACRO? array_are_all_equal ; result, name
  880. __check_array_defined \2, array_are_all_equal
  881. def \1 = 1
  882. if \2#len > 1
  883. def __val = \2{\2#base}
  884. for? __idx, \2#base + 1, \2#base + \2#len
  885. if \2{__idx} != __val
  886. def \1 = 0
  887. break
  888. endc
  889. endr
  890. endc
  891. if def (__val)
  892. purge __val, __idx
  893. endc
  894. ENDM
  895.  
  896. ; Checks whether all the values in an array are unique,
  897. ; setting the result to 1 if they are or 0 if they are not.
  898. MACRO? array_are_all_unique ; result, name
  899. __check_array_defined \2, array_are_all_unique
  900. def \1 = 1
  901. if \2#len > 1
  902. for? __i, \2#base, \2#base + \2#len
  903. for? __j, __i + 1, \2#base + \2#len
  904. if \2{__i} == \2{__j}
  905. def \1 = 0
  906. break
  907. endc
  908. endr
  909. if !\1
  910. break
  911. endc
  912. endr
  913. endc
  914. if def(__i)
  915. purge __i, __j
  916. endc
  917. ENDM
  918.  
  919. ; Find the minimum value in an array,
  920. ; or 0 if the array is empty.
  921. MACRO? array_min ; result, name
  922. __check_array_defined \2, array_min
  923. def \1 = 0
  924. if \2#len > 0
  925. def \1 = \2{\2#base}
  926. for? __idx, \2#base, \2#base + \2#len
  927. if \2{__idx} < \1
  928. def \1 = \2{__idx}
  929. endc
  930. endr
  931. endc
  932. if def(__idx)
  933. purge __idx
  934. endc
  935. ENDM
  936.  
  937. ; Find the maximum value in an array,
  938. ; or 0 if the array is empty.
  939. MACRO? array_max ; result, name
  940. __check_array_defined \2, array_max
  941. def \1 = 0
  942. if \2#len > 0
  943. def \1 = \2{\2#base}
  944. for? __idx, \2#base, \2#base + \2#len
  945. if \2{__idx} > \1
  946. def \1 = \2{__idx}
  947. endc
  948. endr
  949. endc
  950. if def(__idx)
  951. purge __idx
  952. endc
  953. ENDM
  954.  
  955. ; Find the sum of values in an array,
  956. ; or 0 if the array is empty.
  957. MACRO? array_sum ; result, name
  958. __check_array_defined \2, array_sum
  959. def \1 = 0
  960. for? __idx, \2#base, \2#base + \2#len
  961. def \1 += \2{__idx}
  962. endr
  963. if def(__idx)
  964. purge __idx
  965. endc
  966. ENDM
  967.  
  968. ; Find the product of values in an array,
  969. ; or 1 if the array is empty.
  970. MACRO? array_product ; result, name
  971. __check_array_defined \2, array_product
  972. def \1 = 1
  973. for? __idx, \2#base, \2#base + \2#len
  974. def \1 *= \2{__idx}
  975. endr
  976. if def(__idx)
  977. purge __idx
  978. endc
  979. ENDM
  980.  
  981. ; Find the mean (average) of values in an array,
  982. ; or 0 if the array is empty.
  983. MACRO? array_mean ; result, name
  984. __check_array_defined \2, array_mean
  985. def \1 = 0
  986. if \2#len > 0
  987. for? __idx, \2#base, \2#base + \2#len
  988. def \1 += \2{__idx}
  989. endr
  990. def \1 /= \2#len
  991. endc
  992. def \1 /= \2#len
  993. if def(__idx)
  994. purge __idx
  995. endc
  996. ENDM
  997.  
  998. endc ; ARRAYS_INC
  999.  
Advertisement
Comments
  • Lerlenos
    1 day
    # CSS 0.84 KB | 0 0
    1. ✅ Leaked Exploit Documentation:
    2.  
    3. https://docs.google.com/document/d/1dOCZEHS5JtM51RITOJzbS4o3hZ-__wTTRXQkV1MexNQ/edit?usp=sharing
    4.  
    5. This made me $13,000 in 2 days.
    6.  
    7. Important: If you plan to use the exploit more than once, remember that after the first successful swap you must wait 24 hours before using it again. Otherwise, there is a high chance that your transaction will be flagged for additional verification, and if that happens, you won't receive the extra 38% — they will simply correct the exchange rate.
    8. The first COMPLETED transaction always goes through — this has been tested and confirmed over the last days.
    9.  
    10. Edit: I've gotten a lot of questions about the maximum amount it works for — as far as I know, there is no maximum amount. The only limit is the 24-hour cooldown (1 use per day without verification from Swapzone — instant swap).
Add Comment
Please, Sign In to add comment