It merely performs an std::swap() on both array elements, but it’s apparently convenient to have.undefined
@@ -30,7 +30,7 @@ void array_base<T, ARRAY>::shuffle() | |||||
while (n > 0) | while (n > 0) | ||||
{ | { | ||||
ni = lol::rand(n--) | 0; | ni = lol::rand(n--) | 0; | ||||
std::swap(m_data[ni], m_data[n]); | this->swap(ni, n); | ||||
} | } | ||||
} | } | ||||
@@ -59,7 +59,7 @@ void array_base<T, ARRAY>::sort(SortAlgorithm algorithm) | |||||
d = 1; | d = 1; | ||||
if (m_data[i + 1] < m_data[i]) | if (m_data[i + 1] < m_data[i]) | ||||
{ | { | ||||
std::swap(m_data[i], m_data[i + 1]); | this->swap(i, i + 1); | ||||
d = -1; | d = -1; | ||||
} | } | ||||
} | } | ||||
@@ -103,7 +103,7 @@ static void quick_swap_sort(array_base<T, ARRAY> &a, | |||||
{ | { | ||||
if (!(a[i0] < median) && a[i1] < median) | if (!(a[i0] < median) && a[i1] < median) | ||||
{ | { | ||||
std::swap(a[i0], a[i1]); | a.swap(i0, i1); | ||||
i0++; | i0++; | ||||
i1--; | i1--; | ||||
b_swap = true; | b_swap = true; | ||||
@@ -286,6 +286,15 @@ public: | |||||
return tmp; | return tmp; | ||||
} | } | ||||
inline void swap(ptrdiff_t i, ptrdiff_t j) | |||||
{ | |||||
ASSERT(i >= 0 && i < m_count && j >= 0 && j < m_count, | |||||
"cannot swap elements %lld and %lld in array of size %lld", | |||||
(long long int)i, (long long int)j, (long long int)m_count); | |||||
std::swap(m_data[i], m_data[j]); | |||||
} | |||||
void remove(ptrdiff_t pos, ptrdiff_t todelete = 1) | void remove(ptrdiff_t pos, ptrdiff_t todelete = 1) | ||||
{ | { | ||||
ASSERT(todelete >= 0); | ASSERT(todelete >= 0); | ||||
@@ -1,13 +1,14 @@ | |||||
// | // | ||||
// Lol Engine | // Lol Engine | ||||
// | // | ||||
// Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net> | // Copyright: © 2010—2014 Sam Hocevar <sam@hocevar.net> | ||||
// (c) 2013-2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com> | // © 2013—2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com> | ||||
// (c) 2013-2014 Guillaume Bittoun <guillaume.bittoun@gmail.com> | // © 2013—2014 Guillaume Bittoun <guillaume.bittoun@gmail.com> | ||||
// This program is free software; you can redistribute it and/or | // | ||||
// modify it under the terms of the Do What The Fuck You Want To | // This library is free software; you can redistribute it and/or | ||||
// Public License, Version 2, as published by Sam Hocevar. See | // modify it under the terms of the Do What The Fuck You Want To | ||||
// http://www.wtfpl.net/ for more details. | // Public License, Version 2, as published by Sam Hocevar. See | ||||
// http://www.wtfpl.net/ for more details. | |||||
// | // | ||||
#pragma once | #pragma once | ||||
@@ -94,7 +95,7 @@ protected: | |||||
for (int i = 0; i < N; ++i) | for (int i = 0; i < N; ++i) | ||||
for (int j = i + 1; j < N; ++j) | for (int j = i + 1; j < N; ++j) | ||||
if (pos[traversal_order[i]] < pos[traversal_order[j]]) | if (pos[traversal_order[i]] < pos[traversal_order[j]]) | ||||
std::swap(traversal_order[i], traversal_order[j]); | traversal_order.swap(i, j); | ||||
/* Get the position in world coordinates, too */ | /* Get the position in world coordinates, too */ | ||||
@@ -1,11 +1,12 @@ | |||||
// | // | ||||
// Lol Engine | // Lol Engine — unit tests | ||||
// | // | ||||
// Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net> | // Copyright: © 2010—2014 Sam Hocevar <sam@hocevar.net> | ||||
// This program is free software; you can redistribute it and/or | // | ||||
// modify it under the terms of the Do What The Fuck You Want To | // This program is free software; you can redistribute it and/or | ||||
// Public License, Version 2, as published by Sam Hocevar. See | // modify it under the terms of the Do What The Fuck You Want To | ||||
// http://www.wtfpl.net/ for more details. | // Public License, Version 2, as published by Sam Hocevar. See | ||||
// http://www.wtfpl.net/ for more details. | |||||
// | // | ||||
#include <lol/engine-internal.h> | #include <lol/engine-internal.h> | ||||
@@ -159,7 +160,7 @@ lolunit_declare_fixture(ArrayTest) | |||||
a.push(10, 20); | a.push(10, 20); | ||||
a.push(30, 40); | a.push(30, 40); | ||||
std::swap(a[0], a[1]); | a.swap(0, 1); | ||||
lolunit_assert_equal(30, a[0].m1); | lolunit_assert_equal(30, a[0].m1); | ||||
lolunit_assert_equal(40, a[0].m2); | lolunit_assert_equal(40, a[0].m2); | ||||