avl_map.g 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # This file is part of asmc, a bootstrapping OS with minimal seed
  2. # Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. # https://gitlab.com/giomasce/asmc
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. const AVL_LEFT 0
  15. const AVL_RIGHT 4
  16. const AVL_KEY 8
  17. const AVL_VALUE 12
  18. const AVL_PARENT 16
  19. const SIZEOF_AVL 20
  20. fun avl_init 2 {
  21. $key
  22. $parent
  23. @key 1 param = ;
  24. @parent 0 param = ;
  25. $avl
  26. @avl SIZEOF_AVL malloc = ;
  27. avl AVL_LEFT take_addr 0 = ;
  28. avl AVL_RIGHT take_addr 0 = ;
  29. avl AVL_KEY take_addr key strdup = ;
  30. avl AVL_VALUE take_addr 0 = ;
  31. avl AVL_PARENT take_addr parent = ;
  32. avl ret ;
  33. }
  34. fun avl_destroy 1 {
  35. $avl
  36. @avl 0 param = ;
  37. if avl AVL_LEFT take 0 != {
  38. avl AVL_LEFT take avl_destroy ;
  39. }
  40. if avl AVL_RIGHT take 0 != {
  41. avl AVL_RIGHT take avl_destroy ;
  42. }
  43. avl AVL_KEY take free ;
  44. avl free ;
  45. }
  46. const MAP_AVL 0
  47. const MAP_SIZE 4
  48. const SIZEOF_MAP 8
  49. fun map_init 0 {
  50. $map
  51. @map SIZEOF_MAP malloc = ;
  52. map MAP_AVL take_addr 0 = ;
  53. map MAP_SIZE take_addr 0 = ;
  54. map ret ;
  55. }
  56. fun map_destroy 1 {
  57. $map
  58. @map 0 param = ;
  59. if map MAP_AVL take 0 != {
  60. map MAP_AVL take avl_destroy ;
  61. }
  62. map free ;
  63. }
  64. fun _map_find 3 {
  65. $map
  66. $key
  67. $create
  68. @map 2 param = ;
  69. @key 1 param = ;
  70. @create 0 param = ;
  71. $avl
  72. @avl map MAP_AVL take = ;
  73. $ptr
  74. @ptr map MAP_AVL take_addr = ;
  75. $parent
  76. @parent 0 = ;
  77. while 1 {
  78. if avl 0 == {
  79. if create {
  80. @avl key parent avl_init = ;
  81. ptr avl = ;
  82. map MAP_SIZE take_addr map MAP_SIZE take 1 + = ;
  83. avl ret ;
  84. } else {
  85. 0 ret ;
  86. }
  87. }
  88. $cmp
  89. @cmp key avl AVL_KEY take strcmp = ;
  90. if cmp 0 == {
  91. avl ret ;
  92. }
  93. if cmp 0 < {
  94. @ptr avl AVL_LEFT take_addr = ;
  95. @parent avl = ;
  96. @avl avl AVL_LEFT take = ;
  97. } else {
  98. @ptr avl AVL_RIGHT take_addr = ;
  99. @parent avl = ;
  100. @avl avl AVL_RIGHT take = ;
  101. }
  102. }
  103. }
  104. fun map_at 2 {
  105. $map
  106. $key
  107. @map 1 param = ;
  108. @key 0 param = ;
  109. $avl
  110. @avl map key 0 _map_find = ;
  111. avl 0 != "map_at: key does not exist" assert_msg ;
  112. # "map_at(" 1 platform_log ;
  113. # key 1 platform_log ;
  114. # ") = " 1 platform_log ;
  115. # avl AVL_VALUE take itoa 1 platform_log ;
  116. # "\n" 1 platform_log ;
  117. avl AVL_VALUE take ret ;
  118. }
  119. fun map_has 2 {
  120. $map
  121. $key
  122. @map 1 param = ;
  123. @key 0 param = ;
  124. $avl
  125. @avl map key 0 _map_find = ;
  126. # "map_has(" 1 platform_log ;
  127. # key 1 platform_log ;
  128. # ") = " 1 platform_log ;
  129. # avl 0 != itoa 1 platform_log ;
  130. # "\n" 1 platform_log ;
  131. avl 0 != ret ;
  132. }
  133. fun map_set 3 {
  134. $map
  135. $key
  136. $value
  137. @map 2 param = ;
  138. @key 1 param = ;
  139. @value 0 param = ;
  140. $avl
  141. @avl map key 1 _map_find = ;
  142. avl 0 != "map_set: error 1" assert_msg ;
  143. # "map_set(" 1 platform_log ;
  144. # key 1 platform_log ;
  145. # ", " 1 platform_log ;
  146. # value itoa 1 platform_log ;
  147. # ")\n" 1 platform_log ;
  148. avl AVL_VALUE take_addr value = ;
  149. }
  150. fun _map_erase_leaf 2 {
  151. $map
  152. $avl
  153. @map 1 param = ;
  154. @avl 0 param = ;
  155. map MAP_SIZE take_addr map MAP_SIZE take 1 - = ;
  156. avl AVL_LEFT take 0 == "_map_erase_leaf: has left child" assert_msg ;
  157. avl AVL_RIGHT take 0 == "_map_erase_leaf: has right child" assert_msg ;
  158. $parent
  159. @parent avl AVL_PARENT take = ;
  160. avl avl_destroy ;
  161. if parent 0 == {
  162. map MAP_AVL take avl == "_map_erase_leaf: wrong root" assert_msg ;
  163. map MAP_AVL take_addr 0 = ;
  164. ret ;
  165. }
  166. if parent AVL_LEFT take avl == {
  167. parent AVL_LEFT take_addr 0 = ;
  168. ret ;
  169. }
  170. if parent AVL_RIGHT take avl == {
  171. parent AVL_RIGHT take_addr 0 = ;
  172. ret ;
  173. }
  174. 0 "_map_erase_leaf: cannot find among parent's children" assert_msg ;
  175. }
  176. fun _avl_swap 2 {
  177. $avl
  178. $avl2
  179. @avl 1 param = ;
  180. @avl2 0 param = ;
  181. $tmp
  182. @tmp avl AVL_VALUE take = ;
  183. avl AVL_VALUE take_addr avl2 AVL_VALUE take = ;
  184. avl2 AVL_VALUE take_addr tmp = ;
  185. @tmp avl AVL_KEY take = ;
  186. avl AVL_KEY take_addr avl2 AVL_KEY take = ;
  187. avl2 AVL_KEY take_addr tmp = ;
  188. }
  189. fun _map_erase 2 {
  190. $map
  191. $avl
  192. @map 1 param = ;
  193. @avl 0 param = ;
  194. if avl 0 != {
  195. if avl AVL_LEFT take 0 != {
  196. $avl2
  197. @avl2 avl AVL_LEFT take = ;
  198. while avl2 AVL_RIGHT take 0 != {
  199. @avl2 avl2 AVL_RIGHT take = ;
  200. }
  201. avl avl2 _avl_swap ;
  202. map avl2 _map_erase ;
  203. ret ;
  204. }
  205. if avl AVL_RIGHT take 0 != {
  206. $avl2
  207. @avl2 avl AVL_RIGHT take = ;
  208. while avl2 AVL_LEFT take 0 != {
  209. @avl2 avl2 AVL_LEFT take = ;
  210. }
  211. avl avl2 _avl_swap ;
  212. map avl2 _map_erase ;
  213. ret ;
  214. }
  215. map avl _map_erase_leaf ;
  216. }
  217. }
  218. fun map_erase 2 {
  219. $map
  220. $key
  221. @map 1 param = ;
  222. @key 0 param = ;
  223. $avl
  224. @avl map key 0 _map_find = ;
  225. map avl _map_erase ;
  226. # "map_erase(" 1 platform_log ;
  227. # key 1 platform_log ;
  228. # ")\n" 1 platform_log ;
  229. }
  230. fun map_size 1 {
  231. $map
  232. @map 0 param = ;
  233. map MAP_SIZE take ret ;
  234. }
  235. fun _map_foreach 3 {
  236. $avl
  237. $func
  238. $ctx
  239. @avl 2 param = ;
  240. @func 1 param = ;
  241. @ctx 0 param = ;
  242. if avl 0 == {
  243. ret ;
  244. }
  245. avl AVL_LEFT take func ctx _map_foreach ;
  246. ctx avl AVL_KEY take avl AVL_VALUE take func \3 ;
  247. avl AVL_RIGHT take func ctx _map_foreach ;
  248. }
  249. fun map_foreach 3 {
  250. $map
  251. $func
  252. $ctx
  253. @map 2 param = ;
  254. @func 1 param = ;
  255. @ctx 0 param = ;
  256. map MAP_AVL take func ctx _map_foreach ;
  257. }