update credits
[LibreOffice.git] / include / cosv / tpl / vvector.hxx
blobdec58642bf2357017ce3b8551103e0d747f9c28d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef CSV_VVECTOR_HXX
21 #define CSV_VVECTOR_HXX
23 #include <cstddef> // for ptrdiff_t
25 // USED SERVICES
26 #include <vector>
27 #include <cosv/tpl/tpltools.hxx>
32 namespace csv
34 namespace vvector
38 template <class TYPE>
39 struct delete_ptrs
41 static void Destruct(
42 std::vector< TYPE* > &
44 { csv::erase_container_of_heap_ptrs(v); }
46 /// @precond ->it is a valid iterator within v
47 static void Erase(
48 std::vector< TYPE* > &
50 typename std::vector< TYPE* >::iterator
51 it2erase )
52 { delete *it2erase; v.erase(it2erase); }
54 /// @precond ->v.size() > 0
55 static void PopBack(
56 std::vector< TYPE* > &
57 v )
58 { delete v.back(); v.pop_back(); }
60 /// @precond ->it is a valid iterator
61 static void ReplacePtr(
62 typename std::vector< TYPE* >::iterator
63 it,
64 DYN TYPE * pass_new )
65 { delete *it; *it = pass_new; }
69 /** One helper class for the ->csv::VirtualVector.
70 Implements a
72 template <class TYPE>
73 struct keep_ptrs
75 static void Destruct(std::vector< TYPE* > &)
78 static void Erase(
79 std::vector< TYPE* > &
81 typename std::vector< TYPE* >::iterator
82 it2erase )
83 { v.erase(it2erase); }
85 static void PopBack(
86 std::vector< TYPE* > &
87 v )
88 { v.pop_back(); }
90 /// @precond ->it is a valid iterator
91 static void ReplacePtr(
92 typename std::vector< TYPE* >::iterator
93 it,
94 TYPE * io_new )
95 { *it = io_new; }
99 } // namespace vvector
104 /** Implements a vector of different implementations of a base
105 class.
107 Implementation has to be by pointers to get the polymorphic
108 behaviour, however access is by references to the base class.
110 @tpl XX
111 The common base class of vector elements.
113 @tpl PTRDEL
114 Has two possible values:
115 vvector::delete_ptrs<XX> Elements have to be on the heap and
116 are deleted when removed (default).
117 vvector::keep_ptrs<XX> Elements are only referenced and not
118 deleted when removed.
121 template <class XX, class PTRDEL = vvector::delete_ptrs<XX> >
122 class VirtualVector
124 public:
125 typedef VirtualVector<XX,PTRDEL> self;
126 typedef std::vector< DYN XX* > impl_type;
127 typedef typename impl_type::size_type size_type;
128 typedef std::ptrdiff_t difference_type;
130 class const_iterator;
131 class iterator;
133 // LIFECYCLE
134 VirtualVector();
135 explicit VirtualVector(
136 int i_size );
137 ~VirtualVector();
139 // OPERATORS
140 const XX & operator[](
141 size_type i_pos ) const;
142 XX & operator[](
143 size_type i_pos );
145 // OPERATIONS
146 void push_back(
147 DYN XX & i_drElement );
148 void pop_back();
150 iterator insert(
151 iterator i_pos,
152 DYN XX & i_drElement );
153 void erase(
154 iterator i_pos );
155 void replace(
156 iterator i_pos,
157 DYN XX & i_drElement );
158 void reserve(
159 size_type i_size );
161 // INQUIRY
162 bool empty() const;
163 size_t size() const;
164 const_iterator begin() const;
165 const_iterator end() const;
166 const XX & front() const;
167 const XX & back() const;
169 // ACCESS
170 iterator begin();
171 iterator end();
172 XX & front();
173 XX & back();
175 private:
176 // Forbidden:
177 VirtualVector(const VirtualVector&);
178 VirtualVector & operator=(const VirtualVector&);
180 // DATA
181 std::vector< DYN XX* >
182 aData;
188 /** Should be usable for all STL algorithms.
189 Implements the Random Access Iterator concept.
191 template <class XX, class PTRDEL>
192 class VirtualVector<XX,PTRDEL>::
193 const_iterator
195 // This derivation provides type information for the STL
196 // It introduces the types "value_type" and "difference_type".
197 : public std::iterator<std::random_access_iterator_tag,
198 const XX>
200 public:
201 typedef VirtualVector<XX,PTRDEL> my_container;
202 typedef typename my_container::impl_type::const_iterator impl_iterator;
204 // LIFECYCLE
205 const_iterator(
206 impl_iterator i_implIter )
207 : itImpl(i_implIter) {}
210 /////////// STL ITERATOR CONCEPT IMPLEMENTATION //////////////
212 // Default Constructible functions:
213 const_iterator()
214 : itImpl() {}
216 // Assignable functions:
217 // Assignment and copy constructor use the compiler generated versions.
219 // Equality Comparable functions:
220 bool operator==(
221 const_iterator i_other ) const
222 { return itImpl == i_other.itImpl; }
223 bool operator!=(
224 const_iterator i_other ) const
225 { return itImpl != i_other.itImpl; }
227 // Trivial Iterator functions:
228 const XX & operator*() const
229 { return *(*itImpl); }
231 // Input Iterator functions:
232 const_iterator & operator++()
233 { ++itImpl; return *this; }
234 const_iterator operator++(int)
235 { return const_iterator(itImpl++); }
237 // Bidirectional Iterator functions:
238 const_iterator & operator--()
239 { --itImpl; return *this; }
240 const_iterator operator--(int)
241 { return const_iterator(itImpl--); }
243 // Less Than Comparable functions:
244 bool operator<(
245 const_iterator i_other ) const
246 { return itImpl < i_other.itImpl; }
248 // Random Access Iterator functions:
249 const_iterator & operator+=(
250 difference_type i_diff )
251 { itImpl += i_diff; return *this; }
252 const_iterator operator+(
253 difference_type i_diff ) const
254 { const_iterator ret(itImpl);
255 return ret += i_diff; }
256 const_iterator & operator-=(
257 difference_type i_diff )
258 { itImpl -= i_diff; return *this; }
259 const_iterator operator-(
260 difference_type i_diff ) const
261 { const_iterator ret(itImpl);
262 return ret -= i_diff; }
263 difference_type operator-(
264 const_iterator i_it ) const
265 { return itImpl - i_it.itImpl; }
266 const XX & operator[](
267 difference_type i_index )
268 { return *(*itImpl[i_index]); }
270 //////////////////////////////////////////////////////////////////////////
272 private:
273 friend class VirtualVector<XX,PTRDEL>;
274 impl_iterator ImplValue() const { return itImpl; }
276 // DATA
277 impl_iterator itImpl;
283 /** Should be usable for all STL algorithms.
284 Implements the Random Access Iterator concept.
286 template <class XX, class PTRDEL>
287 class VirtualVector<XX,PTRDEL>::
288 iterator
290 // This derivation provides type information for the STL
291 // It introduces the types "value_type" and "difference_type".
292 : public std::iterator<std::random_access_iterator_tag,
295 public:
296 typedef VirtualVector<XX,PTRDEL> my_container;
297 typedef typename my_container::impl_type::iterator impl_iterator;
299 // LIFECYCLE
300 iterator(
301 impl_iterator i_implIter )
302 : itImpl(i_implIter) {}
305 /////////// STL ITERATOR CONCEPT IMPLEMENTATION //////////////
307 // Default Constructible functions:
308 iterator()
309 : itImpl() {}
311 // Assignable functions:
312 // Assignment and copy constructor use the compiler generated versions.
314 // Equality Comparable functions:
315 bool operator==(
316 iterator i_other ) const
317 { return itImpl == i_other.itImpl; }
318 bool operator!=(
319 iterator i_other ) const
320 { return itImpl != i_other.itImpl; }
322 // Trivial Iterator functions:
323 XX & operator*() const
324 { return *(*itImpl); }
326 // Input Iterator functions:
327 iterator & operator++()
328 { ++itImpl; return *this; }
329 iterator operator++(int)
330 { return iterator(itImpl++); }
332 // Bidirectional Iterator functions:
333 iterator & operator--()
334 { --itImpl; return *this; }
335 iterator operator--(int)
336 { return iterator(itImpl--); }
338 // Less Than Comparable functions:
339 bool operator<(
340 iterator i_other ) const
341 { return itImpl < i_other.itImpl; }
343 // Random Access Iterator functions:
344 iterator & operator+=(
345 difference_type i_diff )
346 { itImpl += i_diff; return *this; }
347 iterator operator+(
348 difference_type i_diff ) const
349 { iterator ret(itImpl);
350 return ret += i_diff; }
351 iterator & operator-=(
352 difference_type i_diff )
353 { itImpl -= i_diff; return *this; }
354 iterator operator-(
355 difference_type i_diff ) const
356 { iterator ret(itImpl);
357 return ret -= i_diff; }
358 difference_type operator-(
359 iterator i_it ) const
360 { return itImpl - i_it.itImpl; }
361 XX & operator[](
362 difference_type i_index )
363 { return *(*itImpl[i_index]); }
365 //////////////////////////////////////////////////////////////////////////
367 private:
368 friend class VirtualVector<XX,PTRDEL>;
369 impl_iterator ImplValue() const { return itImpl; }
371 // DATA
372 impl_iterator itImpl;
378 // IMPLEMENTATION
379 template <class XX, class PTRDEL>
380 inline
381 VirtualVector<XX,PTRDEL>::VirtualVector()
382 : aData()
386 template <class XX, class PTRDEL>
387 inline
388 VirtualVector<XX,PTRDEL>::VirtualVector(int i_size)
389 : aData(i_size, 0)
393 template <class XX, class PTRDEL>
394 inline
395 VirtualVector<XX,PTRDEL>::~VirtualVector()
397 PTRDEL::Destruct(aData);
400 template <class XX, class PTRDEL>
401 inline const XX &
402 VirtualVector<XX,PTRDEL>::operator[]( size_type i_pos ) const
404 return *aData[i_pos];
407 template <class XX, class PTRDEL>
408 inline XX &
409 VirtualVector<XX,PTRDEL>::operator[]( size_type i_pos )
411 return *aData[i_pos];
414 template <class XX, class PTRDEL>
415 inline void
416 VirtualVector<XX,PTRDEL>::push_back( DYN XX & i_drElement )
418 aData.push_back(&i_drElement);
421 template <class XX, class PTRDEL>
422 inline void
423 VirtualVector<XX,PTRDEL>::pop_back()
425 if (NOT aData.empty())
426 PTRDEL::PopBack(aData);
429 template <class XX, class PTRDEL>
430 inline typename VirtualVector<XX,PTRDEL>::iterator
431 VirtualVector<XX,PTRDEL>::insert( iterator i_pos,
432 DYN XX & i_drElement )
434 return iterator(aData.insert(i_pos.ImplValue(), &i_drElement));
437 template <class XX, class PTRDEL>
438 inline void
439 VirtualVector<XX,PTRDEL>::erase( iterator i_pos )
441 PTRDEL::Erase(aData, i_pos.ImplValue());
444 template <class XX, class PTRDEL>
445 inline void
446 VirtualVector<XX,PTRDEL>::replace( iterator i_pos,
447 DYN XX & i_drElement )
449 PTRDEL::ReplacePtr(*i_pos, &i_drElement);
452 template <class XX, class PTRDEL>
453 inline void
454 VirtualVector<XX,PTRDEL>::reserve( size_type i_size )
456 aData.reserve(i_size);
459 template <class XX, class PTRDEL>
460 inline bool
461 VirtualVector<XX,PTRDEL>::empty() const
463 return aData.empty();
466 template <class XX, class PTRDEL>
467 inline size_t
468 VirtualVector<XX,PTRDEL>::size() const
470 return aData.size();
473 template <class XX, class PTRDEL>
474 inline typename VirtualVector<XX,PTRDEL>::const_iterator
475 VirtualVector<XX,PTRDEL>::begin() const
477 return const_iterator(aData.begin());
480 template <class XX, class PTRDEL>
481 inline typename VirtualVector<XX,PTRDEL>::const_iterator
482 VirtualVector<XX,PTRDEL>::end() const
484 return const_iterator(aData.end());
487 template <class XX, class PTRDEL>
488 inline const XX &
489 VirtualVector<XX,PTRDEL>::front() const
491 return *aData.front();
494 template <class XX, class PTRDEL>
495 inline const XX &
496 VirtualVector<XX,PTRDEL>::back() const
498 return *aData.back();
501 template <class XX, class PTRDEL>
502 inline typename VirtualVector<XX,PTRDEL>::iterator
503 VirtualVector<XX,PTRDEL>::begin()
505 return iterator(aData.begin());
508 template <class XX, class PTRDEL>
509 inline typename VirtualVector<XX,PTRDEL>::iterator
510 VirtualVector<XX,PTRDEL>::end()
512 return iterator(aData.end());
515 template <class XX, class PTRDEL>
516 inline XX &
517 VirtualVector<XX,PTRDEL>::front()
519 return *aData.front();
522 template <class XX, class PTRDEL>
523 inline XX &
524 VirtualVector<XX,PTRDEL>::back()
526 return *aData.back();
532 } // namespace csv
533 #endif
535 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */