Boost logo

Boost Users :

Subject: Re: [Boost-users] [MultIndex] Iterator/Range validity when modify entries.
From: Deniz Bahadir (dbahadir_at_[hidden])
Date: 2016-09-08 13:17:14


Am 08.09.2016 um 18:39 schrieb Joaquin M López Muñoz:
> El 08/09/2016 a las 18:22, Deniz Bahadir escribió:

Hi Joaquín,

[...]

> You got lucky :-) if key1 is not modified, then the element maintains
> its position in
> Index1 and you can safely continue iterating. The reference guarantees
> that much:
>
> http://www.boost.org/libs/multi_index/doc/reference/hash_indices.html#modify

Looking at the analog section of "replace", I assume I still would be
lucky if the replaced entry would have the same value for key1. :-)

[...]

> The only potential problem would be if modify fails (i.e. it returns
> false) in which case
> the element is erased and the iterator is no longer valid: in your code
> this can't happen
> as Index2 (the index whose key you're changing) is non-unique.
>
>> My follow-up question is then:
>> If the range is indeed invalidated, what would be a working solution
>> for that problem?
>
> This can only happen, as said above, when there is a collision (the
> index is unique and you've
> duplicated the key).

[...]

>> <code>
>>
>> auto& index1 = db.get<Index1>();
>> auto range = index1.equal_range(42);
>>
>> if (range.first != range.second)
>> {
>> auto it = range.first;
>> auto end = --range.second;
>> do {
>> index1.modify(it++, [](Entry& entry)
>> {
>> entry.key2 = 123; // <---- Does this invalidate my range?
>> // entry.key1 is not getting modified!
>> });
>> while (it != end);
>> }
>>
>> </code>
>
> Yep, that'd do, but you don't really need to take this precaution here as
> already explained.

So, just to make sure:
This second solution would even work if Index2 is unique and I have a
collision with key2 (as my example code would definitively have)?

However, the side effect would be, that I end up erasing all elements of
my range except for the first? (Or even erasing that first element, too,
if the database already contains another element outside my range with
that same value for key2.)

>> [...]
>>
>> And one last question:
>> Does this problem or its solution apply to all index-types or just to
>> hashed indexes?
>
> All index types provide the same guarantees wrt modify.
>
>> PS: Oh and by the way: Boost.MultiIndex is a really cool library. :-)
>
> Thank you!
>
> Joaquín M López Muñoz

Thank you very much, that was a really fast and satisfying answer. :-)

Deniz


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net