]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/container/array.h
my own uncrustify run
[xonotic/netradiant.git] / libs / container / array.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #if !defined( INCLUDED_CONTAINER_ARRAY_H )
23 #define INCLUDED_CONTAINER_ARRAY_H
24
25 #include <cstddef>
26 #include <algorithm>
27
28 #include "memory/allocator.h"
29
30 /// \brief An array whose size is variable at run-time.
31 ///
32 /// - Resizing the array destroys all the existing elements and invalidates all iterators.
33 /// - Default-Constructible, Copyable, Assignable.
34 /// - Compatible with the containers and algorithms in the Standard Template Library (STL) - http://www.sgi.com/tech/stl/
35 ///
36 /// \param Element The type to be stored in the array. Must provide a default-constructor and a copy-constructor.
37 /// \param Allocator A custom memory-allocator, conforming to the std::allocator interface.
38 template<typename Element, typename Allocator = DefaultAllocator<Element> >
39 class Array : public Allocator
40 {
41 std::size_t m_size;
42 Element* m_data;
43
44 Element* construct( std::size_t size ){
45 #if 1
46         return New<Element, Allocator>( *this ).vector( size );
47 #else
48         return new Element[size];
49 #endif
50 }
51 template<typename T1>
52 Element* construct( std::size_t size, const T1& value ){
53         return New<Element, Allocator>( *this ).vector( size, value );
54 }
55 void destroy( Element* data, std::size_t size ){
56 #if 1
57         Delete<Element, Allocator>( *this ).vector( data, size );
58 #else
59         delete[] data;
60 #endif
61 }
62
63 public:
64 typedef Element value_type;
65 typedef value_type* iterator;
66 typedef const value_type* const_iterator;
67
68 Array()
69         : m_size( 0 ), m_data( 0 ){
70 }
71 Array( std::size_t size )
72         : m_size( size ), m_data( construct( size ) ){
73 }
74 template<typename T1>
75 Array( std::size_t size, const T1& value )
76         : m_size( size ), m_data( construct( size, value ) ){
77 }
78 Array( const Array& other )
79         : Allocator( other ), m_size( other.size() ), m_data( construct( m_size ) ){
80         std::copy( other.begin(), other.end(), begin() );
81 }
82 template<typename Iterator>
83 Array( Iterator start, Iterator finish )
84         : m_size( std::distance( start, finish ) ), m_data( construct( m_size ) ){
85         std::copy( start, finish, begin() );
86 }
87 ~Array(){
88         destroy( m_data, m_size );
89 }
90
91 Array& operator=( const Array& other ){
92         if ( other.size() == size() ) {
93                 std::copy( other.begin(), other.end(), begin() );
94         }
95         else
96         {
97                 Array temp( other );
98                 temp.swap( *this );
99         }
100         return *this;
101 }
102
103 void swap( Array& other ){
104         std::swap( m_size, other.m_size );
105         std::swap( m_data, other.m_data );
106 }
107
108 iterator begin(){
109         return m_data;
110 }
111 const_iterator begin() const {
112         return m_data;
113 }
114 iterator end(){
115         return m_data + m_size;
116 }
117 const_iterator end() const {
118         return m_data + m_size;
119 }
120
121 value_type& operator[]( std::size_t index ){
122 #if defined( _DEBUG )
123         ASSERT_MESSAGE( index < size(), "array index out of bounds" );
124 #endif
125         return m_data[index];
126 }
127 const value_type& operator[]( std::size_t index ) const {
128 #if defined( _DEBUG )
129         ASSERT_MESSAGE( index < size(), "array index out of bounds" );
130 #endif
131         return m_data[index];
132 }
133 value_type* data(){
134         return m_data;
135 }
136 const value_type* data() const {
137         return m_data;
138 }
139 std::size_t size() const {
140         return m_size;
141 }
142 bool empty() const {
143         return m_size == 0;
144 }
145
146 void resize( std::size_t count ){
147         if ( count != size() ) {
148                 Array temp( count );
149                 temp.swap( *this );
150         }
151 }
152 void resize( std::size_t count, const value_type& value ){
153         if ( count != size() ) {
154                 Array temp( count, value );
155                 temp.swap( *this );
156         }
157 }
158 };
159
160 namespace std
161 {
162 /// \brief Swaps the values of \p self and \p other.
163 /// Overloads std::swap.
164 template<typename Element, typename Allocator>
165 inline void swap( Array<Element, Allocator>& self, Array<Element, Allocator>& other ){
166         self.swap( other );
167 }
168 }
169
170 #endif