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 
74  COWReference<V> m_impl;
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;
195  size_type size() const;
199  size_type max_size() const;
204  size_type capacity() const;
208  bool empty() 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);
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 
520 typedef Array<UInt8> UInt8Array;
521 typedef Array<Int8> Int8Array;
523 typedef Array<Int16> Int16Array;
524 typedef Array<UInt32> UInt32Array;
525 typedef Array<Int32> Int32Array;
526 typedef Array<UInt64> UInt64Array;
527 typedef Array<Int64> Int64Array;
528 typedef Array<Real64> Real64Array;
529 typedef Array<Real32> Real32Array;
530 
531 } // end namespace BLOCXX_NAMESPACE
532 
533 #include "blocxx/ArrayImpl.hpp"
534 
535 #endif
536 
BLOCXX_NAMESPACE::Array::end
iterator end()
Definition: ArrayImpl.hpp:148
BLOCXX_NAMESPACE::Array::const_reverse_iterator
V::const_reverse_iterator const_reverse_iterator
Definition: Array.hpp:121
BLOCXX_NAMESPACE::Int16Array
Array< Int16 > Int16Array
Definition: Array.hpp:553
BLOCXX_NAMESPACE::Array::reverse_iterator
V::reverse_iterator reverse_iterator
Definition: Array.hpp:120
BLOCXX_NAMESPACE::Array::reference
V::reference reference
Definition: Array.hpp:116
vector.hpp
BLOCXX_NAMESPACE::operator>=
bool operator>=(const Array< T > &x, const Array< T > &y)
Determine if one Array is greater than or equal to another.
Definition: Array.hpp:522
BLOCXX_NAMESPACE::Array::contains
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:436
BLOCXX_NAMESPACE::Array::remove
void remove(size_type index)
Remove an element from the Array at a given index.
Definition: ArrayImpl.hpp:316
BLOCXX_NAMESPACE::UInt32Array
Array< UInt32 > UInt32Array
Definition: Array.hpp:554
BLOCXX_NAMESPACE::Array::appendArray
void appendArray(const Array< T > &x)
Append the elements of another Array to the end of this Array.
Definition: ArrayImpl.hpp:345
ArrayFwd.hpp
BLOCXX_NAMESPACE::Array::value_type
V::value_type value_type
Definition: Array.hpp:111
BLOCXX_NAMESPACE::operator!=
bool operator!=(const Array< T > &x, const Array< T > &y)
Determine two Arrays are not equal.
Definition: Array.hpp:476
BLOCXX_NAMESPACE::Array::front
reference front()
Definition: ArrayImpl.hpp:253
Exception.hpp
BLOCXX_NAMESPACE::UInt8Array
Array< UInt8 > UInt8Array
Definition: Array.hpp:550
BLOCXX_NAMESPACE::Array::max_size
size_type max_size() const
Definition: ArrayImpl.hpp:197
BLOCXX_NAMESPACE
Taken from RFC 1321.
Definition: AppenderLogger.cpp:48
BLOCXX_NAMESPACE::Real64Array
Array< Real64 > Real64Array
Definition: Array.hpp:558
BLOCXX_NAMESPACE::Array::pointer
V::pointer pointer
Definition: Array.hpp:112
BLOCXX_NAMESPACE::Array::iterator
V::iterator iterator
Definition: Array.hpp:114
BLOCXX_NAMESPACE::Array::operator+=
Array< T > & operator+=(const T &x)
Append an object to the end of the Array.
Definition: ArrayImpl.hpp:238
BLOCXX_NAMESPACE::Array::rbegin
reverse_iterator rbegin()
Definition: ArrayImpl.hpp:162
BLOCXX_NAMESPACE::Real32Array
Array< Real32 > Real32Array
Definition: Array.hpp:559
BLOCXX_NAMESPACE::Array::const_iterator
V::const_iterator const_iterator
Definition: Array.hpp:115
BLOCXX_NAMESPACE::Array::rend
reverse_iterator rend()
Definition: ArrayImpl.hpp:176
BLOCXX_NAMESPACE::Array::size_type
V::size_type size_type
Definition: Array.hpp:118
BLOCXX_NAMESPACE::Array::swap
void swap(Array< T > &x)
Swap the elements of this Array with the elements of another.
Definition: ArrayImpl.hpp:295
BLOCXX_NAMESPACE::UInt64Array
Array< UInt64 > UInt64Array
Definition: Array.hpp:556
BLOCXX_NAMESPACE::UInt16Array
Array< UInt16 > UInt16Array
Definition: Array.hpp:552
ArrayImpl.hpp
BLOCXX_NAMESPACE::Array::V
std::vector< T, std::allocator< T > > V
Definition: Array.hpp:97
BLOCXX_NAMESPACE::Array::find
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:394
BLOCXX_DECLARE_APIEXCEPTION
#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:426
BLOCXX_NAMESPACE::Array::pop_back
void pop_back()
Remove the last element of the Array.
Definition: ArrayImpl.hpp:352
BLOCXX_NAMESPACE::Array::append
void append(const T &x)
Append an element to the end of the Array.
Definition: ArrayImpl.hpp:288
BLOCXX_NAMESPACE::Array::m_impl
COWReference< V > m_impl
Definition: Array.hpp:104
BLOCXX_NAMESPACE::Array::resize
void resize(size_type new_size, const T &x)
Ensure the Array is a given size.
Definition: ArrayImpl.hpp:373
BLOCXX_NAMESPACE::Array::~Array
~Array()
Destructor.
Definition: ArrayImpl.hpp:91
BLOCXX_NAMESPACE::Array::operator[]
reference operator[](size_type n)
Retrieve A read/write reference to an object in the Array at a given index.
Definition: ArrayImpl.hpp:218
BLOCXX_NAMESPACE::Array::size
size_type size() const
Definition: ArrayImpl.hpp:190
BLOCXX_NAMESPACE::Array::back
reference back()
Definition: ArrayImpl.hpp:267
BLOCXX_NAMESPACE::Array::clear
void clear()
Remove all items from the Array.
Definition: ArrayImpl.hpp:387
COWReference.hpp
BLOCXX_NAMESPACE::Int64Array
Array< Int64 > Int64Array
Definition: Array.hpp:557
BLOCXX_NAMESPACE::Array::const_reference
V::const_reference const_reference
Definition: Array.hpp:117
BLOCXX_NAMESPACE::Array::erase
iterator erase(iterator position)
Remove an element of the Array specified with an iterator.
Definition: ArrayImpl.hpp:359
BLOCXX_NAMESPACE::Int8Array
Array< Int8 > Int8Array
Definition: Array.hpp:551
Types.hpp
BLOCXX_NAMESPACE::Array::insert
iterator insert(iterator position, const T &x)
Insert an element in the Array before an element specified by an iterator.
Definition: ArrayImpl.hpp:302
BLOCXX_NAMESPACE::Array::empty
bool empty() const
Definition: ArrayImpl.hpp:211
BLOCXX_NAMESPACE::Array::const_pointer
V::const_pointer const_pointer
Definition: Array.hpp:113
BLOCXX_NAMESPACE::Array::capacity
size_type capacity() const
Definition: ArrayImpl.hpp:204
BLOCXX_NAMESPACE::Array::reserve
void reserve(size_type n)
Ensure the capacity is at least the size of a given value.
Definition: ArrayImpl.hpp:246
BLOCXX_NAMESPACE::Array::Array
Array()
Default Constructor.
Definition: ArrayImpl.hpp:85
BLOCXX_NAMESPACE::Int32Array
Array< Int32 > Int32Array
Definition: Array.hpp:555
BLOCXX_NAMESPACE::Array::begin
iterator begin()
Definition: ArrayImpl.hpp:134
BLOCXX_NAMESPACE::Array::push_back
void push_back(const T &x)
Append an element to the end of the Array.
Definition: ArrayImpl.hpp:281
BLOCXX_NAMESPACE::Array
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability.
Definition: Array.hpp:96
BLOCXX_NAMESPACE::operator<=
bool operator<=(const Array< T > &x, const Array< T > &y)
Determine if one Array is less than or equal to another.
Definition: Array.hpp:499
BLOCXX_NAMESPACE::Array::difference_type
V::difference_type difference_type
Definition: Array.hpp:119
BLOCXX_NAMESPACE::operator>
bool operator>(const Array< T > &x, const Array< T > &y)
Determine if one Array is greater than another.
Definition: Array.hpp:545