aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
utils_misc.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Utilities for aGrUM.
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_UTILS_H
30 #define GUM_UTILS_H
31 
32 #include <cstdlib>
33 #include <iostream>
34 #include <numeric>
35 #include <utility>
36 #include <vector>
37 #include <tuple>
38 #include <string>
39 #include <type_traits>
40 
41 
42 #ifdef GUM_DEBUG_MODE
43 # define GUM_CAST dynamic_cast
44 #else
45 # define GUM_CAST static_cast
46 #endif // GUM_DEBUG_MODE
47 
48 #include <agrum/agrum.h>
49 
50 #include <agrum/tools/core/utils_dir.h>
51 #include <agrum/tools/core/utils_random.h>
52 #include <agrum/tools/core/utils_string.h>
53 
54 namespace std {
55 
56  /// @ingroup utilities_group
57  /// @{
58 
59  /**
60  * @brief 'std::cout<<' operator for pairs.
61  * @tparam T1 The pair's first's type.
62  * @tparam T2 The pair's second's type.
63  * @param stream The stream to print to.
64  * @param val The pair to print to stream.
65  * @return Returns stream.
66  */
67  template < typename T1, typename T2 >
68  ostream& operator<<(ostream& stream, const pair< T1, T2 >& val);
69 
70  /**
71  * @brief 'std::cout<<' operator for vectors.
72  * @param stream The stream to print to.
73  * @tparam t The std::tuple
74  * @param val The std::vector to print to stream.
75  * @return Returns stream.
76  */
77  template < typename T0, typename... T >
78  std::ostream& operator<<(std::ostream& os, const std::tuple< T0, T... >& t);
79 
80 
81  /**
82  * @brief 'std::cout<<' operator for vectors.
83  * @tparam T The vector's elements type.
84  * @param stream The stream to print to.
85  * @param val The std::vector to print to stream.
86  * @return Returns stream.
87  */
88  template < typename T >
89  ostream& operator<<(ostream& stream, const vector< T >& val);
90 
91  /** *
92  * @brief check if a vector consists in unique values (no duplicate).
93  * @tparam T The vector's elements type.
94  * @param x the vector
95  * @return Returns true if the vector has no duplicate.
96  */
97  template < typename T >
98  bool hasUniqueElts(std::vector< T > const& x);
99 
100  /// @}
101 
102 } /* namespace std */
103 
104 namespace gum {
105 
106  /// @ingroup utilities_group
107  /// @{
108 
109  /**
110  * @brief Forbidden_type<T1,T2> return the "int" type if T1 and T2 are of the
111  * same type, else nothing.
112  *
113  * Use it as a guard in template specification :
114  * @code
115  * // Creates a template except if T is int or char
116  * template<T,forbidden_type<T,int> =0,forbidden_type<T,char> =0> // ...
117  * @endcode
118  *
119  * @param T1 The type to test for.
120  * @param T2 The expected type.
121  */
122  template < typename T1, typename T2 >
123  using forbidden_type = typename std::enable_if< !std::is_same< T1, T2 >::value, int >::type;
124 
125  /**
126  * @brief Implements a stream with the same behaviour as /dev/null.
127  */
128  struct NullStream: std::ostream {
129  NullStream() : std::ios(0), std::ostream(0) {}
130  };
131 
132  /**
133  * @brief Cross-platform replacement for memcmp.
134  * @param in_ A pointer to the block of memory to copy.
135  * @param out_ A pointer to the block of memory receiving copy.
136  * @param size Number of bytes to copy.
137  * @return Returns true if OK.
138  */
139  bool Memcmp(const void* const in_, const void* const out_, unsigned long size);
140 
141  /**
142  * @brief Used for debug purpose.
143  */
144  void _atexit_();
145 
146  /**
147  * @brief Indicate whether two elements are (almost) different or not.
148  * @tparam T The type of the elements to compare.
149  */
150  template < typename T >
151  struct AlmostDifferent {
152  bool operator()(const T& t1, const T& t2) {
153  if (t1 == t2)
154  return false;
155  else if (t1 == 0)
156  return (std::abs(t2) > 1e-5);
157  else
158  return (std::abs(t2 - t1) / t1 > 1e-5);
159  }
160  };
161 
162  /**
163  * @brief Indicate whether two elements are (almost) different or not.
164  * @tparam T The type of the elements to compare.
165  */
166  template < typename T >
167  struct AlmostDifferent< T* > {
168  bool operator()(const T* t1, const T* t2) { return (t1 != t2); }
169  };
170 
171  /// @}
172 
173 } /* namespace gum */
174 
175 // Always include the template implementations
176 #include <agrum/tools/core/utils_misc_tpl.h>
177 
178 #endif /* GUM_UTILS_H */