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