blocxx

Array.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00034 
00040 #ifndef BLOCXX_ARRAY_HPP_INCLUDE_GUARD_
00041 #define BLOCXX_ARRAY_HPP_INCLUDE_GUARD_
00042 #include "blocxx/BLOCXX_config.h"
00043 #include "blocxx/ArrayFwd.hpp"
00044 #include "blocxx/COWReference.hpp"
00045 #include "blocxx/Types.hpp"
00046 #include "blocxx/Exception.hpp"
00047 #include "blocxx/vector.hpp"
00048 
00049 namespace BLOCXX_NAMESPACE
00050 {
00051 
00052 // Declare the OutOfBoundsException
00053 BLOCXX_DECLARE_APIEXCEPTION(OutOfBounds, BLOCXX_COMMON_API);
00054 
00065 template<class T> class Array
00066 {
00067    typedef std::vector<T, std::allocator<T> > V;
00068 
00069 #ifdef BLOCXX_WIN32
00070 #pragma warning (push)
00071 #pragma warning (disable: 4251)
00072 #endif
00073 
00074    COWReference<V> m_impl;
00075 
00076 #ifdef BLOCXX_WIN32
00077 #pragma warning (pop)
00078 #endif
00079 
00080 public:
00081    typedef typename V::value_type value_type;
00082    typedef typename V::pointer pointer;
00083    typedef typename V::const_pointer const_pointer;
00084    typedef typename V::iterator iterator;
00085    typedef typename V::const_iterator const_iterator;
00086    typedef typename V::reference reference;
00087    typedef typename V::const_reference const_reference;
00088    typedef typename V::size_type size_type;
00089    typedef typename V::difference_type difference_type;
00090    typedef typename V::reverse_iterator reverse_iterator;
00091    typedef typename V::const_reverse_iterator const_reverse_iterator;
00092 
00096    Array();
00100    ~Array();
00105    explicit Array(V* toWrap);
00113    Array(size_type n, const T& value);
00121    Array(int n, const T& value);
00129    Array(long n, const T& value);
00136    explicit Array(size_type n);
00142    template<class InputIterator>
00143    Array(InputIterator first, InputIterator last);
00149    iterator begin();
00155    const_iterator begin() const;
00161    iterator end();
00167    const_iterator end() const;
00173    reverse_iterator rbegin();
00179    const_reverse_iterator rbegin() const;
00185    reverse_iterator rend();
00191    const_reverse_iterator rend() const;
00195    size_type size() const;
00199    size_type max_size() const;
00204    size_type capacity() const;
00208    bool empty() const;
00215    reference operator[](size_type n);
00222    const_reference operator[](size_type n) const;
00228    Array<T>& operator+= (const T& x);
00235    void reserve(size_type n);
00239    reference front();
00243    const_reference front() const;
00247    reference back();
00251    const_reference back() const;
00256    void push_back(const T& x);
00262    void append(const T& x);
00267    void swap(Array<T>& x);
00277    iterator insert(iterator position, const T& x);
00285    void insert(size_type position, const T& x);
00289    void remove(size_type index);
00296    void remove(size_type begin, size_type end);
00304    template<class InputIterator>
00305    void insert(iterator position, InputIterator first, InputIterator last);
00310    void appendArray(const Array<T>& x);
00314    void pop_back();
00321    iterator erase(iterator position);
00330    iterator erase(iterator first, iterator last);
00337    void resize(size_type new_size, const T& x);
00344    void resize(size_type new_size);
00349    void clear();
00359    const_iterator find(const T &x, const_iterator first,
00360                                    const_iterator last) const;
00367    const_iterator find(const T &x) const;
00377    iterator       find(const T &x, iterator first, iterator last);
00384    iterator       find(const T &x);
00394    bool contains(const T& x, const_iterator first,
00395                              const_iterator last) const;
00401    bool contains(const T& x) const;
00402 
00411    friend bool operator== <>(const Array<T>& x, const Array<T>& y);
00412 
00430    friend bool operator< <>(const Array<T>& x, const Array<T>& y);
00431 private:
00432 #ifdef BLOCXX_CHECK_ARRAY_INDEXING
00433    void checkValidIndex(size_type index) const;
00434 #endif
00435 };
00436 
00445 template <class T>
00446 inline bool operator != (const Array<T>& x, const Array<T>& y)
00447 {
00448    return !(x == y);
00449 }
00450 
00468 template <class T>
00469 inline bool operator <= (const Array<T>& x, const Array<T>& y)
00470 {
00471    return !(y < x);
00472 }
00473 
00491 template <class T>
00492 inline bool operator >= (const Array<T>& x, const Array<T>& y)
00493 {
00494    return !(x < y);
00495 }
00496 
00514 template <class T>
00515 inline bool operator > (const Array<T>& x, const Array<T>& y)
00516 {
00517    return y < x;
00518 }
00519   
00520 typedef Array<UInt8>      UInt8Array;
00521 typedef Array<Int8>       Int8Array;
00522 typedef Array<UInt16>     UInt16Array;
00523 typedef Array<Int16>      Int16Array;
00524 typedef Array<UInt32>     UInt32Array;
00525 typedef Array<Int32>      Int32Array;
00526 typedef Array<UInt64>     UInt64Array;
00527 typedef Array<Int64>      Int64Array;
00528 typedef Array<Real64>     Real64Array;
00529 typedef Array<Real32>     Real32Array;
00530 
00531 } // end namespace BLOCXX_NAMESPACE
00532 
00533 #include "blocxx/ArrayImpl.hpp"
00534 
00535 #endif
00536