+ template <class OurContainer, class Vector, class InputIterator>
+ void bulk_insert(
+ OurContainer& sorted,
+ Vector& cont,
+ InputIterator first,
+ InputIterator last) {
+ // prevent deref of middle where middle == cont.end()
+ if (first == last) {
+ return;
+ }
+
+ auto const& cmp(sorted.value_comp());
+
+ int const d = distance_if_multipass(first, last);
+ if (d != -1) {
+ cont.reserve(cont.size() + d);
+ }
+ auto const prev_size = cont.size();
+
+ std::copy(first, last, std::back_inserter(cont));
+ auto const middle = cont.begin() + prev_size;
+ if (!std::is_sorted(middle, cont.end(), cmp)) {
+ std::sort(middle, cont.end(), cmp);
+ }
+ if (middle != cont.begin() && cmp(*middle, *(middle - 1))) {
+ std::inplace_merge(cont.begin(), middle, cont.end(), cmp);
+ cont.erase(
+ std::unique(
+ cont.begin(),
+ cont.end(),
+ [&](typename OurContainer::value_type const& a,
+ typename OurContainer::value_type const& b) {
+ return !cmp(a, b) && !cmp(b, a);
+ }),
+ cont.end());
+ }
+ }