static inline void apply(Elements const& elements, size_t & choosen_index, margin_type & sum_of_margins, content_type & smallest_overlap, content_type & smallest_content, Parameters const& parameters, Translator const& translator) { typedef typename Elements::value_type element_type; typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type; typedef typename tag<indexable_type>::type indexable_tag; BOOST_GEOMETRY_INDEX_ASSERT(elements.size() == parameters.get_max_elements() + 1, "wrong number of elements"); // copy elements Elements elements_copy(elements); // MAY THROW, STRONG (alloc, copy) // sort elements element_axis_corner_less<element_type, Translator, indexable_tag, Corner, AxisIndex> elements_less(translator); std::sort(elements_copy.begin(), elements_copy.end(), elements_less); // MAY THROW, BASIC (copy) // init outputs choosen_index = parameters.get_min_elements(); sum_of_margins = 0; smallest_overlap = (std::numeric_limits<content_type>::max)(); smallest_content = (std::numeric_limits<content_type>::max)(); // calculate sum of margins for all distributions size_t index_last = parameters.get_max_elements() - parameters.get_min_elements() + 2; for ( size_t i = parameters.get_min_elements() ; i < index_last ; ++i ) { // TODO - awulkiew: may be optimized - box of group 1 may be initialized with // box of min_elems number of elements and expanded for each iteration by another element Box box1 = rtree::elements_box<Box>(elements_copy.begin(), elements_copy.begin() + i, translator); Box box2 = rtree::elements_box<Box>(elements_copy.begin() + i, elements_copy.end(), translator); sum_of_margins += index::detail::comparable_margin(box1) + index::detail::comparable_margin(box2); content_type ovl = index::detail::intersection_content(box1, box2); content_type con = index::detail::content(box1) + index::detail::content(box2); // TODO - shouldn't here be < instead of <= ? if ( ovl < smallest_overlap || (ovl == smallest_overlap && con <= smallest_content) ) { choosen_index = i; smallest_overlap = ovl; smallest_content = con; } } ::boost::ignore_unused_variable_warning(parameters); }
static inline void apply(Node & n, Node & second_node, Box & box1, Box & box2, parameters_type const& parameters, Translator const& translator, Allocators & allocators) { typedef typename rtree::elements_type<Node>::type elements_type; typedef typename elements_type::value_type element_type; typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type; elements_type & elements1 = rtree::elements(n); elements_type & elements2 = rtree::elements(second_node); BOOST_GEOMETRY_INDEX_ASSERT(elements1.size() == parameters.get_max_elements() + 1, "unexpected elements number"); // copy original elements - use in-memory storage (std::allocator) // TODO: move if noexcept typedef typename rtree::container_from_elements_type<elements_type, element_type>::type container_type; container_type elements_copy(elements1.begin(), elements1.end()); // MAY THROW, STRONG (alloc, copy) container_type elements_backup(elements1.begin(), elements1.end()); // MAY THROW, STRONG (alloc, copy) // calculate initial seeds size_t seed1 = 0; size_t seed2 = 0; quadratic::pick_seeds<Box>(elements_copy, parameters, translator, seed1, seed2); // prepare nodes' elements containers elements1.clear(); BOOST_GEOMETRY_INDEX_ASSERT(elements2.empty(), "second node's elements container should be empty"); BOOST_TRY { // add seeds elements1.push_back(elements_copy[seed1]); // MAY THROW, STRONG (copy) elements2.push_back(elements_copy[seed2]); // MAY THROW, STRONG (alloc, copy) // calculate boxes detail::bounds(rtree::element_indexable(elements_copy[seed1], translator), box1); detail::bounds(rtree::element_indexable(elements_copy[seed2], translator), box2); // remove seeds if (seed1 < seed2) { rtree::move_from_back(elements_copy, elements_copy.begin() + seed2); // MAY THROW, STRONG (copy) elements_copy.pop_back(); rtree::move_from_back(elements_copy, elements_copy.begin() + seed1); // MAY THROW, STRONG (copy) elements_copy.pop_back(); } else { rtree::move_from_back(elements_copy, elements_copy.begin() + seed1); // MAY THROW, STRONG (copy) elements_copy.pop_back(); rtree::move_from_back(elements_copy, elements_copy.begin() + seed2); // MAY THROW, STRONG (copy) elements_copy.pop_back(); } // initialize areas content_type content1 = index::detail::content(box1); content_type content2 = index::detail::content(box2); size_t remaining = elements_copy.size(); // redistribute the rest of the elements while ( !elements_copy.empty() ) { typename container_type::reverse_iterator el_it = elements_copy.rbegin(); bool insert_into_group1 = false; size_t elements1_count = elements1.size(); size_t elements2_count = elements2.size(); // if there is small number of elements left and the number of elements in node is lesser than min_elems // just insert them to this node if ( elements1_count + remaining <= parameters.get_min_elements() ) { insert_into_group1 = true; } else if ( elements2_count + remaining <= parameters.get_min_elements() ) { insert_into_group1 = false; } // insert the best element else { // find element with minimum groups areas increses differences content_type content_increase1 = 0; content_type content_increase2 = 0; el_it = pick_next(elements_copy.rbegin(), elements_copy.rend(), box1, box2, content1, content2, translator, content_increase1, content_increase2); if ( content_increase1 < content_increase2 || ( content_increase1 == content_increase2 && ( content1 < content2 || ( content1 == content2 && elements1_count <= elements2_count ) ) ) ) { insert_into_group1 = true; } else { insert_into_group1 = false; } } // move element to the choosen group element_type const& elem = *el_it; indexable_type const& indexable = rtree::element_indexable(elem, translator); if ( insert_into_group1 ) { elements1.push_back(elem); // MAY THROW, STRONG (copy) geometry::expand(box1, indexable); content1 = index::detail::content(box1); } else { elements2.push_back(elem); // MAY THROW, STRONG (alloc, copy) geometry::expand(box2, indexable); content2 = index::detail::content(box2); } BOOST_GEOMETRY_INDEX_ASSERT(!elements_copy.empty(), "expected more elements"); typename container_type::iterator el_it_base = el_it.base(); rtree::move_from_back(elements_copy, --el_it_base); // MAY THROW, STRONG (copy) elements_copy.pop_back(); BOOST_GEOMETRY_INDEX_ASSERT(0 < remaining, "expected more remaining elements"); --remaining; } } BOOST_CATCH(...) { //elements_copy.clear(); elements1.clear(); elements2.clear(); rtree::destroy_elements<Value, Options, Translator, Box, Allocators>::apply(elements_backup, allocators); //elements_backup.clear(); BOOST_RETHROW // RETHROW, BASIC }