map.g 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 MAP_ELEM_KEY 0
  15. const MAP_ELEM_VALUE 4
  16. const MAP_ELEM_PRESENT 8
  17. const SIZEOF_MAP_ELEM 12
  18. fun map_init 0 {
  19. SIZEOF_MAP_ELEM vector_init ret ;
  20. }
  21. fun map_destroy 1 {
  22. $ptr
  23. @ptr 0 param = ;
  24. $i
  25. @i 0 = ;
  26. while i ptr vector_size < {
  27. ptr i vector_at_addr MAP_ELEM_KEY take free ;
  28. @i i 1 + = ;
  29. }
  30. ptr vector_destroy ;
  31. }
  32. fun _map_find_idx 2 {
  33. $map
  34. $key
  35. @map 1 param = ;
  36. @key 0 param = ;
  37. $i
  38. @i 0 = ;
  39. while i map vector_size < {
  40. if key map i vector_at_addr MAP_ELEM_KEY take strcmp 0 == {
  41. i ret ;
  42. }
  43. @i i 1 + = ;
  44. }
  45. 0xffffffff ret ;
  46. }
  47. fun map_at 2 {
  48. $map
  49. $key
  50. @map 1 param = ;
  51. @key 0 param = ;
  52. $idx
  53. @idx map key _map_find_idx = ;
  54. idx 0xffffffff != "map_at: key does not exist" assert_msg ;
  55. $addr
  56. @addr map idx vector_at_addr = ;
  57. addr MAP_ELEM_PRESENT take "map_at: element is not present" assert_msg ;
  58. # "map_at(" log ;
  59. # key log ;
  60. # ") = " log ;
  61. # addr MAP_ELEM_VALUE take itoa log ;
  62. # "\n" log ;
  63. addr MAP_ELEM_VALUE take ret ;
  64. }
  65. fun map_has 2 {
  66. $map
  67. $key
  68. @map 1 param = ;
  69. @key 0 param = ;
  70. $idx
  71. @idx map key _map_find_idx = ;
  72. if idx 0xffffffff == {
  73. 0 ret ;
  74. }
  75. $addr
  76. @addr map idx vector_at_addr = ;
  77. addr MAP_ELEM_PRESENT take ret ;
  78. }
  79. fun map_set 3 {
  80. $map
  81. $key
  82. $value
  83. @map 2 param = ;
  84. @key 1 param = ;
  85. @value 0 param = ;
  86. $idx
  87. @idx map key _map_find_idx = ;
  88. $addr
  89. if idx 0xffffffff != {
  90. @addr map idx vector_at_addr = ;
  91. } else {
  92. @idx map 0 vector_push_back = ;
  93. @addr map idx vector_at_addr = ;
  94. addr MAP_ELEM_KEY take_addr key strdup = ;
  95. }
  96. addr MAP_ELEM_PRESENT take_addr 1 = ;
  97. addr MAP_ELEM_VALUE take_addr value = ;
  98. }
  99. fun map_erase 2 {
  100. $map
  101. $key
  102. @map 1 param = ;
  103. @key 0 param = ;
  104. $idx
  105. @idx map key _map_find_idx = ;
  106. if idx 0xffffffff != {
  107. $addr
  108. @addr map idx vector_at_addr = ;
  109. addr MAP_ELEM_PRESENT take_addr 0 = ;
  110. }
  111. }
  112. fun _map_size_closure 3 {
  113. $ctx
  114. $key
  115. $value
  116. @ctx 2 param = ;
  117. @key 1 param = ;
  118. @value 0 param = ;
  119. ctx ctx ** 1 + = ;
  120. }
  121. fun map_foreach 3 {
  122. $map
  123. $func
  124. $ctx
  125. @map 2 param = ;
  126. @func 1 param = ;
  127. @ctx 0 param = ;
  128. $i
  129. @i 0 = ;
  130. while i map vector_size < {
  131. $addr
  132. @addr map i vector_at_addr = ;
  133. if addr MAP_ELEM_PRESENT take {
  134. ctx addr MAP_ELEM_KEY take addr MAP_ELEM_VALUE take func \3 ;
  135. }
  136. @i i 1 + = ;
  137. }
  138. }
  139. fun map_size 1 {
  140. $map
  141. @map 0 param = ;
  142. $size
  143. @size 0 = ;
  144. map @_map_size_closure @size map_foreach ;
  145. size ret ;
  146. }