stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Moving all elements up one place in an array
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5445
Page 1 of 1
Author:  skinner36 [ Wed Apr 25, 2018 8:02 am ]
Post subject:  Moving all elements up one place in an array

Hi,

Moving array elements up one value to insert a new value at the beginning of the array can be a bit tedious to code. If the array has been switched around so that element 0 is the right most element then an extra degree of frustration is added to the issue.

This is how I solved the problem for my current project.

Let’s assume we have a dynamic array of 100 structures elements and the rightmost element is 0.

Code: Select all

struct TestStruct
{
   int     value1;
   double value2;
   string value3;
};

TestStruct Test[]

ArrayResize(Test, 100);      // Give it a size of 100

ArraySetAsSeries(Test, true); // Turn the array around so the element 0 is the far right one
Now, your program has been working as you intended but the array is now full of data and you want to add the new element to the beginning of the array at element 0 (farthest right element) and move all of the other element values up one and drop the old 100th element off.

The easiest way to do this that I have found is as follows.

Code: Select all

   // Set the array to read from left to right as a normal array does
    ArraySetAsSeries(Test, false);

     // Add a new empty element to the end 
    ArrayResize(Test, 101);

    // Set the array to read from right to left similar to the bars on a chart
    ArraySetAsSeries(Test, true);

    // Now the array has been turned around delete the last element    
    ArrayResize(Test, 100);

Done
Author:  SteveHopwood [ Wed Apr 25, 2018 10:09 am ]
Post subject:  Moving all elements up one place in an array

Done indeed. :clap: :clap: :clap: :clap:

There is an easier way and that is to use a circular array. Trouble is, I have forgotten how to code one, so I am hoping to attract the attention of one of our resident Coding Genius'. Do one of you have a spare moment?

:xm: :rocket:
Author:  renexxxx [ Wed Apr 25, 2018 11:54 am ]
Post subject:  Moving all elements up one place in an array

If you only ever want to insert a new element at the beginning of the array, John's (skinner36) method is probably the most efficient, as ArraySetAsSeries() is very quick.

But, if you need to insert new elements at any given position, you might want to have a look at Ding Li's LinkedList class, which is an implementation of the abstract List class.
Author:  skinner36 [ Wed Apr 25, 2018 11:58 am ]
Post subject:  Moving all elements up one place in an array

This is only designed to move all of the elements up one to be able to add the new data at the beginning.
Author:  PeterT [ Wed Aug 22, 2018 12:49 pm ]
Post subject:  Moving all elements up one place in an array

This is a blueprint how it can be done just with logic. It assumes, that the size of the array doesn't change at runtime. But I think this was the intention of the OP. It can be written shorter, but it's harder to read then.

Code: Select all

int Array[5];
int SizeOfArray=5;
int InsertPointer=0;

void OnStart()
{
   for(int i=1; i<10; i++)
      AddNew(i);
}

void AddNew(int val)
{
   Array[InsertPointer]=val;
   InsertPointer++;
   if(InsertPointer==SizeOfArray)
      InsertPointer=0;
   PrintArray();
}

int GetIndexInternalByLogicalIndex(int LogicalIndex)
{
   int IndexInternal=(InsertPointer-1)-LogicalIndex;
   if(IndexInternal<0)
      IndexInternal=SizeOfArray+IndexInternal;
   return IndexInternal;
}

void PrintArray()
{
   string text;
   for(int i=SizeOfArray-1; i>=0; i--)
      text+=Array[GetIndexInternalByLogicalIndex(i)]+"|";
   Print(text);
}
Author:  KingHigh [ Fri Nov 01, 2019 10:52 pm ]
Post subject:  Moving all elements up one place in an array

Hi All,

Here is example code for circular queue which I use in a multipair EA typically 10-14 pairs where all data for trading and indies go thru these queues. Once you get the hang of it, its easy. I believe it is about as fast as it can be but would appreciate any better ideas.

KingHigh

Code: Select all

//+------------------------------------------------------------------+
//|                                        KHbar_ArrayShift_TEST.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

extern int LkBk=15;
int InPtr[]={0};
int symidx = 3;
int Price[][28];

void OnDeinit(const int cause)
{
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   ArrayResize( Price, LkBk ); ArrayInitialize(Price,0);
   ArrayResize( InPtr, LkBk ); ArrayInitialize(InPtr,0);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int i;
   for(  i=0; i < LkBk; i++ ){ UpdateData(i,symidx); PrintArray(i); }
   
   UpdateData(i,symidx);
   PrintArray(i); i++;
   UpdateData(i,symidx);
   PrintArray(i); i++;
   UpdateData(i,symidx);
   PrintArray(i); i++;
   UpdateData(i,symidx);
   PrintArray(i); i++;
   UpdateData(i,symidx);
   PrintArray(i); i++;
   UpdateData(i,symidx);
   PrintArray(i); i++;
   UpdateData(i,symidx);
   PrintArray(i); i++;
   return(rates_total);
}

void UpdateData( int val, int symidx )
{
   //Alert("Price idx= "+InPtr[symidx]);
   Price[InPtr[symidx]][symidx] = val; InPtr[symidx]++; if(InPtr[symidx] == LkBk) InPtr[symidx]=0;
}

int CalcOutPtr(int OutPtr, int symidx )
{
   int idx=(InPtr[symidx]-1)-OutPtr;
   //idx = ((idx<0)? LkBk+idx:idx);
   return( ((idx<0)? LkBk+idx:idx) );
}

void PrintArray(int line)
{
   string text;
   for(int i=0; i<=LkBk-1; i++) {  /*Alert("OutPtr= "+CalcOutPtr(i,symidx) );*/ text += (string)Price[CalcOutPtr(i,symidx)][symidx]+"|";}
   Alert("Line= "+line+"|||"+text);
   return;
}
All times are UTC Page 1 of 1