Skip to content

Commit a8bd9a0

Browse files
committed
[partition] minor changes and added robustness test
1 parent 03489ca commit a8bd9a0

File tree

3 files changed

+447
-75
lines changed

3 files changed

+447
-75
lines changed

include/boost/geometry/algorithms/detail/partition.hpp

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,39 @@ struct divide_interval<T, true>
5252
{
5353
static inline T apply(T const& mi, T const& ma)
5454
{
55-
// avoid overflow
55+
// Avoid overflow
5656
return mi / 2 + ma / 2 + (mi % 2 + ma % 2) / 2;
5757
}
5858
};
5959

60-
template <int Dimension, typename Box>
60+
struct visit_no_policy
61+
{
62+
template <typename Box>
63+
static inline void apply(Box const&, std::size_t )
64+
{}
65+
};
66+
67+
struct include_all_policy
68+
{
69+
template <typename Item>
70+
static inline bool apply(Item const&)
71+
{
72+
return true;
73+
}
74+
};
75+
76+
77+
template <std::size_t Dimension, typename Box>
6178
inline void divide_box(Box const& box, Box& lower_box, Box& upper_box)
6279
{
63-
typedef typename coordinate_type<Box>::type ctype;
80+
using coor_t = typename coordinate_type<Box>::type;
6481

65-
// Divide input box into two parts, e.g. left/right
66-
ctype mid = divide_interval<ctype>::apply(
67-
geometry::get<min_corner, Dimension>(box),
68-
geometry::get<max_corner, Dimension>(box));
82+
// Divide input box into two halves
83+
// either left/right (Dimension 0)
84+
// or top/bottom (Dimension 1)
85+
coor_t const mid
86+
= divide_interval<coor_t>::apply(geometry::get<min_corner, Dimension>(box),
87+
geometry::get<max_corner, Dimension>(box));
6988

7089
lower_box = box;
7190
upper_box = box;
@@ -143,7 +162,7 @@ inline bool handle_one(IteratorVector const& input, VisitPolicy& visitor)
143162
{
144163
if (! visitor.apply(**it1, **it2))
145164
{
146-
return false; // interrupt
165+
return false; // Bail out if visitor returns false
147166
}
148167
}
149168
}
@@ -174,7 +193,7 @@ inline bool handle_two(IteratorVector1 const& input1,
174193
{
175194
if (! visitor.apply(*it1, *it2))
176195
{
177-
return false; // interrupt
196+
return false; // Bail out if visitor returns false
178197
}
179198
}
180199
}
@@ -215,11 +234,11 @@ inline bool recurse_ok(IteratorVector1 const& input1,
215234
}
216235

217236

218-
template <int Dimension, typename Box>
237+
template <std::size_t Dimension, typename Box>
219238
class partition_two_ranges;
220239

221240

