void EnsureCapacity(TContainer& container, typename TContainer::size_type index) { // if we need to resize if(container.size() <= index) { // then we shall resize! container.resize(index + 1); } }
inline std::vector<std::shared_ptr<TContainer> > split(TContainer & container, std::size_t const PARTS) { std::vector<std::shared_ptr<TContainer> > split_container; split_container.resize(PARTS); std::size_t const container_original_size = container.size(); for (long i = static_cast<long>(PARTS) - 1; i >= 0; --i) { std::size_t const part_size = container_original_size / PARTS + (container_original_size % PARTS > static_cast<std::size_t>(i)); split_container[i] = std::make_shared<TContainer>(std::make_move_iterator(std::next(container.end(), -part_size)), std::make_move_iterator(container.end()) ); // Make sure the container is smaller now if (container.size() > i * (container_original_size / PARTS + 1)) container.resize(i * (container_original_size / PARTS + 1)); } return split_container; }