t_hashtab and funall( ) order ?

Feb 18, 2010 at 5:15pm

t_hashtab and funall( ) order ?

Hello maxers,

the SDK says :

“You can iterate through all items using hashtab_funall ( ), but the exact order is not under your control as items are added and removed.”

Is anybody know a little bit more about that ? Is it First In First Out, Last In First Out, Random, or anything else ?

((( As i try to deal with asynchronous algorithmic stuff, it should be nice for me to know in which order i apply my function on element of the hashtab ; the ideal should be CHAOS of course ! )))

Thanks.

#48584
Feb 18, 2010 at 9:51pm

You can basically think of it as random. The symbol pointer is used as the hash value which is then mapped with a little math to the slots of the hashtable.

If you need some reliable order, you’ll probably want to maintain a linked list (or similar) in parallel to the hashtable. The dictionary object is basically a hashtable with a parallel maintained linked list (for order preservation). Maybe that would be of use to you.

#174704
Feb 19, 2010 at 7:35am

Hello Joshua,

My hash values provided are for example RIRI FIFI LOULOU : the order of iteration can be LOULOU RIRI FIFI ; ok i understand, but if next time i provide same values again ; will it be the similar ?

RIRI FIFI LOULOU -> LOULOU RIRI FIFI
RIRI FIFI LOULOU -> LOULOU RIRI FIFI ???
FIFI LOULOU RIRI -> LOULOU RIRI FIFI ???

((( I don’t need reliable order, i need the opposite : the hashtab is my colony of cells, and i would like to act on randomly for every generation, with hash value as position in space ; so with same distribution, i would like different order. )))

#174705
Feb 19, 2010 at 5:08pm

Hello Joshua (again),

Sorry for my lack / error of vocabulary ; when i said hash values i mean hash keys … and to be more precise : is there only one hash function, and is the way to go through slots is always the same ?

Anyway, doesn’t matter, best it is doubtless that I made tests.

#174706
Feb 19, 2010 at 6:15pm

There is one hash function, and it has regular behavior. However, it is based on the symbol pointer address, which will be essentially random between launches of the application (it’s not determined by the symbol text). As additional entries are added to the hash table, it’s essentially undetermined as to where they will be inserted.

So as long as you are not adding or removing entries to the hashtable, it will remain the same order every time per application launch. It will not be the same across application launches, nor through modification to the hashtable, and it will not have a logic related to the text of the symbols.

Hope this helps clarify a bit.

#174707
Feb 20, 2010 at 11:52am

Hello Joshua,

Thanks for clarify ; I investigated a little : here to illustrate my naive question :

void test (t_myobject *x)
	{
		void *arg = NULL ;

		tab = (t_hashtab *)hashtab_new (0) ;

		hashtab_store (tab, gensym ("RIRI"), (t_object *)1) ;
		hashtab_store (tab, gensym ("FIFI"), (t_object *)2) ;
		hashtab_store (tab, gensym ("TOTO"), (t_object *)3) ;
		hashtab_store (tab, gensym ("MOMO"), (t_object *)4) ;
		hashtab_store (tab, gensym ("JOJO"), (t_object *)5) ;

		hashtab_funall (tab, (method)myfun, arg) ;

		hashtab_chuck (tab) ;
	}

void myfun (t_hashtab_entry *e, void *arg)
        {
       		if (e->key && e->value)
			{
				post ("%ld", e->value) ;
			}
    	}

Thus as expected (without relaunch) i always have same order.

Max console :

32052698
4
5
1
2
3

But what is 32052698 ?

#174708
Feb 22, 2010 at 7:56pm

Looks like another pointer has been stored in the hashtable, but obviously your code wouldn’t suggest that’s the case. So either there’s a bug in our hashtab code, or there’s some other memory corruption taking place in your object.

Please let us know if this persists and we can investigate further. I’d be surprised to see such a bug, since a lot of our code relies on correct functioning of the hashtab object. However, if it is present, we’d definitely like to fix it ASAP.

Thanks,
Joshua

#174709
Feb 23, 2010 at 8:04am

Hello Joshua,

oops, sorry, i just forget to declare t_hashtab.
– what an idiot !

t_hashtab *tab ;

Question++ : is it safe/stupid to use LONG as hash key ?

hashtab_store (tab, (t_symbol *)12345678, (t_object *)obj) ;

Thanks.

#174710
Feb 23, 2010 at 1:12pm

Yes — it’s fine.

#174711
Feb 23, 2010 at 1:26pm

Hello Timothy,

good to know …
… for the next time !

(I finally don’t use a Hashtab) !??

;-)

Ciao.

#174712

You must be logged in to reply to this topic.