2 Copyright 2005-2010 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
6 Threading Building Blocks is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
10 Threading Building Blocks is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Threading Building Blocks; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 As a special exception, you may use this file as part of a free software
20 library without restriction. Specifically, if other files instantiate
21 templates or use macros or inline functions from this file, or you compile
22 this file and link it with other files to produce an executable, this
23 file does not by itself cause the resulting executable to be covered by
24 the GNU General Public License. This exception does not however
25 invalidate any other reasons why the executable file might be covered by
26 the GNU General Public License.
29 #ifndef __TBB_blocked_range_H
30 #define __TBB_blocked_range_H
32 #include "tbb_stddef.h"
36 /** \page range_req Requirements on range concept
37 Class \c R implementing the concept of range must define:
38 - \code R::R( const R& ); \endcode Copy constructor
39 - \code R::~R(); \endcode Destructor
40 - \code bool R::is_divisible() const; \endcode True if range can be partitioned into two subranges
41 - \code bool R::empty() const; \endcode True if range is empty
42 - \code R::R( R& r, split ); \endcode Split range \c r into two subranges.
45 //! A range over which to iterate.
46 /** @ingroup algorithms */
47 template<typename Value>
51 /** Called a const_iterator for sake of algorithms that need to treat a blocked_range
52 as an STL container. */
53 typedef Value const_iterator;
55 //! Type for size of a range
56 typedef std::size_t size_type;
58 //! Construct range with default-constructed values for begin and end.
59 /** Requires that Value have a default constructor. */
60 blocked_range() : my_end(), my_begin() {}
62 //! Construct range over half-open interval [begin,end), with the given grainsize.
63 blocked_range( Value begin_, Value end_, size_type grainsize_=1 ) :
64 my_end(end_), my_begin(begin_), my_grainsize(grainsize_)
66 __TBB_ASSERT( my_grainsize>0, "grainsize must be positive" );
69 //! Beginning of range.
70 const_iterator begin() const {return my_begin;}
72 //! One past last value in range.
73 const_iterator end() const {return my_end;}
76 /** Unspecified if end()<begin(). */
77 size_type size() const {
78 __TBB_ASSERT( !(end()<begin()), "size() unspecified if end()<begin()" );
79 return size_type(my_end-my_begin);
82 //! The grain size for this range.
83 size_type grainsize() const {return my_grainsize;}
85 //------------------------------------------------------------------------
86 // Methods that implement Range concept
87 //------------------------------------------------------------------------
89 //! True if range is empty.
90 bool empty() const {return !(my_begin<my_end);}
92 //! True if range is divisible.
93 /** Unspecified if end()<begin(). */
94 bool is_divisible() const {return my_grainsize<size();}
97 /** The new Range *this has the second half, the old range r has the first half.
98 Unspecified if end()<begin() or !is_divisible(). */
99 blocked_range( blocked_range& r, split ) :
101 my_begin(do_split(r)),
102 my_grainsize(r.my_grainsize)
106 /** NOTE: my_end MUST be declared before my_begin, otherwise the forking constructor will break. */
109 size_type my_grainsize;
111 //! Auxiliary function used by forking constructor.
112 /** Using this function lets us not require that Value support assignment or default construction. */
113 static Value do_split( blocked_range& r ) {
114 __TBB_ASSERT( r.is_divisible(), "cannot split blocked_range that is not divisible" );
115 Value middle = r.my_begin + (r.my_end-r.my_begin)/2u;
120 template<typename RowValue, typename ColValue>
121 friend class blocked_range2d;
123 template<typename RowValue, typename ColValue, typename PageValue>
124 friend class blocked_range3d;
129 #endif /* __TBB_blocked_range_H */