Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

array.hpp
Go to the documentation of this file.
1 //
2 // immer: immutable data structures for C++
3 // Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
4 //
5 // This software is distributed under the Boost Software License, Version 1.0.
6 // See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
7 //
8 
9 #pragma once
10 
11 #include <immer/memory_policy.hpp>
13 
14 namespace immer {
15 
16 template <typename T, typename MemoryPolicy>
18 
40 template <typename T, typename MemoryPolicy = default_memory_policy>
41 class array
42 {
43  using impl_t = std::conditional_t<
44  MemoryPolicy::use_transient_rvalues,
47 
48  using move_t =
49  std::integral_constant<bool, MemoryPolicy::use_transient_rvalues>;
50 
51 public:
52  using value_type = T;
53  using reference = const T&;
55  using difference_type = std::ptrdiff_t;
56  using const_reference = const T&;
57 
58  using iterator = const T*;
60  using reverse_iterator = std::reverse_iterator<iterator>;
61 
62  using memory_policy = MemoryPolicy;
64 
69  array() = default;
70 
74  array(std::initializer_list<T> values)
75  : impl_{impl_t::from_initializer_list(values)}
76  {}
77 
82  template <typename Iter, typename Sent,
83  std::enable_if_t
84  <detail::compatible_sentinel_v<Iter, Sent>
85  && detail::is_forward_iterator_v<Iter>, bool> = true>
86  array(Iter first, Sent last)
87  : impl_{impl_t::from_range(first, last)}
88  {}
89 
94  array(size_type n, T v = {})
95  : impl_{impl_t::from_fill(n, v)}
96  {}
97 
103  iterator begin() const { return impl_.data(); }
104 
109  iterator end() const { return impl_.data() + impl_.size; }
110 
117 
124 
129  std::size_t size() const { return impl_.size; }
130 
135  bool empty() const { return impl_.d->empty(); }
136 
140  const T* data() const { return impl_.data(); }
141 
145  const T& back() const { return data()[size() - 1]; }
146 
150  const T& front() const { return data()[0]; }
151 
159  { return impl_.get(index); }
160 
167  reference at(size_type index) const
168  { return impl_.get_check(index); }
169 
173  bool operator==(const array& other) const
174  { return impl_.equals(other.impl_); }
175  bool operator!=(const array& other) const
176  { return !(*this == other); }
177 
193  array push_back(value_type value) const&
194  { return impl_.push_back(std::move(value)); }
195 
196  decltype(auto) push_back(value_type value) &&
197  { return push_back_move(move_t{}, std::move(value)); }
198 
215  array set(std::size_t index, value_type value) const&
216  { return impl_.assoc(index, std::move(value)); }
217 
218  decltype(auto) set(size_type index, value_type value) &&
219  { return set_move(move_t{}, index, std::move(value)); }
220 
238  template <typename FnT>
239  array update(std::size_t index, FnT&& fn) const&
240  { return impl_.update(index, std::forward<FnT>(fn)); }
241 
242  template <typename FnT>
243  decltype(auto) update(size_type index, FnT&& fn) &&
244  { return update_move(move_t{}, index, std::forward<FnT>(fn)); }
245 
262  array take(size_type elems) const&
263  { return impl_.take(elems); }
264 
265  decltype(auto) take(size_type elems) &&
266  { return take_move(move_t{}, elems); }
267 
272  transient_type transient() const&
273  { return transient_type{ impl_ }; }
274  transient_type transient() &&
275  { return transient_type{ std::move(impl_) }; }
276 
277  // Semi-private
278  const impl_t& impl() const { return impl_; }
279 
280 private:
282 
283  array(impl_t impl) : impl_(std::move(impl)) {}
284 
285  array&& push_back_move(std::true_type, value_type value)
286  { impl_.push_back_mut({}, std::move(value)); return std::move(*this); }
287  array push_back_move(std::false_type, value_type value)
288  { return impl_.push_back(std::move(value)); }
289 
290  array&& set_move(std::true_type, size_type index, value_type value)
291  { impl_.assoc_mut({}, index, std::move(value)); return std::move(*this); }
292  array set_move(std::false_type, size_type index, value_type value)
293  { return impl_.assoc(index, std::move(value)); }
294 
295  template <typename Fn>
296  array&& update_move(std::true_type, size_type index, Fn&& fn)
297  { impl_.update_mut({}, index, std::forward<Fn>(fn)); return std::move(*this); }
298  template <typename Fn>
299  array update_move(std::false_type, size_type index, Fn&& fn)
300  { return impl_.update(index, std::forward<Fn>(fn)); }
301 
302  array&& take_move(std::true_type, size_type elems)
303  { impl_.take_mut({}, elems); return std::move(*this); }
304  array take_move(std::false_type, size_type elems)
305  { return impl_.take(elems); }
306 
308 };
309 
310 } /* namespace immer */
reverse_iterator rend() const
Definition: array.hpp:123
const impl_t & impl() const
Definition: array.hpp:278
bool empty() const
Definition: array.hpp:135
T value_type
Definition: array.hpp:52
array push_back(value_type value) const &
Definition: array.hpp:193
reference operator[](size_type index) const
Definition: array.hpp:158
array push_back_move(std::false_type, value_type value)
Definition: array.hpp:287
std::conditional_t< MemoryPolicy::use_transient_rvalues, detail::arrays::with_capacity< T, MemoryPolicy >, detail::arrays::no_capacity< T, MemoryPolicy > > impl_t
Definition: array.hpp:46
std::ptrdiff_t difference_type
Definition: array.hpp:55
iterator end() const
Definition: array.hpp:109
array && push_back_move(std::true_type, value_type value)
Definition: array.hpp:285
reference at(size_type index) const
Definition: array.hpp:167
reverse_iterator rbegin() const
Definition: array.hpp:116
Definition: box.hpp:161
const T & back() const
Definition: array.hpp:145
const T * data() const
Definition: array.hpp:140
array take_move(std::false_type, size_type elems)
Definition: array.hpp:304
array(impl_t impl)
Definition: array.hpp:283
const impl< T, B, MP > empty
MemoryPolicy memory_policy
Definition: array.hpp:62
std::integral_constant< bool, MemoryPolicy::use_transient_rvalues > move_t
Definition: array.hpp:49
array update_move(std::false_type, size_type index, Fn &&fn)
Definition: array.hpp:299
std::size_t size_t
Definition: bits.hpp:21
const T & const_reference
Definition: array.hpp:56
array && update_move(std::true_type, size_type index, Fn &&fn)
Definition: array.hpp:296
std::reverse_iterator< iterator > reverse_iterator
Definition: array.hpp:60
array(size_type n, T v={})
Definition: array.hpp:94
iterator begin() const
Definition: array.hpp:103
array update(std::size_t index, FnT &&fn) const &
Definition: array.hpp:239
bool operator!=(const array &other) const
Definition: array.hpp:175
array(std::initializer_list< T > values)
Definition: array.hpp:74
const T & front() const
Definition: array.hpp:150
const T * iterator
Definition: array.hpp:58
array()=default
iterator const_iterator
Definition: array.hpp:59
bool operator==(const array &other) const
Definition: array.hpp:173
array take(size_type elems) const &
Definition: array.hpp:262
array(Iter first, Sent last)
Definition: array.hpp:86
array && take_move(std::true_type, size_type elems)
Definition: array.hpp:302
friend transient_type
Definition: array.hpp:281
array set_move(std::false_type, size_type index, value_type value)
Definition: array.hpp:292
std::size_t size_type
Definition: array.hpp:54
impl_t impl_
Definition: array.hpp:307
std::size_t size() const
Definition: array.hpp:129
array && set_move(std::true_type, size_type index, value_type value)
Definition: array.hpp:290
const T & reference
Definition: array.hpp:53
Released under the MIT license