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 
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 
50 namespace BLOCXX_NAMESPACE
51 {
52 
54 template <typename T>
55 inline Array<T>::Array()
56 : m_impl(new V)
57 {
58 }
60 template <typename T>
62 {
63 }
65 template <typename T>
66 inline Array<T>::Array(V* toWrap)
67 : m_impl(toWrap)
68 {
69 }
71 template <typename T>
72 inline Array<T>::Array(size_type n, const T& value)
73 : m_impl(new V(n, value))
74 {
75 }
77 template <typename T>
78 inline Array<T>::Array(int n, const T& value)
79 : m_impl(new V(n, value))
80 {
81 }
83 template <typename T>
84 inline Array<T>::Array(long n, const T& value)
85 : m_impl(new V(n, value))
86 {
87 }
89 template <typename T>
91 : m_impl(new V(n))
92 {
93 }
95 template <typename T>
96 template<class InputIterator>
97 inline Array<T>::Array(InputIterator first, InputIterator last)
98 : m_impl(new V(first, last))
99 {
100 }
102 template <typename T>
103 inline typename Array<T>::iterator
105 {
106  return m_impl->begin();
107 }
109 template <typename T>
110 inline typename Array<T>::const_iterator
112 {
113  return m_impl->begin();
114 }
116 template <typename T>
117 inline typename Array<T>::iterator
119 {
120  return m_impl->end();
121 }
123 template <typename T>
124 inline typename Array<T>::const_iterator
126 {
127  return m_impl->end();
128 }
130 template <typename T>
131 inline typename Array<T>::reverse_iterator
133 {
134  return m_impl->rbegin();
135 }
137 template <typename T>
138 inline typename Array<T>::const_reverse_iterator
140 {
141  return m_impl->rbegin();
142 }
144 template <typename T>
145 inline typename Array<T>::reverse_iterator
147 {
148  return m_impl->rend();
149 }
151 template <typename T>
152 inline typename Array<T>::const_reverse_iterator
154 {
155  return m_impl->rend();
156 }
158 template <typename T>
159 inline typename Array<T>::size_type
161 {
162  return m_impl->size();
163 }
165 template <typename T>
166 inline typename Array<T>::size_type
168 {
169  return m_impl->max_size();
170 }
172 template <typename T>
173 inline typename Array<T>::size_type
175 {
176  return m_impl->capacity();
177 }
179 template <typename T>
180 inline bool
182 {
183  return m_impl->empty();
184 }
186 template <typename T>
187 inline typename Array<T>::reference
189 {
190 #ifdef BLOCXX_CHECK_ARRAY_INDEXING
191  checkValidIndex(n);
192 #endif
193  return m_impl->operator[](n);
194 }
196 template <typename T>
197 inline typename Array<T>::const_reference
199 {
200 #ifdef BLOCXX_CHECK_ARRAY_INDEXING
201  checkValidIndex(n);
202 #endif
203  return m_impl->operator[](n);
204 }
206 template <typename T>
207 inline Array<T>&
209 {
210  m_impl->push_back(x);
211  return *this;
212 }
214 template <typename T>
215 inline void
217 {
218  m_impl->reserve(n);
219 }
221 template <typename T>
222 inline typename Array<T>::reference
224 {
225  return m_impl->front();
226 }
228 template <typename T>
229 inline typename Array<T>::const_reference
231 {
232  return m_impl->front();
233 }
235 template <typename T>
236 inline typename Array<T>::reference
238 {
239  return m_impl->back();
240 }
242 template <typename T>
243 inline typename Array<T>::const_reference
245 {
246  return m_impl->back();
247 }
249 template <typename T>
250 inline void
252 {
253  m_impl->push_back(x);
254 }
256 template <typename T>
257 inline void
258 Array<T>::append(const T& x)
259 {
260  push_back(x);
261 }
263 template <typename T>
264 inline void
266 {
267  m_impl.swap(x.m_impl);
268 }
270 template <typename T>
271 inline typename Array<T>::iterator
272 Array<T>::insert(iterator position, const T& x)
273 {
274  return m_impl->insert(position, x);
275 }
277 template <typename T>
278 inline void
279 Array<T>::insert(size_type position, const T& x)
280 {
281  m_impl->insert(m_impl->begin() + position, x);
282 }
284 template <typename T>
285 inline void
287 {
288 #ifdef BLOCXX_CHECK_ARRAY_INDEXING
289  checkValidIndex(index);
290 #endif
291  m_impl->erase(m_impl->begin() + index);
292 }
294 template <typename T>
295 inline 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 }
305 template <typename T>
306 template<class InputIterator>
307 inline void
308 Array<T>::insert(iterator position, InputIterator first, InputIterator last)
309 {
310  m_impl->insert(position, first, last);
311 }
313 template <typename T>
314 inline void
316 {
317  insert(end(), x.begin(), x.end());
318 }
320 template <typename T>
321 inline void
323 {
324  m_impl->pop_back();
325 }
327 template <typename T>
328 inline typename Array<T>::iterator
330 {
331  return m_impl->erase(position);
332 }
334 template <typename T>
335 inline typename Array<T>::iterator
337 {
338  return m_impl->erase(first, last);
339 }
341 template <typename T>
342 inline void
343 Array<T>::resize(size_type new_size, const T& x)
344 {
345  m_impl->resize(new_size, x);
346 }
348 template <typename T>
349 inline void
351 {
352  m_impl->resize(new_size);
353 }
355 template <typename T>
356 inline void
358 {
359  m_impl->clear();
360 }
362 template <typename T>
363 inline typename Array<T>::const_iterator
364 Array<T>::find(const T &x, const_iterator first, const_iterator last) const
365 {
366  for( ; first != end(); ++first)
367  {
368  if( x == *first)
369  return first;
370  if(first == last)
371  break;
372  }
373  return end();
374 }
376 template <typename T>
377 inline typename Array<T>::const_iterator
378 Array<T>::find(const T &x) const
379 {
380  return find(x, begin(), end());
381 }
383 template <typename T>
384 inline typename Array<T>::iterator
385 Array<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 }
397 template <typename T>
398 inline typename Array<T>::iterator
399 Array<T>::find(const T &x)
400 {
401  return find(x, begin(), end());
402 }
404 template <typename T>
405 inline bool
406 Array<T>::contains(const T& x, const_iterator first, const_iterator last) const
407 {
408  return find(x, first, last) != end();
409 }
411 template <typename T>
412 inline bool
413 Array<T>::contains(const T& x) const
414 {
415  return find(x, begin(), end()) != end();
416 }
417 
418 #ifdef BLOCXX_CHECK_ARRAY_INDEXING
419 
420 BLOCXX_COMMON_API void throwArrayOutOfBoundsException(size_t size, size_t idx);
421 
423 template <typename T>
424 inline void
425 Array<T>::checkValidIndex(size_type index) const
426 {
427  if (index >= size())
428  {
429  throwArrayOutOfBoundsException(size(), index);
430  }
431 }
432 #endif
433 template<class T>
434 inline bool operator==(const Array<T>& x, const Array<T>& y)
435 {
436  return *x.m_impl == *y.m_impl;
437 }
438 template<class T>
439 inline bool operator<(const Array<T>& x, const Array<T>& y)
440 {
441  return *x.m_impl < *y.m_impl;
442 }
443 template<class T>
444 inline void swap(Array<T>& x, Array<T>& y)
445 {
446  x.swap(y);
447 }
448 
449 } // end namespace BLOCXX_NAMESPACE
450 
451 #endif
452