HOPS
HOPS class reference
MHO_EncodeDecodeValue.hh
Go to the documentation of this file.
1 #ifndef MHO_EncodeValue_HH__
2 #define MHO_EncodeValue_HH__
3 
4 #include <limits>
5 #include <stack>
6 #include <string>
7 
8 #include "MHO_Types.hh"
9 
10 namespace hops
11 {
12 
36 inline std::string encode_value(const uint64_t& value, const std::string& character_set)
37 {
38  std::string encoded_value = "";
39  std::stack< char > cstack;
40  uint64_t base = character_set.size();
41  if(value >= 0)
42  {
43  uint64_t q, r;
44  q = value;
45  do
46  {
47  r = q % base;
48  q = q / base;
49  cstack.push(character_set[r]);
50  }
51  while(q > 0);
52 
53  while(cstack.size() != 0)
54  {
55  encoded_value += cstack.top();
56  cstack.pop();
57  }
58  }
59  return encoded_value;
60 }
61 
69 inline uint64_t decode_value(const std::string& code, const std::string& character_set)
70 {
71  uint64_t decoded_value = 0;
72  std::stack< char > cstack;
73  uint64_t base = character_set.size();
74  uint64_t bpower = 1;
75  for(auto it = code.rbegin(); it != code.rend(); it++)
76  {
77  uint64_t place_value = character_set.find(*it);
78  if(place_value != std::string::npos)
79  {
80  decoded_value += place_value * bpower;
81  bpower *= base;
82  }
83  else
84  {
85  //out of range error
87  }
88  }
89  return decoded_value;
90 }
91 
96 {
97  public:
99  {
100  //we inherited the set of 64 characters from fourfit
101  //consider how we may want to change this in the future:
102  fDefaultChannelChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$%";
103  //this character set is used for >64 channels when constructing multi-character labels (don't use numbers + $%)
104  fExtendedChannelChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
105  };
106 
108 
114  void SetDefaultChannelChars(const std::string& ch_set) { fDefaultChannelChars = ch_set; }
115 
121  void SetExtendedChannelChars(const std::string& ex_set) { fExtendedChannelChars = ex_set; }
122 
123  //default encoding/decoding scheme
130  std::string EncodeValueToLabel(const uint64_t& value) const
131  {
132  if(value < fDefaultChannelChars.size())
133  {
135  }
136  //else multi-char code
137  uint64_t j = value - fDefaultChannelChars.size() + fExtendedChannelChars.size();
139  }
140 
147  uint64_t DecodeLabelToValue(const std::string& label) const
148  {
149  if(label.size() == 1)
150  {
151  return decode_value(label, fDefaultChannelChars);
152  }
153  //else multi-char code
154  uint64_t extended_value = decode_value(label, fExtendedChannelChars);
155  extended_value -= fExtendedChannelChars.size();
156  extended_value += fDefaultChannelChars.size();
157  return extended_value;
158  }
159 
160  protected:
161 
162  //legal characters in channel labels
163  std::string fDefaultChannelChars;
165 };
166 
167 
168 
169 
170 } // namespace hops
171 
172 #endif
Class MHO_ChannelIndexLabeler.
Definition: MHO_EncodeDecodeValue.hh:96
void SetExtendedChannelChars(const std::string &ex_set)
Setter for extended channel chars.
Definition: MHO_EncodeDecodeValue.hh:121
MHO_ChannelIndexLabeler()
Definition: MHO_EncodeDecodeValue.hh:98
uint64_t DecodeLabelToValue(const std::string &label) const
Decodes a channel label to its corresponding value.
Definition: MHO_EncodeDecodeValue.hh:147
void SetDefaultChannelChars(const std::string &ch_set)
Setter for channel chars - provides the option to use different character sets.
Definition: MHO_EncodeDecodeValue.hh:114
std::string fExtendedChannelChars
Definition: MHO_EncodeDecodeValue.hh:164
std::string fDefaultChannelChars
Definition: MHO_EncodeDecodeValue.hh:163
virtual ~MHO_ChannelIndexLabeler()
Definition: MHO_EncodeDecodeValue.hh:107
std::string EncodeValueToLabel(const uint64_t &value) const
Encodes a uint64_t value into a label string using default and extended encoding schemes.
Definition: MHO_EncodeDecodeValue.hh:130
int base
Definition: fourfit3.c:62
#define max(a, b)
Definition: max555.c:10
Definition: MHO_ChannelLabeler.hh:17
std::string encode_value(const uint64_t &value, const std::string &character_set)
Function encode_value.
Definition: MHO_EncodeDecodeValue.hh:36
uint64_t decode_value(const std::string &code, const std::string &character_set)
Function decode_value.
Definition: MHO_EncodeDecodeValue.hh:69
Definition: vex.h:175