-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeArray.h
More file actions
40 lines (37 loc) · 1.08 KB
/
SafeArray.h
File metadata and controls
40 lines (37 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef SAFE_ARRAY_H
#define SAFE_ARRAY_H
#include <cstddef>
#include <cstring> // for memset
#include <stdexcept> // for std::out_of_range
template <typename T, unsigned int N> class SafeArray {
protected:
T a[N]; // Array with N elements of type T
public:
SafeArray() { // Constructor
memset(a, 0, sizeof(a)); // Initialize to zero
}
int Size() const { // Return the size of the array
return N;
}
T &operator[](unsigned int i) { // Safe [] array index operator
#ifdef DEBUG
if (i >= N) {
throw std::out_of_range("Index out of range");
}
#endif
return a[i]; // Return reference to a[i]
}
const T &operator[](unsigned int i) const { // Const version of operator[]
#ifdef DEBUG
if (i >= N) {
throw std::out_of_range("Index out of range");
}
#endif
return a[i]; // Return reference to a[i]
}
};
template <typename T, unsigned int N>
void ArrayMulTemplate(const SafeArray<T, N> &matrixA,
const SafeArray<T, N> &matrixB, SafeArray<T, N> &result,
size_t dimension);
#endif // SAFE_ARRAY_H