BWAPI
|
00001 // Copyright (c) 2005 Tel-Aviv University (Israel). 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/Arrangement_on_surface_2/include/CGAL/Arr_vertex_map.h $ 00015 // $Id: Arr_vertex_map.h 53436 2009-12-15 23:14:31Z lrineau $ 00016 // 00017 // 00018 // Author(s) : Ron Wein <wein@post.tau.ac.il> 00019 // Efi Fogel <efif@post.tau.ac.il> 00020 00021 #ifndef CGAL_ARR_VERTEX_MAP_H 00022 #define CGAL_ARR_VERTEX_MAP_H 00023 00028 #include <CGAL/Unique_hash_map.h> 00029 #if BOOST_VERSION >= 104000 00030 #include <boost/property_map/property_map.hpp> 00031 #else 00032 #include <boost/property_map.hpp> 00033 #endif 00034 #include <boost/graph/properties.hpp> 00035 00036 CGAL_BEGIN_NAMESPACE 00037 00043 template <class Arrangement_> 00044 class Arr_vertex_index_map : public Arr_observer<Arrangement_> 00045 { 00046 public: 00047 00048 typedef Arrangement_ Arrangement_2; 00049 typedef typename Arrangement_2::Vertex_handle Vertex_handle; 00050 00051 // Boost property type definitions: 00052 typedef boost::readable_property_map_tag category; 00053 typedef unsigned int value_type; 00054 typedef value_type reference; 00055 typedef Vertex_handle key_type; 00056 00057 private: 00058 00059 typedef Arr_vertex_index_map<Arrangement_2> Self; 00060 typedef Arr_observer<Arrangement_2> Base; 00061 00062 typedef Unique_hash_map<Vertex_handle, unsigned int> Index_map; 00063 00064 // Data members: 00065 unsigned int n_vertices; // The current number of vertices. 00066 Index_map index_map; // Mapping vertices to indices. 00067 std::vector<Vertex_handle> rev_map; // Mapping indices to vertices. 00068 00069 enum {MIN_REV_MAP_SIZE = 32}; 00070 00071 public: 00072 00074 Arr_vertex_index_map () : 00075 Base (), 00076 n_vertices (0), 00077 rev_map (MIN_REV_MAP_SIZE) 00078 {} 00079 00081 Arr_vertex_index_map (const Arrangement_2& arr) : 00082 Base (const_cast<Arrangement_2&> (arr)) 00083 { 00084 _init(); 00085 } 00086 00088 Arr_vertex_index_map (const Self& other) : 00089 Base (const_cast<Arrangement_2&> (*(other.arrangement()))) 00090 { 00091 _init(); 00092 } 00093 00095 Self& operator= (const Self& other) 00096 { 00097 if (this == &other) 00098 return (*this); 00099 00100 this->detach(); 00101 this->attach (const_cast<Arrangement_2&> (*(other.arrangement()))); 00102 00103 return (*this); 00104 } 00105 00111 unsigned int operator[] (Vertex_handle v) const 00112 { 00113 return index_map[v]; 00114 } 00115 00121 Vertex_handle vertex (const int i) const 00122 { 00123 CGAL_precondition (i < n_vertices); 00124 00125 return rev_map[i]; 00126 } 00127 00129 00130 00135 virtual void after_assign () 00136 { 00137 _init(); 00138 } 00139 00143 virtual void after_clear () 00144 { 00145 _init(); 00146 } 00147 00151 virtual void after_attach () 00152 { 00153 _init(); 00154 } 00155 00159 virtual void after_detach () 00160 { 00161 n_vertices = 0; 00162 index_map.clear(); 00163 } 00164 00169 virtual void after_create_vertex (Vertex_handle v) 00170 { 00171 // Update the number of vertices. 00172 n_vertices++; 00173 00174 // If necessary, allocate memory for the reverse mapping. 00175 if (rev_map.size() < n_vertices) 00176 rev_map.resize (2 * n_vertices); 00177 00178 // Update the mapping of the newly created vertex. 00179 index_map[v] = n_vertices - 1; 00180 rev_map[n_vertices - 1] = v; 00181 } 00182 00187 virtual void after_create_boundary_vertex (Vertex_handle v) 00188 { 00189 // Update the number of vertices. 00190 n_vertices++; 00191 00192 // If necessary, allocate memory for the reverse mapping. 00193 if (rev_map.size() < n_vertices) 00194 rev_map.resize (2 * n_vertices); 00195 00196 // Update the mapping of the newly created vertex. 00197 index_map[v] = n_vertices - 1; 00198 rev_map[n_vertices - 1] = v; 00199 } 00200 00205 virtual void before_remove_vertex (Vertex_handle v) 00206 { 00207 // Update the number of vertices. 00208 n_vertices--; 00209 00210 // Reduce memory consumption in case the number of vertices has 00211 // drastically decreased. 00212 if (2*n_vertices+1 < rev_map.size() && 00213 rev_map.size() / 2 >= MIN_REV_MAP_SIZE) 00214 { 00215 rev_map.resize (rev_map.size() / 2); 00216 } 00217 00218 // Get the current vertex index, and assign this index to the vertex 00219 // currently indexed (n - 1). 00220 unsigned int index = index_map[v]; 00221 00222 if (index == n_vertices) 00223 return; 00224 00225 Vertex_handle last_v = rev_map[n_vertices]; 00226 index_map[last_v] = index; 00227 rev_map[index] = last_v; 00228 00229 // Clear the reverse mapping for the last vertex. 00230 rev_map[n_vertices] = Vertex_handle(); 00231 } 00233 00234 private: 00235 00237 void _init () 00238 { 00239 // Get the number of vertices and allocate the reverse map accordingly. 00240 n_vertices = this->arrangement()->number_of_vertices(); 00241 00242 if (n_vertices < MIN_REV_MAP_SIZE) 00243 rev_map.resize (MIN_REV_MAP_SIZE); 00244 else 00245 rev_map.resize (n_vertices); 00246 00247 // Clear the current mapping. 00248 index_map.clear(); 00249 00250 // Create the initial mapping. 00251 typename Arrangement_2::Vertex_iterator vit; 00252 Vertex_handle vh; 00253 unsigned int index = 0; 00254 00255 for (vit = this->arrangement()->vertices_begin(); 00256 vit != this->arrangement()->vertices_end(); ++vit, ++index) 00257 { 00258 // Map the current vertex to the current index. 00259 vh = vit; 00260 index_map[vh] = index; 00261 rev_map[index] = vh; 00262 } 00263 } 00264 00265 }; 00266 00274 template<class Arrangement> 00275 unsigned int get (const CGAL::Arr_vertex_index_map<Arrangement>& index_map, 00276 typename Arrangement::Vertex_handle v) 00277 { 00278 return index_map[v]; 00279 } 00280 00285 template <class Arrangement_, class Type_> 00286 class Arr_vertex_property_map 00287 { 00288 public: 00289 00290 typedef Arrangement_ Arrangement_2; 00291 typedef typename Arrangement_2::Vertex_handle Vertex_handle; 00292 typedef Arr_vertex_index_map<Arrangement_2> Index_map; 00293 00294 // Boost property type definitions: 00295 typedef boost::read_write_property_map_tag category; 00296 typedef Type_ value_type; 00297 typedef value_type& reference; 00298 typedef Vertex_handle key_type; 00299 00300 private: 00301 00302 typedef Arr_vertex_property_map<Arrangement_2, value_type> Self; 00303 00304 // Data members: 00305 const Index_map *ind_map; // The index map. 00306 value_type *props; // The properties vector. 00307 bool owner; // Should the vector be freed. 00308 00310 Self& operator= (const Self& ); 00311 00312 public: 00313 00315 Arr_vertex_property_map (const Index_map& index_map) : 00316 ind_map (&index_map), 00317 owner (true) 00318 { 00319 props = new value_type [index_map.arrangement()->number_of_vertices()]; 00320 } 00321 00323 Arr_vertex_property_map (const Self& map) : 00324 ind_map (map.ind_map), 00325 props (map.props), 00326 owner (false) 00327 {} 00328 00330 ~Arr_vertex_property_map () 00331 { 00332 if (owner) 00333 delete[] props; 00334 } 00335 00337 const value_type& operator[] (Vertex_handle v) const 00338 { 00339 return props[(*ind_map)[v]]; 00340 } 00341 00343 value_type& operator[] (Vertex_handle v) 00344 { 00345 return props[(*ind_map)[v]]; 00346 } 00347 00348 }; 00349 00357 template<class Arrangement, class Type> 00358 const typename CGAL::Arr_vertex_property_map<Arrangement, Type>::value_type& 00359 get (const CGAL::Arr_vertex_property_map<Arrangement, Type>& prop_map, 00360 typename Arrangement::Vertex_handle v) 00361 { 00362 return prop_map[v]; 00363 } 00364 00372 template<class Arrangement, class Type> 00373 void put (CGAL::Arr_vertex_property_map<Arrangement, Type>& prop_map, 00374 typename Arrangement::Vertex_handle v, 00375 typename CGAL::Arr_vertex_property_map<Arrangement, Type>:: 00376 value_type t) 00377 { 00378 prop_map[v] = t; 00379 } 00380 00381 CGAL_END_NAMESPACE 00382 00383 #endif