Swap

Swaps the buffers between matrices, vectors and dynamic arrays of the same type.

bool matrix<T>::Swap(
  matrix<T>  &mat     // matrix to swap
   );
 
bool matrix<T>::Swap(
  vector<T>  &vec     // vector to swap
   );
 
bool matrix<T>::Swap(
  T          &array[] // array to swap
   );
 
bool vector<T>::Swap(
  vector<T>  &vec     // vector to swap
   );
 
bool vector<T>::Swap(
  matrix<T>  &mat     // matrix to swap
   );
 
bool vector<T>::Swap(
  T          &array[] // array to swap
   );
 

Parameters

mat, vec or array

[in]  The matrix, vector or dynamic array to swap their associated buffers.

Return Value

Returns true if successful, otherwise — false.

Note

The array size must be a multiple of the matrix or vector size. The vector size must be a multiple of the matrix size.

Example:

template<typename T>
bool Test(void)
  {
   matrix<Ta(3,5);
   matrix<Tb(5,4);
   vector<Tv(10);
 
   if(!a.Swap(b) || a.Rows()!=5 || a.Cols()!=4 || b.Rows()!=3 || b.Cols()!=5)
      return(false);
 
   if(!a.Swap(v) || v.Size()!=20 || a.Rows()!=1 || a.Cols()!=10)
      return(false);
 
//--- negative, non dynamic array
   T arr1D[10];
   if(a.Swap(arr1D) || GetLastError()!=ERR_INVALID_ARRAY)
      return(false);
   if(v.Swap(arr1D))
      return(false);
 
//--- negative, bad size array
   T arr2D[][3];
   ArrayResize(arr2D,5);
   v.Resize(15);
   if(a.Swap(arr2D) || !v.Swap(arr2D))
      return(false);
 
   return(true);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
int OnStart(void)
  {
   bool res=true;
 
   res&=Test<double>();
   res&=Test<float>();
   res&=Test<complex>();
   res&=Test<complexf>();
 
   Print(res ? "passed" : "failed");
   return(res ? 0 : -1);
  }

See also

Assign