/*==LICENSE==*

CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011  Cyan Worlds, Inc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Additional permissions under GNU GPL version 3 section 7

If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.

You can contact Cyan Worlds, Inc. by email legal@cyan.com
 or by snail mail at:
      Cyan Worlds, Inc.
      14617 N Newport Hwy
      Mead, WA   99021

*==LICENSE==*/

#include "HeadSpin.h"
#include "hsMemory.h"
#include "hsRadixSort.h"

hsRadixSort::hsRadixSort()
{
    HSMemory::Clear(fHeads, 256*sizeof(Elem*));
    HSMemory::Clear(fTails, 256*sizeof(Elem*));
}

void hsRadixSort::ILink(Elem*& head, Elem*& tail, int i)
{
    if( fHeads[i] )
    {
        if( !head )
            head = fHeads[i];
        else
            tail->fNext = fHeads[i];
        tail = fTails[i];
    }
    fHeads[i] = fTails[i] = nil;
}

void hsRadixSort::ISlot(Elem* in, int i)
{
    if( !fTails[i] )
        fHeads[i] = in;
    else
        fTails[i]->fNext = in;
    in->fNext = nil;
    fTails[i] = in;
}

void hsRadixSort::ICollapse()
{
    Elem* head = nil;
    Elem* tail = nil;

    int i;
    for( i = 0; i < 256; i++ )
        ILink(head, tail, i);

    fList = head;
}

void hsRadixSort::IUnPackSignedInt()
{
    Elem* head = nil;
    Elem* tail = nil;

    int i;
    for( i = 128; i < 256; i++ )
        ILink(head, tail, i);

    for( i = 0; i < 128; i++ )
        ILink(head, tail, i);

    fList = head;
}

void hsRadixSort::IUnPackFloat()
{
    Elem* head = nil;
    Elem* tail = nil;

    int i;
    for( i = 128; i < 256; i++ )
        ILink(head, tail, i);
    fList = head;
    head = tail;
    tail = fList;
    IReverse();

    for( i = 0; i < 128; i++ )
        ILink(head, tail, i);

    fList = head;
}

void hsRadixSort::IReverse()
{
    if( !(fList && fList->fNext) )
        return;

    Elem* p = fList->fNext;
    fList->fNext = nil;
    while( p )
    {
        Elem* n = p->fNext;
        p->fNext = fList;
        fList = p;
        p = n;
    }
}

hsRadixSort::Elem* hsRadixSort::Sort(Elem* inList, uint32_t flags)
{
    if( !(inList && inList->fNext) )
        return inList;

    fList = inList;

    Elem* p;
    Elem* n;

    for( p = fList, n = p->fNext; n; p = n, n = p->fNext )
        ISlot(p, p->fKey.fLong & 0xff);
    ISlot(p, p->fKey.fLong & 0xff);

    ICollapse();

    for( p = fList, n = p->fNext; n; p = n, n = p->fNext )
        ISlot(p, (p->fKey.fLong >> 8) & 0xff);
    ISlot(p, (p->fKey.fLong >> 8) & 0xff);

    ICollapse();

    for( p = fList, n = p->fNext; n; p = n, n = p->fNext )
        ISlot(p, (p->fKey.fLong >> 16) & 0xff);
    ISlot(p, (p->fKey.fLong >> 16) & 0xff);

    ICollapse();

    for( p = fList, n = p->fNext; n; p = n, n = p->fNext )
        ISlot(p, (p->fKey.fLong >> 24) & 0xff);
    ISlot(p, (p->fKey.fLong >> 24) & 0xff);

    if( flags & kSignedInt )
        IUnPackSignedInt();
    else if( flags & kUnsigned )
        ICollapse();
    else
        IUnPackFloat();

    if( flags & kReverse )
        IReverse();

    return fList;
}
