int_t.H 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // (c) Daniel Llorens - 2005-2012, 2016
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. /// @file int_t.H
  7. /// @brief Treat integers as types, and global functions for integers.
  8. #pragma once
  9. #include "ra/macros.H"
  10. namespace mp {
  11. template <int V> using int_t = std::integral_constant<int, V>;
  12. template <bool V> using bool_t = std::integral_constant<bool, V>;
  13. constexpr long pown(int n, int p)
  14. {
  15. return p==0 ? 1 : n*pown(n, p-1);
  16. }
  17. constexpr long pow2(int n)
  18. {
  19. return pown(2, n);
  20. }
  21. constexpr long fact(int i)
  22. {
  23. return (i<2) ? 1 : i*fact(i-1);
  24. }
  25. // Works *almost* to the range of size_t.
  26. constexpr size_t n_over_p(size_t const n, size_t p)
  27. {
  28. if (p>n) {
  29. return 0;
  30. } else if (p>(n-p)) {
  31. p = n-p;
  32. }
  33. size_t v = 1;
  34. for (size_t i=0; i!=p; ++i) {
  35. v = v*(n-i)/(i+1);
  36. }
  37. return v;
  38. }
  39. } // namespace mp
  40. // TODO All users be int, then this take int.
  41. constexpr bool odd(unsigned int N) { return N & 1; }
  42. constexpr bool any(bool const x) { return x; }
  43. constexpr bool every(bool const x) { return x; }