Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
13 KiB

  1. //===--- ImmutableMap.h - Immutable (functional) map 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 ImmutableMap class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_IMMUTABLEMAP_H
  14. #define LLVM_ADT_IMMUTABLEMAP_H
  15. #include "llvm/ADT/ImmutableSet.h"
  16. namespace llvm {
  17. /// ImutKeyValueInfo -Traits class used by ImmutableMap. While both the first
  18. /// and second elements in a pair are used to generate profile information,
  19. /// only the first element (the key) is used by isEqual and isLess.
  20. template <typename T, typename S>
  21. struct ImutKeyValueInfo {
  22. typedef const std::pair<T,S> value_type;
  23. typedef const value_type& value_type_ref;
  24. typedef const T key_type;
  25. typedef const T& key_type_ref;
  26. typedef const S data_type;
  27. typedef const S& data_type_ref;
  28. static inline key_type_ref KeyOfValue(value_type_ref V) {
  29. return V.first;
  30. }
  31. static inline data_type_ref DataOfValue(value_type_ref V) {
  32. return V.second;
  33. }
  34. static inline bool isEqual(key_type_ref L, key_type_ref R) {
  35. return ImutContainerInfo<T>::isEqual(L,R);
  36. }
  37. static inline bool isLess(key_type_ref L, key_type_ref R) {
  38. return ImutContainerInfo<T>::isLess(L,R);
  39. }
  40. static inline bool isDataEqual(data_type_ref L, data_type_ref R) {
  41. return ImutContainerInfo<S>::isEqual(L,R);
  42. }
  43. static inline void Profile(FoldingSetNodeID& ID, value_type_ref V) {
  44. ImutContainerInfo<T>::Profile(ID, V.first);
  45. ImutContainerInfo<S>::Profile(ID, V.second);
  46. }
  47. };
  48. template <typename KeyT, typename ValT,
  49. typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
  50. class ImmutableMap {
  51. public:
  52. typedef typename ValInfo::value_type value_type;
  53. typedef typename ValInfo::value_type_ref value_type_ref;
  54. typedef typename ValInfo::key_type key_type;
  55. typedef typename ValInfo::key_type_ref key_type_ref;
  56. typedef typename ValInfo::data_type data_type;
  57. typedef typename ValInfo::data_type_ref data_type_ref;
  58. typedef ImutAVLTree<ValInfo> TreeTy;
  59. protected:
  60. TreeTy* Root;
  61. public:
  62. /// Constructs a map from a pointer to a tree root. In general one
  63. /// should use a Factory object to create maps instead of directly
  64. /// invoking the constructor, but there are cases where make this
  65. /// constructor public is useful.
  66. explicit ImmutableMap(const TreeTy* R) : Root(const_cast<TreeTy*>(R)) {
  67. if (Root) { Root->retain(); }
  68. }
  69. ImmutableMap(const ImmutableMap &X) : Root(X.Root) {
  70. if (Root) { Root->retain(); }
  71. }
  72. ImmutableMap &operator=(const ImmutableMap &X) {
  73. if (Root != X.Root) {
  74. if (X.Root) { X.Root->retain(); }
  75. if (Root) { Root->release(); }
  76. Root = X.Root;
  77. }
  78. return *this;
  79. }
  80. ~ImmutableMap() {
  81. if (Root) { Root->release(); }
  82. }
  83. class Factory {
  84. typename TreeTy::Factory F;
  85. const bool Canonicalize;
  86. public:
  87. Factory(bool canonicalize = true)
  88. : Canonicalize(canonicalize) {}
  89. Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
  90. : F(Alloc), Canonicalize(canonicalize) {}
  91. ImmutableMap getEmptyMap() { return ImmutableMap(F.getEmptyTree()); }
  92. ImmutableMap add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
  93. TreeTy *T = F.add(Old.Root, std::pair<key_type,data_type>(K,D));
  94. return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
  95. }
  96. ImmutableMap remove(ImmutableMap Old, key_type_ref K) {
  97. TreeTy *T = F.remove(Old.Root,K);
  98. return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
  99. }
  100. typename TreeTy::Factory *getTreeFactory() const {
  101. return const_cast<typename TreeTy::Factory *>(&F);
  102. }
  103. private:
  104. Factory(const Factory& RHS) LLVM_DELETED_FUNCTION;
  105. void operator=(const Factory& RHS) LLVM_DELETED_FUNCTION;
  106. };
  107. bool contains(key_type_ref K) const {
  108. return Root ? Root->contains(K) : false;
  109. }
  110. bool operator==(const ImmutableMap &RHS) const {
  111. return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
  112. }
  113. bool operator!=(const ImmutableMap &RHS) const {
  114. return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
  115. }
  116. TreeTy *getRoot() const {
  117. if (Root) { Root->retain(); }
  118. return Root;
  119. }
  120. TreeTy *getRootWithoutRetain() const {
  121. return Root;
  122. }
  123. void manualRetain() {
  124. if (Root) Root->retain();
  125. }
  126. void manualRelease() {
  127. if (Root) Root->release();
  128. }
  129. bool isEmpty() const { return !Root; }
  130. //===--------------------------------------------------===//
  131. // Foreach - A limited form of map iteration.
  132. //===--------------------------------------------------===//
  133. private:
  134. template <typename Callback>
  135. struct CBWrapper {
  136. Callback C;
  137. void operator()(value_type_ref V) { C(V.first,V.second); }
  138. };
  139. template <typename Callback>
  140. struct CBWrapperRef {
  141. Callback &C;
  142. CBWrapperRef(Callback& c) : C(c) {}
  143. void operator()(value_type_ref V) { C(V.first,V.second); }
  144. };
  145. public:
  146. template <typename Callback>
  147. void foreach(Callback& C) {
  148. if (Root) {
  149. CBWrapperRef<Callback> CB(C);
  150. Root->foreach(CB);
  151. }
  152. }
  153. template <typename Callback>
  154. void foreach() {
  155. if (Root) {
  156. CBWrapper<Callback> CB;
  157. Root->foreach(CB);
  158. }
  159. }
  160. //===--------------------------------------------------===//
  161. // For testing.
  162. //===--------------------------------------------------===//
  163. void verify() const { if (Root) Root->verify(); }
  164. //===--------------------------------------------------===//
  165. // Iterators.
  166. //===--------------------------------------------------===//
  167. class iterator {
  168. typename TreeTy::iterator itr;
  169. iterator() {}
  170. iterator(TreeTy* t) : itr(t) {}
  171. friend class ImmutableMap;
  172. public:
  173. typedef typename ImmutableMap<KeyT,ValT,ValInfo>::value_type value_type;
  174. typedef typename ImmutableMap<KeyT,ValT,ValInfo>::value_type_ref reference;
  175. typedef typename iterator::value_type *pointer;
  176. typedef std::bidirectional_iterator_tag iterator_category;
  177. typename iterator::reference operator*() const { return itr->getValue(); }
  178. typename iterator::pointer operator->() const { return &itr->getValue(); }
  179. key_type_ref getKey() const { return itr->getValue().first; }
  180. data_type_ref getData() const { return itr->getValue().second; }
  181. iterator& operator++() { ++itr; return *this; }
  182. iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
  183. iterator& operator--() { --itr; return *this; }
  184. iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
  185. bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
  186. bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
  187. };
  188. iterator begin() const { return iterator(Root); }
  189. iterator end() const { return iterator(); }
  190. data_type* lookup(key_type_ref K) const {
  191. if (Root) {
  192. TreeTy* T = Root->find(K);
  193. if (T) return &T->getValue().second;
  194. }
  195. return 0;
  196. }
  197. /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
  198. /// which key is the highest in the ordering of keys in the map. This
  199. /// method returns NULL if the map is empty.
  200. value_type* getMaxElement() const {
  201. return Root ? &(Root->getMaxElement()->getValue()) : 0;
  202. }
  203. //===--------------------------------------------------===//
  204. // Utility methods.
  205. //===--------------------------------------------------===//
  206. unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
  207. static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
  208. ID.AddPointer(M.Root);
  209. }
  210. inline void Profile(FoldingSetNodeID& ID) const {
  211. return Profile(ID,*this);
  212. }
  213. };
  214. // NOTE: This will possibly become the new implementation of ImmutableMap some day.
  215. template <typename KeyT, typename ValT,
  216. typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
  217. class ImmutableMapRef {
  218. public:
  219. typedef typename ValInfo::value_type value_type;
  220. typedef typename ValInfo::value_type_ref value_type_ref;
  221. typedef typename ValInfo::key_type key_type;
  222. typedef typename ValInfo::key_type_ref key_type_ref;
  223. typedef typename ValInfo::data_type data_type;
  224. typedef typename ValInfo::data_type_ref data_type_ref;
  225. typedef ImutAVLTree<ValInfo> TreeTy;
  226. typedef typename TreeTy::Factory FactoryTy;
  227. protected:
  228. TreeTy *Root;
  229. FactoryTy *Factory;
  230. public:
  231. /// Constructs a map from a pointer to a tree root. In general one
  232. /// should use a Factory object to create maps instead of directly
  233. /// invoking the constructor, but there are cases where make this
  234. /// constructor public is useful.
  235. explicit ImmutableMapRef(const TreeTy* R, FactoryTy *F)
  236. : Root(const_cast<TreeTy*>(R)),
  237. Factory(F) {
  238. if (Root) { Root->retain(); }
  239. }
  240. explicit ImmutableMapRef(const ImmutableMap<KeyT, ValT> &X,
  241. typename ImmutableMap<KeyT, ValT>::Factory &F)
  242. : Root(X.getRootWithoutRetain()),
  243. Factory(F.getTreeFactory()) {
  244. if (Root) { Root->retain(); }
  245. }
  246. ImmutableMapRef(const ImmutableMapRef &X)
  247. : Root(X.Root),
  248. Factory(X.Factory) {
  249. if (Root) { Root->retain(); }
  250. }
  251. ImmutableMapRef &operator=(const ImmutableMapRef &X) {
  252. if (Root != X.Root) {
  253. if (X.Root)
  254. X.Root->retain();
  255. if (Root)
  256. Root->release();
  257. Root = X.Root;
  258. Factory = X.Factory;
  259. }
  260. return *this;
  261. }
  262. ~ImmutableMapRef() {
  263. if (Root)
  264. Root->release();
  265. }
  266. static inline ImmutableMapRef getEmptyMap(FactoryTy *F) {
  267. return ImmutableMapRef(0, F);
  268. }
  269. void manualRetain() {
  270. if (Root) Root->retain();
  271. }
  272. void manualRelease() {
  273. if (Root) Root->release();
  274. }
  275. ImmutableMapRef add(key_type_ref K, data_type_ref D) const {
  276. TreeTy *NewT = Factory->add(Root, std::pair<key_type, data_type>(K, D));
  277. return ImmutableMapRef(NewT, Factory);
  278. }
  279. ImmutableMapRef remove(key_type_ref K) const {
  280. TreeTy *NewT = Factory->remove(Root, K);
  281. return ImmutableMapRef(NewT, Factory);
  282. }
  283. bool contains(key_type_ref K) const {
  284. return Root ? Root->contains(K) : false;
  285. }
  286. ImmutableMap<KeyT, ValT> asImmutableMap() const {
  287. return ImmutableMap<KeyT, ValT>(Factory->getCanonicalTree(Root));
  288. }
  289. bool operator==(const ImmutableMapRef &RHS) const {
  290. return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
  291. }
  292. bool operator!=(const ImmutableMapRef &RHS) const {
  293. return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
  294. }
  295. bool isEmpty() const { return !Root; }
  296. //===--------------------------------------------------===//
  297. // For testing.
  298. //===--------------------------------------------------===//
  299. void verify() const { if (Root) Root->verify(); }
  300. //===--------------------------------------------------===//
  301. // Iterators.
  302. //===--------------------------------------------------===//
  303. class iterator {
  304. typename TreeTy::iterator itr;
  305. iterator() {}
  306. iterator(TreeTy* t) : itr(t) {}
  307. friend class ImmutableMapRef;
  308. public:
  309. value_type_ref operator*() const { return itr->getValue(); }
  310. value_type* operator->() const { return &itr->getValue(); }
  311. key_type_ref getKey() const { return itr->getValue().first; }
  312. data_type_ref getData() const { return itr->getValue().second; }
  313. iterator& operator++() { ++itr; return *this; }
  314. iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
  315. iterator& operator--() { --itr; return *this; }
  316. iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
  317. bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
  318. bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
  319. };
  320. iterator begin() const { return iterator(Root); }
  321. iterator end() const { return iterator(); }
  322. data_type* lookup(key_type_ref K) const {
  323. if (Root) {
  324. TreeTy* T = Root->find(K);
  325. if (T) return &T->getValue().second;
  326. }
  327. return 0;
  328. }
  329. /// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
  330. /// which key is the highest in the ordering of keys in the map. This
  331. /// method returns NULL if the map is empty.
  332. value_type* getMaxElement() const {
  333. return Root ? &(Root->getMaxElement()->getValue()) : 0;
  334. }
  335. //===--------------------------------------------------===//
  336. // Utility methods.
  337. //===--------------------------------------------------===//
  338. unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
  339. static inline void Profile(FoldingSetNodeID& ID, const ImmutableMapRef &M) {
  340. ID.AddPointer(M.Root);
  341. }
  342. inline void Profile(FoldingSetNodeID& ID) const {
  343. return Profile(ID, *this);
  344. }
  345. };
  346. } // end namespace llvm
  347. #endif