MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

AllocatedArray.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2010-2016 Max Kellermann <max.kellermann@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef ALLOCATED_ARRAY_HXX
31 #define ALLOCATED_ARRAY_HXX
32
33 #include "WritableBuffer.hxx"
34 #include "Compiler.h"
35
36 #include <algorithm>
37
38 #include <assert.h>
39
43 template<class T>
44 class AllocatedArray {
45 typedef WritableBuffer<T> Buffer;
46
47 public:
48 typedef typename Buffer::size_type size_type;
49 typedef typename Buffer::reference_type reference_type;
50 typedef typename Buffer::const_reference_type const_reference_type;
51 typedef typename Buffer::iterator iterator;
52 typedef typename Buffer::const_iterator const_iterator;
53
54 protected:
55 Buffer buffer{nullptr};
56
57 public:
58 constexpr AllocatedArray() = default;
59
60 explicit AllocatedArray(size_type _size)
61 :buffer{new T[_size], _size} {
62 assert(size() == 0 || buffer.data != nullptr);
63 }
64
65 explicit AllocatedArray(const AllocatedArray &other)
66 :buffer{new T[other.buffer.size], other.buffer.size} {
67 assert(size() == 0 || buffer.data != nullptr);
68 assert(other.size() == 0 || other.buffer.data != nullptr);
69
70 std::copy_n(other.buffer.data, buffer.size, buffer.data);
71 }
72
73 AllocatedArray(AllocatedArray &&other)
74 :buffer(other.buffer) {
75 other.buffer = Buffer::Null();
76 }
77
78 ~AllocatedArray() {
79 delete[] buffer.data;
80 }
81
82 AllocatedArray &operator=(const AllocatedArray &other) {
83 assert(size() == 0 || buffer.data != nullptr);
84 assert(other.size() == 0 || other.buffer.data != nullptr);
85
86 if (&other == this)
87 return *this;
88
89 ResizeDiscard(other.size());
90 std::copy_n(other.buffer.data, other.buffer.size, buffer.data);
91 return *this;
92 }
93
94 AllocatedArray &operator=(AllocatedArray &&other) {
95 std::swap(buffer, other.buffer);
96 return *this;
97 }
98
99 constexpr bool IsNull() const {
100 return buffer.IsNull();
101 }
102
106 constexpr bool empty() const {
107 return buffer.IsEmpty();
108 }
109
113 constexpr size_type size() const {
114 return buffer.size;
115 }
116
117 reference_type front() {
118 return buffer.front();
119 }
120
121 const_reference_type front() const {
122 return buffer.front();
123 }
124
125 reference_type back() {
126 return buffer.back();
127 }
128
129 const_reference_type back() const {
130 return buffer.back();
131 }
132
136 reference_type operator[](size_type i) {
137 assert(i < size());
138
139 return buffer.data[i];
140 }
141
145 const_reference_type operator[](size_type i) const {
146 assert(i < size());
147
148 return buffer.data[i];
149 }
150
151 iterator begin() {
152 return buffer.begin();
153 }
154
155 constexpr const_iterator begin() const {
156 return buffer.cbegin();
157 }
158
159 iterator end() {
160 return buffer.end();
161 }
162
163 constexpr const_iterator end() const {
164 return buffer.cend();
165 }
166
170 void ResizeDiscard(size_type _size) {
171 if (_size == buffer.size)
172 return;
173
174 delete[] buffer.data;
175 buffer.size = _size;
176 buffer.data = new T[buffer.size];
177
178 assert(size() == 0 || buffer.data != nullptr);
179 }
180
186 void GrowDiscard(size_type _size) {
187 if (_size > buffer.size)
188 ResizeDiscard(_size);
189 }
190
195 void GrowPreserve(size_type _size, size_type preserve) {
196 if (_size <= buffer.size)
197 return;
198
199 T *new_data = new T[_size];
200 assert(_size == 0 || new_data != nullptr);
201
202 std::move(buffer.data, buffer.data + preserve, new_data);
203
204 delete[] buffer.data;
205 buffer.data = new_data;
206 buffer.size = _size;
207 }
208
214 void SetSize(size_type _size) {
215 assert(_size <= buffer.size);
216
217 buffer.size = _size;
218 }
219 };
220
221 #endif
WritableBuffer.hxx
AllocatedArray::operator[]
reference_type operator[](size_type i)
Returns one element.
Definition: AllocatedArray.hxx:136
AllocatedArray::GrowDiscard
void GrowDiscard(size_type _size)
Grows the array to the specified size, discarding old data.
Definition: AllocatedArray.hxx:186
AllocatedArray::reference_type
Buffer::reference_type reference_type
Definition: AllocatedArray.hxx:49
WritableBuffer::reference_type
T & reference_type
Definition: WritableBuffer.hxx:83
AllocatedArray::ResizeDiscard
void ResizeDiscard(size_type _size)
Resizes the array, discarding old data.
Definition: AllocatedArray.hxx:170
AllocatedArray::GrowPreserve
void GrowPreserve(size_type _size, size_type preserve)
Grows the array to the specified size, preserving the value of a range of elements, starting from the beginning.
Definition: AllocatedArray.hxx:195
AllocatedArray::front
const_reference_type front() const
Definition: AllocatedArray.hxx:121
AllocatedArray::size_type
Buffer::size_type size_type
Definition: AllocatedArray.hxx:48
WritableBuffer::iterator
pointer_type iterator
Definition: WritableBuffer.hxx:87
AllocatedArray::back
reference_type back()
Definition: AllocatedArray.hxx:125
WritableBuffer
A reference to a memory area that is writable.
Definition: Silence.hxx:27
AllocatedArray::operator=
AllocatedArray & operator=(const AllocatedArray &other)
Definition: AllocatedArray.hxx:82
AllocatedArray::SetSize
void SetSize(size_type _size)
Declare that the buffer has the specified size.
Definition: AllocatedArray.hxx:214
AllocatedArray::AllocatedArray
AllocatedArray(size_type _size)
Definition: AllocatedArray.hxx:60
AllocatedArray::const_iterator
Buffer::const_iterator const_iterator
Definition: AllocatedArray.hxx:52
AllocatedArray::begin
constexpr const_iterator begin() const
Definition: AllocatedArray.hxx:155
AllocatedArray::end
constexpr const_iterator end() const
Definition: AllocatedArray.hxx:163
AllocatedArray::AllocatedArray
constexpr AllocatedArray()=default
AllocatedArray::begin
iterator begin()
Definition: AllocatedArray.hxx:151
WritableBuffer::const_reference_type
const T & const_reference_type
Definition: WritableBuffer.hxx:84
AllocatedArray::const_reference_type
Buffer::const_reference_type const_reference_type
Definition: AllocatedArray.hxx:50
AllocatedArray::AllocatedArray
AllocatedArray(AllocatedArray &&other)
Definition: AllocatedArray.hxx:73
Compiler.h
AllocatedArray::iterator
Buffer::iterator iterator
Definition: AllocatedArray.hxx:51
AllocatedArray::empty
constexpr bool empty() const
Returns true if no memory was allocated so far.
Definition: AllocatedArray.hxx:106
WritableBuffer::const_iterator
const_pointer_type const_iterator
Definition: WritableBuffer.hxx:88
WritableBuffer::Null
static constexpr WritableBuffer Null()
Definition: WritableBuffer.hxx:100
AllocatedArray::~AllocatedArray
~AllocatedArray()
Definition: AllocatedArray.hxx:78
AllocatedArray::IsNull
constexpr bool IsNull() const
Definition: AllocatedArray.hxx:99
AllocatedArray::operator=
AllocatedArray & operator=(AllocatedArray &&other)
Definition: AllocatedArray.hxx:94
AllocatedArray::back
const_reference_type back() const
Definition: AllocatedArray.hxx:129
AllocatedArray::end
iterator end()
Definition: AllocatedArray.hxx:159
AllocatedArray::AllocatedArray
AllocatedArray(const AllocatedArray &other)
Definition: AllocatedArray.hxx:65
AllocatedArray::size
constexpr size_type size() const
Returns the number of allocated elements.
Definition: AllocatedArray.hxx:113
AllocatedArray
An array allocated on the heap with a length determined at runtime.
Definition: Util.hxx:28
AllocatedArray::front
reference_type front()
Definition: AllocatedArray.hxx:117
WritableBuffer::size_type
size_t size_type
Definition: WritableBuffer.hxx:82
AllocatedArray::operator[]
const_reference_type operator[](size_type i) const
Returns one constant element.
Definition: AllocatedArray.hxx:145