Boost logo

Boost :

From: Julien Blanc (julien.blanc_at_[hidden])
Date: 2022-06-24 08:08:25


Le 2022-06-23 22:41, Hadriel Kaplan a écrit :

Hi,

Thanks for this feedback.

> Ours is purely for enum-based indexing, and only for scoped enums that
> start at value 0 and increase sequentially – i.e. contiguous, no
> “holes”. We happen to be able to verify that property of enums at
> compile-time, and can static_assert that it’s true, so ours just
> ends up doing a static_cast under-the-hood. (I can’t quite tell what
> indexed_array does in the end, since it also deals with non-contiguous
> index values.)

The current strategy is the following:
* detect if the set of indexing values is contiguous (ie, no holes).
Duplicate values are removed (this is needed to support enums with
aliases such as first or last).
   * If that's the case, use a static_cast and an offset for index
computation. If the offset happens to be zero, this is fully optimized
by the compiler.
   * if that's not the case, the current implementation use an mp_foreach
loop (iterate over all indexes to recompute the index in the underlying
array).

This detection is performed at compile time, and has been firstly
designed to support described-enums, which we get as an integer
list-like. Supporting them without run-time penalty was a design goal.

There is indeed a performance penalty with using non-contiguous enums
with the default indexer. Since several of my use cases for
non-contiguous enums involves mostly either two discontinuous ranges, or
a single range + a few single values, i may improve these specific cases
in the future (this is an implementation detail that will not change the
client-side usage of the library).

Regards,

Julien


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk