diff --git a/examples/denise/test_array_new_methods.wx b/examples/denise/test_array_new_methods.wx new file mode 100644 index 00000000..516d9620 --- /dev/null +++ b/examples/denise/test_array_new_methods.wx @@ -0,0 +1,26 @@ + +#Import "" + +Using std.. + +Function Main() + + 'It always returns a new array + 'we can take advantage of that when creating the array initially. + Local _array := New Int[0].Iota( 10, 1, 3 ) + + Print _array.ToString() + + Print _array.Find( 4 ) '1 + Print _array.FindLast( 6 ) '-1 + Print _array.Contains( 5 ) 'False + Print _array.Contains( 22 ) 'True + + Local i1 := _array.Iota( 30 ) + Print i1.ToString() + i1 = i1.Iota( 20, -19, -2 ) + Print i1.ToString() + i1 = i1.Iota( 10, -4 ) + Print i1.ToString() + +End \ No newline at end of file diff --git a/modules/wonkey/native/wxarray.h b/modules/wonkey/native/wxarray.h index 9c29fd84..34e4374a 100644 --- a/modules/wonkey/native/wxarray.h +++ b/modules/wonkey/native/wxarray.h @@ -232,9 +232,51 @@ template struct wxArray{ for ( int i=0;i0) result+=", "; result+=wxString(_rep->_data[i]); - } + } return result+=" ]"; } + + int find( T value, int start )const{ + wxDebugAssert( start>=0 && start<=length(), "start out of range" ); + int i = start; + int n = length(); + while (i_data[i]) return i; + ++i; + } + return -1; + } + + int findlast( T value, int start )const{ + wxDebugAssert( start>=0 && start<=length(), "start out of range" ); + int i = length(); + int n = start; + while (i_data[i]) return i; + } + return -1; + } + + bool contains( T value )const{ + return find( value, 0 )!=-1; + } + + wxArray iota( int newLength, int start, int step )const{ + wxDebugAssert( newLength>=0, "new length must not be negative" ); + auto r=wxArray( newLength ); + int value = start; + for( int i=0;i0) { + value+=step; + }else{ + value-=step; + } + } + return r; + } + }; template wxString wxDBType( wxArray *p ){ diff --git a/modules/wonkey/types.wx b/modules/wonkey/types.wx index 55c257fe..1d83fe98 100644 --- a/modules/wonkey/types.wx +++ b/modules/wonkey/types.wx @@ -643,6 +643,36 @@ Struct @Array #end Method ToString:String()="toString" + #rem wonkeydoc Finds the index of the first matching value in the array. + In debug builds, a runtime error will occur if `start` is less than 0 or greater than the length of the stack. + @param value The value to find. + @param start The starting index for the search. + @return The index of the value in the stack, or -1 if the value was not found. + #end + Method Find:Int( value:T, start:Int = 0 )="find" + + #rem wonkeydoc Finds the index of the last matching value in the array. + In debug builds, a runtime error will occur if `start` is less than 0 or greater than the length of the stack. + @param value The value to find. + @param start The starting index for the search. + @return The index of the value in the stack, or -1 if the value was not found. + #end + Method FindLast:Int( value:T, start:Int = 0 )="findlast" + + #rem wonkeydoc Checks if the array contains a value. + @param value The value to check for. + @return True if the array contains the value, else false. + #end + Method Contains:Bool( value:T )="contains" + + #rem wonkeydoc Creates a new Array, consisting of numbers between a starting point + and ending point, spaced apart by a given interval. + @param newLength Length of new Array. + @param start The starting point. + @param _step Interval. + #end + Method Iota:T[]( newLength:Int, start:Int = 0, _step:Int = 1 )="iota" + End #rem wonkeydoc Base class of all objects.