home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / parallel / blocked_range.h next >
Encoding:
C/C++ Source or Header  |  2008-10-25  |  3.5 KB  |  142 lines

  1. #ifndef K3DSDK_PARALLEL_BLOCKED_RANGE_H
  2. #define K3DSDK_PARALLEL_BLOCKED_RANGE_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2008, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.     \author Tim Shead (tshead@k-3d.com)
  25. */
  26.  
  27. #include <k3d-parallel-config.h>
  28. #include <k3dsdk/types.h>
  29.  
  30. namespace k3d
  31. {
  32.  
  33. namespace parallel
  34. {
  35.  
  36. template<typename ValueT>
  37. class blocked_range
  38. {
  39. public:
  40.     //! Type of a value
  41.     /** Called a const_iterator for sake of algorithms that need to treat a blocked_range
  42.     as an STL container. */
  43.     typedef ValueT const_iterator;
  44.  
  45.     //! Type for size of a range
  46.     typedef uint_t size_type;
  47.  
  48.     //! Construct range with default-constructed values for begin and end.
  49.     /** Requires that Value have a default constructor. */
  50.     blocked_range() :
  51.         m_begin(),
  52.         m_end()
  53.     {
  54.     }
  55.  
  56.     //! Construct range over half-open interval [begin,end), with the given grainsize.
  57.     blocked_range(ValueT Begin, ValueT End, size_type Grainsize = 1) : 
  58.         m_end(End),
  59.         m_begin(Begin),
  60.         m_grainsize(Grainsize) 
  61.     {
  62. //        __TBB_ASSERT( m_grainsize>0, "grainsize must be positive" );
  63.     }
  64.  
  65.     //! Beginning of range.
  66.     const_iterator begin() const
  67.     {
  68.         return m_begin;
  69.     }
  70.  
  71.     //! One past last value in range.
  72.     const_iterator end() const
  73.     {
  74.         return m_end;
  75.     }
  76.  
  77.     //! Size of the range
  78.     /** Unspecified if end()<begin(). */
  79.     size_type size() const
  80.     {
  81. //        __TBB_ASSERT( !(end() < begin()), "size() unspecified if end()<begin()" );
  82.         return size_type(m_end - m_begin);
  83.     }
  84.  
  85.     //! The grain size for this range.
  86.     size_type grainsize() const
  87.     {
  88.         return m_grainsize;
  89.     }
  90.  
  91.     //------------------------------------------------------------------------
  92.     // Methods that implement Range concept
  93.     //------------------------------------------------------------------------
  94.  
  95.     //! True if range is empty.
  96.     bool empty() const
  97.     {
  98.         return !(m_begin < m_end);
  99.     }
  100.  
  101.     //! True if range is divisible.
  102.     /** Unspecified if end()<begin(). */
  103.     bool is_divisible() const
  104.     {
  105.         return m_grainsize < size();
  106.     }
  107.  
  108.     //! Split range.  
  109.     /** The new Range *this has the second half, the old range r has the first half. 
  110.     Unspecified if end()<begin() or !is_divisible(). */
  111.     template<typename SplitT>
  112.     blocked_range(blocked_range& r, SplitT split) : 
  113.         m_end(r.m_end),
  114.         m_begin(do_split(r)),
  115.         m_grainsize(r.m_grainsize)
  116.     {
  117.     }
  118.  
  119. private:
  120.     /** NOTE: m_end MUST be declared before m_begin, otherwise the forking constructor will break. */
  121.     ValueT m_end;
  122.     ValueT m_begin;
  123.     size_type m_grainsize;
  124.  
  125.     //! Auxilary function used by forking constructor.
  126.     /** Using this function lets us not require that Value support assignment or default construction. */
  127.     static ValueT do_split(blocked_range& r)
  128.     {
  129. //        __TBB_ASSERT( r.is_divisible(), "cannot split blocked_range that is not divisible" );
  130.         ValueT middle = r.m_begin + (r.m_end - r.m_begin) / 2u;
  131.         r.m_end = middle;
  132.         return middle;
  133.     }
  134. };
  135.  
  136. } // namespace parallel
  137.  
  138. } // namespace k3d
  139.  
  140. #endif // !K3DSDK_PARALLEL_BLOCKED_RANGE_H
  141.  
  142.