File: comparators_basic_defs.h

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (367 lines) | stat: -rw-r--r-- 11,184 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* Copyright (c) 1997-2024
   Ewgenij Gawrilow, Michael Joswig, and the polymake team
   Technische Universität Berlin, Germany
   https://polymake.org

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
*/

#pragma once

#include "polymake/internal/operations_basic_defs.h"

#include <limits>
#include <cstdlib>
#include <cmath>
#include <string>
#include <functional>

namespace pm {

enum cmp_value { cmp_lt=-1, cmp_eq=0, cmp_gt=1, cmp_ne=cmp_gt };

template <typename T>
constexpr Int sign_impl(T x, std::true_type)
{
   return x < 0 ? -1 : x > 0;
}

template <typename T>
constexpr Int sign_impl(T x, std::false_type)
{
   return x != 0;
}

template <typename T>
constexpr
std::enable_if_t<std::is_arithmetic<T>::value, Int>
sign(T x)
{
   return sign_impl(x, bool_constant<std::numeric_limits<T>::is_signed>());
}

template <typename TPrimitive, typename T>
constexpr
std::enable_if_t<std::is_arithmetic<T>::value, TPrimitive>
max_value_as(mlist<T>)
{
   return static_cast<TPrimitive>(std::numeric_limits<T>::max());
}

template <typename TPrimitive, typename T>
constexpr
std::enable_if_t<std::is_arithmetic<T>::value, TPrimitive>
min_value_as(mlist<T>)
{
   return static_cast<TPrimitive>(std::numeric_limits<T>::min());
}

template <typename T, bool is_max> struct extremal {};
template <typename T> struct maximal : extremal<T, true> {};
template <typename T> struct minimal : extremal<T, false> {};

namespace operations {

template <typename T1, typename T2>
struct cmp_basic {
   typedef T1 first_argument_type;
   typedef T2 second_argument_type;
   typedef cmp_value result_type;

   template <typename Left, typename Right>
   cmp_value operator() (const Left& a, const Right& b) const
   {
      return a<b ? cmp_lt : cmp_value(a>b);
   }
};

struct cmp_extremal {
   template <typename T, bool _is_max_l, bool _is_max_r>
   cmp_value operator() (const extremal<T,_is_max_l>&, const extremal<T,_is_max_r>&) const
   {
      return _is_max_l == _is_max_r ? cmp_eq : _is_max_l ? cmp_gt : cmp_lt;
   }

   template <typename T, bool _is_max_l>
   cmp_value operator() (const extremal<T,_is_max_l>&, const T&) const
   {
      return _is_max_l ? cmp_gt : cmp_lt;
   }

   template <typename T, bool _is_max_r>
   cmp_value operator() (const T&, const extremal<T,_is_max_r>&) const
   {
      return _is_max_r ? cmp_lt : cmp_gt;
   }
};

template <typename T, bool use_zero_test = has_zero_value<T>::value>
struct cmp_partial_opaque {
   template <typename Left, typename Iterator2>
   cmp_value operator() (partial_left, const Left&, const Iterator2&) const
   {
      return cmp_gt;
   }

   template <typename Iterator1, typename Right>
   cmp_value operator() (partial_right, const Iterator1&, const Right&) const
   {
      return cmp_lt;
   }
};

template <typename T>
struct cmp_partial_opaque<T, true> {
   template <typename Left, typename Iterator>
   cmp_value operator() (partial_left, const Left& x, const Iterator&) const
   {
      return is_zero(x) ? cmp_eq : cmp_gt;
   }

