21 #ifndef _libint2_src_bin_libint_singlstack_h_ 22 #define _libint2_src_bin_libint_singlstack_h_ 26 #include <smart_ptr.h> 29 #include <purgeable.h> 33 class RecurrenceRelation;
42 template <
class T,
class KeyType>
46 typedef KeyType key_type;
47 typedef SafePtr<T> data_type;
50 typedef std::pair<InstanceID,SafePtr<T> > value_type;
51 typedef std::map<key_type,value_type> map_type;
66 map_(), callback_(callback), next_instance_(0)
68 if (PurgingPolicy::purgeable()) {
69 PurgeableStacks::Instance()->register_stack(
this);
80 const value_type&
find(
const SafePtr<T>& obj) {
81 key_type key = ((obj.get())->*callback_)();
83 typedef typename map_type::iterator miter;
84 miter pos = map_.find(key);
85 if (pos != map_.end()) {
86 #if DEBUG || LOCAL_DEBUG 87 std::cout <<
"SingletonStack::find -- " << obj->label() <<
" already found" << std::endl;
92 value_type result(next_instance_++,obj);
94 #if DEBUG || LOCAL_DEBUG 95 std::cout <<
"SingletonStack::find -- " << obj->label() <<
" is new (instid_ = " << next_instance_-1 <<
")" << std::endl;
105 const value_type&
find(
const key_type& key) {
106 static value_type null_value(make_pair(
InstanceID(0),SafePtr<T>()));
107 typedef typename map_type::iterator miter;
108 miter pos = map_.find(key);
109 if (pos != map_.end()) {
110 return (*pos).second;
122 static value_type null_value(make_pair(
InstanceID(0),SafePtr<T>()));
124 if (i.second.first == hashed_key)
133 key_type key = ((obj.get())->*callback_)();
135 typedef typename map_type::iterator miter;
136 miter pos = map_.find(key);
137 if (pos != map_.end()) {
139 #if DEBUG || LOCAL_DEBUG 140 std::cout <<
"Removed from stack " << obj->label() << std::endl;
151 virtual void purge() {
152 for(
iter_type i = map_.begin(); i!=map_.end();) {
153 const T* v = i->second.second.get();
154 if (PurgingPolicy::purge(v))
mpz_class InstanceID
some classes need to have distinct instances to have unique InstanceID's, e.g. generalized Singletons
Definition: key.h:81
key_return_type(T::* HashingFunction)() const
Specifies the type of callback which computes hashes.
Definition: singl_stack.h:59
SingletonStack<T,KeyType> helps to implement Singleton-like objects of type T.
Definition: singl_stack.h:43
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
citer_type end() const
Returns iterator to the end of the stack.
Definition: singl_stack.h:148
citer_type begin() const
Returns iterator to the beginning of the stack.
Definition: singl_stack.h:146
KeyTypes::InstanceID InstanceID
Specifies type for the instance index variables.
Definition: singl_stack.h:49
map_type::iterator iter_type
use iter_type objects to iterate over the stack
Definition: singl_stack.h:53
PurgeableStack< T >::PurgingPolicy PurgingPolicy
PurgingPolicy determines whether and which objects on this stack are obsolete and can be removed.
Definition: singl_stack.h:61
const value_type & find(const SafePtr< T > &obj)
Returns the pointer to the unique instance of object obj.
Definition: singl_stack.h:80
map_type::const_iterator citer_type
const version of iter_type
Definition: singl_stack.h:55
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:121
PurgeableStack is an AbstractPurgeableStack that contains objects of type T.
Definition: purgeable.h:79
const value_type & find(const key_type &key)
Returns the pointer to the unique instance of object corresponding to key.
Definition: singl_stack.h:105
SingletonStack(HashingFunction callback)
callback to compute hash values is the only parameter
Definition: singl_stack.h:65
void remove(const SafePtr< T > &obj)
Searches for obj on the stack and, if found, removes the unique instance.
Definition: singl_stack.h:132
KeyTraits< key_type >::ReturnType key_return_type
hashing function returns keys as key_return_type
Definition: singl_stack.h:57