Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

node.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/detail/util.hpp>
14 
15 #include <limits>
16 
17 namespace immer {
18 namespace detail {
19 namespace arrays {
20 
21 template <typename T, typename MemoryPolicy>
22 struct node
23 {
24  using memory = MemoryPolicy;
25  using heap = typename MemoryPolicy::heap::type;
26  using transience = typename memory::transience_t;
27  using refs_t = typename memory::refcount;
28  using ownee_t = typename transience::ownee;
29  using node_t = node;
30  using edit_t = typename transience::edit;
31 
32  struct data_t
33  {
35  };
36 
38  refs_t,
40 
42 
43  constexpr static std::size_t sizeof_n(size_t count)
44  {
45  return immer_offsetof(impl_t, d.buffer) + sizeof(T) * count;
46  }
47 
48  refs_t& refs() const
49  {
50  return auto_const_cast(get<refs_t>(impl));
51  }
52 
53  const ownee_t& ownee() const { return get<ownee_t>(impl); }
54  ownee_t& ownee() { return get<ownee_t>(impl); }
55 
56  const T* data() const { return reinterpret_cast<const T*>(&impl.d.buffer); }
57  T* data() { return reinterpret_cast<T*>(&impl.d.buffer); }
58 
59  bool can_mutate(edit_t e) const
60  {
61  return refs().unique()
62  || ownee().can_mutate(e);
63  }
64 
65  static void delete_n(node_t* p, size_t sz, size_t cap)
66  {
67  destroy_n(p->data(), sz);
68  heap::deallocate(sizeof_n(cap), p);
69  }
70 
71 
72  static node_t* make_n(size_t n)
73  {
74  return new (heap::allocate(sizeof_n(n))) node_t{};
75  }
76 
77  static node_t* make_e(edit_t e, size_t n)
78  {
79  auto p = make_n(n);
80  p->ownee() = e;
81  return p;
82  }
83 
84  static node_t* fill_n(size_t n, T v)
85  {
86  auto p = make_n(n);
87  try {
88  std::uninitialized_fill_n(p->data(), n, v);
89  return p;
90  } catch (...) {
91  heap::deallocate(sizeof_n(n), p);
92  throw;
93  }
94  }
95 
96  template <typename Iter, typename Sent,
97  std::enable_if_t
98  <detail::compatible_sentinel_v<Iter,Sent>, bool> = true>
99  static node_t* copy_n(size_t n, Iter first, Sent last)
100  {
101  auto p = make_n(n);
102  try {
103  uninitialized_copy(first, last, p->data());
104  return p;
105  } catch (...) {
106  heap::deallocate(sizeof_n(n), p);
107  throw;
108  }
109  }
110 
111  static node_t* copy_n(size_t n, node_t* p, size_t count)
112  {
113  return copy_n(n, p->data(), p->data() + count);
114  }
115 
116  template <typename Iter>
117  static node_t* copy_e(edit_t e, size_t n, Iter first, Iter last)
118  {
119  auto p = copy_n(n, first, last);
120  p->ownee() = e;
121  return p;
122  }
123 
124  static node_t* copy_e(edit_t e, size_t n, node_t* p, size_t count)
125  {
126  return copy_e(e, n, p->data(), p->data() + count);
127  }
128 };
129 
130 } // namespace arrays
131 } // namespace detail
132 } // namespace immer
static node_t * copy_n(size_t n, Iter first, Sent last)
Definition: node.hpp:99
#define immer_offsetof
void destroy_n(T *p, Size n)
Definition: util.hpp:52
typename memory::transience_t transience
Definition: node.hpp:26
MemoryPolicy memory
Definition: node.hpp:24
static node_t * fill_n(size_t n, T v)
Definition: node.hpp:84
bool can_mutate(edit_t e) const
Definition: node.hpp:59
typename MemoryPolicy::heap::type heap
Definition: node.hpp:25
static node_t * make_n(size_t n)
Definition: node.hpp:72
combine_standard_layout_t< data_t, refs_t, ownee_t > impl_t
Definition: node.hpp:39
SinkIter uninitialized_copy(Iterator first, Sentinel last, SinkIter d_first)
Definition: util.hpp:192
std::size_t size_t
Definition: bits.hpp:21
static void delete_n(node_t *p, size_t sz, size_t cap)
Definition: node.hpp:65
typename memory::refcount refs_t
Definition: node.hpp:27
typename transience::ownee ownee_t
Definition: node.hpp:28
static node_t * make_e(edit_t e, size_t n)
Definition: node.hpp:77
static constexpr std::size_t sizeof_n(size_t count)
Definition: node.hpp:43
typename combine_standard_layout< Ts... >::type combine_standard_layout_t
T & auto_const_cast(const T &x)
Definition: util.hpp:32
static node_t * copy_e(edit_t e, size_t n, Iter first, Iter last)
Definition: node.hpp:117
aligned_storage_for< T > buffer
Definition: node.hpp:34
static int count
Definition: tests.c:45
typename std::aligned_storage< sizeof(T), alignof(T)>::type aligned_storage_for
Definition: util.hpp:29
const T * data() const
Definition: node.hpp:56
static node_t * copy_e(edit_t e, size_t n, node_t *p, size_t count)
Definition: node.hpp:124
typename transience::edit edit_t
Definition: node.hpp:30
static node_t * copy_n(size_t n, node_t *p, size_t count)
Definition: node.hpp:111
refs_t & refs() const
Definition: node.hpp:48
const ownee_t & ownee() const
Definition: node.hpp:53
Released under the MIT license