BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Hilbert_sort_2.h
Go to the documentation of this file.
00001 // Copyright (c) 2007  INRIA Sophia-Antipolis (France).
00002 // All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License as
00006 // published by the Free Software Foundation; version 2.1 of the License.
00007 // See the file LICENSE.LGPL distributed with CGAL.
00008 //
00009 // Licensees holding a valid commercial license may use this file in
00010 // accordance with the commercial license agreement provided with the software.
00011 //
00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 //
00015 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Spatial_sorting/include/CGAL/Hilbert_sort_2.h $
00016 // $Id: Hilbert_sort_2.h 47102 2008-11-28 09:07:23Z spion $
00017 //
00018 // Author(s)     : Christophe Delage
00019 
00020 #ifndef CGAL_HILBERT_SORT_2_H
00021 #define CGAL_HILBERT_SORT_2_H
00022 
00023 #include <CGAL/basic.h>
00024 #include <functional>
00025 #include <cstddef>
00026 #include <CGAL/Hilbert_sort_base.h>
00027 
00028 CGAL_BEGIN_NAMESPACE
00029 
00030 namespace CGALi {
00031     template <class K, int x, bool up> struct Hilbert_cmp_2;
00032 
00033     template <class K, int x>
00034     struct Hilbert_cmp_2<K,x,true>
00035         : public std::binary_function<typename K::Point_2,
00036                                       typename K::Point_2, bool>
00037     {
00038         typedef typename K::Point_2 Point;
00039         K k;
00040         Hilbert_cmp_2 (const K &_k = K()) : k(_k) {}
00041         bool operator() (const Point &p, const Point &q) const
00042         { 
00043             return Hilbert_cmp_2<K,x,false> (k) (q, p);
00044         }
00045     };
00046     
00047     template <class K>
00048     struct Hilbert_cmp_2<K,0,false>
00049         : public std::binary_function<typename K::Point_2,
00050                                       typename K::Point_2, bool>
00051     {
00052         typedef typename K::Point_2 Point;
00053         K k;
00054         Hilbert_cmp_2 (const K &_k = K()) : k(_k) {}
00055         bool operator() (const Point &p, const Point &q) const
00056         { 
00057             return k.less_x_2_object() (p, q);
00058         }
00059     };
00060     
00061     template <class K>
00062     struct Hilbert_cmp_2<K,1,false>
00063         : public std::binary_function<typename K::Point_2,
00064                                       typename K::Point_2, bool>
00065     {
00066         typedef typename K::Point_2 Point;
00067         K k;
00068         Hilbert_cmp_2 (const K &_k = K()) : k(_k) {}
00069         bool operator() (const Point &p, const Point &q) const
00070         { 
00071             return k.less_y_2_object() (p, q);
00072         }
00073     };
00074 }
00075 
00076 template <class K>
00077 class Hilbert_sort_2
00078 {
00079 public:
00080     typedef K Kernel;
00081     typedef typename Kernel::Point_2 Point;
00082     
00083 private:
00084     Kernel _k;
00085     std::ptrdiff_t _limit;
00086 
00087     template <int x, bool up> struct Cmp : public CGALi::Hilbert_cmp_2<Kernel,x,up>
00088     { Cmp (const Kernel &k) : CGALi::Hilbert_cmp_2<Kernel,x,up> (k) {} };
00089 
00090 public:
00091     Hilbert_sort_2 (const Kernel &k = Kernel(), std::ptrdiff_t limit = 1)
00092         : _k(k), _limit (limit)
00093     {}
00094 
00095     template <int x, bool upx, bool upy, class RandomAccessIterator>
00096     void sort (RandomAccessIterator begin, RandomAccessIterator end) const
00097     {
00098         const int y = (x + 1) % 2;
00099         if (end - begin <= _limit) return;
00100 
00101         RandomAccessIterator m0 = begin, m4 = end;
00102 
00103         RandomAccessIterator m2 = CGALi::hilbert_split (m0, m4, Cmp< x,  upx> (_k));
00104         RandomAccessIterator m1 = CGALi::hilbert_split (m0, m2, Cmp< y,  upy> (_k));
00105         RandomAccessIterator m3 = CGALi::hilbert_split (m2, m4, Cmp< y, !upy> (_k));
00106 
00107         sort<y, upy, upx> (m0, m1);
00108         sort<x, upx, upy> (m1, m2);
00109         sort<x, upx, upy> (m2, m3);
00110         sort<y,!upy,!upx> (m3, m4);
00111     }
00112 
00113     template <class RandomAccessIterator>
00114     void operator() (RandomAccessIterator begin, RandomAccessIterator end) const
00115     {
00116         sort <0, false, false> (begin, end);
00117     }
00118 };
00119 
00120 CGAL_END_NAMESPACE
00121 
00122 #endif//CGAL_HILBERT_SORT_2_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines