Ver código fonte

base: reintroduce array::swap.

It merely performs an std::swap() on both array elements, but it’s
apparently convenient to have.
undefined
Sam Hocevar 9 anos atrás
pai
commit
51b4210361
4 arquivos alterados com 30 adições e 19 exclusões
  1. +3
    -3
      src/lol/algorithm/sort.h
  2. +9
    -0
      src/lol/base/array.h
  3. +10
    -9
      src/lol/math/noise/simplex.h
  4. +8
    -7
      src/t/base/array.cpp

+ 3
- 3
src/lol/algorithm/sort.h Ver arquivo

@@ -30,7 +30,7 @@ void array_base<T, ARRAY>::shuffle()
while (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;
if (m_data[i + 1] < m_data[i])
{
std::swap(m_data[i], m_data[i + 1]);
this->swap(i, i + 1);
d = -1;
}
}
@@ -103,7 +103,7 @@ static void quick_swap_sort(array_base<T, ARRAY> &a,
{
if (!(a[i0] < median) && a[i1] < median)
{
std::swap(a[i0], a[i1]);
a.swap(i0, i1);
i0++;
i1--;
b_swap = true;


+ 9
- 0
src/lol/base/array.h Ver arquivo

@@ -286,6 +286,15 @@ public:
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)
{
ASSERT(todelete >= 0);


+ 10
- 9
src/lol/math/noise/simplex.h Ver arquivo

@@ -1,13 +1,14 @@
//
// Lol Engine
// Lol Engine
//
// Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
// (c) 2013-2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
// (c) 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
// Public License, Version 2, as published by Sam Hocevar. See
// http://www.wtfpl.net/ for more details.
// Copyright: © 2010—2014 Sam Hocevar <sam@hocevar.net>
// © 2013—2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
// © 2013—2014 Guillaume Bittoun <guillaume.bittoun@gmail.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://www.wtfpl.net/ for more details.
//

#pragma once
@@ -94,7 +95,7 @@ protected:
for (int i = 0; i < N; ++i)
for (int j = i + 1; j < N; ++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 */


+ 8
- 7
src/t/base/array.cpp Ver arquivo

@@ -1,11 +1,12 @@
//
// Lol Engine
// Lol Engine — unit tests
//
// Copyright: (c) 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
// Public License, Version 2, as published by Sam Hocevar. See
// http://www.wtfpl.net/ for more details.
// 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
// Public License, Version 2, as published by Sam Hocevar. See
// http://www.wtfpl.net/ for more details.
//

#include <lol/engine-internal.h>
@@ -159,7 +160,7 @@ lolunit_declare_fixture(ArrayTest)
a.push(10, 20);
a.push(30, 40);

std::swap(a[0], a[1]);
a.swap(0, 1);

lolunit_assert_equal(30, a[0].m1);
lolunit_assert_equal(40, a[0].m2);


Carregando…
Cancelar
Salvar