AnyDSL / thorin2

The Higher ORder INtermediate representation - next gen
https://anydsl.github.io/thorin2/
MIT License
46 stars 9 forks source link

Refactor/absl array #260

Closed leissa closed 9 months ago

leissa commented 9 months ago

This PR removes our home-brew Array<T> and Span<T> classes with absl::InlinedVector<T> and a new Span<T> which is only a thin wrapper for std::span<T>.

Span<T, N>

This is a thin wrapper for std::span<T, N> with the following additional features:

Vector

This thing behaves like a std::vector but is under the hood an absl::InlinedVector. The number of inlined elements is 4 for most element types.

Misc

leissa commented 9 months ago

LGTM. Only the nit that I find the vector<..>() helper a bit confusing as I am then looking for using namespace std or using std::vector to identify if this is coming from std::vector - maybe make_Vector? :)

Alright, I now subclassed absl::InlinedVector instead of aliasing it. So the original c'tors are back via:

auto vec = Vector<int>(23, [](size_t i) { return i; });

There btw seems to be a gcc problem for this:

auto vec = Vector<int>(my_range_of_ints, [](auto i) { return i; });

It's a subtle issue with template instantiation, overloading, concepts, etc. I'm pretty sure it's a gcc bug. Works with clang. As a workaround, you have to explicitly specify the argument:

auto vec = Vector<int>(my_range_of_ints, [](int i) { return i; });

Subclassing gives us another benefit to have vec.span() and vec.view() methods, which are a bit nicer to use:

for (auto elem : vec.view().subspan(1)) { /*...*/ }

instead of

for (auto elem : View(vec).subspan(1)) { /*...*/ }