Download MetaTrader 5

another question for those who knows: b600+ and hash-arrays

To add comments, please log in or register
Carl Schreiber
6777
Carl Schreiber  

Does the mt4 b600+ has hash-arrays?

Would be great!

Gooly

ydrol
593
ydrol  

nope! I've written an implementation base on an array of linked lists, mapping string->Object.

but it still needs a couple of tweaks. Mainly..

1. The string hash function is calculated in mql - might be nice to call a DLL for performance.

2. The value object must be a subclass of my HashValue object. I wonder if there is an implicit parent class that I can use (like Java's Object class ?)

ydrol
593
ydrol  

Here's my stab at it..


The Hash by default will 'adopt' the value objects. In which case it will free the memory when they are no longer in the hash. This behaviour can be disabled in the constructor.

Here is an example:


    
    Hash *h = new Hash();

    // Store values
    h.hPutInt("low",0);
    h.hPutInt("high",1);

    // Get values
    int high = h.hGetInt("high");
 
    // Loop
    HashLoop *l ;
    for( l = new HashLoop(h) ; l.hasNext() ; l.next()) {

        string key = l.key();

        int j = l.valInt();

        Print(key," = ",j);

    }
    delete l;

    delete h;

Files:
hash_2.mqh 15 kb
Alain Verleyen
Moderator
31441
Alain Verleyen  
ydrol:

Here's my stab at it..


The Hash by default will 'adopt' the value objects. In which case it will free the memory when they are no longer in the hash. This can be disabled in the constructor.

Here is an example:


It's very interesting, thank you for sharing. Maybe you can publish it to the codebase ?
ydrol
593
ydrol  
angevoyageur:
It's very interesting, thank you for sharing. Maybe you can publish it to the codebase ?


I've submitted it just before posting here :)
ydrol
593
ydrol  

I've made a tiny change - removed getSize() and replaced with

getSlots() -- size of table / number of linked lists -- not really useful to anything except the HashLoop object.

and getCount()-- number of entries in the hash

Alain Verleyen
Moderator
31441
Alain Verleyen  
ydrol:

I've made a tiny change - removed getSize() and replaced with

getSlots() -- size of table / number of linked lists -- not really useful to anything except the HashLoop object.

and getCount()-- number of entries in the hash

Published.
Ex Ovo Omnia
3216
Ex Ovo Omnia  
Cool. From how many elements does it perform faster than a sequential key iterator?
Carl Schreiber
6777
Carl Schreiber  
ydrol:

Here's my stab at it..


The Hash by default will 'adopt' the value objects. In which case it will free the memory when they are no longer in the hash. This behaviour can be disabled in the constructor.

Here is an example:



Your code is for mql5 and not for (even) mt4 600+ - correct?
ydrol
593
ydrol  

@Ovo,I've not done any benchmarking on it, although I'm not 100% sure you mean - you mean performance for searching? At what point does using a hash function beat just comparing all strings blindly?

@gooly Nope mql4++ (or whatever we will call it :) )

I'd be interested to know if there is a better way to implement generic Objects for the value that is stored (eg void *, void &, or Java's Object etc) rather than using an explicit subclass as I have done (HashValue)

Ex Ovo Omnia
3216
Ex Ovo Omnia  
ydrol:

@Ovo,I've not done any benchmarking on it, although I'm not 100% sure you mean - you mean performance for searching? At what point does using a hash function beat just comparing all strings blindly?

Well, blindly, I meant in a sequence they were stored. For example for 3 elements there are 3 string comparisons, while computing a hash and managing hash slots have constant overhead. So there is a number of key elements, at which the sequential comparison is beaten by the hash search speed.

12
To add comments, please log in or register