Boost logo

Boost Users :

Subject: [Boost-users] Turning Boost.Multi-Index ordered_non_unique index into ordered_unique one, with custom comparator
From: Dominique Devienne (ddevienne_at_[hidden])
Date: 2016-03-02 05:12:02

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
comparator, such that the comparator does not violate the usual
requirement of ordered containers? I'm thinking kinda like SQL NULLs, which
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
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? 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?

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

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at