File: list.h

package info (click to toggle)
indigo 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,936 kB
  • sloc: ansic: 332,816; cpp: 169,470; python: 20,033; java: 13,701; cs: 9,979; asm: 8,475; sql: 6,743; xml: 6,354; javascript: 1,245; sh: 555; php: 506; makefile: 54
file content (229 lines) | stat: -rw-r--r-- 5,244 bytes parent folder | download
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
222
223
224
225
226
227
228
229
/****************************************************************************
 * Copyright (C) from 2009 to Present EPAM Systems.
 *
 * This file is part of Indigo toolkit.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ***************************************************************************/

#ifndef __list_h__
#define __list_h__

#include "base_cpp/pool.h"

namespace indigo
{

    template <typename T> class List
    {
    public:
        struct Elem
        {
            int prev;
            int next;
            T item;
        };

        explicit List() : _pool(new Pool<Elem>), _size(0), _head(-1), _tail(-1), _own_pool(true)
        {
        }

        explicit List(Pool<Elem>& pool) : _pool(&pool), _size(0), _head(-1), _tail(-1), _own_pool(false)
        {
        }

        ~List()
        {
            clear();
            if (_own_pool)
                delete _pool;
        }

        int add()
        {
            if (_size == 0)
            {
                _head = _pool->add();
                _tail = _head;

                Elem& elem = _pool->at(_head);

                elem.prev = -1;
                elem.next = -1;
            }
            else
            {
                int idx = _pool->add();
                Elem& elem = _pool->at(idx);

                _pool->at(_tail).next = idx;
                elem.prev = _tail;
                elem.next = -1;
                _tail = idx;
            }

            _size++;
            return _tail;
        }

        int add(const T& item)
        {
            int idx = add();

            _pool->at(idx).item = item;
            return idx;
        }

        int insertAfter(int existing)
        {
            _pool->at(existing); // will throw if the element does not exist

            int idx = _pool->add();
            Elem& ex = _pool->at(existing);
            Elem& elem = _pool->at(idx);

            elem.next = ex.next;
            elem.prev = existing;
            ex.next = idx;

            if (elem.next != -1)
                _pool->at(elem.next).prev = idx;

            if (_tail == existing)
                _tail = idx;

            _size++;
            return idx;
        }

        int insertBefore(int existing)
        {
            _pool->at(existing); // will throw if the element does not exist

            int idx = _pool->add();
            Elem& ex = _pool->at(existing);
            Elem& elem = _pool->at(idx);

            elem.prev = ex.prev;
            elem.next = existing;
            ex.prev = idx;

            if (elem.prev != -1)
                _pool->at(elem.prev).next = idx;

            if (_head == existing)
                _head = idx;

            _size++;
            return idx;
        }

        void remove(int idx)
        {
            Elem& elem = _pool->at(idx);

            if (elem.prev != -1)
                _pool->at(elem.prev).next = elem.next;
            else
                _head = elem.next;

            if (elem.next != -1)
                _pool->at(elem.next).prev = elem.prev;
            else
                _tail = elem.prev;

            _pool->remove(idx);
            _size--;
        }

        int size() const
        {
            return _size;
        }

        int begin() const
        {
            if (_head == -1)
                return _pool->end();

            return _head;
        }

        int end() const
        {
            return _pool->end();
        }

        int next(int idx) const
        {
            int res = _pool->at(idx).next;

            if (res == -1)
                return _pool->end();

            return res;
        }

        int prev(int idx) const
        {
            return _pool->at(idx).prev;
        }

        int tail() const
        {
            return _tail;
        }

        void clear()
        {
            if (_own_pool)
                _pool->clear();
            // or there may be other lists using the same _pool
            else
                while (_tail != -1)
                {
                    int iter = _tail;

                    _tail = _pool->at(iter).prev;
                    _pool->remove(iter);
                }

            _size = 0;
            _head = -1;
            _tail = -1;
        }

        T& operator[](int index) const
        {
            return _pool->at(index).item;
        }

        T& at(int index) const
        {
            return _pool->at(index).item;
        }

    protected:
        Pool<Elem>* _pool; // the element pool may be shared amongst lists
        int _size;
        int _head;
        int _tail;
        bool _own_pool;

    private:
        List(const List<T>&); // no implicit copy
    };

} // namespace indigo

#endif