BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/in_place_edge_list.h
Go to the documentation of this file.
00001 // Copyright (c) 2003,2004  INRIA Sophia-Antipolis (France).
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/Apollonius_graph_2/include/CGAL/in_place_edge_list.h $
00015 // $Id: in_place_edge_list.h 28567 2006-02-16 14:30:13Z lsaboret $
00016 // 
00017 //
00018 // Author(s)     : Menelaos Karavelas <mkaravel@cse.nd.edu>
00019 
00020 
00021 
00022 #ifndef CGAL_IN_PLACE_EDGE_LIST_H
00023 #define CGAL_IN_PLACE_EDGE_LIST_H
00024 
00025 CGAL_BEGIN_NAMESPACE
00026 
00027 template<class Edge>
00028 class In_place_edge_list {
00029 private:
00030   typedef typename Edge::first_type          Face_handle;
00031   typedef typename Face_handle::value_type   Face;
00032 
00033 private:
00034   Edge _front;
00035   unsigned int _size;
00036 
00037 private:
00038   inline void increase_size() {
00039     _size++;
00040   }
00041 
00042   inline void decrease_size() {
00043     _size--;
00044   }
00045 
00046 public:
00047   bool is_valid() const { return true; }
00048 
00049   inline unsigned int size() const {
00050     return _size;
00051   }
00052 
00053   inline void push_first(const Edge& e) {
00054     _front = e;
00055     set_next(e, e);
00056     set_previous(e, e);
00057     increase_size();
00058   }
00059 
00060   inline Edge next(const Edge& e) const {
00061     CGAL_precondition( is_in_list(e) );
00062     return e.first->next(e.second);
00063   }
00064 
00065   inline Edge previous(const Edge& e) const {
00066     CGAL_precondition( is_in_list(e) );
00067     return e.first->previous(e.second);
00068   }
00069 
00070   inline void set_next(const Edge& e, const Edge& next) {
00071     Edge _next(next.first, next.second);
00072     e.first->set_next(e.second, _next);
00073   }
00074 
00075   inline void set_previous(const Edge& e, const Edge& prev) {
00076     Edge _prev(prev.first, prev.second);
00077     e.first->set_previous(e.second, _prev);
00078   }
00079 
00080   inline bool is_first(const Edge& e) const {
00081     return ( (e.first == _front.first &&
00082               e.second == _front.second) );
00083   }
00084 
00085 public:
00086   inline bool is_singleton() const {
00087     CGAL_precondition( !is_empty() );
00088     return (size() == 1);
00089   }
00090 
00091 public:
00092   In_place_edge_list(const Edge& e = Edge(Face_handle(),-1) )
00093     : _size(0) {
00094     _front = e;
00095   }
00096 
00097   inline Edge front() const {
00098     CGAL_precondition( !is_empty() );
00099     return _front;
00100   }
00101 
00102   inline Edge back() const {
00103     CGAL_precondition( !is_empty() );
00104     return previous(_front);
00105   }
00106 
00107   inline bool is_empty() const {
00108     return ( _front.first == Face_handle() );
00109   }
00110 
00111   inline void pop() {
00112     CGAL_precondition( !is_empty() );
00113     remove(front()); // it is important here that I do not pass the
00114     // variable _front but rather a copy of it...
00115   }
00116 
00117   inline void push_front(const Edge& e) {
00118     CGAL_precondition( !is_in_list(e) );
00119     push(e);
00120     _front = e;
00121   }
00122 
00123   inline void push_back(const Edge& e) {
00124     push(e);
00125   }
00126 
00127   void push(const Edge& e) {
00128     CGAL_precondition( !is_in_list(e) );
00129 
00130     if ( is_empty() ) {
00131       push_first(e);
00132       return;
00133     }
00134     Edge last_edge = back();
00135     set_next(last_edge, e);
00136     set_next(e, _front);
00137     set_previous(e, last_edge);
00138     set_previous(_front, e);
00139 
00140     increase_size();
00141   }
00142 
00143   inline void insert_after(const Edge& e, const Edge& new_e) {
00144     CGAL_precondition( is_in_list(e) );
00145     Edge old_front = _front;
00146     _front = next(e);
00147     push_front(new_e);
00148     _front = old_front;
00149   }
00150 
00151   inline void insert_before(const Edge& e, const Edge& new_e) {
00152     CGAL_precondition( is_in_list(e) );
00153     Edge old_front = _front;
00154     _front = e;
00155     push(new_e);
00156     _front = old_front;
00157   }
00158 
00159   inline void replace(const Edge& e, const Edge& new_e) {
00160     insert_before(e, new_e);
00161     remove(e);
00162   }
00163 
00164   void remove(const Edge& e) {
00165     CGAL_precondition( is_in_list(e) );
00166     static Edge SENTINEL_QUEUE_EDGE = Edge(Face_handle(), -1);
00167 
00168     if ( is_singleton() ) {
00169       _front = SENTINEL_QUEUE_EDGE;
00170       set_next(e, SENTINEL_QUEUE_EDGE);
00171       set_previous(e, SENTINEL_QUEUE_EDGE);
00172       decrease_size();
00173       return;
00174     }
00175 
00176     Edge _next = next(e);
00177     Edge _prev = previous(e);
00178 
00179     if ( is_first(e) ) {
00180       _front = _next;
00181     }
00182 
00183     set_next(e, SENTINEL_QUEUE_EDGE);
00184     set_previous(e, SENTINEL_QUEUE_EDGE);
00185 
00186     set_next(_prev, _next);
00187     set_previous(_next, _prev);
00188     decrease_size();
00189   }
00190 
00191   bool is_in_list(const Edge& e) const {
00192     return e.first->is_in_list(e.second);
00193   }
00194 
00195   void clear() {
00196     while ( !is_empty() ) {
00197       pop();
00198     }
00199   }
00200 };
00201 
00202 CGAL_END_NAMESPACE
00203 
00204 
00205 #endif // CGAL_IN_EDGE_PLACE_LIST_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines