-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyvector.hpp
86 lines (70 loc) · 1.6 KB
/
myvector.hpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once
#include <cstring>
// Simple replacement for std::vector with better NUMA awareness
namespace my
{
template<typename T>
class vector
{
private:
// Internal storage
T *_data = nullptr;
public:
// dimension
int n;
// Default constructor
vector() = default;
// Allocate at the time of construction
vector(int n) : n(n) {
_data = new T [n];
};
void resize(int n_in) {
n = n_in;
delete[] _data;
_data = new T [n];
};
// standard [i] syntax for setting elements
T& operator[](int i) {
return _data[i];
}
// standard [i] syntax for getting elements
const T& operator[](int i) const {
return _data[i];
}
// Rule of five when we manage memory ourselves
// Copy constructor
vector(const vector& other) {
n = other.n;
_data = new T [n];
std::memcpy(_data, other._data, n*sizeof(T));
}
// Copy assignment
vector& operator= (const vector& other) {
auto tmp = other;
std::swap(n, tmp.n);
std::swap(_data, tmp._data);
return *this;
}
// Move constructor
vector(vector&& other) {
n = other.n;
_data = other._data;
other._data = nullptr;
}
// Move assignment
vector& operator= (vector&& other) {
n = other.n;
_data = other._data;
other._data = nullptr;
return *this;
}
// Destructor
~vector() {
delete[] _data;
}
// provide possibility to get raw pointer for data at index (i) (needed for MPI)
T* data(int i=0) {
return _data + i;
}
};
};