suanPan
Storage.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2017-2022 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 
32 class Amplitude;
33 class Constraint;
34 class Converger;
35 class Criterion;
36 class Database;
37 class Domain;
38 class DomainBase;
39 class Element;
40 class Group;
41 class Integrator;
42 class Load;
43 class Material;
44 class Modifier;
45 class Node;
46 class Orientation;
47 class Recorder;
48 class Section;
49 class Solver;
50 
51 template<typename T> const char* StorageType() { return "Unknown"; }
52 
53 template<> inline const char* StorageType<Amplitude>() { return "Amplitude"; }
54 
55 template<> inline const char* StorageType<Constraint>() { return "Constraint"; }
56 
57 template<> inline const char* StorageType<Converger>() { return "Converger"; }
58 
59 template<> inline const char* StorageType<Criterion>() { return "Criterion"; }
60 
61 template<> inline const char* StorageType<Database>() { return "Database"; }
62 
63 template<> inline const char* StorageType<Domain>() { return "Domain"; }
64 
65 template<> inline const char* StorageType<DomainBase>() { return "Domain"; }
66 
67 template<> inline const char* StorageType<Element>() { return "Element"; }
68 
69 template<> inline const char* StorageType<Group>() { return "Group"; }
70 
71 template<> inline const char* StorageType<Integrator>() { return "Integrator"; }
72 
73 template<> inline const char* StorageType<Load>() { return "Load"; }
74 
75 template<> inline const char* StorageType<Material>() { return "Material"; }
76 
77 template<> inline const char* StorageType<Modifier>() { return "Modifier"; }
78 
79 template<> inline const char* StorageType<Node>() { return "Node"; }
80 
81 template<> inline const char* StorageType<Orientation>() { return "Orientation"; }
82 
83 template<> inline const char* StorageType<Recorder>() { return "Recorder"; }
84 
85 template<> inline const char* StorageType<Section>() { return "Section"; }
86 
87 template<> inline const char* StorageType<Solver>() { return "Solver"; }
88 
89 template<typename T> class Storage : public std::enable_shared_from_this<Storage<T>> {
90  const char* type = StorageType<object_type>();
91 
92  std::vector<shared_ptr<T>> fish;
94  const shared_ptr<T> empty = nullptr;
95 
96  using const_iterator = typename suanpan::unordered_map<unsigned, shared_ptr<T>>::const_iterator;
97  using iterator = typename suanpan::unordered_map<unsigned, shared_ptr<T>>::iterator;
98 
102 public:
103  typedef T object_type;
104 
106  Storage(const Storage&) = delete;
107  Storage(Storage&&) noexcept = delete;
108  Storage& operator=(const Storage&) = delete;
109  Storage& operator=(Storage&&) noexcept = delete;
111 
112  const_iterator cbegin() const;
113  const_iterator cend() const;
114  iterator begin();
115  iterator end();
116 
117  bool insert(const shared_ptr<T>&);
118  shared_ptr<T>& operator[](unsigned);
119  const shared_ptr<T>& at(unsigned) const;
120 
121  const std::vector<shared_ptr<T>>& get() const;
122 
123  [[nodiscard]] bool find(unsigned) const;
124  bool erase(unsigned);
125  void enable(unsigned);
126  void disable(unsigned);
127 
128  void update();
129  void enable();
130  void reset();
131  void clear();
132 
133  [[nodiscard]] size_t size() const;
134 };
135 
136 template<typename T> Storage<T>::Storage() { suanpan_debug("Storage %s ctor() called.\n", type); }
137 
138 template<typename T> Storage<T>::~Storage() { suanpan_debug("Storage %s dtor() called.\n", type); }
139 
140 template<typename T> typename Storage<T>::const_iterator Storage<T>::cbegin() const { return pond.cbegin(); }
141 
142 template<typename T> typename Storage<T>::const_iterator Storage<T>::cend() const { return pond.cend(); }
143 
144 template<typename T> typename Storage<T>::iterator Storage<T>::begin() { return pond.begin(); }
145 
146 template<typename T> typename Storage<T>::iterator Storage<T>::end() { return pond.end(); }
147 
148 template<typename T> bool Storage<T>::insert(const shared_ptr<T>& I) {
149  auto flag = pond.insert({I->get_tag(), I}).second;
150  if(!flag) suanpan_warning("Storage fails to insert %s %u.\n", type, I->get_tag());
151  return flag;
152 }
153 
154 template<typename T> shared_ptr<T>& Storage<T>::operator[](const unsigned L) { return pond[L]; }
155 
156 template<typename T> const shared_ptr<T>& Storage<T>::at(const unsigned L) const { return pond.contains(L) ? pond.at(L) : empty; }
157 
158 template<typename T> const std::vector<shared_ptr<T>>& Storage<T>::get() const { return fish; }
159 
160 template<typename T> bool Storage<T>::find(const unsigned L) const { return pond.contains(L); }
161 
162 template<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 
170 template<typename T> void Storage<T>::enable(const unsigned L) { if(find(L)) pond[L]->enable(); }
171 
172 template<typename T> void Storage<T>::disable(const unsigned L) { if(find(L)) pond[L]->disable(); }
173 
174 template<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 
182 template<typename T> void Storage<T>::enable() { for(const auto& I : pond) I.second->enable(); }
183 
184 template<typename T> void Storage<T>::reset() {
185  fish.clear();
186  bait.clear();
187 }
188 
189 template<typename T> void Storage<T>::clear() {
190  pond.clear();
191  reset();
192 }
193 
194 template<typename T> size_t Storage<T>::size() const { return pond.size(); }
195 
196 template<typename T> typename Storage<T>::const_iterator cbegin(const Storage<T>& S) { return S.cbegin(); }
197 
198 template<typename T> typename Storage<T>::const_iterator cend(const Storage<T>& S) { return S.cend(); }
199 
200 template<typename T> typename Storage<T>::iterator begin(Storage<T>& S) { return S.begin(); }
201 
202 template<typename T> typename Storage<T>::iterator end(Storage<T>& S) { return S.end(); }
203 
222 
223 #endif
224 
const char * StorageType< Material >()
Definition: Storage.hpp:75
const char * StorageType< Load >()
Definition: Storage.hpp:73
const char * StorageType< Constraint >()
Definition: Storage.hpp:55
const char * StorageType< Amplitude >()
Definition: Storage.hpp:53
const char * StorageType< Database >()
Definition: Storage.hpp:61
const char * StorageType()
Definition: Storage.hpp:51
const char * StorageType< Domain >()
Definition: Storage.hpp:63
const char * StorageType< DomainBase >()
Definition: Storage.hpp:65
const char * StorageType< Node >()
Definition: Storage.hpp:79
const char * StorageType< Solver >()
Definition: Storage.hpp:87
const char * StorageType< Recorder >()
Definition: Storage.hpp:83
const char * StorageType< Criterion >()
Definition: Storage.hpp:59
const char * StorageType< Integrator >()
Definition: Storage.hpp:71
const char * StorageType< Element >()
Definition: Storage.hpp:67
const char * StorageType< Section >()
Definition: Storage.hpp:85
const char * StorageType< Converger >()
Definition: Storage.hpp:57
const char * StorageType< Modifier >()
Definition: Storage.hpp:77
const char * StorageType< Group >()
Definition: Storage.hpp:69
const char * StorageType< Orientation >()
Definition: Storage.hpp:81
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:90
A Domain class holds all FE model components.
Definition: Domain.h:37
A Element class.
Definition: Element.h:92
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:46
A Load class.
Definition: Load.h:37
A Material abstract base class.
Definition: Material.h:102
A Modifier class.
Definition: Modifier.h:36
The Node class holds the number of DoFs, coordinate, displacement, velocity and acceleration.
Definition: Node.h:77
A Orientation class.
Definition: Orientation.h:40
A Recorder class.
Definition: Recorder.h:35
A Section class.
Definition: Section.h:73
A Solver class defines solvers used in analysis.
Definition: Solver.h:38
A candidate Storage class that stores FEM objects.
Definition: Storage.hpp:89
bool insert(const shared_ptr< T > &)
Definition: Storage.hpp:148
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
Storage()
Definition: Storage.hpp:136
size_t size() const
Definition: Storage.hpp:194
~Storage()
Definition: Storage.hpp:138
iterator end()
Definition: Storage.hpp:146
const shared_ptr< T > & at(unsigned) const
Definition: Storage.hpp:156
void clear()
Definition: Storage.hpp:189
void update()
Definition: Storage.hpp:174
const_iterator cend() const
Definition: Storage.hpp:142
iterator begin()
Definition: Storage.hpp:144
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:103
const_iterator cbegin() const
Definition: Storage.hpp:140
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
void suanpan_debug(const char *M,...)
Definition: print.cpp:64
void suanpan_warning(const char *M,...)
Definition: print.cpp:101