222-
template <int Dimension, typename Box>
241+
template <std::size_t Dimension, typename Box>
223242
class partition_one_range
224243
{
225244
template <typename IteratorVector, typename ExpandPolicy>
@@ -328,10 +347,10 @@ public :
328347
if (! boost::empty(exceeding))
329348
{
330349
// Get the box of exceeding-only
331-
Box exceeding_box = get_new_box(exceeding, expand_policy);
350+
Box const exceeding_box = get_new_box(exceeding, expand_policy);
332351

333-
// Recursively do exceeding elements only, in next dimension they
334-
// will probably be less exceeding within the new box
352+
// Recursively do exceeding elements only, in next dimension they
353+
// will probably be less exceeding within the new box
335354
if (! (next_level(exceeding_box, exceeding, level, min_elements,
336355
visitor, expand_policy, overlaps_policy, box_policy)
337356
// Switch to two forward ranges, combine exceeding with
@@ -341,7 +360,7 @@ public :
341360
&& next_level2(exceeding_box, exceeding, upper, level, min_elements,
342361
visitor, expand_policy, overlaps_policy, box_policy)) )
343362
{
344-
return false; // interrupt
363+
return false; // Bail out if visitor returns false
345364
}
346365
}
347366

@@ -355,7 +374,7 @@ public :
355374

356375
template
357376
<
358-
int Dimension,
377+
std::size_t Dimension,
359378
typename Box
360379
>
361380
class partition_two_ranges
@@ -459,20 +478,20 @@ public :
459478

460479
if (recurse_ok(exceeding1, exceeding2, min_elements, level))
461480
{
462-
Box exceeding_box = get_new_box(exceeding1, exceeding2,
463-
expand_policy1, expand_policy2);
481+
Box const exceeding_box = get_new_box(exceeding1, exceeding2,
482+
expand_policy1, expand_policy2);
464483
if (! next_level(exceeding_box, exceeding1, exceeding2, level,
465484
min_elements, visitor, expand_policy1, overlaps_policy1,
466485
expand_policy2, overlaps_policy2, box_policy))
467486
{
468-
return false; // interrupt
487+
return false; // Bail out if visitor returns false
469488
}
470489
}
471490
else
472491
{
473492
if (! handle_two(exceeding1, exceeding2, visitor))
474493
{
475-
return false; // interrupt
494+
return false; // Bail out if visitor returns false
476495
}
477496
}
478497

@@ -482,23 +501,23 @@ public :
482501
// the same combinations again and again)
483502
if (recurse_ok(lower2, upper2, exceeding1, min_elements, level))
484503
{
485-
Box exceeding_box = get_new_box(exceeding1, expand_policy1);
504+
Box const exceeding_box = get_new_box(exceeding1, expand_policy1);
486505
if (! (next_level(exceeding_box, exceeding1, lower2, level,
487506
min_elements, visitor, expand_policy1, overlaps_policy1,
488507
expand_policy2, overlaps_policy2, box_policy)
489508
&& next_level(exceeding_box, exceeding1, upper2, level,
490509
min_elements, visitor, expand_policy1, overlaps_policy1,
491510
expand_policy2, overlaps_policy2, box_policy)) )
492511
{
493-
return false; // interrupt
512+
return false; // Bail out if visitor returns false
494513
}
495514
}
496515
else
497516
{
498517
if (! (handle_two(exceeding1, lower2, visitor)
499518
&& handle_two(exceeding1, upper2, visitor)) )
500519
{
501-
return false; // interrupt
520+
return false; // Bail out if visitor returns false
502521
}
503522
}
504523
}
@@ -508,23 +527,23 @@ public :
508527
// All exceeding from 2 with lower and upper of 1:
509528
if (recurse_ok(lower1, upper1, exceeding2, min_elements, level))
510529
{
511-
Box exceeding_box = get_new_box(exceeding2, expand_policy2);
530+
Box const exceeding_box = get_new_box(exceeding2, expand_policy2);
512531
if (! (next_level(exceeding_box, lower1, exceeding2, level,
513532
min_elements, visitor, expand_policy1, overlaps_policy1,
514533
expand_policy2, overlaps_policy2, box_policy)
515534
&& next_level(exceeding_box, upper1, exceeding2, level,
516535
min_elements, visitor, expand_policy1, overlaps_policy1,
517536
expand_policy2, overlaps_policy2, box_policy)) )
518537
{
519-
return false; // interrupt
538+
return false; // Bail out if visitor returns false
520539
}
521540
}
522541
else
523542
{
524543
if (! (handle_two(lower1, exceeding2, visitor)
525544
&& handle_two(upper1, exceeding2, visitor)) )
526545
{
527-
return false; // interrupt
546+
return false; // Bail out if visitor returns false
528547
}
529548
}
530549
}
@@ -535,14 +554,14 @@ public :
535554
min_elements, visitor, expand_policy1, overlaps_policy1,
536555
expand_policy2, overlaps_policy2, box_policy) )
537556
{
538-
return false; // interrupt
557+
return false; // Bail out if visitor returns false
539558
}
540559
}
541560
else
542561
{
543562
if (! handle_two(lower1, lower2, visitor))
544563
{
545-
return false; // interrupt
564+
return false; // Bail out if visitor returns false
546565
}
547566
}
548567

