aGrUM  0.14.2
utils_misc_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 // to help IDE parser
28 #include <agrum/core/utils_misc.h>
29 #include <algorithm>
30 #include <functional>
31 #include <iostream>
32 #include <string>
33 #include <tuple>
34 #include <type_traits>
35 
36 namespace std {
37  template < typename T >
38  ostream& operator<<(ostream& stream, const vector< T >& val) {
39  bool deja = false;
40  stream << "[";
41 
42  for (const auto& v : val) {
43  if (deja)
44  stream << " , ";
45  else
46  deja = true;
47  stream << v;
48  }
49 
50  stream << "]";
51 
52  return stream;
53  }
54 
55  template < typename T1, typename T2 >
56  ostream& operator<<(ostream& stream, const pair< T1, T2 >& val) {
57  stream << "(" << val.first << "," << val.second << ")";
58  return stream;
59  }
60 
61  template < size_t N >
63  template < typename... T >
64  static typename std::enable_if< (N < sizeof...(T)) >::type
65  print(std::ostream& os, const std::tuple< T... >& t) {
66  char quote =
67  (std::is_convertible< decltype(std::get< N >(t)), std::string >::value)
68  ? '"'
69  : 0;
70  os << ", " << quote << std::get< N >(t) << quote;
72  }
73  template < typename... T >
74  static typename std::enable_if< !(N < sizeof...(T)) >::type
75  print(std::ostream&, 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;
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) { return *l < *r; };
92  auto refeq = [](T const* l, T const* r) { return *l == *r; };
93 
94  std::vector< T const* > vp;
95  vp.reserve(x.size());
96  for (size_t i = 0; i < x.size(); ++i)
97  vp.push_back(&x[i]);
98  sort(vp.begin(), vp.end(), refless); // O(N log N)
99  // if no adjacent pair (vp_n,vp_n+1) has *vp_n == *vp_n+1
100  return std::adjacent_find(vp.begin(), vp.end(), refeq) == vp.end();
101  }
102 } /* namespace std */
bool hasUniqueElts(std::vector< T > const &x)
Utilities for aGrUM.
STL namespace.
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:583
static std::enable_if< !(N< sizeof...(T)) >::type print(std::ostream &, const std::tuple< T... > &)
static std::enable_if<(N< sizeof...(T)) >::type print(std::ostream &os, const std::tuple< T... > &t)