Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:46

0001 //----------------------------------------------------------------------------
0002 /// @file pivot.hpp
0003 /// @brief This file contains the description of several low level algorithms
0004 ///
0005 /// @author Copyright (c) 2010 2015 Francisco José Tapia (fjtapia@gmail.com )\n
0006 ///         Distributed under the Boost Software License, Version 1.0.\n
0007 ///         ( See accompanying file LICENSE_1_0.txt or copy at
0008 ///           http://www.boost.org/LICENSE_1_0.txt  )
0009 /// @version 0.1
0010 ///
0011 /// @remarks
0012 //-----------------------------------------------------------------------------
0013 #ifndef __BOOST_SORT_COMMON_PIVOT_HPP
0014 #define __BOOST_SORT_COMMON_PIVOT_HPP
0015 
0016 #include <ciso646>
0017 #include <cstdint>
0018 
0019 namespace boost
0020 {
0021 namespace sort
0022 {
0023 namespace common
0024 {
0025 //
0026 //##########################################################################
0027 //                                                                        ##
0028 //                    G L O B A L     V A R I B L E S                     ##
0029 //                                                                        ##
0030 //##########################################################################
0031 //
0032 //-----------------------------------------------------------------------------
0033 //  function : mid3
0034 /// @brief : return the iterator to the mid value of the three values passsed
0035 ///          as parameters
0036 //
0037 /// @param iter_1 : iterator to the first value
0038 /// @param iter_2 : iterator to the second value
0039 /// @param iter_3 : iterator to the third value
0040 /// @param comp : object for to compare two values
0041 /// @return iterator to mid value
0042 //-----------------------------------------------------------------------------
0043 template < typename Iter_t, typename Compare >
0044 inline Iter_t mid3 (Iter_t iter_1, Iter_t iter_2, Iter_t iter_3, Compare comp)
0045 {
0046     using std::swap;
0047     if (comp (*iter_2, *iter_1)) swap ( *iter_2, *iter_1);
0048     if (comp (*iter_3, *iter_2))
0049     {   swap ( *iter_3, *iter_2);
0050         if (comp (*iter_2, *iter_1)) swap ( *iter_2, *iter_1);
0051     };
0052     return iter_2;
0053 };
0054 //
0055 //-----------------------------------------------------------------------------
0056 //  function : pivot3
0057 /// @brief : receive a range between first and last, calcule the mid iterator
0058 ///          with the first, the previous to the last, and the central
0059 ///          position. With this mid iterator swap with the first position
0060 //
0061 /// @param first : iterator to the first element
0062 /// @param last : iterator to the last element
0063 /// @param comp : object for to compare two elements
0064 //-----------------------------------------------------------------------------
0065 template < class Iter_t, class Compare >
0066 inline void pivot3 (Iter_t first, Iter_t last, Compare comp)
0067 {
0068     using std::swap;
0069     auto N2 = (last - first) >> 1;
0070     Iter_t it_val = mid3 (first + 1, first + N2, last - 1, comp);
0071     swap (*first, *it_val);
0072 };
0073 
0074 //
0075 //-----------------------------------------------------------------------------
0076 //  function : mid9
0077 /// @brief : return the iterator to the mid value of the nine values passsed
0078 ///          as parameters
0079 //
0080 /// @param iter_1 : iterator to the first value
0081 /// @param iter_2 : iterator to the second value
0082 /// @param iter_3 : iterator to the third value
0083 /// @param iter_4 : iterator to the fourth value
0084 /// @param iter_5 : iterator to the fifth value
0085 /// @param iter_6 : iterator to the sixth value
0086 /// @param iter_7 : iterator to the seventh value
0087 /// @param iter_8 : iterator to the eighth value
0088 /// @param iter_9 : iterator to the ninth value
0089 /// @return iterator to the mid value
0090 //-----------------------------------------------------------------------------
0091 template < class Iter_t, class Compare >
0092 inline Iter_t mid9 (Iter_t iter_1, Iter_t iter_2, Iter_t iter_3, Iter_t iter_4,
0093                     Iter_t iter_5, Iter_t iter_6, Iter_t iter_7, Iter_t iter_8,
0094                     Iter_t iter_9, Compare comp)
0095 {
0096     return mid3 (mid3 (iter_1, iter_2, iter_3, comp),
0097                  mid3 (iter_4, iter_5, iter_6, comp),
0098                  mid3 (iter_7, iter_8, iter_9, comp), comp);
0099 };
0100 //
0101 //-----------------------------------------------------------------------------
0102 //  function : pivot9
0103 /// @brief : receive a range between first and last, obtain 9 values between
0104 ///          the elements  including the first and the previous to the last.
0105 ///          Obtain the iterator to the mid value and swap with the first
0106 ///          position
0107 //
0108 /// @param first : iterator to the first element
0109 /// @param last : iterator to the last element
0110 /// @param comp : object for to compare two elements
0111 //-----------------------------------------------------------------------------
0112 template < class Iter_t, class Compare >
0113 inline void pivot9 (Iter_t first, Iter_t last, Compare comp)
0114 {
0115     using std::swap;
0116     size_t cupo = (last - first) >> 3;
0117     Iter_t itaux = mid9 (first + 1, first + cupo, first + 2 * cupo,
0118                          first + 3 * cupo, first + 4 * cupo, first + 5 * cupo,
0119                          first + 6 * cupo, first + 7 * cupo, last - 1, comp);
0120     swap (*first, *itaux);
0121 };
0122 //****************************************************************************
0123 };//    End namespace common
0124 };//    End namespace sort
0125 };//    End namespace boost
0126 //****************************************************************************
0127 #endif