@@ -552,37 +571,21 @@ public :
552571
min_elements, visitor, expand_policy1, overlaps_policy1,
553572
expand_policy2, overlaps_policy2, box_policy) )
554573
{
555-
return false; // interrupt
574+
return false; // Bail out if visitor returns false
556575
}
557576
}
558577
else
559578
{
560579
if (! handle_two(upper1, upper2, visitor))
561580
{
562-
return false; // interrupt
581+
return false; // Bail out if visitor returns false
563582
}
564583
}
565584

566585
return true;
567586
}
568587
};
569588

570-
struct visit_no_policy
571-
{
572-
template <typename Box>
573-
static inline void apply(Box const&, std::size_t )
574-
{}
575-
};
576-
577-
struct include_all_policy
578-
{
579-
template <typename Item>
580-
static inline bool apply(Item const&)
581-
{
582-
return true;
583-
}
584-
};
585-
586589

587590
}} // namespace detail::partition
588591

@@ -667,14 +670,14 @@ class partition
667670
std::size_t min_elements,
668671
VisitBoxPolicy box_visitor)
669672
{
670-
typedef typename boost::range_iterator
673+
using iterator_t = typename boost::range_iterator
671674
<
672675
ForwardRange const
673-
>::type iterator_type;
676+
>::type;
674677

675678
if (std::size_t(boost::size(forward_range)) > min_elements)
676679
{
677-
std::vector<iterator_type> iterator_vector;
680+
std::vector<iterator_t> iterator_vector;
678681
Box total;
679682
assign_inverse(total);
680683
expand_to_range<IncludePolicy1>(forward_range, total,
@@ -688,16 +691,16 @@ class partition
688691
}
689692
else
690693
{
691-
for(auto it1 = boost::begin(forward_range);
694+
for (auto it1 = boost::begin(forward_range);
692695
it1 != boost::end(forward_range);
693696
++it1)
694697
{
695698
auto it2 = it1;
696-
for(++it2; it2 != boost::end(forward_range); ++it2)
699+
for (++it2; it2 != boost::end(forward_range); ++it2)
697700
{
698701
if (! visitor.apply(*it1, *it2))
699702
{
700-
return false; // interrupt
703+
return false; // Bail out if visitor returns false
701704
}
702705
}
703706
}
@@ -793,21 +796,21 @@ class partition
793796
std::size_t min_elements,
794797
VisitBoxPolicy box_visitor)
795798
{
796-
typedef typename boost::range_iterator
799+
using iterator1_t = typename boost::range_iterator
797800
<
798801
ForwardRange1 const
799-
>::type iterator_type1;
802+
>::type;
800803

801-
typedef typename boost::range_iterator
804+
using iterator2_t = typename boost::range_iterator
802805
<
803806
ForwardRange2 const
804-
>::type iterator_type2;
807+
>::type;
805808

806809
if (std::size_t(boost::size(forward_range1)) > min_elements
807810
&& std::size_t(boost::size(forward_range2)) > min_elements)
808811
{
809-
std::vector<iterator_type1> iterator_vector1;
810-
std::vector<iterator_type2> iterator_vector2;
812+
std::vector<iterator1_t> iterator_vector1;
813+
std::vector<iterator2_t> iterator_vector2;
811814
Box total;
812815
assign_inverse(total);
813816
expand_to_range<IncludePolicy1>(forward_range1, total,
@@ -835,7 +838,7 @@ class partition
835838
{
836839
if (! visitor.apply(*it1, *it2))
837840
{
838-
return false; // interrupt
841+
return false; // Bail out if visitor returns false
839842
}
840843
}
841844
}

0 commit comments

Comments
 (0)