OGS 6.1.0-1721-g6382411ad
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  delete [] v;
150  }
151 
154  T* const& operator[](std::size_t id) const
155  {
156  return _values[std::vector<std::size_t>::operator[](id)];
157  }
158 
159  T* & operator[](std::size_t id)
160  {
161  return _values[std::vector<std::size_t>::operator[](id)];
162  }
163 
164  void initPropertyValue(std::size_t group_id, T const& value)
165  {
166  if (_n_components != 1)
167  OGS_FATAL("Single-component version of initPropertyValue() is called "
168  "for a multi-components PropertyVector<T*>");
169  auto* p = new T[1];
170  p[0] = value;
171  _values[group_id] = p;
172  }
173 
174  void initPropertyValue(std::size_t group_id, std::vector<T> const& values)
175  {
176  if (_n_components != static_cast<int>(values.size()))
177  OGS_FATAL("The size of provided values in initPropertyValue() is "
178  "not same as the number of components in PropertyVector<T*>");
179 
180  auto* p = new T[values.size()];
181  for (unsigned i=0; i<values.size(); i++)
182  p[i] = values[i];
183  _values[group_id] = p;
184  }
185 
186  std::size_t getNumberOfTuples() const
187  {
188  return std::vector<std::size_t>::size();
189  }
190 
192  std::size_t size() const
193  {
194  return _n_components * std::vector<std::size_t>::size();
195  }
196 
198  std::vector<std::size_t> const& exclude_positions) const override
199  {
200  // create new PropertyVector with modified mapping
202  (
203  _values.size()/_n_components,
204  BaseLib::excludeObjectCopy(*this, exclude_positions),
206  )
207  );
208  // copy pointers to property values
209  for (std::size_t j(0); j<_values.size(); j++) {
210  std::vector<T> values(_values[j], _values[j] + _n_components);
211  t->initPropertyValue(j, values);
212  }
213  return t;
214  }
215 
217  T const& getComponent(std::size_t tuple_index, int component) const
218  {
219  assert(component < _n_components);
220  assert(tuple_index < getNumberOfTuples());
221  const double* p = this->operator[](tuple_index);
222  if (p == nullptr)
223  OGS_FATAL("No data found in the property vector %s "
224  "for the tuple index %d and component %d",
225  getPropertyName().c_str(), tuple_index, component);
226  return p[component];
227  }
228 
229 #ifndef NDEBUG
230  std::ostream& print(std::ostream &os) const
231  {
232  os << "\nPropertyVector<T*> at address: " << this << ":\n";
233  os << "\tmapping (" << size() <<"):\n";
234  std::copy(this->cbegin(), this->cend(),
235  std::ostream_iterator<std::size_t>(os, " "));
236  os << "\n\tvalues (" << _values.size() << "):\n";
237  for (std::size_t k(0); k < _values.size(); k++)
238  {
239  os << "val: " << *(_values[k]) << ", address: " << _values[k] << "\n";
240  }
241  return os;
242  }
243 #endif
244 
245 protected:
257  PropertyVector(std::size_t n_prop_groups,
258  std::vector<std::size_t>
259  item2group_mapping,
260  std::string const& property_name,
261  MeshItemType mesh_item_type,
262  std::size_t n_components)
263  : std::vector<std::size_t>(std::move(item2group_mapping)),
264  PropertyVectorBase(property_name, mesh_item_type, n_components),
265  _values(n_prop_groups * n_components)
266  {}
267 
268 private:
269  std::vector<T*> _values;
270  // hide method
271  T* at(std::size_t);
272 };
273 
274 } // 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:28
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:71
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