#ifndef wasm_analysis_lattices_array_h
#define wasm_analysis_lattices_array_h
#include <array>
#include <utility>
#include "../lattice.h"
#include "bool.h"
#include "flat.h"
namespace wasm::analysis {
template<Lattice L, size_t N> struct Array {
using Element = std::array<typename L::Element, N>;
L lattice;
Array(L&& lattice) : lattice(std::move(lattice)) {}
private:
template<size_t... I>
Element getBottomImpl(std::index_sequence<I...>) const noexcept {
return {((void)I, lattice.getBottom())...};
}
template<size_t... I>
Element getTopImpl(std::index_sequence<I...>) const noexcept {
return {((void)I, lattice.getTop())...};
}
public:
Element getBottom() const noexcept {
return getBottomImpl(std::make_index_sequence<N>());
}
Element getTop() const noexcept
#if __cplusplus >= 202002L
requires FullLattice<L>
#endif
{
return getTopImpl(std::make_index_sequence<N>());
}
LatticeComparison compare(const Element& a, const Element& b) const noexcept {
auto result = EQUAL;
for (size_t i = 0; i < N; ++i) {
switch (lattice.compare(a[i], b[i])) {
case NO_RELATION:
return NO_RELATION;
case EQUAL:
continue;
case LESS:
if (result == GREATER) {
return NO_RELATION;
}
result = LESS;
continue;
case GREATER:
if (result == LESS) {
return NO_RELATION;
}
result = GREATER;
continue;
}
}
return result;
}
bool join(Element& joinee, const Element& joiner) const noexcept {
bool result = false;
for (size_t i = 0; i < N; ++i) {
result |= lattice.join(joinee[i], joiner[i]);
}
return result;
}
bool meet(Element& meetee, const Element& meeter) const noexcept
#if __cplusplus >= 202002L
requires FullLattice<L>
#endif
{
bool result = false;
for (size_t i = 0; i < N; ++i) {
result |= lattice.meet(meetee[i], meeter[i]);
}
return result;
}
};
#if __cplusplus >= 202002L
static_assert(FullLattice<Array<Bool, 1>>);
static_assert(Lattice<Array<Flat<bool>, 1>>);
#endif
}
#endif