2007-10-09 21:38:09 +00:00
|
|
|
//===--- ImmutableMap.h - Immutable (functional) map interface --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-10-09 21:38:09 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the ImmutableMap class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_IMMAP_H
|
|
|
|
#define LLVM_ADT_IMMAP_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/ImmutableSet.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2009-02-20 22:20:18 +00:00
|
|
|
/// ImutKeyValueInfo -Traits class used by ImmutableMap. While both the first
|
|
|
|
/// and second elements in a pair are used to generate profile information,
|
2007-10-09 21:38:09 +00:00
|
|
|
/// only the first element (the key) is used by isEqual and isLess.
|
|
|
|
template <typename T, typename S>
|
|
|
|
struct ImutKeyValueInfo {
|
|
|
|
typedef const std::pair<T,S> value_type;
|
|
|
|
typedef const value_type& value_type_ref;
|
|
|
|
typedef const T key_type;
|
|
|
|
typedef const T& key_type_ref;
|
|
|
|
typedef const S data_type;
|
|
|
|
typedef const S& data_type_ref;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline key_type_ref KeyOfValue(value_type_ref V) {
|
|
|
|
return V.first;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-17 17:36:49 +00:00
|
|
|
static inline data_type_ref DataOfValue(value_type_ref V) {
|
|
|
|
return V.second;
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-17 17:36:49 +00:00
|
|
|
static inline bool isEqual(key_type_ref L, key_type_ref R) {
|
|
|
|
return ImutContainerInfo<T>::isEqual(L,R);
|
2009-01-09 19:25:42 +00:00
|
|
|
}
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline bool isLess(key_type_ref L, key_type_ref R) {
|
|
|
|
return ImutContainerInfo<T>::isLess(L,R);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-17 17:36:49 +00:00
|
|
|
static inline bool isDataEqual(data_type_ref L, data_type_ref R) {
|
|
|
|
return ImutContainerInfo<S>::isEqual(L,R);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
static inline void Profile(FoldingSetNodeID& ID, value_type_ref V) {
|
|
|
|
ImutContainerInfo<T>::Profile(ID, V.first);
|
|
|
|
ImutContainerInfo<S>::Profile(ID, V.second);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
};
|
2007-10-09 21:38:09 +00:00
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
template <typename KeyT, typename ValT,
|
2007-10-09 21:38:09 +00:00
|
|
|
typename ValInfo = ImutKeyValueInfo<KeyT,ValT> >
|
|
|
|
class ImmutableMap {
|
2008-01-08 21:05:59 +00:00
|
|
|
public:
|
2007-10-09 21:38:09 +00:00
|
|
|
typedef typename ValInfo::value_type value_type;
|
|
|
|
typedef typename ValInfo::value_type_ref value_type_ref;
|
|
|
|
typedef typename ValInfo::key_type key_type;
|
|
|
|
typedef typename ValInfo::key_type_ref key_type_ref;
|
|
|
|
typedef typename ValInfo::data_type data_type;
|
|
|
|
typedef typename ValInfo::data_type_ref data_type_ref;
|
2008-01-08 21:05:59 +00:00
|
|
|
typedef ImutAVLTree<ValInfo> TreeTy;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-08 21:05:59 +00:00
|
|
|
private:
|
2007-10-09 21:38:09 +00:00
|
|
|
TreeTy* Root;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
public:
|
2008-01-08 21:05:59 +00:00
|
|
|
/// Constructs a map from a pointer to a tree root. In general one
|
|
|
|
/// should use a Factory object to create maps instead of directly
|
|
|
|
/// invoking the constructor, but there are cases where make this
|
|
|
|
/// constructor public is useful.
|
2008-07-10 22:57:10 +00:00
|
|
|
explicit ImmutableMap(const TreeTy* R) : Root(const_cast<TreeTy*>(R)) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
class Factory {
|
|
|
|
typename TreeTy::Factory F;
|
2009-10-13 19:08:10 +00:00
|
|
|
const bool Canonicalize;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
public:
|
2009-10-13 19:08:10 +00:00
|
|
|
Factory(bool canonicalize = true)
|
|
|
|
: Canonicalize(canonicalize) {}
|
|
|
|
|
|
|
|
Factory(BumpPtrAllocator& Alloc, bool canonicalize = true)
|
|
|
|
: F(Alloc), Canonicalize(canonicalize) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
ImmutableMap GetEmptyMap() { return ImmutableMap(F.GetEmptyTree()); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
ImmutableMap Add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
|
2009-09-03 22:07:30 +00:00
|
|
|
TreeTy *T = F.Add(Old.Root, std::make_pair<key_type,data_type>(K,D));
|
2009-10-13 19:08:10 +00:00
|
|
|
return ImmutableMap(Canonicalize ? F.GetCanonicalTree(T): T);
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
ImmutableMap Remove(ImmutableMap Old, key_type_ref K) {
|
2009-09-03 22:07:30 +00:00
|
|
|
TreeTy *T = F.Remove(Old.Root,K);
|
2009-10-13 19:08:10 +00:00
|
|
|
return ImmutableMap(Canonicalize ? F.GetCanonicalTree(T): T);
|
2009-01-09 19:25:42 +00:00
|
|
|
}
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
private:
|
2009-11-05 23:01:30 +00:00
|
|
|
Factory(const Factory& RHS); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const Factory& RHS); // DO NOT IMPLEMENT
|
2007-10-09 21:38:09 +00:00
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
friend class Factory;
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
bool contains(key_type_ref K) const {
|
|
|
|
return Root ? Root->contains(K) : false;
|
|
|
|
}
|
2008-01-15 23:53:53 +00:00
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
bool operator==(ImmutableMap RHS) const {
|
|
|
|
return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
bool operator!=(ImmutableMap RHS) const {
|
|
|
|
return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-08 21:05:59 +00:00
|
|
|
TreeTy* getRoot() const { return Root; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
bool isEmpty() const { return !Root; }
|
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
//===--------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// Foreach - A limited form of map iteration.
|
|
|
|
//===--------------------------------------------------===//
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename Callback>
|
|
|
|
struct CBWrapper {
|
|
|
|
Callback C;
|
2009-01-09 19:25:42 +00:00
|
|
|
void operator()(value_type_ref V) { C(V.first,V.second); }
|
|
|
|
};
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
template <typename Callback>
|
|
|
|
struct CBWrapperRef {
|
|
|
|
Callback &C;
|
|
|
|
CBWrapperRef(Callback& c) : C(c) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
void operator()(value_type_ref V) { C(V.first,V.second); }
|
2007-10-09 21:38:09 +00:00
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
public:
|
2007-10-09 21:38:09 +00:00
|
|
|
template <typename Callback>
|
2009-01-09 19:25:42 +00:00
|
|
|
void foreach(Callback& C) {
|
|
|
|
if (Root) {
|
2007-10-09 21:38:09 +00:00
|
|
|
CBWrapperRef<Callback> CB(C);
|
|
|
|
Root->foreach(CB);
|
|
|
|
}
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
template <typename Callback>
|
2009-01-09 19:25:42 +00:00
|
|
|
void foreach() {
|
2007-10-09 21:38:09 +00:00
|
|
|
if (Root) {
|
|
|
|
CBWrapper<Callback> CB;
|
|
|
|
Root->foreach(CB);
|
|
|
|
}
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------===//
|
2007-10-09 21:38:09 +00:00
|
|
|
// For testing.
|
2009-01-09 19:25:42 +00:00
|
|
|
//===--------------------------------------------------===//
|
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
void verify() const { if (Root) Root->verify(); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------===//
|
2008-01-02 21:31:48 +00:00
|
|
|
// Iterators.
|
2009-01-09 19:25:42 +00:00
|
|
|
//===--------------------------------------------------===//
|
|
|
|
|
2008-01-02 21:31:48 +00:00
|
|
|
class iterator {
|
|
|
|
typename TreeTy::iterator itr;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-02 21:31:48 +00:00
|
|
|
iterator() {}
|
|
|
|
iterator(TreeTy* t) : itr(t) {}
|
2008-01-15 23:53:53 +00:00
|
|
|
friend class ImmutableMap;
|
2008-01-02 21:31:48 +00:00
|
|
|
|
|
|
|
public:
|
2008-05-03 01:05:46 +00:00
|
|
|
value_type_ref operator*() const { return itr->getValue(); }
|
|
|
|
value_type* operator->() const { return &itr->getValue(); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-05-03 01:05:46 +00:00
|
|
|
key_type_ref getKey() const { return itr->getValue().first; }
|
|
|
|
data_type_ref getData() const { return itr->getValue().second; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
|
2008-05-03 01:05:46 +00:00
|
|
|
iterator& operator++() { ++itr; return *this; }
|
|
|
|
iterator operator++(int) { iterator tmp(*this); ++itr; return tmp; }
|
|
|
|
iterator& operator--() { --itr; return *this; }
|
|
|
|
iterator operator--(int) { iterator tmp(*this); --itr; return tmp; }
|
|
|
|
bool operator==(const iterator& RHS) const { return RHS.itr == itr; }
|
2009-01-09 19:25:42 +00:00
|
|
|
bool operator!=(const iterator& RHS) const { return RHS.itr != itr; }
|
2008-01-02 21:31:48 +00:00
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-02 21:31:48 +00:00
|
|
|
iterator begin() const { return iterator(Root); }
|
2009-01-09 19:25:42 +00:00
|
|
|
iterator end() const { return iterator(); }
|
|
|
|
|
2008-07-07 16:20:55 +00:00
|
|
|
data_type* lookup(key_type_ref K) const {
|
2008-01-15 23:53:53 +00:00
|
|
|
if (Root) {
|
|
|
|
TreeTy* T = Root->find(K);
|
2008-07-07 16:20:55 +00:00
|
|
|
if (T) return &T->getValue().second;
|
2008-01-15 23:53:53 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-07-07 16:20:55 +00:00
|
|
|
return 0;
|
2008-01-15 23:53:53 +00:00
|
|
|
}
|
2009-02-23 17:27:18 +00:00
|
|
|
|
|
|
|
/// getMaxElement - Returns the <key,value> pair in the ImmutableMap for
|
|
|
|
/// which key is the highest in the ordering of keys in the map. This
|
|
|
|
/// method returns NULL if the map is empty.
|
|
|
|
value_type* getMaxElement() const {
|
2009-02-23 17:28:16 +00:00
|
|
|
return Root ? &(Root->getMaxElement()->getValue()) : 0;
|
2009-02-23 17:27:18 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------===//
|
2008-01-02 21:31:48 +00:00
|
|
|
// Utility methods.
|
2009-01-09 19:25:42 +00:00
|
|
|
//===--------------------------------------------------===//
|
|
|
|
|
2008-01-02 21:31:48 +00:00
|
|
|
inline unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
|
|
|
|
|
2008-02-04 21:17:02 +00:00
|
|
|
static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
|
2008-02-05 17:30:43 +00:00
|
|
|
ID.AddPointer(M.Root);
|
2008-01-02 21:31:48 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-01-02 21:31:48 +00:00
|
|
|
inline void Profile(FoldingSetNodeID& ID) const {
|
2008-01-02 22:18:33 +00:00
|
|
|
return Profile(ID,*this);
|
2009-01-09 19:25:42 +00:00
|
|
|
}
|
2007-10-09 21:38:09 +00:00
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-09 21:38:09 +00:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|