Boost logo

Boost Users :

Subject: [Boost-users] [ sorting_view ] -do not sort arrays, sort iterators instead
From: Boris Kats (boriskats_at_[hidden])
Date: 2016-02-27 13:22:02

The sorting procedure is one of the most usable routine in programming.And all sorting template functions have the same signature:template< class RandomIt, class Compare >void sort( RandomIt first, RandomIt last, Compare comp );with conditions: the type of dereferenced RandomIt must meet the requirements of MoveAssignable and MoveConstructible.It is clear that in the sorting process a lot of records are moving in the memory andwhen these records have a big size, these movements, together with calling constructors, can be very expensive.Solution.The proposed template function sorting_view is free of that problem - the sorting procedureis working (and moving) on tiny objects - iterators. As result of that procedure,the initial array of records is remaining intact; however provided std::vector<int> viewallows indirect access to the sorted array. On many occasions user does not need the sortedarray at all and he/she has a luxury to iterate through sorted and unsorted arrays simultaneously.In case of real needs of sorted array, it can be obtained via other template function apply_view with exactly 2*N movements of the records, where N is the size of initial array.For getting sorted view of arrays of user's defined records the appropriate Compare template function  should be supplied.Happy sorting,  Boris Kats.                                        

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at