summaryrefslogtreecommitdiff
path: root/include/cosv/tpl/vvector.hxx
blob: dec58642bf2357017ce3b8551103e0d747f9c28d (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you 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 .
 */

#ifndef CSV_VVECTOR_HXX
#define CSV_VVECTOR_HXX

#include <cstddef>      // for ptrdiff_t

// USED SERVICES
#include <vector>
#include <cosv/tpl/tpltools.hxx>




namespace csv
{
namespace vvector
{


template <class TYPE>
struct delete_ptrs
{
    static void         Destruct(
                            std::vector< TYPE* > &
                                                v)
                        { csv::erase_container_of_heap_ptrs(v); }

    /// @precond ->it is a valid iterator within v
    static void         Erase(
                            std::vector< TYPE* > &
                                                v,
                            typename std::vector< TYPE* >::iterator
                                                it2erase )
                        { delete *it2erase; v.erase(it2erase); }

    /// @precond ->v.size() > 0
    static void         PopBack(
                            std::vector< TYPE* > &
                                                v )
                        { delete v.back(); v.pop_back(); }

    /// @precond ->it is a valid iterator
    static void         ReplacePtr(
                            typename std::vector< TYPE* >::iterator
                                                it,
                            DYN TYPE *          pass_new )
                        { delete *it; *it = pass_new; }
};


/** One helper class for the ->csv::VirtualVector.
    Implements a
*/
template <class TYPE>
struct keep_ptrs
{
    static void         Destruct(std::vector< TYPE* > &)
                        {}

    static void         Erase(
                            std::vector< TYPE* > &
                                                v,
                            typename std::vector< TYPE* >::iterator
                                                it2erase )
                        { v.erase(it2erase); }

    static void         PopBack(
                            std::vector< TYPE* > &
                                                v )
                        { v.pop_back(); }

    /// @precond ->it is a valid iterator
    static void         ReplacePtr(
                            typename std::vector< TYPE* >::iterator
                                                it,
                            TYPE *              io_new )
                        { *it = io_new; }
};


}   // namespace vvector




/** Implements a vector of different implementations of a base
    class.

    Implementation has to be by pointers to get the polymorphic
    behaviour, however access is by references to the base class.

    @tpl XX
    The common base class of vector elements.

    @tpl PTRDEL
    Has two possible values:
        vvector::delete_ptrs<XX>    Elements have to be on the heap and
                                    are deleted when removed (default).
        vvector::keep_ptrs<XX>      Elements are only referenced and not
                                    deleted when removed.

*/
template <class XX, class PTRDEL = vvector::delete_ptrs<XX> >
class VirtualVector
{
  public:
    typedef VirtualVector<XX,PTRDEL>            self;
    typedef std::vector< DYN XX* >              impl_type;
    typedef typename impl_type::size_type       size_type;
    typedef std::ptrdiff_t                      difference_type;

    class const_iterator;
    class iterator;

    // LIFECYCLE
                        VirtualVector();
    explicit            VirtualVector(
                            int                 i_size );
                        ~VirtualVector();

    // OPERATORS
    const XX &          operator[](
                            size_type           i_pos ) const;
    XX &                operator[](
                            size_type           i_pos );

    // OPERATIONS
    void                push_back(
                            DYN XX &            i_drElement );
    void                pop_back();

    iterator            insert(
                            iterator            i_pos,
                            DYN XX &            i_drElement );
    void                erase(
                            iterator            i_pos );
    void                replace(
                            iterator            i_pos,
                            DYN XX &            i_drElement );
    void                reserve(
                            size_type           i_size );

    // INQUIRY
    bool                empty() const;
    size_t              size() const;
    const_iterator      begin() const;
    const_iterator      end() const;
    const XX &          front() const;
    const XX &          back() const;

    // ACCESS
    iterator            begin();
    iterator            end();
    XX &                front();
    XX &                back();

  private:
    // Forbidden:
                        VirtualVector(const VirtualVector&);
    VirtualVector &     operator=(const VirtualVector&);

    // DATA
    std::vector< DYN XX* >
                        aData;
};




/** Should be usable for all STL algorithms.
    Implements the Random Access Iterator concept.
*/
template <class XX, class PTRDEL>
class VirtualVector<XX,PTRDEL>::
      const_iterator

            // This derivation provides type information for the STL
            // It introduces the types "value_type" and "difference_type".
            : public std::iterator<std::random_access_iterator_tag,
                        const XX>
{
  public:
    typedef VirtualVector<XX,PTRDEL>                            my_container;
    typedef typename my_container::impl_type::const_iterator    impl_iterator;

    // LIFECYCLE
                        const_iterator(
                            impl_iterator       i_implIter )
                            : itImpl(i_implIter) {}


    ///////////      STL ITERATOR CONCEPT IMPLEMENTATION        //////////////

    // Default Constructible functions:
                        const_iterator()
                            : itImpl() {}

    // Assignable functions:
        // Assignment and copy constructor use the compiler generated versions.

    // Equality Comparable functions:
    bool                operator==(
                            const_iterator      i_other ) const
                            { return itImpl == i_other.itImpl; }
    bool                operator!=(
                            const_iterator      i_other ) const
                            { return itImpl != i_other.itImpl; }

    // Trivial Iterator functions:
    const XX &          operator*() const
                            { return *(*itImpl); }

    // Input Iterator functions:
    const_iterator &    operator++()
                            { ++itImpl; return *this; }
    const_iterator      operator++(int)
                            { return const_iterator(itImpl++); }

    // Bidirectional Iterator functions:
    const_iterator &    operator--()
                            { --itImpl; return *this; }
    const_iterator      operator--(int)
                            { return const_iterator(itImpl--); }

    // Less Than Comparable functions:
    bool                operator<(
                            const_iterator      i_other ) const
                            { return itImpl < i_other.itImpl; }

    // Random Access Iterator functions:
    const_iterator &    operator+=(
                            difference_type     i_diff )
                            { itImpl += i_diff; return *this; }
    const_iterator      operator+(
                            difference_type     i_diff ) const
                            { const_iterator ret(itImpl);
                              return ret += i_diff; }
    const_iterator &    operator-=(
                            difference_type     i_diff )
                            { itImpl -= i_diff;  return *this; }
    const_iterator      operator-(
                            difference_type     i_diff ) const
                            { const_iterator ret(itImpl);
                              return ret -= i_diff; }
    difference_type     operator-(
                            const_iterator      i_it ) const
                            { return itImpl - i_it.itImpl; }
    const XX &          operator[](
                            difference_type     i_index )
                            { return *(*itImpl[i_index]); }

    //////////////////////////////////////////////////////////////////////////

  private:
    friend class VirtualVector<XX,PTRDEL>;
    impl_iterator       ImplValue() const       { return itImpl; }

    // DATA
    impl_iterator       itImpl;
};




/** Should be usable for all STL algorithms.
    Implements the Random Access Iterator concept.
*/
template <class XX, class PTRDEL>
class VirtualVector<XX,PTRDEL>::
      iterator

            // This derivation provides type information for the STL
            // It introduces the types "value_type" and "difference_type".
            : public std::iterator<std::random_access_iterator_tag,
                        XX>
{
  public:
    typedef VirtualVector<XX,PTRDEL>                            my_container;
    typedef typename my_container::impl_type::iterator          impl_iterator;

    // LIFECYCLE
                        iterator(
                            impl_iterator       i_implIter )
                            : itImpl(i_implIter) {}


    ///////////      STL ITERATOR CONCEPT IMPLEMENTATION        //////////////

    // Default Constructible functions:
                        iterator()
                            : itImpl() {}

    // Assignable functions:
        // Assignment and copy constructor use the compiler generated versions.

    // Equality Comparable functions:
    bool                operator==(
                            iterator            i_other ) const
                            { return itImpl == i_other.itImpl; }
    bool                operator!=(
                            iterator            i_other ) const
                            { return itImpl != i_other.itImpl; }

    // Trivial Iterator functions:
    XX &                operator*() const
                            { return *(*itImpl); }

    // Input Iterator functions:
    iterator &          operator++()
                            { ++itImpl; return *this; }
    iterator            operator++(int)
                            { return iterator(itImpl++); }

    // Bidirectional Iterator functions:
    iterator &          operator--()
                            { --itImpl; return *this; }
    iterator            operator--(int)
                            { return iterator(itImpl--); }

    // Less Than Comparable functions:
    bool                operator<(
                            iterator            i_other ) const
                            { return itImpl < i_other.itImpl; }

    // Random Access Iterator functions:
    iterator &          operator+=(
                            difference_type     i_diff )
                            { itImpl += i_diff; return *this;  }
    iterator            operator+(
                            difference_type     i_diff ) const
                            { iterator ret(itImpl);
                              return ret += i_diff; }
    iterator &          operator-=(
                            difference_type     i_diff )
                            { itImpl -= i_diff; return *this;  }
    iterator            operator-(
                            difference_type     i_diff ) const
                            { iterator ret(itImpl);
                              return ret -= i_diff; }
    difference_type     operator-(
                            iterator            i_it ) const
                            { return itImpl - i_it.itImpl; }
    XX &                operator[](
                            difference_type     i_index )
                            { return *(*itImpl[i_index]); }

    //////////////////////////////////////////////////////////////////////////

  private:
    friend class VirtualVector<XX,PTRDEL>;
    impl_iterator       ImplValue() const       { return itImpl; }

    // DATA
    impl_iterator       itImpl;
};




// IMPLEMENTATION
template <class XX, class PTRDEL>
inline
VirtualVector<XX,PTRDEL>::VirtualVector()
    :   aData()
{
}

template <class XX, class PTRDEL>
inline
VirtualVector<XX,PTRDEL>::VirtualVector(int i_size)
    :   aData(i_size, 0)
{
}

template <class XX, class PTRDEL>
inline
VirtualVector<XX,PTRDEL>::~VirtualVector()
{
    PTRDEL::Destruct(aData);
}

template <class XX, class PTRDEL>
inline const XX &
VirtualVector<XX,PTRDEL>::operator[]( size_type i_pos ) const
{
    return *aData[i_pos];
}

template <class XX, class PTRDEL>
inline XX &
VirtualVector<XX,PTRDEL>::operator[]( size_type i_pos )
{
    return *aData[i_pos];
}

template <class XX, class PTRDEL>
inline void
VirtualVector<XX,PTRDEL>::push_back( DYN XX & i_drElement )
{
    aData.push_back(&i_drElement);
}

template <class XX, class PTRDEL>
inline void
VirtualVector<XX,PTRDEL>::pop_back()
{
    if (NOT aData.empty())
        PTRDEL::PopBack(aData);
}

template <class XX, class PTRDEL>
inline typename VirtualVector<XX,PTRDEL>::iterator
VirtualVector<XX,PTRDEL>::insert( iterator            i_pos,
                                  DYN XX &            i_drElement )
{
    return iterator(aData.insert(i_pos.ImplValue(), &i_drElement));
}

template <class XX, class PTRDEL>
inline void
VirtualVector<XX,PTRDEL>::erase( iterator i_pos )
{
    PTRDEL::Erase(aData, i_pos.ImplValue());
}

template <class XX, class PTRDEL>
inline void
VirtualVector<XX,PTRDEL>::replace( iterator     i_pos,
                                   DYN XX &     i_drElement )
{
    PTRDEL::ReplacePtr(*i_pos, &i_drElement);
}

template <class XX, class PTRDEL>
inline void
VirtualVector<XX,PTRDEL>::reserve( size_type i_size )
{
    aData.reserve(i_size);
}

template <class XX, class PTRDEL>
inline bool
VirtualVector<XX,PTRDEL>::empty() const
{
    return aData.empty();
}

template <class XX, class PTRDEL>
inline size_t
VirtualVector<XX,PTRDEL>::size() const
{
    return aData.size();
}

template <class XX, class PTRDEL>
inline typename VirtualVector<XX,PTRDEL>::const_iterator
VirtualVector<XX,PTRDEL>::begin() const
{
    return const_iterator(aData.begin());
}

template <class XX, class PTRDEL>
inline typename VirtualVector<XX,PTRDEL>::const_iterator
VirtualVector<XX,PTRDEL>::end() const
{
    return const_iterator(aData.end());
}

template <class XX, class PTRDEL>
inline const XX &
VirtualVector<XX,PTRDEL>::front() const
{
    return *aData.front();
}

template <class XX, class PTRDEL>
inline const XX &
VirtualVector<XX,PTRDEL>::back() const
{
    return *aData.back();
}

template <class XX, class PTRDEL>
inline typename VirtualVector<XX,PTRDEL>::iterator
VirtualVector<XX,PTRDEL>::begin()
{
    return iterator(aData.begin());
}

template <class XX, class PTRDEL>
inline typename VirtualVector<XX,PTRDEL>::iterator
VirtualVector<XX,PTRDEL>::end()
{
    return iterator(aData.end());
}

template <class XX, class PTRDEL>
inline XX &
VirtualVector<XX,PTRDEL>::front()
{
    return *aData.front();
}

template <class XX, class PTRDEL>
inline XX &
VirtualVector<XX,PTRDEL>::back()
{
    return *aData.back();
}




}   // namespace csv
#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */