template void FinalSort__(I begin, I end, const Less& less) { if(begin == end) return; I last = end; --last; while(!(begin == last)) { I best = last; I next = last; I ptr = last; for(;;) { if(less(*best, *--ptr)) { // best holds, scan for better candidate do if(ptr == begin) { // best is the final minimum IterSwap(begin, best); ++begin; goto NEXT_ITEM; } while(less(*best, *--ptr)); if(ptr == begin) { // begin is the final minimum, best is 2nd least IterSwap(++begin, best); ++begin; break; } next = ptr; // mark position after new best as the new end of sorted array ++next; // it will hold only if all subseqent iterations define new best (descending order) } else if(ptr == begin) { // begin is the final minimum begin = next; // and everything is sorted up to next break; } best = ptr; } NEXT_ITEM: ; } } template void FinalSort__(T& c, const Less& less) { FinalSort__(c.begin(), c.end(), less); } template void FinalSort__(T& c) { typedef ValueTypeOf VT; FinalSort__(c.begin(), c.end(), std::less()); } template force_inline void OrderIter2__(I a, I b, const Less& less) { if(less(*b, *a)) IterSwap(a, b); } dword Random(dword n); template void Sort__(I l, I h, const Less& less) { for(;;) { int count = int(h - l); if(count < 2) return; if(count < 8) { // Final optimized SelectSort FinalSort__(l, h, less); return; } int pass = 4; for(;;) { I middle = l + (count >> 1); // get the middle element OrderIter2__(l, middle, less); // sort l, middle, h-1 to find median of 3 OrderIter2__(middle, h - 1, less); OrderIter2__(l, middle, less); // median is now in middle IterSwap(l + 1, middle); // move median pivot to l + 1 I ii = l + 1; for(I i = l + 2; i != h - 1; ++i) // do partitioning; already l <= pivot <= h - 1 if(less(*i, *(l + 1))) IterSwap(++ii, i); IterSwap(ii, l + 1); // put pivot back in between partitions I iih = ii; while(iih + 1 != h && !less(*ii, *(iih + 1))) // Find middle range of elements equal to pivot ++iih; if(pass > 5 || min(ii - l, h - iih) > (max(ii - l, h - iih) >> pass)) { // partition sizes ok or we have done max attempts if(ii - l < h - iih - 1) { // recurse on smaller partition, tail on larger Sort__(l, ii, less); l = iih + 1; } else { Sort__(iih + 1, h, less); h = ii; } break; } IterSwap(l, l + (int)Random(count)); // try some other random elements for median pivot IterSwap(middle, l + (int)Random(count)); IterSwap(h - 1, l + (int)Random(count)); pass++; } } } template void Sort(Range& c, const Less& less) { Sort__(c.begin(), c.end(), less); } template void Sort(Range&& c, const Less& less) { Sort(c, less); } template void Sort(Range& c) { Sort__(c.begin(), c.end(), std::less>()); } template void Sort(Range&& c) { Sort(c); } template struct StableSortItem__ { const T& value; int index; StableSortItem__(const T& value, int index) : value(value), index(index) {} }; template struct StableSortIterator__ { II ii; int *vi; typedef StableSortIterator__ Iter; Iter& operator ++ () { ++ii; ++vi; return *this; } Iter& operator -- () { --ii; --vi; return *this; } Iter operator + (int i) const { return Iter(ii + i, vi + i); } Iter operator - (int i) const { return Iter(ii - i, vi - i); } int operator - (Iter b) const { return (int)(ii - b.ii); } bool operator == (Iter b) const { return ii == b.ii; } bool operator != (Iter b) const { return ii != b.ii; } bool operator < (Iter b) const { return ii < b.ii; } StableSortItem__ operator*() const { return StableSortItem__(*ii, *vi); } friend void IterSwap(Iter a, Iter b) { IterSwap(a.ii, b.ii); IterSwap(a.vi, b.vi); } StableSortIterator__(II ii, int *vi) : ii(ii), vi(vi) {} }; template struct StableSortLess__ { const Less& less; bool operator()(const StableSortItem__& a, const StableSortItem__& b) const { if(less(a.value, b.value)) return true; return less(b.value, a.value) ? false : a.index < b.index; } StableSortLess__(const Less& less) : less(less) {} }; template void StableSort(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; Sort__(StableSortIterator__(begin, ~h), StableSortIterator__(end, ~h + count), StableSortLess__(less)); } template void StableSort(Range&& r, const Less& less) { StableSort(r, less); } template void StableSort(Range& r) { StableSort(r, std::less>()); } template void StableSort(Range&& r) { StableSort(r); } template struct IndexSortIterator__ { typedef IndexSortIterator__ Iter; IndexSortIterator__(II ii, VI vi) : ii(ii), vi(vi) {} Iter& operator ++ () { ++ii; ++vi; return *this; } Iter& operator -- () { --ii; --vi; return *this; } const K& operator * () const { return *ii; } Iter operator + (int i) const { return Iter(ii + i, vi + i); } Iter operator - (int i) const { return Iter(ii - i, vi - i); } int operator - (Iter b) const { return (int)(ii - b.ii); } bool operator == (Iter b) const { return ii == b.ii; } bool operator != (Iter b) const { return ii != b.ii; } bool operator < (Iter b) const { return ii < b.ii; } friend void IterSwap (Iter a, Iter b) { IterSwap(a.ii, b.ii); IterSwap(a.vi, b.vi); } II ii; VI vi; }; template void IndexSort(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; Sort__(IndexSortIterator__(r.begin(), r2.begin()), IndexSortIterator__(r.end(), r2.end()), less); } template void IndexSort(MasterRange&& r, Range2&& r2, const Less& less) { IndexSort(r, r2, less); } template void IndexSort(MasterRange& r, Range2& r2) { IndexSort(r, r2, std::less>()); } template void IndexSort(MasterRange&& r, Range2&& r2) { IndexSort(r, r2); } template void StableIndexSort(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; StableSort(SubRange(IndexSortIterator__(r.begin(), r2.begin()), IndexSortIterator__(r.end(), r2.end())).Write(), less); } template void StableIndexSort(MasterRange&& r, Range2&& r2, const Less& less) { StableIndexSort(r, r2, less); } template void StableIndexSort(MasterRange& r, Range2& r2) { StableIndexSort(r, r2, std::less>()); } template void StableIndexSort(MasterRange&& r, Range2&& r2) { StableIndexSort(r, r2); } template struct IndexSort2Iterator__ { typedef IndexSort2Iterator__ Iter; IndexSort2Iterator__(II ii, VI vi, WI wi) : ii(ii), vi(vi), wi(wi) {} Iter& operator ++ () { ++ii; ++vi; ++wi; return *this; } Iter& operator -- () { --ii; --vi; --wi; return *this; } const K& operator * () const { return *ii; } Iter operator + (int i) const { return Iter(ii + i, vi + i, wi + i); } Iter operator - (int i) const { return Iter(ii - i, vi - i, wi - i); } int operator - (Iter b) const { return (int)(ii - b.ii); } bool operator == (Iter b) const { return ii == b.ii; } bool operator != (Iter b) const { return ii != b.ii; } bool operator < (Iter b) const { return ii < b.ii; } friend void IterSwap (Iter a, Iter b) { IterSwap(a.ii, b.ii); IterSwap(a.vi, b.vi); IterSwap(a.wi, b.wi); } II ii; VI vi; WI wi; }; template void IndexSort2(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; Sort__(IndexSort2Iterator__(r.begin(), r2.begin(), r3.begin()), IndexSort2Iterator__(r.end(), r2.end(), r3.end()), less); } template void IndexSort2(MasterRange&& r, Range2&& r2, Range3&& r3, const Less& less) { IndexSort2(r, r2, r3, less); } template void IndexSort2(MasterRange& r, Range2& r2, Range3& r3) { IndexSort2(r, r2, r3, std::less>()); } template void IndexSort2(MasterRange&& r, Range2&& r2, Range3&& r3) { IndexSort2(r, r2, r3); } template void StableIndexSort2(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; StableSort(SubRange(IndexSort2Iterator__(r.begin(), r2.begin(), r3.begin()), IndexSort2Iterator__(r.end(), r2.end(), r3.end())).Write(), less); } template void StableIndexSort2(MasterRange&& r, Range2&& r2, Range3&& r3, const Less& less) { StableIndexSort2(r, r2, r3, less); } template void StableIndexSort2(MasterRange& r, Range2& r2, Range3& r3) { StableIndexSort2(r, r2, r3, std::less>()); } template void StableIndexSort2(MasterRange&& r, Range2&& r2, Range3&& r3) { StableIndexSort2(r, r2, r3); } template struct IndexSort3Iterator__ { typedef IndexSort3Iterator__ Iter; IndexSort3Iterator__(II ii, VI vi, WI wi, XI xi) : ii(ii), vi(vi), wi(wi), xi(xi) {} Iter& operator ++ () { ++ii; ++vi; ++wi; ++xi; return *this; } Iter& operator -- () { --ii; --vi; --wi; --xi; return *this; } const K& operator * () const { return *ii; } Iter operator + (int i) const { return Iter(ii + i, vi + i, wi + i, xi + i); } Iter operator - (int i) const { return Iter(ii - i, vi - i, wi - i, xi - i); } int operator - (Iter b) const { return (int)(ii - b.ii); } bool operator == (Iter b) const { return ii == b.ii; } bool operator != (Iter b) const { return ii != b.ii; } bool operator < (Iter b) const { return ii < b.ii; } friend void IterSwap (Iter a, Iter b) { IterSwap(a.ii, b.ii); IterSwap(a.vi, b.vi); IterSwap(a.wi, b.wi); IterSwap(a.xi, b.xi); } II ii; VI vi; WI wi; XI xi; }; template void IndexSort3(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; Sort__(IndexSort3Iterator__(r.begin(), r2.begin(), r3.begin(), r4.begin()), IndexSort3Iterator__(r.end(), r2.end(), r3.end(), r4.end()), less); } template void IndexSort3(MasterRange&& r, Range2&& r2, Range3&& r3, Range4&& r4, const Less& less) { IndexSort3(r, r2, r3, r4, less); } template void IndexSort3(MasterRange& r, Range2& r2, Range3& r3, Range4& r4) { IndexSort3(r, r2, r3, r4, std::less>()); } template void IndexSort3(MasterRange&& r, Range2&& r2, Range3&& r3, Range4&& r4) { IndexSort3(r, r2, r3, r4); } template void StableIndexSort3(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; StableSort(SubRange(IndexSort3Iterator__(r.begin(), r2.begin(), r3.begin(), r4.begin()), IndexSort3Iterator__(r.end(), r2.end(), r3.end(), r4.end())).Write(), less); } template void StableIndexSort3(MasterRange&& r, Range2&& r2, Range3&& r3, Range4&& r4, const Less& less) { StableIndexSort3(r, r2, r3, r4, less); } template void StableIndexSort3(MasterRange& r, Range2& r2, Range3& r3, Range4& r4) { StableIndexSort3(r, r2, r3, r4, std::less>()); } template void StableIndexSort3(MasterRange&& r, Range2&& r2, Range3&& r3, Range4&& r4) { StableIndexSort3(r, r2, r3, r4); } template struct SortOrderIterator__ : PostfixOps< SortOrderIterator__ > { typedef SortOrderIterator__ Iter; SortOrderIterator__(int *ii, I vi) : ii(ii), vi(vi) {} Iter& operator ++ () { ++ii; return *this; } Iter& operator -- () { --ii; return *this; } const V& operator * () const { return *(vi + *ii); } Iter operator + (int i) const { return Iter(ii + i, vi); } Iter operator - (int i) const { return Iter(ii - i, vi); } int operator - (Iter b) const { return int(ii - b.ii); } bool operator == (Iter b) const { return ii == b.ii; } bool operator != (Iter b) const { return ii != b.ii; } bool operator < (Iter b) const { return ii < b.ii; } friend void IterSwap (Iter a, Iter b) { IterSwap(a.ii, b.ii); } int *ii; I vi; }; template Vector GetSortOrder(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; Sort__(It(index.begin(), begin), It(index.end(), begin), less); return index; } template inline Vector GetSortOrder(const Range& r) { return GetSortOrder(r, std::less>()); } template struct StableSortOrderIterator__ : PostfixOps< StableSortOrderIterator__ > { typedef StableSortOrderIterator__ Iter; StableSortOrderIterator__(int *ii, I vi) : ii(ii), vi(vi) {} Iter& operator ++ () { ++ii; return *this; } Iter& operator -- () { --ii; return *this; } Iter operator + (int i) const { return Iter(ii + i, vi); } Iter operator - (int i) const { return Iter(ii - i, vi); } int operator - (Iter b) const { return int(ii - b.ii); } bool operator == (Iter b) const { return ii == b.ii; } bool operator != (Iter b) const { return ii != b.ii; } bool operator < (Iter b) const { return ii < b.ii; } friend void IterSwap (Iter a, Iter b) { IterSwap(a.ii, b.ii); } StableSortItem__ operator*() const { return StableSortItem__(*(vi + *ii), *ii); } int *ii; I vi; }; template Vector GetStableSortOrder(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; Sort__(It(index.begin(), begin), It(index.end(), begin), StableSortLess__(less)); return index; } template Vector GetStableSortOrder(const Range& r) { return GetStableSortOrder(r, std::less>()); } template void SortByKey(Map& map, const Less& less) { typename Map::KeyContainer k = map.PickKeys(); typename Map::ValueContainer v = map.PickValues(); IndexSort(k, v, less); map = Map(pick(k), pick(v)); } template void SortByKey(Map& map) { SortByKey(map, std::less()); } template void SortByValue(Map& map, const Less& less) { typename Map::KeyContainer k = map.PickKeys(); typename Map::ValueContainer v = map.PickValues(); IndexSort(v, k, less); map = Map(pick(k), pick(v)); } template void SortByValue(Map& map) { SortByValue(map, std::less>()); } template void StableSortByKey(Map& map, const Less& less) { typename Map::KeyContainer k = map.PickKeys(); typename Map::ValueContainer v = map.PickValues(); StableIndexSort(k, v, less); map = Map(pick(k), pick(v)); } template void StableSortByKey(Map& map) { StableSortByKey(map, std::less()); } template void StableSortByValue(Map& map, const Less& less) { typename Map::KeyContainer k = map.PickKeys(); typename Map::ValueContainer v = map.PickValues(); StableIndexSort(v, k, less); map = Map(pick(k), pick(v)); } template void StableSortByValue(Map& map) { StableSortByValue(map, std::less>()); } template void SortIndex(Index& index, const Less& less) { typename Index::ValueContainer k = index.PickKeys(); Sort(k, less); index = Index(pick(k)); } template void SortIndex(Index& index) { SortIndex(index, std::less>()); } template void StableSortIndex(Index& index, const Less& less) { typename Index::ValueContainer k = index.PickKeys(); StableSort(k, less); index = Index(pick(k)); } template void StableSortIndex(Index& index) { StableSortIndex(index, std::less>()); }