Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 22:38:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* sortix/kernel/sortedlist.h
|
|
|
|
* A container that ensures its elements are always sorted when they are
|
|
|
|
* accessed. It also provides binary search.
|
|
|
|
*/
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2022-07-07 23:09:15 +00:00
|
|
|
#ifndef _INCLUDE_SORTIX_KERNEL_SORTEDLIST_H
|
|
|
|
#define _INCLUDE_SORTIX_KERNEL_SORTEDLIST_H
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2012-09-22 12:57:20 +00:00
|
|
|
#include <assert.h>
|
2014-02-25 21:39:57 +00:00
|
|
|
#include <stddef.h>
|
2014-01-04 00:14:35 +00:00
|
|
|
#include <stdint.h>
|
2012-09-22 12:57:20 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
namespace Sortix {
|
|
|
|
|
|
|
|
template <class T> class SortedList
|
2011-10-25 16:21:06 +00:00
|
|
|
{
|
2014-01-04 00:14:35 +00:00
|
|
|
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:
|
|
|
|
T* list;
|
|
|
|
size_t listused;
|
|
|
|
size_t listlength;
|
|
|
|
compare_t comparator;
|
|
|
|
unsigned flags;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SortedList(compare_t thecomparator = NULL) :
|
|
|
|
list(NULL),
|
|
|
|
listused(0),
|
|
|
|
listlength(0),
|
|
|
|
comparator(thecomparator),
|
|
|
|
flags(0)
|
2011-10-25 16:21:06 +00:00
|
|
|
{
|
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
~SortedList()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
public:
|
|
|
|
void SetComparator(compare_t thecomparator)
|
|
|
|
{
|
|
|
|
comparator = thecomparator;
|
|
|
|
flags &= ~FLAG_SORTED;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
for ( size_t i = 0; i < listused; i++ )
|
|
|
|
list[i].~T();
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
listused = 0;
|
|
|
|
flags |= FLAG_SORTED;
|
|
|
|
delete[] list;
|
|
|
|
list = NULL;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
size_t Length()
|
|
|
|
{
|
|
|
|
return listused;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
bool Empty()
|
|
|
|
{
|
|
|
|
return !listused;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
bool IsSorted()
|
|
|
|
{
|
|
|
|
return flags & FLAG_SORTED;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
void Sort(compare_t thecomparator)
|
|
|
|
{
|
|
|
|
comparator = thecomparator;
|
|
|
|
Sort();
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
void Sort()
|
|
|
|
{
|
|
|
|
if ( !listused || (flags & FLAG_SORTED) )
|
|
|
|
return;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
MergeSort(0, listused-1);
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
flags |= FLAG_SORTED;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
void Invalidate()
|
|
|
|
{
|
|
|
|
flags &= ~FLAG_SORTED;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
bool Add(T t)
|
|
|
|
{
|
|
|
|
if ( listused == listlength && !Expand() )
|
|
|
|
return false;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
list[listused++] = t;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
flags &= ~FLAG_SORTED;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
T Get(size_t index)
|
|
|
|
{
|
|
|
|
if ( !(flags & FLAG_SORTED) )
|
|
|
|
Sort();
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
return list[index];
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
// Accesses the elements, but the order might not be sorted.
|
|
|
|
T GetUnsorted(size_t index)
|
|
|
|
{
|
|
|
|
return list[index];
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
T Remove(size_t index)
|
|
|
|
{
|
|
|
|
if ( !(flags & FLAG_SORTED) )
|
|
|
|
Sort();
|
|
|
|
assert(index < listused);
|
2012-01-21 00:31:14 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
// TODO: It may be possible to further speed up removal by delaying
|
|
|
|
// the expensive memory copy operation.
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
T result = list[index];
|
|
|
|
list[index].~T();
|
|
|
|
for ( size_t i = index+1; i < listused; i++ )
|
2011-10-25 16:21:06 +00:00
|
|
|
{
|
2014-01-04 00:14:35 +00:00
|
|
|
list[i-1] = list[i];
|
|
|
|
list[i].~T();
|
2011-10-25 16:21:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
listused--;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
return result;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
size_t Search(T searchee)
|
|
|
|
{
|
|
|
|
return Search(comparator, searchee);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
if ( !listused )
|
2011-10-25 16:21:06 +00:00
|
|
|
return SIZE_MAX;
|
2014-01-04 00:14:35 +00:00
|
|
|
if ( !(flags & FLAG_SORTED) )
|
|
|
|
Sort();
|
|
|
|
|
|
|
|
size_t minindex = 0;
|
|
|
|
size_t maxindex = listused-1;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
do
|
2011-10-25 16:21:06 +00:00
|
|
|
{
|
2014-01-04 00:14:35 +00:00
|
|
|
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;
|
|
|
|
} while ( maxindex + 1 != minindex );
|
|
|
|
|
|
|
|
return SIZE_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool Expand()
|
|
|
|
{
|
|
|
|
size_t newsize = (listused == 0 ) ? 4 : listlength*2;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
T* newlist = new T[newsize*2];
|
|
|
|
if ( !newlist )
|
|
|
|
return false;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
for ( size_t i = 0; i < listused; i++ )
|
|
|
|
{
|
|
|
|
newlist[i] = list[i];
|
|
|
|
list[i].~T();
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
delete[] list;
|
|
|
|
list = newlist;
|
|
|
|
listlength = newsize;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
T* GetDestList()
|
|
|
|
{
|
|
|
|
return list + listlength;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
void MergeSort(size_t minindex, size_t maxindex)
|
|
|
|
{
|
|
|
|
FixupMergeSort(minindex, maxindex);
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
void FixupMergeSort(size_t minindex, size_t maxindex)
|
|
|
|
{
|
|
|
|
MergeSortInternal(minindex, maxindex);
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
T* destlist = GetDestList();
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
do
|
|
|
|
list[minindex] = destlist[minindex];
|
|
|
|
while ( minindex++ != maxindex );
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
void MergeSortInternal(size_t minindex, size_t maxindex)
|
|
|
|
{
|
|
|
|
T* destlist = GetDestList();
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
if ( maxindex - minindex == 1 )
|
|
|
|
{
|
|
|
|
if ( 0 < comparator(list[minindex], list[maxindex]) )
|
2011-10-25 16:21:06 +00:00
|
|
|
{
|
2014-01-04 00:14:35 +00:00
|
|
|
destlist[minindex] = list[maxindex];
|
|
|
|
destlist[maxindex] = list[minindex];
|
|
|
|
list[minindex].~T();
|
|
|
|
list[maxindex].~T();
|
2011-10-25 16:21:06 +00:00
|
|
|
}
|
2014-01-04 00:14:35 +00:00
|
|
|
else
|
2011-10-25 16:21:06 +00:00
|
|
|
{
|
|
|
|
destlist[minindex] = list[minindex];
|
2014-01-04 00:14:35 +00:00
|
|
|
destlist[maxindex] = list[maxindex];
|
2011-10-25 16:21:06 +00:00
|
|
|
list[minindex].~T();
|
2014-01-04 00:14:35 +00:00
|
|
|
list[maxindex].~T();
|
2011-10-25 16:21:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
if ( minindex == maxindex )
|
|
|
|
{
|
|
|
|
destlist[minindex] = list[minindex];
|
|
|
|
list[minindex].~T();
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
const size_t asize = (maxindex - minindex + 1) / 2 + 1;
|
|
|
|
const size_t bsize = (maxindex - minindex) / 2;
|
|
|
|
const size_t astart = minindex;
|
|
|
|
const size_t bstart = minindex + asize;
|
|
|
|
const size_t aend = minindex + asize - 1;
|
|
|
|
const size_t bend = minindex + asize + bsize - 1;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
FixupMergeSort(astart, aend);
|
|
|
|
FixupMergeSort(bstart, bend);
|
|
|
|
|
|
|
|
destlist += minindex;
|
|
|
|
|
|
|
|
size_t aindex = astart;
|
|
|
|
size_t bindex = bstart;
|
2011-10-25 16:21:06 +00:00
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
while ( aindex <= aend && bindex <= bend )
|
|
|
|
{
|
|
|
|
int relation = comparator(list[aindex], list[bindex]);
|
|
|
|
if ( relation <= 0 )
|
2011-10-25 16:21:06 +00:00
|
|
|
{
|
|
|
|
*destlist++ = list[aindex];
|
|
|
|
list[aindex++].~T();
|
|
|
|
}
|
2014-01-04 00:14:35 +00:00
|
|
|
else
|
2011-10-25 16:21:06 +00:00
|
|
|
{
|
|
|
|
*destlist++ = list[bindex];
|
|
|
|
list[bindex++].~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 00:14:35 +00:00
|
|
|
while ( aindex <= aend )
|
|
|
|
{
|
|
|
|
*destlist++ = list[aindex];
|
|
|
|
list[aindex++].~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( bindex <= bend )
|
|
|
|
{
|
|
|
|
*destlist++ = list[bindex];
|
|
|
|
list[bindex++].~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Sortix
|
2011-10-25 16:21:06 +00:00
|
|
|
|
|
|
|
#endif
|