~ivantis/armagetronad/sty+ct+ivantis

« back to all changes in this revision

Viewing changes to src/tools/tLinkedList.cpp

  • Committer: ivantis
  • Date: 2008-09-09 21:33:18 UTC
  • Revision ID: ivantis@ivantis.net-20080909213318-k43y6yuq0zd6wbsa
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
*************************************************************************
 
4
 
 
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
 
6
Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
 
7
 
 
8
**************************************************************************
 
9
 
 
10
This program is free software; you can redistribute it and/or
 
11
modify it under the terms of the GNU General Public License
 
12
as published by the Free Software Foundation; either version 2
 
13
of the License, or (at your option) any later version.
 
14
 
 
15
This program is distributed in the hope that it will be useful,
 
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
GNU General Public License for more details.
 
19
 
 
20
You should have received a copy of the GNU General Public License
 
21
along with this program; if not, write to the Free Software
 
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
23
  
 
24
***************************************************************************
 
25
 
 
26
*/
 
27
 
 
28
#include "tLinkedList.h"
 
29
 
 
30
void tListItemBase::Remove(){
 
31
    if (anchor){
 
32
        *anchor      = next;
 
33
        if (next)
 
34
            next->anchor = anchor;
 
35
        anchor       = NULL;
 
36
        next         = NULL;
 
37
    }
 
38
}
 
39
 
 
40
void  tListItemBase::Insert(tListItemBase *&a){
 
41
    if (anchor)
 
42
        Remove();
 
43
    anchor = &a;
 
44
    next   =  a;
 
45
    a      =  this;
 
46
    if (next)
 
47
        next->anchor = &next;
 
48
}
 
49
 
 
50
int  tListItemBase::Len(){
 
51
    int ret=0;
 
52
    tListItemBase* x=this;
 
53
    while (x){
 
54
        ret++;
 
55
        x = x->next;
 
56
    }
 
57
    return ret;
 
58
}
 
59
 
 
60
void tListItemBase::Sort( Comparator* compare )
 
61
{
 
62
    // early return statements: empty list or single element in list
 
63
    if ( !this || !next )
 
64
    {
 
65
        return;
 
66
    }
 
67
 
 
68
    tListItemBase* middle = this;
 
69
    {
 
70
        // locate the middle of the list
 
71
        tListItemBase* run = *anchor;
 
72
        while ( run )
 
73
        {
 
74
            middle = middle->next;
 
75
            run          = run->next;
 
76
            if ( run )
 
77
            {
 
78
                run = run->next;
 
79
            }
 
80
        }
 
81
    }
 
82
 
 
83
    // split the list in the middle
 
84
    *middle->anchor = NULL;
 
85
    middle->anchor = &middle;
 
86
 
 
87
    // retrieve the anchor of the first half list
 
88
    tListItemBase*& first = *anchor;
 
89
 
 
90
    // sort the two half lists
 
91
    first->Sort( compare );
 
92
    middle->Sort( compare );
 
93
 
 
94
    // merge the lists
 
95
    {
 
96
        tListItemBase** run = &first;
 
97
        while ( middle )
 
98
        {
 
99
            // find the correct place for middle
 
100
            while ( *run && compare( *run, middle ) > 0 )
 
101
                run = &(*run)->next;
 
102
 
 
103
            // remove middle from the second list; care needs to be taken because middle->remove() would modify middle.
 
104
            tListItemBase* insert = middle;
 
105
            insert->Remove();
 
106
 
 
107
            // insert it into the first list
 
108
            insert->Insert( *run );
 
109
            run = &insert->next;
 
110
        }
 
111
    }
 
112
 
 
113
    // done!
 
114
}
 
115