#ifndef LLVM_ADT_IMLIST_H
#define LLVM_ADT_IMLIST_H
#include "llvm/Support/Allocator.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/Support/DataTypes.h"
#include <cassert>
namespace llvm {
template <typename T> class ImmutableListFactory;
template <typename T>
class ImmutableListImpl : public FoldingSetNode {
T Head;
const ImmutableListImpl* Tail;
ImmutableListImpl(const T& head, const ImmutableListImpl* tail = 0)
: Head(head), Tail(tail) {}
friend class ImmutableListFactory<T>;
void operator=(const ImmutableListImpl&) LLVM_DELETED_FUNCTION;
ImmutableListImpl(const ImmutableListImpl&) LLVM_DELETED_FUNCTION;
public:
const T& getHead() const { return Head; }
const ImmutableListImpl* getTail() const { return Tail; }
static inline void Profile(FoldingSetNodeID& ID, const T& H,
const ImmutableListImpl* L){
ID.AddPointer(L);
ID.Add(H);
}
void Profile(FoldingSetNodeID& ID) {
Profile(ID, Head, Tail);
}
};
template <typename T>
class ImmutableList {
public:
typedef T value_type;
typedef ImmutableListFactory<T> Factory;
private:
const ImmutableListImpl<T>* X;
public:
ImmutableList(const ImmutableListImpl<T>* x = 0) : X(x) {}
const ImmutableListImpl<T>* getInternalPointer() const {
return X;
}
class iterator {
const ImmutableListImpl<T>* L;
public:
iterator() : L(0) {}
iterator(ImmutableList l) : L(l.getInternalPointer()) {}
iterator& operator++() { L = L->getTail(); return *this; }
bool operator==(const iterator& I) const { return L == I.L; }
bool operator!=(const iterator& I) const { return L != I.L; }
const value_type& operator*() const { return L->getHead(); }
ImmutableList getList() const { return L; }
};
iterator begin() const { return iterator(X); }
iterator end() const { return iterator(); }
bool isEmpty() const { return !X; }
bool contains(const T& V) const {
for (iterator I = begin(), E = end(); I != E; ++I) {
if (*I == V)
return true;
}
return false;
}
bool isEqual(const ImmutableList& L) const { return X == L.X; }
bool operator==(const ImmutableList& L) const { return isEqual(L); }
const T& getHead() {
assert (!isEmpty() && "Cannot get the head of an empty list.");
return X->getHead();
}
ImmutableList getTail() {
return X ? X->getTail() : 0;
}
void Profile(FoldingSetNodeID& ID) const {
ID.AddPointer(X);
}
};
template <typename T>
class ImmutableListFactory {
typedef ImmutableListImpl<T> ListTy;
typedef FoldingSet<ListTy> CacheTy;
CacheTy Cache;
uintptr_t Allocator;
bool ownsAllocator() const {
return Allocator & 0x1 ? false : true;
}
BumpPtrAllocator& getAllocator() const {
return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
}
public:
ImmutableListFactory()
: Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
ImmutableListFactory(BumpPtrAllocator& Alloc)
: Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
~ImmutableListFactory() {
if (ownsAllocator()) delete &getAllocator();
}
ImmutableList<T> concat(const T& Head, ImmutableList<T> Tail) {
FoldingSetNodeID ID;
void* InsertPos;
const ListTy* TailImpl = Tail.getInternalPointer();
ListTy::Profile(ID, Head, TailImpl);
ListTy* L = Cache.FindNodeOrInsertPos(ID, InsertPos);
if (!L) {
BumpPtrAllocator& A = getAllocator();
L = (ListTy*) A.Allocate<ListTy>();
new (L) ListTy(Head, TailImpl);
Cache.InsertNode(L, InsertPos);
}
return L;
}
ImmutableList<T> add(const T& D, ImmutableList<T> L) {
return concat(D, L);
}
ImmutableList<T> getEmptyList() const {
return ImmutableList<T>(0);
}
ImmutableList<T> create(const T& X) {
return Concat(X, getEmptyList());
}
};
template<typename T> struct DenseMapInfo;
template<typename T> struct DenseMapInfo<ImmutableList<T> > {
static inline ImmutableList<T> getEmptyKey() {
return reinterpret_cast<ImmutableListImpl<T>*>(-1);
}
static inline ImmutableList<T> getTombstoneKey() {
return reinterpret_cast<ImmutableListImpl<T>*>(-2);
}
static unsigned getHashValue(ImmutableList<T> X) {
uintptr_t PtrVal = reinterpret_cast<uintptr_t>(X.getInternalPointer());
return (unsigned((uintptr_t)PtrVal) >> 4) ^
(unsigned((uintptr_t)PtrVal) >> 9);
}
static bool isEqual(ImmutableList<T> X1, ImmutableList<T> X2) {
return X1 == X2;
}
};
template <typename T> struct isPodLike;
template <typename T>
struct isPodLike<ImmutableList<T> > { static const bool value = true; };
}
#endif