Hash table request


#1

Something to hold arbitary objects indexed in yet another arbitary way :slight_smile: Strings or MemoryBlocks, with all what juce has to offer it would be simple i imagine, but very useful.


#2

This:
http://code.google.com/p/juced/source/browse/trunk/juce/src/extended/containers/jucetice_Hash.h

And this:

http://code.google.com/p/juced/source/browse/trunk/juce/src/extended/containers/jucetice_OwnedHash.h

You won’t notice any difference than any other Jules’ class :slight_smile:


#3

thanks, i wrote my own based on ghthash, but i’ really don’t like adding foreign code, it’s hard to maintain later on. i’ll see if yours is faster, maybe Jules will break and add a hash class to juce. [fingers crossed]


#4

well, the juce containers only need a hash map here, then we do not need to fumble around with stl/boost anymore.


#5

…it’s something I’ve been meaning to do for years. Don’t know why I’ve still not got around to it!


#6

[quote=“kraken”]This:
http://code.google.com/p/juced/source/browse/trunk/juce/src/extended/containers/jucetice_Hash.h

And this:

http://code.google.com/p/juced/source/browse/trunk/juce/src/extended/containers/jucetice_OwnedHash.h

You won’t notice any difference than any other Jules’ class :)[/quote]

i had to change the SocpedLock to ScopedLockType to make it compile with the latest juce, and even then it crashes when i try to add elements (i can add 3 elements then i get a crash in the add method)


#7

…strange, i’m using the ownedhash to hold hundred of pointers with no problem. i’ll give it a double check.


#8

i declare my table as:

Hash <String, EdoModulator*, StringHashFunction> modulatorNameHashJucetice;

try to add elements to it

for (int i=0; i<cache.size(); i++)
	{
		modulatorNameHashJucetice.add (cache[i]->getName(), cache[i]);
	}

and i get a crash here:

void add (const KeyType newKey, const ObjectType& newElement) throw()
    {
        const ScopedLockType sl (lock);

        const int writeIndex = HashFunctionToUse::generateHash (newKey, maxSize);
        HashEntry* entry = entries [writeIndex];

        if (! entry)
        {
            insertAt (newKey, newElement, writeIndex);
        }
        else
        {
            while (entry->nextEntry && entry->nextEntry->key != newKey) <----- CRASH
                entry = entry->nextEntry;

            if (! entry->nextEntry)
                insertAt (newKey, newElement, writeIndex);
        }
    }

some watch info on variables


-		entry	0xfdfdfdfd {key={...} element=??? nextEntry=??? }	Hash<juce::String,EdoModulator *,StringHashFunction,juce::DummyCriticalSection>::HashEntry *
+		key	{empty={...} text=??? }	juce::String
		element	CXX0017: Error: symbol "" not found	
		nextEntry	CXX0030: Error: expression cannot be evaluated	
-		newKey	{empty={...} text=0x0275602c "evoBPM" }	juce::String
+		empty	{empty={...} text=0x008c3edc "" }	juce::String
+		text	0x0275602c "evoBPM"	wchar_t *
-		entries	0x02742d50	Hash<juce::String,EdoModulator *,StringHashFunction,juce::DummyCriticalSection>::HashEntry * *
-			0x00000000 {key={...} element=??? nextEntry=??? }	Hash<juce::String,EdoModulator *,StringHashFunction,juce::DummyCriticalSection>::HashEntry *
+		key	{empty={...} text=??? }	juce::String
		element	CXX0030: Error: expression cannot be evaluated	
		nextEntry	CXX0030: Error: expression cannot be evaluated	
-		newElement	0x0271c4c8	EdoModulator * const &
+			0x0271c4c8 {currentValue=-1.0000000000000000 maxValue=1.0000000000000000 minValue=0.00000000000000000 ...}	EdoModulator * const

#9

MMMh, i can use both of those hashes:

class StringHashFunction
{
public:
    static int generateHash (const String& key, const int size) {
        return key.hashCode() % size;
    }
};

class Foo
{
public:
    Foo(int bar_) : (bar(bar_) { }

    int bar;
};

typedef Hash<String, Foo*, StringHashFunction> FooHash;
typedef OwnedHash<String, Foo, StringHashFunction> OwnedFooHash;

and still add thousands objects in both of them…

it seems that setAllocatedSize doesn’t clear your entries there (memory is 0xfdfdfdfd).