blocxx
ArrayImpl.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33// This header file contains the implementation of the Array template class.
34// It is not intended to be used directly by users of the blocxx library.
35
36
44
45#ifndef BLOCXX_ARRAY_IMPL_HPP_INCLUDE_GUARD_
46#define BLOCXX_ARRAY_IMPL_HPP_INCLUDE_GUARD_
47#include "blocxx/BLOCXX_config.h"
48#include "blocxx/Array.hpp"
49
50namespace BLOCXX_NAMESPACE
51{
52
54template <typename T>
56: m_impl(new V)
57{
58}
59
60template <typename T>
62{
63}
64
65template <typename T>
66inline Array<T>::Array(V* toWrap)
67: m_impl(toWrap)
68{
69}
70
71template <typename T>
72inline Array<T>::Array(size_type n, const T& value)
73: m_impl(new V(n, value))
74{
75}
76
77template <typename T>
78inline Array<T>::Array(int n, const T& value)
79: m_impl(new V(n, value))
80{
81}
82
83template <typename T>
84inline Array<T>::Array(long n, const T& value)
85: m_impl(new V(n, value))
86{
87}
88
89template <typename T>
91: m_impl(new V(n))
92{
93}
94
95template <typename T>
96template<class InputIterator>
97inline Array<T>::Array(InputIterator first, InputIterator last)
98: m_impl(new V(first, last))
99{
102template <typename T>
103inline typename Array<T>::iterator
106 return m_impl->begin();
107}
109template <typename T>
110inline typename Array<T>::const_iterator
112{
113 return m_impl->begin();
114}
116template <typename T>
117inline typename Array<T>::iterator
119{
120 return m_impl->end();
123template <typename T>
124inline typename Array<T>::const_iterator
126{
127 return m_impl->end();
128}
129
129/////////////////////////////////////////////////////////////////////////////
130template <typename T>
131inline typename Array<T>::reverse_iterator
133{
134 return m_impl->rbegin();
135}
136
136/////////////////////////////////////////////////////////////////////////////
137template <typename T>
140{
141 return m_impl->rbegin();
142}
143
143/////////////////////////////////////////////////////////////////////////////
144template <typename T>
145inline typename Array<T>::reverse_iterator
147{
148 return m_impl->rend();
151template <typename T>
154{
155 return m_impl->rend();
156}
158template <typename T>
159inline typename Array<T>::size_type
162 return m_impl->size();
163}
165template <typename T>
166inline typename Array<T>::size_type
168{
169 return m_impl->max_size();
170}
172template <typename T>
173inline typename Array<T>::size_type
175{
176 return m_impl->capacity();
177}
178
179template <typename T>
180inline bool
182{
183 return m_impl->empty();
184}
185
185/////////////////////////////////////////////////////////////////////////////
186template <typename T>
187inline typename Array<T>::reference
189{
190#ifdef BLOCXX_CHECK_ARRAY_INDEXING
191 checkValidIndex(n);
192#endif
193 return m_impl->operator[](n);
194}
195/////////////////////////////////////////////////////////////////////////////
196template <typename T>
197inline typename Array<T>::const_reference
200#ifdef BLOCXX_CHECK_ARRAY_INDEXING
201 checkValidIndex(n);
202#endif
203 return m_impl->operator[](n);
206template <typename T>
207inline Array<T>&
209{
210 m_impl->push_back(x);
211 return *this;
212}
214template <typename T>
215inline void
217{
218 m_impl->reserve(n);
219}
220
221template <typename T>
222inline typename Array<T>::reference
224{
225 return m_impl->front();
226}
227
228template <typename T>
229inline typename Array<T>::const_reference
231{
232 return m_impl->front();
233}
234
235template <typename T>
236inline typename Array<T>::reference
238{
239 return m_impl->back();
240}
242template <typename T>
245{
246 return m_impl->back();
249template <typename T>
250inline void
252{
253 m_impl->push_back(x);
254}
256template <typename T>
257inline void
259{
260 push_back(x);
261}
262
262/////////////////////////////////////////////////////////////////////////////
263template <typename T>
264inline void
266{
267 m_impl.swap(x.m_impl);
268}
269
270
270template <typename T>
271inline typename Array<T>::iterator
272Array<T>::insert(iterator position, const T& x)
273{
274 return m_impl->insert(position, x);
275}
276
277template <typename T>
278inline void
279Array<T>::insert(size_type position, const T& x)
280{
281 m_impl->insert(m_impl->begin() + position, x);
282}
283
284template <typename T>
285inline void
287{
288#ifdef BLOCXX_CHECK_ARRAY_INDEXING
289 checkValidIndex(index);
290#endif
291 m_impl->erase(m_impl->begin() + index);
292}
293
294
294template <typename T>
295inline void
297{
298#ifdef BLOCXX_CHECK_ARRAY_INDEXING
299 checkValidIndex(begin);
300 checkValidIndex(end - 1);
301#endif
302 m_impl->erase(m_impl->begin() + begin, m_impl->begin() + end);
303}
304
305template <typename T>
306template<class InputIterator>
307inline void
308Array<T>::insert(iterator position, InputIterator first, InputIterator last)
309{
310 m_impl->insert(position, first, last);
311}
313template <typename T>
314inline void
316{
317 insert(end(), x.begin(), x.end());
318}
319
320template <typename T>
321inline void
323{
324 m_impl->pop_back();
325}
326
327template <typename T>
328inline typename Array<T>::iterator
331 return m_impl->erase(position);
332}
334template <typename T>
335inline typename Array<T>::iterator
338 return m_impl->erase(first, last);
339}
341template <typename T>
342inline void
343Array<T>::resize(size_type new_size, const T& x)
345 m_impl->resize(new_size, x);
346}
348template <typename T>
349inline void
351{
352 m_impl->resize(new_size);
353}
354
355template <typename T>
356inline void
358{
359 m_impl->clear();
360}
362template <typename T>
363inline typename Array<T>::const_iterator
364Array<T>::find(const T &x, const_iterator first, const_iterator last) const
365{
366 for( ; first != end(); ++first)
368 if( x == *first)
369 return first;
370 if(first == last)
371 break;
372 }
373 return end();
374}
376template <typename T>
378Array<T>::find(const T &x) const
379{
380 return find(x, begin(), end());
381}
382
383template <typename T>
384inline typename Array<T>::iterator
385Array<T>::find(const T &x, iterator first, iterator last)
386{
387 for( ; first != end(); ++first)
388 {
389 if( x == *first)
390 return first;
391 if(first == last)
392 break;
393 }
394 return end();
395}
397template <typename T>
398inline typename Array<T>::iterator
399Array<T>::find(const T &x)
400{
401 return find(x, begin(), end());
402}
404template <typename T>
405inline bool
407{
408 return find(x, first, last) != end();
409}
410
411template <typename T>
412inline bool
413Array<T>::contains(const T& x) const
414{
415 return find(x, begin(), end()) != end();
416}
417
418#ifdef BLOCXX_CHECK_ARRAY_INDEXING
420BLOCXX_COMMON_API void throwArrayOutOfBoundsException(size_t size, size_t idx);
421
423template <typename T>
424inline void
426{
427 if (index >= size())
428 {
429 throwArrayOutOfBoundsException(size(), index);
431}
432#endif
433template<class T>
434inline bool operator==(const Array<T>& x, const Array<T>& y)
435{
436 return *x.m_impl == *y.m_impl;
437}
438template<class T>
439inline bool operator<(const Array<T>& x, const Array<T>& y)
440{
441 return *x.m_impl < *y.m_impl;
442}
443template<class T>
444inline void swap(Array<T>& x, Array<T>& y)
445{
446 x.swap(y);
447}
448
449} // end namespace BLOCXX_NAMESPACE
450
451#endif
452
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability.
Definition ArrayFwd.hpp:46
void append(const T &x)
Append an element to the end of the Array.
reverse_iterator rbegin()
void remove(size_type index)
Remove an element from the Array at a given index.
void push_back(const T &x)
Append an element to the end of the Array.
void appendArray(const Array< T > &x)
Append the elements of another Array to the end of this Array.
iterator erase(iterator position)
Remove an element of the Array specified with an iterator.
size_type max_size() const
V::iterator iterator
Definition Array.hpp:84
size_type size() const
V::const_reverse_iterator const_reverse_iterator
Definition Array.hpp:91
V::reference reference
Definition Array.hpp:86
V::const_iterator const_iterator
Definition Array.hpp:85
COWReference< V > m_impl
Definition Array.hpp:74
bool contains(const T &x, const_iterator first, const_iterator last) const
Determine if element x is contained in the array range specified by the first and last iterators.
V::size_type size_type
Definition Array.hpp:88
V::const_reference const_reference
Definition Array.hpp:87
void clear()
Remove all items from the Array.
Array()
Default Constructor.
Definition ArrayImpl.hpp:55
iterator insert(iterator position, const T &x)
Insert an element in the Array before an element specified by an iterator.
void resize(size_type new_size, const T &x)
Ensure the Array is a given size.
Array< T > & operator+=(const T &x)
Append an object to the end of the Array.
void swap(Array< T > &x)
Swap the elements of this Array with the elements of another.
void reserve(size_type n)
Ensure the capacity is at least the size of a given value.
reverse_iterator rend()
std::vector< T, std::allocator< T > > V
Definition Array.hpp:67
const_iterator find(const T &x, const_iterator first, const_iterator last) const
Find element x in the array range specified by the first and last iterators.
size_type capacity() const
V::reverse_iterator reverse_iterator
Definition Array.hpp:90
reference operator[](size_type n)
Retrieve A read/write reference to an object in the Array at a given index.
void pop_back()
Remove the last element of the Array.
Taken from RFC 1321.
bool operator<(const Array< T > &x, const Array< T > &y)
bool operator==(const Array< T > &x, const Array< T > &y)
void swap(Array< T > &x, Array< T > &y)