aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
utils_misc_tpl.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 Template implementation for utilities for aGrUM.
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 // to help IDE parser
30 #include <agrum/tools/core/utils_misc.h>
31 #include <algorithm>
32 #include <functional>
33 #include <iostream>
34 #include <string>
35 #include <tuple>
36 #include <type_traits>
37 
38 namespace std {
39  template < typename T >
40  ostream& operator<<(ostream& stream, const vector< T >& val) {
41  bool deja = false;
42  stream << "[";
43 
44  for (const auto& v: val) {
45  if (deja)
46  stream << " , ";
47  else
48  deja = true;
49  stream << v;
50  }
51 
52  stream << "]";
53 
54  return stream;
55  }
56 
57  template < typename T1, typename T2 >
58  ostream& operator<<(ostream& stream, const pair< T1, T2 >& val) {
59  stream << "(" << val.first << "," << val.second << ")";
60  return stream;
61  }
62 
63  template < size_t N >
64  struct _auxiliary_print_tuple_ {
65  template < typename... T >
66  static typename std::enable_if< (N < sizeof...(T)) >::type print(std::ostream& os,
67  const std::tuple< T... >& t) {
68  char quote
69  = (std::is_convertible< decltype(std::get< N >(t)), std::string >::value) ? '"' : 0;
70  os << ", " << quote << std::get< N >(t) << quote;
71  _auxiliary_print_tuple_< N + 1 >::print(os, t);
72  }
73  template < typename... T >
74  static typename std::enable_if< !(N < sizeof...(T)) >::type print(std::ostream&,
75  const std::tuple< T... >&) {}
76  };
77 
78  template < typename T0, typename... T >
79  std::ostream& operator<<(std::ostream& os, const std::tuple< T0, T... >& t) {
80  char quote = (std::is_convertible< T0, std::string >::value) ? '"' : 0;
81  os << '(' << quote << std::get< 0 >(t) << quote;
82  _auxiliary_print_tuple_< 1 >::print(os, t);
83  return os << ')';
84  }
85 
86  template < class T >
87  bool hasUniqueElts(std::vector< T > const& x) {
88  if (x.size() <= 1) return true;
89  if (x.size() == 2) return x[0] != x[1];
90 
91  auto refless = [](T const* l, T const* r) {
92  return *l < *r;
93  };
94  auto refeq = [](T const* l, T const* r) {
95  return *l == *r;
96  };
97 
98  std::vector< T const* > vp;
99  vp.reserve(x.size());
100  for (size_t i = 0; i < x.size(); ++i)
101  vp.push_back(&x[i]);
102  sort(vp.begin(), vp.end(), refless); // O(N log N)
103  // if no adjacent pair (vp_n,vp_n+1) has *vp_n == *vp_n+1
104  return std::adjacent_find(vp.begin(), vp.end(), refeq) == vp.end();
105  }
106 } /* namespace std */