From 21cfc811cf787ce67d893722f9a03049dc28bec4 Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Thu, 6 Nov 2014 12:54:00 +0000 Subject: [PATCH] Added 'swap' --- deque.h | 8 +++----- queue.h | 25 ++++++++++++++++++++++-- stack.h | 25 ++++++++++++++++++++++-- test/test_deque.cpp | 4 ++-- test/test_list.cpp | 19 +++++++++++++++++- test/test_queue.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++ test/test_stack.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++ test/test_vector.cpp | 21 ++++++++++++++++++++ vector.h | 21 +++++++++++++++++++- 9 files changed, 202 insertions(+), 13 deletions(-) diff --git a/deque.h b/deque.h index 95a6cc25..1e011c68 100644 --- a/deque.h +++ b/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& 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 - void swap(etl::deque< T, MAX_SIZE>& first, etl::deque< T, MAX_SIZE>& second) + void swap(etl::deque& first, etl::deque< T, MAX_SIZE>& second) { first.swap(second); } diff --git a/queue.h b/queue.h index c224095e..29fcde02 100644 --- a/queue.h +++ b/queue.h @@ -32,17 +32,18 @@ SOFTWARE. #include #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 + void swap(etl::queue& first, etl::queue& second) + { + first.swap(second); + } } #endif diff --git a/stack.h b/stack.h index 204dde9b..53a3a1d4 100644 --- a/stack.h +++ b/stack.h @@ -30,18 +30,20 @@ SOFTWARE. #define __ETL_STACK__ #include +#include #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 + void swap(etl::stack& first, etl::stack& second) + { + first.swap(second); + } } #endif diff --git a/test/test_deque.cpp b/test/test_deque.cpp index d2d0363a..ee4ef6b1 100644 --- a/test/test_deque.cpp +++ b/test/test_deque.cpp @@ -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()); diff --git a/test/test_list.cpp b/test/test_list.cpp index 030fc09a..5a75eb3d 100644 --- a/test/test_list.cpp +++ b/test/test_list.cpp @@ -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); + } }; } diff --git a/test/test_queue.cpp b/test/test_queue.cpp index c5bb08b1..87412064 100644 --- a/test/test_queue.cpp +++ b/test/test_queue.cpp @@ -26,6 +26,8 @@ SOFTWARE. #include +#include + #include "../queue.h" namespace @@ -297,5 +299,49 @@ namespace CHECK(pass); } + + //************************************************************************* + TEST(Swap) + { + std::queue compare1; + std::queue compare2; + etl::queue queue1; + etl::queue 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(); + } + } }; } diff --git a/test/test_stack.cpp b/test/test_stack.cpp index 8e8ea909..cff96230 100644 --- a/test/test_stack.cpp +++ b/test/test_stack.cpp @@ -26,6 +26,8 @@ SOFTWARE. #include +#include + #include "../stack.h" namespace @@ -258,5 +260,49 @@ namespace CHECK(pass); } + + //************************************************************************* + TEST(Swap) + { + std::stack compare1; + std::stack compare2; + etl::stack stack1; + etl::stack 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(); + } + } }; } diff --git a/test/test_vector.cpp b/test/test_vector.cpp index 7fc2c6cd..5b1217ee 100644 --- a/test/test_vector.cpp +++ b/test/test_vector.cpp @@ -742,6 +742,27 @@ namespace CHECK_EQUAL(constData.end(), std::end(constData)); } + //************************************************************************* + TEST(Swap) + { + std::vector first = { 1, 2, 3, 4, 5, 6 }; + std::vector 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) { diff --git a/vector.h b/vector.h index 09153463..d30facf1 100644 --- a/vector.h +++ b/vector.h @@ -33,6 +33,7 @@ SOFTWARE. #include #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]; /// + void swap(etl::vector& first, etl::vector& second) + { + first.swap(second); + } } #endif