OGS 6.2.1-97-g73d1aeda3
PropertyVector.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <cassert>
14 #include <iterator>
15 #include <ostream>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include "BaseLib/Algorithm.h"
21 #include "Location.h"
22 
23 namespace MeshLib
24 {
26 {
27 public:
28  virtual PropertyVectorBase* clone(
29  std::vector<std::size_t> const& exclude_positions
30  ) const = 0;
31  virtual ~PropertyVectorBase() = default;
32 
34  std::string const& getPropertyName() const { return _property_name; }
35  int getNumberOfComponents() const { return _n_components; }
36 
37 protected:
38  PropertyVectorBase(std::string property_name,
39  MeshItemType mesh_item_type,
40  std::size_t n_components)
41  : _n_components(n_components),
42  _mesh_item_type(mesh_item_type),
43  _property_name(std::move(property_name))
44  {}
45 
46  int const _n_components;
48  std::string const _property_name;
49 };
50 
55 template <typename PROP_VAL_TYPE>
56 class PropertyVector : public std::vector<PROP_VAL_TYPE>,
57  public PropertyVectorBase
58 {
59 friend class Properties;
60 
61 public:
62  std::size_t getNumberOfTuples() const
63  {
64  return std::vector<PROP_VAL_TYPE>::size() / _n_components;
65  }
66 
68  PROP_VAL_TYPE& getComponent(std::size_t tuple_index, int component)
69  {
70  assert(component < _n_components);
71  assert(tuple_index < getNumberOfTuples());
72  return this->operator[](tuple_index* getNumberOfComponents() +
73  component);
74  }
75 
77  PROP_VAL_TYPE const& getComponent(std::size_t tuple_index,
78  int component) const
79  {
80  assert(component < _n_components);
81  assert(tuple_index < getNumberOfTuples());
82  return this->operator[](tuple_index* getNumberOfComponents() +
83  component);
84  }
85 
87  std::vector<std::size_t> const& exclude_positions) const override
88  {
91  BaseLib::excludeObjectCopy(*this, exclude_positions, *t);
92  return t;
93  }
94 
96  std::size_t size() const
97  {
98  return std::vector<PROP_VAL_TYPE>::size();
99  }
100 
101 protected:
107  explicit PropertyVector(std::string const& property_name,
108  MeshItemType mesh_item_type,
109  std::size_t n_components)
110  : std::vector<PROP_VAL_TYPE>(),
111  PropertyVectorBase(property_name, mesh_item_type, n_components)
112  {}
113 
121  PropertyVector(std::size_t n_property_values,
122  std::string const& property_name,
123  MeshItemType mesh_item_type,
124  std::size_t n_components)
125  : std::vector<PROP_VAL_TYPE>(n_property_values * n_components),
126  PropertyVectorBase(property_name, mesh_item_type, n_components)
127  {}
128 };
129 
139 template <typename T>
140 class PropertyVector<T*> : public std::vector<std::size_t>,
141  public PropertyVectorBase
142 {
143 friend class Properties;
144 public:
146  ~PropertyVector() override
147  {
148  for (auto v : _values)
149  {
150  delete[] v;
151  }
152  }
153 
156  T* const& operator[](std::size_t id) const
157  {
158  return _values[std::vector<std::size_t>::operator[](id)];
159  }
160 
161  T* & operator[](std::size_t id)
162  {
163  return _values[std::vector<std::size_t>::operator[](id)];
164  }
165 
166  void initPropertyValue(std::size_t group_id, T const& value)
167  {
168  if (_n_components != 1)
169  {
170  OGS_FATAL(
171  "Single-component version of initPropertyValue() is called "
172  "for a multi-components PropertyVector<T*>");
173  }
174  auto* p = new T[1];
175  p[0] = value;
176  _values[group_id] = p;
177  }
178 
179  void initPropertyValue(std::size_t group_id, std::vector<T> const& values)
180  {
181  if (_n_components != static_cast<int>(values.size()))
182  {
183  OGS_FATAL(
184  "The size of provided values in initPropertyValue() is "
185  "not same as the number of components in PropertyVector<T*>");
186  }
187 
188  auto* p = new T[values.size()];
189  for (unsigned i = 0; i < values.size(); i++)
190  {
191  p[i] = values[i];
192  }
193  _values[group_id] = p;
194  }
195 
196  std::size_t getNumberOfTuples() const
197  {
198  return std::vector<std::size_t>::size();
199  }
200 
202  std::size_t size() const
203  {
204  return _n_components * std::vector<std::size_t>::size();
205  }
206 
208  std::vector<std::size_t> const& exclude_positions) const override
209  {
210  // create new PropertyVector with modified mapping
212  (
213  _values.size()/_n_components,
214  BaseLib::excludeObjectCopy(*this, exclude_positions),
216  )
217  );
218  // copy pointers to property values
219  for (std::size_t j(0); j<_values.size(); j++) {
220  std::vector<T> values(_values[j], _values[j] + _n_components);
221  t->initPropertyValue(j, values);
222  }
223  return t;
224  }
225 
227  T const& getComponent(std::size_t tuple_index, int component) const
228  {
229  assert(component < _n_components);
230  assert(tuple_index < getNumberOfTuples());
231  const double* p = this->operator[](tuple_index);
232  if (p == nullptr)
233  {
234  OGS_FATAL(
235  "No data found in the property vector %s "
236  "for the tuple index %d and component %d",
237  getPropertyName().c_str(), tuple_index, component);
238  }
239  return p[component];
240  }
241 
242 #ifndef NDEBUG
243  std::ostream& print(std::ostream &os) const
244  {
245  os << "\nPropertyVector<T*> at address: " << this << ":\n";
246  os << "\tmapping (" << size() <<"):\n";
247  std::copy(this->cbegin(), this->cend(),
248  std::ostream_iterator<std::size_t>(os, " "));
249  os << "\n\tvalues (" << _values.size() << "):\n";
250  for (std::size_t k(0); k < _values.size(); k++)
251  {
252  os << "val: " << *(_values[k]) << ", address: " << _values[k] << "\n";
253  }
254  return os;
255  }
256 #endif
257 
258 protected:
270  PropertyVector(std::size_t n_prop_groups,
271  std::vector<std::size_t>
272  item2group_mapping,
273  std::string const& property_name,
274  MeshItemType mesh_item_type,
275  std::size_t n_components)
276  : std::vector<std::size_t>(std::move(item2group_mapping)),
277  PropertyVectorBase(property_name, mesh_item_type, n_components),
278  _values(n_prop_groups * n_components)
279  {}
280 
281 private:
282  std::vector<T*> _values;
283  // hide method
284  T* at(std::size_t);
285 };
286 
287 } // end namespace MeshLib
virtual ~PropertyVectorBase()=default
virtual PropertyVectorBase * clone(std::vector< std::size_t > const &exclude_positions) const =0
std::size_t getNumberOfTuples() const
PropertyVectorBase * clone(std::vector< std::size_t > const &exclude_positions) const override
void initPropertyValue(std::size_t group_id, std::vector< T > const &values)
PropertyVectorBase(std::string property_name, MeshItemType mesh_item_type, std::size_t n_components)
PROP_VAL_TYPE const & getComponent(std::size_t tuple_index, int component) const
Returns the value for the given component stored in the given tuple.
T *const & operator[](std::size_t id) const
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties...
Definition: Properties.h:37
PropertyVectorBase * clone(std::vector< std::size_t > const &exclude_positions) const override
std::vector< T > excludeObjectCopy(std::vector< T > const &src_vec, std::vector< std::size_t > const &exclude_positions)
Definition: Algorithm.h:33
std::size_t size() const
Method returns the number of tuples times the number of tuple components.
~PropertyVector() override
Destructor ensures the deletion of the heap-constructed objects.
MeshItemType
Definition: Location.h:21
PROP_VAL_TYPE & getComponent(std::size_t tuple_index, int component)
Returns the value for the given component stored in the given tuple.
Interface for heuristic search length strategy.
Definition: ProjectData.h:30
std::string const & getPropertyName() const
T const & getComponent(std::size_t tuple_index, int component) const
Returns the value for the given component stored in the given tuple.
static const double p
PropertyVector(std::size_t n_prop_groups, std::vector< std::size_t > item2group_mapping, std::string const &property_name, MeshItemType mesh_item_type, std::size_t n_components)
The constructor taking meta information for the data.
std::string const _property_name
std::ostream & print(std::ostream &os) const
PropertyVector(std::size_t n_property_values, std::string const &property_name, MeshItemType mesh_item_type, std::size_t n_components)
The constructor taking meta information for the data.
std::size_t getNumberOfTuples() const
void initPropertyValue(std::size_t group_id, T const &value)
PropertyVector(std::string const &property_name, MeshItemType mesh_item_type, std::size_t n_components)
The constructor taking meta information for the data.
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
T *& operator[](std::size_t id)
void copy(MatrixOrVector const &x, MatrixOrVector &y)
Copies x to y.
Definition: LinAlg.h:36
MeshItemType getMeshItemType() const
std::size_t size() const
Method returns the number of tuples times the number of tuple components.
MeshItemType const _mesh_item_type