2010-02-01 10:43:31 +00:00
|
|
|
//===--- ImmutableIntervalMap.h - Immutable (functional) map ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the ImmutableIntervalMap class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class Interval {
|
|
|
|
private:
|
2010-08-09 03:43:39 +00:00
|
|
|
int64_t Start;
|
|
|
|
int64_t End;
|
2010-02-01 10:43:31 +00:00
|
|
|
|
|
|
|
public:
|
2010-08-09 03:43:39 +00:00
|
|
|
Interval(int64_t S, int64_t E) : Start(S), End(E) {}
|
2010-02-01 10:43:31 +00:00
|
|
|
|
2010-08-09 03:43:39 +00:00
|
|
|
int64_t getStart() const { return Start; }
|
|
|
|
int64_t getEnd() const { return End; }
|
2010-02-01 10:43:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct ImutIntervalInfo {
|
|
|
|
typedef const std::pair<Interval, T> value_type;
|
|
|
|
typedef const value_type &value_type_ref;
|
|
|
|
typedef const Interval key_type;
|
|
|
|
typedef const Interval &key_type_ref;
|
|
|
|
typedef const T data_type;
|
|
|
|
typedef const T &data_type_ref;
|
|
|
|
|
|
|
|
static key_type_ref KeyOfValue(value_type_ref V) {
|
|
|
|
return V.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
static data_type_ref DataOfValue(value_type_ref V) {
|
|
|
|
return V.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isEqual(key_type_ref L, key_type_ref R) {
|
|
|
|
return L.getStart() == R.getStart() && L.getEnd() == R.getEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isDataEqual(data_type_ref L, data_type_ref R) {
|
|
|
|
return ImutContainerInfo<T>::isEqual(L,R);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isLess(key_type_ref L, key_type_ref R) {
|
|
|
|
// Assume L and R does not overlap.
|
|
|
|
if (L.getStart() < R.getStart()) {
|
|
|
|
assert(L.getEnd() < R.getStart());
|
|
|
|
return true;
|
|
|
|
} else if (L.getStart() == R.getStart()) {
|
|
|
|
assert(L.getEnd() == R.getEnd());
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
assert(L.getStart() > R.getEnd());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-02 05:23:23 +00:00
|
|
|
static bool isContainedIn(key_type_ref K, key_type_ref L) {
|
|
|
|
if (K.getStart() >= L.getStart() && K.getEnd() <= L.getEnd())
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-01 10:43:31 +00:00
|
|
|
static void Profile(FoldingSetNodeID &ID, value_type_ref V) {
|
|
|
|
ID.AddInteger(V.first.getStart());
|
|
|
|
ID.AddInteger(V.first.getEnd());
|
|
|
|
ImutProfileInfo<T>::Profile(ID, V.second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ImutInfo>
|
|
|
|
class ImutIntervalAVLFactory : public ImutAVLFactory<ImutInfo> {
|
|
|
|
typedef ImutAVLTree<ImutInfo> TreeTy;
|
|
|
|
typedef typename ImutInfo::value_type value_type;
|
|
|
|
typedef typename ImutInfo::value_type_ref value_type_ref;
|
|
|
|
typedef typename ImutInfo::key_type key_type;
|
|
|
|
typedef typename ImutInfo::key_type_ref key_type_ref;
|
|
|
|
typedef typename ImutInfo::data_type data_type;
|
|
|
|
typedef typename ImutInfo::data_type_ref data_type_ref;
|
|
|
|
|
|
|
|
public:
|
2010-02-03 09:04:11 +00:00
|
|
|
ImutIntervalAVLFactory(BumpPtrAllocator &Alloc)
|
|
|
|
: ImutAVLFactory<ImutInfo>(Alloc) {}
|
|
|
|
|
2010-02-02 05:23:23 +00:00
|
|
|
TreeTy *Add(TreeTy *T, value_type_ref V) {
|
2010-11-24 00:54:28 +00:00
|
|
|
T = add_internal(V,T);
|
2010-02-08 18:08:46 +00:00
|
|
|
this->MarkImmutable(T);
|
2010-02-01 10:43:31 +00:00
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
2010-02-02 05:23:23 +00:00
|
|
|
TreeTy *Find(TreeTy *T, key_type_ref K) {
|
|
|
|
if (!T)
|
|
|
|
return NULL;
|
|
|
|
|
2010-11-24 00:54:28 +00:00
|
|
|
key_type_ref CurrentKey = ImutInfo::KeyOfValue(this->getValue(T));
|
2010-02-02 05:23:23 +00:00
|
|
|
|
|
|
|
if (ImutInfo::isContainedIn(K, CurrentKey))
|
|
|
|
return T;
|
|
|
|
else if (ImutInfo::isLess(K, CurrentKey))
|
2010-11-24 00:54:28 +00:00
|
|
|
return Find(this->getLeft(T), K);
|
2010-02-02 05:23:23 +00:00
|
|
|
else
|
2010-11-24 00:54:28 +00:00
|
|
|
return Find(this->getRight(T), K);
|
2010-02-02 05:23:23 +00:00
|
|
|
}
|
|
|
|
|
2010-02-01 10:43:31 +00:00
|
|
|
private:
|
2010-11-24 00:54:28 +00:00
|
|
|
TreeTy *add_internal(value_type_ref V, TreeTy *T) {
|
2010-02-02 02:40:56 +00:00
|
|
|
key_type_ref K = ImutInfo::KeyOfValue(V);
|
2010-11-24 00:54:28 +00:00
|
|
|
T = removeAllOverlaps(T, K);
|
2010-02-08 18:08:46 +00:00
|
|
|
if (this->isEmpty(T))
|
|
|
|
return this->CreateNode(NULL, V, NULL);
|
2010-02-01 10:43:31 +00:00
|
|
|
|
|
|
|
assert(!T->isMutable());
|
|
|
|
|
2010-02-08 18:08:46 +00:00
|
|
|
key_type_ref KCurrent = ImutInfo::KeyOfValue(this->Value(T));
|
2010-02-01 10:43:31 +00:00
|
|
|
|
|
|
|
if (ImutInfo::isLess(K, KCurrent))
|
2010-11-24 00:54:28 +00:00
|
|
|
return this->Balance(add_internal(V, this->Left(T)), this->Value(T),
|
2010-07-13 05:50:08 +00:00
|
|
|
this->Right(T));
|
2010-02-01 10:43:31 +00:00
|
|
|
else
|
2010-07-13 05:50:08 +00:00
|
|
|
return this->Balance(this->Left(T), this->Value(T),
|
2010-11-24 00:54:28 +00:00
|
|
|
add_internal(V, this->Right(T)));
|
2010-02-01 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all overlaps from T.
|
2010-11-24 00:54:28 +00:00
|
|
|
TreeTy *removeAllOverlaps(TreeTy *T, key_type_ref K) {
|
2010-02-02 06:22:08 +00:00
|
|
|
bool Changed;
|
2010-02-01 10:43:31 +00:00
|
|
|
do {
|
2010-02-02 06:22:08 +00:00
|
|
|
Changed = false;
|
2010-11-24 00:54:28 +00:00
|
|
|
T = removeOverlap(T, K, Changed);
|
|
|
|
this->markImmutable(T);
|
2010-02-02 06:22:08 +00:00
|
|
|
} while (Changed);
|
2010-02-02 02:40:56 +00:00
|
|
|
|
2010-02-02 06:22:08 +00:00
|
|
|
return T;
|
2010-02-01 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove one overlap from T.
|
2010-11-24 00:54:28 +00:00
|
|
|
TreeTy *removeOverlap(TreeTy *T, key_type_ref K, bool &Changed) {
|
2010-02-02 02:40:56 +00:00
|
|
|
if (!T)
|
|
|
|
return NULL;
|
2010-02-08 18:08:46 +00:00
|
|
|
Interval CurrentK = ImutInfo::KeyOfValue(this->Value(T));
|
2010-02-01 10:43:31 +00:00
|
|
|
|
|
|
|
// If current key does not overlap the inserted key.
|
|
|
|
if (CurrentK.getStart() > K.getEnd())
|
2010-11-24 00:54:28 +00:00
|
|
|
return this->Balance(removeOverlap(this->Left(T), K, Changed),
|
2010-07-13 05:50:08 +00:00
|
|
|
this->Value(T), this->Right(T));
|
2010-02-01 10:43:31 +00:00
|
|
|
else if (CurrentK.getEnd() < K.getStart())
|
2010-07-13 05:50:08 +00:00
|
|
|
return this->Balance(this->Left(T), this->Value(T),
|
2010-11-24 00:54:28 +00:00
|
|
|
removeOverlap(this->Right(T), K, Changed));
|
2010-02-01 10:43:31 +00:00
|
|
|
|
|
|
|
// Current key overlaps with the inserted key.
|
|
|
|
// Remove the current key.
|
2010-02-02 06:22:08 +00:00
|
|
|
Changed = true;
|
2010-02-08 18:08:46 +00:00
|
|
|
data_type_ref OldData = ImutInfo::DataOfValue(this->Value(T));
|
|
|
|
T = this->Remove_internal(CurrentK, T);
|
2010-02-01 10:43:31 +00:00
|
|
|
// Add back the unoverlapped part of the current key.
|
|
|
|
if (CurrentK.getStart() < K.getStart()) {
|
|
|
|
if (CurrentK.getEnd() <= K.getEnd()) {
|
|
|
|
Interval NewK(CurrentK.getStart(), K.getStart()-1);
|
2010-11-24 00:54:28 +00:00
|
|
|
return add_internal(std::make_pair(NewK, OldData), T);
|
2010-02-01 10:43:31 +00:00
|
|
|
} else {
|
|
|
|
Interval NewK1(CurrentK.getStart(), K.getStart()-1);
|
2010-11-24 00:54:28 +00:00
|
|
|
T = add_internal(std::make_pair(NewK1, OldData), T);
|
2010-02-02 06:33:32 +00:00
|
|
|
|
2010-02-01 10:43:31 +00:00
|
|
|
Interval NewK2(K.getEnd()+1, CurrentK.getEnd());
|
2010-11-24 00:54:28 +00:00
|
|
|
return add_internal(std::make_pair(NewK2, OldData), T);
|
2010-02-01 10:43:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (CurrentK.getEnd() > K.getEnd()) {
|
|
|
|
Interval NewK(K.getEnd()+1, CurrentK.getEnd());
|
2010-11-24 00:54:28 +00:00
|
|
|
return add_internal(std::make_pair(NewK, OldData), T);
|
2010-02-02 07:05:31 +00:00
|
|
|
} else
|
|
|
|
return T;
|
2010-02-01 10:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// ImmutableIntervalMap maps an interval [start, end] to a value. The intervals
|
|
|
|
/// in the map are guaranteed to be disjoint.
|
|
|
|
template <typename ValT>
|
|
|
|
class ImmutableIntervalMap
|
|
|
|
: public ImmutableMap<Interval, ValT, ImutIntervalInfo<ValT> > {
|
|
|
|
|
|
|
|
typedef typename ImutIntervalInfo<ValT>::value_type value_type;
|
|
|
|
typedef typename ImutIntervalInfo<ValT>::value_type_ref value_type_ref;
|
|
|
|
typedef typename ImutIntervalInfo<ValT>::key_type key_type;
|
|
|
|
typedef typename ImutIntervalInfo<ValT>::key_type_ref key_type_ref;
|
|
|
|
typedef typename ImutIntervalInfo<ValT>::data_type data_type;
|
|
|
|
typedef typename ImutIntervalInfo<ValT>::data_type_ref data_type_ref;
|
|
|
|
typedef ImutAVLTree<ImutIntervalInfo<ValT> > TreeTy;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ImmutableIntervalMap(TreeTy *R)
|
|
|
|
: ImmutableMap<Interval, ValT, ImutIntervalInfo<ValT> >(R) {}
|
|
|
|
|
|
|
|
class Factory {
|
|
|
|
ImutIntervalAVLFactory<ImutIntervalInfo<ValT> > F;
|
|
|
|
|
|
|
|
public:
|
2010-02-03 09:04:11 +00:00
|
|
|
Factory(BumpPtrAllocator& Alloc) : F(Alloc) {}
|
|
|
|
|
2010-11-24 00:54:28 +00:00
|
|
|
ImmutableIntervalMap getEmptyMap() {
|
|
|
|
return ImmutableIntervalMap(F.getEmptyTree());
|
2010-02-01 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
2010-11-24 00:54:28 +00:00
|
|
|
ImmutableIntervalMap add(ImmutableIntervalMap Old,
|
2010-02-01 10:43:31 +00:00
|
|
|
key_type_ref K, data_type_ref D) {
|
2011-02-22 22:48:47 +00:00
|
|
|
TreeTy *T = F.add(Old.Root, std::pair<key_type, data_type>(K, D));
|
2010-11-24 00:54:28 +00:00
|
|
|
return ImmutableIntervalMap(F.getCanonicalTree(T));
|
2010-02-01 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
2010-11-24 00:54:28 +00:00
|
|
|
ImmutableIntervalMap remove(ImmutableIntervalMap Old, key_type_ref K) {
|
|
|
|
TreeTy *T = F.remove(Old.Root, K);
|
|
|
|
return ImmutableIntervalMap(F.getCanonicalTree(T));
|
2010-02-01 10:43:31 +00:00
|
|
|
}
|
2010-02-02 05:23:23 +00:00
|
|
|
|
2010-11-24 00:54:28 +00:00
|
|
|
data_type *lookup(ImmutableIntervalMap M, key_type_ref K) {
|
2010-02-02 05:23:23 +00:00
|
|
|
TreeTy *T = F.Find(M.getRoot(), K);
|
|
|
|
if (T)
|
|
|
|
return &T->getValue().second;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-01 10:43:31 +00:00
|
|
|
};
|
2010-02-02 05:23:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// For ImmutableIntervalMap, the lookup operation has to be done by the
|
|
|
|
// factory.
|
|
|
|
data_type* lookup(key_type_ref K) const;
|
2010-02-01 10:43:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|