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
|
|
|
|
2010-02-01 10:43:31 +00:00
|
|
|
protected:
|
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.
|
2010-11-30 20:26:45 +00:00
|
|
|
explicit ImmutableMap(const TreeTy* R) : Root(const_cast<TreeTy*>(R)) {
|
|
|
|
if (Root) { Root->retain(); }
|
|
|
|
}
|
|
|
|
ImmutableMap(const ImmutableMap &X) : Root(X.Root) {
|
|
|
|
if (Root) { Root->retain(); }
|
|
|
|
}
|
|
|
|
ImmutableMap &operator=(const ImmutableMap &X) {
|
|
|
|
if (Root != X.Root) {
|
|
|
|
if (X.Root) { X.Root->retain(); }
|
|
|
|
if (Root) { Root->release(); }
|
|
|
|
Root = X.Root;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
~ImmutableMap() {
|
|
|
|
if (Root) { Root->release(); }
|
|
|
|
}
|
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
|
|
|
|
2010-11-24 00:54:28 +00:00
|
|
|
ImmutableMap getEmptyMap() { return ImmutableMap(F.getEmptyTree()); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2010-11-24 00:54:28 +00:00
|
|
|
ImmutableMap add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
|
|
|
|
TreeTy *T = F.add(Old.Root, std::make_pair<key_type,data_type>(K,D));
|
|
|
|
return ImmutableMap(Canonicalize ? F.getCanonicalTree(T): T);
|
2007-10-09 21:38:09 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2010-11-24 00:54:28 +00:00
|
|
|
ImmutableMap remove(ImmutableMap Old, key_type_ref K) {
|
|
|
|
TreeTy *T = F.remove(Old.Root,K);
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2010-11-30 20:26:45 +00:00
|
|
|
bool operator==(const ImmutableMap &RHS) const {
|
2007-10-09 21:38:09 +00:00
|
|
|
return Root && RHS.Root ? Root->isEqual(*RHS.Root) : Root == RHS.Root;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2010-11-30 20:26:45 +00:00
|
|
|
bool operator!=(const ImmutableMap &RHS) const {
|
2007-10-09 21:38:09 +00:00
|
|
|
return Root && RHS.Root ? Root->isNotEqual(*RHS.Root) : Root != RHS.Root;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2010-11-30 20:26:45 +00:00
|
|
|
TreeTy* getRoot() const {
|
|
|
|
if (Root) { Root->retain(); }
|
|
|
|
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
|
|
|
//===--------------------------------------------------===//
|
|
|
|
|
2010-01-22 00:28:27 +00:00
|
|
|
unsigned getHeight() const { return Root ? Root->getHeight() : 0; }
|
2008-01-02 21:31:48 +00:00
|
|
|
|
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
|