Added ImmutableList, a companion ADT to ImmutableSet and ImmutableMap that is used...
[oota-llvm.git] / include / llvm / ADT / ImmutableList.h
1 //==--- ImmutableList.h - Immutable (functional) list interface --*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ImmutableList class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_IMLIST_H
15 #define LLVM_ADT_IMLIST_H
16
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/Support/DataTypes.h"
20 #include <cassert>
21
22 namespace llvm {
23
24 template <typename T> class ImmutableListFactory;
25   
26 template <typename T>
27 class ImmutableListImpl : public FoldingSetNode {
28   T Head;
29   ImmutableListImpl* Tail;
30
31   ImmutableListImpl(const T& head, ImmutableListImpl* tail = 0)
32     : Head(head), Tail(tail) {}
33   
34   friend class ImmutableListFactory<T>;
35   
36   // Do not implement.
37   void operator=(const ImmutableListImpl&);
38   ImmutableListImpl(const ImmutableListImpl&);
39   
40 public:
41   const T& getHead() const { return Head; }
42   ImmutableListImpl* getTail() const { return Tail; }
43   
44   static inline void Profile(FoldingSetNodeID& ID, const T& H,
45                              ImmutableListImpl* L){
46     ID.AddPointer(L);
47     ID.Add(H);
48   }
49   
50   void Profile(FoldingSetNodeID& ID) {
51     Profile(ID, Head, Tail);
52   }
53 };
54   
55 /// ImmutableList - This class represents an immutable (functional) list.
56 ///  It is implemented as a smart pointer (wraps ImmutableListImpl), so it
57 ///  it is intended to always be copied by value as if it were a pointer.
58 ///  This interface matches ImmutableSet and ImmutableMap.  ImmutableList
59 ///  objects should almost never be created directly, and instead should
60 ///  be created by ImmutableListFactory objects that manage the lifetime
61 ///  of a group of lists.  When the factory object is reclaimed, all lists
62 ///  created by that factory are released as well.
63 template <typename T>
64 class ImmutableList {
65 public:
66   typedef T value_type;
67   typedef ImmutableListFactory<T> Factory;
68
69 private:
70   ImmutableListImpl<T>* X;
71
72 public:
73   // This constructor should normally only be called by ImmutableListFactory<T>.
74   // There may be cases, however, when one needs to extract the internal pointer
75   // and reconstruct a list object from that pointer.
76   ImmutableList(ImmutableListImpl<T>* x) : X(x) {}
77
78   ImmutableListImpl<T>* getInternalPointer() const {
79     return X;
80   }
81   
82   class iterator {
83     ImmutableListImpl<T>* L;
84   public:
85     iterator() : L(0) {}
86     iterator(ImmutableList l) : L(l.getInternalPointer()) {}
87     
88     iterator& operator++() { L = L->Tail; }
89     bool operator==(const iterator& I) const { return L == I.L; }
90     ImmutableList operator*() const { return L; }
91   };
92
93   iterator begin() const { return iterator(X); }
94   iterator end() const { return iterator(); }
95
96   bool isEmpty() const { return !X; }
97   
98   bool isEqual(const ImmutableList& L) const { return X == L.X; }  
99   bool operator==(const ImmutableList& L) const { return isEqual(L); }
100
101   const T& getHead() {
102     assert (!isEmpty() && "Cannot get the head of an empty list.");
103     return X->getHead();
104   }
105   
106   ImmutableList getTail() {
107     return X ? X->getTail() : 0;
108   }  
109 };
110   
111 template <typename T>
112 class ImmutableListFactory {
113   typedef ImmutableListImpl<T> ListTy;  
114   typedef FoldingSet<ListTy>   CacheTy;
115   
116   CacheTy Cache;
117   uintptr_t Allocator;
118   
119   bool ownsAllocator() const {
120     return Allocator & 0x1 ? false : true;
121   }
122   
123   BumpPtrAllocator& getAllocator() const { 
124     return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
125   }  
126
127 public:
128   ImmutableListFactory()
129     : Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
130   
131   ImmutableListFactory(BumpPtrAllocator& Alloc)
132   : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
133   
134   ~ImmutableListFactory() {
135     if (ownsAllocator()) delete &getAllocator();
136   }
137   
138   ImmutableList<T> Concat(const T& Head, ImmutableList<T> Tail) {
139     // Profile the new list to see if it already exists in our cache.
140     FoldingSetNodeID ID;
141     void* InsertPos;
142     
143     ListTy* TailImpl = Tail.getInternalPointer();
144     ListTy::Profile(ID, Head, TailImpl);
145     ListTy* L = Cache.FindNodeOrInsertPos(ID, InsertPos);
146     
147     if (!L) {
148       // The list does not exist in our cache.  Create it.
149       BumpPtrAllocator& A = getAllocator();
150       L = (ListTy*) A.Allocate<ListTy>();
151       new (L) ListTy(Head, TailImpl);
152     
153       // Insert the new list into the cache.
154       Cache.InsertNode(L, InsertPos);
155     }
156     
157     return L;
158   }
159   
160   ImmutableList<T> Add(const T& D, ImmutableList<T> L) {
161     return Concat(D, L);
162   }
163   
164   ImmutableList<T> GetEmptyList() const {
165     return ImmutableList<T>(0);
166   }
167   
168   ImmutableList<T> Create(const T& X) {
169     return Concat(X, GetEmptyList());
170   }
171 };
172   
173 } // end llvm namespace
174
175 #endif