~ubuntu-branches/ubuntu/oneiric/blobandconquer/oneiric

« back to all changes in this revision

Viewing changes to src/cplusplus/CList.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Guus Sliepen
  • Date: 2008-06-15 12:04:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080615120429-5ss7cbb4z9mpywj5
Tags: 0.95-1
New upstream release. Closes: #486310

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2006 Parallel Realities
 
3
 
 
4
This program is free software; you can redistribute it and/or
 
5
modify it under the terms of the GNU General Public License
 
6
as published by the Free Software Foundation; either version 2
 
7
of the License, or (at your option) any later version.
 
8
 
 
9
This program is distributed in the hope that it will be useful,
 
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
12
 
 
13
See the GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software
 
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 
 
19
*/
 
20
 
 
21
#include "../headers.h"
 
22
 
 
23
List::List()
 
24
{
 
25
        linkHead.previous = &linkHead;
 
26
 
 
27
        linkTail = &linkHead;
 
28
 
 
29
        name = "Unnamed List";
 
30
        
 
31
        size = 0;
 
32
}
 
33
 
 
34
List::~List()
 
35
{
 
36
        clear();
 
37
}
 
38
 
 
39
void List::setName(const char *name)
 
40
{
 
41
        this->name = name;
 
42
}
 
43
 
 
44
void List::add(Linkable *link)
 
45
{
 
46
        linkTail->next = link;
 
47
        link->previous = linkTail;
 
48
        linkTail = link;
 
49
        
 
50
        linkTail->next = NULL;
 
51
        
 
52
        size++;
 
53
}
 
54
 
 
55
void List::remove(Linkable *link)
 
56
{
 
57
        if (linkTail == link)
 
58
        {
 
59
                linkTail = link->previous;
 
60
        }
 
61
 
 
62
        link->previous->next = link->next;
 
63
 
 
64
        if (link->next != NULL)
 
65
        {
 
66
                link->next->previous = link->previous;
 
67
        }
 
68
        
 
69
        link->next = link->previous = NULL;
 
70
        linkTail->next = NULL;
 
71
        
 
72
        size--;
 
73
        
 
74
        delete link;
 
75
}
 
76
 
 
77
void List::clear()
 
78
{
 
79
        //debug(("%s::clear()\n", name.getText()));
 
80
        
 
81
        int count = 0;
 
82
 
 
83
        Linkable *link, *link2;
 
84
 
 
85
        for (link = linkHead.next ; link != NULL ; link = link2)
 
86
        {
 
87
                link2 = link->next;
 
88
                delete link;
 
89
                count++;
 
90
        }
 
91
 
 
92
        linkHead.next = NULL;
 
93
        linkTail = &linkHead;
 
94
        
 
95
        size = 0;
 
96
        
 
97
        //debug(("%s::clear() - Done\n", name.getText()));
 
98
}
 
99
 
 
100
Linkable *List::getFirstElement()
 
101
{
 
102
        return linkHead.next;
 
103
}
 
104
 
 
105
int List::getSize()
 
106
{
 
107
        return size;
 
108
}
 
109
 
 
110
Linkable *List::getElement(int i)
 
111
{
 
112
        if (i >= size)
 
113
        {
 
114
                printf("WARNING - List::getItem %d exceeds max element %d! Returning last!\n", i, size - 1);
 
115
                i = size - 1;
 
116
        }
 
117
        
 
118
        int count = 0;
 
119
        
 
120
        for (Linkable *link = linkHead.next ; link != NULL ; link = link->next)
 
121
        {
 
122
                if (count == i)
 
123
                {
 
124
                        return link;
 
125
                }
 
126
                
 
127
                count++;
 
128
        }
 
129
        
 
130
        printf("WARNING - List::getItem returning NULL!\n");
 
131
        
 
132
        return NULL;
 
133
}
 
134
 
 
135
Linkable *List::getHead()
 
136
{
 
137
        return &linkHead;
 
138
}
 
139
 
 
140
Linkable *List::getTail()
 
141
{
 
142
        return linkTail;
 
143
}