intmap.bmx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. SuperStrict
  2. Import "common.bmx"
  3. Extern
  4. Function bmx_map_intmap_clear(root:SavlRoot Ptr Ptr)
  5. Function bmx_map_intmap_isempty:Int(root:SavlRoot Ptr)
  6. Function bmx_map_intmap_insert(key:Int, value:Object, root:SavlRoot Ptr Ptr)
  7. Function bmx_map_intmap_contains:Int(key:Int, root:SavlRoot Ptr)
  8. Function bmx_map_intmap_valueforkey:Object(key:Int, root:SavlRoot Ptr)
  9. Function bmx_map_intmap_remove:Int(key:Int, root:SavlRoot Ptr Ptr)
  10. Function bmx_map_intmap_firstnode:SIntMapNode Ptr(root:SavlRoot Ptr)
  11. Function bmx_map_intmap_nextnode:SIntMapNode Ptr(node:SIntMapNode Ptr)
  12. Function bmx_map_intmap_key:Int(node:SIntMapNode Ptr)
  13. Function bmx_map_intmap_value:Object(node:SIntMapNode Ptr)
  14. Function bmx_map_intmap_hasnext:Int(node:SIntMapNode Ptr, root:SavlRoot Ptr)
  15. Function bmx_map_intmap_copy(dst:SavlRoot Ptr Ptr, _root:SavlRoot Ptr)
  16. End Extern
  17. Struct SIntMapNode
  18. Field link:SavlRoot
  19. Field key:Int
  20. Field value:Object
  21. End Struct
  22. Rem
  23. bbdoc: A key/value (Int/Object) map.
  24. End Rem
  25. Type TIntMap
  26. Method Delete()
  27. Clear
  28. End Method
  29. Rem
  30. bbdoc: Clears the map.
  31. about: Removes all keys and values.
  32. End Rem
  33. Method Clear()
  34. ?ngcmod
  35. If Not IsEmpty() Then
  36. _modCount :+ 1
  37. End If
  38. ?
  39. bmx_map_intmap_clear(Varptr _root)
  40. End Method
  41. Rem
  42. bbdoc: Checks if the map is empty.
  43. about: #True if @map is empty, otherwise #False.
  44. End Rem
  45. Method IsEmpty:Int()
  46. Return bmx_map_intmap_isempty(_root)
  47. End Method
  48. Rem
  49. bbdoc: Inserts a key/value pair into the map.
  50. about: If the map already contains @key, its value is overwritten with @value.
  51. End Rem
  52. Method Insert( key:Int,value:Object )
  53. bmx_map_intmap_insert(key, value, Varptr _root)
  54. ?ngcmod
  55. _modCount :+ 1
  56. ?
  57. End Method
  58. Rem
  59. bbdoc: Checks if the map contains @key.
  60. returns: #True if the map contains @key.
  61. End Rem
  62. Method Contains:Int( key:Int )
  63. Return bmx_map_intmap_contains(key, _root)
  64. End Method
  65. Rem
  66. bbdoc: Finds a value given a @key.
  67. returns: The value associated with @key.
  68. about: If the map does not contain @key, a #Null object is returned.
  69. End Rem
  70. Method ValueForKey:Object( key:Int )
  71. Return bmx_map_intmap_valueforkey(key, _root)
  72. End Method
  73. Rem
  74. bbdoc: Remove a key/value pair from the map.
  75. returns: #True if @key was removed, or #False otherwise.
  76. End Rem
  77. Method Remove:Int( key:Int )
  78. ?ngcmod
  79. _modCount :+ 1
  80. ?
  81. Return bmx_map_intmap_remove(key, Varptr _root)
  82. End Method
  83. Method _FirstNode:TIntNode()
  84. If Not IsEmpty() Then
  85. Local node:TIntNode= New TIntNode
  86. node._root = _root
  87. Return node
  88. Else
  89. Return Null
  90. End If
  91. End Method
  92. Rem
  93. bbdoc: Gets the map keys.
  94. returns: An enumeration object
  95. about: The object returned by #Keys can be used with #EachIn to iterate through the keys in the map.
  96. End Rem
  97. Method Keys:TIntMapEnumerator()
  98. Local nodeenum:TIntNodeEnumerator
  99. If Not isEmpty() Then
  100. nodeenum=New TIntKeyEnumerator
  101. nodeenum._node=_FirstNode()
  102. Else
  103. nodeenum=New TIntEmptyEnumerator
  104. End If
  105. Local mapenum:TIntMapEnumerator=New TIntMapEnumerator
  106. mapenum._enumerator=nodeenum
  107. nodeenum._map = Self
  108. ?ngcmod
  109. nodeenum._expectedModCount = _modCount
  110. ?
  111. Return mapenum
  112. End Method
  113. Rem
  114. bbdoc: Get the map values.
  115. returns: An enumeration object.
  116. about: The object returned by #Values can be used with #EachIn to iterate through the values in the map.
  117. End Rem
  118. Method Values:TIntMapEnumerator()
  119. Local nodeenum:TIntNodeEnumerator
  120. If Not isEmpty() Then
  121. nodeenum=New TIntValueEnumerator
  122. nodeenum._node=_FirstNode()
  123. Else
  124. nodeenum=New TIntEmptyEnumerator
  125. End If
  126. Local mapenum:TIntMapEnumerator=New TIntMapEnumerator
  127. mapenum._enumerator=nodeenum
  128. nodeenum._map = Self
  129. ?ngcmod
  130. nodeenum._expectedModCount = _modCount
  131. ?
  132. Return mapenum
  133. End Method
  134. Rem
  135. bbdoc: Returns a copy the contents of this map.
  136. End Rem
  137. Method Copy:TIntMap()
  138. Local map:TIntMap=New TIntMap
  139. bmx_map_intmap_copy(Varptr map._root, _root)
  140. Return map
  141. End Method
  142. Rem
  143. bbdoc: Returns a node enumeration object.
  144. about: The object returned by #ObjectEnumerator can be used with #EachIn to iterate through the nodes in the map.
  145. End Rem
  146. Method ObjectEnumerator:TIntNodeEnumerator()
  147. Local nodeenum:TIntNodeEnumerator
  148. If Not isEmpty() Then
  149. nodeenum = New TIntNodeEnumerator
  150. nodeenum._node=_FirstNode()
  151. nodeenum._map = Self
  152. Else
  153. nodeenum=New TIntEmptyEnumerator
  154. End If
  155. Return nodeenum
  156. End Method
  157. Rem
  158. bbdoc: Finds a value given a @key using index syntax.
  159. returns: The value associated with @key.
  160. about: If the map does not contain @key, a #Null object is returned.
  161. End Rem
  162. Method Operator[]:Object(key:Int)
  163. Return bmx_map_intmap_valueforkey(key, _root)
  164. End Method
  165. Rem
  166. bbdoc: Inserts a key/value pair into the map using index syntax.
  167. about: If the map already contains @key, its value is overwritten with @value.
  168. End Rem
  169. Method Operator[]=(key:Int, value:Object)
  170. bmx_map_intmap_insert(key, value, Varptr _root)
  171. End Method
  172. Field _root:SavlRoot Ptr
  173. ?ngcmod
  174. Field _modCount:Int
  175. ?
  176. End Type
  177. Type TIntNode
  178. Field _root:SavlRoot Ptr
  179. Field _nodePtr:SIntMapNode Ptr
  180. Field _nextNode:SIntMapNode Ptr
  181. Method Key:Int()
  182. Return bmx_map_intmap_key(_nodePtr)
  183. End Method
  184. Method Value:Object()
  185. Return bmx_map_intmap_value(_nodePtr)
  186. End Method
  187. Method HasNext:Int()
  188. Return bmx_map_intmap_hasnext(_nodePtr, _root)
  189. End Method
  190. Method NextNode:TIntNode()
  191. If Not _nodePtr Then
  192. _nodePtr = bmx_map_intmap_firstnode(_root)
  193. Else
  194. '_nodePtr = bmx_map_intmap_nextnode(_nodePtr)
  195. _nodePtr = _nextNode
  196. End If
  197. If HasNext() Then
  198. _nextNode = bmx_map_intmap_nextnode(_nodePtr)
  199. End If
  200. Return Self
  201. End Method
  202. Method Remove()
  203. End Method
  204. End Type
  205. Rem
  206. bbdoc: Int holder for key returned by TIntMap.Keys() enumerator.
  207. about: Because a single instance of #TIntKey is used during enumeration, #value changes on each iteration.
  208. End Rem
  209. Type TIntKey
  210. Rem
  211. bbdoc: Int key value.
  212. End Rem
  213. Field value:Int
  214. End Type
  215. Type TIntNodeEnumerator
  216. Method HasNext:Int()
  217. Local has:Int = _node.HasNext()
  218. If Not has Then
  219. _map = Null
  220. End If
  221. Return has
  222. End Method
  223. Method NextObject:Object()
  224. ?ngcmod
  225. Assert _expectedModCount = _map._modCount, "TIntMap Concurrent Modification"
  226. ?
  227. Local node:TIntNode=_node
  228. _node=_node.NextNode()
  229. Return node
  230. End Method
  231. '***** PRIVATE *****
  232. Field _node:TIntNode
  233. Field _map:TIntMap
  234. ?ngcmod
  235. Field _expectedModCount:Int
  236. ?
  237. End Type
  238. Type TIntKeyEnumerator Extends TIntNodeEnumerator
  239. Field _key:TIntKey = New TIntKey
  240. Method NextObject:Object() Override
  241. ?ngcmod
  242. Assert _expectedModCount = _map._modCount, "TIntMap Concurrent Modification"
  243. ?
  244. Local node:TIntNode=_node
  245. _node=_node.NextNode()
  246. _key.value = node.Key()
  247. Return _key
  248. End Method
  249. End Type
  250. Type TIntValueEnumerator Extends TIntNodeEnumerator
  251. Method NextObject:Object() Override
  252. ?ngcmod
  253. Assert _expectedModCount = _map._modCount, "TIntMap Concurrent Modification"
  254. ?
  255. Local node:TIntNode=_node
  256. _node=_node.NextNode()
  257. Return node.Value()
  258. End Method
  259. End Type
  260. Type TIntMapEnumerator
  261. Method ObjectEnumerator:TIntNodeEnumerator()
  262. Return _enumerator
  263. End Method
  264. Field _enumerator:TIntNodeEnumerator
  265. End Type
  266. Type TIntEmptyEnumerator Extends TIntNodeEnumerator
  267. Method HasNext:Int() Override
  268. _map = Null
  269. Return False
  270. End Method
  271. End Type