Browse Source

algorithm: move Array::Sort out of the base headers because their implementation

requires stuff from lol/math, too.
undefined
Sam Hocevar 10 years ago
parent
commit
92c2f69d07
7 changed files with 137 additions and 74 deletions
  1. +3
    -0
      src/Makefile.am
  2. +1
    -0
      src/core.h
  3. +17
    -0
      src/lol/algorithm/all.h
  4. +101
    -0
      src/lol/algorithm/sort.h
  5. +4
    -74
      src/lol/base/array.h
  6. +2
    -0
      src/lolcore.vcxproj
  7. +9
    -0
      src/lolcore.vcxproj.filters

+ 3
- 0
src/Makefile.am View File

@@ -41,6 +41,9 @@ liblolcore_headers = \
lol/math/geometry.h lol/math/interp.h lol/math/rand.h \ lol/math/geometry.h lol/math/interp.h lol/math/rand.h \
lol/math/constants.h \ lol/math/constants.h \
\ \
lol/algorithm/all.h \
lol/algorithm/sort.h \
\
lol/sys/all.h \ lol/sys/all.h \
lol/sys/init.h lol/sys/file.h lol/sys/thread.h lol/sys/atomic.h \ lol/sys/init.h lol/sys/file.h lol/sys/thread.h lol/sys/atomic.h \
lol/sys/timer.h \ lol/sys/timer.h \


+ 1
- 0
src/core.h View File

@@ -154,6 +154,7 @@ static inline int isnan(float f)
// Base types // Base types
#include <lol/base/all.h> #include <lol/base/all.h>
#include <lol/math/all.h> #include <lol/math/all.h>
#include <lol/algorithm/all.h>
#include <lol/sys/all.h> #include <lol/sys/all.h>
#include <lol/gpu/all.h> #include <lol/gpu/all.h>
#include <lol/image/all.h> #include <lol/image/all.h>


+ 17
- 0
src/lol/algorithm/all.h View File

@@ -0,0 +1,17 @@
//
// Lol Engine
//
// 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.
//

#if !defined __LOL_ALGORITHM_ALL_H__
#define __LOL_ALGORITHM_ALL_H__

#include <lol/algorithm/sort.h>

#endif // __LOL_ALGORITHM_ALL_H__


+ 101
- 0
src/lol/algorithm/sort.h View File

@@ -0,0 +1,101 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
// (c) 2013-2014 Benjamin "Touky" Huet <huet.benjamin@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.
//

#if !defined __LOL_ALGORITHM_SORT_H__
#define __LOL_ALGORITHM_SORT_H__

#include <lol/base/array.h>

namespace lol
{

template<typename T, typename ARRAY>
void ArrayBase<T, ARRAY>::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<typename T, typename ARRAY>
void ArrayBase<T, ARRAY>::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__


+ 4
- 74
src/lol/base/array.h View File

@@ -1,7 +1,8 @@
// //
// Lol Engine // Lol Engine
// //
// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
// Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
// (c) 2013-2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
// This program is free software; you can redistribute it and/or // 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 // modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See // Public License, Version 2, as published by Sam Hocevar. See
@@ -331,81 +332,10 @@ public:
m_reserved = toreserve; 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: public:

inline int Count() const { return m_count; } inline int Count() const { return m_count; }
inline int Bytes() const { return m_count * sizeof(Element); } inline int Bytes() const { return m_count * sizeof(Element); }




+ 2
- 0
src/lolcore.vcxproj View File

@@ -269,6 +269,8 @@
<ClInclude Include="light.h" /> <ClInclude Include="light.h" />
<ClInclude Include="loldebug.h" /> <ClInclude Include="loldebug.h" />
<ClInclude Include="lolgl.h" /> <ClInclude Include="lolgl.h" />
<ClInclude Include="lol\algorithm\all.h" />
<ClInclude Include="lol\algorithm\sort.h" />
<ClInclude Include="lol\base\all.h" /> <ClInclude Include="lol\base\all.h" />
<ClInclude Include="lol\base\array.h" /> <ClInclude Include="lol\base\array.h" />
<ClInclude Include="lol\base\assert.h" /> <ClInclude Include="lol\base\assert.h" />


+ 9
- 0
src/lolcore.vcxproj.filters View File

@@ -31,6 +31,9 @@
<Filter Include="lol"> <Filter Include="lol">
<UniqueIdentifier>{0edcf1a5-3c9c-4425-918c-aa2cbebc51c1}</UniqueIdentifier> <UniqueIdentifier>{0edcf1a5-3c9c-4425-918c-aa2cbebc51c1}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="lol\algorithm">
<UniqueIdentifier>{39f6f872-186f-48af-8e87-bcceb06b1b21}</UniqueIdentifier>
</Filter>
<Filter Include="lol\image"> <Filter Include="lol\image">
<UniqueIdentifier>{f25b3187-b24c-469a-b038-5a968eaa83f6}</UniqueIdentifier> <UniqueIdentifier>{f25b3187-b24c-469a-b038-5a968eaa83f6}</UniqueIdentifier>
</Filter> </Filter>
@@ -357,6 +360,12 @@
<ClInclude Include="platform\sdl\sdlapp.h"> <ClInclude Include="platform\sdl\sdlapp.h">
<Filter>platform\sdl</Filter> <Filter>platform\sdl</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="lol\algorithm\all.h">
<Filter>lol\algorithm</Filter>
</ClInclude>
<ClInclude Include="lol\algorithm\sort.h">
<Filter>lol\algorithm</Filter>
</ClInclude>
<ClInclude Include="lol\image\color.h"> <ClInclude Include="lol\image\color.h">
<Filter>lol\image</Filter> <Filter>lol\image</Filter>
</ClInclude> </ClInclude>


Loading…
Cancel
Save