#pragma once #include #include #include #include #include #include template std::list> 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); } return blocks; }