deque.monkey2 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. Namespace std.collections
  2. #rem monkeydoc @hidden Convenience type alias for Deque\<Int\>.
  3. #end
  4. Alias IntDeque:Deque<Int>
  5. #rem monkeydoc @hidden Convenience type alias for Deque\<Float\>.
  6. #end
  7. Alias FloatDeque:Deque<Float>
  8. #rem monkeydoc @hidden Convenience type alias for Deque\<String\>.
  9. #end
  10. Alias StringDeque:Deque<String>
  11. #rem monkeydoc @hidden
  12. #end
  13. Class Deque<T> Implements IContainer<T>
  14. Struct Iterator Implements IIterator<T>
  15. Private
  16. Field _deque:Deque
  17. Field _index:Int
  18. Field _seq:Int
  19. Method AssertSeq()
  20. DebugAssert( _seq=_deque._seq,"Concurrent list modification" )
  21. End
  22. Method AssertCurrent()
  23. DebugAssert( Not AtEnd,"Invalid list iterator" )
  24. End
  25. Method New( deque:Deque,index:Int )
  26. _deque=deque
  27. _index=index
  28. _seq=deque._seq
  29. End
  30. Public
  31. #rem monkeydoc Checks if the iterator has reached the end of the deque.
  32. #end
  33. Property AtEnd:Bool()
  34. AssertSeq()
  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. Field _seq:Int
  68. Method Normalize( capacity:Int )
  69. Local length:=Length
  70. Local data:=New T[capacity]
  71. If _head<=_tail
  72. _data.CopyTo( data,_head,0,length )
  73. Else
  74. Local n:=Capacity-_head
  75. _data.CopyTo( data,_head,0,n )
  76. _data.CopyTo( data,0,n,_tail )
  77. Endif
  78. _data=data
  79. _tail=length
  80. _head=0
  81. _seq+=1
  82. End
  83. Public
  84. #rem monkeydoc Creates a new deque.
  85. @param length Initalize length of the deque.
  86. #end
  87. Method New()
  88. _data=New T[10]
  89. End
  90. Method New( length:Int )
  91. _tail=length
  92. _data=New T[_tail+1]
  93. End
  94. #rem monkeydoc True if deque is empty.
  95. #end
  96. Property Empty:Bool()
  97. Return _head=_tail
  98. End
  99. #rem monkeydoc Gets the storage capacity of the deque.
  100. The capacity of a deque is the number of values it can contain before memory needs to be reallocated to store more values.
  101. 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.
  102. 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
  103. how many values a deque is likely to contain, in order to prevent the overhead of excessive memory allocation.
  104. @return The current deque capacity.
  105. #end
  106. Property Capacity:Int()
  107. Return _data.Length
  108. End
  109. #rem monkeydoc Gets the number of values in the deque.
  110. @return The number of values in the deque.
  111. #end
  112. Property Length:Int()
  113. If _head<=_tail Return _tail-_head
  114. Return Capacity-_head+_tail
  115. End
  116. #rem monkeydoc Gets the underlying array used by the deque.
  117. Note that the returned array may be longer than the deque length.
  118. @return The array used internally by the deque.
  119. #end
  120. Property Data:T[]()
  121. If Not _head Return _data
  122. Normalize( Capacity )
  123. Return _data
  124. End
  125. #rem monkeydoc Gets an iterator for visiting deque values.
  126. Returns an iterator suitable for use with [[Eachin]], or for manual iteration.
  127. @return A deque iterator.
  128. #end
  129. Method All:Iterator()
  130. Return New Iterator( Self,_head )
  131. End
  132. #rem monkeydoc Converts the deque to an array.
  133. @return An array containing each element of the deque.
  134. #end
  135. Method ToArray:T[]()
  136. Local data:=New T[Length]
  137. If _head<=_tail
  138. _data.CopyTo( data,_head,0,Length )
  139. Else
  140. Local n:=Capacity-_head
  141. _data.CopyTo( data,_head,0,n )
  142. _data.CopyTo( data,0,n,_tail )
  143. Endif
  144. Return data
  145. End
  146. #rem monkeydoc Reserves deque storage capacity.
  147. The capacity of a deque is the number of values it can contain before memory needs to be reallocated to store more values.
  148. 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.
  149. 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
  150. how many values a deque is likely to contain, in order to prevent the overhead of excessive memory allocation.
  151. @param capacity The new capacity.
  152. #end
  153. Method Reserve( capacity:Int )
  154. DebugAssert( capacity>=0 )
  155. If Capacity>=capacity Return
  156. capacity=Max( Length*2+Length,capacity )
  157. Normalize( capacity )
  158. End
  159. #rem monkeydoc Clears the deque.
  160. #end
  161. Method Clear()
  162. If _head<=_tail
  163. For Local i:=_head Until _tail
  164. _data[i]=Null
  165. Next
  166. Else
  167. For Local i:=0 Until _tail
  168. _data[i]=Null
  169. Next
  170. For Local i:=_head Until Capacity
  171. _data[i]=Null
  172. Next
  173. Endif
  174. _head=0
  175. _tail=0
  176. _seq+=1
  177. End
  178. #rem monkeydoc Adds a value at the start of the deque.
  179. #end
  180. Method PushFirst( value:T )
  181. If Length+1=Capacity Reserve( Capacity+1 )
  182. _head-=1
  183. If _head=-1 _head=Capacity-1
  184. _data[_head]=value
  185. _seq+=1
  186. End
  187. #rem monkeydoc Adds a value at the end of the deque.
  188. #end
  189. Method PushLast( value:T )
  190. If Length+1=Capacity Reserve( Capacity+1 )
  191. _data[_tail]=value
  192. _tail+=1
  193. If _tail=Capacity _tail=0
  194. _seq+=1
  195. End
  196. #rem monkeydoc Removes and returns the first value in a deque.
  197. In debug builds, a runtime error will occur if the deque is empty.
  198. #end
  199. Method PopFirst:T()
  200. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  201. Local value:=_data[_head]
  202. _data[_head]=Null
  203. _head+=1
  204. If _head=Capacity _head=0
  205. _seq+=1
  206. Return value
  207. End
  208. #rem monkeydoc Removes and returns the last value in a deque.
  209. In debug builds, a runtime error will occur if the deque is empty.
  210. #end
  211. Method PopLast:T()
  212. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  213. _tail-=1
  214. If _tail=-1 _tail=Capacity-1
  215. Local value:=_data[_tail]
  216. _data[_tail]=Null
  217. _seq+=1
  218. Return value
  219. End
  220. #rem monkeydoc Returns the first value in the deque.
  221. In debug builds, a runtime error will occur if the deque is empty.
  222. #end
  223. Method First:T()
  224. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  225. Return _data[_head]
  226. End
  227. #rem monkeydoc Returns the last value in the deque.
  228. In debug builds, a runtime error will occur if the deque is empty.
  229. #end
  230. Method Last:T()
  231. DebugAssert( Not Empty,"Illegal operation on empty deque" )
  232. Return _data[ _tail>=0 ? _tail-1 Else Capacity-1 ]
  233. End
  234. #rem monkedoc Gets the value of a deque element.
  235. 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.
  236. #end
  237. Method Get:T( index:Int )
  238. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  239. Return _data[ index Mod Capacity ]
  240. End
  241. #rem monkedoc Sets the value of a deque element.
  242. 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.
  243. #end
  244. Method Set( index:Int,value:T )
  245. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  246. _data[ index Mod Capacity ]=value
  247. End
  248. #rem monkedoc Gets the value of a deque element.
  249. 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.
  250. #end
  251. Operator[]:T( index:Int )
  252. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  253. Return _data[ index Mod Capacity ]
  254. End
  255. #rem monkedoc Sets the value of a deque element.
  256. 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.
  257. #end
  258. Operator[]=( index:Int,value:T )
  259. DebugAssert( index>=0 And index<Length,"Deque index out of range" )
  260. _data[ index Mod Capacity ]=value
  261. End
  262. End