Changeset 37861 in vbox for trunk/include/iprt/cpp
- Timestamp:
- Jul 11, 2011 10:03:22 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/cpp/list.h
r36654 r37861 219 219 , m_cCapacity(0) 220 220 { 221 realloc_grow(cCapacity); 221 if (cCapacity > 0) 222 realloc_grow(cCapacity); 222 223 } 223 224 … … 236 237 , m_cCapacity(0) 237 238 { 238 realloc_ grow(other.m_cSize);239 realloc_no_elements_clean(other.m_cSize); 239 240 RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize); 240 241 m_cSize = other.m_cSize; … … 372 373 { 373 374 m_guard.enterWrite(); 374 if (m_cCapacity - m_cSize < other.m_cSize) 375 realloc_grow(m_cCapacity + (other.m_cSize - (m_cCapacity - m_cSize))); 376 RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, m_cSize, other.m_cSize); 377 m_cSize += other.m_cSize; 375 if (RT_LIKELY(other.m_cSize > 0)) 376 { 377 if (m_cCapacity - m_cSize < other.m_cSize) 378 realloc_grow(m_cCapacity + (other.m_cSize - (m_cCapacity - m_cSize))); 379 RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, m_cSize, other.m_cSize); 380 m_cSize += other.m_cSize; 381 } 378 382 m_guard.leaveWrite(); 379 383 … … 391 395 { 392 396 /* Prevent self assignment */ 393 if ( this == &other)397 if (RT_UNLIKELY(this == &other)) 394 398 return *this; 395 399 396 400 m_guard.enterWrite(); 397 /* Values cleanup*/401 /* Delete all items. */ 398 402 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize); 399 400 /* Copy */ 403 /* Need we to realloc memory. */ 401 404 if (other.m_cSize != m_cCapacity) 402 realloc_ grow(other.m_cSize);405 realloc_no_elements_clean(other.m_cSize); 403 406 m_cSize = other.m_cSize; 407 /* Copy new items. */ 404 408 RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize); 405 409 m_guard.leaveWrite(); … … 431 435 * Return the first item as constant object. 432 436 * 437 * @note No boundary checks are done. Make sure there is at least one 438 * element. 439 * 440 * @return The first item. 441 */ 442 GET_CRTYPE first() const 443 { 444 m_guard.enterRead(); 445 GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0); 446 m_guard.leaveRead(); 447 return res; 448 } 449 450 /** 451 * Return the first item. 452 * 453 * @note No boundary checks are done. Make sure there is at least one 454 * element. 455 * 456 * @return The first item. 457 */ 458 GET_RTYPE first() 459 { 460 m_guard.enterRead(); 461 GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0); 462 m_guard.leaveRead(); 463 return res; 464 } 465 466 /** 467 * Return the last item as constant object. 468 * 469 * @note No boundary checks are done. Make sure there is at least one 470 * element. 471 * 472 * @return The last item. 473 */ 474 GET_CRTYPE last() const 475 { 476 m_guard.enterRead(); 477 GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1); 478 m_guard.leaveRead(); 479 return res; 480 } 481 482 /** 483 * Return the last item. 484 * 485 * @note No boundary checks are done. Make sure there is at least one 486 * element. 487 * 488 * @return The last item. 489 */ 490 GET_RTYPE last() 491 { 492 m_guard.enterRead(); 493 GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1); 494 m_guard.leaveRead(); 495 return res; 496 } 497 498 /** 499 * Return the item at position @a i as constant object. 500 * 433 501 * @note No boundary checks are done. Make sure @a i is equal or greater zero 434 502 * and smaller than RTCList::size. 435 503 * 436 * @return The first item. 437 */ 438 GET_CRTYPE first() const 439 { 440 m_guard.enterRead(); 441 GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0); 442 m_guard.leaveRead(); 443 return res; 444 } 445 446 /** 447 * Return the first item. 504 * @param i The position of the item to return. 505 * @return The item at position @a i. 506 */ 507 GET_CRTYPE at(size_t i) const 508 { 509 m_guard.enterRead(); 510 GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i); 511 m_guard.leaveRead(); 512 return res; 513 } 514 515 /** 516 * Return the item at position @a i. 448 517 * 449 518 * @note No boundary checks are done. Make sure @a i is equal or greater zero 450 519 * and smaller than RTCList::size. 451 520 * 452 * @return The first item. 453 */ 454 GET_RTYPE first() 455 { 456 m_guard.enterRead(); 457 GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0); 458 m_guard.leaveRead(); 459 return res; 460 } 461 462 /** 463 * Return the last item as constant object. 521 * @param i The position of the item to return. 522 * @return The item at position @a i. 523 */ 524 GET_RTYPE at(size_t i) 525 { 526 m_guard.enterRead(); 527 GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i); 528 m_guard.leaveRead(); 529 return res; 530 } 531 532 /** 533 * Return the item at position @a i as mutable reference. 464 534 * 465 535 * @note No boundary checks are done. Make sure @a i is equal or greater zero 466 536 * and smaller than RTCList::size. 467 537 * 468 * @return The last item.469 */470 GET_CRTYPE last() const471 {472 m_guard.enterRead();473 GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1);474 m_guard.leaveRead();475 return res;476 }477 478 /**479 * Return the last item.480 *481 * @note No boundary checks are done. Make sure @a i is equal or greater zero482 * and smaller than RTCList::size.483 *484 * @return The last item.485 */486 GET_RTYPE last()487 {488 m_guard.enterRead();489 GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1);490 m_guard.leaveRead();491 return res;492 }493 494 /**495 * Return the item at position @a i as constant object.496 *497 * @note No boundary checks are done. Make sure @a i is equal or greater zero498 * and smaller than RTCList::size.499 *500 * @param i The position of the item to return.501 * @return The item at position @a i.502 */503 GET_CRTYPE at(size_t i) const504 {505 m_guard.enterRead();506 GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i);507 m_guard.leaveRead();508 return res;509 }510 511 /**512 * Return the item at position @a i.513 *514 * @note No boundary checks are done. Make sure @a i is equal or greater zero515 * and smaller than RTCList::size.516 *517 538 * @param i The position of the item to return. 518 539 * @return The item at position @a i. 519 540 */ 520 GET_RTYPE at(size_t i)521 {522 m_guard.enterRead();523 GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i);524 m_guard.leaveRead();525 return res;526 }527 528 /**529 * Return the item at position @a i as mutable reference.530 *531 * @note No boundary checks are done. Make sure @a i is equal or greater zero532 * and smaller than RTCList::size.533 *534 * @param i The position of the item to return.535 * @return The item at position @a i.536 */537 541 T &operator[](size_t i) 538 542 { … … 553 557 { 554 558 m_guard.enterRead(); 555 if ( i >= m_cSize)559 if (RT_UNLIKELY(i >= m_cSize)) 556 560 { 557 561 m_guard.leaveRead(); … … 574 578 { 575 579 m_guard.enterRead(); 576 if ( i >= m_cSize)580 if (RT_UNLIKELY(i >= m_cSize)) 577 581 { 578 582 m_guard.leaveRead(); … … 601 605 * Remove the first item. 602 606 * 603 * @note No boundary checks are done. Make sure @a i is equal or greater zero604 * and smaller than RTCList::size.607 * @note No boundary checks are done. Make sure there is at least one 608 * element. 605 609 */ 606 610 void removeFirst() … … 612 616 * Remove the last item. 613 617 * 614 * @note No boundary checks are done. Make sure @a i is equal or greater zero615 * and smaller than RTCList::size.618 * @note No boundary checks are done. Make sure there is at least one 619 * element. 616 620 */ 617 621 void removeLast() … … 669 673 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize); 670 674 if (m_cSize != DefaultCapacity) 671 realloc_ grow(DefaultCapacity);675 realloc_no_elements_clean(DefaultCapacity); 672 676 m_cSize = 0; 673 677 m_guard.leaveWrite(); … … 691 695 m_guard.leaveRead(); 692 696 return res; 697 } 698 699 RTCListBase<T, ITYPE, MT> &operator<<(const T &val) 700 { 701 return append(val); 693 702 } 694 703 … … 716 725 if ( cNewSize < m_cSize 717 726 && m_pArray) 718 {719 727 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, cNewSize, m_cSize - cNewSize); 728 realloc_no_elements_clean(cNewSize); 729 } 730 731 void realloc_no_elements_clean(size_t cNewSize) 732 { 733 /* Same size? */ 734 if (cNewSize == m_cCapacity) 735 return; 736 737 /* If we get smaller we have to delete some of the objects at the end 738 of the list. */ 739 if ( cNewSize < m_cSize 740 && m_pArray) 720 741 m_cSize -= m_cSize - cNewSize; 721 }722 742 723 743 /* If we get zero we delete the array it self. */ … … 803 823 : BASE(cCapacity) {} 804 824 825 RTCList(const BASE &other) 826 : BASE(other) {} 827 805 828 /* Define our own new and delete. */ 806 829 RTMEMEF_NEW_AND_DELETE_OPERATORS();
Note:
See TracChangeset
for help on using the changeset viewer.