ImmutableList.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==--- ImmutableList.h - Immutable (functional) list interface --*- C++ -*-==//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file defines the ImmutableList class.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ADT_IMMUTABLELIST_H
  19. #define LLVM_ADT_IMMUTABLELIST_H
  20. #include "llvm/ADT/FoldingSet.h"
  21. #include "llvm/Support/Allocator.h"
  22. #include <cassert>
  23. #include <cstdint>
  24. #include <new>
  25. namespace llvm {
  26. template <typename T> class ImmutableListFactory;
  27. template <typename T>
  28. class ImmutableListImpl : public FoldingSetNode {
  29. friend class ImmutableListFactory<T>;
  30. T Head;
  31. const ImmutableListImpl* Tail;
  32. template <typename ElemT>
  33. ImmutableListImpl(ElemT &&head, const ImmutableListImpl *tail = nullptr)
  34. : Head(std::forward<ElemT>(head)), Tail(tail) {}
  35. public:
  36. ImmutableListImpl(const ImmutableListImpl &) = delete;
  37. ImmutableListImpl &operator=(const ImmutableListImpl &) = delete;
  38. const T& getHead() const { return Head; }
  39. const ImmutableListImpl* getTail() const { return Tail; }
  40. static inline void Profile(FoldingSetNodeID& ID, const T& H,
  41. const ImmutableListImpl* L){
  42. ID.AddPointer(L);
  43. ID.Add(H);
  44. }
  45. void Profile(FoldingSetNodeID& ID) {
  46. Profile(ID, Head, Tail);
  47. }
  48. };
  49. /// ImmutableList - This class represents an immutable (functional) list.
  50. /// It is implemented as a smart pointer (wraps ImmutableListImpl), so it
  51. /// it is intended to always be copied by value as if it were a pointer.
  52. /// This interface matches ImmutableSet and ImmutableMap. ImmutableList
  53. /// objects should almost never be created directly, and instead should
  54. /// be created by ImmutableListFactory objects that manage the lifetime
  55. /// of a group of lists. When the factory object is reclaimed, all lists
  56. /// created by that factory are released as well.
  57. template <typename T>
  58. class ImmutableList {
  59. public:
  60. using value_type = T;
  61. using Factory = ImmutableListFactory<T>;
  62. static_assert(std::is_trivially_destructible<T>::value,
  63. "T must be trivially destructible!");
  64. private:
  65. const ImmutableListImpl<T>* X;
  66. public:
  67. // This constructor should normally only be called by ImmutableListFactory<T>.
  68. // There may be cases, however, when one needs to extract the internal pointer
  69. // and reconstruct a list object from that pointer.
  70. ImmutableList(const ImmutableListImpl<T>* x = nullptr) : X(x) {}
  71. const ImmutableListImpl<T>* getInternalPointer() const {
  72. return X;
  73. }
  74. class iterator {
  75. const ImmutableListImpl<T>* L = nullptr;
  76. public:
  77. iterator() = default;
  78. iterator(ImmutableList l) : L(l.getInternalPointer()) {}
  79. iterator& operator++() { L = L->getTail(); return *this; }
  80. bool operator==(const iterator& I) const { return L == I.L; }
  81. bool operator!=(const iterator& I) const { return L != I.L; }
  82. const value_type& operator*() const { return L->getHead(); }
  83. const std::remove_reference_t<value_type> *operator->() const {
  84. return &L->getHead();
  85. }
  86. ImmutableList getList() const { return L; }
  87. };
  88. /// begin - Returns an iterator referring to the head of the list, or
  89. /// an iterator denoting the end of the list if the list is empty.
  90. iterator begin() const { return iterator(X); }
  91. /// end - Returns an iterator denoting the end of the list. This iterator
  92. /// does not refer to a valid list element.
  93. iterator end() const { return iterator(); }
  94. /// isEmpty - Returns true if the list is empty.
  95. bool isEmpty() const { return !X; }
  96. bool contains(const T& V) const {
  97. for (iterator I = begin(), E = end(); I != E; ++I) {
  98. if (*I == V)
  99. return true;
  100. }
  101. return false;
  102. }
  103. /// isEqual - Returns true if two lists are equal. Because all lists created
  104. /// from the same ImmutableListFactory are uniqued, this has O(1) complexity
  105. /// because it the contents of the list do not need to be compared. Note
  106. /// that you should only compare two lists created from the same
  107. /// ImmutableListFactory.
  108. bool isEqual(const ImmutableList& L) const { return X == L.X; }
  109. bool operator==(const ImmutableList& L) const { return isEqual(L); }
  110. /// getHead - Returns the head of the list.
  111. const T& getHead() const {
  112. assert(!isEmpty() && "Cannot get the head of an empty list.");
  113. return X->getHead();
  114. }
  115. /// getTail - Returns the tail of the list, which is another (possibly empty)
  116. /// ImmutableList.
  117. ImmutableList getTail() const {
  118. return X ? X->getTail() : nullptr;
  119. }
  120. void Profile(FoldingSetNodeID& ID) const {
  121. ID.AddPointer(X);
  122. }
  123. };
  124. template <typename T>
  125. class ImmutableListFactory {
  126. using ListTy = ImmutableListImpl<T>;
  127. using CacheTy = FoldingSet<ListTy>;
  128. CacheTy Cache;
  129. uintptr_t Allocator;
  130. bool ownsAllocator() const {
  131. return (Allocator & 0x1) == 0;
  132. }
  133. BumpPtrAllocator& getAllocator() const {
  134. return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
  135. }
  136. public:
  137. ImmutableListFactory()
  138. : Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
  139. ImmutableListFactory(BumpPtrAllocator& Alloc)
  140. : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
  141. ~ImmutableListFactory() {
  142. if (ownsAllocator()) delete &getAllocator();
  143. }
  144. template <typename ElemT>
  145. [[nodiscard]] ImmutableList<T> concat(ElemT &&Head, ImmutableList<T> Tail) {
  146. // Profile the new list to see if it already exists in our cache.
  147. FoldingSetNodeID ID;
  148. void* InsertPos;
  149. const ListTy* TailImpl = Tail.getInternalPointer();
  150. ListTy::Profile(ID, Head, TailImpl);
  151. ListTy* L = Cache.FindNodeOrInsertPos(ID, InsertPos);
  152. if (!L) {
  153. // The list does not exist in our cache. Create it.
  154. BumpPtrAllocator& A = getAllocator();
  155. L = (ListTy*) A.Allocate<ListTy>();
  156. new (L) ListTy(std::forward<ElemT>(Head), TailImpl);
  157. // Insert the new list into the cache.
  158. Cache.InsertNode(L, InsertPos);
  159. }
  160. return L;
  161. }
  162. template <typename ElemT>
  163. [[nodiscard]] ImmutableList<T> add(ElemT &&Data, ImmutableList<T> L) {
  164. return concat(std::forward<ElemT>(Data), L);
  165. }
  166. template <typename... CtorArgs>
  167. [[nodiscard]] ImmutableList<T> emplace(ImmutableList<T> Tail,
  168. CtorArgs &&...Args) {
  169. return concat(T(std::forward<CtorArgs>(Args)...), Tail);
  170. }
  171. ImmutableList<T> getEmptyList() const {
  172. return ImmutableList<T>(nullptr);
  173. }
  174. template <typename ElemT>
  175. ImmutableList<T> create(ElemT &&Data) {
  176. return concat(std::forward<ElemT>(Data), getEmptyList());
  177. }
  178. };
  179. //===----------------------------------------------------------------------===//
  180. // Partially-specialized Traits.
  181. //===----------------------------------------------------------------------===//
  182. template <typename T> struct DenseMapInfo<ImmutableList<T>, void> {
  183. static inline ImmutableList<T> getEmptyKey() {
  184. return reinterpret_cast<ImmutableListImpl<T>*>(-1);
  185. }
  186. static inline ImmutableList<T> getTombstoneKey() {
  187. return reinterpret_cast<ImmutableListImpl<T>*>(-2);
  188. }
  189. static unsigned getHashValue(ImmutableList<T> X) {
  190. uintptr_t PtrVal = reinterpret_cast<uintptr_t>(X.getInternalPointer());
  191. return (unsigned((uintptr_t)PtrVal) >> 4) ^
  192. (unsigned((uintptr_t)PtrVal) >> 9);
  193. }
  194. static bool isEqual(ImmutableList<T> X1, ImmutableList<T> X2) {
  195. return X1 == X2;
  196. }
  197. };
  198. } // end namespace llvm
  199. #endif // LLVM_ADT_IMMUTABLELIST_H
  200. #ifdef __GNUC__
  201. #pragma GCC diagnostic pop
  202. #endif