|
@@ -5,11 +5,11 @@ Namespace std.collections
|
|
#end
|
|
#end
|
|
Alias IntDeque:Deque<Int>
|
|
Alias IntDeque:Deque<Int>
|
|
|
|
|
|
-#rem monkeydoc @hidden Convenience type alias for Deqaue\<Float\>.
|
|
|
|
|
|
+#rem monkeydoc @hidden Convenience type alias for Deque\<Float\>.
|
|
#end
|
|
#end
|
|
Alias FloatDeque:Deque<Float>
|
|
Alias FloatDeque:Deque<Float>
|
|
|
|
|
|
-#rem monkeydoc @hidden Convenience type alias for Dequq\<String\>.
|
|
|
|
|
|
+#rem monkeydoc @hidden Convenience type alias for Deque\<String\>.
|
|
#end
|
|
#end
|
|
Alias StringDeque:Deque<String>
|
|
Alias StringDeque:Deque<String>
|
|
|
|
|
|
@@ -108,7 +108,12 @@ Class Deque<T> Implements IContainer<T>
|
|
End
|
|
End
|
|
|
|
|
|
Public
|
|
Public
|
|
|
|
+
|
|
|
|
+ #rem monkeydoc Creates a new deque.
|
|
|
|
|
|
|
|
+ @param length Initalize length of the deque.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Method New()
|
|
Method New()
|
|
_data=New T[10]
|
|
_data=New T[10]
|
|
End
|
|
End
|
|
@@ -118,19 +123,45 @@ Class Deque<T> Implements IContainer<T>
|
|
_data=New T[_tail+1]
|
|
_data=New T[_tail+1]
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc True if deque is empty.
|
|
|
|
+ #end
|
|
Property Empty:Bool()
|
|
Property Empty:Bool()
|
|
Return _head=_tail
|
|
Return _head=_tail
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Gets the storage capacity of the deque.
|
|
|
|
+
|
|
|
|
+ The capacity of a deque is the number of values it can contain before memory needs to be reallocated to store more values.
|
|
|
|
+
|
|
|
|
+ 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.
|
|
|
|
+
|
|
|
|
+ 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 the overhead of excessive memory allocation.
|
|
|
|
+
|
|
|
|
+ @return The current deque capacity.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Property Capacity:Int()
|
|
Property Capacity:Int()
|
|
Return _data.Length
|
|
Return _data.Length
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Gets the number of values in the deque.
|
|
|
|
+
|
|
|
|
+ @return The number of values in the deque.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Property Length:Int()
|
|
Property Length:Int()
|
|
If _head<=_tail Return _tail-_head
|
|
If _head<=_tail Return _tail-_head
|
|
Return Capacity-_head+_tail
|
|
Return Capacity-_head+_tail
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Gets the underlying array used by the deque.
|
|
|
|
+
|
|
|
|
+ Note that the returned array may be longer than the deque length.
|
|
|
|
+
|
|
|
|
+ @return The array used internally by the deque.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Property Data:T[]()
|
|
Property Data:T[]()
|
|
|
|
|
|
If Not _head Return _data
|
|
If Not _head Return _data
|
|
@@ -140,6 +171,23 @@ Class Deque<T> Implements IContainer<T>
|
|
Return _data
|
|
Return _data
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Gets an iterator for visiting deque values.
|
|
|
|
+
|
|
|
|
+ Returns an iterator suitable for use with [[Eachin]], or for manual iteration.
|
|
|
|
+
|
|
|
|
+ @return A deque iterator.
|
|
|
|
+
|
|
|
|
+ #end
|
|
|
|
+ Method All:Iterator()
|
|
|
|
+
|
|
|
|
+ Return New Iterator( Self,_head )
|
|
|
|
+ End
|
|
|
|
+
|
|
|
|
+ #rem monkeydoc Converts the deque to an array.
|
|
|
|
+
|
|
|
|
+ @return An array containing each element of the deque.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Method ToArray:T[]()
|
|
Method ToArray:T[]()
|
|
|
|
|
|
Local data:=New T[Length]
|
|
Local data:=New T[Length]
|
|
@@ -155,6 +203,18 @@ Class Deque<T> Implements IContainer<T>
|
|
Return data
|
|
Return data
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Reserves deque storage capacity.
|
|
|
|
+
|
|
|
|
+ The capacity of a deque is the number of values it can contain before memory needs to be reallocated to store more values.
|
|
|
|
+
|
|
|
|
+ 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.
|
|
|
|
+
|
|
|
|
+ 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 the overhead of excessive memory allocation.
|
|
|
|
+
|
|
|
|
+ @param capacity The new capacity.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Method Reserve( capacity:Int )
|
|
Method Reserve( capacity:Int )
|
|
DebugAssert( capacity>=0 )
|
|
DebugAssert( capacity>=0 )
|
|
|
|
|
|
@@ -165,6 +225,8 @@ Class Deque<T> Implements IContainer<T>
|
|
Normalize( capacity )
|
|
Normalize( capacity )
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Clears the deque.
|
|
|
|
+ #end
|
|
Method Clear()
|
|
Method Clear()
|
|
If _head<=_tail
|
|
If _head<=_tail
|
|
For Local i:=_head Until _tail
|
|
For Local i:=_head Until _tail
|
|
@@ -183,6 +245,8 @@ Class Deque<T> Implements IContainer<T>
|
|
_seq+=1
|
|
_seq+=1
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Adds a value at the start of the deque.
|
|
|
|
+ #end
|
|
Method PushFirst( value:T )
|
|
Method PushFirst( value:T )
|
|
If Length+1=Capacity Reserve( Capacity+1 )
|
|
If Length+1=Capacity Reserve( Capacity+1 )
|
|
|
|
|
|
@@ -192,6 +256,8 @@ Class Deque<T> Implements IContainer<T>
|
|
_seq+=1
|
|
_seq+=1
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Adds a value at the end of the deque.
|
|
|
|
+ #end
|
|
Method PushLast( value:T )
|
|
Method PushLast( value:T )
|
|
If Length+1=Capacity Reserve( Capacity+1 )
|
|
If Length+1=Capacity Reserve( Capacity+1 )
|
|
|
|
|
|
@@ -201,6 +267,11 @@ Class Deque<T> Implements IContainer<T>
|
|
_seq+=1
|
|
_seq+=1
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Removes and returns the first value in a deque.
|
|
|
|
+
|
|
|
|
+ In debug builds, a runtime error will occur if the deque is empty.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Method PopFirst:T()
|
|
Method PopFirst:T()
|
|
DebugAssert( Not Empty,"Illegal operation on empty deque" )
|
|
DebugAssert( Not Empty,"Illegal operation on empty deque" )
|
|
|
|
|
|
@@ -212,6 +283,11 @@ Class Deque<T> Implements IContainer<T>
|
|
Return value
|
|
Return value
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Removes and returns the last value in a deque.
|
|
|
|
+
|
|
|
|
+ In debug builds, a runtime error will occur if the deque is empty.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Method PopLast:T()
|
|
Method PopLast:T()
|
|
DebugAssert( Not Empty,"Illegal operation on empty deque" )
|
|
DebugAssert( Not Empty,"Illegal operation on empty deque" )
|
|
|
|
|
|
@@ -223,36 +299,66 @@ Class Deque<T> Implements IContainer<T>
|
|
Return value
|
|
Return value
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Returns the first value in the deque.
|
|
|
|
+
|
|
|
|
+ In debug builds, a runtime error will occur if the deque is empty.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Method First:T()
|
|
Method First:T()
|
|
DebugAssert( Not Empty,"Illegal operation on empty deque" )
|
|
DebugAssert( Not Empty,"Illegal operation on empty deque" )
|
|
|
|
|
|
Return _data[_head]
|
|
Return _data[_head]
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkeydoc Returns the last value in the deque.
|
|
|
|
+
|
|
|
|
+ In debug builds, a runtime error will occur if the deque is empty.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Method Last:T()
|
|
Method Last:T()
|
|
DebugAssert( Not Empty,"Illegal operation on empty deque" )
|
|
DebugAssert( Not Empty,"Illegal operation on empty deque" )
|
|
|
|
|
|
- Return _data[ (_tail-1) Mod Capacity ]
|
|
|
|
|
|
+ Return _data[ _tail>=0 ? _tail-1 Else Capacity-1 ]
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkedoc Gets the value of a deque element.
|
|
|
|
+
|
|
|
|
+ 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.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Method Get:T( index:Int )
|
|
Method Get:T( index:Int )
|
|
DebugAssert( index>=0 And index<Length,"Deque index out of range" )
|
|
DebugAssert( index>=0 And index<Length,"Deque index out of range" )
|
|
|
|
|
|
Return _data[ index Mod Capacity ]
|
|
Return _data[ index Mod Capacity ]
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkedoc Sets the value of a deque element.
|
|
|
|
+
|
|
|
|
+ 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.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Method Set( index:Int,value:T )
|
|
Method Set( index:Int,value:T )
|
|
DebugAssert( index>=0 And index<Length,"Deque index out of range" )
|
|
DebugAssert( index>=0 And index<Length,"Deque index out of range" )
|
|
|
|
|
|
_data[ index Mod Capacity ]=value
|
|
_data[ index Mod Capacity ]=value
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkedoc Gets the value of a deque element.
|
|
|
|
+
|
|
|
|
+ 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.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Operator[]:T( index:Int )
|
|
Operator[]:T( index:Int )
|
|
DebugAssert( index>=0 And index<Length,"Deque index out of range" )
|
|
DebugAssert( index>=0 And index<Length,"Deque index out of range" )
|
|
|
|
|
|
Return _data[ index Mod Capacity ]
|
|
Return _data[ index Mod Capacity ]
|
|
End
|
|
End
|
|
|
|
|
|
|
|
+ #rem monkedoc Sets the value of a deque element.
|
|
|
|
+
|
|
|
|
+ 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.
|
|
|
|
+
|
|
|
|
+ #end
|
|
Operator[]=( index:Int,value:T )
|
|
Operator[]=( index:Int,value:T )
|
|
DebugAssert( index>=0 And index<Length,"Deque index out of range" )
|
|
DebugAssert( index>=0 And index<Length,"Deque index out of range" )
|
|
|
|
|