![]() |
|
|||
Warning, file /include/boost/sort/common/rearrange.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 //---------------------------------------------------------------------------- 0002 /// @file rearrange.hpp 0003 /// @brief Indirect algorithm 0004 /// 0005 /// @author Copyright (c) 2016 Francisco Jose 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_REARRANGE_HPP 0014 #define __BOOST_SORT_COMMON_REARRANGE_HPP 0015 0016 #include <functional> 0017 #include <iterator> 0018 #include <type_traits> 0019 #include <vector> 0020 #include <cassert> 0021 #include <boost/sort/common/util/traits.hpp> 0022 0023 0024 namespace boost 0025 { 0026 namespace sort 0027 { 0028 namespace common 0029 { 0030 0031 template<class Iter_data> 0032 struct filter_iterator 0033 { 0034 //----------------------------------------------------------------------- 0035 // Variables 0036 //----------------------------------------------------------------------- 0037 Iter_data origin; 0038 0039 //----------------------------------------------------------------------- 0040 // Functions 0041 //----------------------------------------------------------------------- 0042 filter_iterator(Iter_data global_first): origin(global_first) { }; 0043 size_t operator ()(Iter_data itx) const 0044 { 0045 return size_t(itx - origin); 0046 } 0047 }; 0048 0049 struct filter_pos 0050 { 0051 size_t operator ()(size_t pos) const { return pos; }; 0052 }; 0053 0054 // 0055 //----------------------------------------------------------------------------- 0056 // function : rearrange 0057 /// @brief This function transform a logical sort of the elements in the index 0058 /// of iterators in a physical sort. 0059 // 0060 /// @param global_first : iterator to the first element of the data 0061 /// @param [in] index : vector of the iterators 0062 //----------------------------------------------------------------------------- 0063 template<class Iter_data, class Iter_index, class Filter_pos> 0064 void rearrange(Iter_data global_first, Iter_index itx_first, 0065 Iter_index itx_last, Filter_pos pos) 0066 { 0067 //----------------------------------------------------------------------- 0068 // Metaprogramming 0069 //----------------------------------------------------------------------- 0070 typedef util::value_iter<Iter_data> value_data; 0071 typedef util::value_iter<Iter_index> value_index; 0072 0073 //------------------------------------------------------------------------- 0074 // Code 0075 //------------------------------------------------------------------------- 0076 assert((itx_last - itx_first) >= 0); 0077 size_t pos_dest, pos_src, pos_ini; 0078 size_t nelem = size_t(itx_last - itx_first); 0079 Iter_data data = global_first; 0080 Iter_index index = itx_first; 0081 0082 pos_ini = 0; 0083 while (pos_ini < nelem) 0084 { 0085 while (pos_ini < nelem && pos(index[pos_ini]) == pos_ini) 0086 ++pos_ini; 0087 if (pos_ini == nelem) return; 0088 pos_dest = pos_src = pos_ini; 0089 value_data aux = std::move(data[pos_ini]); 0090 value_index itx_src = std::move(index[pos_ini]); 0091 0092 while ((pos_src = pos(itx_src)) != pos_ini) 0093 { 0094 using std::swap; 0095 data[pos_dest] = std::move(data[pos_src]); 0096 swap(itx_src, index[pos_src]); 0097 pos_dest = pos_src; 0098 }; 0099 0100 data[pos_dest] = std::move(aux); 0101 index[pos_ini] = std::move(itx_src); 0102 ++pos_ini; 0103 }; 0104 } 0105 0106 /* 0107 // 0108 //----------------------------------------------------------------------------- 0109 // function : rearrange_pos 0110 /// @brief This function transform a logical sort of the elements in the index 0111 /// of iterators in a physical sort. 0112 // 0113 /// @param global_first : iterator to the first element of the data 0114 /// @param [in] index : vector of the iterators 0115 //----------------------------------------------------------------------------- 0116 template < class Iter_t, class Number > 0117 void rearrange_pos (Iter_t global_first, std::vector< Number> &index) 0118 { 0119 //------------------------------------------------------------------------- 0120 // METAPROGRAMMING AND DEFINITIONS 0121 //------------------------------------------------------------------------- 0122 static_assert ( std::is_integral<Number>::value, "Incompatible Types"); 0123 typedef iter_value< Iter_t > value_t; 0124 0125 //------------------------------------------------------------------------- 0126 // CODE 0127 //------------------------------------------------------------------------- 0128 size_t pos_dest = 0; 0129 size_t pos_src = 0; 0130 size_t pos_ini = 0; 0131 size_t nelem = index.size ( ); 0132 Iter_t it_dest (global_first), it_src(global_first); 0133 0134 while (pos_ini < nelem) 0135 { 0136 while (pos_ini < nelem and 0137 index[pos_ini] == pos_ini) 0138 { 0139 ++pos_ini; 0140 }; 0141 0142 if (pos_ini == nelem) return; 0143 pos_dest = pos_src = pos_ini; 0144 it_dest = global_first + pos_dest; 0145 value_t Aux = std::move (*it_dest); 0146 0147 while ((pos_src = index[pos_dest]) != pos_ini) 0148 { 0149 index[pos_dest] = it_dest - global_first; 0150 it_src = global_first + pos_src; 0151 *it_dest = std::move (*it_src); 0152 it_dest = it_src; 0153 pos_dest = pos_src; 0154 }; 0155 0156 *it_dest = std::move (Aux); 0157 index[pos_dest] = it_dest - global_first; 0158 ++pos_ini; 0159 }; 0160 }; 0161 */ 0162 // 0163 //**************************************************************************** 0164 }// End namespace common 0165 }// End namespace sort 0166 }// End namespace boost 0167 //**************************************************************************** 0168 // 0169 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |