index_gen.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2002 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Boost.MultiArray Library
  6. // Authors: Ronald Garcia
  7. // Jeremy Siek
  8. // Andrew Lumsdaine
  9. // See http://www.boost.org/libs/multi_array for documentation.
  10. #ifndef BOOST_MULTI_ARRAY_INDEX_GEN_HPP
  11. #define BOOST_MULTI_ARRAY_INDEX_GEN_HPP
  12. #include "boost/array.hpp"
  13. #include "boost/multi_array/index_range.hpp"
  14. #include "boost/multi_array/range_list.hpp"
  15. #include "boost/multi_array/types.hpp"
  16. #include <algorithm>
  17. #include <cstddef>
  18. namespace boost {
  19. namespace detail {
  20. namespace multi_array {
  21. template <int NumRanges, int NumDims>
  22. struct index_gen {
  23. private:
  24. typedef ::boost::detail::multi_array::index index;
  25. typedef ::boost::detail::multi_array::size_type size_type;
  26. typedef index_range<index,size_type> range;
  27. public:
  28. template <int Dims, int Ranges>
  29. struct gen_type {
  30. typedef index_gen<Ranges,Dims> type;
  31. };
  32. typedef typename range_list_generator<range,NumRanges>::type range_list;
  33. range_list ranges_;
  34. index_gen() { }
  35. template <int ND>
  36. explicit index_gen(const index_gen<NumRanges-1,ND>& rhs,
  37. const range& r)
  38. {
  39. std::copy(rhs.ranges_.begin(),rhs.ranges_.end(),ranges_.begin());
  40. *ranges_.rbegin() = r;
  41. }
  42. index_gen<NumRanges+1,NumDims+1>
  43. operator[](const range& r) const
  44. {
  45. index_gen<NumRanges+1,NumDims+1> tmp;
  46. std::copy(ranges_.begin(),ranges_.end(),tmp.ranges_.begin());
  47. *tmp.ranges_.rbegin() = r;
  48. return tmp;
  49. }
  50. index_gen<NumRanges+1,NumDims>
  51. operator[](index idx) const
  52. {
  53. index_gen<NumRanges+1,NumDims> tmp;
  54. std::copy(ranges_.begin(),ranges_.end(),tmp.ranges_.begin());
  55. *tmp.ranges_.rbegin() = range(idx);
  56. return tmp;
  57. }
  58. static index_gen<0,0> indices() {
  59. return index_gen<0,0>();
  60. }
  61. };
  62. } // namespace multi_array
  63. } // namespace detail
  64. } // namespace boost
  65. #endif