#ifndef wasm_analysis_lattices_vector_h
#define wasm_analysis_lattices_vector_h
#include <vector>
#include "../lattice.h"
#include "bool.h"
#include "flat.h"
namespace wasm::analysis {
template<Lattice L> struct Vector {
using Element = std::vector<typename L::Element>;
struct SingletonElement : std::pair<size_t, typename L::Element> {
SingletonElement(size_t i, typename L::Element&& elem)
: std::pair<size_t, typename L::Element>{i, std::move(elem)} {}
};
L lattice;
const size_t size;
Vector(L&& lattice, size_t size) : lattice(std::move(lattice)), size(size) {}
Element getBottom() const noexcept {
return Element(size, lattice.getBottom());
}
Element getTop() const noexcept { return Element(size, lattice.getTop()); }
LatticeComparison compare(const Element& a, const Element& b) const noexcept {
assert(a.size() == size);
assert(b.size() == size);
auto result = EQUAL;
for (size_t i = 0; i < size; ++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 {
assert(joinee.size() == size);
assert(joiner.size() == size);
bool result = false;
for (size_t i = 0; i < size; ++i) {
result |= joinAtIndex(joinee, i, joiner[i]);
}
return result;
}
bool join(Element& joinee, const SingletonElement& joiner) const noexcept {
const auto& [index, elem] = joiner;
assert(index < joinee.size());
return joinAtIndex(joinee, index, elem);
}
bool meet(Element& meetee, const Element& meeter) const noexcept {
assert(meetee.size() == size);
assert(meeter.size() == size);
bool result = false;
for (size_t i = 0; i < size; ++i) {
result |= meetAtIndex(meetee, i, meeter[i]);
}
return result;
}
bool meet(Element& meetee, const SingletonElement& meeter) const noexcept {
const auto& [index, elem] = meeter;
assert(index < meetee.size());
return meetAtIndex(meetee, index, elem);
}
private:
bool joinAtIndex(Element& joinee,
size_t i,
const typename L::Element& elem) const noexcept {
if constexpr (std::is_same_v<typename L::Element, bool>) {
bool e = joinee[i];
if (lattice.join(e, elem)) {
joinee[i] = e;
return true;
}
return false;
} else {
return lattice.join(joinee[i], elem);
}
}
bool meetAtIndex(Element& meetee,
size_t i,
const typename L::Element& elem) const noexcept {
if constexpr (std::is_same_v<typename L::Element, bool>) {
bool e = meetee[i];
if (lattice.meet(e, elem)) {
meetee[i] = e;
return true;
}
return false;
} else {
return lattice.meet(meetee[i], elem);
}
}
};
template<typename L> Vector(L&&, size_t) -> Vector<L>;
#if __cplusplus >= 202002L
static_assert(FullLattice<Vector<Bool>>);
static_assert(Lattice<Vector<Flat<bool>>>);
#endif
}
#endif