Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:13:36

0001 // -*- C++ -*-
0002 //
0003 // This file is part of YODA -- Yet more Objects for Data Analysis
0004 // Copyright (C) 2008-2024 The YODA collaboration (see AUTHORS for details)
0005 //
0006 #ifndef YODA_ARRAY_H
0007 #define YODA_ARRAY_H
0008 
0009 #include <vector>
0010 #include <algorithm>
0011 #include <sstream>
0012 #include <array>
0013 
0014 namespace YODA {
0015   namespace Utils {
0016 
0017 
0018     /// Fixed-size array with constructors from standard array types
0019     template <typename T, int N>
0020     class ndarray {
0021     public:
0022 
0023       using ArrayT = std::array<T,N>;
0024       using const_iterator = typename ArrayT::const_iterator;
0025 
0026       /// @name Constructors
0027       //@{
0028 
0029       /// Default constructor
0030       ndarray() {
0031         clear();
0032       }
0033 
0034       /// Conversion from std::vector
0035       ndarray(const std::vector<T>& vec) {
0036         if (vec.size() != N) {
0037           std::stringstream msg;
0038           msg << "Value vector of wrong size supplied to a " << N << " dimensional array";
0039           throw RangeError(msg.str());
0040         }
0041         for (size_t i = 0; i < N; ++i) _val[i] = vec[i];
0042       }
0043 
0044 
0045       /// Conversion from C++11 array
0046       ndarray(const std::array<T,N> arr) {
0047         _val = arr;
0048       }
0049 
0050 
0051       // /// Conversion from C array
0052       // ndarray(const T arr[N]) {
0053       //   try {
0054       //     std::copy(arr, arr+N, _val);
0055       //   } catch (...) {
0056       //     std::stringstream msg;
0057       //     msg << "Value vector of wrong size supplied to a " << N << " dimensional array";
0058       //     throw RangeError(msg.str());
0059       //   }
0060       // }
0061 
0062       //@}
0063 
0064 
0065       /// @name Accessors and modifiers
0066       //@{
0067 
0068       /// Clear the point values and errors
0069       void clear() {
0070         for (size_t i = 0; i < N; ++i) _val[i] = T();
0071       }
0072 
0073       /// Array-like accessor
0074       T& operator[](size_t i) {
0075         return _val[i];
0076       }
0077 
0078       /// Array-like accessor (const version)
0079       const T& operator[](size_t i) const {
0080         return _val[i];
0081       }
0082 
0083       /// @brief Returns the const begin iterator for the array
0084       const_iterator begin() const { return _val.cbegin(); }
0085 
0086       /// @brief Returns the const end iterator for the array
0087       const_iterator end() const { return _val.cend(); }
0088 
0089       //@}
0090 
0091 
0092     private:
0093 
0094       std::array<T,N> _val;
0095 
0096     };
0097 
0098 
0099   }
0100 }
0101 
0102 #endif