| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#ifndef AVOGADRO_CORE_ARRAY_H |
| 7 |
#define AVOGADRO_CORE_ARRAY_H |
| 8 |
|
| 9 |
#include "avogadrocore.h" |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
namespace Avogadro::Core { |
| 15 |
|
| 16 |
using std::size_t; |
| 17 |
|
| 18 |
namespace internal { |
| 19 |
|
| 20 |
template <typename T> |
| 21 |
class ArrayRefContainer |
| 22 |
{ |
| 23 |
public: |
| 24 |
using ValueType = T; |
| 25 |
using Parent = std::vector<T>; // The parent container for iterators etc |
| 26 |
|
| 27 |
// STL compatibility, forward typedefs from std::vector: |
| 28 |
using value_type = typename Parent::value_type; |
| 29 |
using allocator_type = typename Parent::allocator_type; |
| 30 |
using reference = typename Parent::reference; |
| 31 |
using const_reference = typename Parent::const_reference; |
| 32 |
using pointer = typename Parent::pointer; |
| 33 |
using const_pointer = typename Parent::const_pointer; |
| 34 |
using iterator = typename Parent::iterator; |
| 35 |
using const_iterator = typename Parent::const_iterator; |
| 36 |
using reverse_iterator = typename Parent::reverse_iterator; |
| 37 |
using const_reverse_iterator = typename Parent::const_reverse_iterator; |
| 38 |
using difference_type = typename Parent::difference_type; |
| 39 |
using size_type = typename Parent::size_type; |
| 40 |
|
| 41 |
ArrayRefContainer() : m_ref(1), data() {} |
| 42 |
|
| 43 |
explicit ArrayRefContainer(const size_t n, |
| 44 |
const ValueType& value = ValueType()) |
| 45 |
: m_ref(1), data(n, value) |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
ArrayRefContainer(const ArrayRefContainer& other) : m_ref(1), data(other.data) |
| 50 |
{ |
| 51 |
} |
| 52 |
|
| 53 |
template <typename InputIterator> |
| 54 |
ArrayRefContainer(InputIterator first, InputIterator last) |
| 55 |
: m_ref(1), data(first, last) |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
// Increment the reference count. |
| 60 |
void reref() { ++m_ref; } |
| 61 |
|
| 62 |
// Decrement the reference count, return true unless the reference count has |
| 63 |
// dropped to zero. When it returns false, this object should be deleted. |
| 64 |
bool deref() |
| 65 |
{ |
| 66 |
if (m_ref) |
| 67 |
--m_ref; |
| 68 |
return m_ref > 0; |
| 69 |
} |
| 70 |
|
| 71 |
unsigned int ref() const { return m_ref; } |
| 72 |
|
| 73 |
// Reference count |
| 74 |
unsigned int m_ref; |
| 75 |
// Container for our data |
| 76 |
std::vector<T> data; |
| 77 |
}; |
| 78 |
|
| 79 |
} // namespace internal |
| 80 |
|
| 81 |
/** |
| 82 |
* @class Array array.h <avogadro/core/array.h> |
| 83 |
* @brief Base class for array containers. |
| 84 |
* |
| 85 |
* This templated class gives us a container with copy-on-write semantics, |
| 86 |
* allowing for functions to effectively share data without exposing access or |
| 87 |
* copying large amounts of data until the container is changed. |
| 88 |
* |
| 89 |
* All const functions can be called without copying any data, but a call to a |
| 90 |
* non-const function will trigger a detach call. This is a no-op when the |
| 91 |
* reference count is 1, and will perform a deep copy when the reference count |
| 92 |
* is greater than 1. |
| 93 |
*/ |
| 94 |
template <typename T> |
| 95 |
class Array |
| 96 |
{ |
| 97 |
public: |
| 98 |
using Container = internal::ArrayRefContainer<T>; |
| 99 |
using ValueType = T; |
| 100 |
|
| 101 |
/** Typedefs for STL compatibility @{ */ |
| 102 |
using value_type = typename Container::value_type; |
| 103 |
using allocator_type = typename Container::allocator_type; |
| 104 |
using reference = typename Container::reference; |
| 105 |
using const_reference = typename Container::const_reference; |
| 106 |
using pointer = typename Container::pointer; |
| 107 |
using const_pointer = typename Container::const_pointer; |
| 108 |
using iterator = typename Container::iterator; |
| 109 |
using const_iterator = typename Container::const_iterator; |
| 110 |
using reverse_iterator = typename Container::reverse_iterator; |
| 111 |
using const_reverse_iterator = typename Container::const_reverse_iterator; |
| 112 |
using difference_type = typename Container::difference_type; |
| 113 |
using size_type = typename Container::size_type; |
| 114 |
/** @} */ |
| 115 |
|
| 116 |
/** Constructors for new containers. */ |
| 117 |
Array() : d(new Container()) {} |
| 118 |
|
| 119 |
explicit Array(const size_t n, const ValueType& value = ValueType()) |
| 120 |
: d(new Container(n, value)) |
| 121 |
{ |
| 122 |
} |
| 123 |
|
| 124 |
template <typename InputIterator> |
| 125 |
Array(InputIterator first, InputIterator last) : d(new Container(first, last)) |
| 126 |
{ |
| 127 |
} |
| 128 |
|
| 129 |
/** Copy constructor, note the copy made of the internal data of other. */ |
| 130 |
Array(const Array& other) |
| 131 |
{ |
| 132 |
other.d->reref(); |
| 133 |
d = other.d; |
| 134 |
} |
| 135 |
|
| 136 |
~Array(); |
| 137 |
|
| 138 |
/** |
| 139 |
* Explicitly detach from any other implicitly shared containers. This is not |
| 140 |
* normally necessary, but can be useful when you want to ensure you have a |
| 141 |
* copy of all data. |
| 142 |
*/ |
| 143 |
void detachWithCopy(); |
| 144 |
|
| 145 |
/** |
| 146 |
* Explicitly detach from any other implicitly shared containers. This |
| 147 |
* version does not copy the data. |
| 148 |
*/ |
| 149 |
void detach(); |
| 150 |
|
| 151 |
/** Retrieve a pointer to the underlying data. */ |
| 152 |
T* data() |
| 153 |
{ |
| 154 |
detachWithCopy(); |
| 155 |
return d->data.data(); |
| 156 |
} |
| 157 |
|
| 158 |
const T* data() const { return d->data.data(); } |
| 159 |
|
| 160 |
const T* constData() const { return d->data.data(); } |
| 161 |
|
| 162 |
size_t size() const { return d->data.size(); } |
| 163 |
|
| 164 |
size_t max_size() const { return d->data.max_size(); } |
| 165 |
|
| 166 |
bool empty() const { return d->data.empty(); } |
| 167 |
|
| 168 |
size_t capacity() const { return d->data.capacity(); } |
| 169 |
|
| 170 |
void reserve(const size_t& sz) |
| 171 |
{ |
| 172 |
detachWithCopy(); |
| 173 |
d->data.reserve(sz); |
| 174 |
} |
| 175 |
|
| 176 |
void resize(const size_t& sz, const ValueType& t = ValueType()) |
| 177 |
{ |
| 178 |
detachWithCopy(); |
| 179 |
d->data.resize(sz, t); |
| 180 |
} |
| 181 |
|
| 182 |
void clear() |
| 183 |
{ |
| 184 |
detach(); |
| 185 |
d->data.clear(); |
| 186 |
} |
| 187 |
|
| 188 |
const_iterator begin() const { return d->data.begin(); } |
| 189 |
|
| 190 |
const_iterator end() const { return d->data.end(); } |
| 191 |
|
| 192 |
iterator begin() |
| 193 |
{ |
| 194 |
detachWithCopy(); |
| 195 |
return d->data.begin(); |
| 196 |
} |
| 197 |
|
| 198 |
iterator end() |
| 199 |
{ |
| 200 |
detachWithCopy(); |
| 201 |
return d->data.end(); |
| 202 |
} |
| 203 |
|
| 204 |
const_reverse_iterator rbegin() const { return d->data.rbegin(); } |
| 205 |
|
| 206 |
const_reverse_iterator rend() const { return d->data.rend(); } |
| 207 |
|
| 208 |
reverse_iterator rbegin() |
| 209 |
{ |
| 210 |
detachWithCopy(); |
| 211 |
return d->data.rbegin(); |
| 212 |
} |
| 213 |
|
| 214 |
reverse_iterator rend() |
| 215 |
{ |
| 216 |
detachWithCopy(); |
| 217 |
return d->data.rend(); |
| 218 |
} |
| 219 |
|
| 220 |
reference front() |
| 221 |
{ |
| 222 |
detachWithCopy(); |
| 223 |
return d->data.front(); |
| 224 |
} |
| 225 |
|
| 226 |
const_reference front() const { return d->data.front(); } |
| 227 |
|
| 228 |
reference back() |
| 229 |
{ |
| 230 |
detachWithCopy(); |
| 231 |
return d->data.back(); |
| 232 |
} |
| 233 |
|
| 234 |
const_reference back() const { return d->data.back(); } |
| 235 |
|
| 236 |
template <class InputIterator> |
| 237 |
void assign(InputIterator first, InputIterator last) |
| 238 |
{ |
| 239 |
detachWithCopy(); |
| 240 |
d->data.assign(first, last); |
| 241 |
} |
| 242 |
|
| 243 |
void assign(size_type n, const value_type& val) |
| 244 |
{ |
| 245 |
detachWithCopy(); |
| 246 |
d->data.assign(n, val); |
| 247 |
} |
| 248 |
|
| 249 |
void push_back(const ValueType& v) |
| 250 |
{ |
| 251 |
detachWithCopy(); |
| 252 |
d->data.push_back(v); |
| 253 |
} |
| 254 |
|
| 255 |
void pop_back() |
| 256 |
{ |
| 257 |
detachWithCopy(); |
| 258 |
d->data.pop_back(); |
| 259 |
} |
| 260 |
|
| 261 |
iterator insert(iterator position, const value_type& val) |
| 262 |
{ |
| 263 |
detachWithCopy(); |
| 264 |
return d->data.insert(position, val); |
| 265 |
} |
| 266 |
|
| 267 |
void insert(iterator position, size_type n, const value_type& val) |
| 268 |
{ |
| 269 |
detachWithCopy(); |
| 270 |
d->data.insert(position, n, val); |
| 271 |
} |
| 272 |
|
| 273 |
template <class InputIterator> |
| 274 |
void insert(iterator position, InputIterator first, InputIterator last) |
| 275 |
{ |
| 276 |
detachWithCopy(); |
| 277 |
d->data.insert(position, first, last); |
| 278 |
} |
| 279 |
|
| 280 |
iterator erase(iterator position) |
| 281 |
{ |
| 282 |
detachWithCopy(); |
| 283 |
return d->data.erase(position); |
| 284 |
} |
| 285 |
|
| 286 |
iterator erase(iterator first, iterator last) |
| 287 |
{ |
| 288 |
detachWithCopy(); |
| 289 |
return d->data.erase(first, last); |
| 290 |
} |
| 291 |
|
| 292 |
const ValueType& operator[](const std::size_t& idx) const |
| 293 |
{ |
| 294 |
return d->data[idx]; |
| 295 |
} |
| 296 |
|
| 297 |
ValueType& operator[](const std::size_t& idx) |
| 298 |
{ |
| 299 |
detachWithCopy(); |
| 300 |
return d->data[idx]; |
| 301 |
} |
| 302 |
|
| 303 |
ValueType at(const std::size_t& idx) const { return d->data.at(idx); } |
| 304 |
|
| 305 |
template <typename OtherT> |
| 306 |
Array& operator=(const std::vector<OtherT>& v) |
| 307 |
{ |
| 308 |
detach(); |
| 309 |
d->data = v; |
| 310 |
return *this; |
| 311 |
} |
| 312 |
|
| 313 |
template <typename OtherT> |
| 314 |
Array& operator=(const Array<OtherT>& v) |
| 315 |
{ |
| 316 |
detach(); |
| 317 |
d->data = v.d->data; |
| 318 |
return *this; |
| 319 |
} |
| 320 |
|
| 321 |
Array& operator=(const Array& v) |
| 322 |
{ |
| 323 |
if (this != &v) { |
| 324 |
detach(); |
| 325 |
d->data = v.d->data; |
| 326 |
} |
| 327 |
return *this; |
| 328 |
} |
| 329 |
|
| 330 |
void swap(Array<ValueType>& other) |
| 331 |
{ |
| 332 |
using std::swap; |
| 333 |
swap(d, other.d); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* @param index array position to delete |
| 338 |
* if the index is valid swap it with the last position and pop back. |
| 339 |
* This function does not preserve the elements order. |
| 340 |
*/ |
| 341 |
void swapAndPop(Index index) |
| 342 |
{ |
| 343 |
if (index >= d->data.size()) { |
| 344 |
return; |
| 345 |
} |
| 346 |
detachWithCopy(); |
| 347 |
if (index != d->data.size() - 1) { |
| 348 |
d->data[index] = d->data.back(); |
| 349 |
} |
| 350 |
d->data.pop_back(); |
| 351 |
} |
| 352 |
|
| 353 |
protected: |
| 354 |
Container* d; |
| 355 |
}; |
| 356 |
|
| 357 |
template <typename T> |
| 358 |
inline Array<T>::~Array() |
| 359 |
{ |
| 360 |
if (d && !d->deref()) |
| 361 |
delete d; |
| 362 |
} |
| 363 |
|
| 364 |
template <typename T> |
| 365 |
inline void Array<T>::detachWithCopy() |
| 366 |
{ |
| 367 |
if (d && d->ref() != 1) { |
| 368 |
auto* o = new Container(*d); |
| 369 |
d->deref(); |
| 370 |
d = o; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
template <typename T> |
| 375 |
inline void Array<T>::detach() |
| 376 |
{ |
| 377 |
if (d && d->ref() != 1) { |
| 378 |
d->deref(); |
| 379 |
d = new Container; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
template <typename T> |
| 384 |
inline bool operator==(const Array<T>& lhs, const Array<T>& rhs) |
| 385 |
{ |
| 386 |
return lhs.size() == rhs.size() && |
| 387 |
std::equal(lhs.begin(), lhs.end(), rhs.begin()); |
| 388 |
} |
| 389 |
|
| 390 |
template <typename T> |
| 391 |
inline bool operator!=(const Array<T>& lhs, const Array<T>& rhs) |
| 392 |
{ |
| 393 |
return !(lhs == rhs); |
| 394 |
} |
| 395 |
|
| 396 |
template <typename T> |
| 397 |
inline bool operator<(const Array<T>& lhs, const Array<T>& rhs) |
| 398 |
{ |
| 399 |
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), |
| 400 |
rhs.end()); |
| 401 |
} |
| 402 |
|
| 403 |
template <typename T> |
| 404 |
inline bool operator>(const Array<T>& lhs, const Array<T>& rhs) |
| 405 |
{ |
| 406 |
return rhs < lhs; |
| 407 |
} |
| 408 |
|
| 409 |
template <typename T> |
| 410 |
inline bool operator<=(const Array<T>& lhs, const Array<T>& rhs) |
| 411 |
{ |
| 412 |
return !(rhs < lhs); |
| 413 |
} |
| 414 |
|
| 415 |
template <typename T> |
| 416 |
inline bool operator>=(const Array<T>& lhs, const Array<T>& rhs) |
| 417 |
{ |
| 418 |
return !(lhs < rhs); |
| 419 |
} |
| 420 |
|
| 421 |
template <typename T> |
| 422 |
inline void swap(Array<T>& lhs, Array<T>& rhs) |
| 423 |
{ |
| 424 |
lhs.swap(rhs); |
| 425 |
} |
| 426 |
|
| 427 |
} // namespace Avogadro::Core |
| 428 |
|
| 429 |
#endif // AVOGADRO_CORE_ARRAY_H |