template void CoSort__(CoWork& cw, I l, I h, const Less& less) { int count = int(h - l); I middle = l + (count >> 1); // get the middle element for(;;) { if(count < 200) { // too little elements to gain anything with parellel processing Sort__(l, h, less); return; } if(count > 1000) { middle = l + (count >> 1); // iterators cannot point to the same object! I q = l + 1 + (int)Random((count >> 1) - 2); I w = middle + 1 + (int)Random((count >> 1) - 2); OrderIter5__(l, q, middle, w, h - 1, less); } else OrderIter3__(l, middle, h - 1, less); I pivot = h - 2; IterSwap(pivot, middle); // move median pivot to h - 2 I i = l; I j = h - 2; // l, h - 2, h - 1 already sorted above for(;;) { // Hoare’s partition (modified): while(less(*++i, *pivot)); do if(!(i < j)) goto done; while(!less(*--j, *pivot)); IterSwap(i, j); } done: IterSwap(i, h - 2); // put pivot back in between partitions I ih = i; while(ih + 1 != h && !less(*i, *(ih + 1))) // Find middle range of elements equal to pivot ++ih; int count_l = i - l; if(count_l == 1) // this happens if there are many elements equal to pivot, filter them out for(I q = ih + 1; q != h; ++q) if(!less(*i, *q)) IterSwap(++ih, q); int count_h = h - ih - 1; if(count_l < count_h) { // recurse on smaller partition, tail on larger cw & [=, &cw] { CoSort__(cw, l, i, less); }; l = ih + 1; count = count_h; } else { cw & [=, &cw] { CoSort__(cw, ih + 1, h, less); }; h = i; count = count_l; } if(count > 8 && min(count_l, count_h) < (max(count_l, count_h) >> 2)) // If unbalanced, middle = l + 1 + Random(count - 2); // randomize the next step else middle = l + (count >> 1); // the middle is probably still the best guess otherwise } } template void CoSort__(I l, I h, const Less& less) { CoWork cw; CoSort__(cw, l, h, less); } template void CoSort(Range&& c, const Less& less) { CoSort__(c.begin(), c.end(), less); } template void CoSort(Range&& c) { CoSort__(c.begin(), c.end(), std::less>()); } template void CoStableSort(Range&& r, const Less& less) { auto begin = r.begin(); auto end = r.end(); typedef ValueTypeOf VT; typedef decltype(begin) I; int count = (int)(uintptr_t)(end - begin); Buffer h(count); for(int i = 0; i < count; i++) h[i] = i; CoSort__(StableSortIterator__(begin, ~h), StableSortIterator__(end, ~h + count), StableSortLess__(less)); } template void CoStableSort(Range&& r) { CoStableSort(r, std::less>()); } template void CoIndexSort(MasterRange&& r, Range2&& r2, const Less& less) { ASSERT(r.GetCount() == r2.GetCount()); typedef decltype(r.begin()) I; typedef decltype(r2.begin()) I2; typedef ValueTypeOf VT; if(r.GetCount() == 0) return; CoSort__(IndexSortIterator__(r.begin(), r2.begin()), IndexSortIterator__(r.end(), r2.end()), less); } template void CoIndexSort(MasterRange&& r, Range2&& r2) { CoIndexSort(r, r2, std::less>()); } template void CoStableIndexSort(MasterRange&& r, Range2&& r2, const Less& less) { ASSERT(r.GetCount() == r2.GetCount()); typedef decltype(r.begin()) I; typedef decltype(r2.begin()) I2; typedef ValueTypeOf VT; if(r.GetCount() == 0) return; CoStableSort(SubRange(IndexSortIterator__(r.begin(), r2.begin()), IndexSortIterator__(r.end(), r2.end())).Write(), less); } template void CoStableIndexSort(MasterRange&& r, Range2&& r2) { CoStableIndexSort(r, r2, std::less>()); } template void CoIndexSort2(MasterRange&& r, Range2&& r2, Range3&& r3, const Less& less) { ASSERT(r.GetCount() == r2.GetCount()); ASSERT(r.GetCount() == r3.GetCount()); if(r.GetCount() == 0) return; typedef decltype(r.begin()) I; typedef decltype(r2.begin()) I2; typedef decltype(r3.begin()) I3; typedef ValueTypeOf VT; CoSort__(IndexSort2Iterator__(r.begin(), r2.begin(), r3.begin()), IndexSort2Iterator__(r.end(), r2.end(), r3.end()), less); } template void CoIndexSort2(MasterRange&& r, Range2&& r2, Range3&& r3) { CoIndexSort2(r, r2, r3, std::less>()); } template void CoStableIndexSort2(MasterRange&& r, Range2&& r2, Range3&& r3, const Less& less) { ASSERT(r.GetCount() == r2.GetCount()); ASSERT(r.GetCount() == r3.GetCount()); if(r.GetCount() == 0) return; typedef decltype(r.begin()) I; typedef decltype(r2.begin()) I2; typedef decltype(r3.begin()) I3; typedef ValueTypeOf VT; CoStableSort(SubRange(IndexSort2Iterator__(r.begin(), r2.begin(), r3.begin()), IndexSort2Iterator__(r.end(), r2.end(), r3.end())).Write(), less); } template void CoStableIndexSort2(MasterRange&& r, Range2&& r2, Range3&& r3) { CoStableIndexSort2(r, r2, r3, std::less>()); } template void CoIndexSort3(MasterRange&& r, Range2&& r2, Range3&& r3, Range4&& r4, const Less& less) { ASSERT(r.GetCount() == r2.GetCount()); ASSERT(r.GetCount() == r3.GetCount()); ASSERT(r.GetCount() == r4.GetCount()); if(r.GetCount() == 0) return; typedef decltype(r.begin()) I; typedef decltype(r2.begin()) I2; typedef decltype(r3.begin()) I3; typedef decltype(r4.begin()) I4; typedef ValueTypeOf VT; CoSort__(IndexSort3Iterator__(r.begin(), r2.begin(), r3.begin(), r4.begin()), IndexSort3Iterator__(r.end(), r2.end(), r3.end(), r4.end()), less); } template void CoIndexSort3(MasterRange&& r, Range2&& r2, Range3&& r3, Range4&& r4) { CoIndexSort3(r, r2, r3, r4, std::less>()); } template void CoStableIndexSort3(MasterRange&& r, Range2&& r2, Range3&& r3, Range4&& r4, const Less& less) { ASSERT(r.GetCount() == r2.GetCount()); ASSERT(r.GetCount() == r3.GetCount()); ASSERT(r.GetCount() == r4.GetCount()); if(r.GetCount() == 0) return; typedef decltype(r.begin()) I; typedef decltype(r2.begin()) I2; typedef decltype(r3.begin()) I3; typedef decltype(r4.begin()) I4; typedef ValueTypeOf VT; CoStableSort(SubRange(IndexSort3Iterator__(r.begin(), r2.begin(), r3.begin(), r4.begin()), IndexSort3Iterator__(r.end(), r2.end(), r3.end(), r4.end())).Write(), less); } template void CoStableIndexSort3(MasterRange&& r, Range2&& r2, Range3&& r3, Range4&& r4) { CoStableIndexSort3(r, r2, r3, r4, std::less>()); } template Vector CoGetSortOrder(const Range& r, const Less& less) { auto begin = r.begin(); Vector index; index.SetCount(r.GetCount()); for(int i = index.GetCount(); --i >= 0; index[i] = i) ; typedef SortOrderIterator__> It; CoSort__(It(index.begin(), begin), It(index.end(), begin), less); return index; } template Vector CoGetSortOrder(const Range& r) { return CoGetSortOrder(r, std::less>()); } template Vector CoGetStableSortOrder(const Range& r, const Less& less) { Vector index; index.SetCount(r.GetCount()); for(int i = index.GetCount(); --i >= 0; index[i] = i) ; auto begin = r.begin(); typedef ValueTypeOf VT; typedef StableSortOrderIterator__ It; CoSort__(It(index.begin(), begin), It(index.end(), begin), StableSortLess__(less)); return index; } template Vector CoGetStableSortOrder(const Range& r) { return CoGetSortOrder(r, std::less>()); } template void CoSortByKey(Map& map, const Less& less) { typename Map::KeyContainer k = map.PickKeys(); typename Map::ValueContainer v = map.PickValues(); CoIndexSort(k, v, less); map = Map(pick(k), pick(v)); } template void CoSortByKey(Map& map) { CoSortByKey(map, std::less()); } template void CoSortByValue(Map& map, const Less& less) { typename Map::KeyContainer k = map.PickKeys(); typename Map::ValueContainer v = map.PickValues(); CoIndexSort(v, k, less); map = Map(pick(k), pick(v)); } template void CoSortByValue(Map& map) { CoSortByValue(map, std::less>()); } template void CoStableSortByKey(Map& map, const Less& less) { typename Map::KeyContainer k = map.PickKeys(); typename Map::ValueContainer v = map.PickValues(); CoStableIndexSort(k, v, less); map = Map(pick(k), pick(v)); } template void CoStableSortByKey(Map& map) { CoStableSortByKey(map, std::less()); } template void CoStableSortByValue(Map& map, const Less& less) { typename Map::KeyContainer k = map.PickKeys(); typename Map::ValueContainer v = map.PickValues(); CoStableIndexSort(v, k, less); map = Map(pick(k), pick(v)); } template void CoStableSortByValue(Map& map) { CoStableSortByValue(map, std::less>()); } template void CoSortIndex(Index& index, const Less& less) { typename Index::ValueContainer k = index.PickKeys(); CoSort(k, less); index = Index(pick(k)); } template void CoSortIndex(Index& index) { CoSortIndex(index, std::less>()); } template void CoStableSortIndex(Index& index, const Less& less) { typename Index::ValueContainer k = index.PickKeys(); CoStableSort(k, less); index = Index(pick(k)); } template void CoStableSortIndex(Index& index) { CoStableSortIndex(index, std::less>()); }