summaryrefslogtreecommitdiffstats
path: root/src/util/list-container.h
blob: 70023875c400290c12a5a0b4aced25159e4e4765 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Inkscape::Util::ListContainer - encapsulates lists as STL containers,
 *                                 providing fast appending
 *
 * Authors:
 *   MenTaLguY <mental@rydia.net>
 *
 * Copyright (C) 2005 MenTaLguY
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#ifndef SEEN_INKSCAPE_UTIL_LIST_CONTAINER_H
#define SEEN_INKSCAPE_UTIL_LIST_CONTAINER_H

#include <limits>
#include "util/list.h"

namespace Inkscape {

namespace Util {

template <typename T>
class ListContainer {
public:
    /* default constructible */
    ListContainer() = default;

    /* assignable */
    ListContainer(ListContainer const &other) {
        *this = other;
    }
    ListContainer &operator=(ListContainer const &other) {
        clear();
        for ( const_iterator iter = other.begin() ; iter ; ++iter ) {
            push_back(*iter);
        }
        return *this;
    }
    void swap(ListContainer<T> &other) {
        std::swap(other._head, _head);
        std::swap(other._tail, _tail);
    }

    /* equality comparable */
    bool operator==(ListContainer const &other) const {
        const_iterator iter = _head;
        const_iterator other_iter = other._head;
        while ( iter && other_iter ) {
            if (!( *iter == *other_iter )) {
                return false;
            }
            ++iter;
            ++other_iter;
        }
        return !iter && !other_iter;
    }
    bool operator!=(ListContainer const &other) const {
        return !operator==(other);
    }

    /* lessthan comparable */
    bool operator<(ListContainer const &other) const {
        const_iterator iter = _head;
        const_iterator other_iter = other._head;
        while ( iter && other_iter ) {
            if ( *iter < *other_iter ) {
                return true;
            } else if ( *other_iter < *iter ) {
                return false;
            }
            ++iter;
            ++other_iter;
        }
        return bool(other_iter);
    }
    bool operator>=(ListContainer const &other) const {
        return !operator<(other);
    }

    /* container */
    typedef typename List<T>::value_type value_type;
    typedef List<T> iterator;
    typedef List<T const> const_iterator;
    typedef typename List<T>::reference reference;
    typedef typename List<T>::const_reference const_reference;
    typedef typename List<T>::pointer pointer;
    typedef typename List<T>::difference_type difference_type;
    typedef std::size_t size_type;

    iterator begin() { return _head; }
    const_iterator begin() const { return _head; }
    iterator end() { return iterator(); }
    const_iterator end() const { return const_iterator(); }
    size_type size() const {
        size_type size=0;
        for ( iterator iter = _head ; iter ; ++iter ) {
            size++;
        }
        return size;
    }
    size_type max_size() const {
        return std::numeric_limits<std::size_t>::max();
    }
    bool empty() const { return !_head; }

    /* sequence */
    ListContainer(size_type count, const_reference value) {
        for ( ; count ; --count ) {
            push_back(value);
        }
    }
    ListContainer(size_type count) {
        value_type default_value;
        for ( ; count ; --count ) {
            push_back(default_value);
        }
    }
    template <typename ForwardIterator>
    ListContainer(ForwardIterator i, ForwardIterator j) {
        for ( ; i != j ; ++i ) {
            push_back(*i);
        }
    }

    reference front() { return *_head; }
    const_reference front() const { return *_head; }

    iterator insert(const_iterator position, const_reference value) {
        if (position) {
            if ( position != _head ) {
                MutableList<T> added(value);
                MutableList<T> before=_before(position);
                set_rest(added, rest(before));
                set_rest(before, added);
                return added;
            } else {
                push_front(value);
                return _head;
            }
        } else {
            push_back(value);
            return _tail;
        }
    }
    void insert(const_iterator position, size_type count, const_reference value)
    {
        _insert_from_temp(position, ListContainer(count, value));
    }
    template <typename ForwardIterator>
    void insert(const_iterator position, ForwardIterator i, ForwardIterator j) {
        _insert_from_temp(position, ListContainer(i, j));
    }
    void erase(const_iterator position) {
        erase(position, rest(position));
    }
    void erase(const_iterator i, const_iterator j) {
        if ( i == _head ) {
            _head = static_cast<MutableList<T> &>(j);
            if ( !j || !rest(j) ) {
                _tail = _head;
            }
        } else {
            MutableList<T> before=_before(i);
            if (j) {
                set_rest(before, static_cast<MutableList<T> &>(j));
            } else {
                set_rest(before, MutableList<T>());
                _tail = before;
            }
        }
    }
    void clear() {
        _head = _tail = MutableList<T>();
    }
    void resize(size_type size, const_reference fill) {
        MutableList<T> before;
        MutableList<T> iter;
        for ( iter = _head ; iter && size ; ++iter ) {
            before = iter;
            size--;
        }
        if (size) {
            ListContainer temp(size, fill);
            if (empty()) {
                _head = temp._head;
                _tail = temp._tail;
            } else {
                set_rest(_tail, temp._head);
                _tail = temp._tail;
            }
        } else if (iter) {
            if (before) {
                set_rest(before, MutableList<T>());
                _tail = before;
            } else {
                _head = _tail = MutableList<T>();
            }
        }
    }
    void resize(size_type size) {
        resize(size, value_type());
    }

