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