Fix: Complete recent fix to array_utils boolean mix utility

1d53025868f8954bc1b975a5af5f3fca1f981e1b missed the fix to the VArray case.
This commit is contained in:
Hans Goudey 2024-06-14 18:44:03 -04:00
parent 6edeae5c90
commit 9266fbab02

@ -92,6 +92,17 @@ void invert_booleans(MutableSpan<bool> span, const IndexMask &mask)
mask.foreach_index_optimized<int64_t>([&](const int64_t i) { span[i] = !span[i]; });
}
static bool all_equal(const Span<bool> span, const bool test)
{
return std::all_of(span.begin(), span.end(), [&](const bool value) { return value == test; });
}
static bool all_equal(const VArray<bool> &varray, const IndexRange range, const bool test)
{
return std::all_of(
range.begin(), range.end(), [&](const int64_t i) { return varray[i] == test; });
}
BooleanMix booleans_mix_calc(const VArray<bool> &varray, const IndexRange range_to_check)
{
if (varray.is_empty()) {
@ -111,16 +122,13 @@ BooleanMix booleans_mix_calc(const VArray<bool> &varray, const IndexRange range_
if (init == BooleanMix::Mixed) {
return init;
}
const Span<bool> slice = span.slice(range);
const bool compare_value = (init == BooleanMix::None) ? slice.first() :
(init == BooleanMix::AllTrue);
for (const bool value : slice) {
if (value != compare_value) {
return BooleanMix::Mixed;
}
const bool compare = (init == BooleanMix::None) ? slice.first() :
(init == BooleanMix::AllTrue);
if (all_equal(slice, compare)) {
return compare ? BooleanMix::AllTrue : BooleanMix::AllFalse;
}
return compare_value ? BooleanMix::AllTrue : BooleanMix::AllFalse;
return BooleanMix::Mixed;
},
[&](BooleanMix a, BooleanMix b) { return (a == b) ? a : BooleanMix::Mixed; });
}
@ -133,13 +141,12 @@ BooleanMix booleans_mix_calc(const VArray<bool> &varray, const IndexRange range_
return init;
}
/* Alternatively, this could use #materialize to retrieve many values at once. */
const bool first = varray[range.first()];
for (const int64_t i : range.drop_front(1)) {
if (varray[i] != first) {
return BooleanMix::Mixed;
}
const bool compare = (init == BooleanMix::None) ? varray[range.first()] :
(init == BooleanMix::AllTrue);
if (all_equal(varray, range, compare)) {
return compare ? BooleanMix::AllTrue : BooleanMix::AllFalse;
}
return first ? BooleanMix::AllTrue : BooleanMix::AllFalse;
return BooleanMix::Mixed;
},
[&](BooleanMix a, BooleanMix b) { return (a == b) ? a : BooleanMix::Mixed; });
}