bootstrap.H 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // (c) Daniel Llorens - 2013-2015
  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 bootstrap.H
  7. /// @brief Before all other ra:: includes.
  8. #pragma once
  9. #include <sys/types.h> // ssize_t
  10. #include "ra/tuple-list.H"
  11. #include "ra/tuple-dynamic.H"
  12. #ifdef RA_CHECK_BOUNDS
  13. #define RA_CHECK_BOUNDS_RA_BOOTSTRAP RA_CHECK_BOUNDS
  14. #else
  15. #ifndef RA_CHECK_BOUNDS_RA_BOOTSTRAP
  16. #define RA_CHECK_BOUNDS_RA_BOOTSTRAP 1
  17. #endif
  18. #endif
  19. #if RA_CHECK_BOUNDS_RA_BOOTSTRAP==0
  20. #define CHECK_BOUNDS( cond )
  21. #else
  22. #define CHECK_BOUNDS( cond ) assert( cond )
  23. #endif
  24. namespace ra {
  25. constexpr int VERSION = 4;
  26. static_assert(sizeof(int)>=4, "bad assumption on int");
  27. using rank_t = int;
  28. using dim_t = ssize_t;
  29. dim_t const DIM_ANY = -1099999999;
  30. dim_t const DIM_BAD = -1088888888;
  31. rank_t const RANK_ANY = DIM_ANY;
  32. rank_t const RANK_BAD = DIM_BAD;
  33. constexpr dim_t dim_prod(dim_t const a, dim_t const b)
  34. {
  35. return (a==DIM_ANY) ? DIM_ANY : ((b==DIM_ANY) ? DIM_ANY : a*b);
  36. }
  37. constexpr rank_t rank_sum(rank_t const a, rank_t const b)
  38. {
  39. return (a==RANK_ANY) ? RANK_ANY : ((b==RANK_ANY) ? RANK_ANY : a+b);
  40. }
  41. constexpr rank_t rank_diff(rank_t const a, rank_t const b)
  42. {
  43. return (a==RANK_ANY) ? RANK_ANY : ((b==RANK_ANY) ? RANK_ANY : a-b);
  44. }
  45. constexpr bool inside(dim_t const i, dim_t const b)
  46. {
  47. return i>=0 && i<b;
  48. }
  49. constexpr bool inside(dim_t const i, dim_t const a, dim_t const b)
  50. {
  51. return i>=a && i<b;
  52. }
  53. // used in array constructors.
  54. enum class unspecified_t { value };
  55. constexpr unspecified_t unspecified = unspecified_t::value;
  56. template <class S, class I, class P>
  57. inline void next_in_cube(rank_t const framer, S const & dim, I & i, P & p)
  58. {
  59. for (int j=framer-1; j>=0; --j) {
  60. ++i[j];
  61. if (i[j]<dim[j].size) {
  62. p += dim[j].stride;
  63. return;
  64. } else {
  65. i[j] = 0;
  66. p -= dim[j].stride*(dim[j].size-1);
  67. }
  68. }
  69. p = nullptr;
  70. }
  71. // Order of decl issues.
  72. template <class T, rank_t RANK=RANK_ANY> struct View;
  73. template <class T, class sizes, class strides> struct SmallArray;
  74. template <class V, class Enable=void> struct ra_traits_def;
  75. template <class S> struct default_strides {};
  76. template <class t0, class t1, class ... ti>
  77. struct default_strides<std::tuple<t0, t1, ti ...>>
  78. {
  79. using rest = typename default_strides<std::tuple<t1, ti ...>>::type;
  80. static int const stride0 = t1::value * mp::First_<rest>::value;
  81. using type = mp::Cons_<mp::int_t<stride0>, rest>;
  82. };
  83. template <class tend>
  84. struct default_strides<std::tuple<tend>> { using type = mp::int_list<1>; };
  85. template <>
  86. struct default_strides<std::tuple<>> { using type = mp::int_list<>; };
  87. template <class S> using default_strides_ = typename default_strides<S>::type;
  88. template <class T, dim_t ... sizes>
  89. using Small = SmallArray<T, mp::int_list<sizes ...>, default_strides_<mp::int_list<sizes ...> >>;
  90. // Dope vector element
  91. struct Dim { dim_t size, stride; };
  92. template <int n> struct dots_t
  93. {
  94. static_assert(n>=0);
  95. constexpr static rank_t rank_s() { return n; }
  96. };
  97. template <int n> constexpr dots_t<n> dots = dots_t<n>();
  98. constexpr auto all = dots<1>;
  99. template <int n> struct newaxis_t
  100. {
  101. static_assert(n>=0);
  102. constexpr static rank_t rank_s() { return n; }
  103. };
  104. template <int n=1> constexpr newaxis_t<n> newaxis = newaxis_t<n>();
  105. } // namespace ra
  106. #undef CHECK_BOUNDS
  107. #undef RA_CHECK_BOUNDS_RA_BOOTSTRAP