HOPS
HOPS class reference
MHO_IntervalLabelInterface.hh
Go to the documentation of this file.
1 #ifndef MHO_IntervalLabelInterface_HH__
2 #define MHO_IntervalLabelInterface_HH__
3 
5 
6 namespace hops
7 {
8 
23 {
24  protected:
25  MHO_IntervalLabelInterface(): fIntervalLabelObjectPtr(nullptr)
26  {
27  // fTokenizer.SetDelimiter(",");
28  fDummy["lower_index"] = -1; //dummy object for invalid returns, always has an invalid index
29  fDummy["upper_index"] = -1; //dummy object for invalid returns, always has an invalid index
30  };
31 
33  {
34  // fTokenizer.SetDelimiter(",");
35  fDummy["lower_index"] = -1; //dummy object for invalid returns, always has an invalid index
36  fDummy["upper_index"] = -1; //dummy object for invalid returns, always has an invalid index
37  fIntervalLabelObjectPtr = copy.fIntervalLabelObjectPtr;
38  };
39 
45  void SetIntervalLabelObject(mho_json* obj) { fIntervalLabelObjectPtr = obj; }
46 
47  public:
49 
53  void ClearIntervalLabels() { *fIntervalLabelObjectPtr = mho_json(); }
54 
64  template< typename XValueType >
65  void InsertIntervalLabelKeyValue(std::size_t lower_index, std::size_t upper_index, const std::string& key,
66  const XValueType& value)
67  {
68  std::string ikey = ConstructKey(lower_index, upper_index);
69  (*fIntervalLabelObjectPtr)[ikey][key] = value;
70  (*fIntervalLabelObjectPtr)[ikey]["lower_index"] = lower_index;
71  (*fIntervalLabelObjectPtr)[ikey]["upper_index"] = upper_index;
72  }
73 
84  template< typename XValueType >
85  bool RetrieveIntervalLabelKeyValue(std::size_t lower_index, std::size_t upper_index, const std::string& key,
86  const XValueType& value) const
87  {
88  std::string ikey = ConstructKey(lower_index, upper_index);
89  if(fIntervalLabelObjectPtr->contains(ikey))
90  {
91  if((*fIntervalLabelObjectPtr)[ikey].contains(key))
92  {
93  value = (*fIntervalLabelObjectPtr)[ikey][key].get< XValueType >();
94  return true;
95  }
96  }
97  else
98  {
99  msg_warn("containers", "cannot retrieve a key value pair for interval: " << ikey << "." << eom);
100  }
101  return false;
102  }
103 
111  mho_json& GetIntervalLabelObject(std::size_t lower_index, std::size_t upper_index)
112  {
113  std::string ikey = ConstructKey(lower_index, upper_index);
114  if(fIntervalLabelObjectPtr->contains(ikey))
115  {
116  return (*fIntervalLabelObjectPtr)[ikey];
117  }
118  else
119  {
120  msg_warn("containers", "cannot retrieve interval data for: " << ikey << "." << eom);
121  return fDummy;
122  }
123  }
124 
132  void SetIntervalLabelObject(mho_json& obj, std::size_t lower_index, std::size_t upper_index)
133  {
134  std::string ikey = ConstructKey(lower_index, upper_index);
135  obj["lower_index"] = std::min(lower_index, upper_index);
136  obj["upper_index"] = std::max(lower_index, upper_index);
137  (*fIntervalLabelObjectPtr)[ikey] = obj;
138  // (*fIntervalLabelObjectPtr).emplace(ikey,obj);
139  }
140 
147  std::vector< mho_json > GetMatchingIntervalLabels(std::string key) const
148  {
149  std::vector< mho_json > objects;
150  for(auto it : fIntervalLabelObjectPtr->items())
151  {
152  if(it.value().contains(key))
153  {
154  objects.push_back(it.value());
155  }
156  }
157  return objects;
158  }
159 
168  template< typename XLabelValueType >
169  mho_json GetFirstIntervalWithKeyValue(std::string key, const XLabelValueType& value) const
170  {
171  mho_json obj;
172  for(auto it : fIntervalLabelObjectPtr->items())
173  {
174  if(it.value().contains(key))
175  {
176  XLabelValueType v;
177  v = it.value()[key];
178  if(v == value)
179  {
180  obj = it.value();
181  break;
182  }
183  }
184  }
185  return obj;
186  }
187 
196  static std::string ConstructKey(std::size_t lower_index, std::size_t upper_index)
197  {
198  if(lower_index <= upper_index)
199  {
200  return std::to_string(lower_index) + "," + std::to_string(upper_index);
201  }
202  else
203  {
204  //flip them around
205  return std::to_string(upper_index) + "," + std::to_string(lower_index);
206  }
207  }
208 
217  bool ExtractIndexesFromKey(const std::string& key, std::size_t& lower_index, std::size_t& upper_index)
218  {
219  lower_index = 0;
220  upper_index = 0;
221  if(key.find(',') == std::string::npos)
222  {
223  return false;
224  }
225  auto idx_pair = ExtractIndexesFromKey(key);
226  lower_index = idx_pair.first;
227  upper_index = idx_pair.second;
228  if(upper_index < lower_index)
229  {
230  lower_index = idx_pair.second;
231  upper_index = idx_pair.first;
232  }
233  return true;
234  }
235 
243  static std::pair< std::size_t, std::size_t > ExtractIndexesFromKey(const std::string& key)
244  {
245  std::size_t lower_index = 0;
246  std::size_t upper_index = 0;
247  size_t pos = key.find(',');
248  if(pos == std::string::npos)
249  {
250  return std::make_pair(lower_index, upper_index);
251  }
252  std::string first = key.substr(0, pos);
253  std::string second = key.substr(pos + 1);
254  lower_index = std::stoul(first);
255  upper_index = std::stoul(second);
256  if(upper_index < lower_index)
257  {
258  //flip them
259  std::make_pair(upper_index, lower_index);
260  }
261  return std::make_pair(lower_index, upper_index);
262  }
263 
264  private:
265  mho_json* fIntervalLabelObjectPtr; //array of mho_json objects holding key:value pairs
266  mho_json fDummy;
267 };
268 
269 } // namespace hops
270 
271 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
#define msg_warn(xKEY, xCONTENT)
Definition: MHO_Message.hh:254
Class MHO_IntervalLabelInterface - adds intervals with associated key:value pairs (used by MHO_Axis) ...
Definition: MHO_IntervalLabelInterface.hh:23
void ClearIntervalLabels()
Clears interval labels by setting *fIntervalLabelObjectPtr to an empty json object.
Definition: MHO_IntervalLabelInterface.hh:53
void SetIntervalLabelObject(mho_json &obj, std::size_t lower_index, std::size_t upper_index)
Setter for interval label object.
Definition: MHO_IntervalLabelInterface.hh:132
std::vector< mho_json > GetMatchingIntervalLabels(std::string key) const
get a vector of interval labels which contain a key with the same name
Definition: MHO_IntervalLabelInterface.hh:147
bool RetrieveIntervalLabelKeyValue(std::size_t lower_index, std::size_t upper_index, const std::string &key, const XValueType &value) const
Retrieves a value associated with a key within an interval range.
Definition: MHO_IntervalLabelInterface.hh:85
void SetIntervalLabelObject(mho_json *obj)
Setter for interval label object.
Definition: MHO_IntervalLabelInterface.hh:45
MHO_IntervalLabelInterface(const MHO_IntervalLabelInterface &copy)
Definition: MHO_IntervalLabelInterface.hh:32
static std::pair< std::size_t, std::size_t > ExtractIndexesFromKey(const std::string &key)
Extracts lower and upper indexes from a key string separated by comma.
Definition: MHO_IntervalLabelInterface.hh:243
static std::string ConstructKey(std::size_t lower_index, std::size_t upper_index)
Constructs a key string from lower and upper indices, ensuring lower_index <= upper_index.
Definition: MHO_IntervalLabelInterface.hh:196
mho_json GetFirstIntervalWithKeyValue(std::string key, const XLabelValueType &value) const
Getter for first interval with key value.
Definition: MHO_IntervalLabelInterface.hh:169
virtual ~MHO_IntervalLabelInterface()
Definition: MHO_IntervalLabelInterface.hh:48
mho_json & GetIntervalLabelObject(std::size_t lower_index, std::size_t upper_index)
Get a reference to the dictionary object associated with this index.
Definition: MHO_IntervalLabelInterface.hh:111
bool ExtractIndexesFromKey(const std::string &key, std::size_t &lower_index, std::size_t &upper_index)
Extracts lower and upper indexes from a key string separated by comma.
Definition: MHO_IntervalLabelInterface.hh:217
void InsertIntervalLabelKeyValue(std::size_t lower_index, std::size_t upper_index, const std::string &key, const XValueType &value)
Inserts an interval label key-value pair into a map referenced by fIntervalLabelObjectPtr.
Definition: MHO_IntervalLabelInterface.hh:65
MHO_IntervalLabelInterface()
Definition: MHO_IntervalLabelInterface.hh:25
#define min(a, b)
Definition: max555.c:9
#define max(a, b)
Definition: max555.c:10
Definition: MHO_ChannelLabeler.hh:17
Definition: vex.h:175