[ADT] Just use a union in IntervalMap

IntervalMap has seen type-punned arrays, AlignedCharArrayUnion and
std::aligned_union_t, with varying degrees of buggyness. Plain unions
have become quite powerful, so just try that instead.
This commit is contained in:
Benjamin Kramer
2022-02-19 20:20:52 +01:00
parent d5304d44a5
commit dc4f9f0368

View File

@@ -106,8 +106,6 @@
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/bit.h"
#include "llvm/Support/AlignOf.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/RecyclingAllocator.h"
#include <algorithm>
@@ -969,7 +967,10 @@ public:
private:
// The root data is either a RootLeaf or a RootBranchData instance.
AlignedCharArrayUnion<RootLeaf, RootBranchData> data;
union {
RootLeaf leaf;
RootBranchData branchData;
};
// Tree height.
// 0: Leaves in root.
@@ -983,25 +984,22 @@ private:
// Allocator used for creating external nodes.
Allocator &allocator;
/// Represent data as a node type without breaking aliasing rules.
template <typename T> T &dataAs() const { return *bit_cast<T *>(&data); }
const RootLeaf &rootLeaf() const {
assert(!branched() && "Cannot acces leaf data in branched root");
return dataAs<RootLeaf>();
return leaf;
}
RootLeaf &rootLeaf() {
assert(!branched() && "Cannot acces leaf data in branched root");
return dataAs<RootLeaf>();
return leaf;
}
RootBranchData &rootBranchData() const {
const RootBranchData &rootBranchData() const {
assert(branched() && "Cannot access branch data in non-branched root");
return dataAs<RootBranchData>();
return branchData;
}
RootBranchData &rootBranchData() {
assert(branched() && "Cannot access branch data in non-branched root");
return dataAs<RootBranchData>();
return branchData;
}
const RootBranch &rootBranch() const { return rootBranchData().node; }
@@ -1042,8 +1040,6 @@ private:
public:
explicit IntervalMap(Allocator &a) : height(0), rootSize(0), allocator(a) {
assert((uintptr_t(&data) & (alignof(RootLeaf) - 1)) == 0 &&
"Insufficient alignment");
new(&rootLeaf()) RootLeaf();
}