| 1 | // file : xsd/cxx/tree/containers.hxx |
|---|
| 2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
|---|
| 3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
|---|
| 4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
|---|
| 5 | |
|---|
| 6 | #ifndef XSD_CXX_TREE_CONTAINERS_HXX |
|---|
| 7 | #define XSD_CXX_TREE_CONTAINERS_HXX |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | #include <cstddef> // std::ptrdiff_t |
|---|
| 11 | #include <string> |
|---|
| 12 | #include <vector> |
|---|
| 13 | #include <memory> // std::auto_ptr |
|---|
| 14 | #include <iterator> // std::iterator_traits |
|---|
| 15 | #include <algorithm> // std::equal, std::lexicographical_compare |
|---|
| 16 | #include <iosfwd> |
|---|
| 17 | |
|---|
| 18 | #include <xsd/cxx/tree/elements.hxx> |
|---|
| 19 | |
|---|
| 20 | namespace xsd |
|---|
| 21 | { |
|---|
| 22 | namespace cxx |
|---|
| 23 | { |
|---|
| 24 | namespace tree |
|---|
| 25 | { |
|---|
| 26 | // Test whether X is a fundamental C++ type. |
|---|
| 27 | // |
|---|
| 28 | |
|---|
| 29 | template <typename X> |
|---|
| 30 | struct fundamental_p |
|---|
| 31 | { |
|---|
| 32 | static const bool r = false; |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | // byte |
|---|
| 36 | // |
|---|
| 37 | template <> |
|---|
| 38 | struct fundamental_p<signed char> |
|---|
| 39 | { |
|---|
| 40 | static const bool r = true; |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | template <> |
|---|
| 44 | struct fundamental_p<unsigned char> |
|---|
| 45 | { |
|---|
| 46 | static const bool r = true; |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | // short |
|---|
| 50 | // |
|---|
| 51 | template <> |
|---|
| 52 | struct fundamental_p<short> |
|---|
| 53 | { |
|---|
| 54 | static const bool r = true; |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | template <> |
|---|
| 58 | struct fundamental_p<unsigned short> |
|---|
| 59 | { |
|---|
| 60 | static const bool r = true; |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | // int |
|---|
| 64 | // |
|---|
| 65 | template <> |
|---|
| 66 | struct fundamental_p<int> |
|---|
| 67 | { |
|---|
| 68 | static const bool r = true; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | template <> |
|---|
| 72 | struct fundamental_p<unsigned int> |
|---|
| 73 | { |
|---|
| 74 | static const bool r = true; |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | // long |
|---|
| 78 | // |
|---|
| 79 | template <> |
|---|
| 80 | struct fundamental_p<long> |
|---|
| 81 | { |
|---|
| 82 | static const bool r = true; |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | template <> |
|---|
| 86 | struct fundamental_p<unsigned long> |
|---|
| 87 | { |
|---|
| 88 | static const bool r = true; |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | template <> |
|---|
| 92 | struct fundamental_p<long long> |
|---|
| 93 | { |
|---|
| 94 | static const bool r = true; |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | template <> |
|---|
| 98 | struct fundamental_p<unsigned long long> |
|---|
| 99 | { |
|---|
| 100 | static const bool r = true; |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | // bool |
|---|
| 104 | // |
|---|
| 105 | template <> |
|---|
| 106 | struct fundamental_p<bool> |
|---|
| 107 | { |
|---|
| 108 | static const bool r = true; |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | // float |
|---|
| 112 | // |
|---|
| 113 | template <> |
|---|
| 114 | struct fundamental_p<float> |
|---|
| 115 | { |
|---|
| 116 | static const bool r = true; |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | template <> |
|---|
| 120 | struct fundamental_p<double> |
|---|
| 121 | { |
|---|
| 122 | static const bool r = true; |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | // one (for internal use only) |
|---|
| 126 | // |
|---|
| 127 | template <typename X, bool fund = fundamental_p<X>::r> |
|---|
| 128 | class one; |
|---|
| 129 | |
|---|
| 130 | template <typename X> |
|---|
| 131 | class one<X, false> |
|---|
| 132 | { |
|---|
| 133 | public: |
|---|
| 134 | ~one (); |
|---|
| 135 | |
|---|
| 136 | one (flags, container*); |
|---|
| 137 | |
|---|
| 138 | one (const X&, flags, container*); |
|---|
| 139 | |
|---|
| 140 | one (std::auto_ptr<X>, flags, container*); |
|---|
| 141 | |
|---|
| 142 | one (const one&, flags, container*); |
|---|
| 143 | |
|---|
| 144 | one& |
|---|
| 145 | operator= (const one&); |
|---|
| 146 | |
|---|
| 147 | public: |
|---|
| 148 | const X& |
|---|
| 149 | get () const |
|---|
| 150 | { |
|---|
| 151 | return *x_; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | X& |
|---|
| 155 | get () |
|---|
| 156 | { |
|---|
| 157 | return *x_; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | void |
|---|
| 161 | set (const X&); |
|---|
| 162 | |
|---|
| 163 | void |
|---|
| 164 | set (std::auto_ptr<X>); |
|---|
| 165 | |
|---|
| 166 | bool |
|---|
| 167 | present () const |
|---|
| 168 | { |
|---|
| 169 | return x_ != 0; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | protected: |
|---|
| 173 | X* x_; |
|---|
| 174 | flags flags_; |
|---|
| 175 | container* container_; |
|---|
| 176 | }; |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | template <typename X> |
|---|
| 180 | class one<X, true> |
|---|
| 181 | { |
|---|
| 182 | public: |
|---|
| 183 | one (flags, container*) |
|---|
| 184 | : present_ (false) |
|---|
| 185 | { |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | one (const X& x, flags, container*) |
|---|
| 189 | : x_ (x), present_ (true) |
|---|
| 190 | { |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | one (const one& x, flags, container*) |
|---|
| 194 | : x_ (x.x_), present_ (x.present_) |
|---|
| 195 | { |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | one& |
|---|
| 199 | operator= (const one& x) |
|---|
| 200 | { |
|---|
| 201 | if (this == &x) |
|---|
| 202 | return *this; |
|---|
| 203 | |
|---|
| 204 | x_ = x.x_; |
|---|
| 205 | present_ = x.present_; |
|---|
| 206 | |
|---|
| 207 | return *this; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | public: |
|---|
| 211 | const X& |
|---|
| 212 | get () const |
|---|
| 213 | { |
|---|
| 214 | return x_; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | X& |
|---|
| 218 | get () |
|---|
| 219 | { |
|---|
| 220 | return x_; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | void |
|---|
| 224 | set (const X& x) |
|---|
| 225 | { |
|---|
| 226 | x_ = x; |
|---|
| 227 | present_ = true; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | bool |
|---|
| 231 | present () const |
|---|
| 232 | { |
|---|
| 233 | return present_; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | protected: |
|---|
| 237 | X x_; |
|---|
| 238 | bool present_; |
|---|
| 239 | }; |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | // Note that I cannot get rid of fund because of HP aCC3. |
|---|
| 243 | // |
|---|
| 244 | template <typename X, bool fund = fundamental_p<X>::r> |
|---|
| 245 | class optional; |
|---|
| 246 | |
|---|
| 247 | template <typename X> |
|---|
| 248 | class optional<X, false> |
|---|
| 249 | { |
|---|
| 250 | public: |
|---|
| 251 | ~optional (); |
|---|
| 252 | |
|---|
| 253 | explicit |
|---|
| 254 | optional (flags = 0, container* = 0); |
|---|
| 255 | |
|---|
| 256 | explicit |
|---|
| 257 | optional (const X&, flags = 0, container* = 0); |
|---|
| 258 | |
|---|
| 259 | explicit |
|---|
| 260 | optional (std::auto_ptr<X>, flags = 0, container* = 0); |
|---|
| 261 | |
|---|
| 262 | optional (const optional&, flags = 0, container* = 0); |
|---|
| 263 | |
|---|
| 264 | optional& |
|---|
| 265 | operator= (const X&); |
|---|
| 266 | |
|---|
| 267 | optional& |
|---|
| 268 | operator= (const optional&); |
|---|
| 269 | |
|---|
| 270 | // Pointer-like interface. |
|---|
| 271 | // |
|---|
| 272 | public: |
|---|
| 273 | const X* |
|---|
| 274 | operator-> () const |
|---|
| 275 | { |
|---|
| 276 | return x_; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | X* |
|---|
| 280 | operator-> () |
|---|
| 281 | { |
|---|
| 282 | return x_; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | const X& |
|---|
| 286 | operator* () const |
|---|
| 287 | { |
|---|
| 288 | return *x_; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | X& |
|---|
| 292 | operator* () |
|---|
| 293 | { |
|---|
| 294 | return *x_; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | typedef optional self_; // Simplifier for Sun C++ 5.7. |
|---|
| 298 | typedef void (self_::*bool_convertible) (); |
|---|
| 299 | |
|---|
| 300 | operator bool_convertible () const |
|---|
| 301 | { |
|---|
| 302 | return x_ != 0 ? &self_::true_ : 0; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | // Get/set interface. |
|---|
| 306 | // |
|---|
| 307 | public: |
|---|
| 308 | bool |
|---|
| 309 | present () const |
|---|
| 310 | { |
|---|
| 311 | return x_ != 0; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | const X& |
|---|
| 315 | get () const |
|---|
| 316 | { |
|---|
| 317 | return *x_; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | X& |
|---|
| 321 | get () |
|---|
| 322 | { |
|---|
| 323 | return *x_; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | void |
|---|
| 327 | set (const X&); |
|---|
| 328 | |
|---|
| 329 | void |
|---|
| 330 | set (std::auto_ptr<X>); |
|---|
| 331 | |
|---|
| 332 | void |
|---|
| 333 | reset (); |
|---|
| 334 | |
|---|
| 335 | private: |
|---|
| 336 | void |
|---|
| 337 | true_ (); |
|---|
| 338 | |
|---|
| 339 | private: |
|---|
| 340 | X* x_; |
|---|
| 341 | flags flags_; |
|---|
| 342 | container* container_; |
|---|
| 343 | }; |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | // |
|---|
| 347 | // |
|---|
| 348 | template <typename X> |
|---|
| 349 | class optional<X, true> |
|---|
| 350 | { |
|---|
| 351 | public: |
|---|
| 352 | explicit |
|---|
| 353 | optional (flags = 0, container* = 0) |
|---|
| 354 | : present_ (false) |
|---|
| 355 | { |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | explicit |
|---|
| 359 | optional (const X&, flags = 0, container* = 0); |
|---|
| 360 | |
|---|
| 361 | optional (const optional&, flags = 0, container* = 0); |
|---|
| 362 | |
|---|
| 363 | optional& |
|---|
| 364 | operator= (const X&); |
|---|
| 365 | |
|---|
| 366 | optional& |
|---|
| 367 | operator= (const optional&); |
|---|
| 368 | |
|---|
| 369 | // Pointer-like interface. |
|---|
| 370 | // |
|---|
| 371 | public: |
|---|
| 372 | const X* |
|---|
| 373 | operator-> () const |
|---|
| 374 | { |
|---|
| 375 | return &x_; |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | X* |
|---|
| 379 | operator-> () |
|---|
| 380 | { |
|---|
| 381 | return &x_; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | const X& |
|---|
| 385 | operator* () const |
|---|
| 386 | { |
|---|
| 387 | return get (); |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | X& |
|---|
| 391 | operator* () |
|---|
| 392 | { |
|---|
| 393 | return get (); |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | typedef optional self_; // Simplifier for Sun C++ 5.7. |
|---|
| 397 | typedef void (self_::*bool_convertible) (); |
|---|
| 398 | |
|---|
| 399 | operator bool_convertible () const |
|---|
| 400 | { |
|---|
| 401 | return present () ? &self_::true_ : 0; |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | // Get/set interface. |
|---|
| 405 | // |
|---|
| 406 | public: |
|---|
| 407 | bool |
|---|
| 408 | present () const |
|---|
| 409 | { |
|---|
| 410 | return present_; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | const X& |
|---|
| 414 | get () const |
|---|
| 415 | { |
|---|
| 416 | return x_; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | X& |
|---|
| 420 | get () |
|---|
| 421 | { |
|---|
| 422 | return x_; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | void |
|---|
| 426 | set (const X& y) |
|---|
| 427 | { |
|---|
| 428 | x_ = y; |
|---|
| 429 | present_ = true; |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | void |
|---|
| 433 | reset () |
|---|
| 434 | { |
|---|
| 435 | present_ = false; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | private: |
|---|
| 439 | void |
|---|
| 440 | true_ (); |
|---|
| 441 | |
|---|
| 442 | private: |
|---|
| 443 | bool present_; |
|---|
| 444 | X x_; |
|---|
| 445 | }; |
|---|
| 446 | |
|---|
| 447 | // Comparison operators. |
|---|
| 448 | // |
|---|
| 449 | |
|---|
| 450 | template <typename X, bool fund> |
|---|
| 451 | inline bool |
|---|
| 452 | operator== (const optional<X, fund>& a, const optional<X, fund>& b) |
|---|
| 453 | { |
|---|
| 454 | return !a || !b ? a.present () == b.present () : *a == *b; |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | template <typename X, bool fund> |
|---|
| 458 | inline bool |
|---|
| 459 | operator!= (const optional<X, fund>& a, const optional<X, fund>& b) |
|---|
| 460 | { |
|---|
| 461 | return !(a == b); |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | template <typename X, bool fund> |
|---|
| 465 | inline bool |
|---|
| 466 | operator< (const optional<X, fund>& a, const optional<X, fund>& b) |
|---|
| 467 | { |
|---|
| 468 | return a && (!b || *a < *b); |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | template <typename X, bool fund> |
|---|
| 472 | inline bool |
|---|
| 473 | operator> (const optional<X, fund>& a, const optional<X, fund>& b) |
|---|
| 474 | { |
|---|
| 475 | return b < a; |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | template <typename X, bool fund> |
|---|
| 479 | inline bool |
|---|
| 480 | operator<= (const optional<X, fund>& a, const optional<X, fund>& b) |
|---|
| 481 | { |
|---|
| 482 | return !(a > b); |
|---|
| 483 | } |
|---|
| 484 | |
|---|
| 485 | template <typename X, bool fund> |
|---|
| 486 | inline bool |
|---|
| 487 | operator>= (const optional<X, fund>& a, const optional<X, fund>& b) |
|---|
| 488 | { |
|---|
| 489 | return !(a < b); |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | // Provide an ostream insertion opretaor to prevent confusion from |
|---|
| 493 | // the implicit bool conversion. |
|---|
| 494 | // |
|---|
| 495 | template <typename C, typename X, bool fund> |
|---|
| 496 | std::basic_ostream<C>& |
|---|
| 497 | operator<< (std::basic_ostream<C>&, const optional<X, fund>&); |
|---|
| 498 | |
|---|
| 499 | |
|---|
| 500 | // Sequence. |
|---|
| 501 | // |
|---|
| 502 | |
|---|
| 503 | // Note that I cannot get rid of 'fund' because HP aCC3 likes it |
|---|
| 504 | // this way. |
|---|
| 505 | // |
|---|
| 506 | template <typename X, bool fund = fundamental_p<X>::r> |
|---|
| 507 | class sequence; |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | // Sun CC's <iterator> does not have iterator_traits. To overcome |
|---|
| 511 | // this, we will wrap std::iterator_traits into our own and also |
|---|
| 512 | // specialize it for pointer types. Since Sun CC uses pointer |
|---|
| 513 | // for vector::iterator, it will use the specialization and won't |
|---|
| 514 | // notice the std::iterator_traits. |
|---|
| 515 | // |
|---|
| 516 | #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC |
|---|
| 517 | template <typename I> |
|---|
| 518 | struct iterator_traits |
|---|
| 519 | { |
|---|
| 520 | typedef |
|---|
| 521 | typename std::iterator_traits<I>::iterator_category |
|---|
| 522 | iterator_category; |
|---|
| 523 | |
|---|
| 524 | typedef |
|---|
| 525 | typename std::iterator_traits<I>::value_type |
|---|
| 526 | value_type; |
|---|
| 527 | |
|---|
| 528 | typedef |
|---|
| 529 | typename std::iterator_traits<I>::difference_type |
|---|
| 530 | difference_type; |
|---|
| 531 | }; |
|---|
| 532 | #else |
|---|
| 533 | // The Pointer specialization does not work for reverse and |
|---|
| 534 | // set iterators. But these iterators are user-dfined types |
|---|
| 535 | // and have suitable typedefs that we can use. |
|---|
| 536 | // |
|---|
| 537 | template <typename I> |
|---|
| 538 | struct iterator_traits |
|---|
| 539 | { |
|---|
| 540 | typedef typename I::iterator_category iterator_category; |
|---|
| 541 | typedef typename I::value_type value_type; |
|---|
| 542 | typedef typename I::difference_type difference_type; |
|---|
| 543 | }; |
|---|
| 544 | |
|---|
| 545 | template <typename X> |
|---|
| 546 | struct iterator_traits<X*> |
|---|
| 547 | { |
|---|
| 548 | typedef std::random_access_iterator_tag iterator_category; |
|---|
| 549 | typedef X value_type; |
|---|
| 550 | typedef std::ptrdiff_t difference_type; |
|---|
| 551 | }; |
|---|
| 552 | #endif |
|---|
| 553 | |
|---|
| 554 | // Iterator adapter for complex types. It expects I to point to |
|---|
| 555 | // a smart pointer-like object that has operator*() that returns |
|---|
| 556 | // a refernce to a type static_cast'able to X and get() that |
|---|
| 557 | // returns a pointer to a type static_cast'able to X. |
|---|
| 558 | // |
|---|
| 559 | |
|---|
| 560 | template <typename I, typename X> |
|---|
| 561 | struct iterator_adapter |
|---|
| 562 | { |
|---|
| 563 | typedef X value_type; |
|---|
| 564 | typedef value_type& reference; |
|---|
| 565 | typedef value_type* pointer; |
|---|
| 566 | |
|---|
| 567 | typedef |
|---|
| 568 | typename iterator_traits<I>::iterator_category |
|---|
| 569 | iterator_category; |
|---|
| 570 | |
|---|
| 571 | typedef |
|---|
| 572 | typename iterator_traits<I>::difference_type |
|---|
| 573 | difference_type; |
|---|
| 574 | |
|---|
| 575 | |
|---|
| 576 | public: |
|---|
| 577 | iterator_adapter () |
|---|
| 578 | : i_ () // i_ can be of a pointer type. |
|---|
| 579 | { |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | // Allow iterator to const_iterator conversion. |
|---|
| 583 | // |
|---|
| 584 | template <typename J, typename Y> |
|---|
| 585 | iterator_adapter (const iterator_adapter<J, Y>& j) |
|---|
| 586 | : i_ (j.base ()) |
|---|
| 587 | { |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | explicit |
|---|
| 591 | iterator_adapter (const I& i) |
|---|
| 592 | : i_ (i) |
|---|
| 593 | { |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | public: |
|---|
| 597 | // Forward iterator requirements. |
|---|
| 598 | // |
|---|
| 599 | reference |
|---|
| 600 | operator* () const |
|---|
| 601 | { |
|---|
| 602 | return static_cast<reference> (**i_); |
|---|
| 603 | } |
|---|
| 604 | |
|---|
| 605 | pointer |
|---|
| 606 | operator-> () const |
|---|
| 607 | { |
|---|
| 608 | return static_cast<pointer> (i_->get ()); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | iterator_adapter& |
|---|
| 612 | operator++ () |
|---|
| 613 | { |
|---|
| 614 | ++i_; |
|---|
| 615 | return *this; |
|---|
| 616 | } |
|---|
| 617 | |
|---|
| 618 | iterator_adapter |
|---|
| 619 | operator++ (int) |
|---|
| 620 | { |
|---|
| 621 | iterator_adapter r (*this); |
|---|
| 622 | ++i_; |
|---|
| 623 | return r; |
|---|
| 624 | } |
|---|
| 625 | |
|---|
| 626 | // Bidirectional iterator requirements. |
|---|
| 627 | // |
|---|
| 628 | iterator_adapter& |
|---|
| 629 | operator-- () |
|---|
| 630 | { |
|---|
| 631 | --i_; |
|---|
| 632 | return *this; |
|---|
| 633 | } |
|---|
| 634 | |
|---|
| 635 | iterator_adapter |
|---|
| 636 | operator-- (int) |
|---|
| 637 | { |
|---|
| 638 | iterator_adapter r (*this); |
|---|
| 639 | --i_; |
|---|
| 640 | return r; |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | // Random access iterator requirements. |
|---|
| 644 | // |
|---|
| 645 | reference |
|---|
| 646 | operator[] (difference_type n) const |
|---|
| 647 | { |
|---|
| 648 | return static_cast<reference> (*(i_[n])); |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | iterator_adapter& |
|---|
| 652 | operator+= (difference_type n) |
|---|
| 653 | { |
|---|
| 654 | i_ += n; |
|---|
| 655 | return *this; |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | iterator_adapter |
|---|
| 659 | operator+ (difference_type n) const |
|---|
| 660 | { |
|---|
| 661 | return iterator_adapter (i_ + n); |
|---|
| 662 | } |
|---|
| 663 | |
|---|
| 664 | iterator_adapter& |
|---|
| 665 | operator-= (difference_type n) |
|---|
| 666 | { |
|---|
| 667 | i_ -= n; |
|---|
| 668 | return *this; |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | iterator_adapter |
|---|
| 672 | operator- (difference_type n) const |
|---|
| 673 | { |
|---|
| 674 | return iterator_adapter (i_ - n); |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | public: |
|---|
| 678 | const I& |
|---|
| 679 | base () const |
|---|
| 680 | { |
|---|
| 681 | return i_; |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | private: |
|---|
| 685 | I i_; |
|---|
| 686 | }; |
|---|
| 687 | |
|---|
| 688 | // Note: We use different types for left- and right-hand-side |
|---|
| 689 | // arguments to allow comparison between iterator and const_iterator. |
|---|
| 690 | // |
|---|
| 691 | |
|---|
| 692 | // Forward iterator requirements. |
|---|
| 693 | // |
|---|
| 694 | template <typename I, typename J, typename X, typename Y> |
|---|
| 695 | inline bool |
|---|
| 696 | operator== (const iterator_adapter<I, X>& i, |
|---|
| 697 | const iterator_adapter<J, Y>& j) |
|---|
| 698 | { |
|---|
| 699 | return i.base () == j.base (); |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | template <typename I, typename J, typename X, typename Y> |
|---|
| 703 | inline bool |
|---|
| 704 | operator!= (const iterator_adapter<I, X>& i, |
|---|
| 705 | const iterator_adapter<J, Y>& j) |
|---|
| 706 | { |
|---|
| 707 | return i.base () != j.base (); |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | // Random access iterator requirements |
|---|
| 711 | // |
|---|
| 712 | template <typename I, typename J, typename X, typename Y> |
|---|
| 713 | inline bool |
|---|
| 714 | operator< (const iterator_adapter<I, X>& i, |
|---|
| 715 | const iterator_adapter<J, Y>& j) |
|---|
| 716 | { |
|---|
| 717 | return i.base() < j.base(); |
|---|
| 718 | } |
|---|
| 719 | |
|---|
| 720 | template <typename I, typename J, typename X, typename Y> |
|---|
| 721 | inline bool |
|---|
| 722 | operator> (const iterator_adapter<I, X>& i, |
|---|
| 723 | const iterator_adapter<J, Y>& j) |
|---|
| 724 | { |
|---|
| 725 | return i.base() > j.base(); |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | template <typename I, typename J, typename X, typename Y> |
|---|
| 729 | inline bool |
|---|
| 730 | operator<= (const iterator_adapter<I, X>& i, |
|---|
| 731 | const iterator_adapter<J, Y>& j) |
|---|
| 732 | { |
|---|
| 733 | return i.base() <= j.base(); |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | template <typename I, typename J, typename X, typename Y> |
|---|
| 737 | inline bool |
|---|
| 738 | operator>= (const iterator_adapter<I, X>& i, |
|---|
| 739 | const iterator_adapter<J, Y>& j) |
|---|
| 740 | { |
|---|
| 741 | return i.base() >= j.base(); |
|---|
| 742 | } |
|---|
| 743 | |
|---|
| 744 | template <typename I, typename J, typename X, typename Y> |
|---|
| 745 | inline typename iterator_adapter<I, X>::difference_type |
|---|
| 746 | operator- (const iterator_adapter<I, X>& i, |
|---|
| 747 | const iterator_adapter<J, Y>& j) |
|---|
| 748 | { |
|---|
| 749 | return i.base () - j.base (); |
|---|
| 750 | } |
|---|
| 751 | |
|---|
| 752 | template <typename I, typename X> |
|---|
| 753 | inline iterator_adapter<I, X> |
|---|
| 754 | operator+ (typename iterator_adapter<I, X>::difference_type n, |
|---|
| 755 | const iterator_adapter<I, X>& i) |
|---|
| 756 | { |
|---|
| 757 | return iterator_adapter<I, X> (i.base() + n); |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | // |
|---|
| 761 | // |
|---|
| 762 | class sequence_common |
|---|
| 763 | { |
|---|
| 764 | protected: |
|---|
| 765 | // This is a dangerously destructive automatic pointer. We are going |
|---|
| 766 | // to use it in a controlled environment to save us a lot of coding. |
|---|
| 767 | // |
|---|
| 768 | struct ptr |
|---|
| 769 | { |
|---|
| 770 | ~ptr () |
|---|
| 771 | { |
|---|
| 772 | delete x_; |
|---|
| 773 | } |
|---|
| 774 | |
|---|
| 775 | explicit |
|---|
| 776 | ptr (type* x = 0) |
|---|
| 777 | : x_ (x) |
|---|
| 778 | { |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | ptr (const ptr& y) |
|---|
| 782 | : x_ (y.x_) |
|---|
| 783 | { |
|---|
| 784 | // Yes, hostile takeover. |
|---|
| 785 | // |
|---|
| 786 | y.x_ = 0; |
|---|
| 787 | } |
|---|
| 788 | |
|---|
| 789 | ptr& |
|---|
| 790 | operator= (const ptr& y) |
|---|
| 791 | { |
|---|
| 792 | if (this != &y) |
|---|
| 793 | { |
|---|
| 794 | // Yes, hostile takeover. |
|---|
| 795 | // |
|---|
| 796 | delete x_; |
|---|
| 797 | x_ = y.x_; |
|---|
| 798 | y.x_ = 0; |
|---|
| 799 | } |
|---|
| 800 | |
|---|
| 801 | return *this; |
|---|
| 802 | } |
|---|
| 803 | |
|---|
| 804 | public: |
|---|
| 805 | type& |
|---|
| 806 | operator* () const |
|---|
| 807 | { |
|---|
| 808 | return *x_; |
|---|
| 809 | } |
|---|
| 810 | |
|---|
| 811 | type* |
|---|
| 812 | get () const |
|---|
| 813 | { |
|---|
| 814 | return x_; |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | private: |
|---|
| 818 | mutable type* x_; |
|---|
| 819 | }; |
|---|
| 820 | |
|---|
| 821 | protected: |
|---|
| 822 | typedef std::vector<ptr> base_sequence; |
|---|
| 823 | typedef base_sequence::iterator base_iterator; |
|---|
| 824 | typedef base_sequence::const_iterator base_const_iterator; |
|---|
| 825 | |
|---|
| 826 | typedef base_sequence::size_type size_type; |
|---|
| 827 | typedef base_sequence::difference_type difference_type; |
|---|
| 828 | typedef base_sequence::allocator_type allocator_type; |
|---|
| 829 | |
|---|
| 830 | protected: |
|---|
| 831 | sequence_common (flags f, container* c) |
|---|
| 832 | : flags_ (f), container_ (c) |
|---|
| 833 | { |
|---|
| 834 | } |
|---|
| 835 | |
|---|
| 836 | sequence_common (size_type n, const type& x) |
|---|
| 837 | : flags_ (0), container_ (0) |
|---|
| 838 | { |
|---|
| 839 | assign (n, x); |
|---|
| 840 | } |
|---|
| 841 | |
|---|
| 842 | template <typename I> |
|---|
| 843 | sequence_common (const I& begin, const I& end) |
|---|
| 844 | : flags_ (0), container_ (0) |
|---|
| 845 | { |
|---|
| 846 | assign (begin, end); |
|---|
| 847 | } |
|---|
| 848 | |
|---|
| 849 | sequence_common (const sequence_common& v, flags f, container* c) |
|---|
| 850 | : flags_ (f), container_ (c) |
|---|
| 851 | { |
|---|
| 852 | v_.reserve (v.v_.size ()); |
|---|
| 853 | |
|---|
| 854 | for (base_const_iterator i (v.v_.begin ()), e (v.v_.end ()); |
|---|
| 855 | i != e; ++i) |
|---|
| 856 | { |
|---|
| 857 | ptr p ((**i)._clone (flags_, container_)); |
|---|
| 858 | v_.push_back (p); |
|---|
| 859 | } |
|---|
| 860 | } |
|---|
| 861 | |
|---|
| 862 | public: |
|---|
| 863 | sequence_common& |
|---|
| 864 | operator= (const sequence_common& v) |
|---|
| 865 | { |
|---|
| 866 | if (this == &v) |
|---|
| 867 | return *this; |
|---|
| 868 | |
|---|
| 869 | v_.assign (v.v_.size (), ptr ()); |
|---|
| 870 | |
|---|
| 871 | base_iterator di (v_.begin ()), de (v_.end ()); |
|---|
| 872 | base_const_iterator si (v.v_.begin ()), se (v.v_.end ()); |
|---|
| 873 | |
|---|
| 874 | for (; si != se && di != de; ++si, ++di) |
|---|
| 875 | { |
|---|
| 876 | // We have no ptr_ref. |
|---|
| 877 | // |
|---|
| 878 | ptr p ((**si)._clone (flags_, container_)); |
|---|
| 879 | *di = p; |
|---|
| 880 | } |
|---|
| 881 | |
|---|
| 882 | return *this; |
|---|
| 883 | } |
|---|
| 884 | |
|---|
| 885 | public: |
|---|
| 886 | size_type |
|---|
| 887 | size () const |
|---|
| 888 | { |
|---|
| 889 | return v_.size (); |
|---|
| 890 | } |
|---|
| 891 | |
|---|
| 892 | size_type |
|---|
| 893 | max_size () const |
|---|
| 894 | { |
|---|
| 895 | return v_.max_size (); |
|---|
| 896 | } |
|---|
| 897 | |
|---|
| 898 | size_type |
|---|
| 899 | capacity () const |
|---|
| 900 | { |
|---|
| 901 | return v_.capacity (); |
|---|
| 902 | } |
|---|
| 903 | |
|---|
| 904 | bool |
|---|
| 905 | empty () const |
|---|
| 906 | { |
|---|
| 907 | return v_.empty (); |
|---|
| 908 | } |
|---|
| 909 | |
|---|
| 910 | void |
|---|
| 911 | reserve (size_type n) |
|---|
| 912 | { |
|---|
| 913 | v_.reserve (n); |
|---|
| 914 | } |
|---|
| 915 | |
|---|
| 916 | void |
|---|
| 917 | clear () |
|---|
| 918 | { |
|---|
| 919 | v_.clear (); |
|---|
| 920 | } |
|---|
| 921 | |
|---|
| 922 | protected: |
|---|
| 923 | void |
|---|
| 924 | assign (size_type n, const type& x) |
|---|
| 925 | { |
|---|
| 926 | v_.assign (n, ptr ()); |
|---|
| 927 | |
|---|
| 928 | for (base_iterator i (v_.begin ()), e (v_.end ()); i != e; ++i) |
|---|
| 929 | { |
|---|
| 930 | ptr p (x._clone (flags_, container_)); |
|---|
| 931 | *i = p; |
|---|
| 932 | } |
|---|
| 933 | } |
|---|
| 934 | |
|---|
| 935 | template <typename I> |
|---|
| 936 | void |
|---|
| 937 | assign (const I& begin, const I& end) |
|---|
| 938 | { |
|---|
| 939 | // This is not the fastest way to do it. Also I's type may not |
|---|
| 940 | // have _clone. |
|---|
| 941 | // |
|---|
| 942 | v_.clear (); |
|---|
| 943 | |
|---|
| 944 | for (I i (begin); i != end; ++i) |
|---|
| 945 | { |
|---|
| 946 | ptr p (i->_clone (flags_, container_)); |
|---|
| 947 | v_.push_back (p); |
|---|
| 948 | } |
|---|
| 949 | } |
|---|
| 950 | |
|---|
| 951 | void |
|---|
| 952 | resize (size_type n, const type& x) |
|---|
| 953 | { |
|---|
| 954 | size_type old (v_.size ()); |
|---|
| 955 | v_.resize (n, ptr ()); |
|---|
| 956 | |
|---|
| 957 | if (old < n) |
|---|
| 958 | { |
|---|
| 959 | for (base_iterator i (v_.begin () + old), e (v_.end ()); |
|---|
| 960 | i != e; ++i) |
|---|
| 961 | { |
|---|
| 962 | ptr p (x._clone (flags_, container_)); |
|---|
| 963 | *i = p; |
|---|
| 964 | } |
|---|
| 965 | } |
|---|
| 966 | } |
|---|
| 967 | |
|---|
| 968 | void |
|---|
| 969 | insert (base_iterator p, size_type n, const type& x) |
|---|
| 970 | { |
|---|
| 971 | difference_type d (v_.end () - p); |
|---|
| 972 | v_.insert (p, n, ptr ()); |
|---|
| 973 | |
|---|
| 974 | for (base_iterator i (v_.end () - d); n != 0; --n) |
|---|
| 975 | { |
|---|
| 976 | ptr r (x._clone (flags_, container_)); |
|---|
| 977 | *(--i) = r; |
|---|
| 978 | } |
|---|
| 979 | } |
|---|
| 980 | |
|---|
| 981 | template <typename I> |
|---|
| 982 | void |
|---|
| 983 | insert (base_iterator p, const I& begin, const I& end) |
|---|
| 984 | { |
|---|
| 985 | // This is not the fastest way to do it. Also I's type may not |
|---|
| 986 | // have _clone. |
|---|
| 987 | // |
|---|
| 988 | if (begin != end) |
|---|
| 989 | { |
|---|
| 990 | for (I i (end);;) |
|---|
| 991 | { |
|---|
| 992 | --i; |
|---|
| 993 | ptr r (i->_clone (flags_, container_)); |
|---|
| 994 | p = v_.insert (p, r); |
|---|
| 995 | |
|---|
| 996 | if (i == begin) |
|---|
| 997 | break; |
|---|
| 998 | } |
|---|
| 999 | } |
|---|
| 1000 | } |
|---|
| 1001 | |
|---|
| 1002 | protected: |
|---|
| 1003 | flags flags_; |
|---|
| 1004 | container* container_; |
|---|
| 1005 | base_sequence v_; |
|---|
| 1006 | }; |
|---|
| 1007 | |
|---|
| 1008 | // |
|---|
| 1009 | // |
|---|
| 1010 | template <typename X> |
|---|
| 1011 | class sequence<X, false>: public sequence_common |
|---|
| 1012 | { |
|---|
| 1013 | protected: |
|---|
| 1014 | // For IBM XL C++ 8.0. |
|---|
| 1015 | // |
|---|
| 1016 | typedef sequence_common::ptr ptr; |
|---|
| 1017 | |
|---|
| 1018 | public: |
|---|
| 1019 | typedef X value_type; |
|---|
| 1020 | typedef X* pointer; |
|---|
| 1021 | typedef const X* const_pointer; |
|---|
| 1022 | typedef X& reference; |
|---|
| 1023 | typedef const X& const_reference; |
|---|
| 1024 | |
|---|
| 1025 | typedef |
|---|
| 1026 | iterator_adapter<base_sequence::iterator, X> |
|---|
| 1027 | iterator; |
|---|
| 1028 | |
|---|
| 1029 | typedef |
|---|
| 1030 | iterator_adapter<base_sequence::const_iterator, const X> |
|---|
| 1031 | const_iterator; |
|---|
| 1032 | |
|---|
| 1033 | typedef |
|---|
| 1034 | iterator_adapter<base_sequence::reverse_iterator, X> |
|---|
| 1035 | reverse_iterator; |
|---|
| 1036 | |
|---|
| 1037 | typedef |
|---|
| 1038 | iterator_adapter<base_sequence::const_reverse_iterator, const X> |
|---|
| 1039 | const_reverse_iterator; |
|---|
| 1040 | |
|---|
| 1041 | typedef sequence_common::size_type size_type; |
|---|
| 1042 | typedef sequence_common::difference_type difference_type; |
|---|
| 1043 | typedef sequence_common::allocator_type allocator_type; |
|---|
| 1044 | |
|---|
| 1045 | public: |
|---|
| 1046 | explicit |
|---|
| 1047 | sequence (flags f = 0, container* c = 0) |
|---|
| 1048 | : sequence_common (f, c) |
|---|
| 1049 | { |
|---|
| 1050 | } |
|---|
| 1051 | |
|---|
| 1052 | // The first version causes trouble on IBM XL C++ 7.0 when |
|---|
| 1053 | // a type does not have the default c-tor. While the second |
|---|
| 1054 | // breaks VC++ 8.0 when using dllexport (it appears to |
|---|
| 1055 | // instantiate everything instead of only what's used). |
|---|
| 1056 | // |
|---|
| 1057 | #ifdef _MSC_VER |
|---|
| 1058 | explicit |
|---|
| 1059 | sequence (size_type n, const X& x = X ()) |
|---|
| 1060 | : sequence_common (n, x) |
|---|
| 1061 | { |
|---|
| 1062 | } |
|---|
| 1063 | #else |
|---|
| 1064 | explicit |
|---|
| 1065 | sequence (size_type n) |
|---|
| 1066 | : sequence_common (n, X ()) |
|---|
| 1067 | { |
|---|
| 1068 | } |
|---|
| 1069 | |
|---|
| 1070 | sequence (size_type n, const X& x) |
|---|
| 1071 | : sequence_common (n, x) |
|---|
| 1072 | { |
|---|
| 1073 | } |
|---|
| 1074 | #endif |
|---|
| 1075 | |
|---|
| 1076 | template <typename I> |
|---|
| 1077 | sequence (const I& begin, const I& end) |
|---|
| 1078 | : sequence_common (begin, end) |
|---|
| 1079 | { |
|---|
| 1080 | } |
|---|
| 1081 | |
|---|
| 1082 | sequence (const sequence& v, flags f = 0, container* c = 0) |
|---|
| 1083 | : sequence_common (v, f, c) |
|---|
| 1084 | { |
|---|
| 1085 | } |
|---|
| 1086 | |
|---|
| 1087 | public: |
|---|
| 1088 | void |
|---|
| 1089 | assign (size_type n, const X& x) |
|---|
| 1090 | { |
|---|
| 1091 | sequence_common::assign (n, x); |
|---|
| 1092 | } |
|---|
| 1093 | |
|---|
| 1094 | template <typename I> |
|---|
| 1095 | void |
|---|
| 1096 | assign (const I& begin, const I& end) |
|---|
| 1097 | { |
|---|
| 1098 | sequence_common::assign (begin, end); |
|---|
| 1099 | } |
|---|
| 1100 | |
|---|
| 1101 | public: |
|---|
| 1102 | // The first version causes trouble on IBM XL C++ 7.0 when |
|---|
| 1103 | // a type does not have the default c-tor. While the second |
|---|
| 1104 | // breaks VC++ 8.0 when using dllexport (it appears to |
|---|
| 1105 | // instantiate everything instead of only what's used). |
|---|
| 1106 | // |
|---|
| 1107 | #ifdef _MSC_VER |
|---|
| 1108 | void |
|---|
| 1109 | resize (size_type n, const X& x = X ()) |
|---|
| 1110 | { |
|---|
| 1111 | sequence_common::resize (n, x); |
|---|
| 1112 | } |
|---|
| 1113 | #else |
|---|
| 1114 | void |
|---|
| 1115 | resize (size_type n) |
|---|
| 1116 | { |
|---|
| 1117 | sequence_common::resize (n, X ()); |
|---|
| 1118 | } |
|---|
| 1119 | |
|---|
| 1120 | void |
|---|
| 1121 | resize (size_type n, const X& x) |
|---|
| 1122 | { |
|---|
| 1123 | sequence_common::resize (n, x); |
|---|
| 1124 | } |
|---|
| 1125 | #endif |
|---|
| 1126 | |
|---|
| 1127 | public: |
|---|
| 1128 | const_iterator |
|---|
| 1129 | begin () const |
|---|
| 1130 | { |
|---|
| 1131 | return const_iterator (v_.begin ()); |
|---|
| 1132 | } |
|---|
| 1133 | |
|---|
| 1134 | const_iterator |
|---|
| 1135 | end () const |
|---|
| 1136 | { |
|---|
| 1137 | return const_iterator (v_.end ()); |
|---|
| 1138 | } |
|---|
| 1139 | |
|---|
| 1140 | iterator |
|---|
| 1141 | begin () |
|---|
| 1142 | { |
|---|
| 1143 | return iterator (v_.begin ()); |
|---|
| 1144 | } |
|---|
| 1145 | |
|---|
| 1146 | iterator |
|---|
| 1147 | end () |
|---|
| 1148 | { |
|---|
| 1149 | return iterator (v_.end ()); |
|---|
| 1150 | } |
|---|
| 1151 | |
|---|
| 1152 | // reverse |
|---|
| 1153 | // |
|---|
| 1154 | |
|---|
| 1155 | const_reverse_iterator |
|---|
| 1156 | rbegin () const |
|---|
| 1157 | { |
|---|
| 1158 | return const_reverse_iterator (v_.rbegin ()); |
|---|
| 1159 | } |
|---|
| 1160 | |
|---|
| 1161 | const_reverse_iterator |
|---|
| 1162 | rend () const |
|---|
| 1163 | { |
|---|
| 1164 | return const_reverse_iterator (v_.rend ()); |
|---|
| 1165 | } |
|---|
| 1166 | |
|---|
| 1167 | reverse_iterator |
|---|
| 1168 | rbegin () |
|---|
| 1169 | { |
|---|
| 1170 | return reverse_iterator (v_.rbegin ()); |
|---|
| 1171 | } |
|---|
| 1172 | |
|---|
| 1173 | reverse_iterator |
|---|
| 1174 | rend () |
|---|
| 1175 | { |
|---|
| 1176 | return reverse_iterator (v_.rend ()); |
|---|
| 1177 | } |
|---|
| 1178 | |
|---|
| 1179 | public: |
|---|
| 1180 | X& |
|---|
| 1181 | operator[] (size_type n) |
|---|
| 1182 | { |
|---|
| 1183 | return static_cast<X&> (*(v_[n])); |
|---|
| 1184 | } |
|---|
| 1185 | |
|---|
| 1186 | const X& |
|---|
| 1187 | operator[] (size_type n) const |
|---|
| 1188 | { |
|---|
| 1189 | return static_cast<const X&> (*(v_[n])); |
|---|
| 1190 | } |
|---|
| 1191 | |
|---|
| 1192 | X& |
|---|
| 1193 | at (size_type n) |
|---|
| 1194 | { |
|---|
| 1195 | return static_cast<X&> (*(v_.at (n))); |
|---|
| 1196 | } |
|---|
| 1197 | |
|---|
| 1198 | const X& |
|---|
| 1199 | at (size_type n) const |
|---|
| 1200 | { |
|---|
| 1201 | return static_cast<const X&> (*(v_.at (n))); |
|---|
| 1202 | } |
|---|
| 1203 | |
|---|
| 1204 | X& |
|---|
| 1205 | front () |
|---|
| 1206 | { |
|---|
| 1207 | return static_cast<X&> (*(v_.front ())); |
|---|
| 1208 | } |
|---|
| 1209 | |
|---|
| 1210 | const X& |
|---|
| 1211 | front () const |
|---|
| 1212 | { |
|---|
| 1213 | return static_cast<const X&> (*(v_.front ())); |
|---|
| 1214 | } |
|---|
| 1215 | |
|---|
| 1216 | X& |
|---|
| 1217 | back () |
|---|
| 1218 | { |
|---|
| 1219 | return static_cast<X&> (*(v_.back ())); |
|---|
| 1220 | } |
|---|
| 1221 | |
|---|
| 1222 | const X& |
|---|
| 1223 | back () const |
|---|
| 1224 | { |
|---|
| 1225 | return static_cast<const X&> (*(v_.back ())); |
|---|
| 1226 | } |
|---|
| 1227 | |
|---|
| 1228 | public: |
|---|
| 1229 | void |
|---|
| 1230 | push_back (const X& x) |
|---|
| 1231 | { |
|---|
| 1232 | v_.push_back (ptr (x._clone (flags_, container_))); |
|---|
| 1233 | } |
|---|
| 1234 | |
|---|
| 1235 | void |
|---|
| 1236 | push_back (std::auto_ptr<X> x) |
|---|
| 1237 | { |
|---|
| 1238 | if (x->_container () != container_) |
|---|
| 1239 | x->_container (container_); |
|---|
| 1240 | |
|---|
| 1241 | v_.push_back (ptr (x.release ())); |
|---|
| 1242 | } |
|---|
| 1243 | |
|---|
| 1244 | void |
|---|
| 1245 | pop_back () |
|---|
| 1246 | { |
|---|
| 1247 | v_.pop_back (); |
|---|
| 1248 | } |
|---|
| 1249 | |
|---|
| 1250 | iterator |
|---|
| 1251 | insert (iterator position, const X& x) |
|---|
| 1252 | { |
|---|
| 1253 | return iterator ( |
|---|
| 1254 | v_.insert ( |
|---|
| 1255 | position.base (), ptr (x._clone (flags_, container_)))); |
|---|
| 1256 | } |
|---|
| 1257 | |
|---|
| 1258 | iterator |
|---|
| 1259 | insert (iterator position, std::auto_ptr<X> x) |
|---|
| 1260 | { |
|---|
| 1261 | if (x->_container () != container_) |
|---|
| 1262 | x->_container (container_); |
|---|
| 1263 | |
|---|
| 1264 | return iterator (v_.insert (position.base (), ptr (x.release ()))); |
|---|
| 1265 | } |
|---|
| 1266 | |
|---|
| 1267 | void |
|---|
| 1268 | insert (iterator position, size_type n, const X& x) |
|---|
| 1269 | { |
|---|
| 1270 | sequence_common::insert (position.base (), n, x); |
|---|
| 1271 | } |
|---|
| 1272 | |
|---|
| 1273 | template <typename I> |
|---|
| 1274 | void |
|---|
| 1275 | insert (iterator position, const I& begin, const I& end) |
|---|
| 1276 | { |
|---|
| 1277 | sequence_common::insert (position.base (), begin, end); |
|---|
| 1278 | } |
|---|
| 1279 | |
|---|
| 1280 | iterator |
|---|
| 1281 | erase (iterator position) |
|---|
| 1282 | { |
|---|
| 1283 | return iterator (v_.erase (position.base ())); |
|---|
| 1284 | } |
|---|
| 1285 | |
|---|
| 1286 | iterator |
|---|
| 1287 | erase (iterator begin, iterator end) |
|---|
| 1288 | { |
|---|
| 1289 | return iterator (v_.erase (begin.base (), end.base ())); |
|---|
| 1290 | } |
|---|
| 1291 | |
|---|
| 1292 | // Note that the container object of the two sequences being |
|---|
| 1293 | // swapped should be the same. |
|---|
| 1294 | // |
|---|
| 1295 | void |
|---|
| 1296 | swap (sequence& x) |
|---|
| 1297 | { |
|---|
| 1298 | assert (container_ == x.container_); |
|---|
| 1299 | v_.swap (x.v_); |
|---|
| 1300 | } |
|---|
| 1301 | }; |
|---|
| 1302 | |
|---|
| 1303 | |
|---|
| 1304 | // Specialization for fundamental types. |
|---|
| 1305 | // |
|---|
| 1306 | template <typename X> |
|---|
| 1307 | class sequence<X, true>: public std::vector<X> |
|---|
| 1308 | { |
|---|
| 1309 | typedef std::vector<X> base_sequence; |
|---|
| 1310 | |
|---|
| 1311 | public: |
|---|
| 1312 | explicit |
|---|
| 1313 | sequence (flags = 0, container* = 0) |
|---|
| 1314 | { |
|---|
| 1315 | } |
|---|
| 1316 | |
|---|
| 1317 | explicit |
|---|
| 1318 | sequence (typename base_sequence::size_type n, const X& x = X ()) |
|---|
| 1319 | : base_sequence (n, x) |
|---|
| 1320 | { |
|---|
| 1321 | } |
|---|
| 1322 | |
|---|
| 1323 | template <typename I> |
|---|
| 1324 | sequence (const I& begin, const I& end) |
|---|
| 1325 | : base_sequence (begin, end) |
|---|
| 1326 | { |
|---|
| 1327 | } |
|---|
| 1328 | |
|---|
| 1329 | sequence (const sequence& s, flags = 0, container* = 0) |
|---|
| 1330 | : base_sequence (s) |
|---|
| 1331 | { |
|---|
| 1332 | } |
|---|
| 1333 | }; |
|---|
| 1334 | |
|---|
| 1335 | |
|---|
| 1336 | // Comparison operators. |
|---|
| 1337 | // |
|---|
| 1338 | |
|---|
| 1339 | template <typename X, bool fund> |
|---|
| 1340 | inline bool |
|---|
| 1341 | operator== (const sequence<X, fund>& a, const sequence<X, fund>& b) |
|---|
| 1342 | { |
|---|
| 1343 | return (a.size () == b.size () |
|---|
| 1344 | && std::equal (a.begin (), a.end (), b.begin ())); |
|---|
| 1345 | } |
|---|
| 1346 | |
|---|
| 1347 | template <typename X, bool fund> |
|---|
| 1348 | inline bool |
|---|
| 1349 | operator!= (const sequence<X, fund>& a, const sequence<X, fund>& b) |
|---|
| 1350 | { |
|---|
| 1351 | return !(a == b); |
|---|
| 1352 | } |
|---|
| 1353 | |
|---|
| 1354 | template <typename X, bool fund> |
|---|
| 1355 | inline bool |
|---|
| 1356 | operator< (const sequence<X, fund>& a, const sequence<X, fund>& b) |
|---|
| 1357 | { |
|---|
| 1358 | return std::lexicographical_compare (a.begin (), a.end (), |
|---|
| 1359 | b.begin (), b.end ()); |
|---|
| 1360 | } |
|---|
| 1361 | |
|---|
| 1362 | template <typename X, bool fund> |
|---|
| 1363 | inline bool |
|---|
| 1364 | operator> (const sequence<X, fund>& a, const sequence<X, fund>& b) |
|---|
| 1365 | { |
|---|
| 1366 | return b < a; |
|---|
| 1367 | } |
|---|
| 1368 | |
|---|
| 1369 | template <typename X, bool fund> |
|---|
| 1370 | inline bool |
|---|
| 1371 | operator<= (const sequence<X, fund>& a, const sequence<X, fund>& b) |
|---|
| 1372 | { |
|---|
| 1373 | return !(a > b); |
|---|
| 1374 | } |
|---|
| 1375 | |
|---|
| 1376 | template <typename X, bool fund> |
|---|
| 1377 | inline bool |
|---|
| 1378 | operator>= (const sequence<X, fund>& a, const sequence<X, fund>& b) |
|---|
| 1379 | { |
|---|
| 1380 | return !(a < b); |
|---|
| 1381 | } |
|---|
| 1382 | |
|---|
| 1383 | // Note that the container object of the two sequences being |
|---|
| 1384 | // swapped should be the same. |
|---|
| 1385 | // |
|---|
| 1386 | template <typename X, bool fund> |
|---|
| 1387 | inline void |
|---|
| 1388 | swap (sequence<X, fund>& x, sequence<X, fund>& y) |
|---|
| 1389 | { |
|---|
| 1390 | x.swap (y); |
|---|
| 1391 | } |
|---|
| 1392 | } |
|---|
| 1393 | } |
|---|
| 1394 | } |
|---|
| 1395 | |
|---|
| 1396 | #include <xsd/cxx/tree/containers.txx> |
|---|
| 1397 | |
|---|
| 1398 | #endif // XSD_CXX_TREE_CONTAINERS_HXX |
|---|