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  virtual ~MHO_Axis(){};
77 
78  //overload the CopyTags function to get special treatment of the index/interval labels
85  virtual void CopyTags(const MHO_Axis& rhs)
86  {
87  if(this != &rhs && !(rhs.fObject.empty()))
88  {
89  this->fObject = rhs.fObject;
90  if(this->fObject.contains("index_labels"))
91  {
92  this->SetIndexLabelObject(&(this->fObject["index_labels"]));
93  }
94  if(this->fObject.contains("interval_labels"))
95  {
96  this->SetIntervalLabelObject(&(this->fObject["interval_labels"]));
97  }
98  }
99  }
100 
109 
110  //index selection from matching axis values
117  std::vector< std::size_t > SelectMatchingIndexes(const std::set< XValueType > label_values)
118  {
119  std::vector< std::size_t > selected_idx;
120  //dumb brute force search, for each label value
121  //check all the axis elements for a match
122  for(auto label_it = label_values.begin(); label_it != label_values.end(); label_it++)
123  {
124  for(std::size_t i = 0; i < this->GetSize(); i++)
125  {
126  if((*this)[i] == *label_it)
127  {
128  selected_idx.push_back(i);
129  }
130  }
131  }
132  return selected_idx;
133  }
134 
141  std::vector< std::size_t > SelectMatchingIndexes(const XValueType& label_value)
142  {
143  std::vector< std::size_t > selected_idx;
144  //dumb brute force search, for a single label value
145  //check all the axis elements for a match
146  for(std::size_t i = 0; i < this->GetSize(); i++)
147  {
148  if((*this)[i] == label_value)
149  {
150  selected_idx.push_back(i);
151  }
152  }
153  return selected_idx;
154  }
155 
156  //index selection for first matching axis values (given a single value)
164  bool SelectFirstMatchingIndex(const XValueType& label_value, std::size_t& result)
165  {
166  result = 0;
167  for(std::size_t i = 0; i < this->GetSize(); i++)
168  {
169  if((*this)[i] == label_value)
170  {
171  result = i;
172  return true;
173  }
174  }
175  return false;
176  }
177 
184  virtual uint64_t GetSerializedSize() const override
185  {
186  uint64_t total_size = 0;
187  total_size += sizeof(MHO_ClassVersion);
189  return total_size;
190  }
191 
192 
193 
202  virtual void Copy(const MHO_Axis& rhs)
203  {
204  if(&rhs != this)
205  {
206  MHO_VectorContainer< XValueType >::Copy(rhs); //copy the 1-d array
207  //make sure we point to the correct index_labels object
208  if(this->fObject.contains("index_labels"))
209  {
210  this->SetIndexLabelObject(&(this->fObject["index_labels"]));
211  }
212 
213  if(this->fObject.contains("interval_labels"))
214  {
215  this->SetIntervalLabelObject(&(this->fObject["interval_labels"]));
216  }
217  }
218  }
219 
220  //access the underlying array data as a std::vector<XValueType>
221  //we have only exposed this for use in the python bindings
222  //perhaps we should make them a friend class and hide this?
223  // std::vector< XValueType >* GetRawVector(){return &(this->fData);}
224 
225  template< typename XStream > friend XStream& operator>>(XStream& s, MHO_Axis& aData)
226  {
227  MHO_ClassVersion vers;
228  s >> vers;
229  switch(vers)
230  {
231  case 0:
232  aData.StreamInData_V0(s);
233  break;
234  default:
236  //Flag this as an unknown object version so we can skip over this data
238  }
239  return s;
240  }
241 
242  template< typename XStream > friend XStream& operator<<(XStream& s, const MHO_Axis& aData)
243  {
244  switch(aData.GetVersion())
245  {
246  case 0:
247  s << aData.GetVersion();
248  aData.StreamOutData_V0(s);
249  break;
250  default:
251  msg_error("containers",
252  "error, cannot stream out MHO_Axis object with unknown version: " << aData.GetVersion() << eom);
253  }
254  return s;
255  }
256 
257  private:
264  template< typename XStream > void StreamInData_V0(XStream& s)
265  {
266  s >> static_cast< MHO_VectorContainer< XValueType >& >(*this);
267  //make sure we point to the correct index_labels object
268  if(this->fObject.contains("index_labels"))
269  {
270  this->SetIndexLabelObject(&(this->fObject["index_labels"]));
271  }
272  if(this->fObject.contains("interval_labels"))
273  {
274  this->SetIntervalLabelObject(&(this->fObject["interval_labels"]));
275  }
276  }
277 
284  template< typename XStream > void StreamOutData_V0(XStream& s) const
285  {
286  s << static_cast< const MHO_VectorContainer< XValueType >& >(*this);
287  }
288 
289  virtual MHO_UUID DetermineTypeUUID() const override
290  {
291  MHO_MD5HashGenerator gen;
292  gen.Initialize();
293  std::string name = MHO_ClassIdentity::ClassName(*this);
294  gen << name;
295  gen.Finalize();
296  return gen.GetDigestAsUUID();
297  }
298 };
299 
300 // ////////////////////////////////////////////////////////////////////////////////
301 // //using declarations for all basic 'plain-old-data' types (except bool!)
319 // ////////////////////////////////////////////////////////////////////////////////
320 
321 } // namespace hops
322 
323 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
template meta-programming helper functions, mostly tuple access/modification
Definition: MHO_Meta.hh:378
Class MHO_Axis.
Definition: MHO_Axis.hh:31
virtual ~MHO_Axis()
Definition: MHO_Axis.hh:76
virtual uint64_t GetSerializedSize() const override
Getter for serialized size of axis object.
Definition: MHO_Axis.hh:184
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:164
virtual void Copy(const MHO_Axis &rhs)
Expensive copy for MHO_Axis that handles special treatment of index/interval labels.
Definition: MHO_Axis.hh:202
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:117
friend XStream & operator<<(XStream &s, const MHO_Axis &aData)
Definition: MHO_Axis.hh:242
std::vector< std::size_t > SelectMatchingIndexes(const XValueType &label_value)
Selects indexes for matching axis values (given a single value)
Definition: MHO_Axis.hh:141
friend XStream & operator>>(XStream &s, MHO_Axis &aData)
Definition: MHO_Axis.hh:225
MHO_Axis(std::size_t dim)
Definition: MHO_Axis.hh:46
MHO_Axis()
Definition: MHO_Axis.hh:34
virtual void CopyTags(const MHO_Axis &rhs)
Copies tags from rhs MHO_Axis object, handling index/interval labels specially.
Definition: MHO_Axis.hh:85
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:23
void SetIndexLabelObject(mho_json *obj)
Setter for index label object.
Definition: MHO_IndexLabelInterface.hh:38
Class MHO_IntervalLabelInterface - adds intervals with associated key:value pairs (used by MHO_Axis) ...
Definition: MHO_IntervalLabelInterface.hh:23
void SetIntervalLabelObject(mho_json *obj)
Setter for interval label object.
Definition: MHO_IntervalLabelInterface.hh:45
mho_json fObject
Definition: MHO_JSONHeaderWrapper.hh:43
std::size_t GetSize() const
Getter for size.
Definition: MHO_NDArrayWrapper_1.hh:107
MHO_VectorContainer - basis for for axis data objects in HOPS4, it is an 1-dimensional array object,...
Definition: MHO_VectorContainer.hh:28
virtual MHO_ClassVersion GetVersion() const override
Getter for version.
Definition: MHO_VectorContainer.hh:55
virtual void Copy(const MHO_VectorContainer &rhs)
Expensive copy for MHO_VectorContainer, pointers to exernally managed memory are not transferred,...
Definition: MHO_VectorContainer.hh:82
virtual uint64_t GetSerializedSize() const override
Getter for serialized size.
Definition: MHO_VectorContainer.hh:63
Definition: MHO_ChannelLabeler.hh:17
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:99
static std::string ClassName()
Returns the class name as a string.
Definition: MHO_ClassIdentity.hh:36
static void SetUnknown(XStreamType &)
Setter for unknown.
Definition: MHO_FileStreamer.hh:215