From 4e28b57a641fbf45edb33a3a2aa4ebcced03e78c Mon Sep 17 00:00:00 2001 From: Christophe Delage Date: Mon, 6 Mar 2006 15:24:33 +0000 Subject: [PATCH] Big changes. - brio_sort is now called spatial_sort. - global functions now have the same names in 2D and 3D. --- .gitattributes | 6 +- Spatial_sorting/include/CGAL/BRIO_sort.h | 72 ------------------- Spatial_sorting/include/CGAL/Hilbert_sort_2.h | 39 +++------- Spatial_sorting/include/CGAL/Hilbert_sort_3.h | 48 ++++--------- .../include/CGAL/Hilbert_sort_base.h | 3 +- .../include/CGAL/Multiscale_sort.h | 34 +++++++++ Spatial_sorting/include/CGAL/hilbert_sort.h | 51 +++++++++++++ Spatial_sorting/include/CGAL/spatial_sort.h | 55 ++++++++++++++ .../test/Spatial_sorting/test_hilbert.C | 7 +- .../{test_brio.C => test_multiscale.C} | 8 +-- 10 files changed, 176 insertions(+), 147 deletions(-) delete mode 100644 Spatial_sorting/include/CGAL/BRIO_sort.h create mode 100644 Spatial_sorting/include/CGAL/Multiscale_sort.h create mode 100644 Spatial_sorting/include/CGAL/hilbert_sort.h create mode 100644 Spatial_sorting/include/CGAL/spatial_sort.h rename Spatial_sorting/test/Spatial_sorting/{test_brio.C => test_multiscale.C} (91%) diff --git a/.gitattributes b/.gitattributes index 715efb6bf7e..ad52fd2c67f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1445,13 +1445,15 @@ Spatial_searching/demo/Spatial_searching/spatial_searching.vcproj -text Spatial_searching/doc_tex/Spatial_searching/Fig1.gif -text svneol=unset#unset Spatial_searching/doc_tex/Spatial_searching/Fig1.ps -text Spatial_sorting/description.txt -text -Spatial_sorting/include/CGAL/BRIO_sort.h -text Spatial_sorting/include/CGAL/Hilbert_sort_2.h -text Spatial_sorting/include/CGAL/Hilbert_sort_3.h -text Spatial_sorting/include/CGAL/Hilbert_sort_base.h -text +Spatial_sorting/include/CGAL/Multiscale_sort.h -text +Spatial_sorting/include/CGAL/hilbert_sort.h -text +Spatial_sorting/include/CGAL/spatial_sort.h -text Spatial_sorting/maintainer -text -Spatial_sorting/test/Spatial_sorting/test_brio.C -text Spatial_sorting/test/Spatial_sorting/test_hilbert.C -text +Spatial_sorting/test/Spatial_sorting/test_multiscale.C -text Straight_skeleton_2/demo/Straight_skeleton_2/data/alley_0.poly -text Straight_skeleton_2/demo/Straight_skeleton_2/data/alley_1.poly -text Straight_skeleton_2/demo/Straight_skeleton_2/data/alley_2.poly -text diff --git a/Spatial_sorting/include/CGAL/BRIO_sort.h b/Spatial_sorting/include/CGAL/BRIO_sort.h deleted file mode 100644 index 1500ec85348..00000000000 --- a/Spatial_sorting/include/CGAL/BRIO_sort.h +++ /dev/null @@ -1,72 +0,0 @@ -#ifndef CGAL_BRIO_SORT_H -#define CGAL_BRIO_SORT_H - -#include - -#include -#include - -CGAL_BEGIN_NAMESPACE - -template -class BRIO_sort -{ - Sort sort; - int threshold; - int ratio; -public: - BRIO_sort (int _ratio = 8, int _threshold = 64, const Sort &_sort = Sort()) - : sort (_sort), threshold (_threshold), ratio (_ratio) - {} - - void operator() (RandomAccessIterator begin, RandomAccessIterator end) const - { - RandomAccessIterator middle = begin; - if (end - begin >= threshold) { - middle = begin + (end - begin) / ratio; - this->operator() (begin, middle); - } - sort (middle, end); - } -}; - -template -void brio_sort_2 (RandomAccessIterator begin, RandomAccessIterator end, Kernel k) -{ - typedef Hilbert_sort_2 Sort; - (BRIO_sort (4, 16, Sort (k))) (begin, end); -} - -template -void brio_sort_2 (RandomAccessIterator begin, RandomAccessIterator end) -{ - typedef std::iterator_traits ITraits; - typedef typename ITraits::value_type value_type; - typedef CGAL::Kernel_traits KTraits; - typedef typename KTraits::Kernel Kernel; - - brio_sort_2 (begin, end, Kernel ()); -} - -template -void brio_sort_3 (RandomAccessIterator begin, RandomAccessIterator end, Kernel k) -{ - typedef Hilbert_sort_3 Sort; - (BRIO_sort (8, 64, Sort (k))) (begin, end); -} - -template -void brio_sort_3 (RandomAccessIterator begin, RandomAccessIterator end) -{ - typedef std::iterator_traits ITraits; - typedef typename ITraits::value_type value_type; - typedef CGAL::Kernel_traits KTraits; - typedef typename KTraits::Kernel Kernel; - - brio_sort_3 (begin, end, Kernel ()); -} - -CGAL_END_NAMESPACE - - -#endif//CGAL_BRIO_SORT_H diff --git a/Spatial_sorting/include/CGAL/Hilbert_sort_2.h b/Spatial_sorting/include/CGAL/Hilbert_sort_2.h index b6bc598e2a4..19998bb7998 100644 --- a/Spatial_sorting/include/CGAL/Hilbert_sort_2.h +++ b/Spatial_sorting/include/CGAL/Hilbert_sort_2.h @@ -10,7 +10,6 @@ CGAL_BEGIN_NAMESPACE namespace CGALi { template struct Hilbert_cmp_2; - template struct Hilbert_cmp_2 : public std::binary_function +template class Hilbert_sort_2 { public: - typedef RandomAccessIterator Iterator; typedef K Kernel; typedef typename Kernel::Point_2 Point; @@ -71,17 +69,17 @@ private: public: Hilbert_sort_2 (const Kernel &_k = Kernel()) : k(_k) {} - template - void sort (Iterator begin, Iterator end) const + template + void sort (RandomAccessIterator begin, RandomAccessIterator end) const { const int y = (x + 1) % 2; if (end - begin <= 8) return; - Iterator m0 = begin, m4 = end; + RandomAccessIterator m0 = begin, m4 = end; - Iterator m2 = CGALi::hilbert_split (m0, m4, Cmp< x, upx> (k)); - Iterator m1 = CGALi::hilbert_split (m0, m2, Cmp< y, upy> (k)); - Iterator m3 = CGALi::hilbert_split (m2, m4, Cmp< y, !upy> (k)); + RandomAccessIterator m2 = CGALi::hilbert_split (m0, m4, Cmp< x, upx> (k)); + RandomAccessIterator m1 = CGALi::hilbert_split (m0, m2, Cmp< y, upy> (k)); + RandomAccessIterator m3 = CGALi::hilbert_split (m2, m4, Cmp< y, !upy> (k)); if (end - begin <= 8*4) return; @@ -91,30 +89,13 @@ public: sort (m3, m4); } - void operator() (Iterator begin, Iterator end) const + template + void operator() (RandomAccessIterator begin, RandomAccessIterator end) const { sort <0, false, false> (begin, end); } }; -template -void hilbert_sort_2 (RandomAccessIterator begin, RandomAccessIterator end, - Kernel k) -{ - (Hilbert_sort_2 (k)) (begin, end); -} - -template -void hilbert_sort_2 (RandomAccessIterator begin, RandomAccessIterator end) -{ - typedef std::iterator_traits ITraits; - typedef typename ITraits::value_type value_type; - typedef CGAL::Kernel_traits KTraits; - typedef typename KTraits::Kernel Kernel; - - hilbert_sort_2 (begin, end, Kernel()); -} - CGAL_END_NAMESPACE #endif//CGAL_HILBERT_SORT_2_H diff --git a/Spatial_sorting/include/CGAL/Hilbert_sort_3.h b/Spatial_sorting/include/CGAL/Hilbert_sort_3.h index d673d494b2b..3fb986f7802 100644 --- a/Spatial_sorting/include/CGAL/Hilbert_sort_3.h +++ b/Spatial_sorting/include/CGAL/Hilbert_sort_3.h @@ -10,7 +10,6 @@ CGAL_BEGIN_NAMESPACE namespace CGALi { template struct Hilbert_cmp_3; - template struct Hilbert_cmp_3 : public std::binary_function +template class Hilbert_sort_3 { public: - typedef RandomAccessIterator Iterator; typedef K Kernel; typedef typename Kernel::Point_3 Point; @@ -86,21 +83,21 @@ private: public: Hilbert_sort_3 (const Kernel &_k = Kernel()) : k(_k) {} - template - void sort (Iterator begin, Iterator end) const + template + void sort (RandomAccessIterator begin, RandomAccessIterator end) const { const int y = (x + 1) % 3, z = (x + 2) % 3; if (end - begin <= 8) return; - Iterator m0 = begin, m8 = end; + RandomAccessIterator m0 = begin, m8 = end; - Iterator m4 = CGALi::hilbert_split (m0, m8, Cmp< x, upx> (k)); - Iterator m2 = CGALi::hilbert_split (m0, m4, Cmp< y, upy> (k)); - Iterator m1 = CGALi::hilbert_split (m0, m2, Cmp< z, upz> (k)); - Iterator m3 = CGALi::hilbert_split (m2, m4, Cmp< z, !upz> (k)); - Iterator m6 = CGALi::hilbert_split (m4, m8, Cmp< y, !upy> (k)); - Iterator m5 = CGALi::hilbert_split (m4, m6, Cmp< z, upz> (k)); - Iterator m7 = CGALi::hilbert_split (m6, m8, Cmp< z, !upz> (k)); + RandomAccessIterator m4 = CGALi::hilbert_split (m0, m8, Cmp< x, upx> (k)); + RandomAccessIterator m2 = CGALi::hilbert_split (m0, m4, Cmp< y, upy> (k)); + RandomAccessIterator m1 = CGALi::hilbert_split (m0, m2, Cmp< z, upz> (k)); + RandomAccessIterator m3 = CGALi::hilbert_split (m2, m4, Cmp< z, !upz> (k)); + RandomAccessIterator m6 = CGALi::hilbert_split (m4, m8, Cmp< y, !upy> (k)); + RandomAccessIterator m5 = CGALi::hilbert_split (m4, m6, Cmp< z, upz> (k)); + RandomAccessIterator m7 = CGALi::hilbert_split (m6, m8, Cmp< z, !upz> (k)); if (end - begin <= 8*8) return; @@ -114,30 +111,13 @@ public: sort (m7, m8); } - void operator() (Iterator begin, Iterator end) const + template + void operator() (RandomAccessIterator begin, RandomAccessIterator end) const { sort <0, false, false, false> (begin, end); } }; -template -void hilbert_sort_3 (RandomAccessIterator begin, RandomAccessIterator end, - Kernel k) -{ - (Hilbert_sort_3 (k)) (begin, end); -} - -template -void hilbert_sort_3 (RandomAccessIterator begin, RandomAccessIterator end) -{ - typedef std::iterator_traits ITraits; - typedef typename ITraits::value_type value_type; - typedef CGAL::Kernel_traits KTraits; - typedef typename KTraits::Kernel Kernel; - - return hilbert_sort_3 (begin, end, Kernel()); -} - CGAL_END_NAMESPACE #endif//CGAL_HILBERT_SORT_3_H diff --git a/Spatial_sorting/include/CGAL/Hilbert_sort_base.h b/Spatial_sorting/include/CGAL/Hilbert_sort_base.h index 4043e71e50f..f809ca9cd27 100644 --- a/Spatial_sorting/include/CGAL/Hilbert_sort_base.h +++ b/Spatial_sorting/include/CGAL/Hilbert_sort_base.h @@ -19,9 +19,8 @@ namespace CGALi { std::nth_element (begin, middle, end, cmp); return middle; } -}; +} CGAL_END_NAMESPACE - #endif//CGAL_HILBERT_SORT_BASE_H diff --git a/Spatial_sorting/include/CGAL/Multiscale_sort.h b/Spatial_sorting/include/CGAL/Multiscale_sort.h new file mode 100644 index 00000000000..a1776114e67 --- /dev/null +++ b/Spatial_sorting/include/CGAL/Multiscale_sort.h @@ -0,0 +1,34 @@ +#ifndef CGAL_MULTISCALE_SORT_H +#define CGAL_MULTISCALE_SORT_H + +#include + +CGAL_BEGIN_NAMESPACE + +template +class Multiscale_sort +{ + Sort sort; + int threshold; + int ratio; + +public: + Multiscale_sort (int _ratio = 8, int _threshold = 64, const Sort &_sort = Sort()) + : sort (_sort), threshold (_threshold), ratio (_ratio) + {} + + template + void operator() (RandomAccessIterator begin, RandomAccessIterator end) const + { + RandomAccessIterator middle = begin; + if (end - begin >= threshold) { + middle = begin + (end - begin) / ratio; + this->operator() (begin, middle); + } + sort (middle, end); + } +}; + +CGAL_END_NAMESPACE + +#endif//CGAL_MULTISCALE_SORT_H diff --git a/Spatial_sorting/include/CGAL/hilbert_sort.h b/Spatial_sorting/include/CGAL/hilbert_sort.h new file mode 100644 index 00000000000..dff71e10cf4 --- /dev/null +++ b/Spatial_sorting/include/CGAL/hilbert_sort.h @@ -0,0 +1,51 @@ +#ifndef CGAL_HILBERT_SORT_H +#define CGAL_HILBERT_SORT_H + +#include + +#include +#include + +CGAL_BEGIN_NAMESPACE + +namespace CGALi { + + template + void hilbert_sort (RandomAccessIterator begin, RandomAccessIterator end, + const Kernel &k, typename Kernel::Point_2 *) + { + (Hilbert_sort_2 (k)) (begin, end); + } + + template + void hilbert_sort (RandomAccessIterator begin, RandomAccessIterator end, + const Kernel &k, typename Kernel::Point_3 *) + { + (Hilbert_sort_3 (k)) (begin, end); + } +} + +template +void hilbert_sort (RandomAccessIterator begin, RandomAccessIterator end, + const Kernel &k) +{ + typedef std::iterator_traits ITraits; + typedef typename ITraits::value_type value_type; + + CGALi::hilbert_sort (begin, end, k, static_cast (0)); +} + +template +void hilbert_sort (RandomAccessIterator begin, RandomAccessIterator end) +{ + typedef std::iterator_traits ITraits; + typedef typename ITraits::value_type value_type; + typedef CGAL::Kernel_traits KTraits; + typedef typename KTraits::Kernel Kernel; + + hilbert_sort (begin, end, Kernel()); +} + +CGAL_END_NAMESPACE + +#endif//CGAL_HILBERT_SORT_H diff --git a/Spatial_sorting/include/CGAL/spatial_sort.h b/Spatial_sorting/include/CGAL/spatial_sort.h new file mode 100644 index 00000000000..66a95e81988 --- /dev/null +++ b/Spatial_sorting/include/CGAL/spatial_sort.h @@ -0,0 +1,55 @@ +#ifndef CGAL_SPATIAL_SORT_H +#define CGAL_SPATIAL_SORT_H + +#include + +#include +#include + +#include + +CGAL_BEGIN_NAMESPACE + +namespace CGALi { + + template + void spatial_sort (RandomAccessIterator begin, RandomAccessIterator end, + const Kernel &k, typename Kernel::Point_2 *) + { + typedef Hilbert_sort_2 Sort; + (Multiscale_sort (4, 16, Sort (k))) (begin, end); + } + + template + void spatial_sort (RandomAccessIterator begin, RandomAccessIterator end, + const Kernel &k, typename Kernel::Point_3 *) + { + typedef Hilbert_sort_3 Sort; + (Multiscale_sort (8, 64, Sort (k))) (begin, end); + } +} + +template +void spatial_sort (RandomAccessIterator begin, RandomAccessIterator end, + const Kernel &k) +{ + typedef std::iterator_traits ITraits; + typedef typename ITraits::value_type value_type; + + CGALi::spatial_sort (begin, end, k, static_cast (0)); +} + +template +void spatial_sort (RandomAccessIterator begin, RandomAccessIterator end) +{ + typedef std::iterator_traits ITraits; + typedef typename ITraits::value_type value_type; + typedef CGAL::Kernel_traits KTraits; + typedef typename KTraits::Kernel Kernel; + + spatial_sort (begin, end, Kernel()); +} + +CGAL_END_NAMESPACE + +#endif//CGAL_SPATIAL_SORT_H diff --git a/Spatial_sorting/test/Spatial_sorting/test_hilbert.C b/Spatial_sorting/test/Spatial_sorting/test_hilbert.C index 9e34b3e2c4f..6ddfed9ae7b 100644 --- a/Spatial_sorting/test/Spatial_sorting/test_hilbert.C +++ b/Spatial_sorting/test/Spatial_sorting/test_hilbert.C @@ -1,7 +1,6 @@ #include -#include -#include +#include #include @@ -45,7 +44,7 @@ int main () std::cout << " Sorting points... " << std::flush; - CGAL::hilbert_sort_2 (v.begin(), v.end()); + CGAL::hilbert_sort (v.begin(), v.end()); std::cout << "done." << std::endl; @@ -75,7 +74,7 @@ int main () std::cout << " Sorting points... " << std::flush; - CGAL::hilbert_sort_3 (v.begin(), v.end()); + CGAL::hilbert_sort (v.begin(), v.end()); std::cout << "done." << std::endl; diff --git a/Spatial_sorting/test/Spatial_sorting/test_brio.C b/Spatial_sorting/test/Spatial_sorting/test_multiscale.C similarity index 91% rename from Spatial_sorting/test/Spatial_sorting/test_brio.C rename to Spatial_sorting/test/Spatial_sorting/test_multiscale.C index f8cd691e4a3..ccadf27a266 100644 --- a/Spatial_sorting/test/Spatial_sorting/test_brio.C +++ b/Spatial_sorting/test/Spatial_sorting/test_multiscale.C @@ -1,6 +1,6 @@ #include -#include +#include #include @@ -25,7 +25,7 @@ int main () const int nb_points_2 = 50000, nb_points_3 = 50000; CGAL::Random random (42); - std::cout << "Testing BRIO + Hilbert sort." << std::endl; + std::cout << "Testing Multiscale sort." << std::endl; { std::cout << "Testing 2D: Generating points... " << std::flush; @@ -44,7 +44,7 @@ int main () std::cout << " Sorting points... " << std::flush; - CGAL::brio_sort_2 (v.begin(), v.end()); + CGAL::spatial_sort (v.begin(), v.end()); std::cout << "done." << std::endl; @@ -74,7 +74,7 @@ int main () std::cout << " Sorting points... " << std::flush; - CGAL::brio_sort_3 (v.begin(), v.end()); + CGAL::spatial_sort (v.begin(), v.end()); std::cout << "done." << std::endl;