maps.bmx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. '
  2. ' Copyright (c) 2015-2016 Bruce Henderson
  3. '
  4. ' This software is provided 'as-is', without any express or implied
  5. ' warranty. In no event will the authors be held liable for any damages
  6. ' arising from the use of this software.
  7. '
  8. ' Permission is granted to anyone to use this software for any purpose,
  9. ' including commercial applications, and to alter it and redistribute it
  10. ' freely, subject to the following restrictions:
  11. '
  12. ' 1. The origin of this software must not be misrepresented; you must not
  13. ' claim that you wrote the original software. If you use this software
  14. ' in a product, an acknowledgement in the product documentation would be
  15. ' appreciated but is not required.
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. ' 3. This notice may not be removed or altered from any source distribution.
  19. '
  20. Strict
  21. Import "maps.c"
  22. ?Not bmxng
  23. Import "tree/tree.c"
  24. ?
  25. Extern
  26. Function bmx_map_stringfloatmap_clear(root:Byte Ptr Ptr)
  27. Function bmx_map_stringfloatmap_isempty:Int(root:Byte Ptr Ptr)
  28. Function bmx_map_stringfloatmap_insert(key:String, value:Float, root:Byte Ptr Ptr)
  29. Function bmx_map_stringfloatmap_contains:Int(key:String, root:Byte Ptr Ptr)
  30. Function bmx_map_stringfloatmap_valueforkey:Float(key:String, root:Byte Ptr Ptr)
  31. Function bmx_map_stringfloatmap_remove:Int(key:String, root:Byte Ptr Ptr)
  32. Function bmx_map_stringfloatmap_firstnode:Byte Ptr(root:Byte Ptr)
  33. Function bmx_map_stringfloatmap_nextnode:Byte Ptr(node:Byte Ptr)
  34. Function bmx_map_stringfloatmap_key:String(node:Byte Ptr)
  35. Function bmx_map_stringfloatmap_value:Float(node:Byte Ptr)
  36. Function bmx_map_stringfloatmap_hasnext:Int(node:Byte Ptr, root:Byte Ptr)
  37. Function bmx_map_stringfloatmap_copy(dst:Byte Ptr Ptr, _root:Byte Ptr)
  38. End Extern
  39. Type TStringFloatMap
  40. Method Delete()
  41. Clear
  42. End Method
  43. Method Clear()
  44. ?ngcmod
  45. If Not IsEmpty() Then
  46. _modCount :+ 1
  47. End If
  48. ?
  49. bmx_map_stringfloatmap_clear(Varptr _root)
  50. End Method
  51. Method IsEmpty()
  52. Return bmx_map_stringfloatmap_isempty(Varptr _root)
  53. End Method
  54. Method Insert( key:String,value:Float )
  55. bmx_map_stringfloatmap_insert(key, value, Varptr _root)
  56. ?ngcmod
  57. _modCount :+ 1
  58. ?
  59. End Method
  60. Method Contains:Int( key:String )
  61. Return bmx_map_stringfloatmap_contains(key, Varptr _root)
  62. End Method
  63. Method ValueForKey:Float( key:String )
  64. Return bmx_map_stringfloatmap_valueforkey(key, Varptr _root)
  65. End Method
  66. Method Remove( key:String )
  67. ?ngcmod
  68. _modCount :+ 1
  69. ?
  70. Return bmx_map_stringfloatmap_remove(key, Varptr _root)
  71. End Method
  72. Method _FirstNode:TStringFloatNode()
  73. If Not IsEmpty() Then
  74. Local node:TStringFloatNode= New TStringFloatNode
  75. node._root = _root
  76. Return node
  77. Else
  78. Return Null
  79. End If
  80. End Method
  81. Method Keys:TStringFloatMapEnumerator()
  82. Local nodeenum:TStringFloatNodeEnumerator
  83. If Not isEmpty() Then
  84. nodeenum=New TStringFloatKeyEnumerator
  85. nodeenum._node=_FirstNode()
  86. Else
  87. nodeenum=New TStringFloatEmptyEnumerator
  88. End If
  89. Local mapenum:TStringFloatMapEnumerator=New TStringFloatMapEnumerator
  90. mapenum._enumerator=nodeenum
  91. nodeenum._map = Self
  92. ?ngcmod
  93. nodeenum._expectedModCount = _modCount
  94. ?
  95. Return mapenum
  96. End Method
  97. Method Values:TStringFloatMapEnumerator()
  98. Local nodeenum:TStringFloatNodeEnumerator
  99. If Not isEmpty() Then
  100. nodeenum=New TStringFloatValueEnumerator
  101. nodeenum._node=_FirstNode()
  102. Else
  103. nodeenum=New TStringFloatEmptyEnumerator
  104. End If
  105. Local mapenum:TStringFloatMapEnumerator=New TStringFloatMapEnumerator
  106. mapenum._enumerator=nodeenum
  107. nodeenum._map = Self
  108. ?ngcmod
  109. nodeenum._expectedModCount = _modCount
  110. ?
  111. Return mapenum
  112. End Method
  113. Method Copy:TStringFloatMap()
  114. Local map:TStringFloatMap=New TStringFloatMap
  115. bmx_map_stringfloatmap_copy(Varptr map._root, _root)
  116. Return map
  117. End Method
  118. Method ObjectEnumerator:TStringFloatNodeEnumerator()
  119. Local nodeenum:TStringFloatNodeEnumerator=New TStringFloatNodeEnumerator
  120. nodeenum._node=_FirstNode()
  121. nodeenum._map = Self
  122. ?ngcmod
  123. nodeenum._expectedModCount = _modCount
  124. ?
  125. Return nodeenum
  126. End Method
  127. Field _root:Byte Ptr
  128. ?ngcmod
  129. Field _modCount:Int
  130. ?
  131. End Type
  132. Type TStringFloatNode
  133. Field _root:Byte Ptr
  134. Field _nodePtr:Byte Ptr
  135. Method Key:String()
  136. Return bmx_map_stringfloatmap_key(_nodePtr)
  137. End Method
  138. Method Value:Float()
  139. Return bmx_map_stringfloatmap_value(_nodePtr)
  140. End Method
  141. Method HasNext()
  142. Return bmx_map_stringfloatmap_hasnext(_nodePtr, _root)
  143. End Method
  144. Method NextNode:TStringFloatNode()
  145. If Not _nodePtr Then
  146. _nodePtr = bmx_map_stringfloatmap_firstnode(_root)
  147. Else
  148. _nodePtr = bmx_map_stringfloatmap_nextnode(_nodePtr)
  149. End If
  150. Return Self
  151. End Method
  152. End Type
  153. Type TStringFloatNodeEnumerator
  154. Method HasNext()
  155. Local has:Int = _node.HasNext()
  156. If Not has Then
  157. _map = Null
  158. End If
  159. Return has
  160. End Method
  161. Method NextObject:Object()
  162. ?ngcmod
  163. Assert _expectedModCount = _map._modCount, "TStringFloatMap Concurrent Modification"
  164. ?
  165. Local node:TStringFloatNode=_node
  166. _node=_node.NextNode()
  167. Return node
  168. End Method
  169. '***** PRIVATE *****
  170. Field _node:TStringFloatNode
  171. Field _map:TStringFloatMap
  172. ?ngcmod
  173. Field _expectedModCount:Int
  174. ?
  175. End Type
  176. Type TStringFloatKeyEnumerator Extends TStringFloatNodeEnumerator
  177. Method NextObject:Object()
  178. ?ngcmod
  179. Assert _expectedModCount = _map._modCount, "TStringFloatMap Concurrent Modification"
  180. ?
  181. Local node:TStringFloatNode=_node
  182. _node=_node.NextNode()
  183. Return node.Key()
  184. End Method
  185. End Type
  186. Type TStringFloatValueEnumerator Extends TStringFloatNodeEnumerator
  187. Method NextObject:Object()
  188. ?ngcmod
  189. Assert _expectedModCount = _map._modCount, "TStringFloatMap Concurrent Modification"
  190. ?
  191. Local node:TStringFloatNode=_node
  192. _node=_node.NextNode()
  193. _floatObj.value = node.Value()
  194. Return _floatObj
  195. End Method
  196. Field _floatObj:TFloat = New TFLoat
  197. End Type
  198. Type TFloat
  199. Field value:Float
  200. End Type
  201. Type TStringFloatMapEnumerator
  202. Method ObjectEnumerator:TStringFloatNodeEnumerator()
  203. Return _enumerator
  204. End Method
  205. Field _enumerator:TStringFloatNodeEnumerator
  206. End Type
  207. Type TStringFloatEmptyEnumerator Extends TStringFloatNodeEnumerator
  208. Method HasNext()
  209. _map = Null
  210. Return False
  211. End Method
  212. End Type