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