Skip to content

Commit e12e0ec

Browse files
committed
Added more Methods to Arrays (Find, FindLast, Contains, Iota).
1 parent 3fbb922 commit e12e0ec

3 files changed

Lines changed: 99 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#Import "<std>"
3+
4+
Using std..
5+
6+
Function Main()
7+
8+
'It always returns a new array
9+
'we can take advantage of that when creating the array initially.
10+
Local _array := New Int[0].Iota( 10, 1, 3 )
11+
12+
Print _array.ToString()
13+
14+
Print _array.Find( 4 ) '1
15+
Print _array.FindLast( 6 ) '-1
16+
Print _array.Contains( 5 ) 'False
17+
Print _array.Contains( 22 ) 'True
18+
19+
Local i1 := _array.Iota( 30 )
20+
Print i1.ToString()
21+
i1 = i1.Iota( 20, -19, -2 )
22+
Print i1.ToString()
23+
i1 = i1.Iota( 10, -4 )
24+
Print i1.ToString()
25+
26+
End

modules/wonkey/native/wxarray.h

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,51 @@ template<class T,int D> struct wxArray{
232232
for ( int i=0;i<n;++i) {
233233
if (i>0) result+=", ";
234234
result+=wxString(_rep->_data[i]);
235-
}
235+
}
236236
return result+=" ]";
237237
}
238+
239+
int find( T value, int start )const{
240+
wxDebugAssert( start>=0 && start<=length(), "start out of range" );
241+
int i = start;
242+
int n = length();
243+
while (i<n) {
244+
if (value==_rep->_data[i]) return i;
245+
++i;
246+
}
247+
return -1;
248+
}
249+
250+
int findlast( T value, int start )const{
251+
wxDebugAssert( start>=0 && start<=length(), "start out of range" );
252+
int i = length();
253+
int n = start;
254+
while (i<n) {
255+
--i;
256+
if (value==_rep->_data[i]) return i;
257+
}
258+
return -1;
259+
}
260+
261+
bool contains( T value )const{
262+
return find( value, 0 )!=-1;
263+
}
264+
265+
wxArray<T,1> iota( int newLength, int start, int step )const{
266+
wxDebugAssert( newLength>=0, "new length must not be negative" );
267+
auto r=wxArray<T,1>( newLength );
268+
int value = start;
269+
for( int i=0;i<newLength;++i ) {
270+
r.data()[i]=value;
271+
if (step>0) {
272+
value+=step;
273+
}else{
274+
value-=step;
275+
}
276+
}
277+
return r;
278+
}
279+
238280
};
239281

240282
template<class T,int D> wxString wxDBType( wxArray<T,D> *p ){

modules/wonkey/types.wx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,36 @@ Struct @Array<T>
643643
#end
644644
Method ToString:String()="toString"
645645

646+
#rem wonkeydoc Finds the index of the first matching value in the array.
647+
In debug builds, a runtime error will occur if `start` is less than 0 or greater than the length of the stack.
648+
@param value The value to find.
649+
@param start The starting index for the search.
650+
@return The index of the value in the stack, or -1 if the value was not found.
651+
#end
652+
Method Find:Int( value:T, start:Int = 0 )="find"
653+
654+
#rem wonkeydoc Finds the index of the last matching value in the array.
655+
In debug builds, a runtime error will occur if `start` is less than 0 or greater than the length of the stack.
656+
@param value The value to find.
657+
@param start The starting index for the search.
658+
@return The index of the value in the stack, or -1 if the value was not found.
659+
#end
660+
Method FindLast:Int( value:T, start:Int = 0 )="findlast"
661+
662+
#rem wonkeydoc Checks if the array contains a value.
663+
@param value The value to check for.
664+
@return True if the array contains the value, else false.
665+
#end
666+
Method Contains:Bool( value:T )="contains"
667+
668+
#rem wonkeydoc Creates a new Array, consisting of numbers between a starting point
669+
and ending point, spaced apart by a given interval.
670+
@param newLength Length of new Array.
671+
@param start The starting point.
672+
@param _step Interval.
673+
#end
674+
Method Iota:T[]( newLength:Int, start:Int = 0, _step:Int = 1 )="iota"
675+
646676
End
647677

648678
#rem wonkeydoc Base class of all objects.

0 commit comments

Comments
 (0)