Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

vector.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 
13 #include <immer/memory_policy.hpp>
14 
15 #if IMMER_DEBUG_PRINT
16 #include <immer/flex_vector.hpp>
17 #endif
18 
19 namespace immer {
20 
21 template <typename T,
22  typename MemoryPolicy,
25 class flex_vector;
26 
27 template <typename T,
28  typename MemoryPolicy,
31 class vector_transient;
32 
70 template <typename T,
71  typename MemoryPolicy = default_memory_policy,
73  detail::rbts::bits_t BL = detail::rbts::derive_bits_leaf<T, MemoryPolicy, B>>
74 class vector
75 {
78 
79  using move_t =
80  std::integral_constant<bool, MemoryPolicy::use_transient_rvalues>;
81 
82 public:
83  static constexpr auto bits = B;
84  static constexpr auto bits_leaf = BL;
85  using memory_policy = MemoryPolicy;
86 
87  using value_type = T;
88  using reference = const T&;
90  using difference_type = std::ptrdiff_t;
91  using const_reference = const T&;
92 
95  using reverse_iterator = std::reverse_iterator<iterator>;
96 
98 
103  vector() = default;
104 
108  vector(std::initializer_list<T> values)
110  {}
111 
116  template <typename Iter, typename Sent,
117  std::enable_if_t
118  <detail::compatible_sentinel_v<Iter, Sent>, bool> = true>
119  vector(Iter first, Sent last)
120  : impl_{impl_t::from_range(first, last)}
121  {}
122 
127  vector(size_type n, T v = {})
128  : impl_{impl_t::from_fill(n, v)}
129  {}
130 
136  iterator begin() const { return {impl_}; }
137 
142  iterator end() const { return {impl_, typename iterator::end_t{}}; }
143 
150 
157 
162  size_type size() const { return impl_.size; }
163 
168  bool empty() const { return impl_.size == 0; }
169 
173  const T& back() const { return impl_.back(); }
174 
178  const T& front() const { return impl_.front(); }
179 
187  { return impl_.get(index); }
188 
195  reference at(size_type index) const
196  { return impl_.get_check(index); }
197 
201  bool operator==(const vector& other) const
202  { return impl_.equals(other.impl_); }
203  bool operator!=(const vector& other) const
204  { return !(*this == other); }
205 
222  { return impl_.push_back(std::move(value)); }
223 
224  decltype(auto) push_back(value_type value) &&
225  { return push_back_move(move_t{}, std::move(value)); }
226 
244  vector set(size_type index, value_type value) const&
245  { return impl_.assoc(index, std::move(value)); }
246 
247  decltype(auto) set(size_type index, value_type value) &&
248  { return set_move(move_t{}, index, std::move(value)); }
249 
268  template <typename FnT>
269  vector update(size_type index, FnT&& fn) const&
270  { return impl_.update(index, std::forward<FnT>(fn)); }
271 
272  template <typename FnT>
273  decltype(auto) update(size_type index, FnT&& fn) &&
274  { return update_move(move_t{}, index, std::forward<FnT>(fn)); }
275 
292  vector take(size_type elems) const&
293  { return impl_.take(elems); }
294 
295  decltype(auto) take(size_type elems) &&
296  { return take_move(move_t{}, elems); }
297 
302  transient_type transient() const&
303  { return transient_type{ impl_ }; }
304  transient_type transient() &&
305  { return transient_type{ std::move(impl_) }; }
306 
307  // Semi-private
308  const impl_t& impl() const { return impl_; }
309 
310 #if IMMER_DEBUG_PRINT
311  void debug_print(std::ostream& out=std::cerr) const
312  { flex_t{*this}.debug_print(out); }
313 #endif
314 
315 private:
316  friend flex_t;
318 
320  : impl_(std::move(impl))
321  {
322 #if IMMER_DEBUG_PRINT
323  // force the compiler to generate debug_print, so we can call
324  // it from a debugger
325  [](volatile auto){}(&vector::debug_print);
326 #endif
327  }
328 
329  vector&& push_back_move(std::true_type, value_type value)
330  { impl_.push_back_mut({}, std::move(value)); return std::move(*this); }
331  vector push_back_move(std::false_type, value_type value)
332  { return impl_.push_back(std::move(value)); }
333 
334  vector&& set_move(std::true_type, size_type index, value_type value)
335  { impl_.assoc_mut({}, index, std::move(value)); return std::move(*this); }
336  vector set_move(std::false_type, size_type index, value_type value)
337  { return impl_.assoc(index, std::move(value)); }
338 
339  template <typename Fn>
340  vector&& update_move(std::true_type, size_type index, Fn&& fn)
341  { impl_.update_mut({}, index, std::forward<Fn>(fn)); return std::move(*this); }
342  template <typename Fn>
343  vector update_move(std::false_type, size_type index, Fn&& fn)
344  { return impl_.update(index, std::forward<Fn>(fn)); }
345 
346  vector&& take_move(std::true_type, size_type elems)
347  { impl_.take_mut({}, elems); return std::move(*this); }
348  vector take_move(std::false_type, size_type elems)
349  { return impl_.take(elems); }
350 
352 };
353 
354 } // namespace immer
const T & back() const
Definition: rbtree.hpp:385
vector()=default
rbtree push_back(T value) const
Definition: rbtree.hpp:310
rbtree assoc(size_t idx, T value) const
Definition: rbtree.hpp:420
const auto default_bits
Definition: config.hpp:63
vector && take_move(std::true_type, size_type elems)
Definition: vector.hpp:346
const T & get(size_t index) const
Definition: rbtree.hpp:368
vector take(size_type elems) const &
Definition: vector.hpp:292
reverse_iterator rend() const
Definition: vector.hpp:156
void assoc_mut(edit_t e, size_t idx, T value)
Definition: rbtree.hpp:413
std::size_t size_t
Definition: bits.hpp:20
Definition: box.hpp:161
const T & back() const
Definition: vector.hpp:173
detail::rbts::size_t size_type
Definition: vector.hpp:89
rbtree update(size_t idx, FnT &&fn) const
Definition: rbtree.hpp:398
vector(size_type n, T v={})
Definition: vector.hpp:127
static auto from_range(Iter first, Sent last)
Definition: rbtree.hpp:64
const impl_t & impl() const
Definition: vector.hpp:308
static auto from_fill(size_t n, T v)
Definition: rbtree.hpp:73
const T & front() const
Definition: vector.hpp:178
reverse_iterator rbegin() const
Definition: vector.hpp:149
vector(std::initializer_list< T > values)
Definition: vector.hpp:108
static const rbtree & empty()
Definition: rbtree.hpp:40
friend flex_t
Definition: vector.hpp:316
vector push_back(value_type value) const &
Definition: vector.hpp:221
reference at(size_type index) const
Definition: vector.hpp:195
memory_policy< default_heap_policy, default_refcount_policy > default_memory_policy
vector && update_move(std::true_type, size_type index, Fn &&fn)
Definition: vector.hpp:340
detail::rbts::rbtree_iterator< T, MemoryPolicy, B, BL > iterator
Definition: vector.hpp:93
std::reverse_iterator< iterator > reverse_iterator
Definition: vector.hpp:95
std::ptrdiff_t difference_type
Definition: vector.hpp:90
const T & get_check(size_t index) const
Definition: rbtree.hpp:373
vector && set_move(std::true_type, size_type index, value_type value)
Definition: vector.hpp:334
std::uint32_t bits_t
Definition: bits.hpp:17
const T & front() const
Definition: rbtree.hpp:380
const T & const_reference
Definition: vector.hpp:91
const T & reference
Definition: vector.hpp:88
iterator end() const
Definition: vector.hpp:142
bool operator!=(const vector &other) const
Definition: vector.hpp:203
flex_vector< T, MemoryPolicy, B, BL > flex_t
Definition: vector.hpp:77
vector set_move(std::false_type, size_type index, value_type value)
Definition: vector.hpp:336
bool empty() const
Definition: vector.hpp:168
size_type size() const
Definition: vector.hpp:162
void take_mut(edit_t e, size_t new_size)
Definition: rbtree.hpp:455
MemoryPolicy memory_policy
Definition: vector.hpp:85
iterator begin() const
Definition: vector.hpp:136
bool equals(const rbtree &other) const
Definition: rbtree.hpp:247
void push_back_mut(edit_t e, T value)
Definition: rbtree.hpp:267
vector push_back_move(std::false_type, value_type value)
Definition: vector.hpp:331
void update_mut(edit_t e, size_t idx, FnT &&fn)
Definition: rbtree.hpp:391
static constexpr auto bits_leaf
Definition: vector.hpp:84
friend transient_type
Definition: vector.hpp:317
vector take_move(std::false_type, size_type elems)
Definition: vector.hpp:348
vector(Iter first, Sent last)
Definition: vector.hpp:119
bool operator==(const vector &other) const
Definition: vector.hpp:201
vector update_move(std::false_type, size_type index, Fn &&fn)
Definition: vector.hpp:343
vector(impl_t impl)
Definition: vector.hpp:319
vector update(size_type index, FnT &&fn) const &
Definition: vector.hpp:269
static auto from_initializer_list(std::initializer_list< U > values)
Definition: rbtree.hpp:52
rbtree take(size_t new_size) const
Definition: rbtree.hpp:427
impl_t impl_
Definition: vector.hpp:351
vector && push_back_move(std::true_type, value_type value)
Definition: vector.hpp:329
static constexpr auto bits
Definition: vector.hpp:83
std::integral_constant< bool, MemoryPolicy::use_transient_rvalues > move_t
Definition: vector.hpp:80
reference operator[](size_type index) const
Definition: vector.hpp:186
Released under the MIT license