From 92c2f69d0708fe4780d0f165a16888bcfce1e656 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 28 Feb 2014 13:36:09 +0000 Subject: [PATCH] algorithm: move Array::Sort out of the base headers because their implementation requires stuff from lol/math, too. --- src/Makefile.am | 3 ++ src/core.h | 1 + src/lol/algorithm/all.h | 17 ++++++ src/lol/algorithm/sort.h | 101 ++++++++++++++++++++++++++++++++++++ src/lol/base/array.h | 78 ++-------------------------- src/lolcore.vcxproj | 2 + src/lolcore.vcxproj.filters | 9 ++++ 7 files changed, 137 insertions(+), 74 deletions(-) create mode 100644 src/lol/algorithm/all.h create mode 100644 src/lol/algorithm/sort.h diff --git a/src/Makefile.am b/src/Makefile.am index 427b3b2c..61140b11 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -41,6 +41,9 @@ liblolcore_headers = \ lol/math/geometry.h lol/math/interp.h lol/math/rand.h \ lol/math/constants.h \ \ + lol/algorithm/all.h \ + lol/algorithm/sort.h \ + \ lol/sys/all.h \ lol/sys/init.h lol/sys/file.h lol/sys/thread.h lol/sys/atomic.h \ lol/sys/timer.h \ diff --git a/src/core.h b/src/core.h index 5978dce4..07c67fb7 100644 --- a/src/core.h +++ b/src/core.h @@ -154,6 +154,7 @@ static inline int isnan(float f) // Base types #include #include +#include #include #include #include diff --git a/src/lol/algorithm/all.h b/src/lol/algorithm/all.h new file mode 100644 index 00000000..a47b6395 --- /dev/null +++ b/src/lol/algorithm/all.h @@ -0,0 +1,17 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2014 Sam Hocevar +// 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. +// + +#if !defined __LOL_ALGORITHM_ALL_H__ +#define __LOL_ALGORITHM_ALL_H__ + +#include + +#endif // __LOL_ALGORITHM_ALL_H__ + diff --git a/src/lol/algorithm/sort.h b/src/lol/algorithm/sort.h new file mode 100644 index 00000000..895eb846 --- /dev/null +++ b/src/lol/algorithm/sort.h @@ -0,0 +1,101 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2014 Sam Hocevar +// (c) 2013-2014 Benjamin "Touky" Huet +// 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. +// + +#if !defined __LOL_ALGORITHM_SORT_H__ +#define __LOL_ALGORITHM_SORT_H__ + +#include + +namespace lol +{ + +template +void ArrayBase::Sort(int sort) +{ + int s = 0; + // Classic bubble + if (s++ == sort) + { + int d = 1; + for (int i = 0; i < Count() - 1; i = lol::max(i + d, 0)) + { + if (m_data[i] < m_data[i + 1] || i <= 0) + d = 1; + if (m_data[i + 1] < m_data[i]) + { + Swap(i, i + 1); + d = -1; + } + } + } + // Quick sort with swap + else if (s++ == sort) + { + SortQuickSwap(0, Count()); + } +} + +template +void ArrayBase::SortQuickSwap(int start, int stop) +{ + int m[3] = + { + rand(start, stop), + rand(start, stop), + rand(start, stop) + }; + + for (int i = 0; i < 2; ) + { + if (m_data[m[i+1]] < m_data[m[i]]) + { + int mt = m[i+1]; + m[i+1] = m[i]; + m[i] = mt; + i = 0; + } + else + i++; + } + //actual stuff + T median = m_data[m[1]]; + int i0 = start; + int i1 = stop - 1; + bool swap = false; + while (i0 < i1) + { + if (m_data[i0] >= median && m_data[i1] < median) + { + Swap(i0, i1); + i0++; + i1--; + swap = true; + } + else + { + if (m_data[i0] < median) + i0++; + if (m_data[i1] >= median) + i1--; + } + } + if (stop - start == 1 || !swap) + return; + if (start < i0) + SortQuickSwap(start, i0); + if (i0 < stop) + SortQuickSwap(i0, stop); +} + +} /* namespace lol */ + +#endif // __LOL_ALGORITHM_SORT_H__ + diff --git a/src/lol/base/array.h b/src/lol/base/array.h index ed739909..028a0ac6 100644 --- a/src/lol/base/array.h +++ b/src/lol/base/array.h @@ -1,7 +1,8 @@ // // Lol Engine // -// Copyright: (c) 2010-2013 Sam Hocevar +// Copyright: (c) 2010-2014 Sam Hocevar +// (c) 2013-2014 Benjamin "Touky" Huet // 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 @@ -331,81 +332,10 @@ public: m_reserved = toreserve; } - void Sort(int sort) - { - int s = 0; - //Classic bubble - if (s++ == sort) - { - int d = 1; - for (int i = 0; i < Count() - 1; i = lol::max(i + d, 0)) - { - if (m_data[i] < m_data[i + 1] || i <= 0) - d = 1; - if (m_data[i + 1] < m_data[i]) - { - Swap(i, i + 1); - d = -1; - } - } - } - //Quick sort with swap - else if (s++ == sort) - { - SortQuickSwap(0, Count()); - } - } - void SortQuickSwap(int start, int max) - { - int m[3] = { start + rand(max - start), - start + rand(max - start), - start + rand(max - start) }; - int i = 0; - while (i < 2) - { - if (m_data[m[i+1]] < m_data[m[i]]) - { - int mt = m[i+1]; - m[i+1] = m[i]; - m[i] = mt; - i = 0; - } - else - i++; - } - //actual stuff - T median = m_data[m[1]]; - int i0 = start; - int i1 = max - 1; - bool swap = false; - while (i0 < i1) - { - if (m_data[i0] >= median && - m_data[i1] < median) - { - Swap(i0, i1); - i0++; - i1--; - swap = true; - } - else - { - if (m_data[i0] < median) - i0++; - if (m_data[i1] >= median) - i1--; - } - } - if (max - start == 1 || !swap) - return; - if (start < i0) - SortQuickSwap(start, i0); - if (i0 < max) - SortQuickSwap(i0, max); - } + void Sort(int sort); + void SortQuickSwap(int start, int stop); public: - inline int Count() const { return m_count; } inline int Bytes() const { return m_count * sizeof(Element); } diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index 538d2754..1d17f1d9 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -269,6 +269,8 @@ + + diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index 3eaae1ce..ca071cf6 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -31,6 +31,9 @@ {0edcf1a5-3c9c-4425-918c-aa2cbebc51c1} + + {39f6f872-186f-48af-8e87-bcceb06b1b21} + {f25b3187-b24c-469a-b038-5a968eaa83f6} @@ -357,6 +360,12 @@ platform\sdl + + lol\algorithm + + + lol\algorithm + lol\image