MPD  0.20.6
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;
51  typedef typename Buffer::iterator 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 
74  :buffer(other.buffer) {
75  other.buffer = Buffer::Null();
76  }
77 
79  delete[] buffer.data;
80  }
81 
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 
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
reference_type operator[](size_type i)
Returns one element.
void GrowDiscard(size_type _size)
Grows the array to the specified size, discarding old data.
Buffer::reference_type reference_type
void ResizeDiscard(size_type _size)
Resizes the array, discarding old data.
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.
const_reference_type front() const
Buffer::size_type size_type
pointer_type iterator
reference_type back()
A reference to a memory area that is writable.
Definition: Silence.hxx:27
AllocatedArray & operator=(const AllocatedArray &other)
void SetSize(size_type _size)
Declare that the buffer has the specified size.
AllocatedArray(size_type _size)
Buffer::const_iterator const_iterator
constexpr const_iterator begin() const
constexpr const_iterator end() const
constexpr AllocatedArray()=default
const T & const_reference_type
Buffer::const_reference_type const_reference_type
AllocatedArray(AllocatedArray &&other)
Buffer::iterator iterator
constexpr bool empty() const
Returns true if no memory was allocated so far.
const_pointer_type const_iterator
static constexpr WritableBuffer Null()
constexpr bool IsNull() const
AllocatedArray & operator=(AllocatedArray &&other)
const_reference_type back() const
AllocatedArray(const AllocatedArray &other)
constexpr size_type size() const
Returns the number of allocated elements.
An array allocated on the heap with a length determined at runtime.
Definition: Util.hxx:28
reference_type front()
const_reference_type operator[](size_type i) const
Returns one constant element.