2010-02-01 11:43:31 +01: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:
|
|
|
|
uint64_t Start;
|
|
|
|
uint64_t End;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Interval(uint64_t S, uint64_t E) : Start(S), End(E) {}
|
|
|
|
|
|
|
|
uint64_t getStart() const { return Start; }
|
|
|
|
uint64_t getEnd() const { return End; }
|
|
|
|
};
|
|
|
|
|
|
|
|
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 06:23:23 +01: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 11:43:31 +01: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 10:04:11 +01:00
|
|
|
ImutIntervalAVLFactory(BumpPtrAllocator &Alloc)
|
|
|
|
: ImutAVLFactory<ImutInfo>(Alloc) {}
|
|
|
|
|
2010-02-02 06:23:23 +01:00
|
|
|
TreeTy *Add(TreeTy *T, value_type_ref V) {
|
2010-02-01 11:43:31 +01:00
|
|
|
T = Add_internal(V,T);
|
2010-02-08 19:08:46 +01:00
|
|
|
this->MarkImmutable(T);
|
2010-02-01 11:43:31 +01:00
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
2010-02-02 06:23:23 +01:00
|
|
|
TreeTy *Find(TreeTy *T, key_type_ref K) {
|
|
|
|
if (!T)
|
|
|
|
return NULL;
|
|
|
|
|
2010-02-08 19:08:46 +01:00
|
|
|
key_type_ref CurrentKey = ImutInfo::KeyOfValue(this->Value(T));
|
2010-02-02 06:23:23 +01:00
|
|
|
|
|
|
|
if (ImutInfo::isContainedIn(K, CurrentKey))
|
|
|
|
return T;
|
|
|
|
else if (ImutInfo::isLess(K, CurrentKey))
|
2010-02-08 19:08:46 +01:00
|
|
|
return Find(this->Left(T), K);
|
2010-02-02 06:23:23 +01:00
|
|
|
else
|
2010-02-08 19:08:46 +01:00
|
|
|
return Find(this->Right(T), K);
|
2010-02-02 06:23:23 +01:00
|
|
|
}
|
|
|
|
|
2010-02-01 11:43:31 +01:00
|
|
|
private:
|
|
|
|
TreeTy *Add_internal(value_type_ref V, TreeTy *T) {
|
2010-02-02 03:40:56 +01:00
|
|
|
key_type_ref K = ImutInfo::KeyOfValue(V);
|
|
|
|
T = RemoveAllOverlaps(T, K);
|
2010-02-08 19:08:46 +01:00
|
|
|
if (this->isEmpty(T))
|
|
|
|
return this->CreateNode(NULL, V, NULL);
|
2010-02-01 11:43:31 +01:00
|
|
|
|
|
|
|
assert(!T->isMutable());
|
|
|
|
|
2010-02-08 19:08:46 +01:00
|
|
|
key_type_ref KCurrent = ImutInfo::KeyOfValue(this->Value(T));
|
2010-02-01 11:43:31 +01:00
|
|
|
|
|
|
|
if (ImutInfo::isLess(K, KCurrent))
|
2010-02-08 19:08:46 +01:00
|
|
|
return this->Balance(Add_internal(V, this->Left(T)), this->Value(T), this->Right(T));
|
2010-02-01 11:43:31 +01:00
|
|
|
else
|
2010-02-08 19:08:46 +01:00
|
|
|
return this->Balance(this->Left(T), this->Value(T), Add_internal(V, this->Right(T)));
|
2010-02-01 11:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all overlaps from T.
|
|
|
|
TreeTy *RemoveAllOverlaps(TreeTy *T, key_type_ref K) {
|
2010-02-02 07:22:08 +01:00
|
|
|
bool Changed;
|
2010-02-01 11:43:31 +01:00
|
|
|
do {
|
2010-02-02 07:22:08 +01:00
|
|
|
Changed = false;
|
|
|
|
T = RemoveOverlap(T, K, Changed);
|
2010-02-08 19:08:46 +01:00
|
|
|
this->MarkImmutable(T);
|
2010-02-02 07:22:08 +01:00
|
|
|
} while (Changed);
|
2010-02-02 03:40:56 +01:00
|
|
|
|
2010-02-02 07:22:08 +01:00
|
|
|
return T;
|
2010-02-01 11:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove one overlap from T.
|
2010-02-02 07:22:08 +01:00
|
|
|
TreeTy *RemoveOverlap(TreeTy *T, key_type_ref K, bool &Changed) {
|
2010-02-02 03:40:56 +01:00
|
|
|
if (!T)
|
|
|
|
return NULL;
|
2010-02-08 19:08:46 +01:00
|
|
|
Interval CurrentK = ImutInfo::KeyOfValue(this->Value(T));
|
2010-02-01 11:43:31 +01:00
|
|
|
|
|
|
|
// If current key does not overlap the inserted key.
|
|
|
|
if (CurrentK.getStart() > K.getEnd())
|
2010-02-08 19:08:46 +01:00
|
|
|
return this->Balance(RemoveOverlap(this->Left(T), K, Changed), this->Value(T), this->Right(T));
|
2010-02-01 11:43:31 +01:00
|
|
|
else if (CurrentK.getEnd() < K.getStart())
|
2010-02-08 19:08:46 +01:00
|
|
|
return this->Balance(this->Left(T), this->Value(T), RemoveOverlap(this->Right(T), K, Changed));
|
2010-02-01 11:43:31 +01:00
|
|
|
|
|
|
|
// Current key overlaps with the inserted key.
|
|
|
|
// Remove the current key.
|
2010-02-02 07:22:08 +01:00
|
|
|
Changed = true;
|
2010-02-08 19:08:46 +01:00
|
|
|
data_type_ref OldData = ImutInfo::DataOfValue(this->Value(T));
|
|
|
|
T = this->Remove_internal(CurrentK, T);
|
2010-02-01 11:43:31 +01: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-02-02 07:33:32 +01:00
|
|
|
return Add_internal(std::make_pair(NewK, OldData), T);
|
2010-02-01 11:43:31 +01:00
|
|
|
} else {
|
|
|
|
Interval NewK1(CurrentK.getStart(), K.getStart()-1);
|
2010-02-02 07:33:32 +01:00
|
|
|
T = Add_internal(std::make_pair(NewK1, OldData), T);
|
|
|
|
|
2010-02-01 11:43:31 +01:00
|
|
|
Interval NewK2(K.getEnd()+1, CurrentK.getEnd());
|
2010-02-02 07:33:32 +01:00
|
|
|
return Add_internal(std::make_pair(NewK2, OldData), T);
|
2010-02-01 11:43:31 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (CurrentK.getEnd() > K.getEnd()) {
|
|
|
|
Interval NewK(K.getEnd()+1, CurrentK.getEnd());
|
2010-02-02 07:33:32 +01:00
|
|
|
return Add_internal(std::make_pair(NewK, OldData), T);
|
2010-02-02 08:05:31 +01:00
|
|
|
} else
|
|
|
|
return T;
|
2010-02-01 11:43:31 +01: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 10:04:11 +01:00
|
|
|
Factory(BumpPtrAllocator& Alloc) : F(Alloc) {}
|
|
|
|
|
2010-02-01 11:43:31 +01:00
|
|
|
ImmutableIntervalMap GetEmptyMap() {
|
|
|
|
return ImmutableIntervalMap(F.GetEmptyTree());
|
|
|
|
}
|
|
|
|
|
|
|
|
ImmutableIntervalMap Add(ImmutableIntervalMap Old,
|
|
|
|
key_type_ref K, data_type_ref D) {
|
|
|
|
TreeTy *T = F.Add(Old.Root, std::make_pair<key_type, data_type>(K, D));
|
|
|
|
return ImmutableIntervalMap(F.GetCanonicalTree(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
ImmutableIntervalMap Remove(ImmutableIntervalMap Old, key_type_ref K) {
|
|
|
|
TreeTy *T = F.Remove(Old.Root, K);
|
|
|
|
return ImmutableIntervalMap(F.GetCanonicalTree(T));
|
|
|
|
}
|
2010-02-02 06:23:23 +01:00
|
|
|
|
|
|
|
data_type *Lookup(ImmutableIntervalMap M, key_type_ref K) {
|
|
|
|
TreeTy *T = F.Find(M.getRoot(), K);
|
|
|
|
if (T)
|
|
|
|
return &T->getValue().second;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-01 11:43:31 +01:00
|
|
|
};
|
2010-02-02 06:23:23 +01: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 11:43:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|