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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
/* 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/type_manip.h"
#include <cmath>
#include <string>
#include <functional>
namespace pm {
template <typename T>
std::enable_if_t<std::is_arithmetic<T>::value, T&>
negate(T& x)
{
x=-x;
return x;
}
template <typename T>
std::enable_if_t<is_class_or_union<pure_type_t<T>>::value &&
!std::is_const<std::remove_reference_t<T>>::value, T&&>
negate(T&& x)
{
return std::forward<T>(x.negate());
}
template <typename T>
std::enable_if_t<std::is_integral<T>::value, T&>
complement(T& x)
{
x=~x;
return x;
}
template <typename T, typename=std::enable_if_t<std::is_same<typename object_traits<T>::generic_tag, is_scalar>::value &&
std::is_same<decltype(std::declval<const T&>() * std::declval<const T&>()), T>::value>>
T sqr(const T& x)
{
return x*x;
}
namespace operations {
struct partial {};
struct partial_left : partial {};
struct partial_right : partial {};
#define GuessResultType(name, sign) \
template <typename T1, typename T2> \
struct name##_result { \
using type = decltype(std::declval<const T1&>() sign std::declval<const T2&>()); \
}
GuessResultType(add,+);
GuessResultType(sub,-);
GuessResultType(mul,*);
GuessResultType(div,/);
template <typename Op, typename Result>
struct neg_scalar {
typedef Op argument_type;
typedef Result result_type;
result_type operator() (typename function_argument<Op>::type a) const { return -a; }
void assign(Op& a) const { negate(a); }
};
template <typename Op>
struct neg_scalar<Op,void> {
typedef void result_type;
};
template <typename Left, typename Right, typename Result>
struct add_scalar {
typedef Left first_argument_type;
typedef Right second_argument_type;
typedef Result result_type;
result_type operator() (typename function_argument<Left>::type a, typename function_argument<Right>::type b) const { return a+b; }
template <typename Iterator2>
const Left& operator() (partial_left, const Left& a, const Iterator2&) const { return a; }
template <typename Iterator1>
const Right& operator() (partial_right, const Iterator1&, const Right& b) const { return b; }
void assign(Left& a, typename function_argument<Right>::type b) const { a+=b; }
};
template <typename Left, typename Right, typename Result>
struct sub_scalar {
typedef Left first_argument_type;
typedef Right second_argument_type;
typedef Result result_type;
result_type operator() (typename function_argument<Left>::type a, typename function_argument<Right>::type b) const { return a-b; }
template <typename Iterator2>
const Left& operator() (partial_left, const Left& a, const Iterator2&) const { return a; }
template <typename Iterator1>
result_type operator() (partial_right, const Iterator1&, typename function_argument<Right>::type b) const { return -b; }
void assign(Left& a, typename function_argument<Right>::type b) const { a-=b; }
};
template <typename Left, typename Right, typename Result>
struct mul_scalar {
typedef Left first_argument_type;
typedef Right second_argument_type;
typedef Result result_type;
result_type operator() (typename function_argument<Left>::type a, typename function_argument<Right>::type b) const { return a*b; }
void assign(Left& a, typename function_argument<Right>::type b) const { a*=b; }
};
template <typename Left, typename Right>
struct mul_scalar<Left,Right,void> {
typedef void result_type;
};
template <typename Left, typename Right, typename Result>
struct div_scalar {
typedef Left first_argument_type;
typedef Right second_argument_type;
typedef Result result_type;
result_type operator() (typename function_argument<Left>::type a, typename function_argument<Right>::type b) const { return a/b; }
void assign(Left& a, typename function_argument<Right>::type b) const { a/=b; }
};
template <typename Left, typename Right>
struct div_scalar<Left,Right,void> {
typedef void result_type;
};
template <typename Left, typename Right, typename Result>
struct divexact_scalar : div_scalar <Left, Right, Result> {};
} // end namespace operations
template <typename Char, typename Traits, typename Alloc>
struct spec_object_traits< std::basic_string<Char, Traits, Alloc> >
: spec_object_traits<is_opaque> {};
using std::sqrt;
}
namespace polymake {
using pm::negate;
using pm::complement;
using pm::sqr;
using pm::sqrt;
// TODO: rename back to operations when the old stuff has gone
namespace cleanOperations {
using neg = std::negate<>;
using add = std::plus<>;
using sub = std::minus<>;
using mul = std::multiplies<>;
using div = std::divides<>;
using mod = std::modulus<>;
using bit_not = std::bit_not<>;
using bit_and = std::bit_and<>;
using bit_or = std::bit_or<>;
using bit_xor = std::bit_xor<>;
struct lshift {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l) << std::forward<Right>(r);
}
};
struct rshift {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l) >> std::forward<Right>(r);
}
};
/// check whether the given elementary operation is defined for given argument types
template <typename Operation, typename T1, typename T2=void, typename is_defined=void>
struct can : std::false_type {};
template <typename T>
struct can<neg, T, void, accept_valid_type<decltype(-std::declval<const T&>())>> : std::true_type {};
template <typename T1, typename T2>
struct can<add, T1, T2, accept_valid_type<decltype(std::declval<const T1&>() + std::declval<const T2&>())>> : std::true_type {
using type = decltype(std::declval<const T1&>() + std::declval<const T2&>());
};
template <typename T1, typename T2>
struct can<sub, T1, T2, accept_valid_type<decltype(std::declval<const T1&>() - std::declval<const T2&>())>> : std::true_type {
using type = decltype(std::declval<const T1&>() - std::declval<const T2&>());
};
template <typename T1, typename T2>
struct can<mul, T1, T2, accept_valid_type<decltype(std::declval<const T1&>() * std::declval<const T2&>())>> : std::true_type {
using type = decltype(std::declval<const T1&>() * std::declval<const T2&>());
};
template <typename T1, typename T2>
struct can<div, T1, T2, accept_valid_type<decltype(std::declval<const T1&>() / std::declval<const T2&>())>> : std::true_type {
using type = decltype(std::declval<const T1&>() / std::declval<const T2&>());
};
template <typename T1, typename T2>
struct can<mod, T1, T2, accept_valid_type<decltype(std::declval<const T1&>() % std::declval<const T2&>())>> : std::true_type {
using type = decltype(std::declval<const T1&>() % std::declval<const T2&>());
};
template <typename T>
struct can<bit_not, T, void, accept_valid_type<decltype(~std::declval<const T&>())>> : std::true_type {};
template <typename T1, typename T2>
struct can<bit_and, T1, T2, accept_valid_type<decltype(std::declval<const T1&>() & std::declval<const T2&>())>> : std::true_type {
using type = decltype(std::declval<const T1&>() & std::declval<const T2&>());
};
template <typename T1, typename T2>
struct can<bit_or, T1, T2, accept_valid_type<decltype(std::declval<const T1&>() | std::declval<const T2&>())>> : std::true_type {
using type = decltype(std::declval<const T1&>() | std::declval<const T2&>());
};
template <typename T1, typename T2>
struct can<bit_xor, T1, T2, accept_valid_type<decltype(std::declval<const T1&>() ^ std::declval<const T2&>())>> : std::true_type {
using type = decltype(std::declval<const T1&>() ^ std::declval<const T2&>());
};
template <typename T1, typename T2>
struct can<lshift, T1, T2, accept_valid_type<decltype(std::declval<const T1&>() << std::declval<const T2&>())>> : std::true_type {
using type = decltype(std::declval<const T1&>() << std::declval<const T2&>());
};
template <typename T1, typename T2>
struct can<rshift, T1, T2, accept_valid_type<decltype(std::declval<const T1&>() >> std::declval<const T2&>())>> : std::true_type {
using type = decltype(std::declval<const T1&>() >> std::declval<const T2&>());
};
/// execute the assignment flavor of the given operation
/// that is, the result is to be assigned to the left operand
/// by default a dedicated method assign() is assumed to the operation class
template <typename Operation>
struct assign : public Operation {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return Operation::assign(std::forward<Left>(l), std::forward<Right>(r));
}
};
/// temporarily cast the given opration to its assignment flavor
template <typename Operation>
const assign<Operation>& assignment_flavor_of(const Operation& op)
{
return static_cast<const assign<Operation>&>(op);
}
template <>
struct assign<neg> : neg {
template <typename T>
decltype(auto) operator() (T&& x) const
{
return pm::negate(std::forward<T>(x));
}
};
template <>
struct assign<add> : add {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l += std::forward<Right>(r));
}
};
template <>
struct assign<sub> : sub {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l -= std::forward<Right>(r));
}
};
template <>
struct assign<mul> : mul {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l *= std::forward<Right>(r));
}
};
template <>
struct assign<div> : div {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l /= std::forward<Right>(r));
}
};
template <>
struct assign<mod> : mod {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l %= std::forward<Right>(r));
}
};
template <>
struct assign<bit_not> : bit_not {
template <typename T>
decltype(auto) operator() (T&& x) const
{
return complement(std::forward<T>(x));
}
};
template <>
struct assign<bit_and> : bit_and {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l &= std::forward<Right>(r));
}
};
template <>
struct assign<bit_or> : bit_or {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l |= std::forward<Right>(r));
}
};
template <>
struct assign<bit_xor> : bit_xor {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l ^= std::forward<Right>(r));
}
};
template <>
struct assign<lshift> : lshift {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l <<= std::forward<Right>(r));
}
};
template <>
struct assign<rshift> : rshift {
template <typename Left, typename Right>
decltype(auto) operator() (Left&& l, Right&& r) const
{
return std::forward<Left>(l >>= std::forward<Right>(r));
}
};
/// the flavor of the operation with an implicit default right operand as in sparse containers
/// by default, such a flavor is undefined, which implies that the operation would deliver a default value
template <typename Operation>
struct partial_left : std::false_type {};
/// the flavor of the operation with an implicit default left operand as in sparse containers
/// by default, such a flavor is undefined, which implies that the operation would deliver a default value
template <typename Operation>
struct partial_right : std::false_type {};
/// temporarily cast the given operation to its partial flavor
template <typename Operation>
const partial_left<Operation>& partial_left_flavor_of(const Operation& op)
{
return static_cast<const partial_left<Operation>&>(op);
}
template <typename Operation>
const partial_right<Operation>& partial_right_flavor_of(const Operation& op)
{
return static_cast<const partial_right<Operation>&>(op);
}
template <>
struct partial_left<add> : add {
template <typename Left, typename Iterator2>
Left&& operator() (Left&& l, const Iterator2&) const
{
return std::forward<Left>(l);
}
};
template <>
struct partial_right<add> : add {
template <typename Iterator1, typename Right>
Right&& operator() (const Iterator1&, Right&& r) const
{
return std::forward<Right>(r);
}
};
template <>
struct partial_left<sub> : sub {
template <typename Left, typename Iterator2>
Left&& operator() (Left&& l, const Iterator2&) const
{
return std::forward<Left>(l);
}
};
template <>
struct partial_right<sub> : sub {
template <typename Iterator1, typename Right>
decltype(auto) operator() (const Iterator1&, Right&& r) const
{
return -std::forward<Right>(r);
}
};
template <>
struct partial_left<bit_or> : bit_or {
template <typename Left, typename Iterator2>
Left&& operator() (Left&& l, const Iterator2&) const
{
return std::forward<Left>(l);
}
};
template <>
struct partial_right<bit_or> : bit_or {
template <typename Iterator1, typename Right>
Right&& operator() (const Iterator1&, Right&& r) const
{
return std::forward<Right>(r);
}
};
template <>
struct partial_left<bit_xor> : bit_xor {
template <typename Left, typename Iterator2>
Left&& operator() (Left&& l, const Iterator2&) const
{
return std::forward<Left>(l);
}
};
template <>
struct partial_right<bit_xor> : bit_xor {
template <typename Iterator1, typename Right>
Right&& operator() (const Iterator1&, Right&& r) const
{
return std::forward<Right>(r);
}
};
/// tell whether the given operation has well-defined partial flavors dealing with implicit default operands
template <typename Operation>
using is_partially_defined
= bool_not<mlist_or<is_derived_from<partial_left<Operation>, std::false_type>,
is_derived_from<partial_right<Operation>, std::false_type>>>;
}
}
namespace pm {
namespace cleanOperations = polymake::cleanOperations;
}
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|