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 `RooSpan<NON_CONST_T>` to `RooSpan<T>`, where `T` is a `const` type.
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, std::size_t sizeIn) : _span{beginIn, sizeIn} {}
77
78 constexpr RooSpan(const std::vector<typename std::remove_cv<T>::type>& vec) noexcept :
79 _span{vec}
80 { }
81
82 constexpr RooSpan(std::vector<typename std::remove_cv<T>::type>& vec) noexcept :
83 _span{vec}
84 { }
85
86
87 /// We cannot point to temporary data.
88 constexpr RooSpan(std::vector<value_type>&& payload) = delete;
89
90
91 RooSpan& operator=(const RooSpan& other) = default;
92
93
94 constexpr typename std::span<T>::iterator begin() const {
95 return _span.begin();
96 }
97
98 constexpr typename std::span<T>::iterator end() const {
99 return _span.end();
100 }
101
102 constexpr typename std::span<T>::pointer data() const {
103 return _span.data();
104 }
105
106#ifdef NDEBUG
107 constexpr typename std::span<T>::reference operator[](std::size_t i) const noexcept
108 {
109 return _span[i];
110 }
111#else
112 typename std::span<T>::reference operator[](std::size_t i) const noexcept
113 {
114 assert(i < _span.size());
115 return _span[i];
116 }
117#endif
118
119 constexpr std::size_t size() const noexcept
120 {
121 return _span.size();
122 }
123
124 constexpr bool empty() const noexcept {
125 return _span.empty();
126 }
127
128 constexpr bool isBatch() const noexcept {
129 return true;
130 }
131
132
133 ///Test if the span overlaps with `other`.
134 template <class Span_t>
135 constexpr bool overlaps(const Span_t& other) const {
136 return insideSpan(other.begin()) || insideSpan(other.end()-1)
137 || other.insideSpan(begin()) || other.insideSpan(end()-1);
138 }
139
140 ///Test if the given pointer/iterator is inside the span.
141 template <typename ptr_t>
142 constexpr bool insideSpan(ptr_t ptr) const {
143 return begin() <= ptr && ptr < end();
144 }
145
146private:
147
148 std::span<T> _span;
149};
150
151
152#endif /* ROOFIT_BATCHCOMPUTE_ROOSPAN_H */
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
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:82
constexpr RooSpan()
Definition RooSpan.h:39
constexpr std::size_t size() const noexcept
Definition RooSpan.h:119
constexpr bool insideSpan(ptr_t ptr) const
Test if the given pointer/iterator is inside the span.
Definition RooSpan.h:142
constexpr std::span< T >::iterator end() const
Definition RooSpan.h:98
typename std::remove_cv< T >::type value_type
Definition RooSpan.h:37
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 RooSpan<NON_CONST_T> to RooSpan<T>, where T is a const type.
Definition RooSpan.h:56
constexpr std::span< T >::pointer data() const
Definition RooSpan.h:102
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 >::reference operator[](std::size_t i) const noexcept
Definition RooSpan.h:112
std::span< T > _span
Definition RooSpan.h:148
constexpr RooSpan(typename std::span< T >::pointer beginIn, std::size_t 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:135
constexpr bool isBatch() const noexcept
Definition RooSpan.h:128
constexpr RooSpan(const std::vector< typename std::remove_cv< T >::type > &vec) noexcept
Definition RooSpan.h:78
constexpr std::span< T >::iterator begin() const
Definition RooSpan.h:94
constexpr bool empty() const noexcept
Definition RooSpan.h:124
RooSpan & operator=(const RooSpan &other)=default