[index] Fix get_strategy return value and take care about references to temporaries.

This commit is contained in:
Adam Wulkiewicz
2019-07-01 14:46:55 +02:00
parent c3e8642f52
commit bb7cf70069
4 changed files with 42 additions and 31 deletions

View File

@@ -244,7 +244,7 @@ private:
private:
BoxType m_box;
Strategy const& m_strategy;
Strategy m_strategy;
bool m_initialized;
};

View File

@@ -45,16 +45,18 @@ inline void pick_seeds(Elements const& elements,
typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
typedef Box box_type;
typedef typename index::detail::default_content_result<box_type>::type content_type;
typedef typename index::detail::strategy_type<Parameters>::type strategy_type;
typedef index::detail::bounded_view
<
indexable_type, box_type,
typename strategy_type<Parameters>::type
indexable_type, box_type, strategy_type
> bounded_indexable_view;
const size_t elements_count = parameters.get_max_elements() + 1;
BOOST_GEOMETRY_INDEX_ASSERT(elements.size() == elements_count, "wrong number of elements");
BOOST_GEOMETRY_INDEX_ASSERT(2 <= elements_count, "unexpected number of elements");
strategy_type const& strategy = index::detail::get_strategy(parameters);
content_type greatest_free_content = 0;
seed1 = 0;
seed2 = 1;
@@ -67,11 +69,11 @@ inline void pick_seeds(Elements const& elements,
indexable_type const& ind2 = rtree::element_indexable(elements[j], tr);
box_type enlarged_box;
index::detail::bounds(ind1, enlarged_box, get_strategy(parameters));
index::detail::expand(enlarged_box, ind2, get_strategy(parameters));
index::detail::bounds(ind1, enlarged_box, strategy);
index::detail::expand(enlarged_box, ind2, strategy);
bounded_indexable_view bounded_ind1(ind1, get_strategy(parameters));
bounded_indexable_view bounded_ind2(ind2, get_strategy(parameters));
bounded_indexable_view bounded_ind1(ind1, strategy);
bounded_indexable_view bounded_ind2(ind2, strategy);
content_type free_content = ( index::detail::content(enlarged_box)
- index::detail::content(bounded_ind1) )
- index::detail::content(bounded_ind2);
@@ -137,17 +139,18 @@ struct redistribute_elements<Value, Options, Translator, Box, Allocators, quadra
BOOST_TRY
{
typename index::detail::strategy_type<parameters_type>::type const&
strategy = index::detail::get_strategy(parameters);
// 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,
index::detail::get_strategy(parameters));
box1, strategy);
detail::bounds(rtree::element_indexable(elements_copy[seed2], translator),
box2,
index::detail::get_strategy(parameters));
box2, strategy);
// remove seeds
if (seed1 < seed2)
@@ -198,7 +201,7 @@ struct redistribute_elements<Value, Options, Translator, Box, Allocators, quadra
content_type content_increase2 = 0;
el_it = pick_next(elements_copy.rbegin(), elements_copy.rend(),
box1, box2, content1, content2,
translator, index::detail::get_strategy(parameters),
translator, strategy,
content_increase1, content_increase2);
if ( content_increase1 < content_increase2 ||
@@ -221,15 +224,13 @@ struct redistribute_elements<Value, Options, Translator, Box, Allocators, quadra
if ( insert_into_group1 )
{
elements1.push_back(elem); // MAY THROW, STRONG (copy)
index::detail::expand(box1, indexable,
index::detail::get_strategy(parameters));
index::detail::expand(box1, indexable, strategy);
content1 = index::detail::content(box1);
}
else
{
elements2.push_back(elem); // MAY THROW, STRONG (alloc, copy)
index::detail::expand(box2, indexable,
index::detail::get_strategy(parameters));
index::detail::expand(box2, indexable, strategy);
content2 = index::detail::content(box2);
}

View File

@@ -47,8 +47,8 @@ class element_axis_corner_less
> bounded_view_type;
public:
element_axis_corner_less(Parameters const& parameters, Translator const& tr)
: m_strategy(index::detail::get_strategy(parameters)), m_tr(tr)
element_axis_corner_less(Translator const& tr, strategy_type const& strategy)
: m_tr(tr), m_strategy(strategy)
{}
bool operator()(Element const& e1, Element const& e2) const
@@ -61,15 +61,17 @@ public:
}
private:
strategy_type const& m_strategy;
Translator const& m_tr;
strategy_type const& m_strategy;
};
template <typename Element, typename Parameters, typename Translator, size_t Corner, size_t AxisIndex>
class element_axis_corner_less<Element, Parameters, Translator, box_tag, Corner, AxisIndex>
{
typedef typename index::detail::strategy_type<Parameters>::type strategy_type;
public:
element_axis_corner_less(Parameters const& parameters, Translator const& tr)
element_axis_corner_less(Translator const& tr, strategy_type const&)
: m_tr(tr)
{}
@@ -86,8 +88,10 @@ private:
template <typename Element, typename Parameters, typename Translator, size_t Corner, size_t AxisIndex>
class element_axis_corner_less<Element, Parameters, Translator, point_tag, Corner, AxisIndex>
{
typedef typename index::detail::strategy_type<Parameters>::type strategy_type;
public:
element_axis_corner_less(Parameters const& parameters, Translator const& tr)
element_axis_corner_less(Translator const& tr, strategy_type const& )
: m_tr(tr)
{}
@@ -122,6 +126,9 @@ struct choose_split_axis_and_index_for_corner
BOOST_GEOMETRY_INDEX_ASSERT(elements.size() == parameters.get_max_elements() + 1, "wrong number of elements");
typename index::detail::strategy_type<Parameters>::type const&
strategy = index::detail::get_strategy(parameters);
// copy elements
Elements elements_copy(elements); // MAY THROW, STRONG (alloc, copy)
@@ -132,7 +139,7 @@ struct choose_split_axis_and_index_for_corner
element_axis_corner_less
<
element_type, Parameters, Translator, indexable_tag, Corner, AxisIndex
> elements_less(parameters, translator);
> elements_less(translator, strategy);
std::sort(elements_copy.begin(), elements_copy.end(), elements_less); // MAY THROW, BASIC (copy)
// {
// typename Elements::iterator f = elements_copy.begin() + index_first;
@@ -149,9 +156,6 @@ struct choose_split_axis_and_index_for_corner
smallest_overlap = (std::numeric_limits<content_type>::max)();
smallest_content = (std::numeric_limits<content_type>::max)();
typename index::detail::strategy_type<Parameters>::type
strategy = index::detail::get_strategy(parameters);
// calculate sum of margins for all distributions
for ( size_t i = index_first ; i < index_last ; ++i )
{
@@ -370,10 +374,13 @@ struct nth_element
typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
typedef typename tag<indexable_type>::type indexable_tag;
typename index::detail::strategy_type<Parameters>::type
strategy = index::detail::get_strategy(parameters);
element_axis_corner_less
<
element_type, Parameters, Translator, indexable_tag, Corner, I
> less(parameters, tr);
> less(tr, strategy);
index::detail::nth_element(elements.begin(), elements.begin() + index, elements.end(), less); // MAY THROW, BASIC (copy)
}
}
@@ -464,17 +471,18 @@ struct redistribute_elements<Value, Options, Translator, Box, Allocators, rstar_
BOOST_TRY
{
typename index::detail::strategy_type<parameters_type>::type const&
strategy = index::detail::get_strategy(parameters);
// copy elements to nodes
elements1.assign(elements_copy.begin(), elements_copy.begin() + split_index); // MAY THROW, BASIC
elements2.assign(elements_copy.begin() + split_index, elements_copy.end()); // MAY THROW, BASIC
// calculate boxes
box1 = rtree::elements_box<Box>(elements1.begin(), elements1.end(),
translator,
index::detail::get_strategy(parameters));
translator, strategy);
box2 = rtree::elements_box<Box>(elements2.begin(), elements2.end(),
translator,
index::detail::get_strategy(parameters));
translator, strategy);
}
BOOST_CATCH(...)
{

View File

@@ -290,12 +290,14 @@ template <typename Parameters>
struct strategy_type
{
typedef default_strategy type;
typedef default_strategy result_type;
};
template <typename Parameters, typename Strategy>
struct strategy_type< parameters<Parameters, Strategy> >
{
typedef Strategy type;
typedef Strategy const& result_type;
};
@@ -318,7 +320,7 @@ struct get_strategy_impl<parameters<Parameters, Strategy> >
};
template <typename Parameters>
inline typename strategy_type<Parameters>::type
inline typename strategy_type<Parameters>::result_type
get_strategy(Parameters const& parameters)
{
return get_strategy_impl<Parameters>::apply(parameters);