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;
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);
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