// Auxiliary class: almost the same as CDO (see cdo.h)
template <class Key, class Data, class HashFcn = std::hash<Key> >
-class CDOhash_map : public ContextObj {
+class CDOhash_map : public ContextObj
+{
friend class CDHashMap<Key, Data, HashFcn>;
public:
ContextObj* save(ContextMemoryManager* pCMM) override
{
- return new(pCMM) CDOhash_map(*this);
+ return new (pCMM) CDOhash_map(*this);
}
void restore(ContextObj* data) override
{
CDOhash_map* p = static_cast<CDOhash_map*>(data);
- if(d_map != NULL) {
- if(p->d_map == NULL) {
+ if (d_map != NULL)
+ {
+ if (p->d_map == NULL)
+ {
Assert(d_map->d_map.find(getKey()) != d_map->d_map.end()
&& (*d_map->d_map.find(getKey())).second == this);
// no longer in map (popped beyond first level in which it was)
// put it on a "trash heap" instead, for later deletion.
//
// FIXME multithreading
- if(d_map->d_first == this) {
- Debug("gc") << "remove first-elem " << this << " from map " << d_map << " with next-elem " << d_next << std::endl;
- if(d_next == this) {
+ if (d_map->d_first == this)
+ {
+ Debug("gc") << "remove first-elem " << this << " from map " << d_map
+ << " with next-elem " << d_next << std::endl;
+ if (d_next == this)
+ {
Assert(d_prev == this);
d_map->d_first = NULL;
- } else {
+ }
+ else
+ {
d_map->d_first = d_next;
}
- } else {
- Debug("gc") << "remove nonfirst-elem " << this << " from map " << d_map << std::endl;
+ }
+ else
+ {
+ Debug("gc") << "remove nonfirst-elem " << this << " from map "
+ << d_map << std::endl;
}
d_next->d_prev = d_prev;
d_prev->d_next = d_next;
Debug("gc") << "CDHashMap<> trash push_back " << this << std::endl;
// this->deleteSelf();
enqueueToGarbageCollect();
- } else {
+ }
+ else
+ {
mutable_data() = p->get();
}
}
bool atLevelZero = false)
: ContextObj(false, context), d_value(key, data), d_map(NULL)
{
- if(atLevelZero) {
+ if (atLevelZero)
+ {
// "Initializing" map insertion: this entry will never be
// removed from the map, it's inserted at level 0 as an
// "initializing" element. See
// CDHashMap<>::insertAtContextLevelZero().
mutable_data() = data;
- } else {
+ }
+ else
+ {
// Normal map insertion: first makeCurrent(), then set the data
// and then, later, the map. Order is important; we can't
// initialize d_map in the constructor init list above, because
d_map = map;
CDOhash_map*& first = d_map->d_first;
- if(first == NULL) {
+ if (first == NULL)
+ {
first = d_next = d_prev = this;
- Debug("gc") << "add first-elem " << this << " to map " << d_map << std::endl;
- } else {
- Debug("gc") << "add nonfirst-elem " << this << " to map " << d_map << " with first-elem " << first << "[" << first->d_prev << " " << first->d_next << std::endl;
+ Debug("gc") << "add first-elem " << this << " to map " << d_map
+ << std::endl;
+ }
+ else
+ {
+ Debug("gc") << "add nonfirst-elem " << this << " to map " << d_map
+ << " with first-elem " << first << "[" << first->d_prev << " "
+ << first->d_next << std::endl;
d_prev = first->d_prev;
d_next = first;
d_prev->d_next = this;
}
}
- ~CDOhash_map() {
- destroy();
- }
+ ~CDOhash_map() { destroy(); }
- void set(const Data& data) {
+ void set(const Data& data)
+ {
makeCurrent();
mutable_data() = data;
}
const value_type& getValue() const { return d_value; }
- operator Data() {
- return get();
- }
+ operator Data() { return get(); }
- const Data& operator=(const Data& data) {
+ const Data& operator=(const Data& data)
+ {
set(data);
return data;
}
- CDOhash_map* next() const {
- if(d_next == d_map->d_first) {
+ CDOhash_map* next() const
+ {
+ if (d_next == d_map->d_first)
+ {
return NULL;
- } else {
+ }
+ else
+ {
return d_next;
}
}
-};/* class CDOhash_map<> */
-
+}; /* class CDOhash_map<> */
/**
* Generic templated class for a map which must be saved and restored
* defined for the data class, and operator== for the key class.
*/
template <class Key, class Data, class HashFcn>
-class CDHashMap : public ContextObj {
-
+class CDHashMap : public ContextObj
+{
typedef CDOhash_map<Key, Data, HashFcn> Element;
typedef std::unordered_map<Key, Element*, HashFcn> table_type;
CDHashMap(const CDHashMap&) = delete;
CDHashMap& operator=(const CDHashMap&) = delete;
-public:
+ public:
CDHashMap(Context* context)
- : ContextObj(context), d_map(), d_first(NULL), d_context(context) {}
+ : ContextObj(context), d_map(), d_first(NULL), d_context(context)
+ {
+ }
- ~CDHashMap() {
+ ~CDHashMap()
+ {
Debug("gc") << "cdhashmap" << this << " disappearing, destroying..."
<< std::endl;
destroy();
clear();
}
- void clear() {
+ void clear()
+ {
Debug("gc") << "clearing cdhashmap" << this << ", emptying trash"
<< std::endl;
Debug("gc") << "done emptying trash for " << this << std::endl;
- for (auto& key_element_pair : d_map) {
+ for (auto& key_element_pair : d_map)
+ {
// mark it as being a destruction (short-circuit restore())
Element* element = key_element_pair.second;
element->d_map = nullptr;
// The usual operators of map
- size_t size() const {
- return d_map.size();
- }
+ size_t size() const { return d_map.size(); }
- bool empty() const {
- return d_map.empty();
- }
+ bool empty() const { return d_map.empty(); }
- size_t count(const Key& k) const {
- return d_map.count(k);
- }
+ size_t count(const Key& k) const { return d_map.count(k); }
// If a key is not present, a new object is created and inserted
- Element& operator[](const Key& k) {
- typename table_type::iterator i = d_map.find(k);
-
- Element* obj;
- if(i == d_map.end()) {// create new object
- obj = new(true) Element(d_context, this, k, Data());
- d_map.insert(std::make_pair(k, obj));
- } else {
- obj = (*i).second;
+ Element& operator[](const Key& k)
+ {
+ const auto res = d_map.insert({k, nullptr});
+ if (res.second)
+ { // create new object
+ res.first->second = new (true) Element(d_context, this, k, Data());
}
- return *obj;
+ return *(res.first->second);
}
- bool insert(const Key& k, const Data& d) {
- typename table_type::iterator i = d_map.find(k);
-
- if(i == d_map.end()) {// create new object
- Element* obj = new(true) Element(d_context, this, k, d);
- d_map.insert(std::make_pair(k, obj));
- return true;
- } else {
- (*i).second->set(d);
- return false;
+ bool insert(const Key& k, const Data& d)
+ {
+ const auto res = d_map.insert({k, nullptr});
+ if (res.second)
+ { // create new object
+ res.first->second = new (true) Element(d_context, this, k, d);
}
+ else
+ {
+ res.first->second->set(d);
+ }
+ return res.second;
}
/**
* It is an error (checked via AlwaysAssert()) to
* insertAtContextLevelZero() a key that already is in the map.
*/
- void insertAtContextLevelZero(const Key& k, const Data& d) {
+ void insertAtContextLevelZero(const Key& k, const Data& d)
+ {
AlwaysAssert(d_map.find(k) == d_map.end());
- Element* obj = new(true) Element(d_context, this, k, d,
- true /* atLevelZero */);
+ Element* obj =
+ new (true) Element(d_context, this, k, d, true /* atLevelZero */);
d_map.insert(std::make_pair(k, obj));
}
using value_type = typename CDOhash_map<Key, Data, HashFcn>::value_type;
- class iterator {
+ class iterator
+ {
const Element* d_it;
public:
}
// Postfix increment is not yet supported.
- };/* class CDHashMap<>::iterator */
+ }; /* class CDHashMap<>::iterator */
typedef iterator const_iterator;
- iterator begin() const {
- return iterator(d_first);
- }
+ iterator begin() const { return iterator(d_first); }
- iterator end() const {
- return iterator(NULL);
- }
+ iterator end() const { return iterator(NULL); }
- iterator find(const Key& k) const {
+ iterator find(const Key& k) const
+ {
typename table_type::const_iterator i = d_map.find(k);
- if(i == d_map.end()) {
+ if (i == d_map.end())
+ {
return end();
- } else {
+ }
+ else
+ {
return iterator((*i).second);
}
}
-
-};/* class CDHashMap<> */
+}; /* class CDHashMap<> */
} // namespace context
} // namespace cvc5