Has anybody experienced that when testing a #tDictionary key the returned value is incorrect (meaning, two different values have a key collision)?

Has anybody experienced that when testing a #tDictionary key the returned value is incorrect (meaning, two different values have a key collision)?

My tDictionary has an estimated number of over 6k collisions, meaning that after inserting all values I get about 17k results when iterating over the available keys, but I should get 23k. The key is basically a string made of two IP sockets (IPAddress A, Port A, IP Address B, Port B, separated by dashes), so it's not that short.

Maybe I'm doing something wrong here. Is tDictionary supposed to have key collisions, and if so, how do I insert values that can be retrieved afterwards?

Comments

  1. Be careful with the terminology here. When talking about hash maps (which the TDictionary is), a collision usually means that two different keys are mapped to the same bin. This can happen even though the keys have different hash values.

    How a collision is handled is implementation specific, but in case of TDictionary it handles them transparently so that the elements can still be accessed as if no collision happened.

    As for your issue, one brute force way would be to store the key and value in a TPair, and for each time you add a key-value pair into the dictionary, append the TPair to a TList.

    After each dictionary operation, call a function which iterates over all the elements in the list and check that the keys in the dictionary still matches the elements, if not asm int 3 end;

    ReplyDelete
  2. Asbjørn Heid
    thanks, I was wondering if I overlooked some sort of method with which I'd have to iterate through elements in a bin. But since I had not found any I assumed tDictionary to handle this transparently - thanks for confirming that (and for the idea of how to check if the elements can be found)

    ReplyDelete
  3. Sorry, guys, my bad - I put the test for collisions in a bad place where one of the two values weren't yet retrieved, so basically tDictionary is working fine, it was just a bad compare. I am still missing 6k connections, but it's obviously not the tDictionary's fault. I'll have to look somewhere else now. Thanks!

    ReplyDelete

Post a Comment