stringmap.bmx 7.0 KB

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