Libraries: HashMap implementation

 

HashMap implementation :

An implementation of a HashMap in MQL4

Author: ydrol

 

From the highlighted text of the function below, it was originally commented out but I think that to avoid memory leaks it should be included otherwise the previous value is never deleted since the link to it is lost. Any idea why it was removed? Is this operation done somehow somewhere else? Thanks



HashValue *hPut(string keyName,HashValue *obj) {
   
        HashValue *ret = NULL;
        clearError();
        
        if (find(keyName)) {
            // Return revious value
            ret = _foundEntry._val;
           
            // Replace entry contents
            if (_adoptValues && _foundEntry._val != NULL && CheckPointer(_foundEntry._val) == POINTER_DYNAMIC ) {
                delete _foundEntry._val;
            }
           
            _foundEntry._val = obj;

        }
...
 

I had problems with "Invalid pointer access".

Adding the line below the problem has been solved!


    /// Look for the required entry for key 'name' true if found.

    bool find(string keyName) {

    

         //Alert("finding");

        bool found = false;

        _foundPrev = NULL;


        // Get the index using the hashcode of the string

        _foundIndex = hash(keyName);

        


        if (_foundIndex>_hashSlots ) {


            setError(1,"hGet: bad hashIndex="+(string)_foundIndex+" size "+(string)_hashSlots);


        } else {

Libraries: HashMap implementation
Libraries: HashMap implementation
  • 2014.02.11
  • www.mql5.com
HashMap implementation : Author: ydrol...
 
sokramm:

From the highlighted text of the function below, it was originally commented out but I think that to avoid memory leaks it should be included otherwise the previous value is never deleted since the link to it is lost. Any idea why it was removed? Is this operation done somehow somewhere else? Thanks




Thank you for this - wasted 2 hours trying to figure out where my memory leaks were coming from before I remembered seeing this. I can confirm that you get memory leaks if this piece of code is left commented out.

 

Thanks for this code. The key value pairs output in reverse mode. Does anybody know how to fix that? It's not just simply changing the while loop:

/// Move to next item.
        void next() {

            //config("next : index = ",_index);

            // Advance
            if (_currentEntry != NULL) {
                _currentEntry = _currentEntry._next;
                
            }

            // Keep advancing if _currentEntry is null

            for( _index = _hash.getSlots(); _index <= 0; _index-- ){ 
               _currentEntry = _hash.getEntry(_index);
               
            }
            /*
            while (_currentEntry==NULL) {
                _index++;
                if (_index >= _hash.getSlots() ) return ;
                _currentEntry = _hash.getEntry(_index);
                //Print(_hash.getEntry(_index));
            }*/
        }

Perhaps it needs a more substantial edit.

Reason: