tags:

views:

120

answers:

2

im trying to remove an element from my array and was wondering if I could be ultra-efficeint using multiple indirection with my int ptrs.

The maxSize for my fixed-array lies within my hashTable->maxSize

I need to be able to refer to my hashTable's private member maxSize only as a reference. The varaible: &maxSize, holds nothing, it is NULL. What has my fixed-size is hashTable->maxSize;

bool hashmap::remove(char const * const symbol)
{
int **previous_index = &maxSize;
int  *current_index  = *previous_index;

while ( current_index && 
             strcmp( symbol, hashTable[*current_index].m_symbol ) != 0 ) 
{
 previous_index = &current_index + 1;
 current_index  = *previous_index;
}
return true;
}

I've been trying to get it to compile with a *maxSize int single ptr varaible. But i have a lot of code and its all affected. hehe.

+1  A: 

A hashmap could easily utilise a std::map<std::string, list<T *>> to save a lot of time. Utilising a private map, your remove-symbol function would be as simple as:

bool hashmap::remove (const char *key)
{
    m_map.remove(key);
}
Nick Bedford
thats the ez way?
What i've written is nearly complete
It's a smarter, more efficient way. Use STL, don't fight it.
Nick Bedford
Or use other libraries like boost (which most likely would contain a hash map implementation).
Nick Bedford
Im looking for something along the lines of, "Well mr. newbie, even if you access hashTable->maxSize it still would do 'such and such'..lol, ya know?
I can't understand the code you posted above. its full of strange pointers you don't need, and the implementation looks strange for what you want to do.
Nick Bedford
+3  A: 

If you really want a hashmap, I would recommend that you directly use std::tr1::unordered_map, accepted into the TR1 of the C++ standard back in 2003 and implemented in many compilers. It will become std::unordered_map within the next standard (C++0x).

Now, if you want criticism on the code: do not use C style strings, prefer std::string. If your code is going to unconditionally return true, then consider that maybe your code should not return at all. Comment your algorithm (not each line): Search backwards until I find a match and remove it. Removal is performed ... that will help others detect where your code does not comply with your intentions. I am yet to understand how you are trying to iterate the array. The code probably differs from your intentions in more than one place.

If you are really implementing a hashmap then you should use the hash algorithm to find the element to remove (if it does exist). It does not make sense building a complex system that has O(1) search time to linearly search.

David Rodríguez - dribeas