aboutsummaryrefslogtreecommitdiff
path: root/webrtc/system_wrappers/source/spreadsortlib/spreadsort.hpp
blob: 2d1529aacd55c7b466410da7654ac73be24f38ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
//Templated spread_sort library

//          Copyright Steven J. Ross 2001 - 2009.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org/ for updates, documentation, and revision history.
		  
/*
Some improvements suggested by:
Phil Endecott and Frank Gennari
Cygwin fix provided by:
Scott McMurray
*/

#ifndef BOOST_SPREAD_SORT_H
#define BOOST_SPREAD_SORT_H
#include <algorithm>
#include <vector>
#include "constants.hpp"
#include <cstring>

namespace boost {
  namespace detail {
  	//This only works on unsigned data types
  	template <typename T>
  	inline unsigned 
  	rough_log_2_size(const T& input) 
  	{
  		unsigned result = 0;
  		//The && is necessary on some compilers to avoid infinite loops; it doesn't significantly impair performance
  		while((input >> result) && (result < (8*sizeof(T)))) ++result;
  		return result;
  	}

  	//Gets the maximum size which we'll call spread_sort on to control worst-case performance
  	//Maintains both a minimum size to recurse and a check of distribution size versus count
  	//This is called for a set of bins, instead of bin-by-bin, to avoid performance overhead
  	inline size_t
  	get_max_count(unsigned log_range, size_t count)
  	{
  		unsigned divisor = rough_log_2_size(count);
  		//Making sure the divisor is positive
  		if(divisor > LOG_MEAN_BIN_SIZE)
  			divisor -= LOG_MEAN_BIN_SIZE;
  		else
  			divisor = 1;
  		unsigned relative_width = (LOG_CONST * log_range)/((divisor > MAX_SPLITS) ? MAX_SPLITS : divisor);
  		//Don't try to bitshift more than the size of an element
  		if((8*sizeof(size_t)) <= relative_width)
  			relative_width = (8*sizeof(size_t)) - 1;
  		return (size_t)1 << ((relative_width < (LOG_MEAN_BIN_SIZE + LOG_MIN_SPLIT_COUNT)) ? 
  			(LOG_MEAN_BIN_SIZE + LOG_MIN_SPLIT_COUNT) :  relative_width);
  	}

  	//Find the minimum and maximum using <
  	template <class RandomAccessIter>
  	inline void 
  	find_extremes(RandomAccessIter current, RandomAccessIter last, RandomAccessIter & max, RandomAccessIter & min)
  	{
  		min = max = current;
  		//Start from the second item, as max and min are initialized to the first
  		while(++current < last) {
  			if(*max < *current)
  				max = current;
  			else if(*current < *min)
  				min = current;
  		}
  	}

  	//Uses a user-defined comparison operator to find minimum and maximum
  	template <class RandomAccessIter, class compare>
  	inline void 
  	find_extremes(RandomAccessIter current, RandomAccessIter last, RandomAccessIter & max, RandomAccessIter & min, compare comp)
  	{
  		min = max = current;
  		while(++current < last) {
  			if(comp(*max, *current))
  				max = current;
  			else if(comp(*current, *min))
  				min = current;
  		}
  	}

  	//Gets a non-negative right bit shift to operate as a logarithmic divisor
  	inline int
  	get_log_divisor(size_t count, unsigned log_range)
  	{
  		int log_divisor;
  		//If we can finish in one iteration without exceeding either (2 to the MAX_SPLITS) or n bins, do so
  		if((log_divisor = log_range - rough_log_2_size(count)) <= 0 && log_range < MAX_SPLITS)
  			log_divisor = 0;
  		else {
  			//otherwise divide the data into an optimized number of pieces
  			log_divisor += LOG_MEAN_BIN_SIZE;
  			if(log_divisor < 0)
  				log_divisor = 0;
  			//Cannot exceed MAX_SPLITS or cache misses slow down bin lookups dramatically
  			if((log_range - log_divisor) > MAX_SPLITS)
  				log_divisor = log_range - MAX_SPLITS;
  		}
  		return log_divisor;
  	}

  	template <class RandomAccessIter>
  	inline RandomAccessIter * 
  	size_bins(std::vector<size_t> &bin_sizes, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset, unsigned &cache_end, unsigned bin_count)
  	{
  		//Assure space for the size of each bin, followed by initializing sizes
  		if(bin_count > bin_sizes.size())
  			bin_sizes.resize(bin_count);
  		for(size_t u = 0; u < bin_count; u++)
  			bin_sizes[u] = 0;
  		//Make sure there is space for the bins
  		cache_end = cache_offset + bin_count;
  		if(cache_end > bin_cache.size())
  			bin_cache.resize(cache_end);
  		return &(bin_cache[cache_offset]);
  	}

