deque.monkey2 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 [[Deque.AddFirst]] and [[Deque.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. Field _seq:Int
  25. Method AssertSeq()
  26. DebugAssert( _seq=_deque._seq,"Concurrent list modification" )
  27. End
  28. Method AssertCurrent()
  29. DebugAssert( Not AtEnd,"Invalid list iterator" )
  30. End
  31. Method New( deque:Deque,index:Int )
  32. _deque=deque
  33. _index=index
  34. _seq=deque._seq
  35. End
  36. Public
  37. #rem monkeydoc Checks if the iterator has reached the end of the deque.
  38. #end
  39. Property AtEnd:Bool()
  40. AssertSeq()
  41. Return _index=_deque._tail
  42. End
  43. #rem monkeydoc The value currently pointed to by the iterator.
  44. #end
  45. Property Current:T()
  46. AssertCurrent()
  47. Return _deque._data[_index]
  48. Setter( current:T )
  49. AssertCurrent()
  50. _deque._data[_index]=current
  51. End
  52. #rem monkeydoc Bumps the iterator so it points to the next value in the deqeue.
  53. #end
  54. Method Bump()
  55. AssertCurrent()
  56. _index+=1
  57. If _index=_deque.Capacity _index=0
  58. End
  59. Method Erase()
  60. RuntimeError( "Erase not supported for Deques" )
  61. End
  62. #rem monkeydoc Safely inserts a value before the value pointed to by the iterator.
  63. After calling this method, the iterator will point to the newly added value.
  64. #end
  65. Method Insert( value:T )
  66. RuntimeError( "Insert not supported for Deques" )
  67. End
  68. End
  69. Private
  70. Field _data:T[]
  71. Field _head:Int
  72. Field _tail:Int
  73. Field _seq:Int
  74. Method Normalize( capacity:Int )
  75. Local length:=Length
  76. Local data:=New T[capacity]
  77. If _head<=_tail
  78. _data.CopyTo( data,_head,0,length )
  79. Else
  80. Local n:=Capacity-_head
  81. _data.CopyTo( data,_head,0,n )
  82. _data.CopyTo( data,0,n,_tail )
  83. Endif
  84. _data=data
  85. _tail=length
  86. _head=0
  87. _seq+=1
  88. End
  89. Public
  90. #rem monkeydoc Creates a new deque.
  91. @param length Initalize length of the deque.
  92. #end
  93. Method New()
  94. _data=New T[10]
  95. End
  96. Method New( length:Int )
  97. _tail=length
  98. _data=New T[_tail+1]
  99. End
  100. #rem monkeydoc True if deque is empty.
  101. #end
  102. Property Empty:Bool()
  103. Return _head=_tail
  104. End
  105. #rem monkeydoc Gets the storage capacity of the deque.
  106. The capacity of a deque is the number of values it can contain before memory needs to be reallocated to store more values.
  107. 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.
  108. You don't normally need to worry about deque capacity, but it can be useful to [[Reserve]] deque storage if you know in advance
  109. how many values a deque is likely to contain, in order to prevent the overhead of excessive memory allocation.
  110. @return The current deque capacity.
  111. #end
  112. Property Capacity:Int()
  113. Return _data.Length
  114. End
  115. #rem monkeydoc Gets the number of values in the deque.
  116. @return The number of values in the deque.
  117. #end
  118. Property Length:Int()
  119. If _head<=_tail Return _tail-_head
  120. Return Capacity-_head+_tail
  121. End
  122. #rem monkeydoc Gets the underlying array used by the deque.
  123. Note that the returned array may be longer than the deque length.
  124. @return The array used internally by the deque.
  125. #end
  126. Property Data:T[]()
  127. If Not _head Return _data
  128. Normalize( Capacity )
  129. Return _data
  130. End
  131. #rem monkeydoc Gets an iterator for visiting deque values.
  132. Returns an iterator suitable for use with [[Eachin]], or for manual iteration.
  133. @return A deque iterator.
  134. #end
  135. Method All:Iterator()
  136. Return New Iterator( Self,_head )
  137. End
  138. #rem monkeydoc Converts the deque to an array.
  139. @return An array containing each element of the deque.
  140. #end
  141. Method ToArray:T[]()
  142. Local data:=New T[Length]
  143. If _head<=_tail
  144. _data.CopyTo( data,_head,0,Length )
  145. Else
  146. Local n:=Capacity-_head
  147. _data.CopyTo( data,_head,0,n )
  148. _data.CopyTo( data,0,n,_tail )
  149. Endif
  150. Return data
  151. End
  152. #rem monkeydoc Reserves deque storage capacity.
  153. The capacity of a deque is the number of values it can contain before memory needs to be reallocated to store more values.
  154. 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.
  155. 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.
  156. @param capacity The new capacity.
  157. #end
  158. Method Reserve( capacity:Int )
  159. DebugAssert( capacity>=0 )
  160. If Capacity>=capacity Return
  161. capacity=Max( Length*2+Length,capacity )
  162. Normalize( capacity )
  163. End
  164. #rem monkeydoc Clears the deque.
  165. #end
  166. Method Clear()
  167. If _head<=_tail
  168. For Local i:=_head Until _tail
  169. _data[i]=Null
  170. Next
  171. Else
  172. For Local i:=0 Until _tail
  173. _data[i]=Null
  174. Next
  175. For Local i:=_head Until Capacity
  176. _data[i]=Null
  177. Next
  178. Endif
  179. _head=0
  180. _tail=0
  181. _seq+=1
  182. End
  183. #rem monkeydoc Adds a value at the start of the deque.
  184. #end
  185. Method PushFirst( value:T )
  186. If Length+1=Capacity Reserve( Capacity+1 )
  187. _head-=1
  188. If _head=-1 _head=Capacity-1
  189. _data[_head]=value
  190. _seq+=1
  191. End
  192. #rem monkeydoc Adds a value at the end of the deque.
  193. #end
  194. Method PushLast( value:T )
  195. If Length+1=Capacity Reserve( Capacity+1 )
  196. _data[_tail]=value
  197. _tail+=1
  198. If _tail=Capacity _tail=0
  199. _seq+=1
  200. End
  201. #rem monkeydoc Removes and returns the first value in a deque.
  202. In debug builds, a runtime error will occur if the deque is empty.
  203. #end
  204. Method PopFirst:T()
  205. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  206. Local value:=_data[_head]
  207. _data[_head]=Null
  208. _head+=1
  209. If _head=Capacity _head=0
  210. _seq+=1
  211. Return value
  212. End
  213. #rem monkeydoc Removes and returns the last value in a deque.
  214. In debug builds, a runtime error will occur if the deque is empty.
  215. #end
  216. Method PopLast:T()
  217. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  218. _tail-=1
  219. If _tail=-1 _tail=Capacity-1
  220. Local value:=_data[_tail]
  221. _data[_tail]=Null
  222. _seq+=1
  223. Return value
  224. End
  225. #rem monkeydoc Returns the first value in the deque.
  226. In debug builds, a runtime error will occur if the deque is empty.
  227. #end
  228. Method First:T()
  229. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  230. Return _data[_head]
  231. End
  232. #rem monkeydoc Returns the last value in the deque.
  233. In debug builds, a runtime error will occur if the deque is empty.
  234. #end
  235. Method Last:T()
  236. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  237. Return _data[ _tail>=0 ? _tail-1 Else Capacity-1 ]
  238. End
  239. #rem monkedoc Gets the value of a deque element.
  240. 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.
  241. #end
  242. Method Get:T( index:Int )
  243. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  244. Return _data[ index Mod Capacity ]
  245. End
  246. #rem monkedoc Sets 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 Set( index:Int,value:T )
  250. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  251. _data[ index Mod Capacity ]=value
  252. End
  253. #rem monkedoc Gets 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. Operator[]:T( index:Int )
  257. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  258. Return _data[ index Mod Capacity ]
  259. End
  260. #rem monkedoc Sets 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[]=( index:Int,value:T )
  264. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  265. _data[ index Mod Capacity ]=value
  266. End
  267. End