Clearing an array of defined element(s) - page 3

 
Vladimir Pastushak:

The main task is to clear the array of defined values.

This cannot be the main task. What is collected in this array and how is it then used?

 
Stanislav Korotky:

Well that's not serious. If pulling elephants to solve this particular small problem, I'd mention STL::remove(array, value).

That's just very serious and fundamental. MQL is just verbose. Terribly so.

Fundamental operators such as filter (which in TC is it, filtering), map (mapping), fold (collapsing, summarizing)
should be implemented at once, no questions asked. And be optimized.

 
Maxim Kuznetsov:

This is just very serious and fundamental. MQL is just verbose. It's horrible.

Such fundamental operators as filter (which in TC is filtering), map (mapping), fold (summation)
should be implemented at once, without any questions. And be optimised.

The point is that pulling such a fat ("fundamental") dependency for a small task is nonsense. If to write everything wisely and refactor the whole project - then, of course, but it is not the essence of the original question.

 

Forum on trading, automated trading systems and trading strategy testing

Cleaning an Array from Specified Elements

Dmitry Fedoseev, 2018.11.12 23:05

void DelEl(double & a[],double v){
   int sz=ArraySize(a);
   for(int i=sz-1;i>=0;i--){
      if(a[i]==v){
         sz--;
         for(int j=i;j<sz;j++){
            a[j]=a[j+1];
         }
      }
   }
   ArrayResize(a,sz);
}


This is some kind of shame...©

1. Comparing variables of type double via ==, to put it mildly, is not comical;
2. The code's complexity is O(n^2), and the worst complexity for this problem with an unsorted input sequence should be O(n);
3. The code does not work, because when the function is actually used, it is impossible to determine how many valid values the returned array contains:

void DelEl(double & a[],double v){
   int sz=ArraySize(a);
   for(int i=sz-1;i>=0;i--){
      if(a[i]==v){
         sz--;
         for(int j=i;j<sz;j++){
            a[j]=a[j+1];
         }
      }
   }
   ArrayResize(a,sz);
}


int OnStart(){
   double arr[] = {3, 5, 5, 3, 3, 5, 5, 3};
   double v = 3;
   
   ArrayPrint(arr);
   DelEl(arr, v);
   ArrayPrint(arr);
   
   return INIT_SUCCEEDED;
}


Execution result:

2018.11.13 00:35:33.355 Test123 (EURUSD,H1)     3.00000 5.00000 5.00000 3.00000 3.00000 5.00000 5.00000 3.00000
2018.11.13 00:37:08.495 Test123 (EURUSD,H1)     5.00000 5.00000 5.00000 5.00000 5.00000 5.00000 5.00000 3.00000

The invalid values are highlighted in red, they were never removed from the array.

 
Sergey Dzyublik:


This is some kind of shame...©

1. It's not nice to compare variables of the double type via ==, to put it mildly;
2. The code complexity is O(n^2), and the worst-case complexity for this problem with an unsorted input sequence should be O(n);
3. The code does not work, because when the function is actually used, it is impossible to determine how many valid values the returned array contains:


Execution result:

Invalid values that were never removed from the array are highlighted in red.

Jublik? So be it. Comparing variables of the double type is quite comical if they are not calculated before comparing.

Learn the math. What about shame?

 
Sergey Dzyublik:


This is some kind of shame...©

1. Comparing variables of type double with == is, to put it mildly, not comical;
2. The code complexity is O(n^2), and the worst complexity for this problem with unsorted input sequence should be O(n);
3. The code does not work, because when the function is actually used, it is impossible to determine how many valid values the returned array contains:


Execution result:

Invalid values that were never removed from the array are highlighted in red.

Don't be absurd. Shall I explain your mistake to you, or can you guess it yourself? Not difficult at all...

Three points, three corrals. Actually, no, only two paddocks.

ps, but I have faith in you.

ps2 here's the result if you use the function correctly:


 

That's better:

void DelEl2(double & a[],double v){
   int sz=ArraySize(a);
   int i=0;   
   int j=0;
   for(;i<sz;i++){ 
      if(a[i]!=v){
         a[j]=a[i];
         j++;
      }
   }
   ArrayResize(a,j);
}
 
Dmitry Fedoseev:

That's better:

Dimitri, let me upset you - v is also an array.

And anyway, it's all a load of nonsense.

 
Алексей Тарабанов:

Dimitri, let me upset you - v is also an array.

And in general, all this is total nonsense.

If v is an array, it's not a problem. But the fact that it is nonsense is more likely. I've never felt the need to solve this task.

 

If it's such a speed contest, I'll offer my own variant as well:

int ArrayDeleteVal(int &a[],const int val) // вариант Semko
  {
   int size=ArraySize(a);
   int i=0,start,s,count;
   while(i<size && a[i]!=val) i++; // ищем первый элемент массива со значением val
   start=i; i++;
   while(i<size && a[i]==val) i++; // ищем элемент массива со значением, не равным val
   s=i; i++;
   while(true)
     {
      while(i<size && a[i]!=val) i++; // ищем элемент массива со значением val
      count=i-s;
      if(count>6) { ArrayCopy(a,a,start,s,count); start+=count;} // если нужно скопировать более 6 элементов, то имеет смысл воспользоваться ArrayCopy
      else for(; s<i; start++,s++) a[start]=a[s];                // иначе простой цикл
      if(i==size) break;
      i++;
      while(i<size && a[i]==val) i++; // ищем элемент массива со значением, не равным val
      if(i<size) s=i; else break;
      i++;
     }
   if(start<size) ArrayResize(a,start); else start=size;
   return(start);
  }

I wrote a test script for all variants.
Here's the result for an array of 1 000 000 elements (about 1000 extra values):

2018.11.12 19:50:02.965 ArrayDeleteValue (EURUSD,D1)    вариант Pastushak: Контрольная сумма = 495654091; элементов - 998983; время выполнения = 156457 микросекунд
2018.11.12 19:50:02.969 ArrayDeleteValue (EURUSD,D1)    вариант Korotky: Контрольная сумма = 495654091; элементов - 998983; время выполнения = 2319 микросекунд
2018.11.12 19:50:02.972 ArrayDeleteValue (EURUSD,D1)    вариант Fedoseev: Контрольная сумма = 495654091; элементов - 998983; время выполнения = 1810 микросекунд
2018.11.12 19:50:02.974 ArrayDeleteValue (EURUSD,D1)    вариант Semko: Контрольная сумма = 495654091; элементов - 998983; время выполнения = 785 микросекунд
Who's next? :))
Peter, Awww....
Files: