1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Update <sortix/kernel/sortedlist.h> to current coding conventions.

This commit is contained in:
Jonas 'Sortie' Termansen 2014-01-04 01:14:35 +01:00
parent d5b1d4853d
commit 263cc21058

View file

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014.
This file is part of Sortix.
@ -23,35 +23,32 @@
*******************************************************************************/
#ifndef SORTIX_SORTEDLIST_H
#define SORTIX_SORTEDLIST_H
#ifndef SIZE_MAX
#error Define __STDC_LIMIT_MACROS before including <stdint.h>
#endif
#ifndef INCLUDE_SORTIX_KERNEL_SORTEDLIST_H
#define INCLUDE_SORTIX_KERNEL_SORTEDLIST_H
#include <assert.h>
#include <stdint.h>
namespace Sortix
namespace Sortix {
template <class T> class SortedList
{
template <class T> class SortedList
{
public:
public:
// GCC appears to be broken as the const in the function pointer typedef
// below is ignored for some reason. Is it a compiler bug?
typedef int (*compare_t)(const T, const T);
private:
static const int FLAG_SORTED = (1<<0);
private:
static const int FLAG_SORTED = 1 << 0;
private:
private:
T* list;
size_t listused;
size_t listlength;
compare_t comparator;
unsigned flags;
public:
public:
SortedList(compare_t thecomparator = NULL) :
list(NULL),
listused(0),
@ -67,7 +64,7 @@ namespace Sortix
Clear();
}
public:
public:
void SetComparator(compare_t thecomparator)
{
comparator = thecomparator;
@ -77,9 +74,7 @@ namespace Sortix
void Clear()
{
for ( size_t i = 0; i < listused; i++ )
{
list[i].~T();
}
listused = 0;
flags |= FLAG_SORTED;
@ -99,7 +94,7 @@ namespace Sortix
bool IsSorted()
{
return (flags & FLAG_SORTED);
return flags & FLAG_SORTED;
}
void Sort(compare_t thecomparator)
@ -110,7 +105,8 @@ namespace Sortix
void Sort()
{
if ( !listused || (flags & FLAG_SORTED) ) { return; }
if ( !listused || (flags & FLAG_SORTED) )
return;
MergeSort(0, listused-1);
@ -124,7 +120,8 @@ namespace Sortix
bool Add(T t)
{
if ( listused == listlength && !Expand() ) { return false; }
if ( listused == listlength && !Expand() )
return false;
list[listused++] = t;
@ -135,7 +132,8 @@ namespace Sortix
T Get(size_t index)
{
if ( !(flags & FLAG_SORTED) ) { Sort(); }
if ( !(flags & FLAG_SORTED) )
Sort();
return list[index];
}
@ -148,7 +146,8 @@ namespace Sortix
T Remove(size_t index)
{
if ( !(flags & FLAG_SORTED) ) { Sort(); }
if ( !(flags & FLAG_SORTED) )
Sort();
assert(index < listused);
// TODO: It may be possible to further speed up removal by delaying
@ -174,10 +173,14 @@ namespace Sortix
// Returns the index of the element being searched for using the given
// comparator, or returns SIZE_MAX if not found.
template <class Searchee> size_t Search(int (*searcher)(const T t, const Searchee searchee), const Searchee searchee)
template <class Searchee>
size_t Search(int (*searcher)(const T t, const Searchee searchee),
const Searchee searchee)
{
if ( !listused ) { return SIZE_MAX; }
if ( !(flags & FLAG_SORTED) ) { Sort(); }
if ( !listused )
return SIZE_MAX;
if ( !(flags & FLAG_SORTED) )
Sort();
size_t minindex = 0;
size_t maxindex = listused-1;
@ -187,21 +190,25 @@ namespace Sortix
size_t tryindex = (minindex + maxindex) / 2;
const T& t = list[tryindex];
int relation = searcher(t, searchee);
if ( relation == 0 ) { return tryindex; }
if ( relation < 0 ) { minindex = tryindex+1; }
if ( relation > 0 ) { maxindex = tryindex-1; }
if ( relation == 0 )
return tryindex;
if ( relation < 0 )
minindex = tryindex+1;
if ( relation > 0 )
maxindex = tryindex-1;
} while ( maxindex + 1 != minindex );
return SIZE_MAX;
}
private:
private:
bool Expand()
{
size_t newsize = (listused == 0 ) ? 4 : listlength*2;
T* newlist = new T[newsize*2];
if ( !newlist ) { return false; }
if ( !newlist )
return false;
for ( size_t i = 0; i < listused; i++ )
{
@ -233,9 +240,8 @@ namespace Sortix
T* destlist = GetDestList();
do
{
list[minindex] = destlist[minindex];
} while ( minindex++ != maxindex );
while ( minindex++ != maxindex );
}
void MergeSortInternal(size_t minindex, size_t maxindex)
@ -313,7 +319,8 @@ namespace Sortix
}
}
};
}
};
} // namespace Sortix
#endif