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
39
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"
45#include "blocxx/Types.hpp"
46#include "blocxx/Exception.hpp"
47#include "blocxx/vector.hpp"
48
49namespace BLOCXX_NAMESPACE
50{
51
52// Declare the OutOfBoundsException
53BLOCXX_DECLARE_APIEXCEPTION(OutOfBounds, BLOCXX_COMMON_API);
54
65template<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
80public:
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
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);
208 bool empty() const;
228 Array<T>& operator+= (const T& x);
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();
337 void resize(size_type new_size, const T& x);
344 void resize(size_type new_size);
349 void clear();
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);
431private:
432#ifdef BLOCXX_CHECK_ARRAY_INDEXING
433 void checkValidIndex(size_type index) const;
434#endif
435};
436
445template <class T>
446inline bool operator != (const Array<T>& x, const Array<T>& y)
447{
448 return !(x == y);
449}
450
468template <class T>
469inline bool operator <= (const Array<T>& x, const Array<T>& y)
470{
471 return !(y < x);
472}
473
491template <class T>
492inline bool operator >= (const Array<T>& x, const Array<T>& y)
493{
494 return !(x < y);
495}
496
514template <class T>
515inline 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
#define BLOCXX_DECLARE_APIEXCEPTION(NAME, LINKAGE_SPEC)
Declare a new exception class named <NAME>Exception that derives from Exception This macro is typical...
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability.
Definition Array.hpp:66
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.
Array(size_type n, const T &value)
Construct an Array that consist of a specified number of elements that are copies of a given object.
Definition ArrayImpl.hpp:72
void insert(size_type position, const T &x)
Insert an element in the Array before an element specified by an index.
const_reference back() const
void push_back(const T &x)
Append an element to the end of the Array.
const_iterator end() const
const_reference front() const
void appendArray(const Array< T > &x)
Append the elements of another Array to the end of this Array.
const_reverse_iterator rbegin() const
Array(V *toWrap)
Constructor.
Definition ArrayImpl.hpp:66
V::const_pointer const_pointer
Definition Array.hpp:83
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
const_reference operator[](size_type n) const
Retrieve A read only reference to an object in the Array at a given index.
Array(InputIterator first, InputIterator last)
Construct an Array from a range specified with InputIterators.
Definition ArrayImpl.hpp:97
void insert(iterator position, InputIterator first, InputIterator last)
Insert a range of elements before a given position in the Array.
V::const_reverse_iterator const_reverse_iterator
Definition Array.hpp:91
V::value_type value_type
Definition Array.hpp:81
friend bool operator==(const Array< T > &x, const Array< T > &y)
Determine equality of two Arrays.
V::reference reference
Definition Array.hpp:86
V::const_iterator const_iterator
Definition Array.hpp:85
const_iterator begin() const
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.
Array(long n, const T &value)
Construct an Array that consist of a specified number of elements that are copies of a given object.
Definition ArrayImpl.hpp:84
iterator find(const T &x)
Find element x in the array.
V::size_type size_type
Definition Array.hpp:88
Array(int n, const T &value)
Construct an Array that consist of a specified number of elements that are copies of a given object.
Definition ArrayImpl.hpp:78
V::const_reference const_reference
Definition Array.hpp:87
void clear()
Remove all items from the Array.
Array()
Default Constructor.
Definition ArrayImpl.hpp:55
void remove(size_type begin, size_type end)
Remove an element specified by a range.
bool contains(const T &x) const
Determine if element x is contained in the array.
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(size_type n)
Construct an Array that consist of a specified number of elements that have be constructed using the ...
Definition ArrayImpl.hpp:90
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.
iterator erase(iterator first, iterator last)
Remove elements of the Array specified by a beginning and ending iterator.
V::difference_type difference_type
Definition Array.hpp:89
void reserve(size_type n)
Ensure the capacity is at least the size of a given value.
const_reverse_iterator rend() const
void resize(size_type new_size)
Ensure the Array is a given size.
reverse_iterator rend()
const_iterator find(const T &x) const
Find element x in the array.
iterator find(const T &x, iterator first, iterator last)
Find element x in the array range specified by the first and last iterators.
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.
COWReference A smart pointer that uses non-intrusive reference counting.
Taken from RFC 1321.
Array< UInt16 > UInt16Array
Definition Array.hpp:522
Array< UInt64 > UInt64Array
Definition Array.hpp:526
Array< Real64 > Real64Array
Definition Array.hpp:528
Array< Int8 > Int8Array
Definition Array.hpp:521
Array< UInt8 > UInt8Array
Definition Array.hpp:520
Array< Int16 > Int16Array
Definition Array.hpp:523
bool operator<=(const Array< T > &x, const Array< T > &y)
Determine if one Array is less than or equal to another.
Definition Array.hpp:469
bool operator>(const Array< T > &x, const Array< T > &y)
Determine if one Array is greater than another.
Definition Array.hpp:515
Array< Int32 > Int32Array
Definition Array.hpp:525
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
Array< UInt32 > UInt32Array
Definition Array.hpp:524
Array< Int64 > Int64Array
Definition Array.hpp:527
Array< Real32 > Real32Array
Definition Array.hpp:529
bool operator!=(const Array< T > &x, const Array< T > &y)
Determine two Arrays are not equal.
Definition Array.hpp:446