MADNESS  version 0.9
worldrange.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31  $Id$
32 */
33 #ifndef MADNESS_WORLD_WORLDRANGE_H__INCLUDED
34 #define MADNESS_WORLD_WORLDRANGE_H__INCLUDED
35 
36 #include <iterator>
38 
41 
42 namespace madness {
43 
45  class Split {};
46 
48  template <typename iteratorT>
49  class Range {
50  long n;
51  iteratorT start;
52  iteratorT finish;
53  int chunksize;
54  public:
55  typedef iteratorT iterator;
56 
58 
61  Range(const iterator& start, const iterator& finish, int chunk=1)
62  : n(distance(start,finish))
63  , start(start)
64  , finish(finish)
65  , chunksize(chunk)
66  {
67  if (chunksize < 1) chunksize = 1;
68  }
69 
71  Range(const Range& r)
72  : n(r.n)
73  , start(r.start)
74  , finish(r.finish)
75  , chunksize(r.chunksize)
76  {}
77 
79  Range(Range& left, const Split& /*split*/)
80  : n(0)
81  , start(left.finish)
82  , finish(left.finish)
83  , chunksize(left.chunksize)
84  {
85  if (left.n > chunksize) {
86  int nleft = (left.n+1)/2;
87 
88  start = left.start;
89  advance(start,nleft);
90  finish = left.finish;
91  n = left.n - nleft;
92 
93  left.finish = start;
94  left.n = nleft;
95  }
96  }
97 
99  size_t size() const {
100  return n;
101  }
102 
104  bool empty() const {
105  return n==0;
106  }
107 
108  const iterator& begin() const {
109  return start;
110  }
111 
112  const iterator& end() const {
113  return finish;
114  }
115 
116  unsigned int get_chunksize() const {
117  return chunksize;
118  }
119 
120  private:
121  template<typename integralT, typename distanceT>
122  inline static typename enable_if<std::is_integral<integralT>, void>::type
123  advance(integralT& i, distanceT n) { i += n; }
124 
125  template<typename iterT, typename distanceT>
126  inline static typename disable_if<std::is_integral<iterT>, void>::type
127  advance(iterT& it, distanceT n) { std::advance(it, n); }
128 
129  template<class integralT>
130  inline static typename enable_if<std::is_integral<integralT>, integralT>::type
131  distance(integralT first, integralT last) { return last - first; }
132 
133  template <class iterT>
134  struct diff_type {
135  typedef typename std::iterator_traits<iterT>::difference_type type;
136  };
137 
138  template<class iterT>
139  inline static typename lazy_disable_if<std::is_integral<iterT>, diff_type<iterT> >::type
140  distance(iterT first, iterT last) { return std::distance(first, last); }
141  };
142 
143 } // namespace madness
144 
145 #endif // MADNESS_WORLD_WORLDRANGE_H__INCLUDED
Range(const iterator &start, const iterator &finish, int chunk=1)
Makes the range [start,finish)
Definition: worldrange.h:61
Dummy class a la Intel TBB used to distinguish splitting constructor.
Definition: worldrange.h:45
const iterator & begin() const
Definition: worldrange.h:108
Grossly simplified Boost-like type traits and templates.
size_t size() const
Returns number of items in the range (cost is O(1))
Definition: worldrange.h:99
Range(const Range &r)
Copy constructor ... cost is O(1)
Definition: worldrange.h:71
unsigned int get_chunksize() const
Definition: worldrange.h:116
Range(Range &left, const Split &)
Splits range between new and old (r) objects ... cost is O(1)
Definition: worldrange.h:79
const iterator & end() const
Definition: worldrange.h:112
int distance(const madness::Hash_private::HashIterator< hashT > &it, const madness::Hash_private::HashIterator< hashT > &jt)
Definition: worldhashmap.h:608
void advance(madness::Hash_private::HashIterator< hashT > &it, const distT &dist)
Definition: worldhashmap.h:602
iteratorT iterator
Definition: worldrange.h:55
bool empty() const
Returns true if size=0.
Definition: worldrange.h:104
Range vaguely a la Intel TBB encapsulates random-access STL-like start and end iterators with chunksi...
Definition: worldrange.h:49
enable_if from Boost for conditionally instantiating templates based on type
Definition: enable_if.h:60
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45