HOPS
HOPS class reference
MHO_Axis.hh
Go to the documentation of this file.
1 #ifndef MHO_Axis_HH__
2 #define MHO_Axis_HH__
3 
4 #include <set>
5 
9 #include "MHO_Meta.hh"
10 #include "MHO_VectorContainer.hh"
11 
12 namespace hops
13 {
14 
26 template< typename XValueType >
27 class MHO_Axis: public MHO_AxisBase,
28  public MHO_VectorContainer< XValueType >,
31 {
32 
33  public:
35  {
36  //create and set the pointer to the index label object
37  //std::vector<mho_json> tmp;
38  this->fObject["index_labels"] = mho_json(); // mho_json::array(); //tmp;
39  this->SetIndexLabelObject(&(this->fObject["index_labels"]));
40 
41  //create and set the pointer to the interval label object
42  this->fObject["interval_labels"] = mho_json();
43  this->SetIntervalLabelObject(&(this->fObject["interval_labels"]));
44  };
45 
46  MHO_Axis(std::size_t dim)
48  {
49  //create and set the pointer to the index label object
50  //std::vector<mho_json> tmp;
51  this->fObject["index_labels"] = mho_json(); // mho_json::array(); //tmp;
52  this->SetIndexLabelObject(&(this->fObject["index_labels"]));
53 
54  //create and set the pointer to the interval label object
55  this->fObject["interval_labels"] = mho_json();
56  this->SetIntervalLabelObject(&(this->fObject["interval_labels"]));
57  };
58 
59  //copy constructor
60  MHO_Axis(const MHO_Axis& obj)
62  {
63  if(obj.fObject.contains("index_labels"))
64  {
65  this->fObject["index_labels"] = obj.fObject["index_labels"];
66  }
67  if(obj.fObject.contains("interval_labels"))
68  {
69  this->fObject["interval_labels"] = obj.fObject["interval_labels"];
70  }
71 
72  this->SetIndexLabelObject(&(this->fObject["index_labels"]));
73  this->SetIntervalLabelObject(&(this->fObject["interval_labels"]));
74  };
75 
76  //explicit assignment operator, follows the copy contructor
78  {
79  if(this != &rhs)
80  {
82  this->SetIndexLabelObject(&(this->fObject["index_labels"]));
83  this->SetIntervalLabelObject(&(this->fObject["interval_labels"]));
84  }
85  return *this;
86  };
87 
88  virtual ~MHO_Axis(){};
89 
90  //overload the CopyTags function to get special treatment of the index/interval labels
97  virtual void CopyTags(const MHO_Axis& rhs)
98  {
99  if(this != &rhs && !(rhs.fObject.empty()))
100  {
101  this->fObject = rhs.fObject;
102  if(this->fObject.contains("index_labels"))
103  {
104  this->SetIndexLabelObject(&(this->fObject["index_labels"]));
105  }
106  if(this->fObject.contains("interval_labels"))
107  {
108  this->SetIntervalLabelObject(&(this->fObject["interval_labels"]));
109  }
110  }
111  }
112 
121 
122  //index selection from matching axis values
129  std::vector< std::size_t > SelectMatchingIndexes(const std::set< XValueType > label_values)
130  {
131  std::vector< std::size_t > selected_idx;
132  //dumb brute force search, for each label value
133  //check all the axis elements for a match
134  for(auto label_it = label_values.begin(); label_it != label_values.end(); label_it++)
135  {
136  for(std::size_t i = 0; i < this->GetSize(); i++)
137  {
138  if((*this)[i] == *label_it)
139  {
140  selected_idx.push_back(i);
141  }
142  }
143  }
144  return selected_idx;
145  }
146 
153  std::vector< std::size_t > SelectMatchingIndexes(const XValueType& label_value)
154  {
155  std::vector< std::size_t > selected_idx;
156  //dumb brute force search, for a single label value
157  //check all the axis elements for a match
158  for(std::size_t i = 0; i < this->GetSize(); i++)
159  {
160  if((*this)[i] == label_value)
161  {
162  selected_idx.push_back(i);
163  }
164  }
165  return selected_idx;
166  }
167 
168  //index selection for first matching axis values (given a single value)
176  bool SelectFirstMatchingIndex(const XValueType& label_value, std::size_t& result)
177  {
178  result = 0;
179  for(std::size_t i = 0; i < this->GetSize(); i++)
180  {
181  if((*this)[i] == label_value)
182  {
183  result = i;
184  return true;
185  }
186  }
187  return false;
188  }
189 
196  virtual uint64_t GetSerializedSize() const override
197  {
198  uint64_t total_size = 0;
199  total_size += sizeof(MHO_ClassVersion);
201  return total_size;
202  }
203 
212  virtual void Copy(const MHO_Axis& rhs)
213  {
214  if(&rhs != this)
215  {
216  MHO_VectorContainer< XValueType >::Copy(rhs); //copy the 1-d array
217  //make sure we point to the correct index_labels object
218  if(this->fObject.contains("index_labels"))
219  {
220  this->SetIndexLabelObject(&(this->fObject["index_labels"]));
221  }
222 
223  if(this->fObject.contains("interval_labels"))
224  {
225  this->SetIntervalLabelObject(&(this->fObject["interval_labels"]));
226  }
227  }
228  }
229 
230  //access the underlying array data as a std::vector<XValueType>
231  //we have only exposed this for use in the python bindings
232  //perhaps we should make them a friend class and hide this?
233  // std::vector< XValueType >* GetRawVector(){return &(this->fData);}
234 
235  template< typename XStream > friend XStream& operator>>(XStream& s, MHO_Axis& aData)
236  {
237  MHO_ClassVersion vers;
238  s >> vers;
239  switch(vers)
240  {
241  case 0:
242  aData.StreamInData_V0(s);
243  break;
244  default:
246  //Flag this as an unknown object version so we can skip over this data
248  }
249  return s;
250  }
251 
252  template< typename XStream > friend XStream& operator<<(XStream& s, const MHO_Axis& aData)
253  {
254  switch(aData.GetVersion())
255  {
256  case 0:
257  s << aData.GetVersion();
258  aData.StreamOutData_V0(s);
259  break;
260  default:
261  msg_error("containers",
262  "error, cannot stream out MHO_Axis object with unknown version: " << aData.GetVersion() << eom);
263  }
264  return s;
265  }
266 
267  private:
274  template< typename XStream > void StreamInData_V0(XStream& s)
275  {
276  s >> static_cast< MHO_VectorContainer< XValueType >& >(*this);
277  //make sure we point to the correct index_labels object
278  if(this->fObject.contains("index_labels"))
279  {
280  this->SetIndexLabelObject(&(this->fObject["index_labels"]));
281  }
282  if(this->fObject.contains("interval_labels"))
283  {
284  this->SetIntervalLabelObject(&(this->fObject["interval_labels"]));
285  }
286  }
287 
294  template< typename XStream > void StreamOutData_V0(XStream& s) const
295  {
296  s << static_cast< const MHO_VectorContainer< XValueType >& >(*this);
297  }
298 
299  virtual MHO_UUID DetermineTypeUUID() const override
300  {
301  MHO_MD5HashGenerator gen;
302  gen.Initialize();
303  std::string name = MHO_ClassIdentity::ClassName(*this);
304  gen << name;
305  gen.Finalize();
306  return gen.GetDigestAsUUID();
307  }
308 };
309 
310 // ////////////////////////////////////////////////////////////////////////////////
311 // //using declarations for all basic 'plain-old-data' types (except bool!)
329 // ////////////////////////////////////////////////////////////////////////////////
330 
331 } // namespace hops
332 
333 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
template meta-programming helper functions, mostly tuple access/modification
Definition: MHO_Meta.hh:408
Class MHO_Axis.
Definition: MHO_Axis.hh:31
virtual ~MHO_Axis()
Definition: MHO_Axis.hh:88
virtual uint64_t GetSerializedSize() const override
Getter for serialized size of axis object.
Definition: MHO_Axis.hh:196
bool SelectFirstMatchingIndex(const XValueType &label_value, std::size_t &result)
Selects first matching index for a given label value in axis values.
Definition: MHO_Axis.hh:176
virtual void Copy(const MHO_Axis &rhs)
Expensive copy for MHO_Axis that handles special treatment of index/interval labels.
Definition: MHO_Axis.hh:212
std::vector< std::size_t > SelectMatchingIndexes(const std::set< XValueType > label_values)
Selects indexes from axis where the label values match the provided labels, dumb brute force search.
Definition: MHO_Axis.hh:129
friend XStream & operator<<(XStream &s, const MHO_Axis &aData)
Definition: MHO_Axis.hh:252
std::vector< std::size_t > SelectMatchingIndexes(const XValueType &label_value)
Selects indexes for matching axis values (given a single value)
Definition: MHO_Axis.hh:153
friend XStream & operator>>(XStream &s, MHO_Axis &aData)
Definition: MHO_Axis.hh:235
MHO_Axis(std::size_t dim)
Definition: MHO_Axis.hh:46
MHO_Axis()
Definition: MHO_Axis.hh:34
MHO_Axis & operator=(const MHO_Axis &rhs)
Definition: MHO_Axis.hh:77
virtual void CopyTags(const MHO_Axis &rhs)
Copies tags from rhs MHO_Axis object, handling index/interval labels specially.
Definition: MHO_Axis.hh:97
MHO_Axis(const MHO_Axis &obj)
Definition: MHO_Axis.hh:60
Class MHO_IndexLabelInterface - adds indexes associated with key:value pairs (used by MHO_Axis) const...
Definition: MHO_IndexLabelInterface.hh:22
void SetIndexLabelObject(mho_json *obj)
Setter for index label object.
Definition: MHO_IndexLabelInterface.hh:37
Class MHO_IntervalLabelInterface - adds intervals with associated key:value pairs (used by MHO_Axis) ...
Definition: MHO_IntervalLabelInterface.hh:22
void SetIntervalLabelObject(mho_json *obj)
Setter for interval label object.
Definition: MHO_IntervalLabelInterface.hh:44
mho_json fObject
Definition: MHO_JSONHeaderWrapper.hh:43
MHO_NDArrayWrapper & operator=(const MHO_NDArrayWrapper &rhs)
Definition: MHO_NDArrayWrapper_1.hh:162
std::size_t GetSize() const
Getter for size.
Definition: MHO_NDArrayWrapper_1.hh:99
MHO_VectorContainer - basis for for axis data objects in HOPS4, it is an 1-dimensional array object,...
Definition: MHO_VectorContainer.hh:27
virtual MHO_ClassVersion GetVersion() const override
Getter for version.
Definition: MHO_VectorContainer.hh:54
virtual void Copy(const MHO_VectorContainer &rhs)
Expensive copy for MHO_VectorContainer, pointers to exernally managed memory are not transferred,...
Definition: MHO_VectorContainer.hh:80
virtual uint64_t GetSerializedSize() const override
Getter for serialized size.
Definition: MHO_VectorContainer.hh:62
Definition: MHO_AdhocFlagging.hh:18
uint32_t MHO_ClassVersion
Definition: MHO_ClassIdentity.hh:22
static void ClassVersionErrorMsg(const XClassType &obj, MHO_ClassVersion version)
Generates an error message for when an unknown or unsupported class version is encountered.
Definition: MHO_ClassIdentity.hh:112
static std::string ClassName()
Returns the class name as a string.
Definition: MHO_ClassIdentity.hh:51
static void SetUnknown(XStreamType &)
Setter for unknown.
Definition: MHO_FileStreamer.hh:214