levitation / sparsehash

Automatically exported from code.google.com/p/sparsehash
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Problem when continuously call erase() func #84

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Add some elements into dense_hash_map 
2. erase a certain element to use erase(..) function continuously.
3. sometime even if the element is in the table but "find_position" cannot find 
the one.

What is the expected output? What do you see instead?
can erase continuously.

What version of the product are you using? On what operating system?
2.0.2

Please provide any additional information below.
In the ease function, just set "consider_shrink" flag but if user call the 
function continuously, find_position can make error in "if 
(test_empty(bucknum))" line.

Reason.
   table[10] : value1
   table[11] : value2
   table[12] : value3

   if the three values have same bucknum and user call "erase(value2)" then  table[11] will be set as empty value.
   table[10] : value1
   table[11] : empty
   table[12] : value3

and if user call "erase(value3)" then  find_position will return ILLEGAL_BUCKET.

so, in my case, I use like below but there is no document for this.
    if( mymap.erase(value2))
         mymap.resize(0);

I didn't check "sparsehashtable" but same problem can be occurred.

Original issue reported on code.google.com by emd...@gmail.com on 9 May 2012 at 1:36

GoogleCodeExporter commented 9 years ago
I don't see a bug here. In your example, you say the empty value will be put in 
table[11] when you erase value2. But that shouldn't be the case. It should use 
the deleted key. If the deleted key and empty key are the same then you'll have 
problems; people using the library should take care to avoid that.

I'm marking this as CannotReproduce. Please correct me if that's incorrect.

Original comment by gp...@google.com on 9 May 2012 at 7:09

GoogleCodeExporter commented 9 years ago
You should check below my comment first.

* if the three values have same bucknum and user call "erase(value2)"
then  table[11] will be set as empty value.
   table[10] : value1
   table[11] : empty
   table[12] : value3*

dense_hash_table uses below to make buckum.

*size_type bucknum = hash(key) & bucket_count_minus_one;*

and if bucknum is same then

1. compare empty key

2. compare delete key

3. compare value key.

So, value #11 was deleted then value #12 will be not found.
Additionally, I found some comment in source but I think it's not enough.

//    2) resize(0):
//         When an item is deleted, its memory isn't freed right
//         away.  This allows you to iterate over a hashtable,
//         and call erase(), without invalidating the iterator.
//         To force the memory to be freed, call resize(0).
//         For tr1 compatibility, this can also be called as rehash(0).

-- 

/*********************************************
* @Name Bae, Dong Jin
* @Email Dongjin.Bae@access-company.com
* @Company Access Seoul Co., Ltd.
* @WebPage http://www.access-company.com
* @Comment Fools ignore complexity.
* Pragmatists suffer it. Some can avoid it.
* Geniuses remove it.
***********************************************/

Original comment by emd...@gmail.com on 10 May 2012 at 12:38