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