patch-media_webrtc_trunk_webrtc_system_wrappers_source_spreadsortlib_spreadsort_hpp 146 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385
  1. --- mozilla-release.orig/media/webrtc/trunk/webrtc/system_wrappers/source/spreadsortlib/spreadsort.hpp 2013-12-05 17:07:50.000000000 +0100
  2. +++ mozilla-release/media/webrtc/trunk/webrtc/system_wrappers/source/spreadsortlib/spreadsort.hpp 2014-02-05 09:52:11.000000000 +0100
  3. @@ -1,1688 +1,1694 @@
  4. -//Templated spread_sort library
  5. -
  6. -// Copyright Steven J. Ross 2001 - 2009.
  7. -// Distributed under the Boost Software License, Version 1.0.
  8. -// (See accompanying file LICENSE_1_0.txt or copy at
  9. -// http://www.boost.org/LICENSE_1_0.txt)
  10. -
  11. -// See http://www.boost.org/ for updates, documentation, and revision history.
  12. -
  13. -/*
  14. -Some improvements suggested by:
  15. -Phil Endecott and Frank Gennari
  16. -Cygwin fix provided by:
  17. -Scott McMurray
  18. -*/
  19. -
  20. -#ifndef BOOST_SPREAD_SORT_H
  21. -#define BOOST_SPREAD_SORT_H
  22. -#include <algorithm>
  23. -#include <cstring>
  24. -#include <vector>
  25. -#include "webrtc/system_wrappers/source/spreadsortlib/constants.hpp"
  26. -
  27. -namespace boost {
  28. - namespace detail {
  29. - //This only works on unsigned data types
  30. - template <typename T>
  31. - inline unsigned
  32. - rough_log_2_size(const T& input)
  33. - {
  34. - unsigned result = 0;
  35. - //The && is necessary on some compilers to avoid infinite loops; it doesn't significantly impair performance
  36. - while((input >> result) && (result < (8*sizeof(T)))) ++result;
  37. - return result;
  38. - }
  39. -
  40. - //Gets the maximum size which we'll call spread_sort on to control worst-case performance
  41. - //Maintains both a minimum size to recurse and a check of distribution size versus count
  42. - //This is called for a set of bins, instead of bin-by-bin, to avoid performance overhead
  43. - inline size_t
  44. - get_max_count(unsigned log_range, size_t count)
  45. - {
  46. - unsigned divisor = rough_log_2_size(count);
  47. - //Making sure the divisor is positive
  48. - if(divisor > LOG_MEAN_BIN_SIZE)
  49. - divisor -= LOG_MEAN_BIN_SIZE;
  50. - else
  51. - divisor = 1;
  52. - unsigned relative_width = (LOG_CONST * log_range)/((divisor > MAX_SPLITS) ? MAX_SPLITS : divisor);
  53. - //Don't try to bitshift more than the size of an element
  54. - if((8*sizeof(size_t)) <= relative_width)
  55. - relative_width = (8*sizeof(size_t)) - 1;
  56. - return (size_t)1 << ((relative_width < (LOG_MEAN_BIN_SIZE + LOG_MIN_SPLIT_COUNT)) ?
  57. - (LOG_MEAN_BIN_SIZE + LOG_MIN_SPLIT_COUNT) : relative_width);
  58. - }
  59. -
  60. - //Find the minimum and maximum using <
  61. - template <class RandomAccessIter>
  62. - inline void
  63. - find_extremes(RandomAccessIter current, RandomAccessIter last, RandomAccessIter & max, RandomAccessIter & min)
  64. - {
  65. - min = max = current;
  66. - //Start from the second item, as max and min are initialized to the first
  67. - while(++current < last) {
  68. - if(*max < *current)
  69. - max = current;
  70. - else if(*current < *min)
  71. - min = current;
  72. - }
  73. - }
  74. -
  75. - //Uses a user-defined comparison operator to find minimum and maximum
  76. - template <class RandomAccessIter, class compare>
  77. - inline void
  78. - find_extremes(RandomAccessIter current, RandomAccessIter last, RandomAccessIter & max, RandomAccessIter & min, compare comp)
  79. - {
  80. - min = max = current;
  81. - while(++current < last) {
  82. - if(comp(*max, *current))
  83. - max = current;
  84. - else if(comp(*current, *min))
  85. - min = current;
  86. - }
  87. - }
  88. -
  89. - //Gets a non-negative right bit shift to operate as a logarithmic divisor
  90. - inline int
  91. - get_log_divisor(size_t count, unsigned log_range)
  92. - {
  93. - int log_divisor;
  94. - //If we can finish in one iteration without exceeding either (2 to the MAX_SPLITS) or n bins, do so
  95. - if((log_divisor = log_range - rough_log_2_size(count)) <= 0 && log_range < MAX_SPLITS)
  96. - log_divisor = 0;
  97. - else {
  98. - //otherwise divide the data into an optimized number of pieces
  99. - log_divisor += LOG_MEAN_BIN_SIZE;
  100. - if(log_divisor < 0)
  101. - log_divisor = 0;
  102. - //Cannot exceed MAX_SPLITS or cache misses slow down bin lookups dramatically
  103. - if((log_range - log_divisor) > MAX_SPLITS)
  104. - log_divisor = log_range - MAX_SPLITS;
  105. - }
  106. - return log_divisor;
  107. - }
  108. -
  109. - template <class RandomAccessIter>
  110. - inline RandomAccessIter *
  111. - size_bins(std::vector<size_t> &bin_sizes, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset, unsigned &cache_end, unsigned bin_count)
  112. - {
  113. - //Assure space for the size of each bin, followed by initializing sizes
  114. - if(bin_count > bin_sizes.size())
  115. - bin_sizes.resize(bin_count);
  116. - for(size_t u = 0; u < bin_count; u++)
  117. - bin_sizes[u] = 0;
  118. - //Make sure there is space for the bins
  119. - cache_end = cache_offset + bin_count;
  120. - if(cache_end > bin_cache.size())
  121. - bin_cache.resize(cache_end);
  122. - return &(bin_cache[cache_offset]);
  123. - }
  124. -
  125. - //Implementation for recursive integer sorting
  126. - template <class RandomAccessIter, class div_type, class data_type>
  127. - inline void
  128. - spread_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  129. - , std::vector<size_t> &bin_sizes)
  130. - {
  131. - //This step is roughly 10% of runtime, but it helps avoid worst-case behavior and improve behavior with real data
  132. - //If you know the maximum and minimum ahead of time, you can pass those values in and skip this step for the first iteration
  133. - RandomAccessIter max, min;
  134. - find_extremes(first, last, max, min);
  135. - //max and min will be the same (the first item) iff all values are equivalent
  136. - if(max == min)
  137. - return;
  138. - RandomAccessIter * target_bin;
  139. - unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(*max >> 0) - (*min >> 0)));
  140. - div_type div_min = *min >> log_divisor;
  141. - div_type div_max = *max >> log_divisor;
  142. - unsigned bin_count = div_max - div_min + 1;
  143. - unsigned cache_end;
  144. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  145. -
  146. - //Calculating the size of each bin; this takes roughly 10% of runtime
  147. - for (RandomAccessIter current = first; current != last;)
  148. - bin_sizes[(*(current++) >> log_divisor) - div_min]++;
  149. - //Assign the bin positions
  150. - bins[0] = first;
  151. - for(unsigned u = 0; u < bin_count - 1; u++)
  152. - bins[u + 1] = bins[u] + bin_sizes[u];
  153. -
  154. - //Swap into place
  155. - //This dominates runtime, mostly in the swap and bin lookups
  156. - RandomAccessIter nextbinstart = first;
  157. - for(unsigned u = 0; u < bin_count - 1; ++u) {
  158. - RandomAccessIter * local_bin = bins + u;
  159. - nextbinstart += bin_sizes[u];
  160. - //Iterating over each element in this bin
  161. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  162. - //Swapping elements in current into place until the correct element has been swapped in
  163. - for(target_bin = (bins + ((*current >> log_divisor) - div_min)); target_bin != local_bin;
  164. - target_bin = bins + ((*current >> log_divisor) - div_min)) {
  165. - //3-way swap; this is about 1% faster than a 2-way swap with integers
  166. - //The main advantage is less copies are involved per item put in the correct place
  167. - data_type tmp;
  168. - RandomAccessIter b = (*target_bin)++;
  169. - RandomAccessIter * b_bin = bins + ((*b >> log_divisor) - div_min);
  170. - if (b_bin != local_bin) {
  171. - RandomAccessIter c = (*b_bin)++;
  172. - tmp = *c;
  173. - *c = *b;
  174. - }
  175. - else
  176. - tmp = *b;
  177. - *b = *current;
  178. - *current = tmp;
  179. - }
  180. - }
  181. - *local_bin = nextbinstart;
  182. - }
  183. - bins[bin_count - 1] = last;
  184. -
  185. - //If we've bucketsorted, the array is sorted and we should skip recursion
  186. - if(!log_divisor)
  187. - return;
  188. -
  189. - //Recursing; log_divisor is the remaining range
  190. - size_t max_count = get_max_count(log_divisor, last - first);
  191. - RandomAccessIter lastPos = first;
  192. - for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  193. - size_t count = bin_cache[u] - lastPos;
  194. - //don't sort unless there are at least two items to compare
  195. - if(count < 2)
  196. - continue;
  197. - //using std::sort if its worst-case is better
  198. - if(count < max_count)
  199. - std::sort(lastPos, bin_cache[u]);
  200. - else
  201. - spread_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes);
  202. - }
  203. - }
  204. -
  205. - //Generic bitshift-based 3-way swapping code
  206. - template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  207. - inline void inner_swap_loop(RandomAccessIter * bins, const RandomAccessIter & nextbinstart, unsigned ii, right_shift &shift
  208. - , const unsigned log_divisor, const div_type div_min)
  209. - {
  210. - RandomAccessIter * local_bin = bins + ii;
  211. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  212. - for(RandomAccessIter * target_bin = (bins + (shift(*current, log_divisor) - div_min)); target_bin != local_bin;
  213. - target_bin = bins + (shift(*current, log_divisor) - div_min)) {
  214. - data_type tmp;
  215. - RandomAccessIter b = (*target_bin)++;
  216. - RandomAccessIter * b_bin = bins + (shift(*b, log_divisor) - div_min);
  217. - //Three-way swap; if the item to be swapped doesn't belong in the current bin, swap it to where it belongs
  218. - if (b_bin != local_bin) {
  219. - RandomAccessIter c = (*b_bin)++;
  220. - tmp = *c;
  221. - *c = *b;
  222. - }
  223. - //Note: we could increment current once the swap is done in this case, but that seems to impair performance
  224. - else
  225. - tmp = *b;
  226. - *b = *current;
  227. - *current = tmp;
  228. - }
  229. - }
  230. - *local_bin = nextbinstart;
  231. - }
  232. -
  233. - //Standard swapping wrapper for ascending values
  234. - template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  235. - inline void swap_loop(RandomAccessIter * bins, RandomAccessIter & nextbinstart, unsigned ii, right_shift &shift
  236. - , const std::vector<size_t> &bin_sizes, const unsigned log_divisor, const div_type div_min)
  237. - {
  238. - nextbinstart += bin_sizes[ii];
  239. - inner_swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, log_divisor, div_min);
  240. - }
  241. -
  242. - //Functor implementation for recursive sorting
  243. - template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  244. - inline void
  245. - spread_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  246. - , std::vector<size_t> &bin_sizes, right_shift shift, compare comp)
  247. - {
  248. - RandomAccessIter max, min;
  249. - find_extremes(first, last, max, min, comp);
  250. - if(max == min)
  251. - return;
  252. - unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(shift(*max, 0)) - (shift(*min, 0))));
  253. - div_type div_min = shift(*min, log_divisor);
  254. - div_type div_max = shift(*max, log_divisor);
  255. - unsigned bin_count = div_max - div_min + 1;
  256. - unsigned cache_end;
  257. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  258. -
  259. - //Calculating the size of each bin
  260. - for (RandomAccessIter current = first; current != last;)
  261. - bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  262. - bins[0] = first;
  263. - for(unsigned u = 0; u < bin_count - 1; u++)
  264. - bins[u + 1] = bins[u] + bin_sizes[u];
  265. -
  266. - //Swap into place
  267. - RandomAccessIter nextbinstart = first;
  268. - for(unsigned u = 0; u < bin_count - 1; ++u)
  269. - swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, u, shift, bin_sizes, log_divisor, div_min);
  270. - bins[bin_count - 1] = last;
  271. -
  272. - //If we've bucketsorted, the array is sorted and we should skip recursion
  273. - if(!log_divisor)
  274. - return;
  275. -
  276. - //Recursing
  277. - size_t max_count = get_max_count(log_divisor, last - first);
  278. - RandomAccessIter lastPos = first;
  279. - for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  280. - size_t count = bin_cache[u] - lastPos;
  281. - if(count < 2)
  282. - continue;
  283. - if(count < max_count)
  284. - std::sort(lastPos, bin_cache[u], comp);
  285. - else
  286. - spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift, compare>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift, comp);
  287. - }
  288. - }
  289. -
  290. - //Functor implementation for recursive sorting with only Shift overridden
  291. - template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  292. - inline void
  293. - spread_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  294. - , std::vector<size_t> &bin_sizes, right_shift shift)
  295. - {
  296. - RandomAccessIter max, min;
  297. - find_extremes(first, last, max, min);
  298. - if(max == min)
  299. - return;
  300. - unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(shift(*max, 0)) - (shift(*min, 0))));
  301. - div_type div_min = shift(*min, log_divisor);
  302. - div_type div_max = shift(*max, log_divisor);
  303. - unsigned bin_count = div_max - div_min + 1;
  304. - unsigned cache_end;
  305. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  306. -
  307. - //Calculating the size of each bin
  308. - for (RandomAccessIter current = first; current != last;)
  309. - bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  310. - bins[0] = first;
  311. - for(unsigned u = 0; u < bin_count - 1; u++)
  312. - bins[u + 1] = bins[u] + bin_sizes[u];
  313. -
  314. - //Swap into place
  315. - RandomAccessIter nextbinstart = first;
  316. - for(unsigned ii = 0; ii < bin_count - 1; ++ii)
  317. - swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, bin_sizes, log_divisor, div_min);
  318. - bins[bin_count - 1] = last;
  319. -
  320. - //If we've bucketsorted, the array is sorted and we should skip recursion
  321. - if(!log_divisor)
  322. - return;
  323. -
  324. - //Recursing
  325. - size_t max_count = get_max_count(log_divisor, last - first);
  326. - RandomAccessIter lastPos = first;
  327. - for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  328. - size_t count = bin_cache[u] - lastPos;
  329. - if(count < 2)
  330. - continue;
  331. - if(count < max_count)
  332. - std::sort(lastPos, bin_cache[u]);
  333. - else
  334. - spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift);
  335. - }
  336. - }
  337. -
  338. - //Holds the bin vector and makes the initial recursive call
  339. - template <class RandomAccessIter, class div_type, class data_type>
  340. - inline void
  341. - spread_sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type)
  342. - {
  343. - std::vector<size_t> bin_sizes;
  344. - std::vector<RandomAccessIter> bin_cache;
  345. - spread_sort_rec<RandomAccessIter, div_type, data_type>(first, last, bin_cache, 0, bin_sizes);
  346. - }
  347. -
  348. - template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  349. - inline void
  350. - spread_sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift, compare comp)
  351. - {
  352. - std::vector<size_t> bin_sizes;
  353. - std::vector<RandomAccessIter> bin_cache;
  354. - spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift, compare>(first, last, bin_cache, 0, bin_sizes, shift, comp);
  355. - }
  356. -
  357. - template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  358. - inline void
  359. - spread_sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift)
  360. - {
  361. - std::vector<size_t> bin_sizes;
  362. - std::vector<RandomAccessIter> bin_cache;
  363. - spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(first, last, bin_cache, 0, bin_sizes, shift);
  364. - }
  365. - }
  366. -
  367. - //Top-level sorting call for integers
  368. - template <class RandomAccessIter>
  369. - inline void integer_sort(RandomAccessIter first, RandomAccessIter last)
  370. - {
  371. - //Don't sort if it's too small to optimize
  372. - if(last - first < detail::MIN_SORT_SIZE)
  373. - std::sort(first, last);
  374. - else
  375. - detail::spread_sort(first, last, *first >> 0, *first);
  376. - }
  377. -
  378. - //integer_sort with functors
  379. - template <class RandomAccessIter, class right_shift, class compare>
  380. - inline void integer_sort(RandomAccessIter first, RandomAccessIter last,
  381. - right_shift shift, compare comp) {
  382. - if(last - first < detail::MIN_SORT_SIZE)
  383. - std::sort(first, last, comp);
  384. - else
  385. - detail::spread_sort(first, last, shift(*first, 0), *first, shift, comp);
  386. - }
  387. -
  388. - //integer_sort with right_shift functor
  389. - template <class RandomAccessIter, class right_shift>
  390. - inline void integer_sort(RandomAccessIter first, RandomAccessIter last,
  391. - right_shift shift) {
  392. - if(last - first < detail::MIN_SORT_SIZE)
  393. - std::sort(first, last);
  394. - else
  395. - detail::spread_sort(first, last, shift(*first, 0), *first, shift);
  396. - }
  397. -
  398. - //------------------------------------------------------ float_sort source --------------------------------------
  399. - //Casts a RandomAccessIter to the specified data type
  400. - template<class cast_type, class RandomAccessIter>
  401. - inline cast_type
  402. - cast_float_iter(const RandomAccessIter & floatiter)
  403. - {
  404. - cast_type result;
  405. - std::memcpy(&result, &(*floatiter), sizeof(cast_type));
  406. - return result;
  407. - }
  408. -
  409. - //Casts a data element to the specified datinner_float_a type
  410. - template<class data_type, class cast_type>
  411. - inline cast_type
  412. - mem_cast(const data_type & data)
  413. - {
  414. - cast_type result;
  415. - std::memcpy(&result, &data, sizeof(cast_type));
  416. - return result;
  417. - }
  418. -
  419. - namespace detail {
  420. - template <class RandomAccessIter, class div_type, class right_shift>
  421. - inline void
  422. - find_extremes(RandomAccessIter current, RandomAccessIter last, div_type & max, div_type & min, right_shift shift)
  423. - {
  424. - min = max = shift(*current, 0);
  425. - while(++current < last) {
  426. - div_type value = shift(*current, 0);
  427. - if(max < value)
  428. - max = value;
  429. - else if(value < min)
  430. - min = value;
  431. - }
  432. - }
  433. -
  434. - //Specialized swap loops for floating-point casting
  435. - template <class RandomAccessIter, class div_type, class data_type>
  436. - inline void inner_float_swap_loop(RandomAccessIter * bins, const RandomAccessIter & nextbinstart, unsigned ii
  437. - , const unsigned log_divisor, const div_type div_min)
  438. - {
  439. - RandomAccessIter * local_bin = bins + ii;
  440. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  441. - for(RandomAccessIter * target_bin = (bins + ((cast_float_iter<div_type, RandomAccessIter>(current) >> log_divisor) - div_min)); target_bin != local_bin;
  442. - target_bin = bins + ((cast_float_iter<div_type, RandomAccessIter>(current) >> log_divisor) - div_min)) {
  443. - data_type tmp;
  444. - RandomAccessIter b = (*target_bin)++;
  445. - RandomAccessIter * b_bin = bins + ((cast_float_iter<div_type, RandomAccessIter>(b) >> log_divisor) - div_min);
  446. - //Three-way swap; if the item to be swapped doesn't belong in the current bin, swap it to where it belongs
  447. - if (b_bin != local_bin) {
  448. - RandomAccessIter c = (*b_bin)++;
  449. - tmp = *c;
  450. - *c = *b;
  451. - }
  452. - else
  453. - tmp = *b;
  454. - *b = *current;
  455. - *current = tmp;
  456. - }
  457. - }
  458. - *local_bin = nextbinstart;
  459. - }
  460. -
  461. - template <class RandomAccessIter, class div_type, class data_type>
  462. - inline void float_swap_loop(RandomAccessIter * bins, RandomAccessIter & nextbinstart, unsigned ii
  463. - , const std::vector<size_t> &bin_sizes, const unsigned log_divisor, const div_type div_min)
  464. - {
  465. - nextbinstart += bin_sizes[ii];
  466. - inner_float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, ii, log_divisor, div_min);
  467. - }
  468. -
  469. - template <class RandomAccessIter, class cast_type>
  470. - inline void
  471. - find_extremes(RandomAccessIter current, RandomAccessIter last, cast_type & max, cast_type & min)
  472. - {
  473. - min = max = cast_float_iter<cast_type, RandomAccessIter>(current);
  474. - while(++current < last) {
  475. - cast_type value = cast_float_iter<cast_type, RandomAccessIter>(current);
  476. - if(max < value)
  477. - max = value;
  478. - else if(value < min)
  479. - min = value;
  480. - }
  481. - }
  482. -
  483. - //Special-case sorting of positive floats with casting instead of a right_shift
  484. - template <class RandomAccessIter, class div_type, class data_type>
  485. - inline void
  486. - positive_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  487. - , std::vector<size_t> &bin_sizes)
  488. - {
  489. - div_type max, min;
  490. - find_extremes(first, last, max, min);
  491. - if(max == min)
  492. - return;
  493. - unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  494. - div_type div_min = min >> log_divisor;
  495. - div_type div_max = max >> log_divisor;
  496. - unsigned bin_count = div_max - div_min + 1;
  497. - unsigned cache_end;
  498. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  499. -
  500. - //Calculating the size of each bin
  501. - for (RandomAccessIter current = first; current != last;)
  502. - bin_sizes[(cast_float_iter<div_type, RandomAccessIter>(current++) >> log_divisor) - div_min]++;
  503. - bins[0] = first;
  504. - for(unsigned u = 0; u < bin_count - 1; u++)
  505. - bins[u + 1] = bins[u] + bin_sizes[u];
  506. -
  507. - //Swap into place
  508. - RandomAccessIter nextbinstart = first;
  509. - for(unsigned u = 0; u < bin_count - 1; ++u)
  510. - float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, u, bin_sizes, log_divisor, div_min);
  511. - bins[bin_count - 1] = last;
  512. -
  513. - //Return if we've completed bucketsorting
  514. - if(!log_divisor)
  515. - return;
  516. -
  517. - //Recursing
  518. - size_t max_count = get_max_count(log_divisor, last - first);
  519. - RandomAccessIter lastPos = first;
  520. - for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  521. - size_t count = bin_cache[u] - lastPos;
  522. - if(count < 2)
  523. - continue;
  524. - if(count < max_count)
  525. - std::sort(lastPos, bin_cache[u]);
  526. - else
  527. - positive_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes);
  528. - }
  529. - }
  530. -
  531. - //Sorting negative_ float_s
  532. - //Note that bins are iterated in reverse order because max_neg_float = min_neg_int
  533. - template <class RandomAccessIter, class div_type, class data_type>
  534. - inline void
  535. - negative_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  536. - , std::vector<size_t> &bin_sizes)
  537. - {
  538. - div_type max, min;
  539. - find_extremes(first, last, max, min);
  540. - if(max == min)
  541. - return;
  542. - unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  543. - div_type div_min = min >> log_divisor;
  544. - div_type div_max = max >> log_divisor;
  545. - unsigned bin_count = div_max - div_min + 1;
  546. - unsigned cache_end;
  547. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  548. -
  549. - //Calculating the size of each bin
  550. - for (RandomAccessIter current = first; current != last;)
  551. - bin_sizes[(cast_float_iter<div_type, RandomAccessIter>(current++) >> log_divisor) - div_min]++;
  552. - bins[bin_count - 1] = first;
  553. - for(int ii = bin_count - 2; ii >= 0; --ii)
  554. - bins[ii] = bins[ii + 1] + bin_sizes[ii + 1];
  555. -
  556. - //Swap into place
  557. - RandomAccessIter nextbinstart = first;
  558. - //The last bin will always have the correct elements in it
  559. - for(int ii = bin_count - 1; ii > 0; --ii)
  560. - float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, ii, bin_sizes, log_divisor, div_min);
  561. - //Since we don't process the last bin, we need to update its end position
  562. - bin_cache[cache_offset] = last;
  563. -
  564. - //Return if we've completed bucketsorting
  565. - if(!log_divisor)
  566. - return;
  567. -
  568. - //Recursing
  569. - size_t max_count = get_max_count(log_divisor, last - first);
  570. - RandomAccessIter lastPos = first;
  571. - for(int ii = cache_end - 1; ii >= (int)cache_offset; lastPos = bin_cache[ii], --ii) {
  572. - size_t count = bin_cache[ii] - lastPos;
  573. - if(count < 2)
  574. - continue;
  575. - if(count < max_count)
  576. - std::sort(lastPos, bin_cache[ii]);
  577. - else
  578. - negative_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes);
  579. - }
  580. - }
  581. -
  582. - //Sorting negative_ float_s
  583. - //Note that bins are iterated in reverse order because max_neg_float = min_neg_int
  584. - template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  585. - inline void
  586. - negative_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  587. - , std::vector<size_t> &bin_sizes, right_shift shift)
  588. - {
  589. - div_type max, min;
  590. - find_extremes(first, last, max, min, shift);
  591. - if(max == min)
  592. - return;
  593. - unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  594. - div_type div_min = min >> log_divisor;
  595. - div_type div_max = max >> log_divisor;
  596. - unsigned bin_count = div_max - div_min + 1;
  597. - unsigned cache_end;
  598. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  599. -
  600. - //Calculating the size of each bin
  601. - for (RandomAccessIter current = first; current != last;)
  602. - bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  603. - bins[bin_count - 1] = first;
  604. - for(int ii = bin_count - 2; ii >= 0; --ii)
  605. - bins[ii] = bins[ii + 1] + bin_sizes[ii + 1];
  606. -
  607. - //Swap into place
  608. - RandomAccessIter nextbinstart = first;
  609. - //The last bin will always have the correct elements in it
  610. - for(int ii = bin_count - 1; ii > 0; --ii)
  611. - swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, bin_sizes, log_divisor, div_min);
  612. - //Since we don't process the last bin, we need to update its end position
  613. - bin_cache[cache_offset] = last;
  614. -
  615. - //Return if we've completed bucketsorting
  616. - if(!log_divisor)
  617. - return;
  618. -
  619. - //Recursing
  620. - size_t max_count = get_max_count(log_divisor, last - first);
  621. - RandomAccessIter lastPos = first;
  622. - for(int ii = cache_end - 1; ii >= (int)cache_offset; lastPos = bin_cache[ii], --ii) {
  623. - size_t count = bin_cache[ii] - lastPos;
  624. - if(count < 2)
  625. - continue;
  626. - if(count < max_count)
  627. - std::sort(lastPos, bin_cache[ii]);
  628. - else
  629. - negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift);
  630. - }
  631. - }
  632. -
  633. - template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  634. - inline void
  635. - negative_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  636. - , std::vector<size_t> &bin_sizes, right_shift shift, compare comp)
  637. - {
  638. - div_type max, min;
  639. - find_extremes(first, last, max, min, shift);
  640. - if(max == min)
  641. - return;
  642. - unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  643. - div_type div_min = min >> log_divisor;
  644. - div_type div_max = max >> log_divisor;
  645. - unsigned bin_count = div_max - div_min + 1;
  646. - unsigned cache_end;
  647. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  648. -
  649. - //Calculating the size of each bin
  650. - for (RandomAccessIter current = first; current != last;)
  651. - bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  652. - bins[bin_count - 1] = first;
  653. - for(int ii = bin_count - 2; ii >= 0; --ii)
  654. - bins[ii] = bins[ii + 1] + bin_sizes[ii + 1];
  655. -
  656. - //Swap into place
  657. - RandomAccessIter nextbinstart = first;
  658. - //The last bin will always have the correct elements in it
  659. - for(int ii = bin_count - 1; ii > 0; --ii)
  660. - swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, bin_sizes, log_divisor, div_min);
  661. - //Since we don't process the last bin, we need to update its end position
  662. - bin_cache[cache_offset] = last;
  663. -
  664. - //Return if we've completed bucketsorting
  665. - if(!log_divisor)
  666. - return;
  667. -
  668. - //Recursing
  669. - size_t max_count = get_max_count(log_divisor, last - first);
  670. - RandomAccessIter lastPos = first;
  671. - for(int ii = cache_end - 1; ii >= (int)cache_offset; lastPos = bin_cache[ii], --ii) {
  672. - size_t count = bin_cache[ii] - lastPos;
  673. - if(count < 2)
  674. - continue;
  675. - if(count < max_count)
  676. - std::sort(lastPos, bin_cache[ii], comp);
  677. - else
  678. - negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift, compare>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift, comp);
  679. - }
  680. - }
  681. -
  682. - //Casting special-case for floating-point sorting
  683. - template <class RandomAccessIter, class div_type, class data_type>
  684. - inline void
  685. - float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  686. - , std::vector<size_t> &bin_sizes)
  687. - {
  688. - div_type max, min;
  689. - find_extremes(first, last, max, min);
  690. - if(max == min)
  691. - return;
  692. - unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  693. - div_type div_min = min >> log_divisor;
  694. - div_type div_max = max >> log_divisor;
  695. - unsigned bin_count = div_max - div_min + 1;
  696. - unsigned cache_end;
  697. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  698. -
  699. - //Calculating the size of each bin
  700. - for (RandomAccessIter current = first; current != last;)
  701. - bin_sizes[(cast_float_iter<div_type, RandomAccessIter>(current++) >> log_divisor) - div_min]++;
  702. - //The index of the first positive bin
  703. - div_type first_positive = (div_min < 0) ? -div_min : 0;
  704. - //Resetting if all bins are negative
  705. - if(cache_offset + first_positive > cache_end)
  706. - first_positive = cache_end - cache_offset;
  707. - //Reversing the order of the negative bins
  708. - //Note that because of the negative/positive ordering direction flip
  709. - //We can not depend upon bin order and positions matching up
  710. - //so bin_sizes must be reused to contain the end of the bin
  711. - if(first_positive > 0) {
  712. - bins[first_positive - 1] = first;
  713. - for(int ii = first_positive - 2; ii >= 0; --ii) {
  714. - bins[ii] = first + bin_sizes[ii + 1];
  715. - bin_sizes[ii] += bin_sizes[ii + 1];
  716. - }
  717. - //Handling positives following negatives
  718. - if((unsigned)first_positive < bin_count) {
  719. - bins[first_positive] = first + bin_sizes[0];
  720. - bin_sizes[first_positive] += bin_sizes[0];
  721. - }
  722. - }
  723. - else
  724. - bins[0] = first;
  725. - for(unsigned u = first_positive; u < bin_count - 1; u++) {
  726. - bins[u + 1] = first + bin_sizes[u];
  727. - bin_sizes[u + 1] += bin_sizes[u];
  728. - }
  729. -
  730. - //Swap into place
  731. - RandomAccessIter nextbinstart = first;
  732. - for(unsigned u = 0; u < bin_count; ++u) {
  733. - nextbinstart = first + bin_sizes[u];
  734. - inner_float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, u, log_divisor, div_min);
  735. - }
  736. -
  737. - if(!log_divisor)
  738. - return;
  739. -
  740. - //Handling negative values first
  741. - size_t max_count = get_max_count(log_divisor, last - first);
  742. - RandomAccessIter lastPos = first;
  743. - for(int ii = cache_offset + first_positive - 1; ii >= (int)cache_offset ; lastPos = bin_cache[ii--]) {
  744. - size_t count = bin_cache[ii] - lastPos;
  745. - if(count < 2)
  746. - continue;
  747. - if(count < max_count)
  748. - std::sort(lastPos, bin_cache[ii]);
  749. - //sort negative values using reversed-bin spread_sort
  750. - else
  751. - negative_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes);
  752. - }
  753. -
  754. - for(unsigned u = cache_offset + first_positive; u < cache_end; lastPos = bin_cache[u], ++u) {
  755. - size_t count = bin_cache[u] - lastPos;
  756. - if(count < 2)
  757. - continue;
  758. - if(count < max_count)
  759. - std::sort(lastPos, bin_cache[u]);
  760. - //sort positive values using normal spread_sort
  761. - else
  762. - positive_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes);
  763. - }
  764. - }
  765. -
  766. - //Functor implementation for recursive sorting
  767. - template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  768. - inline void
  769. - float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  770. - , std::vector<size_t> &bin_sizes, right_shift shift)
  771. - {
  772. - div_type max, min;
  773. - find_extremes(first, last, max, min, shift);
  774. - if(max == min)
  775. - return;
  776. - unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  777. - div_type div_min = min >> log_divisor;
  778. - div_type div_max = max >> log_divisor;
  779. - unsigned bin_count = div_max - div_min + 1;
  780. - unsigned cache_end;
  781. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  782. -
  783. - //Calculating the size of each bin
  784. - for (RandomAccessIter current = first; current != last;)
  785. - bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  786. - //The index of the first positive bin
  787. - div_type first_positive = (div_min < 0) ? -div_min : 0;
  788. - //Resetting if all bins are negative
  789. - if(cache_offset + first_positive > cache_end)
  790. - first_positive = cache_end - cache_offset;
  791. - //Reversing the order of the negative bins
  792. - //Note that because of the negative/positive ordering direction flip
  793. - //We can not depend upon bin order and positions matching up
  794. - //so bin_sizes must be reused to contain the end of the bin
  795. - if(first_positive > 0) {
  796. - bins[first_positive - 1] = first;
  797. - for(int ii = first_positive - 2; ii >= 0; --ii) {
  798. - bins[ii] = first + bin_sizes[ii + 1];
  799. - bin_sizes[ii] += bin_sizes[ii + 1];
  800. - }
  801. - //Handling positives following negatives
  802. - if((unsigned)first_positive < bin_count) {
  803. - bins[first_positive] = first + bin_sizes[0];
  804. - bin_sizes[first_positive] += bin_sizes[0];
  805. - }
  806. - }
  807. - else
  808. - bins[0] = first;
  809. - for(unsigned u = first_positive; u < bin_count - 1; u++) {
  810. - bins[u + 1] = first + bin_sizes[u];
  811. - bin_sizes[u + 1] += bin_sizes[u];
  812. - }
  813. -
  814. - //Swap into place
  815. - RandomAccessIter nextbinstart = first;
  816. - for(unsigned u = 0; u < bin_count; ++u) {
  817. - nextbinstart = first + bin_sizes[u];
  818. - inner_swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, u, shift, log_divisor, div_min);
  819. - }
  820. -
  821. - //Return if we've completed bucketsorting
  822. - if(!log_divisor)
  823. - return;
  824. -
  825. - //Handling negative values first
  826. - size_t max_count = get_max_count(log_divisor, last - first);
  827. - RandomAccessIter lastPos = first;
  828. - for(int ii = cache_offset + first_positive - 1; ii >= (int)cache_offset ; lastPos = bin_cache[ii--]) {
  829. - size_t count = bin_cache[ii] - lastPos;
  830. - if(count < 2)
  831. - continue;
  832. - if(count < max_count)
  833. - std::sort(lastPos, bin_cache[ii]);
  834. - //sort negative values using reversed-bin spread_sort
  835. - else
  836. - negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift);
  837. - }
  838. -
  839. - for(unsigned u = cache_offset + first_positive; u < cache_end; lastPos = bin_cache[u], ++u) {
  840. - size_t count = bin_cache[u] - lastPos;
  841. - if(count < 2)
  842. - continue;
  843. - if(count < max_count)
  844. - std::sort(lastPos, bin_cache[u]);
  845. - //sort positive values using normal spread_sort
  846. - else
  847. - spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift);
  848. - }
  849. - }
  850. -
  851. - template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  852. - inline void
  853. - float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  854. - , std::vector<size_t> &bin_sizes, right_shift shift, compare comp)
  855. - {
  856. - div_type max, min;
  857. - find_extremes(first, last, max, min, shift);
  858. - if(max == min)
  859. - return;
  860. - unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  861. - div_type div_min = min >> log_divisor;
  862. - div_type div_max = max >> log_divisor;
  863. - unsigned bin_count = div_max - div_min + 1;
  864. - unsigned cache_end;
  865. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  866. -
  867. - //Calculating the size of each bin
  868. - for (RandomAccessIter current = first; current != last;)
  869. - bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  870. - //The index of the first positive bin
  871. - div_type first_positive = (div_min < 0) ? -div_min : 0;
  872. - //Resetting if all bins are negative
  873. - if(cache_offset + first_positive > cache_end)
  874. - first_positive = cache_end - cache_offset;
  875. - //Reversing the order of the negative bins
  876. - //Note that because of the negative/positive ordering direction flip
  877. - //We can not depend upon bin order and positions matching up
  878. - //so bin_sizes must be reused to contain the end of the bin
  879. - if(first_positive > 0) {
  880. - bins[first_positive - 1] = first;
  881. - for(int ii = first_positive - 2; ii >= 0; --ii) {
  882. - bins[ii] = first + bin_sizes[ii + 1];
  883. - bin_sizes[ii] += bin_sizes[ii + 1];
  884. - }
  885. - //Handling positives following negatives
  886. - if((unsigned)first_positive < bin_count) {
  887. - bins[first_positive] = first + bin_sizes[0];
  888. - bin_sizes[first_positive] += bin_sizes[0];
  889. - }
  890. - }
  891. - else
  892. - bins[0] = first;
  893. - for(unsigned u = first_positive; u < bin_count - 1; u++) {
  894. - bins[u + 1] = first + bin_sizes[u];
  895. - bin_sizes[u + 1] += bin_sizes[u];
  896. - }
  897. -
  898. - //Swap into place
  899. - RandomAccessIter nextbinstart = first;
  900. - for(unsigned u = 0; u < bin_count; ++u) {
  901. - nextbinstart = first + bin_sizes[u];
  902. - inner_swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, u, shift, log_divisor, div_min);
  903. - }
  904. -
  905. - //Return if we've completed bucketsorting
  906. - if(!log_divisor)
  907. - return;
  908. -
  909. - //Handling negative values first
  910. - size_t max_count = get_max_count(log_divisor, last - first);
  911. - RandomAccessIter lastPos = first;
  912. - for(int ii = cache_offset + first_positive - 1; ii >= (int)cache_offset ; lastPos = bin_cache[ii--]) {
  913. - size_t count = bin_cache[ii] - lastPos;
  914. - if(count < 2)
  915. - continue;
  916. - if(count < max_count)
  917. - std::sort(lastPos, bin_cache[ii]);
  918. - //sort negative values using reversed-bin spread_sort
  919. - else
  920. - negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift, comp);
  921. - }
  922. -
  923. - for(unsigned u = cache_offset + first_positive; u < cache_end; lastPos = bin_cache[u], ++u) {
  924. - size_t count = bin_cache[u] - lastPos;
  925. - if(count < 2)
  926. - continue;
  927. - if(count < max_count)
  928. - std::sort(lastPos, bin_cache[u]);
  929. - //sort positive values using normal spread_sort
  930. - else
  931. - spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift, comp);
  932. - }
  933. - }
  934. -
  935. - template <class RandomAccessIter, class cast_type, class data_type>
  936. - inline void
  937. - float_Sort(RandomAccessIter first, RandomAccessIter last, cast_type, data_type)
  938. - {
  939. - std::vector<size_t> bin_sizes;
  940. - std::vector<RandomAccessIter> bin_cache;
  941. - float_sort_rec<RandomAccessIter, cast_type, data_type>(first, last, bin_cache, 0, bin_sizes);
  942. - }
  943. -
  944. - template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  945. - inline void
  946. - float_Sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift)
  947. - {
  948. - std::vector<size_t> bin_sizes;
  949. - std::vector<RandomAccessIter> bin_cache;
  950. - float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(first, last, bin_cache, 0, bin_sizes, shift);
  951. - }
  952. -
  953. - template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  954. - inline void
  955. - float_Sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift, compare comp)
  956. - {
  957. - std::vector<size_t> bin_sizes;
  958. - std::vector<RandomAccessIter> bin_cache;
  959. - float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(first, last, bin_cache, 0, bin_sizes, shift, comp);
  960. - }
  961. - }
  962. -
  963. - //float_sort with casting
  964. - //The cast_type must be equal in size to the data type, and must be a signed integer
  965. - template <class RandomAccessIter, class cast_type>
  966. - inline void float_sort_cast(RandomAccessIter first, RandomAccessIter last, cast_type cVal)
  967. - {
  968. - if(last - first < detail::MIN_SORT_SIZE)
  969. - std::sort(first, last);
  970. - else
  971. - detail::float_Sort(first, last, cVal, *first);
  972. - }
  973. -
  974. - //float_sort with casting to an int
  975. - //Only use this with IEEE floating-point numbers
  976. - template <class RandomAccessIter>
  977. - inline void float_sort_cast_to_int(RandomAccessIter first, RandomAccessIter last)
  978. - {
  979. - int cVal = 0;
  980. - float_sort_cast(first, last, cVal);
  981. - }
  982. -
  983. - //float_sort with functors
  984. - template <class RandomAccessIter, class right_shift>
  985. - inline void float_sort(RandomAccessIter first, RandomAccessIter last, right_shift shift)
  986. - {
  987. - if(last - first < detail::MIN_SORT_SIZE)
  988. - std::sort(first, last);
  989. - else
  990. - detail::float_Sort(first, last, shift(*first, 0), *first, shift);
  991. - }
  992. -
  993. - template <class RandomAccessIter, class right_shift, class compare>
  994. - inline void float_sort(RandomAccessIter first, RandomAccessIter last, right_shift shift, compare comp)
  995. - {
  996. - if(last - first < detail::MIN_SORT_SIZE)
  997. - std::sort(first, last, comp);
  998. - else
  999. - detail::float_Sort(first, last, shift(*first, 0), *first, shift, comp);
  1000. - }
  1001. -
  1002. - //------------------------------------------------- string_sort source ---------------------------------------------
  1003. - namespace detail {
  1004. - //Offsetting on identical characters. This function works a character at a time for optimal worst-case performance.
  1005. - template<class RandomAccessIter>
  1006. - inline void
  1007. - update_offset(RandomAccessIter first, RandomAccessIter finish, unsigned &char_offset)
  1008. - {
  1009. - unsigned nextOffset = char_offset;
  1010. - bool done = false;
  1011. - while(!done) {
  1012. - RandomAccessIter curr = first;
  1013. - do {
  1014. - //ignore empties, but if the nextOffset would exceed the length or not match, exit; we've found the last matching character
  1015. - if((*curr).size() > char_offset && ((*curr).size() <= (nextOffset + 1) || (*curr)[nextOffset] != (*first)[nextOffset])) {
  1016. - done = true;
  1017. - break;
  1018. - }
  1019. - } while(++curr != finish);
  1020. - if(!done)
  1021. - ++nextOffset;
  1022. - }
  1023. - char_offset = nextOffset;
  1024. - }
  1025. -
  1026. - //Offsetting on identical characters. This function works a character at a time for optimal worst-case performance.
  1027. - template<class RandomAccessIter, class get_char, class get_length>
  1028. - inline void
  1029. - update_offset(RandomAccessIter first, RandomAccessIter finish, unsigned &char_offset, get_char getchar, get_length length)
  1030. - {
  1031. - unsigned nextOffset = char_offset;
  1032. - bool done = false;
  1033. - while(!done) {
  1034. - RandomAccessIter curr = first;
  1035. - do {
  1036. - //ignore empties, but if the nextOffset would exceed the length or not match, exit; we've found the last matching character
  1037. - if(length(*curr) > char_offset && (length(*curr) <= (nextOffset + 1) || getchar((*curr), nextOffset) != getchar((*first), nextOffset))) {
  1038. - done = true;
  1039. - break;
  1040. - }
  1041. - } while(++curr != finish);
  1042. - if(!done)
  1043. - ++nextOffset;
  1044. - }
  1045. - char_offset = nextOffset;
  1046. - }
  1047. -
  1048. - //A comparison functor for strings that assumes they are identical up to char_offset
  1049. - template<class data_type, class unsignedchar_type>
  1050. - struct offset_lessthan {
  1051. - offset_lessthan(unsigned char_offset) : fchar_offset(char_offset){}
  1052. - inline bool operator()(const data_type &x, const data_type &y) const
  1053. - {
  1054. - unsigned minSize = std::min(x.size(), y.size());
  1055. - for(unsigned u = fchar_offset; u < minSize; ++u) {
  1056. - if(static_cast<unsignedchar_type>(x[u]) < static_cast<unsignedchar_type>(y[u]))
  1057. - return true;
  1058. - else if(static_cast<unsignedchar_type>(y[u]) < static_cast<unsignedchar_type>(x[u]))
  1059. - return false;
  1060. - }
  1061. - return x.size() < y.size();
  1062. - }
  1063. - unsigned fchar_offset;
  1064. - };
  1065. -
  1066. - //A comparison functor for strings that assumes they are identical up to char_offset
  1067. - template<class data_type, class unsignedchar_type>
  1068. - struct offset_greaterthan {
  1069. - offset_greaterthan(unsigned char_offset) : fchar_offset(char_offset){}
  1070. - inline bool operator()(const data_type &x, const data_type &y) const
  1071. - {
  1072. - unsigned minSize = std::min(x.size(), y.size());
  1073. - for(unsigned u = fchar_offset; u < minSize; ++u) {
  1074. - if(static_cast<unsignedchar_type>(x[u]) > static_cast<unsignedchar_type>(y[u]))
  1075. - return true;
  1076. - else if(static_cast<unsignedchar_type>(y[u]) > static_cast<unsignedchar_type>(x[u]))
  1077. - return false;
  1078. - }
  1079. - return x.size() > y.size();
  1080. - }
  1081. - unsigned fchar_offset;
  1082. - };
  1083. -
  1084. - //A comparison functor for strings that assumes they are identical up to char_offset
  1085. - template<class data_type, class get_char, class get_length>
  1086. - struct offset_char_lessthan {
  1087. - offset_char_lessthan(unsigned char_offset) : fchar_offset(char_offset){}
  1088. - inline bool operator()(const data_type &x, const data_type &y) const
  1089. - {
  1090. - unsigned minSize = std::min(length(x), length(y));
  1091. - for(unsigned u = fchar_offset; u < minSize; ++u) {
  1092. - if(getchar(x, u) < getchar(y, u))
  1093. - return true;
  1094. - else if(getchar(y, u) < getchar(x, u))
  1095. - return false;
  1096. - }
  1097. - return length(x) < length(y);
  1098. - }
  1099. - unsigned fchar_offset;
  1100. - get_char getchar;
  1101. - get_length length;
  1102. - };
  1103. -
  1104. - //String sorting recursive implementation
  1105. - template <class RandomAccessIter, class data_type, class unsignedchar_type>
  1106. - inline void
  1107. - string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  1108. - , unsigned cache_offset, std::vector<size_t> &bin_sizes)
  1109. - {
  1110. - //This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  1111. - //Iterate to the end of the empties. If all empty, return
  1112. - while((*first).size() <= char_offset) {
  1113. - if(++first == last)
  1114. - return;
  1115. - }
  1116. - RandomAccessIter finish = last - 1;
  1117. - //Getting the last non-empty
  1118. - for(;(*finish).size() <= char_offset; --finish) { }
  1119. - ++finish;
  1120. - //Offsetting on identical characters. This section works a character at a time for optimal worst-case performance.
  1121. - update_offset(first, finish, char_offset);
  1122. -
  1123. - const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  1124. - //Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  1125. - const unsigned max_size = bin_count;
  1126. - const unsigned membin_count = bin_count + 1;
  1127. - unsigned cache_end;
  1128. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count) + 1;
  1129. -
  1130. - //Calculating the size of each bin; this takes roughly 10% of runtime
  1131. - for (RandomAccessIter current = first; current != last; ++current) {
  1132. - if((*current).size() <= char_offset) {
  1133. - bin_sizes[0]++;
  1134. - }
  1135. - else
  1136. - bin_sizes[static_cast<unsignedchar_type>((*current)[char_offset]) + 1]++;
  1137. - }
  1138. - //Assign the bin positions
  1139. - bin_cache[cache_offset] = first;
  1140. - for(unsigned u = 0; u < membin_count - 1; u++)
  1141. - bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  1142. -
  1143. - //Swap into place
  1144. - RandomAccessIter nextbinstart = first;
  1145. - //handling empty bins
  1146. - RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
  1147. - nextbinstart += bin_sizes[0];
  1148. - RandomAccessIter * target_bin;
  1149. - //Iterating over each element in the bin of empties
  1150. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1151. - //empties belong in this bin
  1152. - while((*current).size() > char_offset) {
  1153. - target_bin = bins + static_cast<unsignedchar_type>((*current)[char_offset]);
  1154. - iter_swap(current, (*target_bin)++);
  1155. - }
  1156. - }
  1157. - *local_bin = nextbinstart;
  1158. - //iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  1159. - unsigned last_bin = bin_count - 1;
  1160. - for(; last_bin && !bin_sizes[last_bin + 1]; --last_bin) { }
  1161. - //This dominates runtime, mostly in the swap and bin lookups
  1162. - for(unsigned u = 0; u < last_bin; ++u) {
  1163. - local_bin = bins + u;
  1164. - nextbinstart += bin_sizes[u + 1];
  1165. - //Iterating over each element in this bin
  1166. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1167. - //Swapping elements in current into place until the correct element has been swapped in
  1168. - for(target_bin = bins + static_cast<unsignedchar_type>((*current)[char_offset]); target_bin != local_bin;
  1169. - target_bin = bins + static_cast<unsignedchar_type>((*current)[char_offset]))
  1170. - iter_swap(current, (*target_bin)++);
  1171. - }
  1172. - *local_bin = nextbinstart;
  1173. - }
  1174. - bins[last_bin] = last;
  1175. - //Recursing
  1176. - RandomAccessIter lastPos = bin_cache[cache_offset];
  1177. - //Skip this loop for empties
  1178. - for(unsigned u = cache_offset + 1; u < cache_offset + last_bin + 2; lastPos = bin_cache[u], ++u) {
  1179. - size_t count = bin_cache[u] - lastPos;
  1180. - //don't sort unless there are at least two items to compare
  1181. - if(count < 2)
  1182. - continue;
  1183. - //using std::sort if its worst-case is better
  1184. - if(count < max_size)
  1185. - std::sort(lastPos, bin_cache[u], offset_lessthan<data_type, unsignedchar_type>(char_offset + 1));
  1186. - else
  1187. - string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(lastPos, bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes);
  1188. - }
  1189. - }
  1190. -
  1191. - //Sorts strings in reverse order, with empties at the end
  1192. - template <class RandomAccessIter, class data_type, class unsignedchar_type>
  1193. - inline void
  1194. - reverse_string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  1195. - , unsigned cache_offset, std::vector<size_t> &bin_sizes)
  1196. - {
  1197. - //This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  1198. - RandomAccessIter curr = first;
  1199. - //Iterate to the end of the empties. If all empty, return
  1200. - while((*curr).size() <= char_offset) {
  1201. - if(++curr == last)
  1202. - return;
  1203. - }
  1204. - //Getting the last non-empty
  1205. - while((*(--last)).size() <= char_offset) { }
  1206. - ++last;
  1207. - //Offsetting on identical characters. This section works a character at a time for optimal worst-case performance.
  1208. - update_offset(curr, last, char_offset);
  1209. - RandomAccessIter * target_bin;
  1210. -
  1211. - const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  1212. - //Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  1213. - const unsigned max_size = bin_count;
  1214. - const unsigned membin_count = bin_count + 1;
  1215. - const unsigned max_bin = bin_count - 1;
  1216. - unsigned cache_end;
  1217. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count);
  1218. - RandomAccessIter * end_bin = &(bin_cache[cache_offset + max_bin]);
  1219. -
  1220. - //Calculating the size of each bin; this takes roughly 10% of runtime
  1221. - for (RandomAccessIter current = first; current != last; ++current) {
  1222. - if((*current).size() <= char_offset) {
  1223. - bin_sizes[bin_count]++;
  1224. - }
  1225. - else
  1226. - bin_sizes[max_bin - static_cast<unsignedchar_type>((*current)[char_offset])]++;
  1227. - }
  1228. - //Assign the bin positions
  1229. - bin_cache[cache_offset] = first;
  1230. - for(unsigned u = 0; u < membin_count - 1; u++)
  1231. - bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  1232. -
  1233. - //Swap into place
  1234. - RandomAccessIter nextbinstart = last;
  1235. - //handling empty bins
  1236. - RandomAccessIter * local_bin = &(bin_cache[cache_offset + bin_count]);
  1237. - RandomAccessIter lastFull = *local_bin;
  1238. - //Iterating over each element in the bin of empties
  1239. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1240. - //empties belong in this bin
  1241. - while((*current).size() > char_offset) {
  1242. - target_bin = end_bin - static_cast<unsignedchar_type>((*current)[char_offset]);
  1243. - iter_swap(current, (*target_bin)++);
  1244. - }
  1245. - }
  1246. - *local_bin = nextbinstart;
  1247. - nextbinstart = first;
  1248. - //iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  1249. - unsigned last_bin = max_bin;
  1250. - for(; last_bin && !bin_sizes[last_bin]; --last_bin) { }
  1251. - //This dominates runtime, mostly in the swap and bin lookups
  1252. - for(unsigned u = 0; u < last_bin; ++u) {
  1253. - local_bin = bins + u;
  1254. - nextbinstart += bin_sizes[u];
  1255. - //Iterating over each element in this bin
  1256. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1257. - //Swapping elements in current into place until the correct element has been swapped in
  1258. - for(target_bin = end_bin - static_cast<unsignedchar_type>((*current)[char_offset]); target_bin != local_bin;
  1259. - target_bin = end_bin - static_cast<unsignedchar_type>((*current)[char_offset]))
  1260. - iter_swap(current, (*target_bin)++);
  1261. - }
  1262. - *local_bin = nextbinstart;
  1263. - }
  1264. - bins[last_bin] = lastFull;
  1265. - //Recursing
  1266. - RandomAccessIter lastPos = first;
  1267. - //Skip this loop for empties
  1268. - for(unsigned u = cache_offset; u <= cache_offset + last_bin; lastPos = bin_cache[u], ++u) {
  1269. - size_t count = bin_cache[u] - lastPos;
  1270. - //don't sort unless there are at least two items to compare
  1271. - if(count < 2)
  1272. - continue;
  1273. - //using std::sort if its worst-case is better
  1274. - if(count < max_size)
  1275. - std::sort(lastPos, bin_cache[u], offset_greaterthan<data_type, unsignedchar_type>(char_offset + 1));
  1276. - else
  1277. - reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(lastPos, bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes);
  1278. - }
  1279. - }
  1280. -
  1281. - //String sorting recursive implementation
  1282. - template <class RandomAccessIter, class data_type, class unsignedchar_type, class get_char, class get_length>
  1283. - inline void
  1284. - string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  1285. - , unsigned cache_offset, std::vector<size_t> &bin_sizes, get_char getchar, get_length length)
  1286. - {
  1287. - //This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  1288. - //Iterate to the end of the empties. If all empty, return
  1289. - while(length(*first) <= char_offset) {
  1290. - if(++first == last)
  1291. - return;
  1292. - }
  1293. - RandomAccessIter finish = last - 1;
  1294. - //Getting the last non-empty
  1295. - for(;length(*finish) <= char_offset; --finish) { }
  1296. - ++finish;
  1297. - update_offset(first, finish, char_offset, getchar, length);
  1298. -
  1299. - const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  1300. - //Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  1301. - const unsigned max_size = bin_count;
  1302. - const unsigned membin_count = bin_count + 1;
  1303. - unsigned cache_end;
  1304. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count) + 1;
  1305. -
  1306. - //Calculating the size of each bin; this takes roughly 10% of runtime
  1307. - for (RandomAccessIter current = first; current != last; ++current) {
  1308. - if(length(*current) <= char_offset) {
  1309. - bin_sizes[0]++;
  1310. - }
  1311. - else
  1312. - bin_sizes[getchar((*current), char_offset) + 1]++;
  1313. - }
  1314. - //Assign the bin positions
  1315. - bin_cache[cache_offset] = first;
  1316. - for(unsigned u = 0; u < membin_count - 1; u++)
  1317. - bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  1318. -
  1319. - //Swap into place
  1320. - RandomAccessIter nextbinstart = first;
  1321. - //handling empty bins
  1322. - RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
  1323. - nextbinstart += bin_sizes[0];
  1324. - RandomAccessIter * target_bin;
  1325. - //Iterating over each element in the bin of empties
  1326. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1327. - //empties belong in this bin
  1328. - while(length(*current) > char_offset) {
  1329. - target_bin = bins + getchar((*current), char_offset);
  1330. - iter_swap(current, (*target_bin)++);
  1331. - }
  1332. - }
  1333. - *local_bin = nextbinstart;
  1334. - //iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  1335. - unsigned last_bin = bin_count - 1;
  1336. - for(; last_bin && !bin_sizes[last_bin + 1]; --last_bin) { }
  1337. - //This dominates runtime, mostly in the swap and bin lookups
  1338. - for(unsigned ii = 0; ii < last_bin; ++ii) {
  1339. - local_bin = bins + ii;
  1340. - nextbinstart += bin_sizes[ii + 1];
  1341. - //Iterating over each element in this bin
  1342. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1343. - //Swapping elements in current into place until the correct element has been swapped in
  1344. - for(target_bin = bins + getchar((*current), char_offset); target_bin != local_bin;
  1345. - target_bin = bins + getchar((*current), char_offset))
  1346. - iter_swap(current, (*target_bin)++);
  1347. - }
  1348. - *local_bin = nextbinstart;
  1349. - }
  1350. - bins[last_bin] = last;
  1351. -
  1352. - //Recursing
  1353. - RandomAccessIter lastPos = bin_cache[cache_offset];
  1354. - //Skip this loop for empties
  1355. - for(unsigned u = cache_offset + 1; u < cache_offset + last_bin + 2; lastPos = bin_cache[u], ++u) {
  1356. - size_t count = bin_cache[u] - lastPos;
  1357. - //don't sort unless there are at least two items to compare
  1358. - if(count < 2)
  1359. - continue;
  1360. - //using std::sort if its worst-case is better
  1361. - if(count < max_size)
  1362. - std::sort(lastPos, bin_cache[u], offset_char_lessthan<data_type, get_char, get_length>(char_offset + 1));
  1363. - else
  1364. - string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length>(lastPos, bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes, getchar, length);
  1365. - }
  1366. - }
  1367. -
  1368. - //String sorting recursive implementation
  1369. - template <class RandomAccessIter, class data_type, class unsignedchar_type, class get_char, class get_length, class compare>
  1370. - inline void
  1371. - string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  1372. - , unsigned cache_offset, std::vector<size_t> &bin_sizes, get_char getchar, get_length length, compare comp)
  1373. - {
  1374. - //This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  1375. - //Iterate to the end of the empties. If all empty, return
  1376. - while(length(*first) <= char_offset) {
  1377. - if(++first == last)
  1378. - return;
  1379. - }
  1380. - RandomAccessIter finish = last - 1;
  1381. - //Getting the last non-empty
  1382. - for(;length(*finish) <= char_offset; --finish) { }
  1383. - ++finish;
  1384. - update_offset(first, finish, char_offset, getchar, length);
  1385. -
  1386. - const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  1387. - //Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  1388. - const unsigned max_size = bin_count;
  1389. - const unsigned membin_count = bin_count + 1;
  1390. - unsigned cache_end;
  1391. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count) + 1;
  1392. -
  1393. - //Calculating the size of each bin; this takes roughly 10% of runtime
  1394. - for (RandomAccessIter current = first; current != last; ++current) {
  1395. - if(length(*current) <= char_offset) {
  1396. - bin_sizes[0]++;
  1397. - }
  1398. - else
  1399. - bin_sizes[getchar((*current), char_offset) + 1]++;
  1400. - }
  1401. - //Assign the bin positions
  1402. - bin_cache[cache_offset] = first;
  1403. - for(unsigned u = 0; u < membin_count - 1; u++)
  1404. - bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  1405. -
  1406. - //Swap into place
  1407. - RandomAccessIter nextbinstart = first;
  1408. - //handling empty bins
  1409. - RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
  1410. - nextbinstart += bin_sizes[0];
  1411. - RandomAccessIter * target_bin;
  1412. - //Iterating over each element in the bin of empties
  1413. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1414. - //empties belong in this bin
  1415. - while(length(*current) > char_offset) {
  1416. - target_bin = bins + getchar((*current), char_offset);
  1417. - iter_swap(current, (*target_bin)++);
  1418. - }
  1419. - }
  1420. - *local_bin = nextbinstart;
  1421. - //iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  1422. - unsigned last_bin = bin_count - 1;
  1423. - for(; last_bin && !bin_sizes[last_bin + 1]; --last_bin) { }
  1424. - //This dominates runtime, mostly in the swap and bin lookups
  1425. - for(unsigned u = 0; u < last_bin; ++u) {
  1426. - local_bin = bins + u;
  1427. - nextbinstart += bin_sizes[u + 1];
  1428. - //Iterating over each element in this bin
  1429. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1430. - //Swapping elements in current into place until the correct element has been swapped in
  1431. - for(target_bin = bins + getchar((*current), char_offset); target_bin != local_bin;
  1432. - target_bin = bins + getchar((*current), char_offset))
  1433. - iter_swap(current, (*target_bin)++);
  1434. - }
  1435. - *local_bin = nextbinstart;
  1436. - }
  1437. - bins[last_bin] = last;
  1438. -
  1439. - //Recursing
  1440. - RandomAccessIter lastPos = bin_cache[cache_offset];
  1441. - //Skip this loop for empties
  1442. - for(unsigned u = cache_offset + 1; u < cache_offset + last_bin + 2; lastPos = bin_cache[u], ++u) {
  1443. - size_t count = bin_cache[u] - lastPos;
  1444. - //don't sort unless there are at least two items to compare
  1445. - if(count < 2)
  1446. - continue;
  1447. - //using std::sort if its worst-case is better
  1448. - if(count < max_size)
  1449. - std::sort(lastPos, bin_cache[u], comp);
  1450. - else
  1451. - string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(lastPos
  1452. - , bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes, getchar, length, comp);
  1453. - }
  1454. - }
  1455. -
  1456. - //Sorts strings in reverse order, with empties at the end
  1457. - template <class RandomAccessIter, class data_type, class unsignedchar_type, class get_char, class get_length, class compare>
  1458. - inline void
  1459. - reverse_string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  1460. - , unsigned cache_offset, std::vector<size_t> &bin_sizes, get_char getchar, get_length length, compare comp)
  1461. - {
  1462. - //This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  1463. - RandomAccessIter curr = first;
  1464. - //Iterate to the end of the empties. If all empty, return
  1465. - while(length(*curr) <= char_offset) {
  1466. - if(++curr == last)
  1467. - return;
  1468. - }
  1469. - //Getting the last non-empty
  1470. - while(length(*(--last)) <= char_offset) { }
  1471. - ++last;
  1472. - //Offsetting on identical characters. This section works a character at a time for optimal worst-case performance.
  1473. - update_offset(first, last, char_offset, getchar, length);
  1474. -
  1475. - const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  1476. - //Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  1477. - const unsigned max_size = bin_count;
  1478. - const unsigned membin_count = bin_count + 1;
  1479. - const unsigned max_bin = bin_count - 1;
  1480. - unsigned cache_end;
  1481. - RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count);
  1482. - RandomAccessIter *end_bin = &(bin_cache[cache_offset + max_bin]);
  1483. -
  1484. - //Calculating the size of each bin; this takes roughly 10% of runtime
  1485. - for (RandomAccessIter current = first; current != last; ++current) {
  1486. - if(length(*current) <= char_offset) {
  1487. - bin_sizes[bin_count]++;
  1488. - }
  1489. - else
  1490. - bin_sizes[max_bin - getchar((*current), char_offset)]++;
  1491. - }
  1492. - //Assign the bin positions
  1493. - bin_cache[cache_offset] = first;
  1494. - for(unsigned u = 0; u < membin_count - 1; u++)
  1495. - bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  1496. -
  1497. - //Swap into place
  1498. - RandomAccessIter nextbinstart = last;
  1499. - //handling empty bins
  1500. - RandomAccessIter * local_bin = &(bin_cache[cache_offset + bin_count]);
  1501. - RandomAccessIter lastFull = *local_bin;
  1502. - RandomAccessIter * target_bin;
  1503. - //Iterating over each element in the bin of empties
  1504. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1505. - //empties belong in this bin
  1506. - while(length(*current) > char_offset) {
  1507. - target_bin = end_bin - getchar((*current), char_offset);
  1508. - iter_swap(current, (*target_bin)++);
  1509. - }
  1510. - }
  1511. - *local_bin = nextbinstart;
  1512. - nextbinstart = first;
  1513. - //iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  1514. - unsigned last_bin = max_bin;
  1515. - for(; last_bin && !bin_sizes[last_bin]; --last_bin) { }
  1516. - //This dominates runtime, mostly in the swap and bin lookups
  1517. - for(unsigned u = 0; u < last_bin; ++u) {
  1518. - local_bin = bins + u;
  1519. - nextbinstart += bin_sizes[u];
  1520. - //Iterating over each element in this bin
  1521. - for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1522. - //Swapping elements in current into place until the correct element has been swapped in
  1523. - for(target_bin = end_bin - getchar((*current), char_offset); target_bin != local_bin;
  1524. - target_bin = end_bin - getchar((*current), char_offset))
  1525. - iter_swap(current, (*target_bin)++);
  1526. - }
  1527. - *local_bin = nextbinstart;
  1528. - }
  1529. - bins[last_bin] = lastFull;
  1530. - //Recursing
  1531. - RandomAccessIter lastPos = first;
  1532. - //Skip this loop for empties
  1533. - for(unsigned u = cache_offset; u <= cache_offset + last_bin; lastPos = bin_cache[u], ++u) {
  1534. - size_t count = bin_cache[u] - lastPos;
  1535. - //don't sort unless there are at least two items to compare
  1536. - if(count < 2)
  1537. - continue;
  1538. - //using std::sort if its worst-case is better
  1539. - if(count < max_size)
  1540. - std::sort(lastPos, bin_cache[u], comp);
  1541. - else
  1542. - reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(lastPos
  1543. - , bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes, getchar, length, comp);
  1544. - }
  1545. - }
  1546. -
  1547. - //Holds the bin vector and makes the initial recursive call
  1548. - template <class RandomAccessIter, class data_type, class unsignedchar_type>
  1549. - inline void
  1550. - string_sort(RandomAccessIter first, RandomAccessIter last, data_type, unsignedchar_type)
  1551. - {
  1552. - std::vector<size_t> bin_sizes;
  1553. - std::vector<RandomAccessIter> bin_cache;
  1554. - string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(first, last, 0, bin_cache, 0, bin_sizes);
  1555. - }
  1556. -
  1557. - //Holds the bin vector and makes the initial recursive call
  1558. - template <class RandomAccessIter, class data_type, class unsignedchar_type>
  1559. - inline void
  1560. - reverse_string_sort(RandomAccessIter first, RandomAccessIter last, data_type, unsignedchar_type)
  1561. - {
  1562. - std::vector<size_t> bin_sizes;
  1563. - std::vector<RandomAccessIter> bin_cache;
  1564. - reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(first, last, 0, bin_cache, 0, bin_sizes);
  1565. - }
  1566. -
  1567. - //Holds the bin vector and makes the initial recursive call
  1568. - template <class RandomAccessIter, class get_char, class get_length, class data_type, class unsignedchar_type>
  1569. - inline void
  1570. - string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, data_type, unsignedchar_type)
  1571. - {
  1572. - std::vector<size_t> bin_sizes;
  1573. - std::vector<RandomAccessIter> bin_cache;
  1574. - string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length>(first, last, 0, bin_cache, 0, bin_sizes, getchar, length);
  1575. - }
  1576. -
  1577. - //Holds the bin vector and makes the initial recursive call
  1578. - template <class RandomAccessIter, class get_char, class get_length, class compare, class data_type, class unsignedchar_type>
  1579. - inline void
  1580. - string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp, data_type, unsignedchar_type)
  1581. - {
  1582. - std::vector<size_t> bin_sizes;
  1583. - std::vector<RandomAccessIter> bin_cache;
  1584. - string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(first, last, 0, bin_cache, 0, bin_sizes, getchar, length, comp);
  1585. - }
  1586. -
  1587. - //Holds the bin vector and makes the initial recursive call
  1588. - template <class RandomAccessIter, class get_char, class get_length, class compare, class data_type, class unsignedchar_type>
  1589. - inline void
  1590. - reverse_string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp, data_type, unsignedchar_type)
  1591. - {
  1592. - std::vector<size_t> bin_sizes;
  1593. - std::vector<RandomAccessIter> bin_cache;
  1594. - reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(first, last, 0, bin_cache, 0, bin_sizes, getchar, length, comp);
  1595. - }
  1596. - }
  1597. -
  1598. - //Allows character-type overloads
  1599. - template <class RandomAccessIter, class unsignedchar_type>
  1600. - inline void string_sort(RandomAccessIter first, RandomAccessIter last, unsignedchar_type unused)
  1601. - {
  1602. - //Don't sort if it's too small to optimize
  1603. - if(last - first < detail::MIN_SORT_SIZE)
  1604. - std::sort(first, last);
  1605. - else
  1606. - detail::string_sort(first, last, *first, unused);
  1607. - }
  1608. -
  1609. - //Top-level sorting call; wraps using default of unsigned char
  1610. - template <class RandomAccessIter>
  1611. - inline void string_sort(RandomAccessIter first, RandomAccessIter last)
  1612. - {
  1613. - unsigned char unused = '\0';
  1614. - string_sort(first, last, unused);
  1615. - }
  1616. -
  1617. - //Allows character-type overloads
  1618. - template <class RandomAccessIter, class compare, class unsignedchar_type>
  1619. - inline void reverse_string_sort(RandomAccessIter first, RandomAccessIter last, compare comp, unsignedchar_type unused)
  1620. - {
  1621. - //Don't sort if it's too small to optimize
  1622. - if(last - first < detail::MIN_SORT_SIZE)
  1623. - std::sort(first, last, comp);
  1624. - else
  1625. - detail::reverse_string_sort(first, last, *first, unused);
  1626. - }
  1627. -
  1628. - //Top-level sorting call; wraps using default of unsigned char
  1629. - template <class RandomAccessIter, class compare>
  1630. - inline void reverse_string_sort(RandomAccessIter first, RandomAccessIter last, compare comp)
  1631. - {
  1632. - unsigned char unused = '\0';
  1633. - reverse_string_sort(first, last, comp, unused);
  1634. - }
  1635. -
  1636. - template <class RandomAccessIter, class get_char, class get_length>
  1637. - inline void string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length)
  1638. - {
  1639. - //Don't sort if it's too small to optimize
  1640. - if(last - first < detail::MIN_SORT_SIZE)
  1641. - std::sort(first, last);
  1642. - else {
  1643. - //skipping past empties at the beginning, which allows us to get the character type
  1644. - //.empty() is not used so as not to require a user declaration of it
  1645. - while(!length(*first)) {
  1646. - if(++first == last)
  1647. - return;
  1648. - }
  1649. - detail::string_sort(first, last, getchar, length, *first, getchar((*first), 0));
  1650. - }
  1651. - }
  1652. -
  1653. - template <class RandomAccessIter, class get_char, class get_length, class compare>
  1654. - inline void string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp)
  1655. - {
  1656. - //Don't sort if it's too small to optimize
  1657. - if(last - first < detail::MIN_SORT_SIZE)
  1658. - std::sort(first, last, comp);
  1659. - else {
  1660. - //skipping past empties at the beginning, which allows us to get the character type
  1661. - //.empty() is not used so as not to require a user declaration of it
  1662. - while(!length(*first)) {
  1663. - if(++first == last)
  1664. - return;
  1665. - }
  1666. - detail::string_sort(first, last, getchar, length, comp, *first, getchar((*first), 0));
  1667. - }
  1668. - }
  1669. -
  1670. - template <class RandomAccessIter, class get_char, class get_length, class compare>
  1671. - inline void reverse_string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp)
  1672. - {
  1673. - //Don't sort if it's too small to optimize
  1674. - if(last - first < detail::MIN_SORT_SIZE)
  1675. - std::sort(first, last, comp);
  1676. - else {
  1677. - //skipping past empties at the beginning, which allows us to get the character type
  1678. - //.empty() is not used so as not to require a user declaration of it
  1679. - while(!length(*(--last))) {
  1680. - //Note: if there is just one non-empty, and it's at the beginning, then it's already in sorted order
  1681. - if(first == last)
  1682. - return;
  1683. - }
  1684. - //making last just after the end of the non-empty part of the array
  1685. - ++last;
  1686. - detail::reverse_string_sort(first, last, getchar, length, comp, *first, getchar((*first), 0));
  1687. - }
  1688. - }
  1689. -}
  1690. -
  1691. -#endif
  1692. +//Templated spread_sort library
  1693. +
  1694. +// Copyright Steven J. Ross 2001 - 2009.
  1695. +// Distributed under the Boost Software License, Version 1.0.
  1696. +// (See accompanying file LICENSE_1_0.txt or copy at
  1697. +// http://www.boost.org/LICENSE_1_0.txt)
  1698. +
  1699. +// See http://www.boost.org/ for updates, documentation, and revision history.
  1700. +
  1701. +/*
  1702. +Some improvements suggested by:
  1703. +Phil Endecott and Frank Gennari
  1704. +Cygwin fix provided by:
  1705. +Scott McMurray
  1706. +*/
  1707. +
  1708. +#ifndef BOOST_SPREAD_SORT_H
  1709. +#define BOOST_SPREAD_SORT_H
  1710. +#include <algorithm>
  1711. +#include <cstring>
  1712. +#include <vector>
  1713. +#include "webrtc/system_wrappers/source/spreadsortlib/constants.hpp"
  1714. +
  1715. +#include <features.h>
  1716. +#if defined(__UCLIBC__)
  1717. +#undef getchar
  1718. +#endif
  1719. +
  1720. +
  1721. +namespace boost {
  1722. + namespace detail {
  1723. + //This only works on unsigned data types
  1724. + template <typename T>
  1725. + inline unsigned
  1726. + rough_log_2_size(const T& input)
  1727. + {
  1728. + unsigned result = 0;
  1729. + //The && is necessary on some compilers to avoid infinite loops; it doesn't significantly impair performance
  1730. + while((input >> result) && (result < (8*sizeof(T)))) ++result;
  1731. + return result;
  1732. + }
  1733. +
  1734. + //Gets the maximum size which we'll call spread_sort on to control worst-case performance
  1735. + //Maintains both a minimum size to recurse and a check of distribution size versus count
  1736. + //This is called for a set of bins, instead of bin-by-bin, to avoid performance overhead
  1737. + inline size_t
  1738. + get_max_count(unsigned log_range, size_t count)
  1739. + {
  1740. + unsigned divisor = rough_log_2_size(count);
  1741. + //Making sure the divisor is positive
  1742. + if(divisor > LOG_MEAN_BIN_SIZE)
  1743. + divisor -= LOG_MEAN_BIN_SIZE;
  1744. + else
  1745. + divisor = 1;
  1746. + unsigned relative_width = (LOG_CONST * log_range)/((divisor > MAX_SPLITS) ? MAX_SPLITS : divisor);
  1747. + //Don't try to bitshift more than the size of an element
  1748. + if((8*sizeof(size_t)) <= relative_width)
  1749. + relative_width = (8*sizeof(size_t)) - 1;
  1750. + return (size_t)1 << ((relative_width < (LOG_MEAN_BIN_SIZE + LOG_MIN_SPLIT_COUNT)) ?
  1751. + (LOG_MEAN_BIN_SIZE + LOG_MIN_SPLIT_COUNT) : relative_width);
  1752. + }
  1753. +
  1754. + //Find the minimum and maximum using <
  1755. + template <class RandomAccessIter>
  1756. + inline void
  1757. + find_extremes(RandomAccessIter current, RandomAccessIter last, RandomAccessIter & max, RandomAccessIter & min)
  1758. + {
  1759. + min = max = current;
  1760. + //Start from the second item, as max and min are initialized to the first
  1761. + while(++current < last) {
  1762. + if(*max < *current)
  1763. + max = current;
  1764. + else if(*current < *min)
  1765. + min = current;
  1766. + }
  1767. + }
  1768. +
  1769. + //Uses a user-defined comparison operator to find minimum and maximum
  1770. + template <class RandomAccessIter, class compare>
  1771. + inline void
  1772. + find_extremes(RandomAccessIter current, RandomAccessIter last, RandomAccessIter & max, RandomAccessIter & min, compare comp)
  1773. + {
  1774. + min = max = current;
  1775. + while(++current < last) {
  1776. + if(comp(*max, *current))
  1777. + max = current;
  1778. + else if(comp(*current, *min))
  1779. + min = current;
  1780. + }
  1781. + }
  1782. +
  1783. + //Gets a non-negative right bit shift to operate as a logarithmic divisor
  1784. + inline int
  1785. + get_log_divisor(size_t count, unsigned log_range)
  1786. + {
  1787. + int log_divisor;
  1788. + //If we can finish in one iteration without exceeding either (2 to the MAX_SPLITS) or n bins, do so
  1789. + if((log_divisor = log_range - rough_log_2_size(count)) <= 0 && log_range < MAX_SPLITS)
  1790. + log_divisor = 0;
  1791. + else {
  1792. + //otherwise divide the data into an optimized number of pieces
  1793. + log_divisor += LOG_MEAN_BIN_SIZE;
  1794. + if(log_divisor < 0)
  1795. + log_divisor = 0;
  1796. + //Cannot exceed MAX_SPLITS or cache misses slow down bin lookups dramatically
  1797. + if((log_range - log_divisor) > MAX_SPLITS)
  1798. + log_divisor = log_range - MAX_SPLITS;
  1799. + }
  1800. + return log_divisor;
  1801. + }
  1802. +
  1803. + template <class RandomAccessIter>
  1804. + inline RandomAccessIter *
  1805. + size_bins(std::vector<size_t> &bin_sizes, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset, unsigned &cache_end, unsigned bin_count)
  1806. + {
  1807. + //Assure space for the size of each bin, followed by initializing sizes
  1808. + if(bin_count > bin_sizes.size())
  1809. + bin_sizes.resize(bin_count);
  1810. + for(size_t u = 0; u < bin_count; u++)
  1811. + bin_sizes[u] = 0;
  1812. + //Make sure there is space for the bins
  1813. + cache_end = cache_offset + bin_count;
  1814. + if(cache_end > bin_cache.size())
  1815. + bin_cache.resize(cache_end);
  1816. + return &(bin_cache[cache_offset]);
  1817. + }
  1818. +
  1819. + //Implementation for recursive integer sorting
  1820. + template <class RandomAccessIter, class div_type, class data_type>
  1821. + inline void
  1822. + spread_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  1823. + , std::vector<size_t> &bin_sizes)
  1824. + {
  1825. + //This step is roughly 10% of runtime, but it helps avoid worst-case behavior and improve behavior with real data
  1826. + //If you know the maximum and minimum ahead of time, you can pass those values in and skip this step for the first iteration
  1827. + RandomAccessIter max, min;
  1828. + find_extremes(first, last, max, min);
  1829. + //max and min will be the same (the first item) iff all values are equivalent
  1830. + if(max == min)
  1831. + return;
  1832. + RandomAccessIter * target_bin;
  1833. + unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(*max >> 0) - (*min >> 0)));
  1834. + div_type div_min = *min >> log_divisor;
  1835. + div_type div_max = *max >> log_divisor;
  1836. + unsigned bin_count = div_max - div_min + 1;
  1837. + unsigned cache_end;
  1838. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  1839. +
  1840. + //Calculating the size of each bin; this takes roughly 10% of runtime
  1841. + for (RandomAccessIter current = first; current != last;)
  1842. + bin_sizes[(*(current++) >> log_divisor) - div_min]++;
  1843. + //Assign the bin positions
  1844. + bins[0] = first;
  1845. + for(unsigned u = 0; u < bin_count - 1; u++)
  1846. + bins[u + 1] = bins[u] + bin_sizes[u];
  1847. +
  1848. + //Swap into place
  1849. + //This dominates runtime, mostly in the swap and bin lookups
  1850. + RandomAccessIter nextbinstart = first;
  1851. + for(unsigned u = 0; u < bin_count - 1; ++u) {
  1852. + RandomAccessIter * local_bin = bins + u;
  1853. + nextbinstart += bin_sizes[u];
  1854. + //Iterating over each element in this bin
  1855. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1856. + //Swapping elements in current into place until the correct element has been swapped in
  1857. + for(target_bin = (bins + ((*current >> log_divisor) - div_min)); target_bin != local_bin;
  1858. + target_bin = bins + ((*current >> log_divisor) - div_min)) {
  1859. + //3-way swap; this is about 1% faster than a 2-way swap with integers
  1860. + //The main advantage is less copies are involved per item put in the correct place
  1861. + data_type tmp;
  1862. + RandomAccessIter b = (*target_bin)++;
  1863. + RandomAccessIter * b_bin = bins + ((*b >> log_divisor) - div_min);
  1864. + if (b_bin != local_bin) {
  1865. + RandomAccessIter c = (*b_bin)++;
  1866. + tmp = *c;
  1867. + *c = *b;
  1868. + }
  1869. + else
  1870. + tmp = *b;
  1871. + *b = *current;
  1872. + *current = tmp;
  1873. + }
  1874. + }
  1875. + *local_bin = nextbinstart;
  1876. + }
  1877. + bins[bin_count - 1] = last;
  1878. +
  1879. + //If we've bucketsorted, the array is sorted and we should skip recursion
  1880. + if(!log_divisor)
  1881. + return;
  1882. +
  1883. + //Recursing; log_divisor is the remaining range
  1884. + size_t max_count = get_max_count(log_divisor, last - first);
  1885. + RandomAccessIter lastPos = first;
  1886. + for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  1887. + size_t count = bin_cache[u] - lastPos;
  1888. + //don't sort unless there are at least two items to compare
  1889. + if(count < 2)
  1890. + continue;
  1891. + //using std::sort if its worst-case is better
  1892. + if(count < max_count)
  1893. + std::sort(lastPos, bin_cache[u]);
  1894. + else
  1895. + spread_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes);
  1896. + }
  1897. + }
  1898. +
  1899. + //Generic bitshift-based 3-way swapping code
  1900. + template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  1901. + inline void inner_swap_loop(RandomAccessIter * bins, const RandomAccessIter & nextbinstart, unsigned ii, right_shift &shift
  1902. + , const unsigned log_divisor, const div_type div_min)
  1903. + {
  1904. + RandomAccessIter * local_bin = bins + ii;
  1905. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  1906. + for(RandomAccessIter * target_bin = (bins + (shift(*current, log_divisor) - div_min)); target_bin != local_bin;
  1907. + target_bin = bins + (shift(*current, log_divisor) - div_min)) {
  1908. + data_type tmp;
  1909. + RandomAccessIter b = (*target_bin)++;
  1910. + RandomAccessIter * b_bin = bins + (shift(*b, log_divisor) - div_min);
  1911. + //Three-way swap; if the item to be swapped doesn't belong in the current bin, swap it to where it belongs
  1912. + if (b_bin != local_bin) {
  1913. + RandomAccessIter c = (*b_bin)++;
  1914. + tmp = *c;
  1915. + *c = *b;
  1916. + }
  1917. + //Note: we could increment current once the swap is done in this case, but that seems to impair performance
  1918. + else
  1919. + tmp = *b;
  1920. + *b = *current;
  1921. + *current = tmp;
  1922. + }
  1923. + }
  1924. + *local_bin = nextbinstart;
  1925. + }
  1926. +
  1927. + //Standard swapping wrapper for ascending values
  1928. + template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  1929. + inline void swap_loop(RandomAccessIter * bins, RandomAccessIter & nextbinstart, unsigned ii, right_shift &shift
  1930. + , const std::vector<size_t> &bin_sizes, const unsigned log_divisor, const div_type div_min)
  1931. + {
  1932. + nextbinstart += bin_sizes[ii];
  1933. + inner_swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, log_divisor, div_min);
  1934. + }
  1935. +
  1936. + //Functor implementation for recursive sorting
  1937. + template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  1938. + inline void
  1939. + spread_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  1940. + , std::vector<size_t> &bin_sizes, right_shift shift, compare comp)
  1941. + {
  1942. + RandomAccessIter max, min;
  1943. + find_extremes(first, last, max, min, comp);
  1944. + if(max == min)
  1945. + return;
  1946. + unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(shift(*max, 0)) - (shift(*min, 0))));
  1947. + div_type div_min = shift(*min, log_divisor);
  1948. + div_type div_max = shift(*max, log_divisor);
  1949. + unsigned bin_count = div_max - div_min + 1;
  1950. + unsigned cache_end;
  1951. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  1952. +
  1953. + //Calculating the size of each bin
  1954. + for (RandomAccessIter current = first; current != last;)
  1955. + bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  1956. + bins[0] = first;
  1957. + for(unsigned u = 0; u < bin_count - 1; u++)
  1958. + bins[u + 1] = bins[u] + bin_sizes[u];
  1959. +
  1960. + //Swap into place
  1961. + RandomAccessIter nextbinstart = first;
  1962. + for(unsigned u = 0; u < bin_count - 1; ++u)
  1963. + swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, u, shift, bin_sizes, log_divisor, div_min);
  1964. + bins[bin_count - 1] = last;
  1965. +
  1966. + //If we've bucketsorted, the array is sorted and we should skip recursion
  1967. + if(!log_divisor)
  1968. + return;
  1969. +
  1970. + //Recursing
  1971. + size_t max_count = get_max_count(log_divisor, last - first);
  1972. + RandomAccessIter lastPos = first;
  1973. + for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  1974. + size_t count = bin_cache[u] - lastPos;
  1975. + if(count < 2)
  1976. + continue;
  1977. + if(count < max_count)
  1978. + std::sort(lastPos, bin_cache[u], comp);
  1979. + else
  1980. + spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift, compare>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift, comp);
  1981. + }
  1982. + }
  1983. +
  1984. + //Functor implementation for recursive sorting with only Shift overridden
  1985. + template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  1986. + inline void
  1987. + spread_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  1988. + , std::vector<size_t> &bin_sizes, right_shift shift)
  1989. + {
  1990. + RandomAccessIter max, min;
  1991. + find_extremes(first, last, max, min);
  1992. + if(max == min)
  1993. + return;
  1994. + unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(shift(*max, 0)) - (shift(*min, 0))));
  1995. + div_type div_min = shift(*min, log_divisor);
  1996. + div_type div_max = shift(*max, log_divisor);
  1997. + unsigned bin_count = div_max - div_min + 1;
  1998. + unsigned cache_end;
  1999. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  2000. +
  2001. + //Calculating the size of each bin
  2002. + for (RandomAccessIter current = first; current != last;)
  2003. + bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  2004. + bins[0] = first;
  2005. + for(unsigned u = 0; u < bin_count - 1; u++)
  2006. + bins[u + 1] = bins[u] + bin_sizes[u];
  2007. +
  2008. + //Swap into place
  2009. + RandomAccessIter nextbinstart = first;
  2010. + for(unsigned ii = 0; ii < bin_count - 1; ++ii)
  2011. + swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, bin_sizes, log_divisor, div_min);
  2012. + bins[bin_count - 1] = last;
  2013. +
  2014. + //If we've bucketsorted, the array is sorted and we should skip recursion
  2015. + if(!log_divisor)
  2016. + return;
  2017. +
  2018. + //Recursing
  2019. + size_t max_count = get_max_count(log_divisor, last - first);
  2020. + RandomAccessIter lastPos = first;
  2021. + for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  2022. + size_t count = bin_cache[u] - lastPos;
  2023. + if(count < 2)
  2024. + continue;
  2025. + if(count < max_count)
  2026. + std::sort(lastPos, bin_cache[u]);
  2027. + else
  2028. + spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift);
  2029. + }
  2030. + }
  2031. +
  2032. + //Holds the bin vector and makes the initial recursive call
  2033. + template <class RandomAccessIter, class div_type, class data_type>
  2034. + inline void
  2035. + spread_sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type)
  2036. + {
  2037. + std::vector<size_t> bin_sizes;
  2038. + std::vector<RandomAccessIter> bin_cache;
  2039. + spread_sort_rec<RandomAccessIter, div_type, data_type>(first, last, bin_cache, 0, bin_sizes);
  2040. + }
  2041. +
  2042. + template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  2043. + inline void
  2044. + spread_sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift, compare comp)
  2045. + {
  2046. + std::vector<size_t> bin_sizes;
  2047. + std::vector<RandomAccessIter> bin_cache;
  2048. + spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift, compare>(first, last, bin_cache, 0, bin_sizes, shift, comp);
  2049. + }
  2050. +
  2051. + template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  2052. + inline void
  2053. + spread_sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift)
  2054. + {
  2055. + std::vector<size_t> bin_sizes;
  2056. + std::vector<RandomAccessIter> bin_cache;
  2057. + spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(first, last, bin_cache, 0, bin_sizes, shift);
  2058. + }
  2059. + }
  2060. +
  2061. + //Top-level sorting call for integers
  2062. + template <class RandomAccessIter>
  2063. + inline void integer_sort(RandomAccessIter first, RandomAccessIter last)
  2064. + {
  2065. + //Don't sort if it's too small to optimize
  2066. + if(last - first < detail::MIN_SORT_SIZE)
  2067. + std::sort(first, last);
  2068. + else
  2069. + detail::spread_sort(first, last, *first >> 0, *first);
  2070. + }
  2071. +
  2072. + //integer_sort with functors
  2073. + template <class RandomAccessIter, class right_shift, class compare>
  2074. + inline void integer_sort(RandomAccessIter first, RandomAccessIter last,
  2075. + right_shift shift, compare comp) {
  2076. + if(last - first < detail::MIN_SORT_SIZE)
  2077. + std::sort(first, last, comp);
  2078. + else
  2079. + detail::spread_sort(first, last, shift(*first, 0), *first, shift, comp);
  2080. + }
  2081. +
  2082. + //integer_sort with right_shift functor
  2083. + template <class RandomAccessIter, class right_shift>
  2084. + inline void integer_sort(RandomAccessIter first, RandomAccessIter last,
  2085. + right_shift shift) {
  2086. + if(last - first < detail::MIN_SORT_SIZE)
  2087. + std::sort(first, last);
  2088. + else
  2089. + detail::spread_sort(first, last, shift(*first, 0), *first, shift);
  2090. + }
  2091. +
  2092. + //------------------------------------------------------ float_sort source --------------------------------------
  2093. + //Casts a RandomAccessIter to the specified data type
  2094. + template<class cast_type, class RandomAccessIter>
  2095. + inline cast_type
  2096. + cast_float_iter(const RandomAccessIter & floatiter)
  2097. + {
  2098. + cast_type result;
  2099. + std::memcpy(&result, &(*floatiter), sizeof(cast_type));
  2100. + return result;
  2101. + }
  2102. +
  2103. + //Casts a data element to the specified datinner_float_a type
  2104. + template<class data_type, class cast_type>
  2105. + inline cast_type
  2106. + mem_cast(const data_type & data)
  2107. + {
  2108. + cast_type result;
  2109. + std::memcpy(&result, &data, sizeof(cast_type));
  2110. + return result;
  2111. + }
  2112. +
  2113. + namespace detail {
  2114. + template <class RandomAccessIter, class div_type, class right_shift>
  2115. + inline void
  2116. + find_extremes(RandomAccessIter current, RandomAccessIter last, div_type & max, div_type & min, right_shift shift)
  2117. + {
  2118. + min = max = shift(*current, 0);
  2119. + while(++current < last) {
  2120. + div_type value = shift(*current, 0);
  2121. + if(max < value)
  2122. + max = value;
  2123. + else if(value < min)
  2124. + min = value;
  2125. + }
  2126. + }
  2127. +
  2128. + //Specialized swap loops for floating-point casting
  2129. + template <class RandomAccessIter, class div_type, class data_type>
  2130. + inline void inner_float_swap_loop(RandomAccessIter * bins, const RandomAccessIter & nextbinstart, unsigned ii
  2131. + , const unsigned log_divisor, const div_type div_min)
  2132. + {
  2133. + RandomAccessIter * local_bin = bins + ii;
  2134. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  2135. + for(RandomAccessIter * target_bin = (bins + ((cast_float_iter<div_type, RandomAccessIter>(current) >> log_divisor) - div_min)); target_bin != local_bin;
  2136. + target_bin = bins + ((cast_float_iter<div_type, RandomAccessIter>(current) >> log_divisor) - div_min)) {
  2137. + data_type tmp;
  2138. + RandomAccessIter b = (*target_bin)++;
  2139. + RandomAccessIter * b_bin = bins + ((cast_float_iter<div_type, RandomAccessIter>(b) >> log_divisor) - div_min);
  2140. + //Three-way swap; if the item to be swapped doesn't belong in the current bin, swap it to where it belongs
  2141. + if (b_bin != local_bin) {
  2142. + RandomAccessIter c = (*b_bin)++;
  2143. + tmp = *c;
  2144. + *c = *b;
  2145. + }
  2146. + else
  2147. + tmp = *b;
  2148. + *b = *current;
  2149. + *current = tmp;
  2150. + }
  2151. + }
  2152. + *local_bin = nextbinstart;
  2153. + }
  2154. +
  2155. + template <class RandomAccessIter, class div_type, class data_type>
  2156. + inline void float_swap_loop(RandomAccessIter * bins, RandomAccessIter & nextbinstart, unsigned ii
  2157. + , const std::vector<size_t> &bin_sizes, const unsigned log_divisor, const div_type div_min)
  2158. + {
  2159. + nextbinstart += bin_sizes[ii];
  2160. + inner_float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, ii, log_divisor, div_min);
  2161. + }
  2162. +
  2163. + template <class RandomAccessIter, class cast_type>
  2164. + inline void
  2165. + find_extremes(RandomAccessIter current, RandomAccessIter last, cast_type & max, cast_type & min)
  2166. + {
  2167. + min = max = cast_float_iter<cast_type, RandomAccessIter>(current);
  2168. + while(++current < last) {
  2169. + cast_type value = cast_float_iter<cast_type, RandomAccessIter>(current);
  2170. + if(max < value)
  2171. + max = value;
  2172. + else if(value < min)
  2173. + min = value;
  2174. + }
  2175. + }
  2176. +
  2177. + //Special-case sorting of positive floats with casting instead of a right_shift
  2178. + template <class RandomAccessIter, class div_type, class data_type>
  2179. + inline void
  2180. + positive_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  2181. + , std::vector<size_t> &bin_sizes)
  2182. + {
  2183. + div_type max, min;
  2184. + find_extremes(first, last, max, min);
  2185. + if(max == min)
  2186. + return;
  2187. + unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  2188. + div_type div_min = min >> log_divisor;
  2189. + div_type div_max = max >> log_divisor;
  2190. + unsigned bin_count = div_max - div_min + 1;
  2191. + unsigned cache_end;
  2192. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  2193. +
  2194. + //Calculating the size of each bin
  2195. + for (RandomAccessIter current = first; current != last;)
  2196. + bin_sizes[(cast_float_iter<div_type, RandomAccessIter>(current++) >> log_divisor) - div_min]++;
  2197. + bins[0] = first;
  2198. + for(unsigned u = 0; u < bin_count - 1; u++)
  2199. + bins[u + 1] = bins[u] + bin_sizes[u];
  2200. +
  2201. + //Swap into place
  2202. + RandomAccessIter nextbinstart = first;
  2203. + for(unsigned u = 0; u < bin_count - 1; ++u)
  2204. + float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, u, bin_sizes, log_divisor, div_min);
  2205. + bins[bin_count - 1] = last;
  2206. +
  2207. + //Return if we've completed bucketsorting
  2208. + if(!log_divisor)
  2209. + return;
  2210. +
  2211. + //Recursing
  2212. + size_t max_count = get_max_count(log_divisor, last - first);
  2213. + RandomAccessIter lastPos = first;
  2214. + for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  2215. + size_t count = bin_cache[u] - lastPos;
  2216. + if(count < 2)
  2217. + continue;
  2218. + if(count < max_count)
  2219. + std::sort(lastPos, bin_cache[u]);
  2220. + else
  2221. + positive_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes);
  2222. + }
  2223. + }
  2224. +
  2225. + //Sorting negative_ float_s
  2226. + //Note that bins are iterated in reverse order because max_neg_float = min_neg_int
  2227. + template <class RandomAccessIter, class div_type, class data_type>
  2228. + inline void
  2229. + negative_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  2230. + , std::vector<size_t> &bin_sizes)
  2231. + {
  2232. + div_type max, min;
  2233. + find_extremes(first, last, max, min);
  2234. + if(max == min)
  2235. + return;
  2236. + unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  2237. + div_type div_min = min >> log_divisor;
  2238. + div_type div_max = max >> log_divisor;
  2239. + unsigned bin_count = div_max - div_min + 1;
  2240. + unsigned cache_end;
  2241. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  2242. +
  2243. + //Calculating the size of each bin
  2244. + for (RandomAccessIter current = first; current != last;)
  2245. + bin_sizes[(cast_float_iter<div_type, RandomAccessIter>(current++) >> log_divisor) - div_min]++;
  2246. + bins[bin_count - 1] = first;
  2247. + for(int ii = bin_count - 2; ii >= 0; --ii)
  2248. + bins[ii] = bins[ii + 1] + bin_sizes[ii + 1];
  2249. +
  2250. + //Swap into place
  2251. + RandomAccessIter nextbinstart = first;
  2252. + //The last bin will always have the correct elements in it
  2253. + for(int ii = bin_count - 1; ii > 0; --ii)
  2254. + float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, ii, bin_sizes, log_divisor, div_min);
  2255. + //Since we don't process the last bin, we need to update its end position
  2256. + bin_cache[cache_offset] = last;
  2257. +
  2258. + //Return if we've completed bucketsorting
  2259. + if(!log_divisor)
  2260. + return;
  2261. +
  2262. + //Recursing
  2263. + size_t max_count = get_max_count(log_divisor, last - first);
  2264. + RandomAccessIter lastPos = first;
  2265. + for(int ii = cache_end - 1; ii >= (int)cache_offset; lastPos = bin_cache[ii], --ii) {
  2266. + size_t count = bin_cache[ii] - lastPos;
  2267. + if(count < 2)
  2268. + continue;
  2269. + if(count < max_count)
  2270. + std::sort(lastPos, bin_cache[ii]);
  2271. + else
  2272. + negative_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes);
  2273. + }
  2274. + }
  2275. +
  2276. + //Sorting negative_ float_s
  2277. + //Note that bins are iterated in reverse order because max_neg_float = min_neg_int
  2278. + template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  2279. + inline void
  2280. + negative_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  2281. + , std::vector<size_t> &bin_sizes, right_shift shift)
  2282. + {
  2283. + div_type max, min;
  2284. + find_extremes(first, last, max, min, shift);
  2285. + if(max == min)
  2286. + return;
  2287. + unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  2288. + div_type div_min = min >> log_divisor;
  2289. + div_type div_max = max >> log_divisor;
  2290. + unsigned bin_count = div_max - div_min + 1;
  2291. + unsigned cache_end;
  2292. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  2293. +
  2294. + //Calculating the size of each bin
  2295. + for (RandomAccessIter current = first; current != last;)
  2296. + bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  2297. + bins[bin_count - 1] = first;
  2298. + for(int ii = bin_count - 2; ii >= 0; --ii)
  2299. + bins[ii] = bins[ii + 1] + bin_sizes[ii + 1];
  2300. +
  2301. + //Swap into place
  2302. + RandomAccessIter nextbinstart = first;
  2303. + //The last bin will always have the correct elements in it
  2304. + for(int ii = bin_count - 1; ii > 0; --ii)
  2305. + swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, bin_sizes, log_divisor, div_min);
  2306. + //Since we don't process the last bin, we need to update its end position
  2307. + bin_cache[cache_offset] = last;
  2308. +
  2309. + //Return if we've completed bucketsorting
  2310. + if(!log_divisor)
  2311. + return;
  2312. +
  2313. + //Recursing
  2314. + size_t max_count = get_max_count(log_divisor, last - first);
  2315. + RandomAccessIter lastPos = first;
  2316. + for(int ii = cache_end - 1; ii >= (int)cache_offset; lastPos = bin_cache[ii], --ii) {
  2317. + size_t count = bin_cache[ii] - lastPos;
  2318. + if(count < 2)
  2319. + continue;
  2320. + if(count < max_count)
  2321. + std::sort(lastPos, bin_cache[ii]);
  2322. + else
  2323. + negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift);
  2324. + }
  2325. + }
  2326. +
  2327. + template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  2328. + inline void
  2329. + negative_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  2330. + , std::vector<size_t> &bin_sizes, right_shift shift, compare comp)
  2331. + {
  2332. + div_type max, min;
  2333. + find_extremes(first, last, max, min, shift);
  2334. + if(max == min)
  2335. + return;
  2336. + unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  2337. + div_type div_min = min >> log_divisor;
  2338. + div_type div_max = max >> log_divisor;
  2339. + unsigned bin_count = div_max - div_min + 1;
  2340. + unsigned cache_end;
  2341. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  2342. +
  2343. + //Calculating the size of each bin
  2344. + for (RandomAccessIter current = first; current != last;)
  2345. + bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  2346. + bins[bin_count - 1] = first;
  2347. + for(int ii = bin_count - 2; ii >= 0; --ii)
  2348. + bins[ii] = bins[ii + 1] + bin_sizes[ii + 1];
  2349. +
  2350. + //Swap into place
  2351. + RandomAccessIter nextbinstart = first;
  2352. + //The last bin will always have the correct elements in it
  2353. + for(int ii = bin_count - 1; ii > 0; --ii)
  2354. + swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, bin_sizes, log_divisor, div_min);
  2355. + //Since we don't process the last bin, we need to update its end position
  2356. + bin_cache[cache_offset] = last;
  2357. +
  2358. + //Return if we've completed bucketsorting
  2359. + if(!log_divisor)
  2360. + return;
  2361. +
  2362. + //Recursing
  2363. + size_t max_count = get_max_count(log_divisor, last - first);
  2364. + RandomAccessIter lastPos = first;
  2365. + for(int ii = cache_end - 1; ii >= (int)cache_offset; lastPos = bin_cache[ii], --ii) {
  2366. + size_t count = bin_cache[ii] - lastPos;
  2367. + if(count < 2)
  2368. + continue;
  2369. + if(count < max_count)
  2370. + std::sort(lastPos, bin_cache[ii], comp);
  2371. + else
  2372. + negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift, compare>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift, comp);
  2373. + }
  2374. + }
  2375. +
  2376. + //Casting special-case for floating-point sorting
  2377. + template <class RandomAccessIter, class div_type, class data_type>
  2378. + inline void
  2379. + float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  2380. + , std::vector<size_t> &bin_sizes)
  2381. + {
  2382. + div_type max, min;
  2383. + find_extremes(first, last, max, min);
  2384. + if(max == min)
  2385. + return;
  2386. + unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  2387. + div_type div_min = min >> log_divisor;
  2388. + div_type div_max = max >> log_divisor;
  2389. + unsigned bin_count = div_max - div_min + 1;
  2390. + unsigned cache_end;
  2391. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  2392. +
  2393. + //Calculating the size of each bin
  2394. + for (RandomAccessIter current = first; current != last;)
  2395. + bin_sizes[(cast_float_iter<div_type, RandomAccessIter>(current++) >> log_divisor) - div_min]++;
  2396. + //The index of the first positive bin
  2397. + div_type first_positive = (div_min < 0) ? -div_min : 0;
  2398. + //Resetting if all bins are negative
  2399. + if(cache_offset + first_positive > cache_end)
  2400. + first_positive = cache_end - cache_offset;
  2401. + //Reversing the order of the negative bins
  2402. + //Note that because of the negative/positive ordering direction flip
  2403. + //We can not depend upon bin order and positions matching up
  2404. + //so bin_sizes must be reused to contain the end of the bin
  2405. + if(first_positive > 0) {
  2406. + bins[first_positive - 1] = first;
  2407. + for(int ii = first_positive - 2; ii >= 0; --ii) {
  2408. + bins[ii] = first + bin_sizes[ii + 1];
  2409. + bin_sizes[ii] += bin_sizes[ii + 1];
  2410. + }
  2411. + //Handling positives following negatives
  2412. + if((unsigned)first_positive < bin_count) {
  2413. + bins[first_positive] = first + bin_sizes[0];
  2414. + bin_sizes[first_positive] += bin_sizes[0];
  2415. + }
  2416. + }
  2417. + else
  2418. + bins[0] = first;
  2419. + for(unsigned u = first_positive; u < bin_count - 1; u++) {
  2420. + bins[u + 1] = first + bin_sizes[u];
  2421. + bin_sizes[u + 1] += bin_sizes[u];
  2422. + }
  2423. +
  2424. + //Swap into place
  2425. + RandomAccessIter nextbinstart = first;
  2426. + for(unsigned u = 0; u < bin_count; ++u) {
  2427. + nextbinstart = first + bin_sizes[u];
  2428. + inner_float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, u, log_divisor, div_min);
  2429. + }
  2430. +
  2431. + if(!log_divisor)
  2432. + return;
  2433. +
  2434. + //Handling negative values first
  2435. + size_t max_count = get_max_count(log_divisor, last - first);
  2436. + RandomAccessIter lastPos = first;
  2437. + for(int ii = cache_offset + first_positive - 1; ii >= (int)cache_offset ; lastPos = bin_cache[ii--]) {
  2438. + size_t count = bin_cache[ii] - lastPos;
  2439. + if(count < 2)
  2440. + continue;
  2441. + if(count < max_count)
  2442. + std::sort(lastPos, bin_cache[ii]);
  2443. + //sort negative values using reversed-bin spread_sort
  2444. + else
  2445. + negative_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes);
  2446. + }
  2447. +
  2448. + for(unsigned u = cache_offset + first_positive; u < cache_end; lastPos = bin_cache[u], ++u) {
  2449. + size_t count = bin_cache[u] - lastPos;
  2450. + if(count < 2)
  2451. + continue;
  2452. + if(count < max_count)
  2453. + std::sort(lastPos, bin_cache[u]);
  2454. + //sort positive values using normal spread_sort
  2455. + else
  2456. + positive_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes);
  2457. + }
  2458. + }
  2459. +
  2460. + //Functor implementation for recursive sorting
  2461. + template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  2462. + inline void
  2463. + float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  2464. + , std::vector<size_t> &bin_sizes, right_shift shift)
  2465. + {
  2466. + div_type max, min;
  2467. + find_extremes(first, last, max, min, shift);
  2468. + if(max == min)
  2469. + return;
  2470. + unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  2471. + div_type div_min = min >> log_divisor;
  2472. + div_type div_max = max >> log_divisor;
  2473. + unsigned bin_count = div_max - div_min + 1;
  2474. + unsigned cache_end;
  2475. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  2476. +
  2477. + //Calculating the size of each bin
  2478. + for (RandomAccessIter current = first; current != last;)
  2479. + bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  2480. + //The index of the first positive bin
  2481. + div_type first_positive = (div_min < 0) ? -div_min : 0;
  2482. + //Resetting if all bins are negative
  2483. + if(cache_offset + first_positive > cache_end)
  2484. + first_positive = cache_end - cache_offset;
  2485. + //Reversing the order of the negative bins
  2486. + //Note that because of the negative/positive ordering direction flip
  2487. + //We can not depend upon bin order and positions matching up
  2488. + //so bin_sizes must be reused to contain the end of the bin
  2489. + if(first_positive > 0) {
  2490. + bins[first_positive - 1] = first;
  2491. + for(int ii = first_positive - 2; ii >= 0; --ii) {
  2492. + bins[ii] = first + bin_sizes[ii + 1];
  2493. + bin_sizes[ii] += bin_sizes[ii + 1];
  2494. + }
  2495. + //Handling positives following negatives
  2496. + if((unsigned)first_positive < bin_count) {
  2497. + bins[first_positive] = first + bin_sizes[0];
  2498. + bin_sizes[first_positive] += bin_sizes[0];
  2499. + }
  2500. + }
  2501. + else
  2502. + bins[0] = first;
  2503. + for(unsigned u = first_positive; u < bin_count - 1; u++) {
  2504. + bins[u + 1] = first + bin_sizes[u];
  2505. + bin_sizes[u + 1] += bin_sizes[u];
  2506. + }
  2507. +
  2508. + //Swap into place
  2509. + RandomAccessIter nextbinstart = first;
  2510. + for(unsigned u = 0; u < bin_count; ++u) {
  2511. + nextbinstart = first + bin_sizes[u];
  2512. + inner_swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, u, shift, log_divisor, div_min);
  2513. + }
  2514. +
  2515. + //Return if we've completed bucketsorting
  2516. + if(!log_divisor)
  2517. + return;
  2518. +
  2519. + //Handling negative values first
  2520. + size_t max_count = get_max_count(log_divisor, last - first);
  2521. + RandomAccessIter lastPos = first;
  2522. + for(int ii = cache_offset + first_positive - 1; ii >= (int)cache_offset ; lastPos = bin_cache[ii--]) {
  2523. + size_t count = bin_cache[ii] - lastPos;
  2524. + if(count < 2)
  2525. + continue;
  2526. + if(count < max_count)
  2527. + std::sort(lastPos, bin_cache[ii]);
  2528. + //sort negative values using reversed-bin spread_sort
  2529. + else
  2530. + negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift);
  2531. + }
  2532. +
  2533. + for(unsigned u = cache_offset + first_positive; u < cache_end; lastPos = bin_cache[u], ++u) {
  2534. + size_t count = bin_cache[u] - lastPos;
  2535. + if(count < 2)
  2536. + continue;
  2537. + if(count < max_count)
  2538. + std::sort(lastPos, bin_cache[u]);
  2539. + //sort positive values using normal spread_sort
  2540. + else
  2541. + spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift);
  2542. + }
  2543. + }
  2544. +
  2545. + template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  2546. + inline void
  2547. + float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  2548. + , std::vector<size_t> &bin_sizes, right_shift shift, compare comp)
  2549. + {
  2550. + div_type max, min;
  2551. + find_extremes(first, last, max, min, shift);
  2552. + if(max == min)
  2553. + return;
  2554. + unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  2555. + div_type div_min = min >> log_divisor;
  2556. + div_type div_max = max >> log_divisor;
  2557. + unsigned bin_count = div_max - div_min + 1;
  2558. + unsigned cache_end;
  2559. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  2560. +
  2561. + //Calculating the size of each bin
  2562. + for (RandomAccessIter current = first; current != last;)
  2563. + bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  2564. + //The index of the first positive bin
  2565. + div_type first_positive = (div_min < 0) ? -div_min : 0;
  2566. + //Resetting if all bins are negative
  2567. + if(cache_offset + first_positive > cache_end)
  2568. + first_positive = cache_end - cache_offset;
  2569. + //Reversing the order of the negative bins
  2570. + //Note that because of the negative/positive ordering direction flip
  2571. + //We can not depend upon bin order and positions matching up
  2572. + //so bin_sizes must be reused to contain the end of the bin
  2573. + if(first_positive > 0) {
  2574. + bins[first_positive - 1] = first;
  2575. + for(int ii = first_positive - 2; ii >= 0; --ii) {
  2576. + bins[ii] = first + bin_sizes[ii + 1];
  2577. + bin_sizes[ii] += bin_sizes[ii + 1];
  2578. + }
  2579. + //Handling positives following negatives
  2580. + if((unsigned)first_positive < bin_count) {
  2581. + bins[first_positive] = first + bin_sizes[0];
  2582. + bin_sizes[first_positive] += bin_sizes[0];
  2583. + }
  2584. + }
  2585. + else
  2586. + bins[0] = first;
  2587. + for(unsigned u = first_positive; u < bin_count - 1; u++) {
  2588. + bins[u + 1] = first + bin_sizes[u];
  2589. + bin_sizes[u + 1] += bin_sizes[u];
  2590. + }
  2591. +
  2592. + //Swap into place
  2593. + RandomAccessIter nextbinstart = first;
  2594. + for(unsigned u = 0; u < bin_count; ++u) {
  2595. + nextbinstart = first + bin_sizes[u];
  2596. + inner_swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, u, shift, log_divisor, div_min);
  2597. + }
  2598. +
  2599. + //Return if we've completed bucketsorting
  2600. + if(!log_divisor)
  2601. + return;
  2602. +
  2603. + //Handling negative values first
  2604. + size_t max_count = get_max_count(log_divisor, last - first);
  2605. + RandomAccessIter lastPos = first;
  2606. + for(int ii = cache_offset + first_positive - 1; ii >= (int)cache_offset ; lastPos = bin_cache[ii--]) {
  2607. + size_t count = bin_cache[ii] - lastPos;
  2608. + if(count < 2)
  2609. + continue;
  2610. + if(count < max_count)
  2611. + std::sort(lastPos, bin_cache[ii]);
  2612. + //sort negative values using reversed-bin spread_sort
  2613. + else
  2614. + negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift, comp);
  2615. + }
  2616. +
  2617. + for(unsigned u = cache_offset + first_positive; u < cache_end; lastPos = bin_cache[u], ++u) {
  2618. + size_t count = bin_cache[u] - lastPos;
  2619. + if(count < 2)
  2620. + continue;
  2621. + if(count < max_count)
  2622. + std::sort(lastPos, bin_cache[u]);
  2623. + //sort positive values using normal spread_sort
  2624. + else
  2625. + spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift, comp);
  2626. + }
  2627. + }
  2628. +
  2629. + template <class RandomAccessIter, class cast_type, class data_type>
  2630. + inline void
  2631. + float_Sort(RandomAccessIter first, RandomAccessIter last, cast_type, data_type)
  2632. + {
  2633. + std::vector<size_t> bin_sizes;
  2634. + std::vector<RandomAccessIter> bin_cache;
  2635. + float_sort_rec<RandomAccessIter, cast_type, data_type>(first, last, bin_cache, 0, bin_sizes);
  2636. + }
  2637. +
  2638. + template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  2639. + inline void
  2640. + float_Sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift)
  2641. + {
  2642. + std::vector<size_t> bin_sizes;
  2643. + std::vector<RandomAccessIter> bin_cache;
  2644. + float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(first, last, bin_cache, 0, bin_sizes, shift);
  2645. + }
  2646. +
  2647. + template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  2648. + inline void
  2649. + float_Sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift, compare comp)
  2650. + {
  2651. + std::vector<size_t> bin_sizes;
  2652. + std::vector<RandomAccessIter> bin_cache;
  2653. + float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(first, last, bin_cache, 0, bin_sizes, shift, comp);
  2654. + }
  2655. + }
  2656. +
  2657. + //float_sort with casting
  2658. + //The cast_type must be equal in size to the data type, and must be a signed integer
  2659. + template <class RandomAccessIter, class cast_type>
  2660. + inline void float_sort_cast(RandomAccessIter first, RandomAccessIter last, cast_type cVal)
  2661. + {
  2662. + if(last - first < detail::MIN_SORT_SIZE)
  2663. + std::sort(first, last);
  2664. + else
  2665. + detail::float_Sort(first, last, cVal, *first);
  2666. + }
  2667. +
  2668. + //float_sort with casting to an int
  2669. + //Only use this with IEEE floating-point numbers
  2670. + template <class RandomAccessIter>
  2671. + inline void float_sort_cast_to_int(RandomAccessIter first, RandomAccessIter last)
  2672. + {
  2673. + int cVal = 0;
  2674. + float_sort_cast(first, last, cVal);
  2675. + }
  2676. +
  2677. + //float_sort with functors
  2678. + template <class RandomAccessIter, class right_shift>
  2679. + inline void float_sort(RandomAccessIter first, RandomAccessIter last, right_shift shift)
  2680. + {
  2681. + if(last - first < detail::MIN_SORT_SIZE)
  2682. + std::sort(first, last);
  2683. + else
  2684. + detail::float_Sort(first, last, shift(*first, 0), *first, shift);
  2685. + }
  2686. +
  2687. + template <class RandomAccessIter, class right_shift, class compare>
  2688. + inline void float_sort(RandomAccessIter first, RandomAccessIter last, right_shift shift, compare comp)
  2689. + {
  2690. + if(last - first < detail::MIN_SORT_SIZE)
  2691. + std::sort(first, last, comp);
  2692. + else
  2693. + detail::float_Sort(first, last, shift(*first, 0), *first, shift, comp);
  2694. + }
  2695. +
  2696. + //------------------------------------------------- string_sort source ---------------------------------------------
  2697. + namespace detail {
  2698. + //Offsetting on identical characters. This function works a character at a time for optimal worst-case performance.
  2699. + template<class RandomAccessIter>
  2700. + inline void
  2701. + update_offset(RandomAccessIter first, RandomAccessIter finish, unsigned &char_offset)
  2702. + {
  2703. + unsigned nextOffset = char_offset;
  2704. + bool done = false;
  2705. + while(!done) {
  2706. + RandomAccessIter curr = first;
  2707. + do {
  2708. + //ignore empties, but if the nextOffset would exceed the length or not match, exit; we've found the last matching character
  2709. + if((*curr).size() > char_offset && ((*curr).size() <= (nextOffset + 1) || (*curr)[nextOffset] != (*first)[nextOffset])) {
  2710. + done = true;
  2711. + break;
  2712. + }
  2713. + } while(++curr != finish);
  2714. + if(!done)
  2715. + ++nextOffset;
  2716. + }
  2717. + char_offset = nextOffset;
  2718. + }
  2719. +
  2720. + //Offsetting on identical characters. This function works a character at a time for optimal worst-case performance.
  2721. + template<class RandomAccessIter, class get_char, class get_length>
  2722. + inline void
  2723. + update_offset(RandomAccessIter first, RandomAccessIter finish, unsigned &char_offset, get_char getchar, get_length length)
  2724. + {
  2725. + unsigned nextOffset = char_offset;
  2726. + bool done = false;
  2727. + while(!done) {
  2728. + RandomAccessIter curr = first;
  2729. + do {
  2730. + //ignore empties, but if the nextOffset would exceed the length or not match, exit; we've found the last matching character
  2731. + if(length(*curr) > char_offset && (length(*curr) <= (nextOffset + 1) || getchar((*curr), nextOffset) != getchar((*first), nextOffset))) {
  2732. + done = true;
  2733. + break;
  2734. + }
  2735. + } while(++curr != finish);
  2736. + if(!done)
  2737. + ++nextOffset;
  2738. + }
  2739. + char_offset = nextOffset;
  2740. + }
  2741. +
  2742. + //A comparison functor for strings that assumes they are identical up to char_offset
  2743. + template<class data_type, class unsignedchar_type>
  2744. + struct offset_lessthan {
  2745. + offset_lessthan(unsigned char_offset) : fchar_offset(char_offset){}
  2746. + inline bool operator()(const data_type &x, const data_type &y) const
  2747. + {
  2748. + unsigned minSize = std::min(x.size(), y.size());
  2749. + for(unsigned u = fchar_offset; u < minSize; ++u) {
  2750. + if(static_cast<unsignedchar_type>(x[u]) < static_cast<unsignedchar_type>(y[u]))
  2751. + return true;
  2752. + else if(static_cast<unsignedchar_type>(y[u]) < static_cast<unsignedchar_type>(x[u]))
  2753. + return false;
  2754. + }
  2755. + return x.size() < y.size();
  2756. + }
  2757. + unsigned fchar_offset;
  2758. + };
  2759. +
  2760. + //A comparison functor for strings that assumes they are identical up to char_offset
  2761. + template<class data_type, class unsignedchar_type>
  2762. + struct offset_greaterthan {
  2763. + offset_greaterthan(unsigned char_offset) : fchar_offset(char_offset){}
  2764. + inline bool operator()(const data_type &x, const data_type &y) const
  2765. + {
  2766. + unsigned minSize = std::min(x.size(), y.size());
  2767. + for(unsigned u = fchar_offset; u < minSize; ++u) {
  2768. + if(static_cast<unsignedchar_type>(x[u]) > static_cast<unsignedchar_type>(y[u]))
  2769. + return true;
  2770. + else if(static_cast<unsignedchar_type>(y[u]) > static_cast<unsignedchar_type>(x[u]))
  2771. + return false;
  2772. + }
  2773. + return x.size() > y.size();
  2774. + }
  2775. + unsigned fchar_offset;
  2776. + };
  2777. +
  2778. + //A comparison functor for strings that assumes they are identical up to char_offset
  2779. + template<class data_type, class get_char, class get_length>
  2780. + struct offset_char_lessthan {
  2781. + offset_char_lessthan(unsigned char_offset) : fchar_offset(char_offset){}
  2782. + inline bool operator()(const data_type &x, const data_type &y) const
  2783. + {
  2784. + unsigned minSize = std::min(length(x), length(y));
  2785. + for(unsigned u = fchar_offset; u < minSize; ++u) {
  2786. + if(getchar(x, u) < getchar(y, u))
  2787. + return true;
  2788. + else if(getchar(y, u) < getchar(x, u))
  2789. + return false;
  2790. + }
  2791. + return length(x) < length(y);
  2792. + }
  2793. + unsigned fchar_offset;
  2794. + get_char getchar;
  2795. + get_length length;
  2796. + };
  2797. +
  2798. + //String sorting recursive implementation
  2799. + template <class RandomAccessIter, class data_type, class unsignedchar_type>
  2800. + inline void
  2801. + string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  2802. + , unsigned cache_offset, std::vector<size_t> &bin_sizes)
  2803. + {
  2804. + //This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  2805. + //Iterate to the end of the empties. If all empty, return
  2806. + while((*first).size() <= char_offset) {
  2807. + if(++first == last)
  2808. + return;
  2809. + }
  2810. + RandomAccessIter finish = last - 1;
  2811. + //Getting the last non-empty
  2812. + for(;(*finish).size() <= char_offset; --finish) { }
  2813. + ++finish;
  2814. + //Offsetting on identical characters. This section works a character at a time for optimal worst-case performance.
  2815. + update_offset(first, finish, char_offset);
  2816. +
  2817. + const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  2818. + //Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  2819. + const unsigned max_size = bin_count;
  2820. + const unsigned membin_count = bin_count + 1;
  2821. + unsigned cache_end;
  2822. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count) + 1;
  2823. +
  2824. + //Calculating the size of each bin; this takes roughly 10% of runtime
  2825. + for (RandomAccessIter current = first; current != last; ++current) {
  2826. + if((*current).size() <= char_offset) {
  2827. + bin_sizes[0]++;
  2828. + }
  2829. + else
  2830. + bin_sizes[static_cast<unsignedchar_type>((*current)[char_offset]) + 1]++;
  2831. + }
  2832. + //Assign the bin positions
  2833. + bin_cache[cache_offset] = first;
  2834. + for(unsigned u = 0; u < membin_count - 1; u++)
  2835. + bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  2836. +
  2837. + //Swap into place
  2838. + RandomAccessIter nextbinstart = first;
  2839. + //handling empty bins
  2840. + RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
  2841. + nextbinstart += bin_sizes[0];
  2842. + RandomAccessIter * target_bin;
  2843. + //Iterating over each element in the bin of empties
  2844. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  2845. + //empties belong in this bin
  2846. + while((*current).size() > char_offset) {
  2847. + target_bin = bins + static_cast<unsignedchar_type>((*current)[char_offset]);
  2848. + iter_swap(current, (*target_bin)++);
  2849. + }
  2850. + }
  2851. + *local_bin = nextbinstart;
  2852. + //iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  2853. + unsigned last_bin = bin_count - 1;
  2854. + for(; last_bin && !bin_sizes[last_bin + 1]; --last_bin) { }
  2855. + //This dominates runtime, mostly in the swap and bin lookups
  2856. + for(unsigned u = 0; u < last_bin; ++u) {
  2857. + local_bin = bins + u;
  2858. + nextbinstart += bin_sizes[u + 1];
  2859. + //Iterating over each element in this bin
  2860. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  2861. + //Swapping elements in current into place until the correct element has been swapped in
  2862. + for(target_bin = bins + static_cast<unsignedchar_type>((*current)[char_offset]); target_bin != local_bin;
  2863. + target_bin = bins + static_cast<unsignedchar_type>((*current)[char_offset]))
  2864. + iter_swap(current, (*target_bin)++);
  2865. + }
  2866. + *local_bin = nextbinstart;
  2867. + }
  2868. + bins[last_bin] = last;
  2869. + //Recursing
  2870. + RandomAccessIter lastPos = bin_cache[cache_offset];
  2871. + //Skip this loop for empties
  2872. + for(unsigned u = cache_offset + 1; u < cache_offset + last_bin + 2; lastPos = bin_cache[u], ++u) {
  2873. + size_t count = bin_cache[u] - lastPos;
  2874. + //don't sort unless there are at least two items to compare
  2875. + if(count < 2)
  2876. + continue;
  2877. + //using std::sort if its worst-case is better
  2878. + if(count < max_size)
  2879. + std::sort(lastPos, bin_cache[u], offset_lessthan<data_type, unsignedchar_type>(char_offset + 1));
  2880. + else
  2881. + string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(lastPos, bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes);
  2882. + }
  2883. + }
  2884. +
  2885. + //Sorts strings in reverse order, with empties at the end
  2886. + template <class RandomAccessIter, class data_type, class unsignedchar_type>
  2887. + inline void
  2888. + reverse_string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  2889. + , unsigned cache_offset, std::vector<size_t> &bin_sizes)
  2890. + {
  2891. + //This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  2892. + RandomAccessIter curr = first;
  2893. + //Iterate to the end of the empties. If all empty, return
  2894. + while((*curr).size() <= char_offset) {
  2895. + if(++curr == last)
  2896. + return;
  2897. + }
  2898. + //Getting the last non-empty
  2899. + while((*(--last)).size() <= char_offset) { }
  2900. + ++last;
  2901. + //Offsetting on identical characters. This section works a character at a time for optimal worst-case performance.
  2902. + update_offset(curr, last, char_offset);
  2903. + RandomAccessIter * target_bin;
  2904. +
  2905. + const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  2906. + //Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  2907. + const unsigned max_size = bin_count;
  2908. + const unsigned membin_count = bin_count + 1;
  2909. + const unsigned max_bin = bin_count - 1;
  2910. + unsigned cache_end;
  2911. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count);
  2912. + RandomAccessIter * end_bin = &(bin_cache[cache_offset + max_bin]);
  2913. +
  2914. + //Calculating the size of each bin; this takes roughly 10% of runtime
  2915. + for (RandomAccessIter current = first; current != last; ++current) {
  2916. + if((*current).size() <= char_offset) {
  2917. + bin_sizes[bin_count]++;
  2918. + }
  2919. + else
  2920. + bin_sizes[max_bin - static_cast<unsignedchar_type>((*current)[char_offset])]++;
  2921. + }
  2922. + //Assign the bin positions
  2923. + bin_cache[cache_offset] = first;
  2924. + for(unsigned u = 0; u < membin_count - 1; u++)
  2925. + bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  2926. +
  2927. + //Swap into place
  2928. + RandomAccessIter nextbinstart = last;
  2929. + //handling empty bins
  2930. + RandomAccessIter * local_bin = &(bin_cache[cache_offset + bin_count]);
  2931. + RandomAccessIter lastFull = *local_bin;
  2932. + //Iterating over each element in the bin of empties
  2933. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  2934. + //empties belong in this bin
  2935. + while((*current).size() > char_offset) {
  2936. + target_bin = end_bin - static_cast<unsignedchar_type>((*current)[char_offset]);
  2937. + iter_swap(current, (*target_bin)++);
  2938. + }
  2939. + }
  2940. + *local_bin = nextbinstart;
  2941. + nextbinstart = first;
  2942. + //iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  2943. + unsigned last_bin = max_bin;
  2944. + for(; last_bin && !bin_sizes[last_bin]; --last_bin) { }
  2945. + //This dominates runtime, mostly in the swap and bin lookups
  2946. + for(unsigned u = 0; u < last_bin; ++u) {
  2947. + local_bin = bins + u;
  2948. + nextbinstart += bin_sizes[u];
  2949. + //Iterating over each element in this bin
  2950. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  2951. + //Swapping elements in current into place until the correct element has been swapped in
  2952. + for(target_bin = end_bin - static_cast<unsignedchar_type>((*current)[char_offset]); target_bin != local_bin;
  2953. + target_bin = end_bin - static_cast<unsignedchar_type>((*current)[char_offset]))
  2954. + iter_swap(current, (*target_bin)++);
  2955. + }
  2956. + *local_bin = nextbinstart;
  2957. + }
  2958. + bins[last_bin] = lastFull;
  2959. + //Recursing
  2960. + RandomAccessIter lastPos = first;
  2961. + //Skip this loop for empties
  2962. + for(unsigned u = cache_offset; u <= cache_offset + last_bin; lastPos = bin_cache[u], ++u) {
  2963. + size_t count = bin_cache[u] - lastPos;
  2964. + //don't sort unless there are at least two items to compare
  2965. + if(count < 2)
  2966. + continue;
  2967. + //using std::sort if its worst-case is better
  2968. + if(count < max_size)
  2969. + std::sort(lastPos, bin_cache[u], offset_greaterthan<data_type, unsignedchar_type>(char_offset + 1));
  2970. + else
  2971. + reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(lastPos, bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes);
  2972. + }
  2973. + }
  2974. +
  2975. + //String sorting recursive implementation
  2976. + template <class RandomAccessIter, class data_type, class unsignedchar_type, class get_char, class get_length>
  2977. + inline void
  2978. + string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  2979. + , unsigned cache_offset, std::vector<size_t> &bin_sizes, get_char getchar, get_length length)
  2980. + {
  2981. + //This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  2982. + //Iterate to the end of the empties. If all empty, return
  2983. + while(length(*first) <= char_offset) {
  2984. + if(++first == last)
  2985. + return;
  2986. + }
  2987. + RandomAccessIter finish = last - 1;
  2988. + //Getting the last non-empty
  2989. + for(;length(*finish) <= char_offset; --finish) { }
  2990. + ++finish;
  2991. + update_offset(first, finish, char_offset, getchar, length);
  2992. +
  2993. + const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  2994. + //Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  2995. + const unsigned max_size = bin_count;
  2996. + const unsigned membin_count = bin_count + 1;
  2997. + unsigned cache_end;
  2998. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count) + 1;
  2999. +
  3000. + //Calculating the size of each bin; this takes roughly 10% of runtime
  3001. + for (RandomAccessIter current = first; current != last; ++current) {
  3002. + if(length(*current) <= char_offset) {
  3003. + bin_sizes[0]++;
  3004. + }
  3005. + else
  3006. + bin_sizes[getchar((*current), char_offset) + 1]++;
  3007. + }
  3008. + //Assign the bin positions
  3009. + bin_cache[cache_offset] = first;
  3010. + for(unsigned u = 0; u < membin_count - 1; u++)
  3011. + bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  3012. +
  3013. + //Swap into place
  3014. + RandomAccessIter nextbinstart = first;
  3015. + //handling empty bins
  3016. + RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
  3017. + nextbinstart += bin_sizes[0];
  3018. + RandomAccessIter * target_bin;
  3019. + //Iterating over each element in the bin of empties
  3020. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  3021. + //empties belong in this bin
  3022. + while(length(*current) > char_offset) {
  3023. + target_bin = bins + getchar((*current), char_offset);
  3024. + iter_swap(current, (*target_bin)++);
  3025. + }
  3026. + }
  3027. + *local_bin = nextbinstart;
  3028. + //iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  3029. + unsigned last_bin = bin_count - 1;
  3030. + for(; last_bin && !bin_sizes[last_bin + 1]; --last_bin) { }
  3031. + //This dominates runtime, mostly in the swap and bin lookups
  3032. + for(unsigned ii = 0; ii < last_bin; ++ii) {
  3033. + local_bin = bins + ii;
  3034. + nextbinstart += bin_sizes[ii + 1];
  3035. + //Iterating over each element in this bin
  3036. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  3037. + //Swapping elements in current into place until the correct element has been swapped in
  3038. + for(target_bin = bins + getchar((*current), char_offset); target_bin != local_bin;
  3039. + target_bin = bins + getchar((*current), char_offset))
  3040. + iter_swap(current, (*target_bin)++);
  3041. + }
  3042. + *local_bin = nextbinstart;
  3043. + }
  3044. + bins[last_bin] = last;
  3045. +
  3046. + //Recursing
  3047. + RandomAccessIter lastPos = bin_cache[cache_offset];
  3048. + //Skip this loop for empties
  3049. + for(unsigned u = cache_offset + 1; u < cache_offset + last_bin + 2; lastPos = bin_cache[u], ++u) {
  3050. + size_t count = bin_cache[u] - lastPos;
  3051. + //don't sort unless there are at least two items to compare
  3052. + if(count < 2)
  3053. + continue;
  3054. + //using std::sort if its worst-case is better
  3055. + if(count < max_size)
  3056. + std::sort(lastPos, bin_cache[u], offset_char_lessthan<data_type, get_char, get_length>(char_offset + 1));
  3057. + else
  3058. + string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length>(lastPos, bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes, getchar, length);
  3059. + }
  3060. + }
  3061. +
  3062. + //String sorting recursive implementation
  3063. + template <class RandomAccessIter, class data_type, class unsignedchar_type, class get_char, class get_length, class compare>
  3064. + inline void
  3065. + string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  3066. + , unsigned cache_offset, std::vector<size_t> &bin_sizes, get_char getchar, get_length length, compare comp)
  3067. + {
  3068. + //This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  3069. + //Iterate to the end of the empties. If all empty, return
  3070. + while(length(*first) <= char_offset) {
  3071. + if(++first == last)
  3072. + return;
  3073. + }
  3074. + RandomAccessIter finish = last - 1;
  3075. + //Getting the last non-empty
  3076. + for(;length(*finish) <= char_offset; --finish) { }
  3077. + ++finish;
  3078. + update_offset(first, finish, char_offset, getchar, length);
  3079. +
  3080. + const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  3081. + //Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  3082. + const unsigned max_size = bin_count;
  3083. + const unsigned membin_count = bin_count + 1;
  3084. + unsigned cache_end;
  3085. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count) + 1;
  3086. +
  3087. + //Calculating the size of each bin; this takes roughly 10% of runtime
  3088. + for (RandomAccessIter current = first; current != last; ++current) {
  3089. + if(length(*current) <= char_offset) {
  3090. + bin_sizes[0]++;
  3091. + }
  3092. + else
  3093. + bin_sizes[getchar((*current), char_offset) + 1]++;
  3094. + }
  3095. + //Assign the bin positions
  3096. + bin_cache[cache_offset] = first;
  3097. + for(unsigned u = 0; u < membin_count - 1; u++)
  3098. + bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  3099. +
  3100. + //Swap into place
  3101. + RandomAccessIter nextbinstart = first;
  3102. + //handling empty bins
  3103. + RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
  3104. + nextbinstart += bin_sizes[0];
  3105. + RandomAccessIter * target_bin;
  3106. + //Iterating over each element in the bin of empties
  3107. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  3108. + //empties belong in this bin
  3109. + while(length(*current) > char_offset) {
  3110. + target_bin = bins + getchar((*current), char_offset);
  3111. + iter_swap(current, (*target_bin)++);
  3112. + }
  3113. + }
  3114. + *local_bin = nextbinstart;
  3115. + //iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  3116. + unsigned last_bin = bin_count - 1;
  3117. + for(; last_bin && !bin_sizes[last_bin + 1]; --last_bin) { }
  3118. + //This dominates runtime, mostly in the swap and bin lookups
  3119. + for(unsigned u = 0; u < last_bin; ++u) {
  3120. + local_bin = bins + u;
  3121. + nextbinstart += bin_sizes[u + 1];
  3122. + //Iterating over each element in this bin
  3123. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  3124. + //Swapping elements in current into place until the correct element has been swapped in
  3125. + for(target_bin = bins + getchar((*current), char_offset); target_bin != local_bin;
  3126. + target_bin = bins + getchar((*current), char_offset))
  3127. + iter_swap(current, (*target_bin)++);
  3128. + }
  3129. + *local_bin = nextbinstart;
  3130. + }
  3131. + bins[last_bin] = last;
  3132. +
  3133. + //Recursing
  3134. + RandomAccessIter lastPos = bin_cache[cache_offset];
  3135. + //Skip this loop for empties
  3136. + for(unsigned u = cache_offset + 1; u < cache_offset + last_bin + 2; lastPos = bin_cache[u], ++u) {
  3137. + size_t count = bin_cache[u] - lastPos;
  3138. + //don't sort unless there are at least two items to compare
  3139. + if(count < 2)
  3140. + continue;
  3141. + //using std::sort if its worst-case is better
  3142. + if(count < max_size)
  3143. + std::sort(lastPos, bin_cache[u], comp);
  3144. + else
  3145. + string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(lastPos
  3146. + , bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes, getchar, length, comp);
  3147. + }
  3148. + }
  3149. +
  3150. + //Sorts strings in reverse order, with empties at the end
  3151. + template <class RandomAccessIter, class data_type, class unsignedchar_type, class get_char, class get_length, class compare>
  3152. + inline void
  3153. + reverse_string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  3154. + , unsigned cache_offset, std::vector<size_t> &bin_sizes, get_char getchar, get_length length, compare comp)
  3155. + {
  3156. + //This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  3157. + RandomAccessIter curr = first;
  3158. + //Iterate to the end of the empties. If all empty, return
  3159. + while(length(*curr) <= char_offset) {
  3160. + if(++curr == last)
  3161. + return;
  3162. + }
  3163. + //Getting the last non-empty
  3164. + while(length(*(--last)) <= char_offset) { }
  3165. + ++last;
  3166. + //Offsetting on identical characters. This section works a character at a time for optimal worst-case performance.
  3167. + update_offset(first, last, char_offset, getchar, length);
  3168. +
  3169. + const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  3170. + //Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  3171. + const unsigned max_size = bin_count;
  3172. + const unsigned membin_count = bin_count + 1;
  3173. + const unsigned max_bin = bin_count - 1;
  3174. + unsigned cache_end;
  3175. + RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count);
  3176. + RandomAccessIter *end_bin = &(bin_cache[cache_offset + max_bin]);
  3177. +
  3178. + //Calculating the size of each bin; this takes roughly 10% of runtime
  3179. + for (RandomAccessIter current = first; current != last; ++current) {
  3180. + if(length(*current) <= char_offset) {
  3181. + bin_sizes[bin_count]++;
  3182. + }
  3183. + else
  3184. + bin_sizes[max_bin - getchar((*current), char_offset)]++;
  3185. + }
  3186. + //Assign the bin positions
  3187. + bin_cache[cache_offset] = first;
  3188. + for(unsigned u = 0; u < membin_count - 1; u++)
  3189. + bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  3190. +
  3191. + //Swap into place
  3192. + RandomAccessIter nextbinstart = last;
  3193. + //handling empty bins
  3194. + RandomAccessIter * local_bin = &(bin_cache[cache_offset + bin_count]);
  3195. + RandomAccessIter lastFull = *local_bin;
  3196. + RandomAccessIter * target_bin;
  3197. + //Iterating over each element in the bin of empties
  3198. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  3199. + //empties belong in this bin
  3200. + while(length(*current) > char_offset) {
  3201. + target_bin = end_bin - getchar((*current), char_offset);
  3202. + iter_swap(current, (*target_bin)++);
  3203. + }
  3204. + }
  3205. + *local_bin = nextbinstart;
  3206. + nextbinstart = first;
  3207. + //iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  3208. + unsigned last_bin = max_bin;
  3209. + for(; last_bin && !bin_sizes[last_bin]; --last_bin) { }
  3210. + //This dominates runtime, mostly in the swap and bin lookups
  3211. + for(unsigned u = 0; u < last_bin; ++u) {
  3212. + local_bin = bins + u;
  3213. + nextbinstart += bin_sizes[u];
  3214. + //Iterating over each element in this bin
  3215. + for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  3216. + //Swapping elements in current into place until the correct element has been swapped in
  3217. + for(target_bin = end_bin - getchar((*current), char_offset); target_bin != local_bin;
  3218. + target_bin = end_bin - getchar((*current), char_offset))
  3219. + iter_swap(current, (*target_bin)++);
  3220. + }
  3221. + *local_bin = nextbinstart;
  3222. + }
  3223. + bins[last_bin] = lastFull;
  3224. + //Recursing
  3225. + RandomAccessIter lastPos = first;
  3226. + //Skip this loop for empties
  3227. + for(unsigned u = cache_offset; u <= cache_offset + last_bin; lastPos = bin_cache[u], ++u) {
  3228. + size_t count = bin_cache[u] - lastPos;
  3229. + //don't sort unless there are at least two items to compare
  3230. + if(count < 2)
  3231. + continue;
  3232. + //using std::sort if its worst-case is better
  3233. + if(count < max_size)
  3234. + std::sort(lastPos, bin_cache[u], comp);
  3235. + else
  3236. + reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(lastPos
  3237. + , bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes, getchar, length, comp);
  3238. + }
  3239. + }
  3240. +
  3241. + //Holds the bin vector and makes the initial recursive call
  3242. + template <class RandomAccessIter, class data_type, class unsignedchar_type>
  3243. + inline void
  3244. + string_sort(RandomAccessIter first, RandomAccessIter last, data_type, unsignedchar_type)
  3245. + {
  3246. + std::vector<size_t> bin_sizes;
  3247. + std::vector<RandomAccessIter> bin_cache;
  3248. + string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(first, last, 0, bin_cache, 0, bin_sizes);
  3249. + }
  3250. +
  3251. + //Holds the bin vector and makes the initial recursive call
  3252. + template <class RandomAccessIter, class data_type, class unsignedchar_type>
  3253. + inline void
  3254. + reverse_string_sort(RandomAccessIter first, RandomAccessIter last, data_type, unsignedchar_type)
  3255. + {
  3256. + std::vector<size_t> bin_sizes;
  3257. + std::vector<RandomAccessIter> bin_cache;
  3258. + reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(first, last, 0, bin_cache, 0, bin_sizes);
  3259. + }
  3260. +
  3261. + //Holds the bin vector and makes the initial recursive call
  3262. + template <class RandomAccessIter, class get_char, class get_length, class data_type, class unsignedchar_type>
  3263. + inline void
  3264. + string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, data_type, unsignedchar_type)
  3265. + {
  3266. + std::vector<size_t> bin_sizes;
  3267. + std::vector<RandomAccessIter> bin_cache;
  3268. + string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length>(first, last, 0, bin_cache, 0, bin_sizes, getchar, length);
  3269. + }
  3270. +
  3271. + //Holds the bin vector and makes the initial recursive call
  3272. + template <class RandomAccessIter, class get_char, class get_length, class compare, class data_type, class unsignedchar_type>
  3273. + inline void
  3274. + string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp, data_type, unsignedchar_type)
  3275. + {
  3276. + std::vector<size_t> bin_sizes;
  3277. + std::vector<RandomAccessIter> bin_cache;
  3278. + string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(first, last, 0, bin_cache, 0, bin_sizes, getchar, length, comp);
  3279. + }
  3280. +
  3281. + //Holds the bin vector and makes the initial recursive call
  3282. + template <class RandomAccessIter, class get_char, class get_length, class compare, class data_type, class unsignedchar_type>
  3283. + inline void
  3284. + reverse_string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp, data_type, unsignedchar_type)
  3285. + {
  3286. + std::vector<size_t> bin_sizes;
  3287. + std::vector<RandomAccessIter> bin_cache;
  3288. + reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(first, last, 0, bin_cache, 0, bin_sizes, getchar, length, comp);
  3289. + }
  3290. + }
  3291. +
  3292. + //Allows character-type overloads
  3293. + template <class RandomAccessIter, class unsignedchar_type>
  3294. + inline void string_sort(RandomAccessIter first, RandomAccessIter last, unsignedchar_type unused)
  3295. + {
  3296. + //Don't sort if it's too small to optimize
  3297. + if(last - first < detail::MIN_SORT_SIZE)
  3298. + std::sort(first, last);
  3299. + else
  3300. + detail::string_sort(first, last, *first, unused);
  3301. + }
  3302. +
  3303. + //Top-level sorting call; wraps using default of unsigned char
  3304. + template <class RandomAccessIter>
  3305. + inline void string_sort(RandomAccessIter first, RandomAccessIter last)
  3306. + {
  3307. + unsigned char unused = '\0';
  3308. + string_sort(first, last, unused);
  3309. + }
  3310. +
  3311. + //Allows character-type overloads
  3312. + template <class RandomAccessIter, class compare, class unsignedchar_type>
  3313. + inline void reverse_string_sort(RandomAccessIter first, RandomAccessIter last, compare comp, unsignedchar_type unused)
  3314. + {
  3315. + //Don't sort if it's too small to optimize
  3316. + if(last - first < detail::MIN_SORT_SIZE)
  3317. + std::sort(first, last, comp);
  3318. + else
  3319. + detail::reverse_string_sort(first, last, *first, unused);
  3320. + }
  3321. +
  3322. + //Top-level sorting call; wraps using default of unsigned char
  3323. + template <class RandomAccessIter, class compare>
  3324. + inline void reverse_string_sort(RandomAccessIter first, RandomAccessIter last, compare comp)
  3325. + {
  3326. + unsigned char unused = '\0';
  3327. + reverse_string_sort(first, last, comp, unused);
  3328. + }
  3329. +
  3330. + template <class RandomAccessIter, class get_char, class get_length>
  3331. + inline void string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length)
  3332. + {
  3333. + //Don't sort if it's too small to optimize
  3334. + if(last - first < detail::MIN_SORT_SIZE)
  3335. + std::sort(first, last);
  3336. + else {
  3337. + //skipping past empties at the beginning, which allows us to get the character type
  3338. + //.empty() is not used so as not to require a user declaration of it
  3339. + while(!length(*first)) {
  3340. + if(++first == last)
  3341. + return;
  3342. + }
  3343. + detail::string_sort(first, last, getchar, length, *first, getchar((*first), 0));
  3344. + }
  3345. + }
  3346. +
  3347. + template <class RandomAccessIter, class get_char, class get_length, class compare>
  3348. + inline void string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp)
  3349. + {
  3350. + //Don't sort if it's too small to optimize
  3351. + if(last - first < detail::MIN_SORT_SIZE)
  3352. + std::sort(first, last, comp);
  3353. + else {
  3354. + //skipping past empties at the beginning, which allows us to get the character type
  3355. + //.empty() is not used so as not to require a user declaration of it
  3356. + while(!length(*first)) {
  3357. + if(++first == last)
  3358. + return;
  3359. + }
  3360. + detail::string_sort(first, last, getchar, length, comp, *first, getchar((*first), 0));
  3361. + }
  3362. + }
  3363. +
  3364. + template <class RandomAccessIter, class get_char, class get_length, class compare>
  3365. + inline void reverse_string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp)
  3366. + {
  3367. + //Don't sort if it's too small to optimize
  3368. + if(last - first < detail::MIN_SORT_SIZE)
  3369. + std::sort(first, last, comp);
  3370. + else {
  3371. + //skipping past empties at the beginning, which allows us to get the character type
  3372. + //.empty() is not used so as not to require a user declaration of it
  3373. + while(!length(*(--last))) {
  3374. + //Note: if there is just one non-empty, and it's at the beginning, then it's already in sorted order
  3375. + if(first == last)
  3376. + return;
  3377. + }
  3378. + //making last just after the end of the non-empty part of the array
  3379. + ++last;
  3380. + detail::reverse_string_sort(first, last, getchar, length, comp, *first, getchar((*first), 0));
  3381. + }
  3382. + }
  3383. +}
  3384. +
  3385. +#endif