1 //===--- ImmutableMap.h - Immutable (functional) map 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 ImmutableMap class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_IMMAP_H
15 #define LLVM_ADT_IMMAP_H
17 #include "llvm/ADT/ImmutableSet.h"
21 /// ImutKeyValueInfo -Traits class used by ImmutableMap. While both the first and
22 /// 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;
33 static inline key_type_ref KeyOfValue(value_type_ref V) {
37 static inline bool isEqual(key_type_ref L, key_type_ref R) {
38 return ImutContainerInfo<T>::isEqual(L,R);
41 static inline bool isLess(key_type_ref L, key_type_ref R) {
42 return ImutContainerInfo<T>::isLess(L,R);
45 static inline void Profile(FoldingSetNodeID& ID, value_type_ref V) {
46 ImutContainerInfo<T>::Profile(ID, V.first);
47 ImutContainerInfo<S>::Profile(ID, V.second);
52 template <typename KeyT, typename ValT,
53 typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
55 typedef typename ValInfo::value_type value_type;
56 typedef typename ValInfo::value_type_ref value_type_ref;
57 typedef typename ValInfo::key_type key_type;
58 typedef typename ValInfo::key_type_ref key_type_ref;
59 typedef typename ValInfo::data_type data_type;
60 typedef typename ValInfo::data_type_ref data_type_ref;
63 typedef ImutAVLTree<ValInfo> TreeTy;
66 ImmutableMap(TreeTy* R) : Root(R) {}
71 typename TreeTy::Factory F;
76 ImmutableMap GetEmptyMap() { return ImmutableMap(F.GetEmptyTree()); }
78 ImmutableMap Add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
79 return ImmutableMap(F.Add(Old.Root,std::make_pair<key_type,data_type>(K,D)));
82 ImmutableMap Remove(ImmutableMap Old, key_type_ref K) {
83 return ImmutableMap(F.Remove(Old.Root,K));
87 Factory(const Factory& RHS) {};
88 void operator=(const Factory& RHS) {};
93 bool contains(key_type_ref K) const {
94 return Root ? Root->contains(K) : false;
97 data_type* find(key_type_ref K) const {
99 TreeTy* T = Root->find(K);
100 if (T) return &T->getValue().second;
106 bool operator==(ImmutableMap RHS) const {
107 return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
110 bool operator!=(ImmutableMap RHS) const {
111 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
114 bool isEmpty() const { return !Root; }
116 //===--------------------------------------------------===//
117 // Foreach - A limited form of map iteration.
118 //===--------------------------------------------------===//
121 template <typename Callback>
124 void operator()(value_type_ref V) { C(V.first,V.second); }
127 template <typename Callback>
128 struct CBWrapperRef {
130 CBWrapperRef(Callback& c) : C(c) {}
132 void operator()(value_type_ref V) { C(V.first,V.second); }
136 template <typename Callback>
137 void foreach(Callback& C) {
139 CBWrapperRef<Callback> CB(C);
144 template <typename Callback>
147 CBWrapper<Callback> CB;
152 //===--------------------------------------------------===//
154 //===--------------------------------------------------===//
156 void verify() const { if (Root) Root->verify(); }
157 unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
161 } // end namespace llvm