blocxx
Array.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 
34 
40 #ifndef BLOCXX_ARRAY_HPP_INCLUDE_GUARD_
41 #define BLOCXX_ARRAY_HPP_INCLUDE_GUARD_
42 #include "blocxx/BLOCXX_config.h"
43 #include "blocxx/ArrayFwd.hpp"
44 #include "blocxx/COWReference.hpp"
45 #include "blocxx/Types.hpp"
46 #include "blocxx/Exception.hpp"
47 #include "blocxx/vector.hpp"
48 
49 namespace BLOCXX_NAMESPACE
50 {
51 
52 // Declare the OutOfBoundsException
53 BLOCXX_DECLARE_APIEXCEPTION(OutOfBounds, BLOCXX_COMMON_API);
54 
65 template<class T> class Array
66 {
67  typedef std::vector<T, std::allocator<T> > V;
68 
69 #ifdef BLOCXX_WIN32
70 #pragma warning (push)
71 #pragma warning (disable: 4251)
72 #endif
73 
75 
76 #ifdef BLOCXX_WIN32
77 #pragma warning (pop)
78 #endif
79 
80 public:
81  typedef typename V::value_type value_type;
82  typedef typename V::pointer pointer;
83  typedef typename V::const_pointer const_pointer;
84  typedef typename V::iterator iterator;
85  typedef typename V::const_iterator const_iterator;
86  typedef typename V::reference reference;
87  typedef typename V::const_reference const_reference;
88  typedef typename V::size_type size_type;
89  typedef typename V::difference_type difference_type;
90  typedef typename V::reverse_iterator reverse_iterator;
91  typedef typename V::const_reverse_iterator const_reverse_iterator;
92 
96  Array();
100  ~Array();
105  explicit Array(V* toWrap);
113  Array(size_type n, const T& value);
121  Array(int n, const T& value);
129  Array(long n, const T& value);
136  explicit Array(size_type n);
142  template<class InputIterator>
143  Array(InputIterator first, InputIterator last);
149  iterator begin();
155  const_iterator begin() const;
161  iterator end();
167  const_iterator end() const;
173  reverse_iterator rbegin();
179  const_reverse_iterator rbegin() const;
185  reverse_iterator rend();
191  const_reverse_iterator rend() const;
195  size_type size() const;
199  size_type max_size() const;
204  size_type capacity() const;
208  bool empty() const;
215  reference operator[](size_type n);
222  const_reference operator[](size_type n) const;
228  Array<T>& operator+= (const T& x);
235  void reserve(size_type n);
239  reference front();
243  const_reference front() const;
247  reference back();
251  const_reference back() const;
256  void push_back(const T& x);
262  void append(const T& x);
267  void swap(Array<T>& x);
277  iterator insert(iterator position, const T& x);
285  void insert(size_type position, const T& x);
289  void remove(size_type index);
296  void remove(size_type begin, size_type end);
304  template<class InputIterator>
305  void insert(iterator position, InputIterator first, InputIterator last);
310  void appendArray(const Array<T>& x);
314  void pop_back();
321  iterator erase(iterator position);
330  iterator erase(iterator first, iterator last);
337  void resize(size_type new_size, const T& x);
344  void resize(size_type new_size);
349  void clear();
359  const_iterator find(const T &x, const_iterator first,
360  const_iterator last) const;
367  const_iterator find(const T &x) const;
377  iterator find(const T &x, iterator first, iterator last);
384  iterator find(const T &x);
394  bool contains(const T& x, const_iterator first,
395  const_iterator last) const;
401  bool contains(const T& x) const;
402 
411  friend bool operator== <>(const Array<T>& x, const Array<T>& y);
412 
430  friend bool operator< <>(const Array<T>& x, const Array<T>& y);
431 private:
432 #ifdef BLOCXX_CHECK_ARRAY_INDEXING
433  void checkValidIndex(size_type index) const;
434 #endif
435 };
436 
445 template <class T>
446 inline bool operator != (const Array<T>& x, const Array<T>& y)
447 {
448  return !(x == y);
449 }
450 
468 template <class T>
469 inline bool operator <= (const Array<T>& x, const Array<T>& y)
470 {
471  return !(y < x);
472 }
473 
491 template <class T>
492 inline bool operator >= (const Array<T>& x, const Array<T>& y)
493 {
494  return !(x < y);
495 }
496 
514 template <class T>
515 inline bool operator > (const Array<T>& x, const Array<T>& y)
516 {
517  return y < x;
518 }
519 
530 
531 } // end namespace BLOCXX_NAMESPACE
532 
533 #include "blocxx/ArrayImpl.hpp"
534 
535 #endif
536 
Array< Int8 > Int8Array
Definition: Array.hpp:521
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.
Definition: ArrayImpl.hpp:364
Array< UInt64 > UInt64Array
Definition: Array.hpp:526
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability...
Definition: Array.hpp:65
Taken from RFC 1321.
Array< Int64 > Int64Array
Definition: Array.hpp:527
bool operator!=(const Array< T > &x, const Array< T > &y)
Determine two Arrays are not equal.
Definition: Array.hpp:446
void append(const T &x)
Append an element to the end of the Array.
Definition: ArrayImpl.hpp:258
size_type max_size() const
Definition: ArrayImpl.hpp:167
std::vector< T, std::allocator< T > > V
Definition: Array.hpp:67
V::iterator iterator
Definition: Array.hpp:84
iterator insert(iterator position, const T &x)
Insert an element in the Array before an element specified by an iterator.
Definition: ArrayImpl.hpp:272
~Array()
Destructor.
Definition: ArrayImpl.hpp:61
Array< UInt16 > UInt16Array
Definition: Array.hpp:522
Array()
Default Constructor.
Definition: ArrayImpl.hpp:55
Array< Real32 > Real32Array
Definition: Array.hpp:529
Array< UInt32 > UInt32Array
Definition: Array.hpp:524
iterator erase(iterator position)
Remove an element of the Array specified with an iterator.
Definition: ArrayImpl.hpp:329
void reserve(size_type n)
Ensure the capacity is at least the size of a given value.
Definition: ArrayImpl.hpp:216
COWReference< V > m_impl
Definition: Array.hpp:74
Array< Real64 > Real64Array
Definition: Array.hpp:528
void resize(size_type new_size, const T &x)
Ensure the Array is a given size.
Definition: ArrayImpl.hpp:343
Array< T > & operator+=(const T &x)
Append an object to the end of the Array.
Definition: ArrayImpl.hpp:208
V::const_reverse_iterator const_reverse_iterator
Definition: Array.hpp:91
V::difference_type difference_type
Definition: Array.hpp:89
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...
Definition: ArrayImpl.hpp:406
void appendArray(const Array< T > &x)
Append the elements of another Array to the end of this Array.
Definition: ArrayImpl.hpp:315
V::reverse_iterator reverse_iterator
Definition: Array.hpp:90
size_type capacity() const
Definition: ArrayImpl.hpp:174
void push_back(const T &x)
Append an element to the end of the Array.
Definition: ArrayImpl.hpp:251
reverse_iterator rbegin()
Definition: ArrayImpl.hpp:132
reference operator[](size_type n)
Retrieve A read/write reference to an object in the Array at a given index.
Definition: ArrayImpl.hpp:188
bool operator>(const Array< T > &x, const Array< T > &y)
Determine if one Array is greater than another.
Definition: Array.hpp:515
bool operator>=(const Array< T > &x, const Array< T > &y)
Determine if one Array is greater than or equal to another.
Definition: Array.hpp:492
V::const_pointer const_pointer
Definition: Array.hpp:83
Array< Int16 > Int16Array
Definition: Array.hpp:523
Array< UInt8 > UInt8Array
Definition: Array.hpp:520
V::const_iterator const_iterator
Definition: Array.hpp:85
V::value_type value_type
Definition: Array.hpp:81
V::reference reference
Definition: Array.hpp:86
void pop_back()
Remove the last element of the Array.
Definition: ArrayImpl.hpp:322
void swap(Array< T > &x)
Swap the elements of this Array with the elements of another.
Definition: ArrayImpl.hpp:265
Array< Int32 > Int32Array
Definition: Array.hpp:525
V::const_reference const_reference
Definition: Array.hpp:87
#define BLOCXX_DECLARE_APIEXCEPTION(NAME, LINKAGE_SPEC)
Declare a new exception class named <NAME>Exception that derives from Exception This macro is typical...
Definition: Exception.hpp:396
reverse_iterator rend()
Definition: ArrayImpl.hpp:146
V::size_type size_type
Definition: Array.hpp:88
void clear()
Remove all items from the Array.
Definition: ArrayImpl.hpp:357
V::pointer pointer
Definition: Array.hpp:82
size_type size() const
Definition: ArrayImpl.hpp:160