BWAPI
|
00001 // Copyright (c) 1997-2007 ETH Zurich (Switzerland). 00002 // All rights reserved. 00003 // 00004 // This file is part of CGAL (www.cgal.org); you may redistribute it under 00005 // the terms of the Q Public License version 1.0. 00006 // See the file LICENSE.QPL distributed with CGAL. 00007 // 00008 // Licensees holding a valid commercial license may use this file in 00009 // accordance with the commercial license agreement provided with the software. 00010 // 00011 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00012 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00013 // 00014 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/QP_solver/include/CGAL/QP_solver/functors.h $ 00015 // $Id: functors.h 46451 2008-10-23 14:31:10Z gaertner $ 00016 // 00017 // 00018 // Author(s) : Sven Schoenherr 00019 // Bernd Gaertner <gaertner@inf.ethz.ch> 00020 // Franz Wessendorp 00021 // Kaspar Fischer 00022 00023 #ifndef CGAL_QP_SOLVER_FUNCTORS_H 00024 #define CGAL_QP_SOLVER_FUNCTORS_H 00025 00026 #include <CGAL/QP_solver/basic.h> 00027 #include <CGAL/function_objects.h> 00028 #include <functional> 00029 #include <iterator> 00030 00031 CGAL_BEGIN_NAMESPACE 00032 00033 // ================== 00034 // class declarations 00035 // ================== 00036 template < class VectorIt, 00037 bool check_lower = false, bool check_upper = false > 00038 class QP_vector_accessor; 00039 00040 template < class MatrixIt, 00041 bool check_1st_lower = false, bool check_1st_upper = false, 00042 bool check_2nd_lower = false, bool check_2nd_upper = false > 00043 class QP_matrix_accessor; 00044 00045 template < class MatrixIt, typename ET > 00046 class QP_matrix_pairwise_accessor; 00047 00048 00049 template < class RndAccIt > 00050 class Value_by_basic_index; 00051 00052 template<typename Map> 00053 class Map_with_default; 00054 00055 // ===================== 00056 // class implementations 00057 // ===================== 00058 00059 // ------------------- 00060 // QP_vector_accessor 00061 // ------------------- 00062 template < class VectorIt, bool check_lower, bool check_upper > 00063 class QP_vector_accessor : public std::unary_function< 00064 int, typename std::iterator_traits<VectorIt>::value_type > { 00065 00066 public: 00067 typedef typename 00068 std::unary_function< 00069 int, 00070 typename std::iterator_traits<VectorIt>::value_type >::result_type 00071 result_type; 00072 QP_vector_accessor( VectorIt it, int lower = 0, int upper = 0) 00073 : z( 0), v( it), l(lower), u(upper) 00074 {} 00075 00076 result_type operator ( ) ( int i) const 00077 { 00078 if ( check_lower && i < l) return z; 00079 if ( check_upper && i >= u) return z; 00080 return v[ i]; 00081 } 00082 00083 private: 00084 const result_type z; 00085 VectorIt v; 00086 int l; 00087 int u; 00088 }; 00089 00090 // ---------------------------------------------------------------------------- 00091 00092 // ------------------- 00093 // QP_matrix_accessor 00094 // ------------------- 00095 template < class MatrixIt, bool check_1st_lower, bool check_1st_upper, 00096 bool check_2nd_lower, bool check_2nd_upper > 00097 class QP_matrix_accessor { 00098 00099 public: 00100 typedef int argument1_type; 00101 typedef int argument2_type; 00102 typedef typename std::iterator_traits<MatrixIt>::value_type VectorIt; 00103 typedef typename std::iterator_traits<VectorIt>::value_type result_type; 00104 00105 QP_matrix_accessor( MatrixIt it, int lower_1 = 0, int upper_1 = 0, 00106 int lower_2 = 0, int upper_2 = 0) 00107 : z( 0), m( it) 00108 { 00109 if ( check_1st_lower) l1 = lower_1; 00110 if ( check_1st_upper) u1 = upper_1; 00111 if ( check_2nd_lower) l2 = lower_2; 00112 if ( check_2nd_upper) u2 = upper_2; 00113 } 00114 00115 result_type operator () ( int r, int c) const 00116 { 00117 if ( check_1st_lower && ( r < l1)) return z; 00118 if ( check_1st_upper && ( r >= u1)) return z; 00119 if ( check_2nd_lower && ( c < l2)) return z; 00120 if ( check_2nd_upper && ( c >= u2)) return z; 00121 return VectorIt(m[ r])[ c]; 00122 } 00123 00124 private: 00125 const result_type z; 00126 MatrixIt m; 00127 int l1, u1, l2, u2; 00128 }; 00129 00130 // ---------------------------------------------------------------------------- 00131 00132 // ---------------------------- 00133 // QP_matrix_pairwise_accessor 00134 // ---------------------------- 00135 template < class MatrixIt, typename ResultType > 00136 class QP_matrix_pairwise_accessor { 00137 typedef typename std::iterator_traits<MatrixIt>::value_type VectorIt; 00138 00139 public: 00140 typedef int argument_type; 00141 typedef ResultType result_type; 00142 00143 // The following default constructor is needed to make it possible 00144 // to use QP_matrix_pairwise_accessor with CGAL's Join_input_iterator_1 00145 // (more precisely: once Join_input_iterator_1 should not use an internal 00146 // mutable variable 'val' anymore, you can remove the following default 00147 // constructor). 00148 //QP_matrix_pairwise_accessor() {} 00149 00150 QP_matrix_pairwise_accessor( MatrixIt it, int row) 00151 : m (it), v (*(it + row)), r (row) 00152 {} 00153 00154 ResultType operator () ( int c) const 00155 { 00156 // make sure that only entries on or below the diagonal are 00157 // accessed 00158 if (c <= r) 00159 return ResultType(v[ c]); 00160 else 00161 return ResultType((*(m + c))[ r]); 00162 } 00163 00164 private: 00165 MatrixIt m; 00166 VectorIt v; 00167 int r; 00168 }; 00169 00170 00171 // ---------------------------------------------------------------------------- 00172 00173 // -------------------- 00174 // Value_by_basic_index 00175 // -------------------- 00176 template < class RndAccIt > 00177 class Value_by_basic_index : public std::unary_function< 00178 int, typename std::iterator_traits<RndAccIt>::value_type > { 00179 00180 public: 00181 typedef typename 00182 std::unary_function< 00183 int, typename std::iterator_traits 00184 <RndAccIt>::value_type >::result_type 00185 result_type; 00186 00187 Value_by_basic_index( RndAccIt x_B_O_it, int n_original) 00188 : o( x_B_O_it), s( x_B_O_it), 00189 l( n_original), u( n_original+0), 00190 z( 0) 00191 { } 00192 00193 Value_by_basic_index( RndAccIt x_B_O_it, int n_original, 00194 RndAccIt x_B_S_it, int n_slack = 0) 00195 : o( x_B_O_it), s( x_B_S_it), 00196 l( n_original), u( n_original+n_slack), 00197 z( 0) 00198 { } 00199 00200 result_type operator () ( int i) const 00201 { 00202 if ( i < 0) return z; 00203 if ( i >= l && i < u) return s[ i]; 00204 return o[ i]; 00205 } 00206 00207 private: 00208 RndAccIt o, s; 00209 int l, u; 00210 result_type z; 00211 }; 00212 00213 // ---------------------------------------------------------------------------- 00214 00215 // -------------------- 00216 // Access_by_index 00217 // -------------------- 00218 // A functor whose operator(int i) provides access to the i-th element 00219 // of a random access iterator. 00220 template < typename RndAccIt, typename ArgType > 00221 class QP_access_by_index { 00222 public: 00223 typedef typename std::iterator_traits<RndAccIt>::value_type result_type; 00224 00225 QP_access_by_index(RndAccIt it = RndAccIt()) : a(it) {} 00226 00227 result_type operator () (ArgType i) const { return a[i]; } 00228 00229 private: 00230 RndAccIt a; 00231 }; 00232 00233 00234 // ------------------- 00235 // Map_with_default 00236 // ------------------- 00237 template<typename Map> 00238 class Map_with_default { 00239 // public types 00240 public: 00241 typedef typename Map::mapped_type mapped_type; 00242 typedef typename Map::difference_type difference_type; 00243 typedef mapped_type result_type; 00244 // data members 00245 private: 00246 const Map* map; // pointer to map 00247 mapped_type d; // default value 00248 00249 public: 00250 // construction 00251 Map_with_default () 00252 : map(0), d() 00253 {} 00254 00255 Map_with_default (const Map* m, const mapped_type& v = mapped_type()) 00256 : map(m), d(v) 00257 {} 00258 00259 // operator() 00260 const mapped_type& operator() (difference_type n) const { 00261 CGAL_qpe_precondition (map != 0); 00262 typename Map::const_iterator i = map->find (n); 00263 if (i != map->end()) 00264 return i->second; 00265 else 00266 return d; 00267 } 00268 }; 00269 00270 CGAL_END_NAMESPACE 00271 00272 #endif // CGAL_QP_SOLVER_FUNCTORS_H 00273 00274 // ===== EOF ==================================================================