  	//Implementation for recursive integer sorting
  	template <class RandomAccessIter, class div_type, class data_type>
  	inline void 
  	spread_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  				  , std::vector<size_t> &bin_sizes)
  	{
  		//This step is roughly 10% of runtime, but it helps avoid worst-case behavior and improve behavior with real data
  		//If you know the maximum and minimum ahead of time, you can pass those values in and skip this step for the first iteration
  		RandomAccessIter max, min;
  		find_extremes(first, last, max, min);
  		//max and min will be the same (the first item) iff all values are equivalent
  		if(max == min)
  			return;
  		RandomAccessIter * target_bin;
  		unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(*max >> 0) - (*min >> 0)));
  		div_type div_min = *min >> log_divisor;
  		div_type div_max = *max >> log_divisor;
  		unsigned bin_count = div_max - div_min + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  	
  		//Calculating the size of each bin; this takes roughly 10% of runtime
  		for (RandomAccessIter current = first; current != last;)
  			bin_sizes[(*(current++) >> log_divisor) - div_min]++;
  		//Assign the bin positions
  		bins[0] = first;
  		for(unsigned u = 0; u < bin_count - 1; u++)
  			bins[u + 1] = bins[u] + bin_sizes[u];
  
  		//Swap into place
  		//This dominates runtime, mostly in the swap and bin lookups
  		RandomAccessIter nextbinstart = first;
  		for(unsigned u = 0; u < bin_count - 1; ++u) {
  			RandomAccessIter * local_bin = bins + u;
  			nextbinstart += bin_sizes[u];
  			//Iterating over each element in this bin
  			for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  				//Swapping elements in current into place until the correct element has been swapped in
  				for(target_bin = (bins + ((*current >> log_divisor) - div_min));  target_bin != local_bin; 
  					target_bin = bins + ((*current >> log_divisor) - div_min)) {
  					//3-way swap; this is about 1% faster than a 2-way swap with integers
  					//The main advantage is less copies are involved per item put in the correct place
  					data_type tmp;
  					RandomAccessIter b = (*target_bin)++;
  					RandomAccessIter * b_bin = bins + ((*b >> log_divisor) - div_min);
  					if (b_bin != local_bin) {
  						RandomAccessIter c = (*b_bin)++;
  						tmp = *c;
  						*c = *b;
  					} 
  					else
  						tmp = *b;
  					*b = *current;
  					*current = tmp;
  				}
  			}
  			*local_bin = nextbinstart;
  		}
  		bins[bin_count - 1] = last;
  
  		//If we've bucketsorted, the array is sorted and we should skip recursion
  		if(!log_divisor)
  			return;
  
  		//Recursing; log_divisor is the remaining range
  		size_t max_count = get_max_count(log_divisor, last - first);
  		RandomAccessIter lastPos = first;
  		for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			//don't sort unless there are at least two items to compare
  			if(count < 2)
  				continue;
  			//using std::sort if its worst-case is better
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[u]);
  			else
  				spread_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes);
  		}
  	}

  	//Generic bitshift-based 3-way swapping code
  	template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  	inline void inner_swap_loop(RandomAccessIter * bins, const RandomAccessIter & nextbinstart, unsigned ii, right_shift &shift
  		, const unsigned log_divisor, const div_type div_min) 
  	{
  		RandomAccessIter * local_bin = bins + ii;
  		for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  			for(RandomAccessIter * target_bin = (bins + (shift(*current, log_divisor) - div_min));  target_bin != local_bin; 
  				target_bin = bins + (shift(*current, log_divisor) - div_min)) {
  				data_type tmp;
  				RandomAccessIter b = (*target_bin)++;
  				RandomAccessIter * b_bin = bins + (shift(*b, log_divisor) - div_min);
  				//Three-way swap; if the item to be swapped doesn't belong in the current bin, swap it to where it belongs
  				if (b_bin != local_bin) {
  					RandomAccessIter c = (*b_bin)++;
  					tmp = *c;
  					*c = *b;
  				} 
  				//Note: we could increment current once the swap is done in this case, but that seems to impair performance
  				else
  					tmp = *b;
  				*b = *current;
  				*current = tmp;
  			}
  		}
  		*local_bin = nextbinstart;
  	}

  	//Standard swapping wrapper for ascending values
  	template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  	inline void swap_loop(RandomAccessIter * bins, RandomAccessIter & nextbinstart, unsigned ii, right_shift &shift
  		, const std::vector<size_t> &bin_sizes, const unsigned log_divisor, const div_type div_min) 
  	{
  		nextbinstart += bin_sizes[ii];
  		inner_swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, log_divisor, div_min);
  	}

  	//Functor implementation for recursive sorting
  	template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  	inline void 
  	spread_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  					, std::vector<size_t> &bin_sizes, right_shift shift, compare comp)
  	{
  		RandomAccessIter max, min;
  		find_extremes(first, last, max, min, comp);
  		if(max == min)
  			return;
  		unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(shift(*max, 0)) - (shift(*min, 0))));
  		div_type div_min = shift(*min, log_divisor);
  		div_type div_max = shift(*max, log_divisor);
  		unsigned bin_count = div_max - div_min + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  			
  		//Calculating the size of each bin
  		for (RandomAccessIter current = first; current != last;)
  			bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  		bins[0] = first;
  		for(unsigned u = 0; u < bin_count - 1; u++)
  			bins[u + 1] = bins[u] + bin_sizes[u];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		for(unsigned u = 0; u < bin_count - 1; ++u)
  			swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, u, shift, bin_sizes, log_divisor, div_min);
  		bins[bin_count - 1] = last;
  		
  		//If we've bucketsorted, the array is sorted and we should skip recursion
  		if(!log_divisor)
  			return;
  		
  		//Recursing
  		size_t max_count = get_max_count(log_divisor, last - first);
  		RandomAccessIter lastPos = first;
  		for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[u], comp);
  			else
  				spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift, compare>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift, comp);
  		}
  	}

  	//Functor implementation for recursive sorting with only Shift overridden
  	template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  	inline void 
  	spread_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  					, std::vector<size_t> &bin_sizes, right_shift shift)
  	{
  		RandomAccessIter max, min;
  		find_extremes(first, last, max, min);
  		if(max == min)
  			return;
  		unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(shift(*max, 0)) - (shift(*min, 0))));
  		div_type div_min = shift(*min, log_divisor);
  		div_type div_max = shift(*max, log_divisor);
  		unsigned bin_count = div_max - div_min + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  			
  		//Calculating the size of each bin
  		for (RandomAccessIter current = first; current != last;)
  			bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  		bins[0] = first;
  		for(unsigned u = 0; u < bin_count - 1; u++)
  			bins[u + 1] = bins[u] + bin_sizes[u];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		for(unsigned ii = 0; ii < bin_count - 1; ++ii)
  			swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, bin_sizes, log_divisor, div_min);
  		bins[bin_count - 1] = last;
  		
  		//If we've bucketsorted, the array is sorted and we should skip recursion
  		if(!log_divisor)
  			return;
  		
  		//Recursing
  		size_t max_count = get_max_count(log_divisor, last - first);
  		RandomAccessIter lastPos = first;
  		for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[u]);
  			else
  				spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift);
  		}
  	}

  	//Holds the bin vector and makes the initial recursive call
  	template <class RandomAccessIter, class div_type, class data_type>
  	inline void 
  	spread_sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		spread_sort_rec<RandomAccessIter, div_type, data_type>(first, last, bin_cache, 0, bin_sizes);
  	}

  	template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  	inline void 
  	spread_sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift, compare comp)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift, compare>(first, last, bin_cache, 0, bin_sizes, shift, comp);
  	}

  	template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  	inline void 
  	spread_sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(first, last, bin_cache, 0, bin_sizes, shift);
  	}
  }

  //Top-level sorting call for integers
  template <class RandomAccessIter>
  inline void integer_sort(RandomAccessIter first, RandomAccessIter last) 
  {
  	//Don't sort if it's too small to optimize
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last);
  	else
  		detail::spread_sort(first, last, *first >> 0, *first);
  }

  //integer_sort with functors
  template <class RandomAccessIter, class right_shift, class compare>
  inline void integer_sort(RandomAccessIter first, RandomAccessIter last,
  						right_shift shift, compare comp) {
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last, comp);
  	else
  		detail::spread_sort(first, last, shift(*first, 0), *first, shift, comp);
  }

  //integer_sort with right_shift functor
  template <class RandomAccessIter, class right_shift>
  inline void integer_sort(RandomAccessIter first, RandomAccessIter last,
  						right_shift shift) {
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last);
  	else
  		detail::spread_sort(first, last, shift(*first, 0), *first, shift);
  }

  //------------------------------------------------------ float_sort source --------------------------------------
  //Casts a RandomAccessIter to the specified data type
  template<class cast_type, class RandomAccessIter>
  inline cast_type
  cast_float_iter(const RandomAccessIter & floatiter)
  {
  	cast_type result;
  	std::memcpy(&result, &(*floatiter), sizeof(cast_type));
  	return result;
  }

  //Casts a data element to the specified datinner_float_a type
  template<class data_type, class cast_type>
  inline cast_type
  mem_cast(const data_type & data)
  {
  	cast_type result;
  	std::memcpy(&result, &data, sizeof(cast_type));
  	return result;
  }

  namespace detail {
  	template <class RandomAccessIter, class div_type, class right_shift>
  	inline void 
  	find_extremes(RandomAccessIter current, RandomAccessIter last, div_type & max, div_type & min, right_shift shift)
  	{
  		min = max = shift(*current, 0);
  		while(++current < last) {
  			div_type value = shift(*current, 0);
  			if(max < value)
  				max = value;
  			else if(value < min)
  				min = value;
  		}
  	}

  	//Specialized swap loops for floating-point casting
  	template <class RandomAccessIter, class div_type, class data_type>
  	inline void inner_float_swap_loop(RandomAccessIter * bins, const RandomAccessIter & nextbinstart, unsigned ii
  		, const unsigned log_divisor, const div_type div_min) 
  	{
  		RandomAccessIter * local_bin = bins + ii;
  		for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  			for(RandomAccessIter * target_bin = (bins + ((cast_float_iter<div_type, RandomAccessIter>(current) >> log_divisor) - div_min));  target_bin != local_bin; 
  				target_bin = bins + ((cast_float_iter<div_type, RandomAccessIter>(current) >> log_divisor) - div_min)) {
  				data_type tmp;
  				RandomAccessIter b = (*target_bin)++;
  				RandomAccessIter * b_bin = bins + ((cast_float_iter<div_type, RandomAccessIter>(b) >> log_divisor) - div_min);
  				//Three-way swap; if the item to be swapped doesn't belong in the current bin, swap it to where it belongs
  				if (b_bin != local_bin) {
  					RandomAccessIter c = (*b_bin)++;
  					tmp = *c;
  					*c = *b;
  				} 
  				else
  					tmp = *b;
  				*b = *current;
  				*current = tmp;
  			}
  		}
  		*local_bin = nextbinstart;
  	}

  	template <class RandomAccessIter, class div_type, class data_type>
  	inline void float_swap_loop(RandomAccessIter * bins, RandomAccessIter & nextbinstart, unsigned ii
  		, const std::vector<size_t> &bin_sizes, const unsigned log_divisor, const div_type div_min) 
  	{
  		nextbinstart += bin_sizes[ii];
  		inner_float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, ii, log_divisor, div_min);
  	}

  	template <class RandomAccessIter, class cast_type>
  	inline void 
  	find_extremes(RandomAccessIter current, RandomAccessIter last, cast_type & max, cast_type & min)
  	{
  		min = max = cast_float_iter<cast_type, RandomAccessIter>(current);
  		while(++current < last) {
  			cast_type value = cast_float_iter<cast_type, RandomAccessIter>(current);
  			if(max < value)
  				max = value;
  			else if(value < min)
  				min = value;
  		}
  	}

  	//Special-case sorting of positive floats with casting instead of a right_shift
  	template <class RandomAccessIter, class div_type, class data_type>
  	inline void 
  	positive_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  					, std::vector<size_t> &bin_sizes)
  	{
  		div_type max, min;
  		find_extremes(first, last, max, min);
  		if(max == min)
  			return;
  		unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  		div_type div_min = min >> log_divisor;
  		div_type div_max = max >> log_divisor;
  		unsigned bin_count = div_max - div_min + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  			
  		//Calculating the size of each bin
  		for (RandomAccessIter current = first; current != last;)
  			bin_sizes[(cast_float_iter<div_type, RandomAccessIter>(current++) >> log_divisor) - div_min]++;
  		bins[0] = first;
  		for(unsigned u = 0; u < bin_count - 1; u++)
  			bins[u + 1] = bins[u] + bin_sizes[u];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		for(unsigned u = 0; u < bin_count - 1; ++u)
  			float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, u, bin_sizes, log_divisor, div_min);
  		bins[bin_count - 1] = last;
  		
  		//Return if we've completed bucketsorting
  		if(!log_divisor)
  			return;
  		
  		//Recursing
  		size_t max_count = get_max_count(log_divisor, last - first);
  		RandomAccessIter lastPos = first;
  		for(unsigned u = cache_offset; u < cache_end; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[u]);
  			else
  				positive_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes);
  		}
  	}

  	//Sorting negative_ float_s
  	//Note that bins are iterated in reverse order because max_neg_float = min_neg_int
  	template <class RandomAccessIter, class div_type, class data_type>
  	inline void 
  	negative_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  					, std::vector<size_t> &bin_sizes)
  	{
  		div_type max, min;
  		find_extremes(first, last, max, min);
  		if(max == min)
  			return;
  		unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  		div_type div_min = min >> log_divisor;
  		div_type div_max = max >> log_divisor;
  		unsigned bin_count = div_max - div_min + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  			
  		//Calculating the size of each bin
  		for (RandomAccessIter current = first; current != last;)
  			bin_sizes[(cast_float_iter<div_type, RandomAccessIter>(current++) >> log_divisor) - div_min]++;
  		bins[bin_count - 1] = first;
  		for(int ii = bin_count - 2; ii >= 0; --ii)
  			bins[ii] = bins[ii + 1] + bin_sizes[ii + 1];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		//The last bin will always have the correct elements in it
  		for(int ii = bin_count - 1; ii > 0; --ii)
  			float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, ii, bin_sizes, log_divisor, div_min);
  		//Since we don't process the last bin, we need to update its end position
  		bin_cache[cache_offset] = last;
  		
  		//Return if we've completed bucketsorting
  		if(!log_divisor)
  			return;
  		
  		//Recursing
  		size_t max_count = get_max_count(log_divisor, last - first);
  		RandomAccessIter lastPos = first;
  		for(int ii = cache_end - 1; ii >= (int)cache_offset; lastPos = bin_cache[ii], --ii) {
  			size_t count = bin_cache[ii] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[ii]);
  			else
  				negative_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes);
  		}
  	}

  	//Sorting negative_ float_s
  	//Note that bins are iterated in reverse order because max_neg_float = min_neg_int
  	template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  	inline void 
  	negative_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  					, std::vector<size_t> &bin_sizes, right_shift shift)
  	{
  		div_type max, min;
  		find_extremes(first, last, max, min, shift);
  		if(max == min)
  			return;
  		unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  		div_type div_min = min >> log_divisor;
  		div_type div_max = max >> log_divisor;
  		unsigned bin_count = div_max - div_min + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  			
  		//Calculating the size of each bin
  		for (RandomAccessIter current = first; current != last;)
  			bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  		bins[bin_count - 1] = first;
  		for(int ii = bin_count - 2; ii >= 0; --ii)
  			bins[ii] = bins[ii + 1] + bin_sizes[ii + 1];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		//The last bin will always have the correct elements in it
  		for(int ii = bin_count - 1; ii > 0; --ii)
  			swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, bin_sizes, log_divisor, div_min);
  		//Since we don't process the last bin, we need to update its end position
  		bin_cache[cache_offset] = last;
  		
  		//Return if we've completed bucketsorting
  		if(!log_divisor)
  			return;
  		
  		//Recursing
  		size_t max_count = get_max_count(log_divisor, last - first);
  		RandomAccessIter lastPos = first;
  		for(int ii = cache_end - 1; ii >= (int)cache_offset; lastPos = bin_cache[ii], --ii) {
  			size_t count = bin_cache[ii] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[ii]);
  			else
  				negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift);
  		}
  	}

  	template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  	inline void 
  	negative_float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  					, std::vector<size_t> &bin_sizes, right_shift shift, compare comp)
  	{
  		div_type max, min;
  		find_extremes(first, last, max, min, shift);
  		if(max == min)
  			return;
  		unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  		div_type div_min = min >> log_divisor;
  		div_type div_max = max >> log_divisor;
  		unsigned bin_count = div_max - div_min + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  			
  		//Calculating the size of each bin
  		for (RandomAccessIter current = first; current != last;)
  			bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  		bins[bin_count - 1] = first;
  		for(int ii = bin_count - 2; ii >= 0; --ii)
  			bins[ii] = bins[ii + 1] + bin_sizes[ii + 1];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		//The last bin will always have the correct elements in it
  		for(int ii = bin_count - 1; ii > 0; --ii)
  			swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, ii, shift, bin_sizes, log_divisor, div_min);
  		//Since we don't process the last bin, we need to update its end position
  		bin_cache[cache_offset] = last;
  		
  		//Return if we've completed bucketsorting
  		if(!log_divisor)
  			return;
  		
  		//Recursing
  		size_t max_count = get_max_count(log_divisor, last - first);
  		RandomAccessIter lastPos = first;
  		for(int ii = cache_end - 1; ii >= (int)cache_offset; lastPos = bin_cache[ii], --ii) {
  			size_t count = bin_cache[ii] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[ii], comp);
  			else
  				negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift, compare>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift, comp);
  		}
  	}

  	//Casting special-case for floating-point sorting
  	template <class RandomAccessIter, class div_type, class data_type>
  	inline void 
  	float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  					, std::vector<size_t> &bin_sizes)
  	{
  		div_type max, min;
  		find_extremes(first, last, max, min);
  		if(max == min)
  			return;
  		unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  		div_type div_min = min >> log_divisor;
  		div_type div_max = max >> log_divisor;
  		unsigned bin_count = div_max - div_min + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  			
  		//Calculating the size of each bin
  		for (RandomAccessIter current = first; current != last;)
  			bin_sizes[(cast_float_iter<div_type, RandomAccessIter>(current++) >> log_divisor) - div_min]++;
  		//The index of the first positive bin
  		div_type first_positive = (div_min < 0) ? -div_min : 0;
  		//Resetting if all bins are negative
  		if(cache_offset + first_positive > cache_end)
  			first_positive = cache_end - cache_offset;
  		//Reversing the order of the negative bins
  		//Note that because of the negative/positive ordering direction flip
  		//We can not depend upon bin order and positions matching up
  		//so bin_sizes must be reused to contain the end of the bin
  		if(first_positive > 0) {
  			bins[first_positive - 1] = first;
  			for(int ii = first_positive - 2; ii >= 0; --ii) {
  				bins[ii] = first + bin_sizes[ii + 1];
  				bin_sizes[ii] += bin_sizes[ii + 1];
  			}
  			//Handling positives following negatives
  			if((unsigned)first_positive < bin_count) {
  				bins[first_positive] = first + bin_sizes[0];
  				bin_sizes[first_positive] += bin_sizes[0];
  			}
  		}
  		else
  			bins[0] = first;
  		for(unsigned u = first_positive; u < bin_count - 1; u++) {
  			bins[u + 1] = first + bin_sizes[u];
  			bin_sizes[u + 1] += bin_sizes[u];
  		}
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		for(unsigned u = 0; u < bin_count; ++u) {
  			nextbinstart = first + bin_sizes[u];
  			inner_float_swap_loop<RandomAccessIter, div_type, data_type>(bins, nextbinstart, u, log_divisor, div_min);
  		}
  		
  		if(!log_divisor)
  			return;
  		
  		//Handling negative values first
  		size_t max_count = get_max_count(log_divisor, last - first);
  		RandomAccessIter lastPos = first;
  		for(int ii = cache_offset + first_positive - 1; ii >= (int)cache_offset ; lastPos = bin_cache[ii--]) {
  			size_t count = bin_cache[ii] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[ii]);
  			//sort negative values using reversed-bin spread_sort
  			else
  				negative_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes);
  		}
  		
  		for(unsigned u = cache_offset + first_positive; u < cache_end; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[u]);
  			//sort positive values using normal spread_sort
  			else
  				positive_float_sort_rec<RandomAccessIter, div_type, data_type>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes);
  		}
  	}

  	//Functor implementation for recursive sorting
  	template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  	inline void 
  	float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  					, std::vector<size_t> &bin_sizes, right_shift shift)
  	{
  		div_type max, min;
  		find_extremes(first, last, max, min, shift);
  		if(max == min)
  			return;
  		unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  		div_type div_min = min >> log_divisor;
  		div_type div_max = max >> log_divisor;
  		unsigned bin_count = div_max - div_min + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  			
  		//Calculating the size of each bin
  		for (RandomAccessIter current = first; current != last;)
  			bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  		//The index of the first positive bin
  		div_type first_positive = (div_min < 0) ? -div_min : 0;
  		//Resetting if all bins are negative
  		if(cache_offset + first_positive > cache_end)
  			first_positive = cache_end - cache_offset;
  		//Reversing the order of the negative bins
  		//Note that because of the negative/positive ordering direction flip
  		//We can not depend upon bin order and positions matching up
  		//so bin_sizes must be reused to contain the end of the bin
  		if(first_positive > 0) {
  			bins[first_positive - 1] = first;
  			for(int ii = first_positive - 2; ii >= 0; --ii) {
  				bins[ii] = first + bin_sizes[ii + 1];
  				bin_sizes[ii] += bin_sizes[ii + 1];
  			}
  			//Handling positives following negatives
  			if((unsigned)first_positive < bin_count) {
  				bins[first_positive] = first + bin_sizes[0];
  				bin_sizes[first_positive] += bin_sizes[0];
  			}
  		}
  		else
  			bins[0] = first;
  		for(unsigned u = first_positive; u < bin_count - 1; u++) {
  			bins[u + 1] = first + bin_sizes[u];
  			bin_sizes[u + 1] += bin_sizes[u];
  		}
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		for(unsigned u = 0; u < bin_count; ++u) {
  			nextbinstart = first + bin_sizes[u];
  			inner_swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, u, shift, log_divisor, div_min);
  		}
  		
  		//Return if we've completed bucketsorting
  		if(!log_divisor)
  			return;
  		
  		//Handling negative values first
  		size_t max_count = get_max_count(log_divisor, last - first);
  		RandomAccessIter lastPos = first;
  		for(int ii = cache_offset + first_positive - 1; ii >= (int)cache_offset ; lastPos = bin_cache[ii--]) {
  			size_t count = bin_cache[ii] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[ii]);
  			//sort negative values using reversed-bin spread_sort
  			else
  				negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift);
  		}
  		
  		for(unsigned u = cache_offset + first_positive; u < cache_end; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[u]);
  			//sort positive values using normal spread_sort
  			else
  				spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift);
  		}
  	}

  	template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  	inline void 
  	float_sort_rec(RandomAccessIter first, RandomAccessIter last, std::vector<RandomAccessIter> &bin_cache, unsigned cache_offset
  					, std::vector<size_t> &bin_sizes, right_shift shift, compare comp)
  	{
  		div_type max, min;
  		find_extremes(first, last, max, min, shift);
  		if(max == min)
  			return;
  		unsigned log_divisor = get_log_divisor(last - first, rough_log_2_size((size_t)(max) - min));
  		div_type div_min = min >> log_divisor;
  		div_type div_max = max >> log_divisor;
  		unsigned bin_count = div_max - div_min + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, bin_count);
  			
  		//Calculating the size of each bin
  		for (RandomAccessIter current = first; current != last;)
  			bin_sizes[shift(*(current++), log_divisor) - div_min]++;
  		//The index of the first positive bin
  		div_type first_positive = (div_min < 0) ? -div_min : 0;
  		//Resetting if all bins are negative
  		if(cache_offset + first_positive > cache_end)
  			first_positive = cache_end - cache_offset;
  		//Reversing the order of the negative bins
  		//Note that because of the negative/positive ordering direction flip
  		//We can not depend upon bin order and positions matching up
  		//so bin_sizes must be reused to contain the end of the bin
  		if(first_positive > 0) {
  			bins[first_positive - 1] = first;
  			for(int ii = first_positive - 2; ii >= 0; --ii) {
  				bins[ii] = first + bin_sizes[ii + 1];
  				bin_sizes[ii] += bin_sizes[ii + 1];
  			}
  			//Handling positives following negatives
  			if((unsigned)first_positive < bin_count) {
  				bins[first_positive] = first + bin_sizes[0];
  				bin_sizes[first_positive] += bin_sizes[0];
  			}
  		}
  		else
  			bins[0] = first;
  		for(unsigned u = first_positive; u < bin_count - 1; u++) {
  			bins[u + 1] = first + bin_sizes[u];
  			bin_sizes[u + 1] += bin_sizes[u];
  		}
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		for(unsigned u = 0; u < bin_count; ++u) {
  			nextbinstart = first + bin_sizes[u];
  			inner_swap_loop<RandomAccessIter, div_type, data_type, right_shift>(bins, nextbinstart, u, shift, log_divisor, div_min);
  		}
  		
  		//Return if we've completed bucketsorting
  		if(!log_divisor)
  			return;
  		
  		//Handling negative values first
  		size_t max_count = get_max_count(log_divisor, last - first);
  		RandomAccessIter lastPos = first;
  		for(int ii = cache_offset + first_positive - 1; ii >= (int)cache_offset ; lastPos = bin_cache[ii--]) {
  			size_t count = bin_cache[ii] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[ii]);
  			//sort negative values using reversed-bin spread_sort
  			else
  				negative_float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[ii], bin_cache, cache_end, bin_sizes, shift, comp);
  		}
  		
  		for(unsigned u = cache_offset + first_positive; u < cache_end; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			if(count < 2)
  				continue;
  			if(count < max_count)
  				std::sort(lastPos, bin_cache[u]);
  			//sort positive values using normal spread_sort
  			else
  				spread_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(lastPos, bin_cache[u], bin_cache, cache_end, bin_sizes, shift, comp);
  		}
  	}

  	template <class RandomAccessIter, class cast_type, class data_type>
  	inline void 
  	float_Sort(RandomAccessIter first, RandomAccessIter last, cast_type, data_type)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		float_sort_rec<RandomAccessIter, cast_type, data_type>(first, last, bin_cache, 0, bin_sizes);
  	}

  	template <class RandomAccessIter, class div_type, class data_type, class right_shift>
  	inline void 
  	float_Sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(first, last, bin_cache, 0, bin_sizes, shift);
  	}

  	template <class RandomAccessIter, class div_type, class data_type, class right_shift, class compare>
  	inline void 
  	float_Sort(RandomAccessIter first, RandomAccessIter last, div_type, data_type, right_shift shift, compare comp)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		float_sort_rec<RandomAccessIter, div_type, data_type, right_shift>(first, last, bin_cache, 0, bin_sizes, shift, comp);
  	}
  }

  //float_sort with casting
  //The cast_type must be equal in size to the data type, and must be a signed integer
  template <class RandomAccessIter, class cast_type>
  inline void float_sort_cast(RandomAccessIter first, RandomAccessIter last, cast_type cVal) 
  {
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last);
  	else
  		detail::float_Sort(first, last, cVal, *first);
  }

  //float_sort with casting to an int
  //Only use this with IEEE floating-point numbers
  template <class RandomAccessIter>
  inline void float_sort_cast_to_int(RandomAccessIter first, RandomAccessIter last) 
  {
  	int cVal = 0;
  	float_sort_cast(first, last, cVal);
  }

  //float_sort with functors
  template <class RandomAccessIter, class right_shift>
  inline void float_sort(RandomAccessIter first, RandomAccessIter last, right_shift shift) 
  {
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last);
  	else
  		detail::float_Sort(first, last, shift(*first, 0), *first, shift);
  }

  template <class RandomAccessIter, class right_shift, class compare>
  inline void float_sort(RandomAccessIter first, RandomAccessIter last, right_shift shift, compare comp) 
  {
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last, comp);
  	else
  		detail::float_Sort(first, last, shift(*first, 0), *first, shift, comp);
  }

  //------------------------------------------------- string_sort source ---------------------------------------------
  namespace detail {
  	//Offsetting on identical characters.  This function works a character at a time for optimal worst-case performance.
  	template<class RandomAccessIter>
  	inline void
  	update_offset(RandomAccessIter first, RandomAccessIter finish, unsigned &char_offset)
  	{
  		unsigned nextOffset = char_offset;
  		bool done = false;
  		while(!done) {
  			RandomAccessIter curr = first;
  			do {
  				//ignore empties, but if the nextOffset would exceed the length or not match, exit; we've found the last matching character
  				if((*curr).size() > char_offset && ((*curr).size() <= (nextOffset + 1) || (*curr)[nextOffset] != (*first)[nextOffset])) {
  					done = true;
  					break;
  				}
  			} while(++curr != finish);
  			if(!done)
  				++nextOffset;
  		} 
  		char_offset = nextOffset;
  	}

  	//Offsetting on identical characters.  This function works a character at a time for optimal worst-case performance.
  	template<class RandomAccessIter, class get_char, class get_length>
  	inline void
  	update_offset(RandomAccessIter first, RandomAccessIter finish, unsigned &char_offset, get_char getchar, get_length length)
  	{
  		unsigned nextOffset = char_offset;
  		bool done = false;
  		while(!done) {
  			RandomAccessIter curr = first;
  			do {
  				//ignore empties, but if the nextOffset would exceed the length or not match, exit; we've found the last matching character
  				if(length(*curr) > char_offset && (length(*curr) <= (nextOffset + 1) || getchar((*curr), nextOffset) != getchar((*first), nextOffset))) {
  					done = true;
  					break;
  				}
  			} while(++curr != finish);
  			if(!done)
  				++nextOffset;
  		} 
  		char_offset = nextOffset;
  	}

  	//A comparison functor for strings that assumes they are identical up to char_offset
  	template<class data_type, class unsignedchar_type>
  	struct offset_lessthan {
  		offset_lessthan(unsigned char_offset) : fchar_offset(char_offset){}
  		inline bool operator()(const data_type &x, const data_type &y) const 
  		{
  			unsigned minSize = std::min(x.size(), y.size());
  			for(unsigned u = fchar_offset; u < minSize; ++u) {
  				if(static_cast<unsignedchar_type>(x[u]) < static_cast<unsignedchar_type>(y[u]))
  					return true;
  				else if(static_cast<unsignedchar_type>(y[u]) < static_cast<unsignedchar_type>(x[u]))
  					return false;
  			}
  			return x.size() < y.size();
  		}
  		unsigned fchar_offset;
  	};

  	//A comparison functor for strings that assumes they are identical up to char_offset
  	template<class data_type, class unsignedchar_type>
  	struct offset_greaterthan {
  		offset_greaterthan(unsigned char_offset) : fchar_offset(char_offset){}
  		inline bool operator()(const data_type &x, const data_type &y) const 
  		{
  			unsigned minSize = std::min(x.size(), y.size());
  			for(unsigned u = fchar_offset; u < minSize; ++u) {
  				if(static_cast<unsignedchar_type>(x[u]) > static_cast<unsignedchar_type>(y[u]))
  					return true;
  				else if(static_cast<unsignedchar_type>(y[u]) > static_cast<unsignedchar_type>(x[u]))
  					return false;
  			}
  			return x.size() > y.size();
  		}
  		unsigned fchar_offset;
  	};

  	//A comparison functor for strings that assumes they are identical up to char_offset
  	template<class data_type, class get_char, class get_length>
  	struct offset_char_lessthan {
  		offset_char_lessthan(unsigned char_offset) : fchar_offset(char_offset){}
  		inline bool operator()(const data_type &x, const data_type &y) const 
  		{
  			unsigned minSize = std::min(length(x), length(y));
  			for(unsigned u = fchar_offset; u < minSize; ++u) {
  				if(getchar(x, u) < getchar(y, u))
  					return true;
  				else if(getchar(y, u) < getchar(x, u))
  					return false;
  			}
  			return length(x) < length(y);
  		}
  		unsigned fchar_offset;
  		get_char getchar;
  		get_length length;
  	};

  	//String sorting recursive implementation
  	template <class RandomAccessIter, class data_type, class unsignedchar_type>
  	inline void 
  	string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  		, unsigned cache_offset, std::vector<size_t> &bin_sizes)
  	{
  		//This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  		//Iterate to the end of the empties.  If all empty, return
  		while((*first).size() <= char_offset) {
  			if(++first == last)
  				return;
  		}
  		RandomAccessIter finish = last - 1;
  		//Getting the last non-empty
  		for(;(*finish).size() <= char_offset; --finish) { }
  		++finish;
  		//Offsetting on identical characters.  This section works a character at a time for optimal worst-case performance.
  		update_offset(first, finish, char_offset);
  		
  		const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  		//Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  		const unsigned max_size = bin_count;
  		const unsigned membin_count = bin_count + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count) + 1;
  			
  		//Calculating the size of each bin; this takes roughly 10% of runtime
  		for (RandomAccessIter current = first; current != last; ++current) {
  			if((*current).size() <= char_offset) {
  				bin_sizes[0]++;
  			}
  			else
  				bin_sizes[static_cast<unsignedchar_type>((*current)[char_offset]) + 1]++;
  		}
  		//Assign the bin positions
  		bin_cache[cache_offset] = first;
  		for(unsigned u = 0; u < membin_count - 1; u++)
  			bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		//handling empty bins
  		RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
  		nextbinstart +=	bin_sizes[0];
  		RandomAccessIter * target_bin;
  		//Iterating over each element in the bin of empties
  		for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  			//empties belong in this bin
  			while((*current).size() > char_offset) {
  				target_bin = bins + static_cast<unsignedchar_type>((*current)[char_offset]);
  				iter_swap(current, (*target_bin)++);
  			}
  		}
  		*local_bin = nextbinstart;
  		//iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  		unsigned last_bin = bin_count - 1;
  		for(; last_bin && !bin_sizes[last_bin + 1]; --last_bin) { }
  		//This dominates runtime, mostly in the swap and bin lookups
  		for(unsigned u = 0; u < last_bin; ++u) {
  			local_bin = bins + u;
  			nextbinstart += bin_sizes[u + 1];
  			//Iterating over each element in this bin
  			for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  				//Swapping elements in current into place until the correct element has been swapped in
  				for(target_bin = bins + static_cast<unsignedchar_type>((*current)[char_offset]);  target_bin != local_bin; 
  					target_bin = bins + static_cast<unsignedchar_type>((*current)[char_offset]))
  					iter_swap(current, (*target_bin)++);
  			}
  			*local_bin = nextbinstart;
  		}
  		bins[last_bin] = last;
  		//Recursing
  		RandomAccessIter lastPos = bin_cache[cache_offset];
  		//Skip this loop for empties
  		for(unsigned u = cache_offset + 1; u < cache_offset + last_bin + 2; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			//don't sort unless there are at least two items to compare
  			if(count < 2)
  				continue;
  			//using std::sort if its worst-case is better
  			if(count < max_size)
  				std::sort(lastPos, bin_cache[u], offset_lessthan<data_type, unsignedchar_type>(char_offset + 1));
  			else
  				string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(lastPos, bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes);
  		}
  	}

  	//Sorts strings in reverse order, with empties at the end
  	template <class RandomAccessIter, class data_type, class unsignedchar_type>
  	inline void 
  	reverse_string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  		, unsigned cache_offset, std::vector<size_t> &bin_sizes)
  	{
  		//This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  		RandomAccessIter curr = first;
  		//Iterate to the end of the empties.  If all empty, return
  		while((*curr).size() <= char_offset) {
  			if(++curr == last)
  				return;
  		}
  		//Getting the last non-empty
  		while((*(--last)).size() <= char_offset) { }
  		++last;
  		//Offsetting on identical characters.  This section works a character at a time for optimal worst-case performance.
  		update_offset(curr, last, char_offset);
  		RandomAccessIter * target_bin;
  		
  		const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  		//Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  		const unsigned max_size = bin_count;
  		const unsigned membin_count = bin_count + 1;
  		const unsigned max_bin = bin_count - 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count);
  		RandomAccessIter * end_bin = &(bin_cache[cache_offset + max_bin]);
  			
  		//Calculating the size of each bin; this takes roughly 10% of runtime
  		for (RandomAccessIter current = first; current != last; ++current) {
  			if((*current).size() <= char_offset) {
  				bin_sizes[bin_count]++;
  			}
  			else
  				bin_sizes[max_bin - static_cast<unsignedchar_type>((*current)[char_offset])]++;
  		}
  		//Assign the bin positions
  		bin_cache[cache_offset] = first;
  		for(unsigned u = 0; u < membin_count - 1; u++)
  			bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = last;
  		//handling empty bins
  		RandomAccessIter * local_bin = &(bin_cache[cache_offset + bin_count]);
  		RandomAccessIter lastFull = *local_bin;
  		//Iterating over each element in the bin of empties
  		for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  			//empties belong in this bin
  			while((*current).size() > char_offset) {
  				target_bin = end_bin - static_cast<unsignedchar_type>((*current)[char_offset]);
  				iter_swap(current, (*target_bin)++);
  			}
  		}
  		*local_bin = nextbinstart;
  		nextbinstart = first;
  		//iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  		unsigned last_bin = max_bin;
  		for(; last_bin && !bin_sizes[last_bin]; --last_bin) { }
  		//This dominates runtime, mostly in the swap and bin lookups
  		for(unsigned u = 0; u < last_bin; ++u) {
  			local_bin = bins + u;
  			nextbinstart += bin_sizes[u];
  			//Iterating over each element in this bin
  			for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  				//Swapping elements in current into place until the correct element has been swapped in
  				for(target_bin = end_bin - static_cast<unsignedchar_type>((*current)[char_offset]);  target_bin != local_bin; 
  					target_bin = end_bin - static_cast<unsignedchar_type>((*current)[char_offset]))
  					iter_swap(current, (*target_bin)++);
  			}
  			*local_bin = nextbinstart;
  		}
  		bins[last_bin] = lastFull;
  		//Recursing
  		RandomAccessIter lastPos = first;
  		//Skip this loop for empties
  		for(unsigned u = cache_offset; u <= cache_offset + last_bin; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			//don't sort unless there are at least two items to compare
  			if(count < 2)
  				continue;
  			//using std::sort if its worst-case is better
  			if(count < max_size)
  				std::sort(lastPos, bin_cache[u], offset_greaterthan<data_type, unsignedchar_type>(char_offset + 1));
  			else
  				reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(lastPos, bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes);
  		}
  	}

  	//String sorting recursive implementation
  	template <class RandomAccessIter, class data_type, class unsignedchar_type, class get_char, class get_length>
  	inline void 
  	string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  		, unsigned cache_offset, std::vector<size_t> &bin_sizes, get_char getchar, get_length length)
  	{
  		//This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  		//Iterate to the end of the empties.  If all empty, return
  		while(length(*first) <= char_offset) {
  			if(++first == last)
  				return;
  		}
  		RandomAccessIter finish = last - 1;
  		//Getting the last non-empty
  		for(;length(*finish) <= char_offset; --finish) { }
  		++finish;
  		update_offset(first, finish, char_offset, getchar, length);
  		
  		const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  		//Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  		const unsigned max_size = bin_count;
  		const unsigned membin_count = bin_count + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count) + 1;
  			
  		//Calculating the size of each bin; this takes roughly 10% of runtime
  		for (RandomAccessIter current = first; current != last; ++current) {
  			if(length(*current) <= char_offset) {
  				bin_sizes[0]++;
  			}
  			else
  				bin_sizes[getchar((*current), char_offset) + 1]++;
  		}
  		//Assign the bin positions
  		bin_cache[cache_offset] = first;
  		for(unsigned u = 0; u < membin_count - 1; u++)
  			bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		//handling empty bins
  		RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
  		nextbinstart +=	bin_sizes[0];
  		RandomAccessIter * target_bin;
  		//Iterating over each element in the bin of empties
  		for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  			//empties belong in this bin
  			while(length(*current) > char_offset) {
  				target_bin = bins + getchar((*current), char_offset);
  				iter_swap(current, (*target_bin)++);
  			}
  		}
  		*local_bin = nextbinstart;
  		//iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  		unsigned last_bin = bin_count - 1;
  		for(; last_bin && !bin_sizes[last_bin + 1]; --last_bin) { }
  		//This dominates runtime, mostly in the swap and bin lookups
  		for(unsigned ii = 0; ii < last_bin; ++ii) {
  			local_bin = bins + ii;
  			nextbinstart += bin_sizes[ii + 1];
  			//Iterating over each element in this bin
  			for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  				//Swapping elements in current into place until the correct element has been swapped in
  				for(target_bin = bins + getchar((*current), char_offset);  target_bin != local_bin; 
  					target_bin = bins + getchar((*current), char_offset))
  					iter_swap(current, (*target_bin)++);
  			}
  			*local_bin = nextbinstart;
  		}
  		bins[last_bin] = last;
  		
  		//Recursing
  		RandomAccessIter lastPos = bin_cache[cache_offset];
  		//Skip this loop for empties
  		for(unsigned u = cache_offset + 1; u < cache_offset + last_bin + 2; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			//don't sort unless there are at least two items to compare
  			if(count < 2)
  				continue;
  			//using std::sort if its worst-case is better
  			if(count < max_size)
  				std::sort(lastPos, bin_cache[u], offset_char_lessthan<data_type, get_char, get_length>(char_offset + 1));
  			else
  				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);
  		}
  	}

  	//String sorting recursive implementation
  	template <class RandomAccessIter, class data_type, class unsignedchar_type, class get_char, class get_length, class compare>
  	inline void 
  	string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  		, unsigned cache_offset, std::vector<size_t> &bin_sizes, get_char getchar, get_length length, compare comp)
  	{
  		//This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  		//Iterate to the end of the empties.  If all empty, return
  		while(length(*first) <= char_offset) {
  			if(++first == last)
  				return;
  		}
  		RandomAccessIter finish = last - 1;
  		//Getting the last non-empty
  		for(;length(*finish) <= char_offset; --finish) { }
  		++finish;
  		update_offset(first, finish, char_offset, getchar, length);
  		
  		const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  		//Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  		const unsigned max_size = bin_count;
  		const unsigned membin_count = bin_count + 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count) + 1;
  			
  		//Calculating the size of each bin; this takes roughly 10% of runtime
  		for (RandomAccessIter current = first; current != last; ++current) {
  			if(length(*current) <= char_offset) {
  				bin_sizes[0]++;
  			}
  			else
  				bin_sizes[getchar((*current), char_offset) + 1]++;
  		}
  		//Assign the bin positions
  		bin_cache[cache_offset] = first;
  		for(unsigned u = 0; u < membin_count - 1; u++)
  			bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = first;
  		//handling empty bins
  		RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
  		nextbinstart +=	bin_sizes[0];
  		RandomAccessIter * target_bin;
  		//Iterating over each element in the bin of empties
  		for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  			//empties belong in this bin
  			while(length(*current) > char_offset) {
  				target_bin = bins + getchar((*current), char_offset);
  				iter_swap(current, (*target_bin)++);
  			}
  		}
  		*local_bin = nextbinstart;
  		//iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  		unsigned last_bin = bin_count - 1;
  		for(; last_bin && !bin_sizes[last_bin + 1]; --last_bin) { }
  		//This dominates runtime, mostly in the swap and bin lookups
  		for(unsigned u = 0; u < last_bin; ++u) {
  			local_bin = bins + u;
  			nextbinstart += bin_sizes[u + 1];
  			//Iterating over each element in this bin
  			for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  				//Swapping elements in current into place until the correct element has been swapped in
  				for(target_bin = bins + getchar((*current), char_offset);  target_bin != local_bin; 
  					target_bin = bins + getchar((*current), char_offset))
  					iter_swap(current, (*target_bin)++);
  			}
  			*local_bin = nextbinstart;
  		}
  		bins[last_bin] = last;
  		
  		//Recursing
  		RandomAccessIter lastPos = bin_cache[cache_offset];
  		//Skip this loop for empties
  		for(unsigned u = cache_offset + 1; u < cache_offset + last_bin + 2; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			//don't sort unless there are at least two items to compare
  			if(count < 2)
  				continue;
  			//using std::sort if its worst-case is better
  			if(count < max_size)
  				std::sort(lastPos, bin_cache[u], comp);
  			else
  				string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(lastPos
  					, bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes, getchar, length, comp);
  		}
  	}

  	//Sorts strings in reverse order, with empties at the end
  	template <class RandomAccessIter, class data_type, class unsignedchar_type, class get_char, class get_length, class compare>
  	inline void 
  	reverse_string_sort_rec(RandomAccessIter first, RandomAccessIter last, unsigned char_offset, std::vector<RandomAccessIter> &bin_cache
  		, unsigned cache_offset, std::vector<size_t> &bin_sizes, get_char getchar, get_length length, compare comp)
  	{
  		//This section is not strictly necessary, but makes handling of long identical substrings much faster, with a mild average performance impact.
  		RandomAccessIter curr = first;
  		//Iterate to the end of the empties.  If all empty, return
  		while(length(*curr) <= char_offset) {
  			if(++curr == last)
  				return;
  		}
  		//Getting the last non-empty
  		while(length(*(--last)) <= char_offset) { }
  		++last;
  		//Offsetting on identical characters.  This section works a character at a time for optimal worst-case performance.
  		update_offset(first, last, char_offset, getchar, length);
  		
  		const unsigned bin_count = (1 << (sizeof(unsignedchar_type)*8));
  		//Equal worst-case between radix and comparison-based is when bin_count = n*log(n).
  		const unsigned max_size = bin_count;
  		const unsigned membin_count = bin_count + 1;
  		const unsigned max_bin = bin_count - 1;
  		unsigned cache_end;
  		RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset, cache_end, membin_count);
  		RandomAccessIter *end_bin = &(bin_cache[cache_offset + max_bin]);
  			
  		//Calculating the size of each bin; this takes roughly 10% of runtime
  		for (RandomAccessIter current = first; current != last; ++current) {
  			if(length(*current) <= char_offset) {
  				bin_sizes[bin_count]++;
  			}
  			else
  				bin_sizes[max_bin - getchar((*current), char_offset)]++;
  		}
  		//Assign the bin positions
  		bin_cache[cache_offset] = first;
  		for(unsigned u = 0; u < membin_count - 1; u++)
  			bin_cache[cache_offset + u + 1] = bin_cache[cache_offset + u] + bin_sizes[u];
  		
  		//Swap into place
  		RandomAccessIter nextbinstart = last;
  		//handling empty bins
  		RandomAccessIter * local_bin = &(bin_cache[cache_offset + bin_count]);
  		RandomAccessIter lastFull = *local_bin;
  		RandomAccessIter * target_bin;
  		//Iterating over each element in the bin of empties
  		for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  			//empties belong in this bin
  			while(length(*current) > char_offset) {
  				target_bin = end_bin - getchar((*current), char_offset);
  				iter_swap(current, (*target_bin)++);
  			}
  		}
  		*local_bin = nextbinstart;
  		nextbinstart = first;
  		//iterate backwards to find the last bin with elements in it; this saves iterations in multiple loops
  		unsigned last_bin = max_bin;
  		for(; last_bin && !bin_sizes[last_bin]; --last_bin) { }
  		//This dominates runtime, mostly in the swap and bin lookups
  		for(unsigned u = 0; u < last_bin; ++u) {
  			local_bin = bins + u;
  			nextbinstart += bin_sizes[u];
  			//Iterating over each element in this bin
  			for(RandomAccessIter current = *local_bin; current < nextbinstart; ++current) {
  				//Swapping elements in current into place until the correct element has been swapped in
  				for(target_bin = end_bin - getchar((*current), char_offset);  target_bin != local_bin; 
  					target_bin = end_bin - getchar((*current), char_offset))
  					iter_swap(current, (*target_bin)++);
  			}
  			*local_bin = nextbinstart;
  		}
  		bins[last_bin] = lastFull;
  		//Recursing
  		RandomAccessIter lastPos = first;
  		//Skip this loop for empties
  		for(unsigned u = cache_offset; u <= cache_offset + last_bin; lastPos = bin_cache[u], ++u) {
  			size_t count = bin_cache[u] - lastPos;
  			//don't sort unless there are at least two items to compare
  			if(count < 2)
  				continue;
  			//using std::sort if its worst-case is better
  			if(count < max_size)
  				std::sort(lastPos, bin_cache[u], comp);
  			else
  				reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(lastPos
  					, bin_cache[u], char_offset + 1, bin_cache, cache_end, bin_sizes, getchar, length, comp);
  		}
  	}

  	//Holds the bin vector and makes the initial recursive call
  	template <class RandomAccessIter, class data_type, class unsignedchar_type>
  	inline void 
  	string_sort(RandomAccessIter first, RandomAccessIter last, data_type, unsignedchar_type)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(first, last, 0, bin_cache, 0, bin_sizes);
  	}

  	//Holds the bin vector and makes the initial recursive call
  	template <class RandomAccessIter, class data_type, class unsignedchar_type>
  	inline void 
  	reverse_string_sort(RandomAccessIter first, RandomAccessIter last, data_type, unsignedchar_type)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		reverse_string_sort_rec<RandomAccessIter, data_type, unsignedchar_type>(first, last, 0, bin_cache, 0, bin_sizes);
  	}

  	//Holds the bin vector and makes the initial recursive call
  	template <class RandomAccessIter, class get_char, class get_length, class data_type, class unsignedchar_type>
  	inline void 
  	string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, data_type, unsignedchar_type)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length>(first, last, 0, bin_cache, 0, bin_sizes, getchar, length);
  	}

  	//Holds the bin vector and makes the initial recursive call
  	template <class RandomAccessIter, class get_char, class get_length, class compare, class data_type, class unsignedchar_type>
  	inline void 
  	string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp, data_type, unsignedchar_type)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		string_sort_rec<RandomAccessIter, data_type, unsignedchar_type, get_char, get_length, compare>(first, last, 0, bin_cache, 0, bin_sizes, getchar, length, comp);
  	}

  	//Holds the bin vector and makes the initial recursive call
  	template <class RandomAccessIter, class get_char, class get_length, class compare, class data_type, class unsignedchar_type>
  	inline void 
  	reverse_string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp, data_type, unsignedchar_type)
  	{
  		std::vector<size_t> bin_sizes;
  		std::vector<RandomAccessIter> bin_cache;
  		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);
  	}
  }

  //Allows character-type overloads
  template <class RandomAccessIter, class unsignedchar_type>
  inline void string_sort(RandomAccessIter first, RandomAccessIter last, unsignedchar_type unused) 
  {
  	//Don't sort if it's too small to optimize
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last);
  	else
  		detail::string_sort(first, last, *first, unused);
  }

  //Top-level sorting call; wraps using default of unsigned char
  template <class RandomAccessIter>
  inline void string_sort(RandomAccessIter first, RandomAccessIter last) 
  {
  	unsigned char unused = '\0';
  	string_sort(first, last, unused);
  }

  //Allows character-type overloads
  template <class RandomAccessIter, class compare, class unsignedchar_type>
  inline void reverse_string_sort(RandomAccessIter first, RandomAccessIter last, compare comp, unsignedchar_type unused) 
  {
  	//Don't sort if it's too small to optimize
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last, comp);
  	else
  		detail::reverse_string_sort(first, last, *first, unused);
  }

  //Top-level sorting call; wraps using default of unsigned char
  template <class RandomAccessIter, class compare>
  inline void reverse_string_sort(RandomAccessIter first, RandomAccessIter last, compare comp) 
  {
  	unsigned char unused = '\0';
  	reverse_string_sort(first, last, comp, unused);
  }

  template <class RandomAccessIter, class get_char, class get_length>
  inline void string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length) 
  {
  	//Don't sort if it's too small to optimize
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last);
  	else {
  		//skipping past empties at the beginning, which allows us to get the character type 
  		//.empty() is not used so as not to require a user declaration of it
  		while(!length(*first)) {
  			if(++first == last)
  				return;
  		}
  		detail::string_sort(first, last, getchar, length, *first, getchar((*first), 0));
  	}
  }

  template <class RandomAccessIter, class get_char, class get_length, class compare>
  inline void string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp) 
  {
  	//Don't sort if it's too small to optimize
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last, comp);
  	else {
  		//skipping past empties at the beginning, which allows us to get the character type 
  		//.empty() is not used so as not to require a user declaration of it
  		while(!length(*first)) {
  			if(++first == last)
  				return;
  		}
  		detail::string_sort(first, last, getchar, length, comp, *first, getchar((*first), 0));
  	}
  }

  template <class RandomAccessIter, class get_char, class get_length, class compare>
  inline void reverse_string_sort(RandomAccessIter first, RandomAccessIter last, get_char getchar, get_length length, compare comp) 
  {
  	//Don't sort if it's too small to optimize
  	if(last - first < detail::MIN_SORT_SIZE)
  		std::sort(first, last, comp);
  	else {
  		//skipping past empties at the beginning, which allows us to get the character type 
  		//.empty() is not used so as not to require a user declaration of it
  		while(!length(*(--last))) {
  			//Note: if there is just one non-empty, and it's at the beginning, then it's already in sorted order
  			if(first == last)
  				return;
  		}
  		//making last just after the end of the non-empty part of the array
  		++last;
  		detail::reverse_string_sort(first, last, getchar, length, comp, *first, getchar((*first), 0));
  	}
  }
}

#endif