2008-06-30 18:07:38 +00:00
|
|
|
//==--- ImmutableList.h - Immutable (functional) list interface --*- 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 ImmutableList class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
template <typename T>
|
|
|
|
class ImmutableListImpl : public FoldingSetNode {
|
|
|
|
T Head;
|
2008-10-21 05:59:33 +00:00
|
|
|
const ImmutableListImpl* Tail;
|
2008-06-30 18:07:38 +00:00
|
|
|
|
2008-10-21 05:59:33 +00:00
|
|
|
ImmutableListImpl(const T& head, const ImmutableListImpl* tail = 0)
|
2008-06-30 18:07:38 +00:00
|
|
|
: Head(head), Tail(tail) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
friend class ImmutableListFactory<T>;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
// Do not implement.
|
|
|
|
void operator=(const ImmutableListImpl&);
|
|
|
|
ImmutableListImpl(const ImmutableListImpl&);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
public:
|
|
|
|
const T& getHead() const { return Head; }
|
2008-10-21 05:59:33 +00:00
|
|
|
const ImmutableListImpl* getTail() const { return Tail; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
static inline void Profile(FoldingSetNodeID& ID, const T& H,
|
2008-10-21 05:59:33 +00:00
|
|
|
const ImmutableListImpl* L){
|
2008-06-30 18:07:38 +00:00
|
|
|
ID.AddPointer(L);
|
|
|
|
ID.Add(H);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
void Profile(FoldingSetNodeID& ID) {
|
|
|
|
Profile(ID, Head, Tail);
|
|
|
|
}
|
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
/// ImmutableList - This class represents an immutable (functional) list.
|
|
|
|
/// It is implemented as a smart pointer (wraps ImmutableListImpl), so it
|
|
|
|
/// it is intended to always be copied by value as if it were a pointer.
|
|
|
|
/// This interface matches ImmutableSet and ImmutableMap. ImmutableList
|
|
|
|
/// objects should almost never be created directly, and instead should
|
|
|
|
/// be created by ImmutableListFactory objects that manage the lifetime
|
|
|
|
/// of a group of lists. When the factory object is reclaimed, all lists
|
|
|
|
/// created by that factory are released as well.
|
|
|
|
template <typename T>
|
|
|
|
class ImmutableList {
|
|
|
|
public:
|
|
|
|
typedef T value_type;
|
|
|
|
typedef ImmutableListFactory<T> Factory;
|
|
|
|
|
|
|
|
private:
|
2008-10-21 05:59:33 +00:00
|
|
|
const ImmutableListImpl<T>* X;
|
2008-06-30 18:07:38 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
// This constructor should normally only be called by ImmutableListFactory<T>.
|
|
|
|
// There may be cases, however, when one needs to extract the internal pointer
|
|
|
|
// and reconstruct a list object from that pointer.
|
2008-10-21 05:59:33 +00:00
|
|
|
ImmutableList(const ImmutableListImpl<T>* x = 0) : X(x) {}
|
2008-06-30 18:07:38 +00:00
|
|
|
|
2008-10-21 05:59:33 +00:00
|
|
|
const ImmutableListImpl<T>* getInternalPointer() const {
|
2008-06-30 18:07:38 +00:00
|
|
|
return X;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
class iterator {
|
2008-10-21 05:59:33 +00:00
|
|
|
const ImmutableListImpl<T>* L;
|
2008-06-30 18:07:38 +00:00
|
|
|
public:
|
|
|
|
iterator() : L(0) {}
|
|
|
|
iterator(ImmutableList l) : L(l.getInternalPointer()) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-07-02 00:06:55 +00:00
|
|
|
iterator& operator++() { L = L->getTail(); return *this; }
|
2008-06-30 18:07:38 +00:00
|
|
|
bool operator==(const iterator& I) const { return L == I.L; }
|
2008-07-02 00:06:55 +00:00
|
|
|
bool operator!=(const iterator& I) const { return L != I.L; }
|
2009-01-09 19:25:42 +00:00
|
|
|
const value_type& operator*() const { return L->getHead(); }
|
|
|
|
ImmutableList getList() const { return L; }
|
2008-06-30 18:07:38 +00:00
|
|
|
};
|
|
|
|
|
2008-06-30 20:41:22 +00:00
|
|
|
/// begin - Returns an iterator referring to the head of the list, or
|
|
|
|
/// an iterator denoting the end of the list if the list is empty.
|
2008-06-30 18:07:38 +00:00
|
|
|
iterator begin() const { return iterator(X); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 20:41:22 +00:00
|
|
|
/// end - Returns an iterator denoting the end of the list. This iterator
|
|
|
|
/// does not refer to a valid list element.
|
2008-06-30 18:07:38 +00:00
|
|
|
iterator end() const { return iterator(); }
|
|
|
|
|
2008-06-30 20:41:22 +00:00
|
|
|
/// isEmpty - Returns true if the list is empty.
|
2008-06-30 18:07:38 +00:00
|
|
|
bool isEmpty() const { return !X; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 20:41:22 +00:00
|
|
|
/// isEqual - Returns true if two lists are equal. Because all lists created
|
|
|
|
/// from the same ImmutableListFactory are uniqued, this has O(1) complexity
|
|
|
|
/// because it the contents of the list do not need to be compared. Note
|
|
|
|
/// that you should only compare two lists created from the same
|
|
|
|
/// ImmutableListFactory.
|
2009-01-09 19:25:42 +00:00
|
|
|
bool isEqual(const ImmutableList& L) const { return X == L.X; }
|
2008-06-30 20:41:22 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
bool operator==(const ImmutableList& L) const { return isEqual(L); }
|
|
|
|
|
2008-06-30 20:41:22 +00:00
|
|
|
/// getHead - Returns the head of the list.
|
2008-06-30 18:07:38 +00:00
|
|
|
const T& getHead() {
|
|
|
|
assert (!isEmpty() && "Cannot get the head of an empty list.");
|
|
|
|
return X->getHead();
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 20:41:22 +00:00
|
|
|
/// getTail - Returns the tail of the list, which is another (possibly empty)
|
|
|
|
/// ImmutableList.
|
2008-06-30 18:07:38 +00:00
|
|
|
ImmutableList getTail() {
|
|
|
|
return X ? X->getTail() : 0;
|
2009-01-09 19:25:42 +00:00
|
|
|
}
|
2008-11-15 09:26:02 +00:00
|
|
|
|
|
|
|
void Profile(FoldingSetNodeID& ID) const {
|
|
|
|
ID.AddPointer(X);
|
|
|
|
}
|
2008-06-30 18:07:38 +00:00
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
template <typename T>
|
|
|
|
class ImmutableListFactory {
|
2009-01-09 19:25:42 +00:00
|
|
|
typedef ImmutableListImpl<T> ListTy;
|
2008-06-30 18:07:38 +00:00
|
|
|
typedef FoldingSet<ListTy> CacheTy;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
CacheTy Cache;
|
|
|
|
uintptr_t Allocator;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
bool ownsAllocator() const {
|
|
|
|
return Allocator & 0x1 ? false : true;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
BumpPtrAllocator& getAllocator() const {
|
2008-06-30 18:07:38 +00:00
|
|
|
return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
|
2009-01-09 19:25:42 +00:00
|
|
|
}
|
2008-06-30 18:07:38 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ImmutableListFactory()
|
|
|
|
: Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
ImmutableListFactory(BumpPtrAllocator& Alloc)
|
|
|
|
: Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
~ImmutableListFactory() {
|
|
|
|
if (ownsAllocator()) delete &getAllocator();
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
ImmutableList<T> Concat(const T& Head, ImmutableList<T> Tail) {
|
|
|
|
// Profile the new list to see if it already exists in our cache.
|
|
|
|
FoldingSetNodeID ID;
|
|
|
|
void* InsertPos;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-10-21 05:59:33 +00:00
|
|
|
const ListTy* TailImpl = Tail.getInternalPointer();
|
2008-06-30 18:07:38 +00:00
|
|
|
ListTy::Profile(ID, Head, TailImpl);
|
|
|
|
ListTy* L = Cache.FindNodeOrInsertPos(ID, InsertPos);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
if (!L) {
|
|
|
|
// The list does not exist in our cache. Create it.
|
|
|
|
BumpPtrAllocator& A = getAllocator();
|
|
|
|
L = (ListTy*) A.Allocate<ListTy>();
|
|
|
|
new (L) ListTy(Head, TailImpl);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
// Insert the new list into the cache.
|
|
|
|
Cache.InsertNode(L, InsertPos);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
return L;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
ImmutableList<T> Add(const T& D, ImmutableList<T> L) {
|
|
|
|
return Concat(D, L);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
ImmutableList<T> GetEmptyList() const {
|
|
|
|
return ImmutableList<T>(0);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
ImmutableList<T> Create(const T& X) {
|
|
|
|
return Concat(X, GetEmptyList());
|
|
|
|
}
|
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-16 04:14:33 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-07-11 22:43:07 +00:00
|
|
|
// Partially-specialized Traits.
|
2008-11-16 04:14:33 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-07-11 22:43:07 +00:00
|
|
|
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());
|
2009-01-09 19:25:42 +00:00
|
|
|
return (unsigned((uintptr_t)PtrVal) >> 4) ^
|
2008-07-11 22:43:07 +00:00
|
|
|
(unsigned((uintptr_t)PtrVal) >> 9);
|
|
|
|
}
|
|
|
|
static bool isEqual(ImmutableList<T> X1, ImmutableList<T> X2) {
|
|
|
|
return X1 == X2;
|
|
|
|
}
|
|
|
|
static bool isPod() { return true; }
|
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-30 18:07:38 +00:00
|
|
|
} // end llvm namespace
|
|
|
|
|
|
|
|
#endif
|