Lumiera  0.pre.03
»edit your freedom«
util.hpp
Go to the documentation of this file.
1 /*
2  UTIL.hpp - collection of small helper functions used "everywhere"
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
37 #ifndef LIB_UTIL_H
38 #define LIB_UTIL_H
39 
40 #include "include/limits.hpp"
41 #include "lib/hash-standard.hpp"
42 
43 #include <string>
44 
45 namespace std {// forward declarations to avoid pervasive includes
46 
47  template<typename T>
48  class allocator;
49  template<typename K, typename CMP, class ALLO>
50  class set;
51 
52  template<typename IT, typename V>
53  IT find (IT, IT, V const&);
54  template<typename IT, typename V>
55  IT remove (IT, IT, V const&);
56 }
57 
58 
59 const char* cStr (std::string const&);
60 
61 
62 
63 namespace util {
64 
65  using std::string;
66  using CStr = const char*;
67 
68 
69  template <class NUM>
70  inline int constexpr
71  sgn (NUM n)
72  {
73  return (n==0)? 0 :((n<0)? -1:+1 );
74  }
75 
76  template <class N1, class N2>
77  inline N1 constexpr
78  min (N1 n1, N2 n2)
79  {
80  return n2 < n1? N1(n2) : n1;
81  }
82 
83  template <class N1, class N2>
84  inline N1 constexpr
85  max (N1 n1, N2 n2)
86  {
87  return n1 < n2? N1(n2) : n1;
88  }
89 
91  template <typename NUM>
92  inline NUM constexpr
93  noneg (NUM val)
94  {
95  return (0<val? val : 0);
96  }
97 
99  template <typename NUM, typename NB>
100  inline NUM constexpr
101  limited (NB lowerBound, NUM val, NB upperBound)
102  {
103  return min ( max (val, lowerBound)
104  , upperBound);
105  }
106 
107  template <typename NUM, typename NB>
108  inline bool constexpr
109  isLimited (NB lowerBound, NUM val, NB upperBound)
110  {
111  return lowerBound <= val
112  and val <= upperBound;
113  }
114 
118  inline uint
119  uNum (CStr charPtr)
120  {
121  if (!charPtr) return 0;
122  int parsedNumber (std::atoi (charPtr));
123  return limited (0, parsedNumber, LUMIERA_MAX_ORDINAL_NUMBER);
124  }
125 
126  inline int
127  sNum (CStr charPtr)
128  {
129  if (!charPtr) return 0;
130  int parsedNumber (std::atoi (charPtr));
131  return limited (-LUMIERA_MAX_ORDINAL_NUMBER, parsedNumber, LUMIERA_MAX_ORDINAL_NUMBER);
132  }
133 
134  template<class OBJ>
135  inline uint
136  uNum (OBJ const& spec)
137  {
138  return uNum (cStr(spec));
139  }
140 
141  template<class OBJ>
142  inline int
143  sNum (OBJ const& spec)
144  {
145  return sNum (cStr(spec));
146  }
147 
148 
149 
150  /* ======== generic empty check ========= */
151 
154  template <class CONT>
155  inline bool
156  isnil (const CONT& container)
157  {
158  return container.empty();
159  }
160 
161  template <class CONT>
162  inline bool
163  isnil (const CONT* pContainer)
164  {
165  return !pContainer or pContainer->empty();
166  }
167 
168  template <class CONT>
169  inline bool
170  isnil (CONT* pContainer)
171  {
172  return !pContainer or pContainer->empty();
173  }
174 
175  inline bool
176  isnil (CStr charPtr)
177  {
178  return !charPtr or !(*charPtr);
179  }
180 
181 
182 
184  inline bool
185  startsWith (string const& str, string const& prefix)
186  {
187  return 0 == str.rfind (prefix, 0);
188  }
189 
190  inline bool
191  startsWith (string const& str, CStr prefix)
192  {
193  return 0 == str.rfind (prefix, 0);
194  }
195 
197  inline bool
198  endsWith (string const& str, string const& suffix)
199  {
200  size_t l = suffix.length();
201  if (l > str.length()) return false;
202  size_t pos = str.length() - l;
203  return pos == str.find (suffix, pos);
204  }
205 
206  inline bool
207  endsWith (string const& str, CStr suffix)
208  {
209  return endsWith (str, string(suffix));
210  }
211 
212  inline void
213  removePrefix (string& str, string const& prefix)
214  {
215  if (not startsWith (str,prefix)) return;
216  str = str.substr (prefix.length());
217  }
218 
219  inline void
220  removeSuffix (string& str, string const& suffix)
221  {
222  if (not endsWith (str,suffix)) return;
223  str.resize(str.length() - suffix.length());
224  }
225 
226 
228  template <typename MAP>
229  inline bool
230  contains (MAP& map, typename MAP::key_type const& key)
231  {
232  return map.find(key) != map.end();
233  }
234 
236  template <typename T, class CMP, class ALO>
237  inline bool
238  contains (std::set<T,CMP,ALO> const& set, T const& val)
239  {
240  return set.end() != set.find (val);
241  }
242 
244  template <typename T>
245  inline bool
246  contains (std::string const& str, const T& val)
247  {
248  return str.find (val) != std::string::npos;
249  }
250 
253  template <typename SEQ>
254  inline bool
255  contains (SEQ const& cont, typename SEQ::const_reference val)
256  {
257  typename SEQ::const_iterator begin = cont.begin();
258  typename SEQ::const_iterator end = cont.end();
259 
260  return end != std::find(begin,end, val);
261  }
262 
264  template <class IT>
265  inline bool
266  linearSearch (IT iter, typename IT::value_type const& val)
267  {
268  IT end{};
269  return end != std::find (std::move (iter), end, val);
270  }
271 
273  template <typename MAP>
274  inline typename MAP::mapped_type
275  getValue_or_default (MAP& map, typename MAP::key_type const& key
276  , typename MAP::mapped_type defaultVal)
277  {
278  typename MAP::const_iterator pos = map.find (key);
279  if (pos != map.end())
280  return pos->second;
281  else
282  return defaultVal;
283  }
284 
290  template <typename MAP>
291  inline typename MAP::mapped_type const &
292  access_or_default (MAP& map, typename MAP::key_type const& key
293  , typename MAP::mapped_type const& refDefault)
294  {
295  typename MAP::const_iterator pos = map.find (key);
296  if (pos != map.end())
297  return pos->second;
298  else
299  return refDefault;
300  }
301 
304  template <typename SEQ>
305  inline typename SEQ::iterator
306  removeall (SEQ& coll, typename SEQ::value_type const& val)
307  {
308  typename SEQ::iterator collEnd = coll.end();
309  return coll.erase (std::remove (coll.begin(), collEnd, val),
310  collEnd
311  );
312  }
313 
314 
318  template<class SET, typename FUN>
319  bool remove_if (SET& set, FUN test)
320  {
321  typedef typename SET::iterator Itor;
322  bool found = false;
323  Itor end = set.end();
324  Itor begin = set.begin();
325  Itor pos = begin;
326  while (pos!=end)
327  {
328  if (not test(*pos))
329  ++pos;
330  else
331  {
332  found = true;
333  if (pos==begin)
334  {
335  set.erase(pos);
336  pos = begin = set.begin();
337  }
338  else
339  {
340  set.erase(pos--);
341  ++pos;
342  }
343  end = set.end();
344  } }
345  return found;
346  }
347 
348 
352  template<class OBJ>
353  inline OBJ*
354  unConst (const OBJ* o)
355  {
356  return const_cast<OBJ*> (o);
357  }
358 
359  template<class OBJ>
360  inline OBJ&
361  unConst (OBJ const& ro)
362  {
363  return const_cast<OBJ&> (ro);
364  }
365 
366 
370  template<class A, class B>
371  inline bool
372  isSameObject (A const& a, B const& b)
373  {
374  return static_cast<const void*> (std::addressof(a))
375  == static_cast<const void*> (std::addressof(b));
376  }
377 
379  template<class X>
380  inline const void*
381  getAddr (X& x)
382  {
383  return static_cast<const void*> (std::addressof(x));
384  }
385  template<class X>
386  inline const void*
387  getAddr (X* x)
388  {
389  return static_cast<const void*> (x);
390  }
391 
393  template<typename X>
394  inline size_t
395  slotNr (X const& x)
396  {
397  return reinterpret_cast<size_t> (std::addressof(x)) / sizeof(size_t);
398  }
399  template<typename X>
400  inline size_t
401  slotNr (X const* x)
402  {
403  return reinterpret_cast<size_t> (x) / sizeof(size_t);;
404  }
405 
406 
411  template<typename A, typename B>
412  inline bool
413  isCloseBy (A&& a, B&& b, size_t consideredNearby =50)
414  {
415  size_t loc1 = slotNr (std::forward<A> (a));
416  size_t loc2 = slotNr (std::forward<B> (b));
417  size_t dist = loc2 > loc1? loc2-loc1:loc1-loc2;
418  return dist < consideredNearby;
419  }
420 
421 
422 
423 
443  string sanitise (string const& org);
444 
445 
449  string trim (string const& org);
450 
451 
459  bool boolVal (string const&);
460 
461 
466  bool isYes (string const&) noexcept;
467 
472  bool isNo (string const&) noexcept;
473 
474 } // namespace util
475 
476 
477 
478  /* === some common macro definitions === */
479 
483 #define SIDEEFFECT __attribute__ ((unused));
484 
486 #define STRINGIFY(TOKEN) __STRNGFY(TOKEN)
487 #define __STRNGFY(TOKEN) #TOKEN
488 
492 #define INSTANCEOF(CLASS, EXPR) (dynamic_cast<const CLASS*> (EXPR))
493 
495 #ifndef MAX // this is present to make this definition play nice with GLib
496 #define MAX(A,B) ((A < B) ? (B) : (A))
497 #endif
498 
499 #endif /*UTIL_HPP_*/
const char * cStr(std::string const &)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:68
bool isYes(string const &textForm) noexcept
check the given text if it can be interpreted as affirmative answer (bool true).
Definition: util.cpp:116
const void * getAddr(X &x)
extract address but strip any type info
Definition: util.hpp:381
bool startsWith(string const &str, string const &prefix)
check if string starts with a given prefix
Definition: util.hpp:185
Definition: run.hpp:49
STL namespace.
bool isCloseBy(A &&a, B &&b, size_t consideredNearby=50)
determine heuristically if two objects are located „close to each other“ in memory.
Definition: util.hpp:413
std::string sanitise(std::string const &)
produce an identifier based on the given string.
Definition: util.cpp:65
hard wired safety limits.
bool boolVal(string const &textForm)
interpret text representation of a boolean value.
Definition: util.cpp:107
NUM constexpr noneg(NUM val)
cut a numeric value to be >=0
Definition: util.hpp:93
bool linearSearch(IT iter, typename IT::value_type const &val)
use (and exhaust) a »Lumiera Forward Iterator« for linear search
Definition: util.hpp:266
size_t slotNr(X const &x)
the addressable memory »slot« — platform dependent
Definition: util.hpp:395
bool contains(MAP &map, typename MAP::key_type const &key)
shortcut for containment test on a map
Definition: util.hpp:230
uint uNum(CStr charPtr)
positive integral number from textual representation
Definition: util.hpp:119
OBJ * unConst(const OBJ *o)
shortcut to save some typing when having to define const and non-const variants of member functions ...
Definition: util.hpp:354
bool isNo(string const &textForm) noexcept
check if the given text is empty or can be interpreted as rejection (bool false)- ...
Definition: util.cpp:123
MAP::mapped_type const & access_or_default(MAP &map, typename MAP::key_type const &key, typename MAP::mapped_type const &refDefault)
expose a reference to a map entry, with fall-back to some default object
Definition: util.hpp:292
Helper to use a single extension point for specialised hash functions.
bool remove_if(SET &set, FUN test)
remove all elements fulfilling a given predicate from a (sorted) set.
Definition: util.hpp:319
string trim(string const &org)
remove leading and trailing whitespace
Definition: util.cpp:93
MAP::mapped_type getValue_or_default(MAP &map, typename MAP::key_type const &key, typename MAP::mapped_type defaultVal)
fetch value from a Map, or return a default if not found
Definition: util.hpp:275
NUM constexpr limited(NB lowerBound, NUM val, NB upperBound)
force a numeric to be within bounds, inclusively
Definition: util.hpp:101
bool endsWith(string const &str, string const &suffix)
check if string ends with the given suffix
Definition: util.hpp:198
SEQ::iterator removeall(SEQ &coll, typename SEQ::value_type const &val)
shortcut for removing all copies of an Element in any sequential collection
Definition: util.hpp:306
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:372