LIBINT 2.9.0
singl_stack.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint compiler.
5 *
6 * Libint compiler is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Libint compiler is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Libint compiler. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#ifndef _libint2_src_bin_libint_singlstack_h_
22#define _libint2_src_bin_libint_singlstack_h_
23
24#include <hashable.h>
25#include <key.h>
26#include <purgeable.h>
27#include <smart_ptr.h>
28
29#include <iostream>
30#include <map>
31
32namespace libint2 {
33
34class RecurrenceRelation;
35
44template <class T, class KeyType>
45class SingletonStack : public PurgeableStack<T> {
46 public:
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;
54 typedef typename map_type::iterator iter_type;
56 typedef typename map_type::const_iterator citer_type;
58 typedef typename KeyTraits<key_type>::ReturnType key_return_type;
60 typedef key_return_type (T::*HashingFunction)() const;
64
67 : map_(), callback_(callback), next_instance_(0) {
68 if (PurgingPolicy::purgeable()) { // if this stack contains objects that
69 // can be purged, add to the registry
70 PurgeableStacks::Instance()->register_stack(this);
71 }
72 }
73
74 virtual ~SingletonStack() {}
75
81 const value_type& find(const std::shared_ptr<T>& obj) {
82 key_type key = ((obj.get())->*callback_)();
83
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;
90#endif
91 return (*pos).second;
92 } else {
93 value_type result(next_instance_++, obj);
94 map_[key] = result;
95#if DEBUG || LOCAL_DEBUG
96 std::cout << "SingletonStack::find -- " << obj->label()
97 << " is new (instid_ = " << next_instance_ - 1 << ")"
98 << std::endl;
99#endif
100 return map_[key];
101 }
102 }
103
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;
115 } else {
116 return null_value;
117 }
118 }
119
124 const value_type& find_hashed(const InstanceID& hashed_key) const {
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;
129 }
130 return null_value;
131 }
132
135 void remove(const std::shared_ptr<T>& obj) {
136 key_type key = ((obj.get())->*callback_)();
137
138 typedef typename map_type::iterator miter;
139 miter pos = map_.find(key);
140 if (pos != map_.end()) {
141 map_.erase(pos);
142#if DEBUG || LOCAL_DEBUG
143 std::cout << "Removed from stack " << obj->label() << std::endl;
144#endif
145 }
146 }
147
149 citer_type begin() const { return map_.begin(); }
151 citer_type end() const { return map_.end(); }
152
153 // Implementation of PurgeableStack::purge()
154 void purge() override {
155 for (iter_type i = map_.begin(); i != map_.end();) {
156 const T* v = i->second.second.get();
158 // map::erase invalidates the iterator, increment it beforehand
159 map_.erase(i++);
160 else
161 ++i;
162 }
163 }
164
165 private:
166 map_type map_;
167 HashingFunction callback_;
168 InstanceID next_instance_;
169};
170
171}; // namespace libint2
172
173#endif
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