Browse Source

Added 1D Array.Resize.

Mark Sibly 9 years ago
parent
commit
b99edc58d4
2 changed files with 30 additions and 16 deletions
  1. 18 16
      modules/monkey/native/bbarray.h
  2. 12 0
      modules/monkey/types.monkey2

+ 18 - 16
modules/monkey/native/bbarray.h

@@ -86,6 +86,7 @@ template<class T,int D> class bbArray : public bbGCNode{
 	
 	
 	int size( int q )const{
 	int size( int q )const{
 		bbDebugAssert( q<D,"Array dimension out of range" );
 		bbDebugAssert( q<D,"Array dimension out of range" );
+		
 		return this ? (q ? _sizes[q]/_sizes[q-1] : _sizes[0]) : 0;
 		return this ? (q ? _sizes[q]/_sizes[q-1] : _sizes[0]) : 0;
 	}
 	}
 	
 	
@@ -114,24 +115,23 @@ template<class T,int D> class bbArray : public bbGCNode{
 		for( int i=0;i<newlen;++i ) r->data()[i]=data()[from+i];
 		for( int i=0;i<newlen;++i ) r->data()[i]=data()[from+i];
 		return r;
 		return r;
 	}
 	}
+
+	bbArray<T,1> *resize( int newLength )const{
+		bbDebugAssert( newLength>=0,
+			"Array Resize new length must not be negative" );
+			
+		if( newLength<=0 ) newLength=0;
+		int n=this->length();
+		if( n>newLength ) n=newLength;
+		bbArray<T,1> *r=create( newLength );
+		for( int i=0;i<n;++i ) r->data()[i]=data()[i];
+		return r;
+	}
 	
 	
 	void copyTo( bbArray<T,1> *dst,int offset,int dstOffset,int count )const{
 	void copyTo( bbArray<T,1> *dst,int offset,int dstOffset,int count )const{
-		int length=this->length();
-		if( offset<0 ){
-			offset=0;
-		}else if( offset>length ){
-			offset=length;
-		}
-		int dstLength=dst->length();
-		if( dstOffset<0 ){
-			dstOffset=0;
-		}else if( dstOffset>dstLength ){
-			dstOffset=dstLength;
-		}
+		bbDebugAssert( offset>=0 && dstOffset>=0 && count>=0 && offset+count<=length() && dstOffset+count<=dst->length(),
+			"Array CopyTo parameters out of range" );
 
 
-		if( offset+count>length ) count=length-offset;
-		if( dstOffset+count>dstLength) count=dstLength-dstOffset;
-		
 		if( dst==this && dstOffset>offset ){
 		if( dst==this && dstOffset>offset ){
 			for( int i=count-1;i>=0;--i ) dst->data()[dstOffset+i]=data()[offset+i];
 			for( int i=count-1;i>=0;--i ) dst->data()[dstOffset+i]=data()[offset+i];
 		}else{
 		}else{
@@ -141,7 +141,9 @@ template<class T,int D> class bbArray : public bbGCNode{
 	
 	
 	//fast 1D version	
 	//fast 1D version	
 	T &at( int index ){
 	T &at( int index ){
-		bbDebugAssert( index>=0 && index<length(),"Array index out of range" );
+		bbDebugAssert( index>=0 && index<length(),
+			"Array index out of range" );
+			
 		return data()[index];
 		return data()[index];
 	}
 	}
 	
 	

+ 12 - 0
modules/monkey/types.monkey2

@@ -374,6 +374,18 @@ Struct @Array<T>
 	
 	
 	Method Slice:T[]( from:Int,term:Int )="slice" 
 	Method Slice:T[]( from:Int,term:Int )="slice" 
 	
 	
+	
+	#rem monkeydoc Resizes an array.
+	
+	Returns a copy of the array resized to length `newLength'.
+	
+	@param newLength The length of the new array.
+	
+	@return A new array.
+	
+	#end
+	Method Resize:T[]( newLength:Int )="resize"
+	
 	#rem monkeydoc Gets the size of a single array dimension.
 	#rem monkeydoc Gets the size of a single array dimension.
 	
 	
 	@param dimensions The dimension.
 	@param dimensions The dimension.