MPQC  3.0.0-alpha
range.hpp
1 #ifndef MPQC_RANGE_HPP
2 #define MPQC_RANGE_HPP
3 
4 #include <iostream>
5 #include <vector>
6 #include <stdint.h>
7 
8 #include <boost/range/irange.hpp>
9 #include <boost/iterator/iterator_facade.hpp>
10 #include <boost/utility/enable_if.hpp>
11 #include <boost/type_traits/is_integral.hpp>
12 #include <boost/preprocessor/repetition.hpp>
13 
14 #include <boost/fusion/include/size.hpp>
15 #include <boost/fusion/include/at_c.hpp>
16 #include <util/misc/assert.h>
17 
18 namespace mpqc {
19 
22 
23  struct range :
24  boost::iterator_range<boost::range_detail::integer_iterator<int64_t> >
25  {
26 
27  // typedef boost::iterator_range<
28  // boost::range_detail::integer_iterator<int>
29  // >::iterator_category iterator_category;
30 
31  typedef boost::iterator_range<
32  boost::range_detail::integer_iterator<int64_t> > iterator_range;
33 
34  template<class S>
35  struct tie;
36 
37  explicit range(int64_t size = 0)
38  : iterator_range(int64_t(0), size) {}
39 
40  range(int64_t begin, int64_t end)
41  : iterator_range(begin, end) {}
42 
43  int64_t size() const {
44  return end() - begin();
45  }
46 
47  bool test(int64_t value) const {
48  return ((*this->begin() <= value) && (value < *this->end()));
49  }
50 
51  };
52 
54  inline std::ostream& operator<<(std::ostream &os, const range &r) {
55  os << *r.begin() << ":" << *r.end();
56  return os;
57  }
58 
59  inline bool operator==(const range &a, const range &b) {
60  return (a.begin() == b.begin() && a.end() == b.end());
61  }
62 
63  inline range intersection(const range &a, const range &b) {
64  int64_t begin = std::max(*a.begin(), *b.begin());
65  int64_t end = std::min(*a.end(), *b.end());
66  return (begin < end) ? range(begin, end) : range();
67  }
68 
70  inline range operator&(const range &a, const range &b) {
71  return intersection(a,b);
72  }
73 
74 
76  inline std::vector<range> partition(const range &R, size_t N) {
77  std::vector<range> v;
78  size_t m = R.size()%N;
79  size_t b = R.size()/N;
80  int64_t r = *R.begin();
81  for (size_t i = 0; i < m; ++i) {
82  v.push_back(range(r, r+b+1));
83  r += b+1;
84  }
85  for (size_t i = m; i < N; ++i) {
86  v.push_back(range(r, r+b));
87  r += b;
88  }
89  MPQC_ASSERT(r == *R.end());
90  return v;
91  }
92 
94  inline std::vector<range> split(range r, size_t N) {
95  if (N >= r.size())
96  return std::vector<range>(1, r);
97  std::vector<range> blocks;
98  for (int64_t i = *r.begin(); i < *r.end(); i += N) {
99  blocks.push_back(range(i, std::min<int64_t>(i+N, *r.end())));
100  }
101  return blocks;
102  }
103 
105  inline std::vector<range> balanced_split(const range &r, size_t N) {
106  size_t nb = (r.size() + N-1)/N; // number of blocks
107  return partition(r, nb);
108  }
109 
110 
112  inline std::ostream& operator<<(std::ostream &os, const std::vector<range> &r) {
113  os << "[ ";
114  for (int i = 0; i < r.size(); ++i) {
115  os << r[i] << ",";
116  }
117  os << " ]";
118  return os;
119  }
120 
121 
123  inline range range_cast(const range &r) {
124  return r;
125  }
126 
128  template<class R>
129  typename boost::enable_if<boost::is_integral<R>, range>::type
130  range_cast(const R &r) {
131  //std::cout << "range_cast<R>" << r << std::endl;
132  return range(r,r+1);
133  }
134 
136 
137 
138 }
139 
140 namespace mpqc {
141 
142  template<class It>
143  struct Range : boost::iterator_range<It>
144  {
145  typedef boost::iterator_range<It> iterator_range;
146  Range(It begin, It end) : iterator_range(begin, end) {}
147  //size_t size() const { return iterator_range::end() - begin(); }
148  };
149 
150 }
151 
152 namespace sc {
153  using mpqc::range;
154 }
155 
156 #endif // MPQC_RANGE_HPP
mpqc
Contains new MPQC code since version 3.
Definition: integralenginepool.hpp:37
mpqc::range
Definition: range.hpp:23
mpqc::range_cast
range range_cast(const range &r)
Cast range to range, return argument unchanged.
Definition: range.hpp:123
mpqc::balanced_split
std::vector< range > balanced_split(const range &r, size_t N)
Split range into roughly equal blocks of size N or less.
Definition: range.hpp:105
mpqc::operator&
range operator&(const range &a, const range &b)
Range intersection.
Definition: range.hpp:70
mpqc::partition
std::vector< range > partition(const range &R, size_t N)
Partition range into N blocks.
Definition: range.hpp:76
mpqc::range::tie
boost::tuple tie wrapper
Definition: tie.hpp:15
mpqc::operator<<
void operator<<(Array< T > A, const V &v)
Write to Array from a generic vector V.
Definition: array.hpp:191
mpqc::split
std::vector< range > split(range r, size_t N)
Split range into blocks of size N.
Definition: range.hpp:94
mpqc::Range
Definition: range.hpp:143
sc
Contains all MPQC code up to version 3.
Definition: mpqcin.h:14

Generated at Sun Jan 26 2020 23:24:01 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.