   template <typename Iterator, typename Right>
   cmp_value operator() (partial_right, const Iterator&, const Right& x) const
   {
      return is_zero(x) ? cmp_eq : cmp_lt;
   }
};

struct cmp_partial_scalar {
   template <typename Left, typename Iterator>
   cmp_value operator() (partial_left, const Left& a, const Iterator&) const
   {
      return cmp_value(sign(a));
   }
   template <typename Iterator, typename Right>
   cmp_value operator() (partial_right, const Iterator&, const Right& b) const
   {
      return cmp_value(-sign(b));
   }
};

template <typename T1, typename T2=T1, typename enabled=void>
struct cmp_scalar { };

template <typename T1, typename T2>
struct cmp_scalar<T1, T2, typename std::enable_if<std::numeric_limits<T1>::is_signed && std::numeric_limits<T2>::is_signed &&
                                                  are_less_greater_comparable<T1, T2>::value>::type>
   : cmp_extremal
   , cmp_partial_scalar {

   typedef T1 first_argument_type;
   typedef T2 second_argument_type;
   typedef cmp_value result_type;

   using cmp_extremal::operator();
   using cmp_partial_scalar::operator();

   template <typename Left, typename Right>
   typename std::enable_if<(std::numeric_limits<Left>::is_integer && std::numeric_limits<Left>::is_signed &&
                            std::numeric_limits<Right>::is_integer && std::numeric_limits<Right>::is_signed), cmp_value>::type
   operator() (const Left& a, const Right& b) const
   {
      return cmp_value(sign(a-b));
   }

   template <typename Left, typename Right>
   typename std::enable_if<!(std::numeric_limits<Left>::is_integer && std::numeric_limits<Left>::is_signed &&
                             std::numeric_limits<Right>::is_integer && std::numeric_limits<Right>::is_signed), cmp_value>::type
   operator() (const Left& a, const Right& b) const
   {
      return cmp_basic<Left, Right>()(a, b);
   }
};

template <typename T1, typename T2>
struct cmp_scalar<T1, T2, typename std::enable_if<!(std::numeric_limits<T1>::is_signed && std::numeric_limits<T2>::is_signed) &&
                                                  are_less_greater_comparable<T1, T2>::value>::type>
   : cmp_extremal
   , cmp_basic<T1, T2> {

   using cmp_extremal::operator();
   using cmp_basic<T1, T2>::operator();

   template <typename Left, typename Iterator>
   cmp_value operator() (partial_left, const Left& a, const Iterator&) const
   {
      return is_zero(a) ? cmp_eq : cmp_gt;
   }
   template <typename Iterator, typename Right>
   cmp_value operator() (partial_right, const Iterator&, const Right& b) const
   {
      return is_zero(b) ? cmp_eq : cmp_lt;
   }
};

template <typename T1, typename T2=T1, typename enabled=void>
struct cmp_unordered_impl { };

template <typename T1, typename T2>
struct cmp_unordered_impl<T1, T2, typename std::enable_if<are_comparable<T1, T2>::value>::type> {
   typedef T1 first_argument_type;
   typedef T2 second_argument_type;
   typedef cmp_value result_type;

   static const bool partially_defined = has_zero_value<T1>::value && has_zero_value<T2>::value;

   template <typename Left, typename Right>
   cmp_value operator()(const Left& l, const Right& r) const
   {
      return l==r ? cmp_eq : cmp_ne;
   }

   template <typename Left, typename Iterator>
   std::enable_if_t<has_zero_value<Left>::value, cmp_value>
   operator() (partial_left, const Left& a, const Iterator&) const
   {
      return is_zero(a) ? cmp_eq : cmp_ne;
   }

   template <typename Iterator, typename Right>
   std::enable_if_t<has_zero_value<Right>::value, cmp_value>
   operator() (partial_right, const Iterator&, const Right& b) const
   {
      return is_zero(b) ? cmp_eq : cmp_ne;
   }
};

template <typename T, typename enabled=void>
struct cmp_opaque { };

template <typename T>
struct cmp_opaque<T, typename std::enable_if<is_less_greater_comparable<T>::value>::type>
   : cmp_extremal
   , cmp_basic<T, T>
   , cmp_partial_opaque<T> {
   using cmp_extremal::operator();
   using cmp_basic<T, T>::operator();
   using cmp_partial_opaque<T>::operator();
};

template <typename Char, typename Traits, typename Alloc>
struct cmp_partial_opaque<std::basic_string<Char, Traits, Alloc>, false> {

