aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-06-10 06:12:52 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-06-10 06:12:52 +0800
commit0c3c963d64f42c9dc30b71d6b7d5f13c56611ec0 (patch)
treee65660002b55fd11329ef21c4f86d0ba0bf347b1
parentf089c76d548c4a6c8116a3cd18c12cc6f4d53821 (diff)
downloadtangerine-mcl-0c3c963d64f42c9dc30b71d6b7d5f13c56611ec0.tar.gz
tangerine-mcl-0c3c963d64f42c9dc30b71d6b7d5f13c56611ec0.tar.zst
tangerine-mcl-0c3c963d64f42c9dc30b71d6b7d5f13c56611ec0.zip
add FixedArray::swap
-rw-r--r--include/mcl/array.hpp18
-rw-r--r--test/array_test.cpp27
2 files changed, 44 insertions, 1 deletions
diff --git a/include/mcl/array.hpp b/include/mcl/array.hpp
index 488a9fe..84c5f37 100644
--- a/include/mcl/array.hpp
+++ b/include/mcl/array.hpp
@@ -118,6 +118,24 @@ public:
n_ = 0;
}
size_t size() const { return n_; }
+ void swap(FixedArray<T, maxSize>& rhs)
+ {
+ T *minP = p_;
+ size_t minN = n_;
+ T *maxP = rhs.p_;
+ size_t maxN = rhs.n_;
+ if (minP > maxP) {
+ swap_(minP, maxP);
+ swap_(minN, maxN);
+ }
+ for (size_t i = 0; i < minN; i++) {
+ swap_(minP[i], maxP[i]);
+ }
+ for (size_t i = minN; i < maxN; i++) {
+ minP[i] = maxP[i];
+ }
+ swap_(n_, rhs.n_);
+ }
T& operator[](size_t n) { return p_[n]; }
const T& operator[](size_t n) const { return p_[n]; }
T* data() { return p_; }
diff --git a/test/array_test.cpp b/test/array_test.cpp
index b0c3c16..d9b66c7 100644
--- a/test/array_test.cpp
+++ b/test/array_test.cpp
@@ -1,13 +1,28 @@
#include <mcl/array.hpp>
#include <cybozu/test.hpp>
+template<class Array, size_t an, size_t bn>
+void swapTest(const int (&a)[an], const int (&b)[bn])
+{
+ Array s, t;
+ CYBOZU_TEST_ASSERT(s.resize(an));
+ CYBOZU_TEST_ASSERT(t.resize(bn));
+ for (size_t i = 0; i < an; i++) s[i] = a[i];
+ for (size_t i = 0; i < bn; i++) t[i] = b[i];
+ s.swap(t);
+ CYBOZU_TEST_EQUAL(s.size(), bn);
+ CYBOZU_TEST_EQUAL(t.size(), an);
+ CYBOZU_TEST_EQUAL_ARRAY(s, b, s.size());
+ CYBOZU_TEST_EQUAL_ARRAY(t, a, t.size());
+}
+
CYBOZU_TEST_AUTO(resize)
{
mcl::Array<int> a, b;
CYBOZU_TEST_EQUAL(a.size(), 0);
CYBOZU_TEST_EQUAL(b.size(), 0);
- const size_t n = 3;
+ const size_t n = 5;
bool ok = a.resize(n);
CYBOZU_TEST_ASSERT(ok);
CYBOZU_TEST_EQUAL(n, a.size());
@@ -29,6 +44,11 @@ CYBOZU_TEST_AUTO(resize)
CYBOZU_TEST_ASSERT(ok);
CYBOZU_TEST_EQUAL(b.size(), large);
CYBOZU_TEST_EQUAL_ARRAY(a.data(), b.data(), small);
+
+ const int aTbl[] = { 3, 4 };
+ const int bTbl[] = { 7, 6, 5, 3 };
+ swapTest<mcl::Array<int> >(aTbl, bTbl);
+ swapTest<mcl::Array<int> >(bTbl, aTbl);
}
CYBOZU_TEST_AUTO(FixedArray)
@@ -57,4 +77,9 @@ CYBOZU_TEST_AUTO(FixedArray)
const size_t large = n + 1;
ok = b.resize(large);
CYBOZU_TEST_ASSERT(!ok);
+
+ const int aTbl[] = { 3, 4 };
+ const int bTbl[] = { 7, 6, 5, 3 };
+ swapTest<mcl::FixedArray<int, n> >(aTbl, bTbl);
+ swapTest<mcl::FixedArray<int, n> >(bTbl, aTbl);
}