[1] | 1 | //----------------------------------------------------------- |
---|
| 2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
| 3 | // arnault@lal.in2p3.fr |
---|
| 4 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
| 5 | //----------------------------------------------------------- |
---|
| 6 | |
---|
| 7 | #ifndef __cmt_list_h__ |
---|
| 8 | #define __cmt_list_h__ |
---|
| 9 | |
---|
| 10 | #include <iostream> |
---|
| 11 | |
---|
| 12 | template <class T> |
---|
| 13 | class cmt_list_node |
---|
| 14 | { |
---|
| 15 | public: |
---|
| 16 | cmt_list_node (const T& t) : m_left(0), m_t(t), m_right(0) |
---|
| 17 | { |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | ~cmt_list_node () |
---|
| 21 | { |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | cmt_list_node<T>* m_left; |
---|
| 25 | T m_t; |
---|
| 26 | cmt_list_node<T>* m_right; |
---|
| 27 | }; |
---|
| 28 | |
---|
| 29 | template <class T> |
---|
| 30 | class cmt_list |
---|
| 31 | { |
---|
| 32 | public: |
---|
| 33 | typedef cmt_list_node<T> NodeV; |
---|
| 34 | typedef NodeV* Node; |
---|
| 35 | |
---|
| 36 | class iterator |
---|
| 37 | { |
---|
| 38 | public: |
---|
| 39 | iterator () : m_node (0) |
---|
| 40 | { |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | iterator (Node node) : m_node (node) |
---|
| 44 | { |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | iterator& operator ++ () |
---|
| 48 | { |
---|
| 49 | if (m_node == 0) return (*this); |
---|
| 50 | m_node = m_node->m_right; |
---|
| 51 | return (*this); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | iterator& operator -- () |
---|
| 55 | { |
---|
| 56 | if (m_node == 0) return (*this); |
---|
| 57 | m_node = m_node->m_left; |
---|
| 58 | return (*this); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | int operator == (const iterator& other) const |
---|
| 62 | { |
---|
| 63 | return (m_node == other.m_node); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | int operator != (const iterator& other) const |
---|
| 67 | { |
---|
| 68 | const iterator& me = *this; |
---|
| 69 | return (! (me == other)); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | T& operator * () const |
---|
| 73 | { |
---|
| 74 | if (m_node == 0) |
---|
| 75 | { |
---|
| 76 | static T t; |
---|
| 77 | |
---|
| 78 | return (t); |
---|
| 79 | } |
---|
| 80 | else |
---|
| 81 | { |
---|
| 82 | return (m_node->m_t); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | Node get_node () const |
---|
| 87 | { |
---|
| 88 | return (m_node); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | private: |
---|
| 92 | Node m_node; |
---|
| 93 | }; |
---|
| 94 | |
---|
| 95 | class const_iterator |
---|
| 96 | { |
---|
| 97 | public: |
---|
| 98 | const_iterator () : m_node (0) |
---|
| 99 | { |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | const_iterator (const Node node) : m_node (node) |
---|
| 103 | { |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | const_iterator& operator ++ () |
---|
| 107 | { |
---|
| 108 | if (m_node == 0) return (*this); |
---|
| 109 | m_node = m_node->m_right; |
---|
| 110 | return (*this); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | const_iterator& operator -- () |
---|
| 114 | { |
---|
| 115 | if (m_node == 0) return (*this); |
---|
| 116 | m_node = m_node->m_left; |
---|
| 117 | return (*this); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | int operator == (const const_iterator& other) const |
---|
| 121 | { |
---|
| 122 | return (m_node == other.m_node); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | int operator != (const const_iterator& other) const |
---|
| 126 | { |
---|
| 127 | const iterator& me = *this; |
---|
| 128 | return (! (me == other)); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | const T& operator * () const |
---|
| 132 | { |
---|
| 133 | if (m_node == 0) |
---|
| 134 | { |
---|
| 135 | static const T t; |
---|
| 136 | return (t); |
---|
| 137 | } |
---|
| 138 | else |
---|
| 139 | { |
---|
| 140 | return (m_node->m_t); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | const Node get_node () const |
---|
| 145 | { |
---|
| 146 | return (m_node); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | private: |
---|
| 150 | const Node m_node; |
---|
| 151 | }; |
---|
| 152 | |
---|
| 153 | cmt_list () |
---|
| 154 | { |
---|
| 155 | m_first = 0; |
---|
| 156 | m_last = 0; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | ~cmt_list () |
---|
| 160 | { |
---|
| 161 | while (m_last != 0) |
---|
| 162 | { |
---|
| 163 | remove (m_last); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | void push_front (const T& t) |
---|
| 168 | { |
---|
| 169 | Node node = new NodeV (t); |
---|
| 170 | |
---|
| 171 | if (m_first == 0) |
---|
| 172 | { |
---|
| 173 | m_last = node; |
---|
| 174 | } |
---|
| 175 | else |
---|
| 176 | { |
---|
| 177 | m_first->m_left = node; |
---|
| 178 | node->m_right = m_first; |
---|
| 179 | } |
---|
| 180 | m_first = node; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | void push_back (const T& t) |
---|
| 184 | { |
---|
| 185 | Node node = new NodeV (t); |
---|
| 186 | |
---|
| 187 | if (m_last == 0) |
---|
| 188 | { |
---|
| 189 | m_first = node; |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
| 193 | m_last->m_right = node; |
---|
| 194 | node->m_left = m_last; |
---|
| 195 | } |
---|
| 196 | m_last = node; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | void pop_front () |
---|
| 200 | { |
---|
| 201 | remove (m_first); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | void pop_back () |
---|
| 205 | { |
---|
| 206 | remove (m_last); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | void erase (iterator it) |
---|
| 210 | { |
---|
| 211 | remove (it.get_node ()); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | void erase (const_iterator it) |
---|
| 215 | { |
---|
| 216 | remove (it.get_node ()); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | int size () |
---|
| 220 | { |
---|
| 221 | int count = 0; |
---|
| 222 | Node node = m_first; |
---|
| 223 | while (node != 0) |
---|
| 224 | { |
---|
| 225 | node = node->m_right; |
---|
| 226 | count++; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | return (count); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | iterator begin () |
---|
| 233 | { |
---|
| 234 | return (iterator (m_first)); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | iterator last () |
---|
| 238 | { |
---|
| 239 | return (iterator (m_last)); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | iterator end () |
---|
| 243 | { |
---|
| 244 | iterator it; |
---|
| 245 | return (it); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | const_iterator begin () const |
---|
| 249 | { |
---|
| 250 | return (const_iterator (m_first)); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | const_iterator last () const |
---|
| 254 | { |
---|
| 255 | return (const_iterator (m_last)); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | const_iterator end () const |
---|
| 259 | { |
---|
| 260 | const_iterator it; |
---|
| 261 | return (it); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | void exchange (iterator it1, iterator it2) |
---|
| 265 | { |
---|
| 266 | if (it1 == it2) return; |
---|
| 267 | |
---|
| 268 | Node node1 = it1.get_node (); |
---|
| 269 | if (node1 == 0) return; |
---|
| 270 | Node node2 = it2.get_node (); |
---|
| 271 | if (node2 == 0) return; |
---|
| 272 | |
---|
| 273 | Node l1 = node1->m_left; |
---|
| 274 | Node r1 = node1->m_right; |
---|
| 275 | Node l2 = node2->m_left; |
---|
| 276 | Node r2 = node2->m_right; |
---|
| 277 | |
---|
| 278 | Node f = m_first; |
---|
| 279 | Node l = m_last; |
---|
| 280 | |
---|
| 281 | if ((l2 != 0) && (l2 != node1)) l2->m_right = node1; |
---|
| 282 | if ((r2 != 0) && (r2 != node1)) r2->m_left = node1; |
---|
| 283 | if ((l1 != 0) && (l1 != node2)) r1->m_right = node2; |
---|
| 284 | if ((r1 != 0) && (r1 != node2)) r1->m_left = node2; |
---|
| 285 | |
---|
| 286 | node1->m_left = l2; |
---|
| 287 | node1->m_right = r2; |
---|
| 288 | node2->m_left = l1; |
---|
| 289 | node2->m_right = r1; |
---|
| 290 | |
---|
| 291 | if (node1 == f) |
---|
| 292 | { |
---|
| 293 | m_first = node2; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | if (node1 == l) |
---|
| 297 | { |
---|
| 298 | m_last = node2; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | if (node2 == f) |
---|
| 302 | { |
---|
| 303 | m_first = node1; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | if (node2 == l) |
---|
| 307 | { |
---|
| 308 | m_last = node1; |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | void insert_before (iterator it, const T& t) |
---|
| 313 | { |
---|
| 314 | Node node = it.get_node (); |
---|
| 315 | |
---|
| 316 | if (node == 0) |
---|
| 317 | { |
---|
| 318 | push_front (t); |
---|
| 319 | } |
---|
| 320 | else |
---|
| 321 | { |
---|
| 322 | Node new_node = new NodeV (t); |
---|
| 323 | |
---|
| 324 | new_node->m_right = node; |
---|
| 325 | new_node->m_left = node->m_left; |
---|
| 326 | |
---|
| 327 | if (node->m_left != 0) |
---|
| 328 | { |
---|
| 329 | node->m_left->m_right = new_node; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | node->m_left = new_node; |
---|
| 333 | |
---|
| 334 | if (node == m_first) |
---|
| 335 | { |
---|
| 336 | m_first = new_node; |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | void insert_after (iterator it, const T& t) |
---|
| 342 | { |
---|
| 343 | Node node = it.get_node (); |
---|
| 344 | |
---|
| 345 | if (node == 0) |
---|
| 346 | { |
---|
| 347 | push_back (t); |
---|
| 348 | } |
---|
| 349 | else |
---|
| 350 | { |
---|
| 351 | Node new_node = new NodeV (t); |
---|
| 352 | |
---|
| 353 | new_node->m_left = node; |
---|
| 354 | new_node->m_right = node->m_right; |
---|
| 355 | |
---|
| 356 | if (node->m_right != 0) |
---|
| 357 | { |
---|
| 358 | node->m_right->m_left = new_node; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | node->m_right = new_node; |
---|
| 362 | |
---|
| 363 | if (node == m_last) |
---|
| 364 | { |
---|
| 365 | m_last = new_node; |
---|
| 366 | } |
---|
| 367 | } |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | void move_to_front (iterator source_it) |
---|
| 371 | { |
---|
| 372 | move_before (source_it, begin ()); |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | void move_to_back (iterator source_it) |
---|
| 376 | { |
---|
| 377 | move_before (source_it, end ()); |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | void move_before (iterator source_it, iterator dest_it) |
---|
| 381 | { |
---|
| 382 | if (source_it == dest_it) return; |
---|
| 383 | |
---|
| 384 | Node source = source_it.get_node (); |
---|
| 385 | if (source == 0) return; |
---|
| 386 | |
---|
| 387 | Node dest = dest_it.get_node (); |
---|
| 388 | |
---|
| 389 | // First remove the source |
---|
| 390 | |
---|
| 391 | if (source == m_first) |
---|
| 392 | { |
---|
| 393 | m_first = source->m_right; |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | if (source == m_last) |
---|
| 397 | { |
---|
| 398 | m_last = source->m_left; |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | if (source->m_right != 0) source->m_right->m_left = source->m_left; |
---|
| 402 | if (source->m_left != 0) source->m_left->m_right = source->m_right; |
---|
| 403 | |
---|
| 404 | source->m_right = 0; |
---|
| 405 | source->m_left = 0; |
---|
| 406 | |
---|
| 407 | if (dest == 0) |
---|
| 408 | { |
---|
| 409 | // = move_to_back |
---|
| 410 | |
---|
| 411 | source->m_right = 0; |
---|
| 412 | source->m_left = m_last; |
---|
| 413 | |
---|
| 414 | if (m_last != 0) |
---|
| 415 | { |
---|
| 416 | m_last->m_right = source; |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | m_last = source; |
---|
| 420 | |
---|
| 421 | if (m_first == 0) m_first = source; |
---|
| 422 | } |
---|
| 423 | else |
---|
| 424 | { |
---|
| 425 | source->m_right = dest; |
---|
| 426 | source->m_left = dest->m_left; |
---|
| 427 | |
---|
| 428 | if (dest->m_left != 0) |
---|
| 429 | { |
---|
| 430 | dest->m_left->m_right = source; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | dest->m_left = source; |
---|
| 434 | |
---|
| 435 | if (dest == m_first) |
---|
| 436 | { |
---|
| 437 | m_first = source; |
---|
| 438 | } |
---|
| 439 | } |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | void move_after (iterator source_it, iterator dest_it) |
---|
| 443 | { |
---|
| 444 | if (source_it == dest_it) return; |
---|
| 445 | |
---|
| 446 | Node source = source_it.get_node (); |
---|
| 447 | if (source == 0) return; |
---|
| 448 | |
---|
| 449 | Node dest = dest_it.get_node (); |
---|
| 450 | |
---|
| 451 | // First remove the source |
---|
| 452 | |
---|
| 453 | if (source == m_first) |
---|
| 454 | { |
---|
| 455 | m_first = source->m_right; |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | if (source == m_last) |
---|
| 459 | { |
---|
| 460 | m_last = source->m_left; |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | if (source->m_right != 0) source->m_right->m_left = source->m_left; |
---|
| 464 | if (source->m_left != 0) source->m_left->m_right = source->m_right; |
---|
| 465 | |
---|
| 466 | source->m_right = 0; |
---|
| 467 | source->m_left = 0; |
---|
| 468 | |
---|
| 469 | if (dest == 0) |
---|
| 470 | { |
---|
| 471 | // = move_to_front |
---|
| 472 | |
---|
| 473 | source->m_left = 0; |
---|
| 474 | source->m_right = m_first; |
---|
| 475 | |
---|
| 476 | if (m_first != 0) |
---|
| 477 | { |
---|
| 478 | m_first->m_left = source; |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | m_first = source; |
---|
| 482 | |
---|
| 483 | if (m_last == 0) m_last = source; |
---|
| 484 | } |
---|
| 485 | else |
---|
| 486 | { |
---|
| 487 | source->m_left = dest; |
---|
| 488 | source->m_right = dest->m_right; |
---|
| 489 | |
---|
| 490 | if (dest->m_right != 0) |
---|
| 491 | { |
---|
| 492 | dest->m_right->m_left = source; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | dest->m_right = source; |
---|
| 496 | |
---|
| 497 | if (dest == m_last) |
---|
| 498 | { |
---|
| 499 | m_last = source; |
---|
| 500 | } |
---|
| 501 | } |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | private: |
---|
| 505 | void remove (Node node) |
---|
| 506 | { |
---|
| 507 | if (node == 0) return; |
---|
| 508 | |
---|
| 509 | if (node == m_first) |
---|
| 510 | { |
---|
| 511 | m_first = node->m_right; |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | if (node == m_last) |
---|
| 515 | { |
---|
| 516 | m_last = node->m_left; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | if (node->m_right != 0) node->m_right->m_left = node->m_left; |
---|
| 520 | if (node->m_left != 0) node->m_left->m_right = node->m_right; |
---|
| 521 | |
---|
| 522 | node->m_right = 0; |
---|
| 523 | node->m_left = 0; |
---|
| 524 | |
---|
| 525 | delete node; |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | Node m_first; |
---|
| 529 | Node m_last; |
---|
| 530 | }; |
---|
| 531 | |
---|
| 532 | class A |
---|
| 533 | { |
---|
| 534 | public: |
---|
| 535 | A () |
---|
| 536 | { |
---|
| 537 | static int id = 0; |
---|
| 538 | i = id; |
---|
| 539 | id++; |
---|
| 540 | } |
---|
| 541 | |
---|
| 542 | ~A () |
---|
| 543 | { |
---|
| 544 | std::cout << "kill A i=" << i << std::endl; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | int i; |
---|
| 548 | }; |
---|
| 549 | |
---|
| 550 | #endif |
---|