yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.2
ndarray.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of YODA -- Yet more Objects for Data Analysis
4// Copyright (C) 2008-2024 The YODA collaboration (see AUTHORS for details)
5//
6#ifndef YODA_ARRAY_H
7#define YODA_ARRAY_H
8
9#include <vector>
10#include <algorithm>
11#include <sstream>
12#include <array>
13
14namespace YODA {
15 namespace Utils {
16
17
19 template <typename T, int N>
20 class ndarray {
21 public:
22
23 using ArrayT = std::array<T,N>;
24 using const_iterator = typename ArrayT::const_iterator;
25
27
28
30 ndarray() {
31 clear();
32 }
33
35 ndarray(const std::vector<T>& vec) {
36 if (vec.size() != N) {
37 std::stringstream msg;
38 msg << "Value vector of wrong size supplied to a " << N << " dimensional array";
39 throw RangeError(msg.str());
40 }
41 for (size_t i = 0; i < N; ++i) _val[i] = vec[i];
42 }
43
44
46 ndarray(const std::array<T,N> arr) {
47 _val = arr;
48 }
49
50
51 // /// Conversion from C array
52 // ndarray(const T arr[N]) {
53 // try {
54 // std::copy(arr, arr+N, _val);
55 // } catch (...) {
56 // std::stringstream msg;
57 // msg << "Value vector of wrong size supplied to a " << N << " dimensional array";
58 // throw RangeError(msg.str());
59 // }
60 // }
61
63
64
66
67
69 void clear() {
70 for (size_t i = 0; i < N; ++i) _val[i] = T();
71 }
72
74 T& operator[](size_t i) {
75 return _val[i];
76 }
77
79 const T& operator[](size_t i) const {
80 return _val[i];
81 }
82
84 const_iterator begin() const { return _val.cbegin(); }
85
87 const_iterator end() const { return _val.cend(); }
88
90
91
92 private:
93
94 std::array<T,N> _val;
95
96 };
97
98
99 }
100}
101
102#endif
Anonymous namespace to limit visibility.