suanPan
Storage.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2024 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
27#ifndef STORAGE_H
28#define STORAGE_H
29
30#include <Toolbox/container.h>
31
32class Amplitude;
33class Expression;
34class Constraint;
35class Converger;
36class Criterion;
37class Database;
38class Domain;
39class DomainBase;
40class Element;
41class Group;
42class Integrator;
43class Load;
44class Material;
45class Modifier;
46class Node;
47class Orientation;
48class Recorder;
49class Section;
50class Solver;
51
52template<typename T> const char* StorageType() { return "Unknown"; }
53
54template<> inline const char* StorageType<Amplitude>() { return "Amplitude"; }
55
56template<> inline const char* StorageType<Expression>() { return "Expression"; }
57
58template<> inline const char* StorageType<Constraint>() { return "Constraint"; }
59
60template<> inline const char* StorageType<Converger>() { return "Converger"; }
61
62template<> inline const char* StorageType<Criterion>() { return "Criterion"; }
63
64template<> inline const char* StorageType<Database>() { return "Database"; }
65
66template<> inline const char* StorageType<Domain>() { return "Domain"; }
67
68template<> inline const char* StorageType<DomainBase>() { return "Domain"; }
69
70template<> inline const char* StorageType<Element>() { return "Element"; }
71
72template<> inline const char* StorageType<Group>() { return "Group"; }
73
74template<> inline const char* StorageType<Integrator>() { return "Integrator"; }
75
76template<> inline const char* StorageType<Load>() { return "Load"; }
77
78template<> inline const char* StorageType<Material>() { return "Material"; }
79
80template<> inline const char* StorageType<Modifier>() { return "Modifier"; }
81
82template<> inline const char* StorageType<Node>() { return "Node"; }
83
84template<> inline const char* StorageType<Orientation>() { return "Orientation"; }
85
86template<> inline const char* StorageType<Recorder>() { return "Recorder"; }
87
88template<> inline const char* StorageType<Section>() { return "Section"; }
89
90template<> inline const char* StorageType<Solver>() { return "Solver"; }
91
92template<typename T> class Storage : public std::enable_shared_from_this<Storage<T>> {
93 const char* type = StorageType<object_type>();
94
95 std::vector<shared_ptr<T>> fish;
97 const shared_ptr<T> empty = nullptr;
98
99 using const_iterator = typename suanpan::unordered_map<unsigned, shared_ptr<T>>::const_iterator;
100 using iterator = typename suanpan::unordered_map<unsigned, shared_ptr<T>>::iterator;
101
105public:
106 using object_type = T;
107
108 Storage() = default;
109 Storage(const Storage&) = delete;
110 Storage(Storage&&) noexcept = delete;
111 Storage& operator=(const Storage&) = delete;
112 Storage& operator=(Storage&&) noexcept = delete;
113 ~Storage() = default;
114
115 const_iterator cbegin() const;
116 const_iterator cend() const;
117 iterator begin();
118 iterator end();
119
120 bool insert(const shared_ptr<T>&);
121 shared_ptr<T>& operator[](unsigned);
122 const shared_ptr<T>& at(unsigned) const;
123
124 const std::vector<shared_ptr<T>>& get() const;
125
126 [[nodiscard]] bool find(unsigned) const;
127 bool erase(unsigned);
128 void enable(unsigned);
129 void disable(unsigned);
130
131 void update();
132 void enable();
133 void reset();
134 void clear();
135
136 [[nodiscard]] size_t size() const;
137};
138
139template<typename T> typename Storage<T>::const_iterator Storage<T>::cbegin() const { return pond.cbegin(); }
140
141template<typename T> typename Storage<T>::const_iterator Storage<T>::cend() const { return pond.cend(); }
142
143template<typename T> typename Storage<T>::iterator Storage<T>::begin() { return pond.begin(); }
144
145template<typename T> typename Storage<T>::iterator Storage<T>::end() { return pond.end(); }
146
147template<typename T> bool Storage<T>::insert(const shared_ptr<T>& I) {
148 auto flag = pond.insert({I->get_tag(), I}).second;
149 if(!flag)
150 suanpan_warning("Fail to insert {} {}.\n", type, I->get_tag());
151 return flag;
152}
153
154template<typename T> shared_ptr<T>& Storage<T>::operator[](const unsigned L) { return pond[L]; }
155
156template<typename T> const shared_ptr<T>& Storage<T>::at(const unsigned L) const { return pond.contains(L) ? pond.at(L) : empty; }
157
158template<typename T> const std::vector<shared_ptr<T>>& Storage<T>::get() const { return fish; }
159
160template<typename T> bool Storage<T>::find(const unsigned L) const { return pond.contains(L); }
161
162template<typename T> bool Storage<T>::erase(const unsigned L) {
163#ifdef SUANPAN_MT
164 return pond.unsafe_erase(L) == 1;
165#else
166 return pond.erase(L) == 1;
167#endif
168}
169
170template<typename T> void Storage<T>::enable(const unsigned L) { if(find(L)) pond[L]->enable(); }
171
172template<typename T> void Storage<T>::disable(const unsigned L) { if(find(L)) pond[L]->disable(); }
173
174template<typename T> void Storage<T>::update() {
175 reset();
176 fish.reserve(size());
177 for(const auto& [tag, obj] : pond)
178 if(obj->is_active()) fish.push_back(obj);
179 else bait.insert(tag);
180}
181
182template<typename T> void Storage<T>::enable() { for(const auto& I : pond) I.second->enable(); }
183
184template<typename T> void Storage<T>::reset() {
185 fish.clear();
186 bait.clear();
187}
188
189template<typename T> void Storage<T>::clear() {
190 pond.clear();
191 reset();
192}
193
194template<typename T> size_t Storage<T>::size() const { return pond.size(); }
195
196template<typename T> typename Storage<T>::const_iterator cbegin(const Storage<T>& S) { return S.cbegin(); }
197
198template<typename T> typename Storage<T>::const_iterator cend(const Storage<T>& S) { return S.cend(); }
199
200template<typename T> typename Storage<T>::iterator begin(Storage<T>& S) { return S.begin(); }
201
202template<typename T> typename Storage<T>::iterator end(Storage<T>& S) { return S.end(); }
203
204template<typename T> using dual = std::pair<unsigned, shared_ptr<T>>;
205
225
226#endif
227
const char * StorageType< Criterion >()
Definition: Storage.hpp:62
const char * StorageType()
Definition: Storage.hpp:52
std::pair< unsigned, shared_ptr< T > > dual
Definition: Storage.hpp:204
const char * StorageType< Modifier >()
Definition: Storage.hpp:80
const char * StorageType< Converger >()
Definition: Storage.hpp:60
const char * StorageType< Element >()
Definition: Storage.hpp:70
const char * StorageType< Constraint >()
Definition: Storage.hpp:58
const char * StorageType< Orientation >()
Definition: Storage.hpp:84
const char * StorageType< Expression >()
Definition: Storage.hpp:56
const char * StorageType< Solver >()
Definition: Storage.hpp:90
const char * StorageType< DomainBase >()
Definition: Storage.hpp:68
const char * StorageType< Group >()
Definition: Storage.hpp:72
const char * StorageType< Amplitude >()
Definition: Storage.hpp:54
const char * StorageType< Load >()
Definition: Storage.hpp:76
const char * StorageType< Integrator >()
Definition: Storage.hpp:74
const char * StorageType< Domain >()
Definition: Storage.hpp:66
const char * StorageType< Section >()
Definition: Storage.hpp:88
const char * StorageType< Node >()
Definition: Storage.hpp:82
const char * StorageType< Recorder >()
Definition: Storage.hpp:86
const char * StorageType< Database >()
Definition: Storage.hpp:64
const char * StorageType< Material >()
Definition: Storage.hpp:78
An Amplitude class that can generate Amplitude pattern.
Definition: Amplitude.h:67
A Constraint class.
Definition: Constraint.h:36
The Converger class handles converger test to indicate if the iteration converges according to variou...
Definition: Converger.h:44
A Criterion class.
Definition: Criterion.h:38
A Database class is a top level container.
Definition: Database.h:33
The DomainBase class is a template.
Definition: DomainBase.h:104
A Domain class holds all FE model components.
Definition: Domain.h:38
A Element class.
Definition: Element.h:117
A Expression class represents a maths expression.
Definition: Expression.h:34
The Group class.
Definition: Group.h:36
The Integrator class is basically a wrapper of the DomainBase class with regard to some status changi...
Definition: Integrator.h:51
A Load class.
Definition: Load.h:37
A Material abstract base class.
Definition: Material.h:111
A Modifier class.
Definition: Modifier.h:36
The Node class holds the number of DoFs, coordinate, displacement, velocity and acceleration.
Definition: Node.h:80
A Orientation class.
Definition: Orientation.h:48
A Recorder class.
Definition: Recorder.h:35
A Section class.
Definition: Section.h:77
A Solver class defines solvers used in analysis.
Definition: Solver.h:38
A candidate Storage class that stores FEM objects.
Definition: Storage.hpp:92
bool insert(const shared_ptr< T > &)
Definition: Storage.hpp:147
void reset()
Definition: Storage.hpp:184
shared_ptr< T > & operator[](unsigned)
Definition: Storage.hpp:154
bool find(unsigned) const
Definition: Storage.hpp:160
Storage(const Storage &)=delete
void enable()
Definition: Storage.hpp:182
size_t size() const
Definition: Storage.hpp:194
iterator end()
Definition: Storage.hpp:145
const shared_ptr< T > & at(unsigned) const
Definition: Storage.hpp:156
Storage()=default
void clear()
Definition: Storage.hpp:189
void update()
Definition: Storage.hpp:174
const_iterator cend() const
Definition: Storage.hpp:141
iterator begin()
Definition: Storage.hpp:143
bool erase(unsigned)
Definition: Storage.hpp:162
void disable(unsigned)
Definition: Storage.hpp:172
Storage(Storage &&) noexcept=delete
const std::vector< shared_ptr< T > > & get() const
Definition: Storage.hpp:158
T object_type
Definition: Storage.hpp:106
const_iterator cbegin() const
Definition: Storage.hpp:139
std::vector< T > vector
Definition: container.h:53
std::unordered_set< T > unordered_set
Definition: container.h:55
std::unordered_map< T, D > unordered_map
Definition: container.h:57
#define suanpan_warning(...)
Definition: suanPan.h:308