@@ -625,13 +625,34 @@ class Array : public ArrayBase<T, DIM, Array<T, DIM, SPACE>>
625
625
* \param [in] value the value to be added to the back.
626
626
*
627
627
* \note Reallocation is done if the new size will exceed the capacity.
628
- * \note If used in a device kernel, the number of push_backs must not exceed
629
- * the capacity, since device-side reallocations aren't supported.
630
- * \note Array must be allocated in unified memory if calling on the device.
631
- *
628
+ *
629
+ * \pre DIM == 1
630
+ */
631
+ void push_back (const T& value);
632
+
633
+ /* !
634
+ * \brief Push a value to the back of the array.
635
+ *
636
+ * \param [in] value the value to move to the back.
637
+ *
638
+ * \note Reallocation is done if the new size will exceed the capacity.
639
+ *
640
+ * \pre DIM == 1
641
+ */
642
+ void push_back (T&& value);
643
+
644
+ /* !
645
+ * \brief Inserts new element at the end of the Array.
646
+ *
647
+ * \param [in] args the arguments to forward to constructor of the element.
648
+ *
649
+ * \note Reallocation is done if the new size will exceed the capacity.
650
+ * \note The size increases by 1.
651
+ *
632
652
* \pre DIM == 1
633
653
*/
634
- AXOM_HOST_DEVICE void push_back (const T& value);
654
+ template <typename ... Args>
655
+ void emplace_back (Args&&... args);
635
656
636
657
/* !
637
658
* \brief Push a value to the back of the array.
@@ -642,10 +663,13 @@ class Array : public ArrayBase<T, DIM, Array<T, DIM, SPACE>>
642
663
* \note If used in a device kernel, the number of push_backs must not exceed
643
664
* the capacity, since device-side reallocations aren't supported.
644
665
* \note Array must be allocated in unified memory if calling on the device.
645
- *
666
+ *
646
667
* \pre DIM == 1
647
668
*/
648
- AXOM_HOST_DEVICE void push_back (T&& value);
669
+ // / @{
670
+ AXOM_HOST_DEVICE void push_back_device (const T& value);
671
+ AXOM_HOST_DEVICE void push_back_device (T&& value);
672
+ // / @}
649
673
650
674
/* !
651
675
* \brief Inserts new element at the end of the Array.
@@ -657,11 +681,11 @@ class Array : public ArrayBase<T, DIM, Array<T, DIM, SPACE>>
657
681
* \note If used in a device kernel, the number of push_backs must not exceed
658
682
* the capacity, since device-side reallocations aren't supported.
659
683
* \note Array must be allocated in unified memory if calling on the device.
660
- *
684
+ *
661
685
* \pre DIM == 1
662
686
*/
663
687
template <typename ... Args>
664
- AXOM_HOST_DEVICE void emplace_back (Args&&... args);
688
+ AXOM_HOST_DEVICE void emplace_back_device (Args&&... args);
665
689
666
690
// / @}
667
691
@@ -1425,15 +1449,15 @@ inline typename Array<T, DIM, SPACE>::ArrayIterator Array<T, DIM, SPACE>::emplac
1425
1449
1426
1450
// ------------------------------------------------------------------------------
1427
1451
template <typename T, int DIM, MemorySpace SPACE>
1428
- AXOM_HOST_DEVICE inline void Array<T, DIM, SPACE>::push_back(const T& value)
1452
+ inline void Array<T, DIM, SPACE>::push_back(const T& value)
1429
1453
{
1430
1454
static_assert (DIM == 1 , " push_back is only supported for 1D arrays" );
1431
1455
emplace_back (value);
1432
1456
}
1433
1457
1434
1458
// ------------------------------------------------------------------------------
1435
1459
template <typename T, int DIM, MemorySpace SPACE>
1436
- AXOM_HOST_DEVICE inline void Array<T, DIM, SPACE>::push_back(T&& value)
1460
+ inline void Array<T, DIM, SPACE>::push_back(T&& value)
1437
1461
{
1438
1462
static_assert (DIM == 1 , " push_back is only supported for 1D arrays" );
1439
1463
emplace_back (std::move (value));
@@ -1442,8 +1466,33 @@ AXOM_HOST_DEVICE inline void Array<T, DIM, SPACE>::push_back(T&& value)
1442
1466
// ------------------------------------------------------------------------------
1443
1467
AXOM_SUPPRESS_HD_WARN
1444
1468
template <typename T, int DIM, MemorySpace SPACE>
1469
+ AXOM_HOST_DEVICE inline void Array<T, DIM, SPACE>::push_back_device(const T& value)
1470
+ {
1471
+ static_assert (DIM == 1 , " push_back_device is only supported for 1D arrays" );
1472
+ emplace_back_device (value);
1473
+ }
1474
+
1475
+ // ------------------------------------------------------------------------------
1476
+ template <typename T, int DIM, MemorySpace SPACE>
1477
+ AXOM_HOST_DEVICE inline void Array<T, DIM, SPACE>::push_back_device(T&& value)
1478
+ {
1479
+ static_assert (DIM == 1 , " push_back_device is only supported for 1D arrays" );
1480
+ emplace_back_device (std::move (value));
1481
+ }
1482
+
1483
+ // ------------------------------------------------------------------------------
1484
+ template <typename T, int DIM, MemorySpace SPACE>
1445
1485
template <typename ... Args>
1446
- AXOM_HOST_DEVICE inline void Array<T, DIM, SPACE>::emplace_back(Args&&... args)
1486
+ inline void Array<T, DIM, SPACE>::emplace_back(Args&&... args)
1487
+ {
1488
+ static_assert (DIM == 1 , " emplace_back is only supported for 1D arrays" );
1489
+ emplace (size (), std::forward<Args>(args)...);
1490
+ }
1491
+
1492
+ // ------------------------------------------------------------------------------
1493
+ template <typename T, int DIM, MemorySpace SPACE>
1494
+ template <typename ... Args>
1495
+ AXOM_HOST_DEVICE inline void Array<T, DIM, SPACE>::emplace_back_device(Args&&... args)
1447
1496
{
1448
1497
static_assert (DIM == 1 , " emplace_back is only supported for 1D arrays" );
1449
1498
#ifdef AXOM_DEVICE_CODE
@@ -1685,7 +1734,12 @@ template <typename T, int DIM, MemorySpace SPACE>
1685
1734
inline void Array<T, DIM, SPACE>::dynamicRealloc(IndexType new_num_elements)
1686
1735
{
1687
1736
assert (m_resize_ratio >= 1.0 );
1688
- IndexType new_capacity = new_num_elements * m_resize_ratio + 0.5 ;
1737
+
1738
+ // Using resize strategy from LLVM libc++ (vector::__recommend()):
1739
+ // new_capacity = max(capacity() * resize_ratio, new_num_elements)
1740
+ IndexType new_capacity =
1741
+ axom::utilities::max<IndexType>(this ->capacity () * m_resize_ratio + 0.5 ,
1742
+ new_num_elements);
1689
1743
const IndexType block_size = this ->blockSize ();
1690
1744
const IndexType remainder = new_capacity % block_size;
1691
1745
if (remainder != 0 )
0 commit comments