    /* front insertion sequence */
    void push_front(const_reference value) {
        if (_head) {
            _head = cons(value, _head);
        } else {
            _head = _tail = MutableList<T>(value);
        }
    }
    void pop_front() {
        if (_head) {
            _head = rest(_head);
            if (!_head) {
                _tail = _head;
            }
        }
    }

    /* back insertion sequence */
    reference back() { return *_tail; }
    const_reference back() const { return *_tail; }
    void push_back(const_reference value) {
        if (_tail) {
            MutableList<T> added(value);
            set_rest(_tail, added);
            _tail = added;
        } else {
            _head = _tail = MutableList<T>(value);
        }
    }
    // we're not required to provide pop_back if we can't
    // implement it efficiently

    /* additional */
    MutableList<T> detatchList() {
        MutableList<T> list=_head;
        _head = _tail = MutableList<T>();
        return list;
    }
    iterator insert_after(const_iterator pos, const_reference value) {
        MutableList<T> added(value);
        if (pos) {
            MutableList<T> before=static_cast<MutableList<T> &>(pos);
            set_rest(added, rest(before));
            set_rest(before, added);
            if ( _tail == before ) {
                _tail = added;
            }
        } else {
            push_front(value);
        }
    }
    void insert_after(const_iterator position, size_type count,
                      const_reference value)
    {
        _insert_after_from_temp(position, ListContainer(count, value));
    }
    template <typename ForwardIterator>
    void insert_after(const_iterator position,
                      ForwardIterator i, ForwardIterator j)
    {
        _insert_after_from_temp(position, ListContainer(i, j));
    }
    void erase_after(const_iterator position) {
        if (!position) {
            pop_front();
        } else {
            MutableList<T> before=static_cast<MutableList<T> &>(position);
            MutableList<T> removed=rest(before);
            set_rest(before, rest(removed));
            if ( removed == _tail ) {
                _tail = before;
            }
        }
    }

private:
    MutableList<T> _head;
    MutableList<T> _tail;

    MutableList<T> _before(const_iterator position) {
        for ( MutableList<T> iter = _head ; iter ; ++iter ) {
            if ( rest(iter) == position ) {
                return iter;
            }
        }
        return MutableList<T>();
    }
    void _insert_from_temp(const_iterator pos, ListContainer const &temp) {
        if (temp.empty()) {
            return;
        }
        if (empty()) { /* if empty, just take the whole thing */
            _head = temp._head;
            _tail = temp._tail;
        } else if (pos) {
            if ( pos == _head ) { /* prepend */
                set_rest(temp._tail, _head);
                _head = temp._head;
            } else { /* insert somewhere in the middle */
                MutableList<T> before=_before(pos);
                set_rest(temp._tail, static_cast<MutableList<T> &>(pos));
                set_rest(before, temp._head);
            }
        } else { /* append */
            set_rest(_tail, temp._head);
            _tail = temp._tail;
        }
    }
    void _insert_after_from_temp(const_iterator pos,
                                 ListContainer const &temp)
    {
        if (temp.empty()) {
            return;
        }
        if (empty()) { /* if empty, just take the whole thing */
            _head = temp._head;
            _tail = temp._tail;
        } else if (pos) {
            if ( pos == _tail ) { /* append */
                set_rest(_tail, temp._head);
                _tail = temp._tail;
            } else { /* insert somewhere in the middle */
                MutableList<T> before=static_cast<MutableList<T> &>(pos);
                set_rest(temp._tail, rest(before));
                set_rest(before, temp._head);
            }
        } else { /* prepend */
            set_rest(temp._tail, _head);
            _head = temp._head;
        }
    }
};

}

}

#endif
/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :