Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
BracketAdapter.h
Go to the documentation of this file.
1// Author: Stephan Hageboeck, CERN 25 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_BRACKETADAPTER_H
18#define ROOFIT_BATCHCOMPUTE_BRACKETADAPTER_H
19
20#include "RooSpan.h"
21
22#include <vector>
23
24namespace RooBatchCompute {
25
26///Little adapter that gives a bracket operator to types that don't
27///have one. It completely ignores the index and returns a constant.
28template <class T = double>
30 public:
31
32 constexpr BracketAdapter(T payload) noexcept :
33 _payload{payload} { }
34
35 constexpr BracketAdapter(RooSpan<const T> payload) noexcept :
36 _payload{payload[0]} { }
37
38 constexpr double operator[](std::size_t) const {
39 return _payload;
40 }
41
42 constexpr operator double() const {
43 return _payload;
44 }
45
46 constexpr bool isBatch() const noexcept {
47 return false;
48 }
49
50 private:
51 const T _payload;
52};
53
54
56 public:
57 /// Construct adapter from a fallback value and a batch of values.
58 /// - If `batch.size() == 0`, always return `payload`.
59 /// - Else, return `batch[i]`.
60 BracketAdapterWithMask(double payload, const RooSpan<const double>& batch) noexcept :
61 _isBatch(!batch.empty()),
62 _payload(payload),
63 _pointer(batch.empty() ? &_payload : batch.data()),
64 _mask(batch.size() > 1 ? ~static_cast<size_t>(0): 0)
65 {
66 }
67
68 /// Construct adapter from a batch of values.
69 /// - If `batch.size() == 1`, always return the value at `batch[0]`.
70 /// - Else, return `batch[i]`.
72 _isBatch(batch.size() > 1),
73 _payload(batch[0]),
74 _pointer(batch.data()),
75 _mask(batch.size() > 1 ? ~static_cast<size_t>(0): 0)
76 {
77 assert(batch.size() > 0);
78 }
79
81 _isBatch(other._isBatch),
82 _payload(other._payload),
83 _pointer(other._isBatch ? other._pointer : &_payload),
84 _mask(other._mask)
85 {
86 }
87
89
90 inline double operator[](std::size_t i) const noexcept {
91 return _pointer[ i & _mask];
92 }
93
94 inline bool isBatch() const noexcept {
95 return _isBatch;
96 }
97
98 private:
99 const bool _isBatch;
100 const double _payload;
101 const double* __restrict const _pointer;
102 const size_t _mask;
103};
104
105}
106
107#endif /* ROOFIT_BATCHCOMPUTE_BRACKETADAPTER_H */
double
BracketAdapterWithMask(RooSpan< const double > batch)
Construct adapter from a batch of values.
const double *__restrict const _pointer
BracketAdapterWithMask(double payload, const RooSpan< const double > &batch) noexcept
Construct adapter from a fallback value and a batch of values.
BracketAdapterWithMask & operator=(const BracketAdapterWithMask &other)=delete
BracketAdapterWithMask(const BracketAdapterWithMask &other) noexcept
double operator[](std::size_t i) const noexcept
Little adapter that gives a bracket operator to types that don't have one.
constexpr BracketAdapter(T payload) noexcept
constexpr bool isBatch() const noexcept
constexpr double operator[](std::size_t) const
constexpr BracketAdapter(RooSpan< const T > payload) noexcept
A simple container to hold a batch of data values.
Definition RooSpan.h:34
constexpr std::span< T >::index_type size() const noexcept
Definition RooSpan.h:121
Namespace for dispatching RooFit computations to various backends.