deque.monkey2 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. Namespace std.collections
  2. #rem monkeydoc Convenience type alias for Deque\<Int\>.
  3. #end
  4. Alias IntDeque:Deque<Int>
  5. #rem monkeydoc Convenience type alias for Deque\<Float\>.
  6. #end
  7. Alias FloatDeque:Deque<Float>
  8. #rem monkeydoc Convenience type alias for Deque\<String\>.
  9. #end
  10. Alias StringDeque:Deque<String>
  11. #rem monkeydoc The Deque class.
  12. A deque is a 'double ended queue' (usually pronounced 'deck').
  13. You can efficiently add items to either end of a deque using [[AddFirst]] and [[AddLast]], and remove items using [[RemoveFirst]] and [[RemoveLast]].
  14. Deques implement the [[IContainer]] interface so can be used with Eachin loops.
  15. Note that you should NOT modify a deque while iterating through it with an eachin loop. Doing so while cause a 'concurrent deque modification' runtime error in debug mode. Please see [[IContainer]] for more information.
  16. #end
  17. Class Deque<T> Implements IContainer<T>
  18. #rem monkeydoc The Deque.Iterator struct.
  19. #end
  20. Struct Iterator Implements IIterator<T>
  21. Private
  22. Field _deque:Deque
  23. Field _index:Int
  24. Method AssertCurrent()
  25. DebugAssert( _index<>_deque._tail,"Invalid deque iterator" )
  26. End
  27. Method New( deque:Deque,index:Int )
  28. _deque=deque
  29. _index=index
  30. End
  31. Public
  32. #rem monkeydoc Checks if the iterator has reached the end of the deque.
  33. #end
  34. Property AtEnd:Bool()
  35. Return _index=_deque._tail
  36. End
  37. #rem monkeydoc The value currently pointed to by the iterator.
  38. #end
  39. Property Current:T()
  40. AssertCurrent()
  41. Return _deque._data[_index]
  42. Setter( current:T )
  43. AssertCurrent()
  44. _deque._data[_index]=current
  45. End
  46. #rem monkeydoc Bumps the iterator so it points to the next value in the deqeue.
  47. #end
  48. Method Bump()
  49. AssertCurrent()
  50. _index+=1
  51. If _index=_deque.Capacity _index=0
  52. End
  53. Method Erase()
  54. RuntimeError( "Erase not supported for Deques" )
  55. End
  56. #rem monkeydoc Safely inserts a value before the value pointed to by the iterator.
  57. After calling this method, the iterator will point to the newly added value.
  58. #end
  59. Method Insert( value:T )
  60. RuntimeError( "Insert not supported for Deques" )
  61. End
  62. End
  63. Private
  64. Field _data:T[]
  65. Field _head:Int
  66. Field _tail:Int
  67. Method Normalize( capacity:Int )
  68. Local length:=Length
  69. Local data:=New T[capacity]
  70. If _head<=_tail
  71. _data.CopyTo( data,_head,0,length )
  72. Else
  73. Local n:=Capacity-_head
  74. _data.CopyTo( data,_head,0,n )
  75. _data.CopyTo( data,0,n,_tail )
  76. Endif
  77. _data=data
  78. _tail=length
  79. _head=0
  80. End
  81. Public
  82. #rem monkeydoc Creates a new deque.
  83. @param length Initalize length of the deque.
  84. #end
  85. Method New()
  86. _data=New T[10]
  87. End
  88. Method New( length:Int )
  89. _tail=length
  90. _data=New T[_tail+1]
  91. End
  92. #rem monkeydoc True if deque is empty.
  93. #end
  94. Property Empty:Bool()
  95. Return _head=_tail
  96. End
  97. #rem monkeydoc Gets the storage capacity of the deque.
  98. The capacity of a deque is the number of values it can contain before memory needs to be reallocated to store more values.
  99. If a deque's length equals its capacity, then the next Add or Insert operation will need to allocate more memory to 'grow' the deque.
  100. You don't normally need to worry about deque capacity, but it can be useful to [[Reserve]] deque storage if you know in advance
  101. how many values a deque is likely to contain, in order to prevent the overhead of excessive memory allocation.
  102. @return The current deque capacity.
  103. #end
  104. Property Capacity:Int()
  105. Return _data.Length
  106. End
  107. #rem monkeydoc Gets the number of values in the deque.
  108. @return The number of values in the deque.
  109. #end
  110. Property Length:Int()
  111. If _head<=_tail Return _tail-_head
  112. Return Capacity-_head+_tail
  113. End
  114. #rem monkeydoc Gets the underlying array used by the deque.
  115. Note that the returned array may be longer than the deque length.
  116. @return The array used internally by the deque.
  117. #end
  118. Property Data:T[]()
  119. If Not _head Return _data
  120. Normalize( Capacity )
  121. Return _data
  122. End
  123. #rem monkeydoc Gets an iterator for visiting deque values.
  124. Returns an iterator suitable for use with Eachin, or for manual iteration.
  125. @return A deque iterator.
  126. #end
  127. Method All:Iterator()
  128. Return New Iterator( Self,_head )
  129. End
  130. #rem monkeydoc Converts the deque to an array.
  131. @return An array containing each element of the deque.
  132. #end
  133. Method ToArray:T[]()
  134. Local data:=New T[Length]
  135. If _head<=_tail
  136. _data.CopyTo( data,_head,0,Length )
  137. Else
  138. Local n:=Capacity-_head
  139. _data.CopyTo( data,_head,0,n )
  140. _data.CopyTo( data,0,n,_tail )
  141. Endif
  142. Return data
  143. End
  144. #rem monkeydoc Reserves deque storage capacity.
  145. The capacity of a deque is the number of values it can contain before memory needs to be reallocated to store more values.
  146. If a deque's length equals its capacity, then the next Add, Insert or Push operation will need to allocate more memory to 'grow' the deque.
  147. You don't normally need to worry about deque capacity, but it can be useful to use [[Reserve]] to preallocate deque storage if you know in advance how many values a deque is likely to contain, in order to prevent excessive memory allocation.
  148. @param capacity The new capacity.
  149. #end
  150. Method Reserve( capacity:Int )
  151. DebugAssert( capacity>=0 )
  152. If Capacity>=capacity Return
  153. capacity=Max( Length*2+Length,capacity )
  154. Normalize( capacity )
  155. End
  156. #rem monkeydoc Clears the deque.
  157. #end
  158. Method Clear()
  159. If _head<=_tail
  160. For Local i:=_head Until _tail
  161. _data[i]=Null
  162. Next
  163. Else
  164. For Local i:=0 Until _tail
  165. _data[i]=Null
  166. Next
  167. For Local i:=_head Until Capacity
  168. _data[i]=Null
  169. Next
  170. Endif
  171. _head=0
  172. _tail=0
  173. End
  174. #rem monkeydoc Adds a value at the start of the deque.
  175. #end
  176. Method AddFirst( value:T )
  177. If Length+1=Capacity Reserve( Capacity+1 )
  178. _head-=1
  179. If _head=-1 _head=Capacity-1
  180. _data[_head]=value
  181. End
  182. #rem monkeydoc Adds a value at the end of the deque.
  183. #end
  184. Method AddLast( value:T )
  185. If Length+1=Capacity Reserve( Capacity+1 )
  186. _data[_tail]=value
  187. _tail+=1
  188. If _tail=Capacity _tail=0
  189. End
  190. #rem monkeydoc @deprecated Use [[AddFirst]]
  191. #end
  192. Method PushFirst( value:T )
  193. AddFirst( value)
  194. End
  195. #rem monkeydoc @deprecated Use [[AddLast]]
  196. #end
  197. Method PushLast( value:T )
  198. AddLast( value)
  199. End
  200. #rem monkeydoc Removes and returns the first value in a deque.
  201. In debug builds, a runtime error will occur if the deque is empty.
  202. #end
  203. Method RemoveFirst:T()
  204. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  205. Local value:=_data[_head]
  206. _data[_head]=Null
  207. _head+=1
  208. If _head=Capacity _head=0
  209. Return value
  210. End
  211. #rem monkeydoc Removes and returns the last value in a deque.
  212. In debug builds, a runtime error will occur if the deque is empty.
  213. #end
  214. Method RemoveLast:T()
  215. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  216. _tail-=1
  217. If _tail=-1 _tail=Capacity-1
  218. Local value:=_data[_tail]
  219. _data[_tail]=Null
  220. Return value
  221. End
  222. #rem monkeydoc @deprecated use [[RemoveFirst]].
  223. #end
  224. Method PopFirst:T()
  225. Return RemoveFirst()
  226. End
  227. #rem monkeydoc @deprecated use [[RemoveLast]].
  228. #end
  229. Method PopLast:T()
  230. Return RemoveLast()
  231. End
  232. #rem monkeydoc Returns the first value in the deque.
  233. In debug builds, a runtime error will occur if the deque is empty.
  234. #end
  235. Method First:T()
  236. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  237. Return _data[_head]
  238. End
  239. #rem monkeydoc Returns the last value in the deque.
  240. In debug builds, a runtime error will occur if the deque is empty.
  241. #end
  242. Method Last:T()
  243. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  244. Return _data[ _tail>0 ? _tail-1 Else Capacity-1 ]
  245. End
  246. #rem monkedoc Gets the value of a deque element.
  247. In debug builds, a runtime error will occur if `index` is less than 0, or greater than or equal to the length of the deque.
  248. #end
  249. Method Get:T( index:Int )
  250. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  251. Return _data[ (index+_head) Mod Capacity ]
  252. End
  253. #rem monkedoc Sets the value of a deque element.
  254. In debug builds, a runtime error will occur if `index` is less than 0, or greater than or equal to the length of the deque.
  255. #end
  256. Method Set( index:Int,value:T )
  257. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  258. _data[ (index+_head) Mod Capacity ]=value
  259. End
  260. #rem monkedoc Gets the value of a deque element.
  261. In debug builds, a runtime error will occur if `index` is less than 0, or greater than or equal to the length of the deque.
  262. #end
  263. Operator[]:T( index:Int )
  264. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  265. Return _data[ (index+_head) Mod Capacity ]
  266. End
  267. #rem monkedoc Sets the value of a deque element.
  268. In debug builds, a runtime error will occur if `index` is less than 0, or greater than or equal to the length of the deque.
  269. #end
  270. Operator[]=( index:Int,value:T )
  271. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  272. _data[ (index+_head) Mod Capacity ]=value
  273. End
  274. End