mirror of
https://github.com/ETLCPP/etl.git
synced 2026-10-28 18:07:10 +08:00
Added 'swap'
This commit is contained in:
parent
a849a56751
commit
21cfc811cf
8
deque.h
8
deque.h
@ -46,6 +46,7 @@ namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// A fixed capacity double ended queue.
|
||||
///\node The deque allocates one more element than the specified maximum size.
|
||||
///\tparam T The type of items this deque holds.
|
||||
///\tparam MAX_SIZE_ The capacity of the deque
|
||||
///\ingroup deque
|
||||
@ -120,7 +121,7 @@ namespace etl
|
||||
//*************************************************************************
|
||||
/// Swap
|
||||
//*************************************************************************
|
||||
void swap(deque<T, MAX_SIZE>& other)
|
||||
void swap(deque& other)
|
||||
{
|
||||
// Swap the internal iterators.
|
||||
this->first.swap(other.first);
|
||||
@ -136,15 +137,12 @@ namespace etl
|
||||
/// The buffer.
|
||||
T buffer[BUFFER_SIZE];
|
||||
};
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
//*************************************************************************
|
||||
/// Swap
|
||||
//*************************************************************************
|
||||
template <typename T, const size_t MAX_SIZE>
|
||||
void swap(etl::deque< T, MAX_SIZE>& first, etl::deque< T, MAX_SIZE>& second)
|
||||
void swap(etl::deque<T, MAX_SIZE>& first, etl::deque< T, MAX_SIZE>& second)
|
||||
{
|
||||
first.swap(second);
|
||||
}
|
||||
|
||||
25
queue.h
25
queue.h
@ -32,17 +32,18 @@ SOFTWARE.
|
||||
#include <cstddef>
|
||||
|
||||
#include "iqueue.h"
|
||||
#include "container.h"
|
||||
|
||||
//*****************************************************************************
|
||||
///\defgroup queue queue
|
||||
/// A First-in / first-out queue with the capacity defined at compile time,
|
||||
/// A First-in / first-out queue with the capacity defined at compile time,
|
||||
/// written in the STL style.
|
||||
///\note Uses a predefined array, so MAX_SIZE_ elements will be always be constructed.
|
||||
///\ingroup containers
|
||||
//*****************************************************************************
|
||||
|
||||
namespace etl
|
||||
{
|
||||
{
|
||||
//***************************************************************************
|
||||
///\ingroup queue
|
||||
/// A fixed capacity queue.
|
||||
@ -63,10 +64,30 @@ namespace etl
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swap
|
||||
//*************************************************************************
|
||||
void swap(queue& other)
|
||||
{
|
||||
std::swap_ranges(etl::begin(buffer), etl::end(buffer), etl::begin(other.buffer));
|
||||
std::swap(this->in, other.in);
|
||||
std::swap(this->out, other.out);
|
||||
std::swap(this->current_size, other.current_size);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
T buffer[SIZE]; ///< The internal buffer.
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Swap
|
||||
//*************************************************************************
|
||||
template <typename T, const size_t MAX_SIZE>
|
||||
void swap(etl::queue<T, MAX_SIZE>& first, etl::queue<T, MAX_SIZE>& second)
|
||||
{
|
||||
first.swap(second);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
25
stack.h
25
stack.h
@ -30,18 +30,20 @@ SOFTWARE.
|
||||
#define __ETL_STACK__
|
||||
|
||||
#include <cstddef>
|
||||
#include <algorithm>
|
||||
|
||||
#include "istack.h"
|
||||
#include "container.h"
|
||||
|
||||
//*****************************************************************************
|
||||
///\defgroup stack stack
|
||||
/// A First-in / first-out stack with the capacity defined at compile time,
|
||||
/// A First-in / first-out stack with the capacity defined at compile time,
|
||||
/// written in the STL style.
|
||||
///\ingroup containers
|
||||
//*****************************************************************************
|
||||
|
||||
namespace etl
|
||||
{
|
||||
{
|
||||
//***************************************************************************
|
||||
///\ingroup stack
|
||||
/// A fixed capacity stack.
|
||||
@ -63,10 +65,29 @@ namespace etl
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swap
|
||||
//*************************************************************************
|
||||
void swap(stack& other)
|
||||
{
|
||||
std::swap_ranges(etl::begin(buffer), etl::end(buffer), etl::begin(other.buffer));
|
||||
std::swap(this->top_index, other.top_index);
|
||||
std::swap(this->current_size, other.current_size);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
T buffer[SIZE]; ///< The internal buffer.
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Swap
|
||||
//*************************************************************************
|
||||
template <typename T, const size_t MAX_SIZE>
|
||||
void swap(etl::stack<T, MAX_SIZE>& first, etl::stack<T, MAX_SIZE>& second)
|
||||
{
|
||||
first.swap(second);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -122,7 +122,7 @@ namespace FixedDequeTest
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(StdSwap)
|
||||
TEST(Swap)
|
||||
{
|
||||
CompareData compare_data1 = { 1, 2, 3, 4, 5, 6 };
|
||||
CompareData compare_data2 = { 6, 5, 4 };
|
||||
@ -130,7 +130,7 @@ namespace FixedDequeTest
|
||||
Data data1(compare_data1.begin(), compare_data1.end());
|
||||
Data data2(compare_data2.begin(), compare_data2.end());
|
||||
|
||||
std::swap(data1, data2);
|
||||
swap(data1, data2);
|
||||
|
||||
CHECK_EQUAL(compare_data2.size(), data1.size());
|
||||
CHECK_EQUAL(compare_data1.size(), data2.size());
|
||||
|
||||
@ -48,6 +48,7 @@ namespace
|
||||
InitialData unsorted_data;
|
||||
InitialData sorted_data;
|
||||
InitialData non_unique_data;
|
||||
InitialData small_data;
|
||||
|
||||
bool are_equal;
|
||||
|
||||
@ -58,7 +59,8 @@ namespace
|
||||
{
|
||||
unsorted_data = { 1, 0, 3, 2, 5, 4, 7, 6, 9, 8 };
|
||||
sorted_data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
non_unique_data = { 0, 0, 1, 1, 2, 3, 3, 3, 4, 5 };
|
||||
non_unique_data = { 0, 0, 1, 1, 2, 3, 3, 3, 4, 5 };
|
||||
small_data = { 0, 1, 2, 3, 4, 5 };
|
||||
}
|
||||
};
|
||||
|
||||
@ -733,5 +735,20 @@ namespace
|
||||
|
||||
CHECK(are_equal);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, Swap)
|
||||
{
|
||||
Data first(unsorted_data.begin(), unsorted_data.end());
|
||||
Data second(small_data.begin(), small_data.end());
|
||||
|
||||
swap(first, second);
|
||||
|
||||
are_equal = std::equal(first.begin(), first.end(), small_data.begin());
|
||||
CHECK(are_equal);
|
||||
|
||||
are_equal = std::equal(second.begin(), second.end(), unsorted_data.begin());
|
||||
CHECK(are_equal);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -26,6 +26,8 @@ SOFTWARE.
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <queue>
|
||||
|
||||
#include "../queue.h"
|
||||
|
||||
namespace
|
||||
@ -297,5 +299,49 @@ namespace
|
||||
|
||||
CHECK(pass);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(Swap)
|
||||
{
|
||||
std::queue<int> compare1;
|
||||
std::queue<int> compare2;
|
||||
etl::queue<int, 4> queue1;
|
||||
etl::queue<int, 4> stack2;
|
||||
|
||||
compare1.push(1);
|
||||
compare1.push(2);
|
||||
compare1.push(3);
|
||||
compare1.push(4);
|
||||
|
||||
queue1.push(1);
|
||||
queue1.push(2);
|
||||
queue1.push(3);
|
||||
queue1.push(4);
|
||||
|
||||
compare2.push(5);
|
||||
compare2.push(6);
|
||||
|
||||
stack2.push(5);
|
||||
stack2.push(6);
|
||||
|
||||
swap(queue1, stack2);
|
||||
|
||||
CHECK_EQUAL(compare2.size(), queue1.size());
|
||||
CHECK_EQUAL(compare1.size(), stack2.size());
|
||||
|
||||
for (size_t i = 0; i < queue1.size(); ++i)
|
||||
{
|
||||
CHECK_EQUAL(compare2.front(), queue1.front());
|
||||
compare2.pop();
|
||||
queue1.pop();
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < stack2.size(); ++i)
|
||||
{
|
||||
CHECK_EQUAL(compare1.front(), stack2.front());
|
||||
compare1.pop();
|
||||
stack2.pop();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -26,6 +26,8 @@ SOFTWARE.
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <stack>
|
||||
|
||||
#include "../stack.h"
|
||||
|
||||
namespace
|
||||
@ -258,5 +260,49 @@ namespace
|
||||
|
||||
CHECK(pass);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(Swap)
|
||||
{
|
||||
std::stack<int> compare1;
|
||||
std::stack<int> compare2;
|
||||
etl::stack<int, 4> stack1;
|
||||
etl::stack<int, 4> stack2;
|
||||
|
||||
compare1.push(1);
|
||||
compare1.push(2);
|
||||
compare1.push(3);
|
||||
compare1.push(4);
|
||||
|
||||
stack1.push(1);
|
||||
stack1.push(2);
|
||||
stack1.push(3);
|
||||
stack1.push(4);
|
||||
|
||||
compare2.push(5);
|
||||
compare2.push(6);
|
||||
|
||||
stack2.push(5);
|
||||
stack2.push(6);
|
||||
|
||||
swap(stack1, stack2);
|
||||
|
||||
CHECK_EQUAL(compare2.size(), stack1.size());
|
||||
CHECK_EQUAL(compare1.size(), stack2.size());
|
||||
|
||||
for (size_t i = 0; i < stack1.size(); ++i)
|
||||
{
|
||||
CHECK_EQUAL(compare2.top(), stack1.top());
|
||||
compare2.pop();
|
||||
stack1.pop();
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < stack2.size(); ++i)
|
||||
{
|
||||
CHECK_EQUAL(compare1.top(), stack2.top());
|
||||
compare1.pop();
|
||||
stack2.pop();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -742,6 +742,27 @@ namespace
|
||||
CHECK_EQUAL(constData.end(), std::end(constData));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(Swap)
|
||||
{
|
||||
std::vector<int> first = { 1, 2, 3, 4, 5, 6 };
|
||||
std::vector<int> second = { 6, 5, 4 };
|
||||
|
||||
Data data1(first.begin(), first.end());
|
||||
Data data2(second.begin(), second.end());
|
||||
|
||||
swap(data1, data2);
|
||||
|
||||
CHECK_EQUAL(second.size(), data1.size());
|
||||
CHECK_EQUAL(first.size(), data2.size());
|
||||
|
||||
CHECK_EQUAL(std::distance(second.begin(), second.end()), std::distance(data1.begin(), data1.end()));
|
||||
CHECK_EQUAL(std::distance(first.begin(), first.end()), std::distance(data2.begin(), data2.end()));
|
||||
|
||||
CHECK(std::equal(second.begin(), second.end(), data1.begin()));
|
||||
CHECK(std::equal(first.begin(), first.end(), data2.begin()));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST_FIXTURE(SetupFixture, Equal)
|
||||
{
|
||||
|
||||
21
vector.h
21
vector.h
@ -33,6 +33,7 @@ SOFTWARE.
|
||||
#include <iterator>
|
||||
|
||||
#include "ivector.h"
|
||||
#include "container.h"
|
||||
|
||||
//*****************************************************************************
|
||||
///\defgroup vector vector
|
||||
@ -108,11 +109,29 @@ namespace etl
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//*************************************************************************
|
||||
/// Swap
|
||||
//*************************************************************************
|
||||
void swap(vector& other)
|
||||
{
|
||||
std::swap_ranges(etl::begin(buffer), etl::end(buffer), etl::begin(other.buffer));
|
||||
std::swap(this->current_size, other.current_size);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
T buffer[MAX_SIZE]; ///<The array that stores the elements.
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Swap
|
||||
//*************************************************************************
|
||||
template <typename T, const size_t MAX_SIZE>
|
||||
void swap(etl::vector<T, MAX_SIZE>& first, etl::vector<T, MAX_SIZE>& second)
|
||||
{
|
||||
first.swap(second);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user