Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooSpan.h
Go to the documentation of this file.
1// Author: Stephan Hageboeck, CERN 7 Feb 2019
2
3/*****************************************************************************
4 * RooFit
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2019, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17#ifndef ROOFIT_BATCHCOMPUTE_ROOSPAN_H
18#define ROOFIT_BATCHCOMPUTE_ROOSPAN_H
19
20#include "ROOT/RSpan.hxx"
21#include <vector>
22#include <cassert>
23
24////////////////////////////////////////////////////////////////////////////
25/// A simple container to hold a batch of data values.
26/// It can operate in two modes:
27/// * Span: It holds only a pointer to the storage held by another object
28/// like a std::span does.
29/// * Temp data: It holds its own data, and exposes the span.
30/// This mode is necessary to ship data that are not available in
31/// a contiguous storage like e.g. data from a TTree. This means, however, that
32/// data have to be copied, and only live as long as the span.
33template<class T>
34class RooSpan {
35public:
36 using iterator = typename std::span<T>::iterator;
37 using value_type = typename std::remove_cv<T>::type;
38
39 constexpr RooSpan() :
40 _span{} { }
41
42 constexpr RooSpan(RooSpan&& other) :
43 _span{other._span.data(), other._span.size()}
44 { }
45
46 constexpr RooSpan(const RooSpan& other) :
47 _span{other._span}
48 { }
49
50
51 /// Conversion constructor from <T> to <const T>
52 /// If the input span owns some memory, the const-version of the
53 /// span will copy the shared_ptr.
54 template<typename NON_CONST_T,
55 typename = typename std::enable_if<std::is_same<const NON_CONST_T, T>::value>::type >
56 constexpr RooSpan(const RooSpan<NON_CONST_T>& other) :
57 _span{other.data(), other.size()}
58 { }
59
60
61 /// Construct from a range. Data held by foreign object.
62 template < class InputIterator>
63 constexpr RooSpan(InputIterator beginIn, InputIterator endIn) :
64 _span{beginIn, endIn}
65 { }
66
67
68 /// Construct from start and end pointers.
69 constexpr RooSpan(typename std::span<T>::pointer beginIn,
70 typename std::span<T>::pointer endIn) :
71 _span{beginIn, endIn}
72 { }
73
74
75 /// Construct from start pointer and size.
76 constexpr RooSpan(typename std::span<T>::pointer beginIn,
77 typename std::span<T>::index_type sizeIn) :
78 _span{beginIn, sizeIn}
79 { }
80
81
82 constexpr RooSpan(const std::vector<typename std::remove_cv<T>::type>& vec) noexcept :
83 _span{vec}
84 { }
85
86 constexpr RooSpan(std::vector<typename std::remove_cv<T>::type>& vec) noexcept :
87 _span{vec}
88 { }
89
90
91 /// We cannot point to temporary data.
92 constexpr RooSpan(std::vector<value_type>&& payload) = delete;
93
94
95 RooSpan<T>& operator=(const RooSpan<T>& other) = default;
96
97
98 constexpr typename std::span<T>::iterator begin() const {
99 return _span.begin();
100 }
101
102 constexpr typename std::span<T>::iterator end() const {
103 return _span.end();
104 }
105
106 constexpr typename std::span<T>::pointer data() const {
107 return _span.data();
108 }
109
110#ifdef NDEBUG
111 constexpr typename std::span<T>::reference operator[](typename std::span<T>::index_type i) const noexcept {
112 return _span[i];
113 }
114#else
115 typename std::span<T>::reference operator[](typename std::span<T>::index_type i) const noexcept {
116 assert(i < _span.size());
117 return _span[i];
118 }
119#endif
120
121 constexpr typename std::span<T>::index_type size() const noexcept {
122 return _span.size();
123 }
124
125 constexpr bool empty() const noexcept {
126 return _span.empty();
127 }
128
129 constexpr bool isBatch() const noexcept {
130 return true;
131 }
132
133
134 ///Test if the span overlaps with `other`.
135 template <class Span_t>
136 constexpr bool overlaps(const Span_t& other) const {
137 return insideSpan(other.begin()) || insideSpan(other.end()-1)
138 || other.insideSpan(begin()) || other.insideSpan(end()-1);
139 }
140
141 ///Test if the given pointer/iterator is inside the span.
142 template <typename ptr_t>
143 constexpr bool insideSpan(ptr_t ptr) const {
144 return begin() <= ptr && ptr < end();
145 }
146
147private:
148
149 std::span<T> _span;
150};
151
152
153#endif /* ROOFIT_BATCHCOMPUTE_ROOSPAN_H */
int type
Definition TGX11.cxx:121
A simple container to hold a batch of data values.
Definition RooSpan.h:34
constexpr RooSpan(std::vector< typename std::remove_cv< T >::type > &vec) noexcept
Definition RooSpan.h:86
constexpr RooSpan()
Definition RooSpan.h:39
constexpr bool insideSpan(ptr_t ptr) const
Test if the given pointer/iterator is inside the span.
Definition RooSpan.h:143
constexpr std::span< T >::iterator end() const
Definition RooSpan.h:102
RooSpan< T > & operator=(const RooSpan< T > &other)=default
typename std::remove_cv< T >::type value_type
Definition RooSpan.h:37
std::span< T >::reference operator[](typename std::span< T >::index_type i) const noexcept
Definition RooSpan.h:115
constexpr RooSpan(InputIterator beginIn, InputIterator endIn)
Construct from a range. Data held by foreign object.
Definition RooSpan.h:63
constexpr RooSpan(const RooSpan< NON_CONST_T > &other)
Conversion constructor from <T> to <const T> If the input span owns some memory, the const-version of...
Definition RooSpan.h:56
constexpr std::span< T >::pointer data() const
Definition RooSpan.h:106
constexpr std::span< T >::index_type size() const noexcept
Definition RooSpan.h:121
constexpr RooSpan(const RooSpan &other)
Definition RooSpan.h:46
constexpr RooSpan(typename std::span< T >::pointer beginIn, typename std::span< T >::pointer endIn)
Construct from start and end pointers.
Definition RooSpan.h:69
constexpr RooSpan(RooSpan &&other)
Definition RooSpan.h:42
typename std::span< T >::iterator iterator
Definition RooSpan.h:36
constexpr RooSpan(std::vector< value_type > &&payload)=delete
We cannot point to temporary data.
std::span< T > _span
Definition RooSpan.h:149
constexpr RooSpan(typename std::span< T >::pointer beginIn, typename std::span< T >::index_type sizeIn)
Construct from start pointer and size.
Definition RooSpan.h:76
constexpr bool overlaps(const Span_t &other) const
Test if the span overlaps with other.
Definition RooSpan.h:136
constexpr bool isBatch() const noexcept
Definition RooSpan.h:129
constexpr RooSpan(const std::vector< typename std::remove_cv< T >::type > &vec) noexcept
Definition RooSpan.h:82
constexpr std::span< T >::iterator begin() const
Definition RooSpan.h:98
constexpr bool empty() const noexcept
Definition RooSpan.h:125