Boost logo

Boost Users :

Subject: Re: [Boost-users] Turning Boost.Multi-Index ordered_non_unique index into ordered_unique one, with custom comparator
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2016-03-02 10:34:24


AMDG

On 03/02/2016 03:12 AM, Dominique Devienne wrote:
> I would like to support a slightly unusual use-case.
>
> We have a class with a integer member, and a BMI container for instances of
> this class.
>
> That member is supposed to be unique, *except* for a special "invalid"
> value (-1),
> which several instances can use.
>
> Right now, we use an ordered_non_unique index, because of the -1's, but
> that leaves
> the door open for other duplicate "valid" values, which should be forbidden.
>
> So my question is whether one can index this member uniquely, thanks to a
> custom
> comparator, such that the comparator does not violate the usual
> strict-weak-ordering
> requirement of ordered containers? I'm thinking kinda like SQL NULLs, which
> don't
> compare equal to each other, i.e. in this case -1 would never equal another
> -1, and use
> another arbitrary (but stable) ordering specifically for those -1's. This
> assumes though
> I can know which instance the int member is part of, i.e. will the
> comparator get the integer

  You could use the identity key extractor so that
the comparator sees the whole object.

> by value or reference, and if a reference, is that a reference to the
> "internal" element,
> such that the arbitrary order can be the address of the element for
> example?

  You can't use the address, because copies
need to be equivalent. You might be able
to get away with it if you make the elements
non-copyable and only insert them with emplace.

> Or to avoid
> element movement in case of reallocs in the container, can the comparator
> access an
> iterator to project it into another random-access index of the same
> container, to use that
> index's integer-index as the arbitrary order?
>

  The comparator needs to work on objects that
are not in the container. Otherwise, you
would have problems with insertion and lookup.

> I'd appreciate some feedback/insights into the above. Thanks, --DD
>

In Christ,
Steven Watanabe


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