2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined( INCLUDED_CONTAINER_ARRAY_H )
23 #define INCLUDED_CONTAINER_ARRAY_H
25 #include "globaldefs.h"
29 #include "memory/allocator.h"
31 /// \brief An array whose size is variable at run-time.
33 /// - Resizing the array destroys all the existing elements and invalidates all iterators.
34 /// - Default-Constructible, Copyable, Assignable.
35 /// - Compatible with the containers and algorithms in the Standard Template Library (STL) - http://www.sgi.com/tech/stl/
37 /// \param Element The type to be stored in the array. Must provide a default-constructor and a copy-constructor.
38 /// \param Allocator A custom memory-allocator, conforming to the std::allocator interface.
39 template<typename Element, typename Allocator = DefaultAllocator<Element> >
40 class Array : public Allocator
45 Element* construct( std::size_t size ){
47 return New<Element, Allocator>( *this ).vector( size );
49 return new Element[size];
53 Element* construct( std::size_t size, const T1& value ){
54 return New<Element, Allocator>( *this ).vector( size, value );
56 void destroy( Element* data, std::size_t size ){
58 Delete<Element, Allocator>( *this ).vector( data, size );
65 typedef Element value_type;
66 typedef value_type* iterator;
67 typedef const value_type* const_iterator;
70 : m_size( 0 ), m_data( 0 ){
72 Array( std::size_t size )
73 : m_size( size ), m_data( construct( size ) ){
76 Array( std::size_t size, const T1& value )
77 : m_size( size ), m_data( construct( size, value ) ){
79 Array( const Array& other )
80 : Allocator( other ), m_size( other.size() ), m_data( construct( m_size ) ){
81 std::copy( other.begin(), other.end(), begin() );
83 template<typename Iterator>
84 Array( Iterator start, Iterator finish )
85 : m_size( std::distance( start, finish ) ), m_data( construct( m_size ) ){
86 std::copy( start, finish, begin() );
89 destroy( m_data, m_size );
92 Array& operator=( const Array& other ){
93 if ( other.size() == size() ) {
94 std::copy( other.begin(), other.end(), begin() );
104 void swap( Array& other ){
105 std::swap( m_size, other.m_size );
106 std::swap( m_data, other.m_data );
112 const_iterator begin() const {
116 return m_data + m_size;
118 const_iterator end() const {
119 return m_data + m_size;
122 value_type& operator[]( std::size_t index ){
124 ASSERT_MESSAGE( index < size(), "array index out of bounds" );
126 return m_data[index];
128 const value_type& operator[]( std::size_t index ) const {
130 ASSERT_MESSAGE( index < size(), "array index out of bounds" );
132 return m_data[index];
137 const value_type* data() const {
140 std::size_t size() const {
147 void resize( std::size_t count ){
148 if ( count != size() ) {
153 void resize( std::size_t count, const value_type& value ){
154 if ( count != size() ) {
155 Array temp( count, value );
163 /// \brief Swaps the values of \p self and \p other.
164 /// Overloads std::swap.
165 template<typename Element, typename Allocator>
166 inline void swap( Array<Element, Allocator>& self, Array<Element, Allocator>& other ){