LLVM API Documentation

ImmutableList.h

Go to the documentation of this file.
00001 //==--- ImmutableList.h - Immutable (functional) list interface --*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the ImmutableList class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ADT_IMLIST_H
00015 #define LLVM_ADT_IMLIST_H
00016 
00017 #include "llvm/Support/Allocator.h"
00018 #include "llvm/ADT/FoldingSet.h"
00019 #include "llvm/Support/DataTypes.h"
00020 #include <cassert>
00021 
00022 namespace llvm {
00023 
00024 template <typename T> class ImmutableListFactory;
00025 
00026 template <typename T>
00027 class ImmutableListImpl : public FoldingSetNode {
00028   T Head;
00029   const ImmutableListImpl* Tail;
00030 
00031   ImmutableListImpl(const T& head, const ImmutableListImpl* tail = 0)
00032     : Head(head), Tail(tail) {}
00033 
00034   friend class ImmutableListFactory<T>;
00035 
00036   // Do not implement.
00037   void operator=(const ImmutableListImpl&);
00038   ImmutableListImpl(const ImmutableListImpl&);
00039 
00040 public:
00041   const T& getHead() const { return Head; }
00042   const ImmutableListImpl* getTail() const { return Tail; }
00043 
00044   static inline void Profile(FoldingSetNodeID& ID, const T& H,
00045                              const ImmutableListImpl* L){
00046     ID.AddPointer(L);
00047     ID.Add(H);
00048   }
00049 
00050   void Profile(FoldingSetNodeID& ID) {
00051     Profile(ID, Head, Tail);
00052   }
00053 };
00054 
00055 /// ImmutableList - This class represents an immutable (functional) list.
00056 ///  It is implemented as a smart pointer (wraps ImmutableListImpl), so it
00057 ///  it is intended to always be copied by value as if it were a pointer.
00058 ///  This interface matches ImmutableSet and ImmutableMap.  ImmutableList
00059 ///  objects should almost never be created directly, and instead should
00060 ///  be created by ImmutableListFactory objects that manage the lifetime
00061 ///  of a group of lists.  When the factory object is reclaimed, all lists
00062 ///  created by that factory are released as well.
00063 template <typename T>
00064 class ImmutableList {
00065 public:
00066   typedef T value_type;
00067   typedef ImmutableListFactory<T> Factory;
00068 
00069 private:
00070   const ImmutableListImpl<T>* X;
00071 
00072 public:
00073   // This constructor should normally only be called by ImmutableListFactory<T>.
00074   // There may be cases, however, when one needs to extract the internal pointer
00075   // and reconstruct a list object from that pointer.
00076   ImmutableList(const ImmutableListImpl<T>* x = 0) : X(x) {}
00077 
00078   const ImmutableListImpl<T>* getInternalPointer() const {
00079     return X;
00080   }
00081 
00082   class iterator {
00083     const ImmutableListImpl<T>* L;
00084   public:
00085     iterator() : L(0) {}
00086     iterator(ImmutableList l) : L(l.getInternalPointer()) {}
00087 
00088     iterator& operator++() { L = L->getTail(); return *this; }
00089     bool operator==(const iterator& I) const { return L == I.L; }
00090     bool operator!=(const iterator& I) const { return L != I.L; }
00091     const value_type& operator*() const { return L->getHead(); }
00092     ImmutableList getList() const { return L; }
00093   };
00094 
00095   /// begin - Returns an iterator referring to the head of the list, or
00096   ///  an iterator denoting the end of the list if the list is empty.
00097   iterator begin() const { return iterator(X); }
00098 
00099   /// end - Returns an iterator denoting the end of the list.  This iterator
00100   ///  does not refer to a valid list element.
00101   iterator end() const { return iterator(); }
00102 
00103   /// isEmpty - Returns true if the list is empty.
00104   bool isEmpty() const { return !X; }
00105 
00106   /// isEqual - Returns true if two lists are equal.  Because all lists created
00107   ///  from the same ImmutableListFactory are uniqued, this has O(1) complexity
00108   ///  because it the contents of the list do not need to be compared.  Note
00109   ///  that you should only compare two lists created from the same
00110   ///  ImmutableListFactory.
00111   bool isEqual(const ImmutableList& L) const { return X == L.X; }
00112 
00113   bool operator==(const ImmutableList& L) const { return isEqual(L); }
00114 
00115   /// getHead - Returns the head of the list.
00116   const T& getHead() {
00117     assert (!isEmpty() && "Cannot get the head of an empty list.");
00118     return X->getHead();
00119   }
00120 
00121   /// getTail - Returns the tail of the list, which is another (possibly empty)
00122   ///  ImmutableList.
00123   ImmutableList getTail() {
00124     return X ? X->getTail() : 0;
00125   }
00126 
00127   void Profile(FoldingSetNodeID& ID) const {
00128     ID.AddPointer(X);
00129   }
00130 };
00131 
00132 template <typename T>
00133 class ImmutableListFactory {
00134   typedef ImmutableListImpl<T> ListTy;
00135   typedef FoldingSet<ListTy>   CacheTy;
00136 
00137   CacheTy Cache;
00138   uintptr_t Allocator;
00139 
00140   bool ownsAllocator() const {
00141     return Allocator & 0x1 ? false : true;
00142   }
00143 
00144   BumpPtrAllocator& getAllocator() const {
00145     return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
00146   }
00147 
00148 public:
00149   ImmutableListFactory()
00150     : Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
00151 
00152   ImmutableListFactory(BumpPtrAllocator& Alloc)
00153   : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
00154 
00155   ~ImmutableListFactory() {
00156     if (ownsAllocator()) delete &getAllocator();
00157   }
00158 
00159   ImmutableList<T> Concat(const T& Head, ImmutableList<T> Tail) {
00160     // Profile the new list to see if it already exists in our cache.
00161     FoldingSetNodeID ID;
00162     void* InsertPos;
00163 
00164     const ListTy* TailImpl = Tail.getInternalPointer();
00165     ListTy::Profile(ID, Head, TailImpl);
00166     ListTy* L = Cache.FindNodeOrInsertPos(ID, InsertPos);
00167 
00168     if (!L) {
00169       // The list does not exist in our cache.  Create it.
00170       BumpPtrAllocator& A = getAllocator();
00171       L = (ListTy*) A.Allocate<ListTy>();
00172       new (L) ListTy(Head, TailImpl);
00173 
00174       // Insert the new list into the cache.
00175       Cache.InsertNode(L, InsertPos);
00176     }
00177 
00178     return L;
00179   }
00180 
00181   ImmutableList<T> Add(const T& D, ImmutableList<T> L) {
00182     return Concat(D, L);
00183   }
00184 
00185   ImmutableList<T> GetEmptyList() const {
00186     return ImmutableList<T>(0);
00187   }
00188 
00189   ImmutableList<T> Create(const T& X) {
00190     return Concat(X, GetEmptyList());
00191   }
00192 };
00193 
00194 //===----------------------------------------------------------------------===//
00195 // Partially-specialized Traits.
00196 //===----------------------------------------------------------------------===//
00197 
00198 template<typename T> struct DenseMapInfo;
00199 template<typename T> struct DenseMapInfo<ImmutableList<T> > {
00200   static inline ImmutableList<T> getEmptyKey() {
00201     return reinterpret_cast<ImmutableListImpl<T>*>(-1);
00202   }
00203   static inline ImmutableList<T> getTombstoneKey() {
00204     return reinterpret_cast<ImmutableListImpl<T>*>(-2);
00205   }
00206   static unsigned getHashValue(ImmutableList<T> X) {
00207     uintptr_t PtrVal = reinterpret_cast<uintptr_t>(X.getInternalPointer());
00208     return (unsigned((uintptr_t)PtrVal) >> 4) ^
00209            (unsigned((uintptr_t)PtrVal) >> 9);
00210   }
00211   static bool isEqual(ImmutableList<T> X1, ImmutableList<T> X2) {
00212     return X1 == X2;
00213   }
00214   static bool isPod() { return true; }
00215 };
00216 
00217 } // end llvm namespace
00218 
00219 #endif