Metall  v0.28
A persistent memory allocator for data-centric analytics
array.hpp
Go to the documentation of this file.
1 // Copyright 2021 Lawrence Livermore National Security, LLC and other Metall
2 // Project Developers. See the top-level COPYRIGHT file for details.
3 //
4 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
5 
6 #ifndef METALL_JSON_ARRAY_HPP
7 #define METALL_JSON_ARRAY_HPP
8 
9 #include <iostream>
10 #include <memory>
11 #include <algorithm>
12 
15 #include <metall/json/json_fwd.hpp>
16 
17 namespace metall::json {
18 
19 namespace {
20 namespace mc = metall::container;
21 }
22 
23 namespace jsndtl {
24 
27 template <typename allocator_type, typename other_array_type>
29  const other_array_type &other_array) noexcept {
30  if (array.size() != other_array.size()) return false;
31  return std::equal(array.begin(), array.end(), other_array.begin());
32 }
33 
34 } // namespace jsndtl
35 
38 #ifdef DOXYGEN_SKIP
39 template <typename Alloc = std::allocator<std::byte>>
40 #else
41 template <typename Alloc>
42 #endif
43 class array {
44  public:
46 
47  private:
48  template <typename alloc, typename T>
49  using other_scoped_allocator = mc::scoped_allocator_adaptor<
50  typename std::allocator_traits<alloc>::template rebind_alloc<T>>;
51  using aray_allocator_type = other_scoped_allocator<Alloc, value_type>;
53 
54  public:
55  using allocator_type = Alloc;
56  using iterator = typename array_type::iterator;
57  using const_iterator = typename array_type::const_iterator;
58  using reference = value_type &;
59  using const_reference = const value_type &;
60 
62  array() = default;
63 
66  explicit array(const allocator_type &alloc) : m_array(alloc) {}
67 
69  array(const array &) = default;
70 
72  array(const array &other, const allocator_type &alloc)
73  : m_array(other.m_array, alloc) {}
74 
76  array(array &&) noexcept = default;
77 
79  array(array &&other, const allocator_type &alloc) noexcept
80  : m_array(std::move(other.m_array), alloc) {}
81 
83  array &operator=(const array &) = default;
84 
86  array &operator=(array &&) noexcept = default;
87 
89  void swap(array &other) noexcept {
90  using std::swap;
91  swap(m_array, other.m_array);
92  }
93 
96  std::size_t size() const noexcept { return m_array.size(); }
97 
101  std::size_t capacity() const noexcept { return m_array.capacity(); }
102 
105  void resize(const std::size_t size) {
106  m_array.resize(size, value_type{m_array.get_allocator()});
107  }
108 
110  void clear() { m_array.clear(); }
111 
115  reference operator[](const std::size_t index) { return m_array[index]; }
116 
120  const_reference operator[](const std::size_t index) const {
121  return m_array[index];
122  }
123 
126  iterator begin() { return m_array.begin(); }
127 
130  const_iterator begin() const { return m_array.begin(); }
131 
134  iterator end() { return m_array.end(); }
135 
138  const_iterator end() const { return m_array.end(); }
139 
145  iterator erase(iterator position) { return m_array.erase(position); }
146 
152  iterator erase(const_iterator position) { return m_array.erase(position); }
153 
157  void push_back(const value_type &value) { m_array.push_back(value); }
158 
162  void push_back(value_type &&value) { m_array.push_back(std::move(value)); }
163 
169  friend bool operator==(const array &lhs, const array &rhs) noexcept {
170  return jsndtl::general_array_equal(lhs, rhs);
171  }
172 
178  friend bool operator!=(const array &lhs, const array &rhs) noexcept {
179  return !(lhs == rhs);
180  }
181 
183  allocator_type get_allocator() const noexcept {
184  return m_array.get_allocator();
185  }
186 
187  private:
188  array_type m_array{allocator_type{}};
189 };
190 
192 template <typename allocator_type>
193 inline void swap(array<allocator_type> &lhd,
194  array<allocator_type> &rhd) noexcept {
195  lhd.swap(rhd);
196 }
197 
198 } // namespace metall::json
199 
200 #endif // METALL_JSON_ARRAY_HPP
JSON array. An array is an ordered collection of values.
Definition: array.hpp:43
array(const array &)=default
Copy constructor.
const_iterator begin() const
Returns an iterator that is at the beginning of the array.
Definition: array.hpp:130
friend bool operator==(const array &lhs, const array &rhs) noexcept
Return true if two arrays are equal. Arrays are equal when their sizes are the same,...
Definition: array.hpp:169
void swap(array &other) noexcept
Swap contents.
Definition: array.hpp:89
array & operator=(const array &)=default
Copy assignment operator.
Alloc allocator_type
Definition: array.hpp:55
iterator begin()
Returns an iterator that is at the beginning of the array.
Definition: array.hpp:126
array()=default
Constructor.
array(const allocator_type &alloc)
Constructor.
Definition: array.hpp:66
array(array &&) noexcept=default
Move constructor.
void push_back(value_type &&value)
Add an element to the end of the array. Expand (resize) the array if capacity() < size() + 1.
Definition: array.hpp:162
array(const array &other, const allocator_type &alloc)
Allocator-extended copy constructor.
Definition: array.hpp:72
iterator erase(iterator position)
Erases the element at 'position'.
Definition: array.hpp:145
const_iterator end() const
Returns an iterator that is at the end of the array.
Definition: array.hpp:138
void clear()
Clear the contents.
Definition: array.hpp:110
const_reference operator[](const std::size_t index) const
Access an element.
Definition: array.hpp:120
array & operator=(array &&) noexcept=default
Move assignment operator.
void resize(const std::size_t size)
Change the number of elements stored.
Definition: array.hpp:105
iterator erase(const_iterator position)
Erases the element at 'position'.
Definition: array.hpp:152
iterator end()
Returns an iterator that is at the end of the array.
Definition: array.hpp:134
std::size_t size() const noexcept
Returns the number of values.
Definition: array.hpp:96
typename array_type::const_iterator const_iterator
Definition: array.hpp:57
typename array_type::iterator iterator
Definition: array.hpp:56
std::size_t capacity() const noexcept
Returns the number of values that can be held in currently allocated storage.
Definition: array.hpp:101
allocator_type get_allocator() const noexcept
Return an allocator object.
Definition: array.hpp:183
void push_back(const value_type &value)
Add an element to the end of the array. Expand (resize) the array if capacity() < size() + 1.
Definition: array.hpp:157
reference operator[](const std::size_t index)
Access an element.
Definition: array.hpp:115
friend bool operator!=(const array &lhs, const array &rhs) noexcept
Return true if two arrays are not equal. Arrays are equal when their sizes are the same,...
Definition: array.hpp:178
JSON value. A container that holds a single bool, int64, uint64, double, JSON string,...
Definition: value.hpp:82
Namespace for Metall container.
boost::container::vector< T, Allocator > vector
A vector container that uses Metall as its default allocator.
Definition: vector.hpp:17
boost::container::scoped_allocator_adaptor< OuterAlloc, InnerAlloc... > scoped_allocator_adaptor
An allocator which can be used with multilevel containers.
Definition: scoped_allocator.hpp:16
bool general_array_equal(const array< allocator_type > &array, const other_array_type &other_array) noexcept
Provides 'equal' calculation for other array types that have the same interface as the array class.
Definition: array.hpp:28
Namespace for Metall JSON container, which is in an experimental phase.
Definition: array.hpp:17
void swap(array< allocator_type > &lhd, array< allocator_type > &rhd) noexcept
Swap value instances.
Definition: array.hpp:193