BUG: CArray failed Search function in a shared object

 

Hey all,


i found something strange with CArrayInt and CArrayString. I have a main object which can load other objects into his running environment. This objects can communicate with each other while sharing an object which handles a conversation. Objects can add a new item with a "add" interface and can access this data with a "get" interface. To store for example index information internaly in this shared object, i use this CArray objects -> example:  "CArrayInt   *m_vlist_a;"

I can add new items to this list and getting a true as result, i can access the counter and getting the correct number of added items, also i can access this items by using the index number. But with i use any of the Search commands, it returns a false as result:

Code:

Print("::::TOTAL:         "+IntegerToString(this.m_vlist_a.Total()));
Print("::::Item:          "+IntegerToString(this.m_vlist_a.At(0)));
Print("::::Search Result: "+IntegerToString(this.m_vlist_a.Search(0)));

Result:

2018.06.27 08:19:28.445 debug (EURUSD,M1)       ::::TOTAL:         6
2018.06.27 08:19:28.445 debug (EURUSD,M1)       ::::Item:          0
2018.06.27 08:19:28.445 debug (EURUSD,M1)       ::::Search Result: -1


Thank you

 
Christian Stern:

Hey all,


i found something strange with CArrayInt and CArrayString. I have a main object which can load other objects into his running environment. This objects can communicate with each other while sharing an object which handles a conversation. Objects can add a new item with a "add" interface and can access this data with a "get" interface. To store for example index information internaly in this shared object, i use this CArray objects -> example:  "CArrayInt   *m_vlist_a;"

I can add new items to this list and getting a true as result, i can access the counter and getting the correct number of added items, also i can access this items by using the index number. But with i use any of the Search commands, it returns a false as result:

Code:

Result:


Thank you

You need to sort the data before using search function.
 

narf*** thank you


reading between the documention lines will show this:

   //--- sort array
   array.Sort();


In my eyes its still a bug in terms of handling. If this would be a requirement for searching it should be sort by default if the index has changed. But in the other hand, in terms of big data and cpu time you don't want to sort each time you exec the search request.

I would set a default value and if the user want's to sort, he can overwrite the option -> "foo.Search(bar, true)", where the second argument is the sort switch. Or the function is handling this by them self. Like if first time exec of search and the sort flag is false or when a update/delete request has been executed ( may there set the sort flag back to false ), than sort.

Or

Add a hint in the header section of CArray_xyz in the documentation.

 
Christian Stern:

narf*** thank you

If this would be a requirement for searching it should be sort by default if the index has changed.

Search uses the binary-search algorithm, and order for it to work it must have sorted data. Like you said, it would be unexpected to search your array and discover that your indexes have changed. That is why you must explicitly sort your collection before calling binary search. If you do not want to sort then you need to call SearchLinear instead, which uses a standard O(n) loop to find the index. If you want the collection to automatically sort itself then you could easily subclass your own. And in terms of big data, make sure to reserve the amount of elements you need before hand then you can use InsertSort as you add new elements after the initial load.

#include <Arrays\ArrayInt.mqh>
class VectorInt : public CArrayInt{
   public: int Search(const int element){
      this.Sort();
      return CArrayInt::Search(element);
   }
};
 
nicholi shen:

Search uses the binary-search algorithm, and order for it to work it must have sorted data. Like you said, it would be unexpected to search your array and discover that your indexes have changed. That is why you must explicitly sort your collection before calling binary search. If you do not want to sort then you need to call SearchLinear instead, which uses a standard O(n) loop to find the index. If you want the collection to automatically sort itself then you could easily subclass your own. And in terms of big data, make sure to reserve the amount of elements you need before hand then you can use InsertSort as you add new elements after the initial load.

Thank you all, yeah you right, thats why i continuing using this language. Its very flexible.

But anyway, maybe you guys can update thr documentation and add in the describtion of this function with this:

"Note:

 Before using search_xy, run Sort() function."


Usally in other languages you don't need to take care on this. It took me hours while trying to find out why this search function returns false.

I totaly agree with your arguments, just would like to do a RFC.