PandA-2024.02
utility.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
42 #ifndef UTILITY_HPP
43 #define UTILITY_HPP
44 
45 #include "panda_types.hpp"
46 #include "string_manipulation.hpp"
47 
48 #include "config_HAVE_ASSERTS.hpp"
49 
50 #include <boost/concept/usage.hpp>
51 #include <boost/iterator/iterator_facade.hpp>
52 #include <boost/lexical_cast.hpp>
53 
54 #include <limits>
55 #include <sstream>
56 #include <string>
57 #include <utility>
58 #include <vector>
59 
61 #define STR_CST_string_separator std::string(1, 31)
62 
64 #define STR_CST_string_separator_char static_cast<char>(31)
65 
67 #define INFINITE_INT (std::numeric_limits<int>::max())
68 
70 #define INFINITE_UINT (std::numeric_limits<unsigned int>::max())
71 
73 #define INFINITE_SIZE_T (std::numeric_limits<size_t>::max())
74 
76 #define INFINITE_DOUBLE (std::numeric_limits<double>::max())
77 
79 #define INFINITE_LONG_DOUBLE (std::numeric_limits<long double>::max())
80 
82 #define INFINITE_ULONGLONG_INT (std::numeric_limits<unsigned long long int>::max())
83 
85 #define MINUS_INFINITE_LONG_DOUBLE (std::numeric_limits<long double>::min())
86 
88 #ifdef NDEBUG
89 #define DEBUG_PARAMETER(parameter)
90 #else
91 #define DEBUG_PARAMETER(parameter) parameter
92 #endif
93 #if HAVE_ASSERTS
94 #define ASSERT_PARAMETER(parameter) parameter
95 #else
96 #define ASSERT_PARAMETER(parameter)
97 #endif
98 
102 #define GET_CLASS_NAME(meth) #meth
103 
107 #define GET_FUNCTION_DEBUG_LEVEL(parameters) parameters->GetFunctionDebugLevel(GET_CLASS(*this), __func__)
108 
109 template <class G>
110 std::string convert_to_binary(G _value, unsigned long long precision)
111 {
112  auto value = integer_cst_t(_value);
113  std::string bin_value;
114  for(unsigned int ind = 0; ind < precision; ind++)
115  {
116  bin_value = bin_value + (((integer_cst_t(1) << (precision - ind - 1)) & value) ? '1' : '0');
117  }
118  return bin_value;
119 }
120 
121 template <typename _InputIt>
122 std::string container_to_string(_InputIt first, _InputIt last, const std::string& separator, bool trim_empty = true)
123 {
124  std::string str;
125  while(first != last)
126  {
127  auto estr = boost::lexical_cast<std::string>(*first);
128  if(!trim_empty || estr.size())
129  {
130  if(str.size())
131  {
132  str += separator;
133  }
134  str += estr;
135  }
136  ++first;
137  }
138  return str;
139 }
140 
141 template <typename _Container>
142 std::string container_to_string(const _Container& _c, const std::string& separator, bool trim_empty = true)
143 {
144  return container_to_string(_c.begin(), _c.end(), separator, trim_empty);
145 }
146 
147 template <typename _OutputIt>
148 constexpr void string_to_container(_OutputIt first, const std::string& str, const std::string& separator,
149  bool trim_empty = true)
150 {
151  auto curr = str.data();
152  std::string::size_type last = 0;
153  do
154  {
155  auto next = str.find(separator, last);
156  auto _len = next != std::string::npos ? (next - last) : (str.size() - last);
157  if(!trim_empty || _len)
158  {
159  *first++ = boost::lexical_cast<typename _OutputIt::container_type::value_type>(curr, _len);
160  }
161  last += _len + 1;
162  curr += _len + 1;
163  } while(last < str.size());
164 }
165 
166 template <typename _Container>
167 constexpr _Container string_to_container(const std::string& str, const std::string& separator, bool trim_empty = true)
168 {
169  _Container _c;
170  string_to_container(std::inserter(_c, _c.end()), str, separator, trim_empty);
171  return _c;
172 }
173 
178 {
179  private:
181  const std::string delimiter;
182 
183  public:
187  string_separator() : delimiter(std::string())
188  {
189  }
190 
195  explicit string_separator(const std::string& _delimiter) : delimiter(_delimiter)
196  {
197  }
198 
206  bool operator()(std::string::const_iterator& next, std::string::const_iterator& end,
207  std::basic_string<char, std::char_traits<char>, std::allocator<char>>& tok)
208  {
209  if(next == end)
210  {
211  return false;
212  }
213  std::string current(next, end);
214  if(current.find(delimiter) != std::string::npos)
215  {
216  tok = current.substr(0, current.find(delimiter));
217  for(size_t counter = current.find(delimiter) + delimiter.size(); counter != 0; counter--)
218  {
219  next++;
220  }
221  return true;
222  }
223  else
224  {
225  tok = current;
226  next = end;
227  return true;
228  }
229  }
230 
234  void reset()
235  {
236  }
237 };
238 
243 template <class T>
245 {
246  private:
248 
249  public:
251  {
252  long double ld = example;
253  (void)ld;
254  }
255 };
256 
262 template <class T>
263 void ShuffleVector(typename std::vector<T>& shuffle, const unsigned int seed)
264 {
265  srand(seed);
266  size_t size = shuffle.size();
267  while(size > 1)
268  {
269  size_t k = static_cast<size_t>(rand()) % size;
270 
271  size--;
272 
273  T temp = shuffle[size];
274  shuffle[size] = shuffle[k];
275  shuffle[k] = temp;
276  }
277 }
278 
280 struct TimeStamp
281 {
282  std::string timestamp;
286  TimeStamp();
287 
292  explicit TimeStamp(const std::string& timestamp);
293 
297  static std::string GetCurrentTimeStamp();
298 
304  friend std::ostream& operator<<(std::ostream& os, const TimeStamp& timestamp);
305 
306  friend bool operator<=(const TimeStamp& timestamp1, const TimeStamp& timestamp2);
307 };
308 #endif
Functor to tokenize string used with boost::tokenizer.
Definition: utility.hpp:177
Concept checking class This class is used to check that an object can be converted into long double...
Definition: utility.hpp:244
std::string timestamp
Definition: utility.hpp:282
const std::string delimiter
The delimiter.
Definition: utility.hpp:181
string_separator()
Empty constructor.
Definition: utility.hpp:187
Definition of hash function for EdgeDescriptor.
Definition: graph.hpp:1321
string_separator(const std::string &_delimiter)
Constructor.
Definition: utility.hpp:195
Auxiliary methods for manipulating string.
bool operator()(std::string::const_iterator &next, std::string::const_iterator &end, std::basic_string< char, std::char_traits< char >, std::allocator< char >> &tok)
Tokenize operator.
Definition: utility.hpp:206
std::string convert_to_binary(G _value, unsigned long long precision)
Definition: utility.hpp:110
APInt integer_cst_t
Definition: panda_types.hpp:47
static const uint32_t k[]
Definition: sha-256.c:22
constexpr void string_to_container(_OutputIt first, const std::string &str, const std::string &separator, bool trim_empty=true)
Definition: utility.hpp:148
void reset()
Reset function (required to implement boost tokenizerFunction model.
Definition: utility.hpp:234
BOOST_CONCEPT_USAGE(check_long_double)
Definition: utility.hpp:250
bool operator<=(const DiscrepancyOpInfo &a, const DiscrepancyOpInfo &b)
char str[25]
Definition: fixedptc.c:8
std::ostream & operator<<(std::ostream &OS, const Range &R)
Definition: Range.cpp:2523
The type used for timestamp.
Definition: utility.hpp:280
std::string container_to_string(_InputIt first, _InputIt last, const std::string &separator, bool trim_empty=true)
Definition: utility.hpp:122
unsigned counter[N_THREADS]
Definition: data.c:3
void ShuffleVector(typename std::vector< T > &shuffle, const unsigned int seed)
Randomly shuffle a vector.
Definition: utility.hpp:263

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13