#pragma once #include #include #include #include #include #include template auto partition_(Iterator b, Iterator e, Fun && function, size_t output_size) { using namespace std; using T = typename decay::type; list elements(b, e); list> blocks; using ref = typename list>::iterator; vector A(output_size); auto it = begin(elements); auto ed = end(elements); while(it != ed){ const auto current = it++; const auto y = function(*current); if(y >= output_size) throw runtime_error("Output is too big"); auto & ar = A[y]; if(ar == ref{}){ ar = blocks.insert(blocks.end(), list{}); } ar->splice(ar->end(), elements, current); } assert(s == accumulate(begin(blocks), end(blocks), 0, [](size_t l, const list & r) { return l + r.size(); })); return blocks; }