File: LinkedList.cpp

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (221 lines) | stat: -rw-r--r-- 3,228 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
-------------------------------------------------------------------------
 CxxTest: A lightweight C++ unit testing library.
 Copyright (c) 2008 Sandia Corporation.
 This software is distributed under the LGPL License v3
 For more information, see the COPYING file in the top CxxTest directory.
 Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
 the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
*/

#ifndef __cxxtest__LinkedList_cpp__
#define __cxxtest__LinkedList_cpp__

#include <cxxtest/LinkedList.h>

namespace CxxTest
{
List GlobalFixture::_list = { 0, 0 };
List RealSuiteDescription::_suites = { 0, 0 };

void List::initialize()
{
    _head = _tail = 0;
}

Link *List::head()
{
    Link *l = _head;
    while (l && !l->active())
    {
        l = l->next();
    }
    return l;
}

const Link *List::head() const
{
    Link *l = _head;
    while (l && !l->active())
    {
        l = l->next();
    }
    return l;
}

Link *List::tail()
{
    Link *l = _tail;
    while (l && !l->active())
    {
        l = l->prev();
    }
    return l;
}

const Link *List::tail() const
{
    Link *l = _tail;
    while (l && !l->active())
    {
        l = l->prev();
    }
    return l;
}

bool List::empty() const
{
    return (_head == 0);
}

unsigned List::size() const
{
    unsigned count = 0;
    for (const Link *l = head(); l != 0; l = l->next())
    {
        ++ count;
    }
    return count;
}

Link *List::nth(unsigned n)
{
    Link *l = head();
    while (n --)
    {
        l = l->next();
    }
    return l;
}

void List::activateAll()
{
    for (Link *l = _head; l != 0; l = l->justNext())
    {
        l->setActive(true);
    }
}

void List::leaveOnly(const Link &link)
{
    for (Link *l = head(); l != 0; l = l->next())
    {
        if (l != &link)
        {
            l->setActive(false);
        }
    }
}

Link::Link() :
    _next(0),
    _prev(0),
    _active(true)
{
}

Link::~Link()
{
}

bool Link::active() const
{
    return _active;
}

void Link::setActive(bool value)
{
    _active = value;
}

Link * Link::justNext()
{
    return _next;
}

Link * Link::justPrev()
{
    return _prev;
}

Link * Link::next()
{
    Link *l = _next;
    while (l && !l->_active)
    {
        l = l->_next;
    }
    return l;
}

Link * Link::prev()
{
    Link *l = _prev;
    while (l && !l->_active)
    {
        l = l->_prev;
    }
    return l;
}

const Link * Link::next() const
{
    Link *l = _next;
    while (l && !l->_active)
    {
        l = l->_next;
    }
    return l;
}

const Link * Link::prev() const
{
    Link *l = _prev;
    while (l && !l->_active)
    {
        l = l->_prev;
    }
    return l;
}

void Link::attach(List &l)
{
    if (l._tail)
    {
        l._tail->_next = this;
    }

    _prev = l._tail;
    _next = 0;

    if (l._head == 0)
    {
        l._head = this;
    }
    l._tail = this;
}

void Link::detach(List &l)
{
    if (_prev)
    {
        _prev->_next = _next;
    }
    else
    {
        l._head = _next;
    }

    if (_next)
    {
        _next->_prev = _prev;
    }
    else
    {
        l._tail = _prev;
    }
}
}

#endif // __cxxtest__LinkedList_cpp__