Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/denise/test_array_new_methods.wx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#Import "<std>"

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
44 changes: 43 additions & 1 deletion modules/wonkey/native/wxarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,51 @@ template<class T,int D> struct wxArray{
for ( int i=0;i<n;++i) {
if (i>0) 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<n) {
if (value==_rep->_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<n) {
--i;
if (value==_rep->_data[i]) return i;
}
return -1;
}

bool contains( T value )const{
return find( value, 0 )!=-1;
}

wxArray<T,1> iota( int newLength, int start, int step )const{
wxDebugAssert( newLength>=0, "new length must not be negative" );
auto r=wxArray<T,1>( newLength );
int value = start;
for( int i=0;i<newLength;++i ) {
r.data()[i]=value;
if (step>0) {
value+=step;
}else{
value-=step;
}
}
return r;
}

};

template<class T,int D> wxString wxDBType( wxArray<T,D> *p ){
Expand Down
30 changes: 30 additions & 0 deletions modules/wonkey/types.wx
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,36 @@ Struct @Array<T>
#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.
Expand Down
Loading