pass generic class data field - page 2

 
JC: If we're going to get personal,
Apparently you took it that way. I mention 30 items and you post something about 10,000 items.
 
whroeder1:
Apparently you took it that way. I mention 30 items and you post something about 10,000 items.

The best case is that insertion sort will save nanoseconds compared to quicksort (e.g. array of 10 items), and the worst case is that it will be whole seconds slower (e.g. array of 10,000 items), as in the benchmark I attached.

The only good time to use insertion sort is a scenario where you know that you are dealing with an array which will never, ever, ever have more than 10-20 items. Those cases are vanishingly rare. Even if you think that you will always be dealing with a small array, the downside in nanoseconds of using quicksort is preferable to the possible consequences if your assumption turns out to be wrong.

This is the sort of scenario where you would otherwise be saying to people "Do NOT use insertion sort, EVER. For ANY Reason."

 
bapo101:

I've also found it much cleaner to encapsulate your functions by deriving your own collection class. 

class InstrumentList : public CList
{
public:
   Instrument* GetPointer(const string symbol)
   {
      for(Instrument *i=GetFirstNode();i!=NULL;i=i.Next())
         if(symbol == i.symbol)
            return i;
      int index = Add(symbol);
      if(index > 0)
         return GetNodeAtIndex(index);
      return NULL;
   }
   int Add(const string symbol)
   {
      for(int i=0;i<SymbolsTotal(false);i++)
         if(StringFind(SymbolName(i,false),symbol) > 0)
            return Add(new Instrument(SymbolName(i,false)));
      return -1;
   }
   void  ToLog()
   {
      for(Instrument *i=GetFirstNode();i!=NULL;i=i.Next())
         Print(i.symbol," ",i.csi);
   }
};

 

void OnStart()
{
//---
   InstrumentList list;
   for(int i=0;i<SymbolsTotal(false);i++)
      list.Add(SymbolName(i,false)); // object creation happens inside the list class now
 
   list.Sort(SORT_SPREAD);
   
   for(Instrument *i=list.GetFirstNode();i!=NULL;i=i.Next())
      Print(i.symbol," has a spread of ",i.spread); 
   
   Instrument *eurusd = list.GetPointer("EURUSD"); // if this symbol doesn't exist the list class attempts to find the symbol and create an object
   if(eurusd!=NULL)
   {
      Comment(eurusd.csi);
   }
   
   list.ToLog();
   
}
 
Nice ... I'll get going with this - thanks
 

Doesn't add anything but I post for completeness your code that I bent to fit my situation

//+------------------------------------------------------------------+
//| instrument List                                                  |
//+------------------------------------------------------------------+
class instrumentList : public CList
  {
public:
   //+------------------------------------------------------------------+
   //|display symbols data                                              |
   //+------------------------------------------------------------------+
   void sortDisplayData(string var,int pY,int sVar)
     {
      int colorOdd=0;
      if(sVar<7)
        {
         for(instrument *j=GetLastNode();j!=NULL;j=j.Prev())
           {
            setSymbols(colorOdd,var,pY,j);
            pY++;
           }
        }
      else
        {
         for(instrument *j=GetFirstNode();j!=NULL;j=j.Next())
           {
            setSymbols(colorOdd,var,pY,j);
            pY++;
           }
        }
     }
   void  ToLog()
     {
      for(instrument *i=GetFirstNode();i!=NULL;i=i.Next())
         Print(i.symbol," ",i.csi);
     }
  };