Skip to content

Commit

Permalink
#1934: Remove list initializer constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
thearusable committed Sep 23, 2024
1 parent 1d828ae commit 13b5d7f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 67 deletions.
37 changes: 13 additions & 24 deletions src/vt/utils/container/circular_phases_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

#include "vt/config.h"
#include <vector>
#include <algorithm>

namespace vt { namespace util { namespace container {

Expand All @@ -61,6 +62,12 @@ class CircularPhasesBuffer {
constexpr static auto no_phase = std::numeric_limits<PhaseType>::max();
constexpr static auto no_index = std::numeric_limits<std::size_t>::max();

/**
* \brief Creates a new pair to be stored in the buffer
*
* \param phase - the phase to be assigned, \c no_phase otherwise
* \return the new pair
*/
static StoredPair makeEmptyPair(const PhaseType& phase = no_phase) {
return std::make_pair(phase, StoredType{});
}
Expand All @@ -75,33 +82,16 @@ class CircularPhasesBuffer {
CircularPhasesBuffer(std::size_t size_in = 1)
: buffer_(size_in, makeEmptyPair())
{
vtAssert(size_in > 0, "Size of circular phases buffer needs to be greather than zero.");
vtAssert(size_in > 0, "Size of CircularPhasesBuffer needs to be greather than zero.");
}

/**
* \brief Construct a CircularPhasesBuffer.
*
* \param[in] in_list the initializer list with elements to be put into the buffer
*/
CircularPhasesBuffer(std::initializer_list<StoredPair> in_list) {
const auto& [min, max] = std::minmax(in_list, [](const StoredPair& lhs, const StoredPair& rhs) {
return lhs.first < rhs.first;
});
// Calcuate the size of the buffer to hold all phases including the missing ones
buffer_.resize((max.first - min.first) + 1, makeEmptyPair());

for (auto pair : in_list) {
buffer_[phaseToIndex(pair.first)] = pair;
updateHead(pair.first);
}
}

/**
* \brief Check if phase is present in the buffer.
*
* \param[in] phase the phase to look for
*
* \return whether buffer contains the phase or not
* \return whenever the buffer contains the phase or not
*/
bool contains(const PhaseType& phase) const {
return buffer_[phaseToIndex(phase)].first == phase;
Expand Down Expand Up @@ -129,7 +119,6 @@ class CircularPhasesBuffer {
* \param[in] phase the phase for which data will be stored
* \param[in] obj the data to store
*/
// probably best to get phase and data and store it when it is within valid range of phases
void store(const PhaseType& phase, StoredType data) {
vtAssert(canBeStored(phase), "Phase is out of valid range");

Expand Down Expand Up @@ -168,15 +157,15 @@ class CircularPhasesBuffer {
* \param[in] new_size_in the requested new size of the buffer
*/
void resize(const std::size_t new_size_in) {
vtAssert(new_size_in > 0, "Size of circular phases buffer needs to be greather than zero.");
if (new_size_in == buffer_.size()) {
auto new_size = std::max(std::size_t{1}, new_size_in);
if (new_size == buffer_.size()) {
return;
}

// temporary vector to copy the elements to retain
std::vector<StoredPair> tmp(new_size_in, makeEmptyPair());
std::vector<StoredPair> tmp(new_size, makeEmptyPair());
// number of elements to copy
auto num = std::min(new_size_in, buffer_.size() - numFree());
auto num = std::min(new_size, buffer_.size() - numFree());

// copy num phases
auto to_copy = head_phase_;
Expand Down
43 changes: 0 additions & 43 deletions tests/unit/utils/test_circular_phases_buffer.nompi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,47 +276,4 @@ TEST_F(TestCircularPhasesBuffer, test_circular_phases_buffer_forward_iter) {
}
}

TEST_F(TestCircularPhasesBuffer, test_circular_phases_buffer_list_init) {
{
CircularBufferType buffer{};

EXPECT_EQ(true, buffer.empty());
EXPECT_EQ(0, buffer.size());
EXPECT_EQ(1, buffer.capacity());
}

{
CircularBufferType buffer{{0, {0}}, {1, {1}}, {2, {2}}};

EXPECT_EQ(false, buffer.empty());
EXPECT_EQ(3, buffer.size());
EXPECT_EQ(3, buffer.capacity());
validatePresentPhases(buffer, {0, 1, 2});
EXPECT_EQ(2, buffer.frontPhase());
EXPECT_EQ(2, buffer.frontData().x);
}

{
CircularBufferType buffer{{4, {4}}, {9, {9}}, {0, {0}}};

EXPECT_EQ(false, buffer.empty());
EXPECT_EQ(3, buffer.size());
EXPECT_EQ(10, buffer.capacity());
validatePresentPhases(buffer, {0, 4, 9});
EXPECT_EQ(9, buffer.frontPhase());
EXPECT_EQ(9, buffer.frontData().x);
}

{
CircularBufferType buffer{{9, {9}}, {4, {4}}, {12, {12}}};

EXPECT_EQ(false, buffer.empty());
EXPECT_EQ(3, buffer.size());
EXPECT_EQ(9, buffer.capacity());
validatePresentPhases(buffer, {12, 4, 9});
EXPECT_EQ(12, buffer.frontPhase());
EXPECT_EQ(12, buffer.frontData().x);
}
}

}}} /* end namespace vt::tests::unit */

0 comments on commit 13b5d7f

Please sign in to comment.