| [1474] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| [10757] | 5 | |
|---|
| [1474] | 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| [10757] | 23 | |
|---|
| [1474] | 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | #ifndef TRISTRIP_GRAPH_ARRAY_H |
|---|
| 51 | #define TRISTRIP_GRAPH_ARRAY_H |
|---|
| 52 | |
|---|
| [10764] | 53 | #include <stdlib.h> |
|---|
| 54 | |
|---|
| [1474] | 55 | |
|---|
| 56 | namespace common_structures { |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | template <class nodetype, class arctype> |
|---|
| 63 | class graph_array |
|---|
| 64 | { |
|---|
| 65 | public: |
|---|
| 66 | |
|---|
| 67 | class arc; |
|---|
| 68 | class node; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | typedef size_t nodeid; |
|---|
| [1505] | 72 | typedef typename std::vector<node>::iterator node_iterator; |
|---|
| 73 | typedef typename std::vector<node>::const_iterator const_node_iterator; |
|---|
| 74 | typedef typename std::vector<node>::reverse_iterator node_reverse_iterator; |
|---|
| 75 | typedef typename std::vector<node>::const_reverse_iterator const_node_reverse_iterator; |
|---|
| [1474] | 76 | |
|---|
| 77 | typedef graph_array<nodetype, arctype> _mytype; |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | class arc |
|---|
| 82 | { |
|---|
| 83 | public: |
|---|
| [9655] | 84 | arc() {} |
|---|
| [1474] | 85 | arc & mark() { m_Marker = true; return (* this); } |
|---|
| 86 | arc & unmark() { m_Marker = false; return (* this); } |
|---|
| 87 | bool marked() const { return m_Marker; } |
|---|
| 88 | |
|---|
| 89 | node_iterator initial() const { return m_Initial; } |
|---|
| 90 | node_iterator terminal() const { return m_Terminal; } |
|---|
| 91 | |
|---|
| 92 | arctype & operator * () { return m_Elem; } |
|---|
| 93 | const arctype & operator * () const { return m_Elem; } |
|---|
| 94 | |
|---|
| 95 | protected: |
|---|
| 96 | friend class graph_array<nodetype, arctype>; |
|---|
| 97 | |
|---|
| 98 | arc(const node_iterator & Initial, const node_iterator & Terminal) |
|---|
| 99 | : m_Initial(Initial), m_Terminal(Terminal), m_Marker(false) { } |
|---|
| 100 | |
|---|
| 101 | arc(const node_iterator & Initial, const node_iterator & Terminal, const arctype & Elem) |
|---|
| 102 | : m_Initial(Initial), m_Terminal(Terminal), m_Elem(Elem), m_Marker(false) { } |
|---|
| 103 | |
|---|
| 104 | node_iterator m_Initial; |
|---|
| 105 | node_iterator m_Terminal; |
|---|
| 106 | arctype m_Elem; |
|---|
| 107 | bool m_Marker; |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| [1505] | 112 | typedef typename std::list<arc>::iterator out_arc_iterator; |
|---|
| 113 | typedef typename std::list<arc>::const_iterator const_out_arc_iterator; |
|---|
| [1474] | 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | class node |
|---|
| 118 | { |
|---|
| 119 | public: |
|---|
| 120 | node & mark() { m_Marker = true; return (* this); } |
|---|
| 121 | node & unmark() { m_Marker = false; return (* this); } |
|---|
| 122 | bool marked() const { return m_Marker; } |
|---|
| 123 | |
|---|
| 124 | bool out_empty() const { return m_OutArcs.empty(); } |
|---|
| 125 | size_t number_of_out_arcs() const { return m_OutArcs.size(); } |
|---|
| 126 | |
|---|
| 127 | out_arc_iterator out_begin() { return m_OutArcs.begin(); } |
|---|
| 128 | out_arc_iterator out_end() { return m_OutArcs.end(); } |
|---|
| 129 | const_out_arc_iterator out_begin() const { return m_OutArcs.begin(); } |
|---|
| 130 | const_out_arc_iterator out_end() const { return m_OutArcs.end(); } |
|---|
| 131 | |
|---|
| 132 | nodetype & operator * () { return m_Elem; } |
|---|
| 133 | nodetype * operator -> () { return &m_Elem; } |
|---|
| 134 | const nodetype & operator * () const { return m_Elem; } |
|---|
| 135 | const nodetype * operator -> () const { return &m_Elem; } |
|---|
| 136 | |
|---|
| 137 | nodetype & operator = (const nodetype & Elem) { return (m_Elem = Elem); } |
|---|
| 138 | |
|---|
| [4006] | 139 | node() : m_Marker(false) { } |
|---|
| [1474] | 140 | protected: |
|---|
| 141 | friend class graph_array<nodetype, arctype>; |
|---|
| 142 | friend class std::vector<node>; |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | std::list<arc> m_OutArcs; |
|---|
| 146 | nodetype m_Elem; |
|---|
| 147 | bool m_Marker; |
|---|
| 148 | }; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | graph_array(); |
|---|
| 153 | explicit graph_array(const size_t NbNodes); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | void clear(); |
|---|
| 157 | bool empty() const; |
|---|
| 158 | void setsize(const size_t NbNodes); |
|---|
| 159 | size_t size() const; |
|---|
| 160 | |
|---|
| 161 | node & operator [] (const nodeid & i); |
|---|
| 162 | const node & operator [] (const nodeid & i) const; |
|---|
| 163 | |
|---|
| 164 | node_iterator begin(); |
|---|
| 165 | node_iterator end(); |
|---|
| 166 | const_node_iterator begin() const; |
|---|
| 167 | const_node_iterator end() const; |
|---|
| 168 | |
|---|
| 169 | node_reverse_iterator rbegin(); |
|---|
| 170 | node_reverse_iterator rend(); |
|---|
| 171 | const_node_reverse_iterator rbegin() const; |
|---|
| 172 | const_node_reverse_iterator rend() const; |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | size_t number_of_arcs() const; |
|---|
| 176 | |
|---|
| 177 | void erase_arcs(); |
|---|
| 178 | void erase_arcs(const node_iterator & Initial); |
|---|
| 179 | out_arc_iterator erase_arc(const out_arc_iterator & Pos); |
|---|
| 180 | |
|---|
| 181 | out_arc_iterator insert_arc(const nodeid & Initial, const nodeid & Terminal); |
|---|
| 182 | out_arc_iterator insert_arc(const nodeid & Initial, const nodeid & Terminal, const arctype & Elem); |
|---|
| 183 | out_arc_iterator insert_arc(const node_iterator & Initial, const node_iterator & Terminal); |
|---|
| 184 | out_arc_iterator insert_arc(const node_iterator & Initial, const node_iterator & Terminal, const arctype & Elem); |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | out_arc_iterator insert(const nodeid & Initial, const nodeid & Terminal) { return insert_arc(Initial, Terminal); } |
|---|
| 188 | out_arc_iterator insert(const nodeid & Initial, const nodeid & Terminal, const arctype & Elem) { return insert_arc(Initial, Terminal, Elem); } |
|---|
| 189 | out_arc_iterator insert(const node_iterator & Initial, const node_iterator & Terminal) { return insert_arc(Initial, Terminal); } |
|---|
| 190 | out_arc_iterator insert(const node_iterator & Initial, const node_iterator & Terminal, const arctype & Elem) { return insert_arc(Initial, Terminal, Elem); } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | void swap(_mytype & Right); |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | protected: |
|---|
| [9630] | 200 | |
|---|
| 201 | graph_array& operator = (const graph_array&) { return *this; } |
|---|
| 202 | |
|---|
| [1474] | 203 | size_t m_NbArcs; |
|---|
| 204 | std::vector<node> m_Nodes; |
|---|
| 205 | }; |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | template <class nodetype, class arctype> |
|---|
| 211 | void unmark_nodes(graph_array<nodetype, arctype> & G); |
|---|
| 212 | |
|---|
| 213 | template <class nodetype, class arctype> |
|---|
| [1505] | 214 | void unmark_arcs_from_node(typename graph_array<nodetype, arctype>::node & N); |
|---|
| [1474] | 215 | |
|---|
| 216 | template <class nodetype, class arctype> |
|---|
| 217 | void unmark_arcs(graph_array<nodetype, arctype> & G); |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | template <class nodetype, class arctype> |
|---|
| 227 | inline graph_array<nodetype, arctype>::graph_array() : m_NbArcs(0) { } |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | template <class nodetype, class arctype> |
|---|
| 231 | inline graph_array<nodetype, arctype>::graph_array(const size_t NbNodes) : m_NbArcs(0), m_Nodes(NbNodes) { } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | template <class nodetype, class arctype> |
|---|
| 235 | inline void graph_array<nodetype, arctype>::clear() { |
|---|
| 236 | m_NbArcs = 0; |
|---|
| 237 | m_Nodes.clear(); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | template <class nodetype, class arctype> |
|---|
| 243 | inline bool graph_array<nodetype, arctype>::empty() const { |
|---|
| 244 | return m_Nodes.empty(); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | template <class nodetype, class arctype> |
|---|
| 249 | inline size_t graph_array<nodetype, arctype>::size() const { |
|---|
| 250 | return m_Nodes.size(); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | template <class nodetype, class arctype> |
|---|
| 255 | inline void graph_array<nodetype, arctype>::setsize(const size_t NbNodes) { |
|---|
| 256 | clear(); |
|---|
| 257 | m_Nodes.resize(NbNodes); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | template <class nodetype, class arctype> |
|---|
| [1705] | 262 | inline typename graph_array<nodetype, arctype>::node & graph_array<nodetype, arctype>::operator [] (const nodeid & i) { |
|---|
| [1474] | 263 | return m_Nodes[i]; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | template <class nodetype, class arctype> |
|---|
| [1705] | 268 | inline const typename graph_array<nodetype, arctype>::node & graph_array<nodetype, arctype>::operator [] (const nodeid & i) const { |
|---|
| [1474] | 269 | return m_Nodes[i]; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | template <class nodetype, class arctype> |
|---|
| [1705] | 274 | inline typename graph_array<nodetype, arctype>::node_iterator graph_array<nodetype, arctype>::begin() { |
|---|
| [1474] | 275 | return m_Nodes.begin(); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | template <class nodetype, class arctype> |
|---|
| [1705] | 280 | inline typename graph_array<nodetype, arctype>::node_iterator graph_array<nodetype, arctype>::end() { |
|---|
| [1474] | 281 | return m_Nodes.end(); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | template <class nodetype, class arctype> |
|---|
| [1705] | 286 | inline typename graph_array<nodetype, arctype>::const_node_iterator graph_array<nodetype, arctype>::begin() const { |
|---|
| [1474] | 287 | return m_Nodes.begin(); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | template <class nodetype, class arctype> |
|---|
| [1705] | 292 | inline typename graph_array<nodetype, arctype>::const_node_iterator graph_array<nodetype, arctype>::end() const { |
|---|
| [1474] | 293 | return m_Nodes.end(); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | template <class nodetype, class arctype> |
|---|
| [1705] | 298 | inline typename graph_array<nodetype, arctype>::node_reverse_iterator graph_array<nodetype, arctype>::rbegin() { |
|---|
| [1474] | 299 | return m_Nodes.rbegin(); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | template <class nodetype, class arctype> |
|---|
| [1705] | 304 | inline typename graph_array<nodetype, arctype>::node_reverse_iterator graph_array<nodetype, arctype>::rend() { |
|---|
| [1474] | 305 | return m_Nodes.rend(); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | template <class nodetype, class arctype> |
|---|
| [1705] | 310 | inline typename graph_array<nodetype, arctype>::const_node_reverse_iterator graph_array<nodetype, arctype>::rbegin() const { |
|---|
| [1474] | 311 | return m_Nodes.rbegin(); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | template <class nodetype, class arctype> |
|---|
| [1705] | 316 | inline typename graph_array<nodetype, arctype>::const_node_reverse_iterator graph_array<nodetype, arctype>::rend() const { |
|---|
| [1474] | 317 | return m_Nodes.rend(); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | template <class nodetype, class arctype> |
|---|
| 322 | inline size_t graph_array<nodetype, arctype>::number_of_arcs() const { |
|---|
| 323 | return m_NbArcs; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | template <class nodetype, class arctype> |
|---|
| [1705] | 328 | inline typename graph_array<nodetype, arctype>::out_arc_iterator graph_array<nodetype, arctype>::insert_arc(const nodeid & Initial, const nodeid & Terminal) { |
|---|
| [1474] | 329 | return (insert(begin() + Initial, begin() + Terminal)); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | template <class nodetype, class arctype> |
|---|
| [1705] | 334 | inline typename graph_array<nodetype, arctype>::out_arc_iterator graph_array<nodetype, arctype>::insert_arc(const nodeid & Initial, const nodeid & Terminal, const arctype & Elem) { |
|---|
| [1474] | 335 | return (insert(begin() + Initial, begin() + Terminal, Elem)); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | template <class nodetype, class arctype> |
|---|
| [1705] | 340 | inline typename graph_array<nodetype, arctype>::out_arc_iterator graph_array<nodetype, arctype>::insert_arc(const node_iterator & Initial, const node_iterator & Terminal) { |
|---|
| [1474] | 341 | ++m_NbArcs; |
|---|
| 342 | Initial->m_OutArcs.push_back(arc(Initial, Terminal)); |
|---|
| 343 | return (--(Initial->m_OutArcs.end())); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | template <class nodetype, class arctype> |
|---|
| [1705] | 348 | inline typename graph_array<nodetype, arctype>::out_arc_iterator graph_array<nodetype, arctype>::insert_arc(const node_iterator & Initial, const node_iterator & Terminal, const arctype & Elem) { |
|---|
| [1474] | 349 | ++m_NbArcs; |
|---|
| 350 | Initial->m_OutArcs.push_back(arc(Initial, Terminal, Elem)); |
|---|
| 351 | return (--(Initial->m_OutArcs.end())); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | template <class nodetype, class arctype> |
|---|
| [1705] | 356 | inline typename graph_array<nodetype, arctype>::out_arc_iterator graph_array<nodetype, arctype>::erase_arc(const out_arc_iterator & Pos) { |
|---|
| [1474] | 357 | --m_NbArcs; |
|---|
| 358 | return (Pos->initial()->m_OutArcs.erase(Pos)); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | template <class nodetype, class arctype> |
|---|
| 363 | inline void graph_array<nodetype, arctype>::erase_arcs(const node_iterator & Initial) { |
|---|
| 364 | m_NbArcs -= (Initial->m_OutArcs.size()); |
|---|
| 365 | Initial->m_OutArcs.clear(); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | template <class nodetype, class arctype> |
|---|
| 370 | inline void graph_array<nodetype, arctype>::erase_arcs() { |
|---|
| 371 | m_NbArcs = 0; |
|---|
| [3208] | 372 | for (nodeid i = 0; i < this->Size(); ++i) |
|---|
| [1474] | 373 | m_Nodes[i].m_OutArcs.clear(); |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | template <class nodetype, class arctype> |
|---|
| 378 | inline void graph_array<nodetype, arctype>::swap(_mytype & Right) { |
|---|
| 379 | std::swap(m_NbArcs, Right.m_NbArcs); |
|---|
| 380 | std::swap(m_Nodes, Right.m_Nodes); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | template <class nodetype, class arctype> |
|---|
| 390 | void unmark_nodes(graph_array<nodetype, arctype> & G) |
|---|
| 391 | { |
|---|
| [2085] | 392 | typedef typename graph_array<nodetype, arctype>::node_iterator node_it; |
|---|
| [1474] | 393 | |
|---|
| 394 | for (node_it NodeIt = G.begin(); NodeIt != G.end(); ++NodeIt) |
|---|
| 395 | NodeIt->unmark(); |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | template <class nodetype, class arctype> |
|---|
| [1505] | 400 | void unmark_arcs_from_node(typename graph_array<nodetype, arctype>::node & N) |
|---|
| [1474] | 401 | { |
|---|
| [2085] | 402 | typedef typename graph_array<nodetype, arctype>::out_arc_iterator arc_it; |
|---|
| [1474] | 403 | |
|---|
| 404 | for (arc_it ArcIt = N.out_begin(); ArcIt != N.out_end(); ++ArcIt) |
|---|
| 405 | ArcIt->unmark(); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | template <class nodetype, class arctype> |
|---|
| 410 | void unmark_arcs(graph_array<nodetype, arctype> & G) |
|---|
| 411 | { |
|---|
| [2085] | 412 | typedef typename graph_array<nodetype, arctype>::node_iterator node_it; |
|---|
| [1474] | 413 | |
|---|
| 414 | for (node_it NodeIt = G.begin(); NodeIt != G.end(); ++NodeIt) |
|---|
| 415 | unmark_arcs_from_node(* NodeIt); |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| [1562] | 421 | } |
|---|
| [1474] | 422 | |
|---|
| 423 | #endif |
|---|