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