Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

flex_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 namespace immer {
16 
17 template <typename T,
18  typename MP,
21 class vector;
22 
23 template <typename T,
24  typename MP,
28 
57 template <typename T,
58  typename MemoryPolicy = default_memory_policy,
60  detail::rbts::bits_t BL = detail::rbts::derive_bits_leaf<T, MemoryPolicy, B>>
62 {
64 
65  using move_t =
66  std::integral_constant<bool, MemoryPolicy::use_transient_rvalues>;
67 
68 public:
69  static constexpr auto bits = B;
70  static constexpr auto bits_leaf = BL;
71  using memory_policy = MemoryPolicy;
72 
73  using value_type = T;
74  using reference = const T&;
76  using difference_type = std::ptrdiff_t;
77  using const_reference = const T&;
78 
81  using reverse_iterator = std::reverse_iterator<iterator>;
82 
84 
89  flex_vector() = default;
90 
94  flex_vector(std::initializer_list<T> values)
96  {}
97 
102  template <typename Iter, typename Sent,
103  std::enable_if_t
104  <detail::compatible_sentinel_v<Iter, Sent>, bool> = true>
105  flex_vector(Iter first, Sent last)
106  : impl_{impl_t::from_range(first, last)}
107  {}
108 
113  flex_vector(size_type n, T v = {})
114  : impl_{impl_t::from_fill(n, v)}
115  {}
116 
123  : impl_ { v.impl_.size, v.impl_.shift,
124  v.impl_.root->inc(), v.impl_.tail->inc() }
125  {}
126 
132  iterator begin() const { return {impl_}; }
133 
138  iterator end() const { return {impl_, typename iterator::end_t{}}; }
139 
146 
153 
158  size_type size() const { return impl_.size; }
159 
164  bool empty() const { return impl_.size == 0; }
165 
169  const T& back() const { return impl_.back(); }
170 
174  const T& front() const { return impl_.front(); }
175 
183  { return impl_.get(index); }
184 
191  reference at(size_type index) const
192  { return impl_.get_check(index); }
193 
197  bool operator==(const flex_vector& other) const
198  { return impl_.equals(other.impl_); }
199  bool operator!=(const flex_vector& other) const
200  { return !(*this == other); }
201 
218  { return impl_.push_back(std::move(value)); }
219 
220  decltype(auto) push_back(value_type value) &&
221  { return push_back_move(move_t{}, std::move(value)); }
222 
239  { return flex_vector{}.push_back(value) + *this; }
240 
258  flex_vector set(size_type index, value_type value) const&
259  { return impl_.assoc(index, std::move(value)); }
260 
261  decltype(auto) set(size_type index, value_type value) &&
262  { return set_move(move_t{}, index, std::move(value)); }
263 
283  template <typename FnT>
284  flex_vector update(size_type index, FnT&& fn) const&
285  { return impl_.update(index, std::forward<FnT>(fn)); }
286 
287  template <typename FnT>
288  decltype(auto) update(size_type index, FnT&& fn) &&
289  { return update_move(move_t{}, index, std::forward<FnT>(fn)); }
290 
307  flex_vector take(size_type elems) const&
308  { return impl_.take(elems); }
309 
310  decltype(auto) take(size_type elems) &&
311  { return take_move(move_t{}, elems); }
312 
329  flex_vector drop(size_type elems) const&
330  { return impl_.drop(elems); }
331 
332  decltype(auto) drop(size_type elems) &&
333  { return drop_move(move_t{}, elems); }
334 
351  friend flex_vector operator+ (const flex_vector& l, const flex_vector& r)
352  { return l.impl_.concat(r.impl_); }
353 
354  friend decltype(auto) operator+ (flex_vector&& l, const flex_vector& r)
355  { return concat_move(move_t{}, std::move(l), r); }
356 
357  friend decltype(auto) operator+ (const flex_vector& l, flex_vector&& r)
358  { return concat_move(move_t{}, l, std::move(r)); }
359 
360  friend decltype(auto) operator+ (flex_vector&& l, flex_vector&& r)
361  { return concat_move(move_t{}, std::move(l), std::move(r)); }
362 
379  flex_vector insert(size_type pos, T value) const&
380  { return take(pos).push_back(std::move(value)) + drop(pos); }
381  decltype(auto) insert(size_type pos, T value) &&
382  {
383  using std::move;
384  auto rs = drop(pos);
385  return std::move(*this).take(pos).push_back(
386  std::move(value)) + std::move(rs);
387  }
388 
390  { return take(pos) + std::move(value) + drop(pos); }
391  decltype(auto) insert(size_type pos, flex_vector value) &&
392  {
393  using std::move;
394  auto rs = drop(pos);
395  return std::move(*this).take(pos) + std::move(value) + std::move(rs);
396  }
397 
414  { return take(pos) + drop(pos + 1); }
415  decltype(auto) erase(size_type pos) &&
416  {
417  auto rs = drop(pos + 1);
418  return std::move(*this).take(pos) + std::move(rs);
419  }
420 
422  { return lpos > pos ? take(pos) + drop(lpos) : *this; }
423  decltype(auto) erase(size_type pos, size_type lpos) &&
424  {
425  if (lpos > pos) {
426  auto rs = drop(lpos);
427  return std::move(*this).take(pos) + std::move(rs);
428  } else {
429  return std::move(*this);
430  }
431  }
432 
437  transient_type transient() const&
438  { return transient_type{ impl_ }; }
439  transient_type transient() &&
440  { return transient_type{ std::move(impl_) }; }
441 
442  // Semi-private
443  const impl_t& impl() const { return impl_; }
444 
445 #if IMMER_DEBUG_PRINT
446  void debug_print(std::ostream& out=std::cerr) const
447  { impl_.debug_print(out); }
448 #endif
449 
450 private:
452 
454  : impl_(std::move(impl))
455  {
456 #if IMMER_DEBUG_PRINT
457  // force the compiler to generate debug_print, so we can call
458  // it from a debugger
459  [](volatile auto){}(&flex_vector::debug_print);
460 #endif
461  }
462 
463  flex_vector&& push_back_move(std::true_type, value_type value)
464  { impl_.push_back_mut({}, std::move(value)); return std::move(*this); }
465  flex_vector push_back_move(std::false_type, value_type value)
466  { return impl_.push_back(std::move(value)); }
467 
468  flex_vector&& set_move(std::true_type, size_type index, value_type value)
469  { impl_.assoc_mut({}, index, std::move(value)); return std::move(*this); }
470  flex_vector set_move(std::false_type, size_type index, value_type value)
471  { return impl_.assoc(index, std::move(value)); }
472 
473  template <typename Fn>
474  flex_vector&& update_move(std::true_type, size_type index, Fn&& fn)
475  { impl_.update_mut({}, index, std::forward<Fn>(fn)); return std::move(*this); }
476  template <typename Fn>
477  flex_vector update_move(std::false_type, size_type index, Fn&& fn)
478  { return impl_.update(index, std::forward<Fn>(fn)); }
479 
480  flex_vector&& take_move(std::true_type, size_type elems)
481  { impl_.take_mut({}, elems); return std::move(*this); }
482  flex_vector take_move(std::false_type, size_type elems)
483  { return impl_.take(elems); }
484 
485  flex_vector&& drop_move(std::true_type, size_type elems)
486  { impl_.drop_mut({}, elems); return std::move(*this); }
487  flex_vector drop_move(std::false_type, size_type elems)
488  { return impl_.drop(elems); }
489 
490  static flex_vector&& concat_move(std::true_type, flex_vector&& l, const flex_vector& r)
491  { concat_mut_l(l.impl_, {}, r.impl_); return std::move(l); }
492  static flex_vector&& concat_move(std::true_type, const flex_vector& l, flex_vector&& r)
493  { concat_mut_r(l.impl_, r.impl_, {}); return std::move(r); }
494  static flex_vector&& concat_move(std::true_type, flex_vector&& l, flex_vector&& r)
495  { concat_mut_lr_l(l.impl_, {}, r.impl_, {}); return std::move(l); }
496  static flex_vector concat_move(std::false_type, const flex_vector& l, const flex_vector& r)
497  { return l.impl_.concat(r.impl_); }
498 
500 };
501 
502 } // namespace immer
static flex_vector && concat_move(std::true_type, const flex_vector &l, flex_vector &&r)
static auto from_fill(size_t n, T v)
Definition: rrbtree.hpp:80
const T & front() const
const auto default_bits
Definition: config.hpp:63
static constexpr auto bits
Definition: flex_vector.hpp:69
const T & back() const
Definition: rrbtree.hpp:488
void update_mut(edit_t e, size_t idx, FnT &&fn)
Definition: rrbtree.hpp:494
flex_vector update(size_type index, FnT &&fn) const &
flex_vector update_move(std::false_type, size_type index, Fn &&fn)
detail::rbts::rrbtree_iterator< T, MemoryPolicy, B, BL > iterator
Definition: flex_vector.hpp:79
iterator end() const
flex_vector erase(size_type pos, size_type lpos) const &
void take_mut(edit_t e, size_t new_size)
Definition: rrbtree.hpp:531
reference operator[](size_type index) const
std::size_t size_t
Definition: bits.hpp:20
Definition: box.hpp:161
static flex_vector && concat_move(std::true_type, flex_vector &&l, const flex_vector &r)
static flex_vector concat_move(std::false_type, const flex_vector &l, const flex_vector &r)
flex_vector()=default
flex_vector erase(size_type pos) const &
friend flex_vector operator+(const flex_vector &l, const flex_vector &r)
std::reverse_iterator< iterator > reverse_iterator
Definition: flex_vector.hpp:81
reference at(size_type index) const
const impl_t & impl() const
flex_vector take_move(std::false_type, size_type elems)
flex_vector drop(size_type elems) const &
reverse_iterator rend() const
static const rrbtree & empty()
Definition: rrbtree.hpp:47
flex_vector(size_type n, T v={})
static auto from_initializer_list(std::initializer_list< U > values)
Definition: rrbtree.hpp:59
void push_back_mut(edit_t e, T value)
Definition: rrbtree.hpp:397
const T & get_check(size_t index) const
Definition: rrbtree.hpp:476
flex_vector push_back_move(std::false_type, value_type value)
detail::rbts::size_t size_type
Definition: flex_vector.hpp:75
flex_vector && push_back_move(std::true_type, value_type value)
memory_policy< default_heap_policy, default_refcount_policy > default_memory_policy
std::integral_constant< bool, MemoryPolicy::use_transient_rvalues > move_t
Definition: flex_vector.hpp:66
bool operator!=(const flex_vector &other) const
flex_vector && drop_move(std::true_type, size_type elems)
flex_vector(impl_t impl)
flex_vector take(size_type elems) const &
std::uint32_t bits_t
Definition: bits.hpp:17
flex_vector(vector< T, MemoryPolicy, B, BL > v)
rrbtree push_back(T value) const
Definition: rrbtree.hpp:418
static flex_vector && concat_move(std::true_type, flex_vector &&l, flex_vector &&r)
iterator begin() const
flex_vector insert(size_type pos, T value) const &
size_type size() const
const T & front() const
Definition: rrbtree.hpp:483
rrbtree update(size_t idx, FnT &&fn) const
Definition: rrbtree.hpp:501
flex_vector && update_move(std::true_type, size_type index, Fn &&fn)
rrbtree drop(size_t elems) const
Definition: rrbtree.hpp:635
reverse_iterator rbegin() const
const T & get(size_t index) const
Definition: rrbtree.hpp:471
flex_vector && set_move(std::true_type, size_type index, value_type value)
bool operator==(const flex_vector &other) const
rrbtree concat(const rrbtree &r) const
Definition: rrbtree.hpp:659
bool equals(const rrbtree &other) const
Definition: rrbtree.hpp:259
void assoc_mut(edit_t e, size_t idx, T value)
Definition: rrbtree.hpp:517
static auto from_range(Iter first, Sent last)
Definition: rrbtree.hpp:71
const T & back() const
void drop_mut(edit_t e, size_t elems)
Definition: rrbtree.hpp:600
flex_vector drop_move(std::false_type, size_type elems)
static constexpr auto bits_leaf
Definition: flex_vector.hpp:70
flex_vector(std::initializer_list< T > values)
Definition: flex_vector.hpp:94
MemoryPolicy memory_policy
Definition: flex_vector.hpp:71
flex_vector push_front(value_type value) const
const T & const_reference
Definition: flex_vector.hpp:77
flex_vector set_move(std::false_type, size_type index, value_type value)
rrbtree take(size_t new_size) const
Definition: rrbtree.hpp:572
bool empty() const
impl_t impl_
Definition: vector.hpp:351
std::ptrdiff_t difference_type
Definition: flex_vector.hpp:76
flex_vector(Iter first, Sent last)
rrbtree assoc(size_t idx, T value) const
Definition: rrbtree.hpp:524
flex_vector && take_move(std::true_type, size_type elems)
flex_vector push_back(value_type value) const &
flex_vector insert(size_type pos, flex_vector value) const &
Released under the MIT license