sort.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2008 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 2, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this library; see the file COPYING.  If not, write to
00018 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00019 // MA 02111-1307, USA.
00020 
00021 // As a special exception, you may use this file as part of a free
00022 // software library without restriction.  Specifically, if other files
00023 // instantiate templates or use macros or inline functions from this
00024 // file, or you compile this file and link it with other files to
00025 // produce an executable, this file does not by itself cause the
00026 // resulting executable to be covered by the GNU General Public
00027 // License.  This exception does not however invalidate any other
00028 // reasons why the executable file might be covered by the GNU General
00029 // Public License.
00030 
00031 /** @file parallel/sort.h
00032  *  @brief Parallel sorting algorithm switch.
00033  *  This file is a GNU parallel extension to the Standard C++ Library.
00034  */
00035 
00036 // Written by Johannes Singler.
00037 
00038 #ifndef _GLIBCXX_PARALLEL_SORT_H
00039 #define _GLIBCXX_PARALLEL_SORT_H 1
00040 
00041 #include <parallel/basic_iterator.h>
00042 #include <parallel/features.h>
00043 #include <parallel/parallel.h>
00044 
00045 #if _GLIBCXX_ASSERTIONS
00046 #include <parallel/checkers.h>
00047 #endif
00048 
00049 #if _GLIBCXX_MERGESORT
00050 #include <parallel/multiway_mergesort.h>
00051 #endif
00052 
00053 #if _GLIBCXX_QUICKSORT
00054 #include <parallel/quicksort.h>
00055 #endif
00056 
00057 #if _GLIBCXX_BAL_QUICKSORT
00058 #include <parallel/balanced_quicksort.h>
00059 #endif
00060 
00061 namespace __gnu_parallel
00062 {
00063   /** 
00064    *  @brief Choose a parallel sorting algorithm.
00065    *  @param begin Begin iterator of input sequence.
00066    *  @param end End iterator of input sequence.
00067    *  @param comp Comparator.
00068    *  @param stable Sort stable.
00069    *  @callgraph 
00070    */
00071   template<typename RandomAccessIterator, typename Comparator>
00072     inline void
00073     parallel_sort(RandomAccessIterator begin, RandomAccessIterator end,
00074                   Comparator comp, bool stable)
00075     {
00076       _GLIBCXX_CALL(end - begin)
00077       typedef std::iterator_traits<RandomAccessIterator> traits_type;
00078       typedef typename traits_type::value_type value_type;
00079       typedef typename traits_type::difference_type difference_type;
00080 
00081       if (begin != end)
00082       {
00083         difference_type n = end - begin;
00084 
00085         if (false) ;
00086 #if _GLIBCXX_MERGESORT
00087         else if (stable)
00088           {
00089             if(_Settings::get().sort_splitting == EXACT)
00090               parallel_sort_mwms<true, true>
00091                 (begin, end, comp, get_max_threads());
00092             else
00093               parallel_sort_mwms<true, false>
00094                 (begin, end, comp, get_max_threads());
00095           }
00096         else if (_Settings::get().sort_algorithm == MWMS)
00097           {
00098             if(_Settings::get().sort_splitting == EXACT)
00099               parallel_sort_mwms<false, true>
00100                 (begin, end, comp, get_max_threads());
00101             else
00102               parallel_sort_mwms<false, false>
00103                 (begin, end, comp, get_max_threads());
00104           }
00105 #endif
00106 #if _GLIBCXX_QUICKSORT
00107         else if (!stable && _Settings::get().sort_algorithm == QS)
00108           parallel_sort_qs(begin, end, comp, n, get_max_threads());
00109 #endif
00110 #if _GLIBCXX_BAL_QUICKSORT
00111         else if (!stable && _Settings::get().sort_algorithm == QS_BALANCED)
00112           parallel_sort_qsb(begin, end, comp, n, get_max_threads());
00113 #endif
00114         else if(stable)
00115           __gnu_sequential::stable_sort(begin, end, comp);
00116         else
00117           __gnu_sequential::sort(begin, end, comp);
00118       }
00119     }
00120 } // end namespace __gnu_parallel
00121 
00122 #endif

Generated on Wed Dec 31 12:48:59 2008 for libstdc++ by  doxygen 1.5.6