Thread: HashTable help?

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything directly wrong with it.

    I'm a bit confused as to what this does:
    Code:
        char* val = HashTableLookUp(this, key);
        StringDestruct(val);
        val = StringConstruct(value);
        return false;
    Mostly confuzzled as to the purpose of the line in red is...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #32
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    StringConstruct just does a malloc of the size of the string in the heap.. it just create a brand new copy of the given string on the heap.. just to simplify so we don't have to write malloc and copy th e string to the heap using strcpy..

  3. #33
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Don't you want to inseart the new alllocated string into the HashTable?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #34
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes I do, but doesn't val right now already points to the new allocated String, which is in the heap?? val here is just the address of the value of a given node if I change that adress to the address of the new string in the heap then doesn't that mean that the hash table points to that
    Last edited by -EquinoX-; 03-28-2008 at 03:42 PM.

  5. #35
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    val here is just the address of the value of a given node
    You just replaced that addres - now you need to put it into the list - otherwise list will still hold the old address - which is already invalid due to Destruct called
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #36
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh I see what you mean, so how do I adjust it? I can't use the HashTableLookup then?? I have to first use the for loop...

    and anyway what I don't understand is that list has the .value and .key there, I changed the .value to another address...
    Last edited by -EquinoX-; 03-28-2008 at 03:52 PM.

  7. #37
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would modify the HashTableLookUp function to actually return pointer to the pointer to string instead of pointer to string

    in this way you would be able replace the address of the string inplace
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #38
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I am doing this for an assignment an it has to follow the signature so that's why I can only return it to a pointer to a char...

  9. #39
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    val is just the .value part of the node that has the corresponding .key

    so to be said more clearly the method HashTableInsert should first check if the key exists in the hash table, okay and say we do find that key.. we first have to find the location of the linked list in the hash table by finding the value of the key and say we found it! YAY.. it's on X... X is the head of the linked list.. and there are other nodes in the X linked list with many different keys and one of the keys is the value were looking for.. and say we found that node, which has the key we're looking for.. that node has a .key file and also a .value file.. so here the job of the method is to modify the .value part.. do you get what I mean?
    Last edited by -EquinoX-; 03-28-2008 at 04:03 PM.

  10. #40
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    anyone??

  11. #41
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by -EquinoX- View Post
    anyone??
    It's not clear what your question is, or what your code that you posted is supposed to do. If the string is already in the table, shouldn't you update the value, as opposed to deleting the string and leaving the node hanging there with no data in it?

  12. #42
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes this is what I mean, I want to update the string with the latest one if the same key is going to be inserted with a certain value

    I am not leaving the node hanging in there, I am trying to update the value with the latest one.. so am I doing it wrong??

    Code:
    char* val = HashTableLookUp(this, key);
        StringDestruct(val);
        val = StringConstruct(value);
        return false;
    HashTableLookUp just returns the address where the old value that is going to be replaced with the one is, after that I did a string destruct, to free the memory in the heap that is used by the old value and construct a new value in the heap by using the string construct and assign the address of that to val

  13. #43
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Except that you absolutely positively under no circumstances want to take anything, at all, ever, out of your linked list in a function called "insert".

    If you find a match on the key, then there's no point in taking out the new key and putting the same exact key in, is there? You can maybe update the value, if that's what you're supposed to do upon finding a duplicate. But you're deleting the key and then not putting the (same) key back -- your third line above assigns a new value to val, it doesn't reassign the pointer in the hash table linked list itself. But that's all irrelevant, because you don't want to delete anything from the list.

  14. #44
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    no I understand what you mean, I am not suppose to change the key if I am trying to insert a key that already exists in the hash table.. I need to update the value.. that I understand

    the pointer in the hash table linked list.. so I can't use the HashTableLookUp then.. I'll have to use a for loop to loop from the start of the linked list to find the key that I want to update the value and then from there I just update the pointer?? is that what you mean?

  15. #45
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by -EquinoX- View Post
    no I understand what you mean, I am not suppose to change the key if I am trying to insert a key that already exists in the hash table.. I need to update the value.. that I understand
    If this was true, you wouldn't have typed the next paragraph.

    Quote Originally Posted by -EquinoX- View Post
    the pointer in the hash table linked list.. so I can't use the HashTableLookUp then.. I'll have to use a for loop to loop from the start of the linked list to find the key that I want to update the value and then from there I just update the pointer?? is that what you mean?
    You had it down earlier, you just didn't bother to code it up:
    1. Hash the key.
    2. Go to hashtable[that_hashed_value], which is a linked list.
    3. Go down the linked list (not a for loop, but walking a list_t pointer).
    4. If you find a node in the list with the key you're holding, change the value of that node (i.e., change list_ptr->value). (If you start fiddling with the key, you have lost.)
    5. If you walk off the end of the list, then the key wasn't there, so malloc a new node and insert it into the list (you have this part).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hashtable v.s. Dictionary
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-06-2008, 02:09 AM
  2. Missing Entries in Hashtable
    By wuzzo87 in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 12:46 AM
  3. Replies: 12
    Last Post: 10-22-2006, 08:37 PM
  4. game hashtable
    By disks86 in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2005, 01:42 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM