21#ifndef _libint2_src_bin_libint_singlstack_h_
22#define _libint2_src_bin_libint_singlstack_h_
34class RecurrenceRelation;
44template <
class T,
class KeyType>
47 typedef KeyType key_type;
48 typedef std::shared_ptr<T> data_type;
51 typedef std::pair<InstanceID, std::shared_ptr<T> > value_type;
52 typedef std::map<key_type, value_type> map_type;
67 : map_(), callback_(callback), next_instance_(0) {
70 PurgeableStacks::Instance()->register_stack(
this);
81 const value_type&
find(
const std::shared_ptr<T>& obj) {
82 key_type key = ((obj.get())->*callback_)();
84 typedef typename map_type::iterator miter;
85 miter pos = map_.find(key);
86 if (pos != map_.end()) {
87#if DEBUG || LOCAL_DEBUG
88 std::cout <<
"SingletonStack::find -- " << obj->label()
89 <<
" already found" << std::endl;
93 value_type result(next_instance_++, obj);
95#if DEBUG || LOCAL_DEBUG
96 std::cout <<
"SingletonStack::find -- " << obj->label()
97 <<
" is new (instid_ = " << next_instance_ - 1 <<
")"
108 const value_type&
find(
const key_type& key) {
109 static value_type null_value(
110 make_pair(
InstanceID(0), std::shared_ptr<T>()));
111 typedef typename map_type::iterator miter;
112 miter pos = map_.find(key);
113 if (pos != map_.end()) {
114 return (*pos).second;
125 static value_type null_value(
126 make_pair(
InstanceID(0), std::shared_ptr<T>()));
127 for (
auto& i : map_) {
128 if (i.second.first == hashed_key)
return i.second;
135 void remove(
const std::shared_ptr<T>& obj) {
136 key_type key = ((obj.get())->*callback_)();
138 typedef typename map_type::iterator miter;
139 miter pos = map_.find(key);
140 if (pos != map_.end()) {
142#if DEBUG || LOCAL_DEBUG
143 std::cout <<
"Removed from stack " << obj->label() << std::endl;
154 void purge()
override {
155 for (
iter_type i = map_.begin(); i != map_.end();) {
156 const T* v = i->second.second.get();
PurgeableStack is an AbstractPurgeableStack that contains objects of type T.
Definition purgeable.h:76
SingletonStack<T,KeyType> helps to implement Singleton-like objects of type T.
Definition singl_stack.h:45
KeyTraits< key_type >::ReturnType key_return_type
hashing function returns keys as key_return_type
Definition singl_stack.h:58
const value_type & find(const key_type &key)
Returns the pointer to the unique instance of object corresponding to key.
Definition singl_stack.h:108
citer_type end() const
Returns iterator to the end of the stack.
Definition singl_stack.h:151
const value_type & find(const std::shared_ptr< T > &obj)
Returns the pointer to the unique instance of object obj.
Definition singl_stack.h:81
citer_type begin() const
Returns iterator to the beginning of the stack.
Definition singl_stack.h:149
void remove(const std::shared_ptr< T > &obj)
Searches for obj on the stack and, if found, removes the unique instance.
Definition singl_stack.h:135
key_return_type(T::* HashingFunction)() const
Specifies the type of callback which computes hashes.
Definition singl_stack.h:60
KeyTypes::InstanceID InstanceID
Specifies type for the instance index variables.
Definition singl_stack.h:50
map_type::iterator iter_type
use iter_type objects to iterate over the stack
Definition singl_stack.h:54
PurgeableStack< T >::PurgingPolicy PurgingPolicy
PurgingPolicy determines whether and which objects on this stack are obsolete and can be removed.
Definition singl_stack.h:63
SingletonStack(HashingFunction callback)
callback to compute hash values is the only parameter
Definition singl_stack.h:66
const value_type & find_hashed(const InstanceID &hashed_key) const
Returns the pointer to the unique instance of object corresponding to the hashed_key.
Definition singl_stack.h:124
map_type::const_iterator citer_type
const version of iter_type
Definition singl_stack.h:56
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
Determines whether an object should be purged from a stack.
Definition purgeable.h:35
static bool purge(const T *ref)
returns true if obj should be purged
Definition purgeable.h:49
static bool purgeable()
returns true if objects of this type can be purged
Definition purgeable.h:37
mpz_class InstanceID
some classes need to have distinct instances to have unique InstanceID's, e.g.
Definition key.h:86