Passing an array to a function: any way to make it type-independent?

 

I made a function ArrayPrepend() that insert a value in the first element [0] of an array
shifting the values already present down by 1. (the last element is lost)

Currently I need 2 different functions to do this, because ArrayPrepend() only accept arrays of the declared type:

void intArrayPrepend(int& vector[], int val) {
   for( i=ArraySize(vector) ; i>0 ; i-- ) vector[i] = vector[i-1];
   vector[0] = val;
}

void dblArrayPrepend(double& vector[], double val) {
   for( i=ArraySize(vector) ; i>0 ; i-- ) vector[i] = vector[i-1];
   vector[0] = val;
}

Do you know any way to make it type-indipendent? A single ArrayPrepend() that accept double and int types?

Thank you

 

maybe something like this ...

void arrayfunction(int& array1[], double& array2[], string& array3[], int type)
{
 switch(type)
 {  
    case 1: //operate on int array
    case 2: //operate on double array
    case 3: //operate on string array
 }
}
 
oddpip:

I made a function ArrayPrepend() that insert a value in the first element [0] of an array
shifting the values already present down by 1. (the last element is lost)

Currently I need 2 different functions to do this, because ArrayPrepend() only accept arrays of the declared type:

Do you know any way to make it type-indipendent? A single ArrayPrepend() that accept double and int types?

Thank you

Why don't you just make it a series array and increase it's size by one, then make it a non series array and reduce it's size by one . . . thats how I do it, test it against your current version I assume it will be a lot faster.
 
RaptorUK:
Why don't you just make it a series array and increase it's size by one, then make it a non series array and reduce it's size by one . . . thats how I do it, test it against your current version I assume it will be a lot faster.
Nice idea ^_^

// INSERTION OF THE VALUE 'K' AT THE TOP OF THE ARRAY
//  [0] show where is the first element

//  [0] -> { A , B , C , D }
ArraySetAsSeries( vector, true )                
// { A , B , C , D } <- [0]
ArrayResize( vector, ArraySize(vector)+1 ) 
// { _ , A , B , C , D } <- [0]
ArraySetAsSeries( vector, false )               
// [0] -> { _ , A , B , C , D }
ArrayResize( vector, ArraySize(vector)-1 ) 
// [0] -> { _ , A , B , C }
vector[0] = K
//  [0] ->{ K , A , B , C }
 
The new (beta) build allows function overlading; old does not?
 

Yes it does but it would still mean creating two functions. I would like to compare the speed of doing it one function by passing an ID variable to define the type like I did above, with creating a class and two overloaded functions in the C++ way, I might do that in the new beta later and see which is faster. (I might suprise myself by accomplishing that without getting all lost in a C++ mess)

 

The compiler will choose proper overloaded function at compile-time; whereas distinguishing via ID (bad style) happens at run-time. No doubts which is faster..

 

Here some dirty trick, just for the fun of it.. uses a system dll's memcpy.

The other possibillity is to use your own dll where you are free to do what you want.


// MT4 - build 568 script

#import "msvcrt.dll"
        int memcpy(int&[], int&[], int);
        int memcpy(double&[], double&[], int);
        int memcpy(int, int, int);
#import

int int_array[3] = {3, 2, 1};
int int_element[1];

double double_array[3] = {3.3, 2.2, 1.1};
double double_element[1];

// note: the following first two parameters do not in fact transport ints but pointers to memory
void push_front(int dst, int src, int dst_size, int src_size)
{
        memcpy(dst + src_size, dst, dst_size - src_size);
        memcpy(dst, src, src_size);
}

void OnStart()
{
        int_element[0] = 4; // push 4 to front of int_array
        push_front(memcpy(int_array, int_array, 0), memcpy(int_element, int_element, 0), ArraySize(int_array)*sizeof(int), sizeof(int));
        
        for(int i = 0; i < ArraySize(int_array); i++)
                Print(int_array[i]);    
        
        double_element[0] = 4.4; // push 4.4 to front of double_array
        push_front(memcpy(double_array, double_array, 0), memcpy(double_element, double_element, 0), ArraySize(double_array)*sizeof(double), sizeof(double));
        
        for(i = 0; i < ArraySize(double_array); i++)
                Print(double_array[i]);         
}
 

Hmmm thats interesting so how did you know memcpy is in that dll and how did you know the correct parameters to pass to it ?

 
SDC: Hmmm thats interesting so how did you know memcpy is in that dll and how did you know the correct parameters to pass to it ?
Good question. I wonder what type of programming likely puts someone in contact with such functions. Windows System Programming?
 
SDC:

Hmmm thats interesting so how did you know memcpy is in that dll and how did you know the correct parameters to pass to it ?

Dependency Walker will tell you the functions that a DLL exposes.
Reason: