forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxadapt.hpp
More file actions
603 lines (563 loc) · 26.7 KB
/
Copy pathxadapt.hpp
File metadata and controls
603 lines (563 loc) · 26.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XTENSOR_ADAPT_HPP
#define XTENSOR_ADAPT_HPP
#include <array>
#include <cstddef>
#include <memory>
#include <type_traits>
#include <xtl/xsequence.hpp>
#include "xarray.hpp"
#include "xtensor.hpp"
#include "xfixed.hpp"
#include "xbuffer_adaptor.hpp"
namespace xt
{
namespace detail
{
template <class>
struct array_size_impl;
template <class T, std::size_t N>
struct array_size_impl<std::array<T, N>>
{
static constexpr std::size_t value = N;
};
template <class C>
using array_size = array_size_impl<std::decay_t<C>>;
template <class P>
struct default_allocator_for_ptr
{
using type = std::allocator<std::remove_const_t<std::remove_pointer_t<std::remove_reference_t<P>>>>;
};
template <class P>
using default_allocator_for_ptr_t = typename default_allocator_for_ptr<P>::type;
template <class T>
using not_an_array = xtl::negation<is_array<T>>;
template <class T>
using not_a_pointer = xtl::negation<std::is_pointer<T>>;
template <class T>
using not_a_layout = xtl::negation<std::is_same<layout_type,T>>;
}
/**************************
* xarray_adaptor builder *
**************************/
/**
* Constructs an xarray_adaptor of the given stl-like container,
* with the specified shape and layout.
* @param container the container to adapt
* @param shape the shape of the xarray_adaptor
* @param l the layout_type of the xarray_adaptor
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, class SC,
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
detail::not_a_pointer<C>)>
inline xarray_adaptor<xtl::closure_type_t<C>, L, std::decay_t<SC>>
adapt(C&& container, const SC& shape, layout_type l = L)
{
static_assert(!xtl::is_integral<SC>::value, "shape cannot be a integer");
using return_type = xarray_adaptor<xtl::closure_type_t<C>, L, std::decay_t<SC>>;
return return_type(std::forward<C>(container), shape, l);
}
/**
* Constructs an non-owning xarray_adaptor from a pointer with the specified shape and layout.
* @param pointer the container to adapt
* @param shape the shape of the xarray_adaptor
* @param l the layout_type of the xarray_adaptor
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, class SC,
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
std::is_pointer<C>)>
inline auto adapt(C&& pointer, const SC& shape, layout_type l = L)
{
static_assert(!xtl::is_integral<SC>::value, "shape cannot be a integer");
using buffer_type = xbuffer_adaptor<C, xt::no_ownership, detail::default_allocator_for_ptr_t<C>>;
using return_type = xarray_adaptor<buffer_type, L, std::decay_t<SC>>;
std::size_t size = compute_size(shape);
return return_type(buffer_type(pointer, size), shape, l);
}
/**
* Constructs an xarray_adaptor of the given stl-like container,
* with the specified shape and strides.
* @param container the container to adapt
* @param shape the shape of the xarray_adaptor
* @param strides the strides of the xarray_adaptor
*/
template <class C, class SC, class SS,
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
detail::not_a_layout<std::decay_t<SS>>)>
inline xarray_adaptor<xtl::closure_type_t<C>, layout_type::dynamic, std::decay_t<SC>>
adapt(C&& container, SC&& shape, SS&& strides)
{
static_assert(!xtl::is_integral<std::decay_t<SC>>::value, "shape cannot be a integer");
using return_type = xarray_adaptor<xtl::closure_type_t<C>, layout_type::dynamic, std::decay_t<SC>>;
return return_type(std::forward<C>(container),
xtl::forward_sequence<typename return_type::inner_shape_type, SC>(shape),
xtl::forward_sequence<typename return_type::inner_strides_type, SS>(strides));
}
/**
* Constructs an xarray_adaptor of the given dynamically allocated C array,
* with the specified shape and layout.
* @param pointer the pointer to the beginning of the dynamic array
* @param size the size of the dynamic array
* @param ownership indicates whether the adaptor takes ownership of the array.
* Possible values are ``no_ownership()`` or ``acquire_ownership()``
* @param shape the shape of the xarray_adaptor
* @param l the layout_type of the xarray_adaptor
* @param alloc the allocator used for allocating / deallocating the dynamic array
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class O, class SC, class A = detail::default_allocator_for_ptr_t<P>,
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>)>
inline xarray_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, L, SC>
adapt(P&& pointer, typename A::size_type size, O ownership, const SC& shape, layout_type l = L, const A& alloc = A())
{
static_assert(!xtl::is_integral<SC>::value, "shape cannot be a integer");
(void)ownership;
using buffer_type = xbuffer_adaptor<xtl::closure_type_t<P>, O, A>;
using return_type = xarray_adaptor<buffer_type, L, SC>;
buffer_type buf(std::forward<P>(pointer), size, alloc);
return return_type(std::move(buf), shape, l);
}
/**
* Constructs an xarray_adaptor of the given dynamically allocated C array,
* with the specified shape and strides.
* @param pointer the pointer to the beginning of the dynamic array
* @param size the size of the dynamic array
* @param ownership indicates whether the adaptor takes ownership of the array.
* Possible values are ``no_ownership()`` or ``acquire_ownership()``
* @param shape the shape of the xarray_adaptor
* @param strides the strides of the xarray_adaptor
* @param alloc the allocator used for allocating / deallocating the dynamic array
*/
template <class P, class O, class SC, class SS, class A = detail::default_allocator_for_ptr_t<P>,
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
detail::not_a_layout<std::decay_t<SS>>)>
inline xarray_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, layout_type::dynamic, std::decay_t<SC>>
adapt(P&& pointer, typename A::size_type size, O ownership, SC&& shape, SS&& strides, const A& alloc = A())
{
static_assert(!xtl::is_integral<std::decay_t<SC>>::value, "shape cannot be a integer");
(void)ownership;
using buffer_type = xbuffer_adaptor<xtl::closure_type_t<P>, O, A>;
using return_type = xarray_adaptor<buffer_type, layout_type::dynamic, std::decay_t<SC>>;
buffer_type buf(std::forward<P>(pointer), size, alloc);
return return_type(std::move(buf),
xtl::forward_sequence<typename return_type::inner_shape_type, SC>(shape),
xtl::forward_sequence<typename return_type::inner_strides_type, SS>(strides));
}
/**
* Contructs an xarray_adaptor of the given C array allocated on the stack, with the
* specified shape and layout.
* @param c_array the C array allocated on the stack
* @param shape the shape of the xarray_adaptor
* @param l the layout_type of the xarray_adaptor
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class T, std::size_t N, class SC,
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>)>
inline auto adapt(T (&c_array)[N], const SC& shape, layout_type l = L)
{
return adapt(&c_array[0], N, xt::no_ownership(), shape, l);
}
/**
* Contructs an xarray_adaptor of the given C array allocated on the stack, with the
* specified shape and stirdes.
* @param c_array the C array allocated on the stack
* @param shape the shape of the xarray_adaptor
* @param strides the strides of the xarray_adaptor
*/
template <class T, std::size_t N, class SC, class SS,
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
detail::not_a_layout<std::decay_t<SS>>)>
inline auto adapt(T (&c_array)[N], SC&& shape, SS&& strides)
{
return adapt(&c_array[0], N, xt::no_ownership(),
std::forward<SC>(shape),
std::forward<SS>(strides));
}
/***************************
* xtensor_adaptor builder *
***************************/
/**
* Constructs a 1-D xtensor_adaptor of the given stl-like container,
* with the specified layout_type.
* @param container the container to adapt
* @param l the layout_type of the xtensor_adaptor
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C>
inline xtensor_adaptor<C, 1, L>
adapt(C&& container, layout_type l = L)
{
const std::array<typename std::decay_t<C>::size_type, 1> shape{container.size()};
using return_type = xtensor_adaptor<xtl::closure_type_t<C>, 1, L>;
return return_type(std::forward<C>(container), shape, l);
}
/**
* Constructs an xtensor_adaptor of the given stl-like container,
* with the specified shape and layout_type.
* @param container the container to adapt
* @param shape the shape of the xtensor_adaptor
* @param l the layout_type of the xtensor_adaptor
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, class SC,
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>,
detail::not_a_pointer<C>)>
inline xtensor_adaptor<C, detail::array_size<SC>::value, L>
adapt(C&& container, const SC& shape, layout_type l = L)
{
static_assert(!xtl::is_integral<SC>::value, "shape cannot be a integer");
constexpr std::size_t N = detail::array_size<SC>::value;
using return_type = xtensor_adaptor<xtl::closure_type_t<C>, N, L>;
return return_type(std::forward<C>(container), shape, l);
}
/**
* Constructs an non-owning xtensor_adaptor from a pointer with the specified shape and layout.
* @param pointer the pointer to adapt
* @param shape the shape of the xtensor_adaptor
* @param l the layout_type of the xtensor_adaptor
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, class SC,
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>,
std::is_pointer<C>)>
inline auto adapt(C&& pointer, const SC& shape, layout_type l = L)
{
static_assert(!xtl::is_integral<SC>::value, "shape cannot be a integer");
using buffer_type = xbuffer_adaptor<C, xt::no_ownership, detail::default_allocator_for_ptr_t<C>>;
constexpr std::size_t N = detail::array_size<SC>::value;
using return_type = xtensor_adaptor<buffer_type, N, L>;
return return_type(buffer_type(pointer, compute_size(shape)), shape, l);
}
/**
* Constructs an xtensor_adaptor of the given stl-like container,
* with the specified shape and strides.
* @param container the container to adapt
* @param shape the shape of the xtensor_adaptor
* @param strides the strides of the xtensor_adaptor
*/
template <class C, class SC, class SS,
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>,
detail::not_a_layout<std::decay_t<SS>>)>
inline xtensor_adaptor<C, detail::array_size<SC>::value, layout_type::dynamic>
adapt(C&& container, SC&& shape, SS&& strides)
{
static_assert(!xtl::is_integral<std::decay_t<SC>>::value, "shape cannot be a integer");
constexpr std::size_t N = detail::array_size<SC>::value;
using return_type = xtensor_adaptor<xtl::closure_type_t<C>, N, layout_type::dynamic>;
return return_type(std::forward<C>(container),
xtl::forward_sequence<typename return_type::inner_shape_type, SC>(shape),
xtl::forward_sequence<typename return_type::inner_strides_type, SS>(strides));
}
/**
* Constructs a 1-D xtensor_adaptor of the given dynamically allocated C array,
* with the specified layout.
* @param pointer the pointer to the beginning of the dynamic array
* @param size the size of the dynamic array
* @param ownership indicates whether the adaptor takes ownership of the array.
* Possible values are ``no_ownership()`` or ``acquire_ownership()``
* @param l the layout_type of the xtensor_adaptor
* @param alloc the allocator used for allocating / deallocating the dynamic array
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class O, class A = detail::default_allocator_for_ptr_t<P>>
inline xtensor_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, 1, L>
adapt(P&& pointer, typename A::size_type size, O ownership, layout_type l = L, const A& alloc = A())
{
(void)ownership;
using buffer_type = xbuffer_adaptor<xtl::closure_type_t<P>, O, A>;
using return_type = xtensor_adaptor<buffer_type, 1, L>;
buffer_type buf(std::forward<P>(pointer), size, alloc);
const std::array<typename A::size_type, 1> shape{size};
return return_type(std::move(buf), shape, l);
}
/**
* Constructs an xtensor_adaptor of the given dynamically allocated C array,
* with the specified shape and layout.
* @param pointer the pointer to the beginning of the dynamic array
* @param size the size of the dynamic array
* @param ownership indicates whether the adaptor takes ownership of the array.
* Possible values are ``no_ownership()`` or ``acquire_ownership()``
* @param shape the shape of the xtensor_adaptor
* @param l the layout_type of the xtensor_adaptor
* @param alloc the allocator used for allocating / deallocating the dynamic array
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class O, class SC, class A = detail::default_allocator_for_ptr_t<P>,
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>)>
inline xtensor_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, detail::array_size<SC>::value, L>
adapt(P&& pointer, typename A::size_type size, O ownership, const SC& shape, layout_type l = L, const A& alloc = A())
{
static_assert(!xtl::is_integral<SC>::value, "shape cannot be a integer");
(void)ownership;
using buffer_type = xbuffer_adaptor<xtl::closure_type_t<P>, O, A>;
constexpr std::size_t N = detail::array_size<SC>::value;
using return_type = xtensor_adaptor<buffer_type, N, L>;
buffer_type buf(std::forward<P>(pointer), size, alloc);
return return_type(std::move(buf), shape, l);
}
/**
* Constructs an xtensor_adaptor of the given dynamically allocated C array,
* with the specified shape and strides.
* @param pointer the pointer to the beginning of the dynamic array
* @param size the size of the dynamic array
* @param ownership indicates whether the adaptor takes ownership of the array.
* Possible values are ``no_ownership()`` or ``acquire_ownership()``
* @param shape the shape of the xtensor_adaptor
* @param strides the strides of the xtensor_adaptor
* @param alloc the allocator used for allocating / deallocating the dynamic array
*/
template <class P, class O, class SC, class SS, class A = detail::default_allocator_for_ptr_t<P>,
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>,
detail::not_a_layout<std::decay_t<SS>>)>
inline xtensor_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, detail::array_size<SC>::value, layout_type::dynamic>
adapt(P&& pointer, typename A::size_type size, O ownership, SC&& shape, SS&& strides, const A& alloc = A())
{
static_assert(!xtl::is_integral<std::decay_t<SC>>::value, "shape cannot be a integer");
(void)ownership;
using buffer_type = xbuffer_adaptor<xtl::closure_type_t<P>, O, A>;
constexpr std::size_t N = detail::array_size<SC>::value;
using return_type = xtensor_adaptor<buffer_type, N, layout_type::dynamic>;
buffer_type buf(std::forward<P>(pointer), size, alloc);
return return_type(std::move(buf),
xtl::forward_sequence<typename return_type::inner_shape_type, SC>(shape),
xtl::forward_sequence<typename return_type::inner_strides_type, SS>(strides));
}
/**
* Contructs an xtensor_adaptor of the given C array allocated on the stack, with the
* specified shape and layout.
* @param c_array the C array allocated on the stack
* @param shape the shape of the xarray_adaptor
* @param l the layout_type of the xarray_adaptor
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class T, std::size_t N, class SC,
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>)>
inline auto adapt(T (&c_array)[N], const SC& shape, layout_type l = L)
{
return adapt(&c_array[0], N, xt::no_ownership(), shape, l);
}
/**
* Contructs an xtensor_adaptor of the given C array allocated on the stack, with the
* specified shape and stirdes.
* @param c_array the C array allocated on the stack
* @param shape the shape of the xarray_adaptor
* @param strides the strides of the xarray_adaptor
*/
template <class T, std::size_t N, class SC, class SS,
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>,
detail::not_a_layout<std::decay_t<SS>>)>
inline auto adapt(T (&c_array)[N], SC&& shape, SS&& strides)
{
return adapt(&c_array[0], N, xt::no_ownership(),
std::forward<SC>(shape),
std::forward<SS>(strides));
}
/**
* Constructs an non-owning xtensor_fixed_adaptor from a pointer with the
* specified shape and layout.
* @param pointer the pointer to adapt
* @param shape the shape of the xtensor_fixed_adaptor
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, std::size_t... X,
XTL_REQUIRES(std::is_pointer<C>)>
inline auto adapt(C&& pointer, const fixed_shape<X...>& /*shape*/)
{
using buffer_type = xbuffer_adaptor<C, xt::no_ownership, detail::default_allocator_for_ptr_t<C>>;
using return_type = xfixed_adaptor<buffer_type, fixed_shape<X...>, L>;
return return_type(buffer_type(pointer, detail::fixed_compute_size<fixed_shape<X...>>::value));
}
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, class T, std::size_t N>
inline auto adapt(C&& ptr, const T(&shape)[N])
{
using shape_type = std::array<std::size_t, N>;
return adapt(std::forward<C>(ptr), xtl::forward_sequence<shape_type, decltype(shape)>(shape));
}
/*****************************
* smart_ptr adapter builder *
*****************************/
/**
* Adapt a smart pointer to a typed memory block (unique_ptr or shared_ptr)
*
* \code{.cpp}
* #include <xtensor/xadapt.hpp>
* #include <xtensor/xio.hpp>
*
* std::shared_ptr<double> sptr(new double[8], std::default_delete<double[]>());
* sptr.get()[2] = 321.;
* std::vector<size_t> shape = {4, 2};
* auto xptr = adapt_smart_ptr(sptr, shape);
* xptr(1, 3) = 123.;
* std::cout << xptr;
* \endcode
*
* @param smart_ptr a smart pointer to a memory block of T[]
* @param shape The desired shape
* @param l The desired memory layout
*
* @return xarray_adaptor for memory
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class SC,
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>)>
auto adapt_smart_ptr(P&& smart_ptr, const SC& shape, layout_type l = L)
{
using buffer_adaptor = xbuffer_adaptor<decltype(smart_ptr.get()), smart_ownership,
std::decay_t<P>>;
return xarray_adaptor<buffer_adaptor, L, std::decay_t<SC>>(
buffer_adaptor(smart_ptr.get(), compute_size(shape), std::forward<P>(smart_ptr)),
shape,
l
);
}
/**
* Adapt a smart pointer (shared_ptr or unique_ptr)
*
* This function allows to automatically adapt a shared or unique pointer to
* a given shape and operate naturally on it. Memory will be automatically
* handled by the smart pointer implementation.
*
* \code{.cpp}
* #include <xtensor/xadapt.hpp>
* #include <xtensor/xio.hpp>
*
* struct Buffer {
* Buffer(std::vector<double>& buf) : m_buf(buf) {}
* ~Buffer() { std::cout << "deleted" << std::endl; }
* std::vector<double> m_buf;
* };
*
* auto data = std::vector<double>{1,2,3,4,5,6,7,8};
* auto shared_buf = std::make_shared<Buffer>(data);
* auto unique_buf = std::make_unique<Buffer>(data);
*
* std::cout << shared_buf.use_count() << std::endl;
* {
* std::vector<size_t> shape = {2, 4};
* auto obj = adapt_smart_ptr(shared_buf.get()->m_buf.data(),
* shape, shared_buf);
* // Use count increased to 2
* std::cout << shared_buf.use_count() << std::endl;
* std::cout << obj << std::endl;
* }
* // Use count reset to 1
* std::cout << shared_buf.use_count() << std::endl;
*
* {
* std::vector<size_t> shape = {2, 4};
* auto obj = adapt_smart_ptr(unique_buf.get()->m_buf.data(),
* shape, std::move(unique_buf));
* std::cout << obj << std::endl;
* }
* \endcode
*
* @param data_ptr A pointer to a typed data block (e.g. double*)
* @param shape The desired shape
* @param smart_ptr A smart pointer to move or copy, in order to manage memory
* @param l The desired memory layout
*
* @return xarray_adaptor on the memory
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class SC, class D,
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
detail::not_a_layout<std::decay_t<D>>)>
auto adapt_smart_ptr(P&& data_ptr, const SC& shape, D&& smart_ptr, layout_type l = L)
{
using buffer_adaptor = xbuffer_adaptor<P, smart_ownership,
std::decay_t<D>>;
return xarray_adaptor<buffer_adaptor, L, std::decay_t<SC>>(
buffer_adaptor(data_ptr, compute_size(shape), std::forward<D>(smart_ptr)),
shape,
l
);
}
/**
* Adapt a smart pointer to a typed memory block (unique_ptr or shared_ptr)
*
* \code{.cpp}
* #include <xtensor/xadapt.hpp>
* #include <xtensor/xio.hpp>
*
* std::shared_ptr<double> sptr(new double[8], std::default_delete<double[]>());
* sptr.get()[2] = 321.;
* auto xptr = adapt_smart_ptr(sptr, {4, 2});
* xptr(1, 3) = 123.;
* std::cout << xptr;
* \endcode
*
* @param smart_ptr a smart pointer to a memory block of T[]
* @param shape The desired shape
* @param l The desired memory layout
*
* @return xtensor_adaptor for memory
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class I, std::size_t N>
auto adapt_smart_ptr(P&& smart_ptr, const I(&shape)[N], layout_type l = L)
{
using buffer_adaptor = xbuffer_adaptor<decltype(smart_ptr.get()), smart_ownership,
std::decay_t<P>>;
std::array<std::size_t, N> fshape = xtl::forward_sequence<std::array<std::size_t, N>, decltype(shape)>(shape);
return xtensor_adaptor<buffer_adaptor, N, L>(
buffer_adaptor(smart_ptr.get(), compute_size(fshape), std::forward<P>(smart_ptr)),
std::move(fshape),
l
);
}
/**
* Adapt a smart pointer (shared_ptr or unique_ptr)
*
* This function allows to automatically adapt a shared or unique pointer to
* a given shape and operate naturally on it. Memory will be automatically
* handled by the smart pointer implementation.
*
* \code{.cpp}
* #include <xtensor/xadapt.hpp>
* #include <xtensor/xio.hpp>
*
* struct Buffer {
* Buffer(std::vector<double>& buf) : m_buf(buf) {}
* ~Buffer() { std::cout << "deleted" << std::endl; }
* std::vector<double> m_buf;
* };
*
* auto data = std::vector<double>{1,2,3,4,5,6,7,8};
* auto shared_buf = std::make_shared<Buffer>(data);
* auto unique_buf = std::make_unique<Buffer>(data);
*
* std::cout << shared_buf.use_count() << std::endl;
* {
* auto obj = adapt_smart_ptr(shared_buf.get()->m_buf.data(),
* {2, 4}, shared_buf);
* // Use count increased to 2
* std::cout << shared_buf.use_count() << std::endl;
* std::cout << obj << std::endl;
* }
* // Use count reset to 1
* std::cout << shared_buf.use_count() << std::endl;
*
* {
* auto obj = adapt_smart_ptr(unique_buf.get()->m_buf.data(),
* {2, 4}, std::move(unique_buf));
* std::cout << obj << std::endl;
* }
* \endcode
*
* @param data_ptr A pointer to a typed data block (e.g. double*)
* @param shape The desired shape
* @param smart_ptr A smart pointer to move or copy, in order to manage memory
* @param l The desired memory layout
*
* @return xtensor_adaptor on the memory
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class I, std::size_t N, class D,
XTL_REQUIRES(detail::not_a_layout<std::decay_t<D>>)>
auto adapt_smart_ptr(P&& data_ptr, const I(&shape)[N], D&& smart_ptr, layout_type l = L)
{
using buffer_adaptor = xbuffer_adaptor<P, smart_ownership,
std::decay_t<D>>;
std::array<std::size_t, N> fshape = xtl::forward_sequence<std::array<std::size_t, N>, decltype(shape)>(shape);
return xtensor_adaptor<buffer_adaptor, N, L>(
buffer_adaptor(data_ptr, compute_size(fshape), std::forward<D>(smart_ptr)),
std::move(fshape),
l
);
}
}
#endif