diff --git a/TinySTL/List.h b/TinySTL/List.h index a11f8d6..e5d8b6c 100644 --- a/TinySTL/List.h +++ b/TinySTL/List.h @@ -163,9 +163,9 @@ namespace TinySTL{ void merge(list& x); template void merge(list& x, Compare comp); - //void sort(); - //template - //void sort(Compare comp); + void sort(); + template + void sort(Compare comp); void reverse(); private: void ctorAux(size_type n, const value_type& val, std::true_type){ @@ -451,6 +451,35 @@ namespace TinySTL{ bool operator!= (const list& lhs, const list& rhs){ return !(lhs == rhs); } + template + void list::sort(){ + sort(TinySTL::less()); + } + template + template + void list::sort(Compare comp){ + if (empty() || head.p->next == tail.p) + return; + + list carry; + list counter[64]; + int fill = 0; + while (!empty()){ + carry.splice(carry.begin(), *this, begin()); + int i = 0; + while (i < fill && !counter[i].empty()){ + counter[i].merge(carry, comp); + carry.swap(counter[i++]); + } + carry.swap(counter[i]); + if (i == fill) + ++fill; + } + for (int i = 0; i != fill; ++i){ + counter[i].merge(counter[i - 1], comp); + } + swap(counter[fill - 1]); + } } #endif \ No newline at end of file