Logo ROOT  
Reference Guide
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_ROOFITCORE_INC_ROOSPAN_H_
18 #define ROOFIT_ROOFITCORE_INC_ROOSPAN_H_
19 
20 #include "ROOT/RSpan.hxx"
21 #include <vector>
22 
23 ////////////////////////////////////////////////////////////////////////////
24 /// A simple container to hold a batch of data values.
25 /// It can operate in two modes:
26 /// * Span: It holds only a pointer to the storage held by another object
27 /// like a std::span does.
28 /// * Temp data: It holds its own data, and exposes the span.
29 /// This mode is necessary to ship data that are not available in
30 /// a contiguous storage like e.g. data from a TTree. This means, however, that
31 /// data have to be copied, and only live as long as the span.
32 template<class T>
33 class RooSpan {
34 public:
35  using iterator = typename std::span<T>::iterator;
36  using value_type = typename std::remove_cv<T>::type;
37 
38  constexpr RooSpan() :
39  _auxStorage{},
40  _span{} { }
41 
42  constexpr RooSpan(RooSpan&& other) :
43  _auxStorage{std::move(other._auxStorage)},
44  _span{other._span.data(), other._span.size()}
45  { }
46 
47  constexpr RooSpan(const RooSpan& other) :
49  _span{other._span}
50  { }
51 
52 
53  /// Conversion constructor from <T> to <const T>
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  _auxStorage{},
58  _span{other.data(), other.size()}
59  { }
60 
61  /// Construct from a range. Data held by foreign object.
62  template < class InputIterator>
63  constexpr RooSpan(InputIterator beginIn, InputIterator endIn) :
64  _auxStorage{},
65  _span{beginIn, endIn}
66  { }
67 
68 
69  /// Construct from start and end pointers.
70  constexpr RooSpan(typename std::span<T>::pointer beginIn,
71  typename std::span<T>::pointer endIn) :
72  _auxStorage{},
73  _span{beginIn, endIn}
74  { }
75 
76 
77  /// Construct from start pointer and size.
78  constexpr RooSpan(typename std::span<T>::pointer beginIn,
79  typename std::span<T>::index_type sizeIn) :
80  _auxStorage{},
81  _span{beginIn, sizeIn}
82  { }
83 
84 
85  constexpr RooSpan(const std::vector<typename std::remove_cv<T>::type>& vec) noexcept :
86  _auxStorage{},
87  _span{vec}
88  { }
89 
90  constexpr RooSpan(std::vector<typename std::remove_cv<T>::type>& vec) noexcept :
91  _auxStorage{},
92  _span{vec}
93  { }
94 
95 
96  /// Hand data over to this span. This will mean that the data will get
97  /// deleted when it goes out of scope. Try to avoid this because
98  /// unnecessary copies will be made.
99  constexpr RooSpan(std::vector<value_type>&& payload) :
100  _auxStorage{new std::vector<value_type>(std::forward<std::vector<value_type>>(payload))},
101  _span{_auxStorage->begin(), _auxStorage->end()}
102  { }
103 
104 
105  RooSpan<T>& operator=(const RooSpan<T>& other) = default;
106 
107 
108  constexpr typename std::span<T>::iterator begin() const {
109  return _span.begin();
110  }
111 
112  constexpr typename std::span<T>::iterator end() const {
113  return _span.end();
114  }
115 
116  constexpr typename std::span<T>::pointer data() const {
117  return _span.data();
118  }
119 
120  typename std::span<T>::reference operator[](typename std::span<T>::index_type i) const noexcept {
121  assert(i < _span.size());
122  return _span[i];
123  }
124 
125  constexpr typename std::span<T>::index_type size() const noexcept {
126  return _span.size();
127  }
128 
129  constexpr bool empty() const noexcept {
130  return _span.empty();
131  }
132 
133  constexpr bool isBatch() const noexcept {
134  return true;
135  }
136 
137 
138  ///Test if the span overlaps with `other`.
139  template <class Span_t>
140  bool overlaps(const Span_t& other) const {
141  return insideSpan(other.begin()) || insideSpan(other.end()-1)
142  || other.insideSpan(begin()) || other.insideSpan(end()-1);
143  }
144 
145  ///Test if the given pointer/iterator is inside the span.
146  template <typename ptr_t>
147  bool insideSpan(ptr_t ptr) const {
148  return begin() <= ptr && ptr < end();
149  }
150 
151 private:
152 
153  /// If a class does not own a contiguous block of memory, which
154  /// could be used to create a span, the memory has to be kept alive
155  /// until all referring spans are destroyed.
156  std::shared_ptr<std::vector<value_type>> _auxStorage;
157  std::span<T> _span;
158 };
159 
160 
161 #endif /* ROOFIT_ROOFITCORE_INC_ROOSPAN_H_ */
RooSpan::value_type
typename std::remove_cv< T >::type value_type
Definition: RooSpan.h:48
RooSpan::overlaps
bool overlaps(const Span_t &other) const
Test if the span overlaps with other.
Definition: RooSpan.h:152
RooSpan::iterator
typename std::span< T >::iterator iterator
Definition: RooSpan.h:47
RooSpan::insideSpan
bool insideSpan(ptr_t ptr) const
Test if the given pointer/iterator is inside the span.
Definition: RooSpan.h:159
RooSpan::size
constexpr std::span< T >::index_type size() const noexcept
Definition: RooSpan.h:137
RooSpan::_span
std::span< T > _span
Definition: RooSpan.h:169
RooSpan::end
constexpr std::span< T >::iterator end() const
Definition: RooSpan.h:124
RooSpan::RooSpan
constexpr RooSpan()
Definition: RooSpan.h:50
RooSpan::empty
constexpr bool empty() const noexcept
Definition: RooSpan.h:141
RooSpan::data
constexpr std::span< T >::pointer data() const
Definition: RooSpan.h:128
RooSpan::_auxStorage
std::shared_ptr< std::vector< value_type > > _auxStorage
If a class does not own a contiguous block of memory, which could be used to create a span,...
Definition: RooSpan.h:168
RooSpan::operator[]
std::span< T >::reference operator[](typename std::span< T >::index_type i) const noexcept
Definition: RooSpan.h:132
RooSpan::begin
constexpr std::span< T >::iterator begin() const
Definition: RooSpan.h:120
RooSpan::operator=
RooSpan< T > & operator=(const RooSpan< T > &other)=default
TMVA::DNN::forward
void forward(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
apply the weights (and functions) in forward direction of the DNN
Definition: NeuralNet.icc:546
type
int type
Definition: TGX11.cxx:121
RSpan.hxx
RooSpan::RooSpan
constexpr RooSpan(typename std::span< T >::pointer beginIn, typename std::span< T >::index_type sizeIn)
Construct from start pointer and size.
Definition: RooSpan.h:90
RooSpan::isBatch
constexpr bool isBatch() const noexcept
Definition: RooSpan.h:145
RooSpan
A simple container to hold a batch of data values.
Definition: RooSpan.h:33