Interface Documentation
Version: invalid
simple_id.hh
Go to the documentation of this file.
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
18 // system headers
19 #include <iostream>
20 #include <tuple>
21 
22 namespace flecsi {
23 namespace util {
24 
25 namespace detail {
26 
30 template<typename... Ts>
31 class are_integral : public std::integral_constant<bool, true>
32 {};
33 
34 template<typename T, typename... Ts>
35 class are_integral<T, Ts...>
36  : public std::integral_constant<bool,
37  (std::is_integral<T>::value && are_integral<Ts...>::value)>
38 {};
39 
41 template<typename... Ts>
42 constexpr bool are_integral_v = are_integral<Ts...>::value;
43 
52 
53 // This function terminates the chain
54 template<std::size_t I = 0,
55  typename TupleA,
56  typename TupleB,
57  std::enable_if_t<std::tuple_size<TupleA>::value ==
58  std::tuple_size<TupleB>::value &&
59  I == std::tuple_size<TupleA>::value - 1> * = nullptr>
60 bool
61 less_than(const TupleA & a, const TupleB & b) {
62  return (std::get<I>(a) < std::get<I>(b));
63 }
64 
65 // this function is the main one
66 template<std::size_t I = 0,
67  typename TupleA,
68  typename TupleB,
69  typename = std::enable_if_t<std::tuple_size<TupleA>::value ==
70  std::tuple_size<TupleB>::value &&
71  (I < std::tuple_size<TupleA>::value - 1) &&
72  std::tuple_size<TupleA>::value >= 2>>
73 bool
74 less_than(const TupleA & a, const TupleB & b) {
75  if(std::get<I>(a) == std::get<I>(b))
76  return less_than<I + 1>(a, b);
77  else
78  return (std::get<I>(a) < std::get<I>(b));
79 }
80 
88 
89 // this one terminates the chain
90 template<std::size_t I = 0,
91  typename TupleA,
92  typename TupleB,
93  std::enable_if_t<std::tuple_size<TupleA>::value ==
94  std::tuple_size<TupleB>::value &&
95  I == std::tuple_size<TupleA>::value - 1> * = nullptr>
96 bool
97 equal_to(const TupleA & a, const TupleB & b) {
98  return (std::get<I>(a) == std::get<I>(b));
99 }
100 
101 // this is the main routine
102 template<std::size_t I = 0,
103  typename TupleA,
104  typename TupleB,
105  typename = std::enable_if_t<std::tuple_size<TupleA>::value ==
106  std::tuple_size<TupleB>::value &&
107  (I < std::tuple_size<TupleA>::value - 1) &&
108  std::tuple_size<TupleA>::value >= 2>>
109 bool
110 equal_to(const TupleA & a, const TupleB & b) {
111  if(std::get<I>(a) != std::get<I>(b))
112  return false;
113  else
114  return equal_to<I + 1>(a, b);
115 }
116 
124 
125 // this routine terminates the chain
126 template<std::size_t I = 0,
127  typename TupleA,
128  std::enable_if_t<I == std::tuple_size<TupleA>::value - 1> * = nullptr>
129 std::ostream &
130 print(std::ostream & output, const TupleA & a, const char *) {
131  output << std::get<I>(a);
132  return output;
133 }
134 
135 // the main calling routine
136 template<std::size_t I = 0,
137  typename TupleA,
138  typename = std::enable_if_t<(I < std::tuple_size<TupleA>::value - 1) &&
139  std::tuple_size<TupleA>::value >= 2>>
140 std::ostream &
141 print(std::ostream & output, const TupleA & a, const char * sep) {
142  output << std::get<I>(a) << sep;
143  return print<I + 1>(output, a, sep);
144 }
145 
146 } // namespace detail
147 
153 
154 template<typename T>
156 
159 template<typename... Args>
160 struct lexical_comparison<std::tuple<Args...>> {
161  using value_type = typename std::tuple<Args...>;
162  bool operator()(const value_type & a, const value_type & b) const {
163  return detail::less_than(a, b);
164  }
165 };
166 
176 
177 template<typename T, typename Compare>
179 {};
180 
181 template<template<typename> class Compare, typename... Args>
182 class simple_id_t<std::tuple<Args...>, Compare<std::tuple<Args...>>>
183 {
184 
185  //===========================================================================
186  // Private data members
187  //===========================================================================
188 
190  using value_type = std::tuple<Args...>;
191 
193  value_type data_;
194 
196  static constexpr auto length_ = sizeof...(Args);
197 
198 public:
199  //===========================================================================
200  // Public constructors
201  //===========================================================================
202 
204  simple_id_t() = default;
205 
207  simple_id_t(const simple_id_t &) = default;
208 
211  simple_id_t(const value_type & data) : data_{data} {}
212 
217  template<typename... Ts,
218  typename = std::enable_if_t<sizeof...(Ts) == sizeof...(Args)>>
219  simple_id_t(Ts &&... ts) : data_{std::make_tuple(std::forward<Ts>(ts)...)} {}
220 
222  static constexpr auto size() noexcept {
223  return length_;
224  }
225 
230  friend std::ostream & operator<<(std::ostream & output,
231  const simple_id_t & id) {
232  detail::print(output, id.data_, ", ");
233  return output;
234  }
235 
239  bool operator==(const simple_id_t & id) const {
240  return detail::equal_to(data_, id.data_);
241  }
242 
246  bool operator<(const simple_id_t & id) const {
247  return Compare<value_type>{}(data_, id.data_);
248  }
249 
250 }; // simple_id
251 
252 } // namespace util
253 } // namespace flecsi
friend std::ostream & operator<<(std::ostream &output, const simple_id_t &id)
Definition: simple_id.hh:230
std::ostream & print(std::ostream &output, const TupleA &a, const char *)
Print function for a tuple.
Definition: simple_id.hh:130
bool operator<(const simple_id_t &id) const
Definition: simple_id.hh:246
A simple id type that can be constructed from multiple indexes.
Definition: simple_id.hh:178
A helper to identify if all types Ts are integral.
Definition: simple_id.hh:31
static constexpr auto size() noexcept
Return the size of the tuple.
Definition: simple_id.hh:222
bool operator==(const simple_id_t &id) const
Definition: simple_id.hh:239
constexpr bool are_integral_v
Equal to true if Ts are all integral types.
Definition: simple_id.hh:42
bool less_than(const TupleA &a, const TupleB &b)
Comparison function for two tuples Comparison is done in a lexical fashion.
Definition: simple_id.hh:61
Definition: simple_id.hh:155
bool equal_to(const TupleA &a, const TupleB &b)
Comparison function for two tuples.
Definition: simple_id.hh:97
Definition: control.hh:31