   template <typename Left, typename Iterator>
   cmp_value operator() (partial_left, const Left& a, const Iterator&) const
   {
      return a.empty() ? cmp_eq : cmp_gt;
   }
   template <typename Iterator, typename Right>
   cmp_value operator() (partial_right, const Iterator&, const Right& b) const
   {
      return b.empty() ? cmp_eq : cmp_lt;
   }
};

template <typename T1, typename T2=T1>
struct cmp_scalar_with_leeway : cmp_extremal {

   typedef T1 first_argument_type;
   typedef T2 second_argument_type;
   typedef cmp_value result_type;

   using cmp_extremal::operator();

   template <typename Left, typename Right>
   cmp_value operator()(const Left& a, const Right& b) const
   {
      return !is_zero(a-b) ? cmp_scalar<Left,Right>()(a, b) : cmp_eq;
   }

   template <typename Left, typename Iterator2>
   cmp_value operator()(partial_left, const Left& a, const Iterator2& b) const
   {
      return !is_zero(a) ? cmp_partial_scalar()(partial_left(), a, b) : cmp_eq;
   }

   template <typename Right, typename Iterator1>
   cmp_value operator()(partial_right, const Iterator1& a, const Right& b) const
   {
      return !is_zero(b) ? cmp_partial_scalar()(partial_right(), a, b) : cmp_eq;
   }
};

template <typename T>
struct cmp_pointer {
   typedef T* first_argument_type;
   typedef T* second_argument_type;
   typedef cmp_value result_type;

   cmp_value operator()(T* a, T* b) const
   {
      return cmp_scalar<long, long>()(long(a), long(b));
   }
};

} // end namespace operations

template <typename T1, typename T2>
T1& assign_max(T1& max, const T2& x) { if (max<x) max=x; return max; }

template <typename T1, typename T2>
T1& assign_min(T1& min, const T2& x) { if (min>x) min=x; return min; }

template <typename T1, typename T2, typename T3>
void assign_min_max(T1& min, T2& max, const T3& x)
{
   if (min>x) min=x; else if (max<x) max=x;
}

template <typename T, typename Tag=typename object_traits<T>::generic_tag>
struct hash_func;

template <typename T>
struct hash_func<T, is_scalar> : public std::hash<T> {};

template <typename Char, typename Traits, typename Alloc>
struct hash_func<std::basic_string<Char, Traits, Alloc>, is_opaque> : public std::hash< std::basic_string<Char, Traits, Alloc> > {};

template <typename T>
struct hash_func<T*, is_not_object> {
   size_t operator() (T* ptr) const { return size_t(ptr); }
};

using std::abs;
using std::isfinite;

/// return the sign of the inifinite value, or 0 if the value is finite
/// std::isinf returns bool nowadays which is insufficient for efficient operations
inline
Int isinf(double x) noexcept
{
   return std::isinf(x) ? (x>0)*2-1 : 0;
}

constexpr Int isinf(long) { return 0; }

template <typename T, bool is_max>
struct spec_object_traits< extremal<T, is_max> > : spec_object_traits<T> {};

template <typename T>
struct spec_object_traits< maximal<T> > : spec_object_traits<T> {};

template <typename T>
struct spec_object_traits< minimal<T> > : spec_object_traits<T> {};

template <typename T1, typename T2, typename Comparator, typename=cmp_value>
struct are_comparable_via : std::false_type {};

template <typename T1, typename T2, typename Comparator>
struct are_comparable_via<T1, T2, Comparator, decltype(std::declval<Comparator>()(std::declval<const T1&>(), std::declval<const T2&>()))> : std::true_type {};

} // end namespace pm


// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: