1 //==--- ImmutableList.h - Immutable (functional) list interface --*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ImmutableList class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_IMLIST_H
15 #define LLVM_ADT_IMLIST_H
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/System/DataTypes.h"
24 template <typename T> class ImmutableListFactory;
27 class ImmutableListImpl : public FoldingSetNode {
29 const ImmutableListImpl* Tail;
31 ImmutableListImpl(const T& head, const ImmutableListImpl* tail = 0)
32 : Head(head), Tail(tail) {}
34 friend class ImmutableListFactory<T>;
37 void operator=(const ImmutableListImpl&);
38 ImmutableListImpl(const ImmutableListImpl&);
41 const T& getHead() const { return Head; }
42 const ImmutableListImpl* getTail() const { return Tail; }
44 static inline void Profile(FoldingSetNodeID& ID, const T& H,
45 const ImmutableListImpl* L){
50 void Profile(FoldingSetNodeID& ID) {
51 Profile(ID, Head, Tail);
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.
67 typedef ImmutableListFactory<T> Factory;
70 const ImmutableListImpl<T>* X;
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(const ImmutableListImpl<T>* x = 0) : X(x) {}
78 const ImmutableListImpl<T>* getInternalPointer() const {
83 const ImmutableListImpl<T>* L;
86 iterator(ImmutableList l) : L(l.getInternalPointer()) {}
88 iterator& operator++() { L = L->getTail(); return *this; }
89 bool operator==(const iterator& I) const { return L == I.L; }
90 bool operator!=(const iterator& I) const { return L != I.L; }
91 const value_type& operator*() const { return L->getHead(); }
92 ImmutableList getList() const { return L; }
95 /// begin - Returns an iterator referring to the head of the list, or
96 /// an iterator denoting the end of the list if the list is empty.
97 iterator begin() const { return iterator(X); }
99 /// end - Returns an iterator denoting the end of the list. This iterator
100 /// does not refer to a valid list element.
101 iterator end() const { return iterator(); }
103 /// isEmpty - Returns true if the list is empty.
104 bool isEmpty() const { return !X; }
106 /// isEqual - Returns true if two lists are equal. Because all lists created
107 /// from the same ImmutableListFactory are uniqued, this has O(1) complexity
108 /// because it the contents of the list do not need to be compared. Note
109 /// that you should only compare two lists created from the same
110 /// ImmutableListFactory.
111 bool isEqual(const ImmutableList& L) const { return X == L.X; }
113 bool operator==(const ImmutableList& L) const { return isEqual(L); }
115 /// getHead - Returns the head of the list.
117 assert (!isEmpty() && "Cannot get the head of an empty list.");
121 /// getTail - Returns the tail of the list, which is another (possibly empty)
123 ImmutableList getTail() {
124 return X ? X->getTail() : 0;
127 void Profile(FoldingSetNodeID& ID) const {
132 template <typename T>
133 class ImmutableListFactory {
134 typedef ImmutableListImpl<T> ListTy;
135 typedef FoldingSet<ListTy> CacheTy;
140 bool ownsAllocator() const {
141 return Allocator & 0x1 ? false : true;
144 BumpPtrAllocator& getAllocator() const {
145 return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
149 ImmutableListFactory()
150 : Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
152 ImmutableListFactory(BumpPtrAllocator& Alloc)
153 : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
155 ~ImmutableListFactory() {
156 if (ownsAllocator()) delete &getAllocator();
159 ImmutableList<T> Concat(const T& Head, ImmutableList<T> Tail) {
160 // Profile the new list to see if it already exists in our cache.
164 const ListTy* TailImpl = Tail.getInternalPointer();
165 ListTy::Profile(ID, Head, TailImpl);
166 ListTy* L = Cache.FindNodeOrInsertPos(ID, InsertPos);
169 // The list does not exist in our cache. Create it.
170 BumpPtrAllocator& A = getAllocator();
171 L = (ListTy*) A.Allocate<ListTy>();
172 new (L) ListTy(Head, TailImpl);
174 // Insert the new list into the cache.
175 Cache.InsertNode(L, InsertPos);
181 ImmutableList<T> Add(const T& D, ImmutableList<T> L) {
185 ImmutableList<T> GetEmptyList() const {
186 return ImmutableList<T>(0);
189 ImmutableList<T> Create(const T& X) {
190 return Concat(X, GetEmptyList());
194 //===----------------------------------------------------------------------===//
195 // Partially-specialized Traits.
196 //===----------------------------------------------------------------------===//
198 template<typename T> struct DenseMapInfo;
199 template<typename T> struct DenseMapInfo<ImmutableList<T> > {
200 static inline ImmutableList<T> getEmptyKey() {
201 return reinterpret_cast<ImmutableListImpl<T>*>(-1);
203 static inline ImmutableList<T> getTombstoneKey() {
204 return reinterpret_cast<ImmutableListImpl<T>*>(-2);
206 static unsigned getHashValue(ImmutableList<T> X) {
207 uintptr_t PtrVal = reinterpret_cast<uintptr_t>(X.getInternalPointer());
208 return (unsigned((uintptr_t)PtrVal) >> 4) ^
209 (unsigned((uintptr_t)PtrVal) >> 9);
211 static bool isEqual(ImmutableList<T> X1, ImmutableList<T> X2) {
216 template <typename T> struct isPodLike;
217 template <typename T>
218 struct isPodLike<ImmutableList<T> > { static const bool value = true; };
220 } // end llvm namespace