-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsizebounded.ipp
More file actions
149 lines (134 loc) · 3.89 KB
/
sizebounded.ipp
File metadata and controls
149 lines (134 loc) · 3.89 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "sizebounded/sizebounded.hpp"
#include <cstring>
template <typename T, std::size_t sz>
const T& sizebounded<T,sz>::get(std::size_t i) const
{
return _buffer[i];
}
template <typename T, std::size_t sz>
sizebounded<T,sz>::sizebounded()
{
_buffer = new T[sz];
#ifdef DEBUG
if (getenv("PRINT_DEBUG") != NULL) {
std::clog << "CTOR sizebounded" << std::endl; }
#endif
}
template <typename T, std::size_t sz>
sizebounded<T,sz>::~sizebounded()
{
#ifdef DEBUG
if (getenv("PRINT_DEBUG") != NULL) {
std::clog << "DTOR sizebounded" << std::endl; }
#endif
if (_buffer) { delete[] _buffer; }
}
template <typename T, std::size_t sz>
sizebounded<T,sz>::sizebounded(sizebounded<T,sz> const & sb2)
{
if (_buffer) { delete[] _buffer; }
_buffer = new T[sz];
memcopy(*this, sb2, sz);
#ifdef DEBUG
if (getenv("PRINT_DEBUG") != NULL) {
std::clog << "sizebounded(sizebounded const &)" << std::endl; }
#endif
}
template <typename T, std::size_t sz>
sizebounded<T,sz>& sizebounded<T,sz>::operator=(sizebounded<T,sz> const & sb2)
{
if (_buffer) { delete[] _buffer; }
_buffer = new T[sz];
memcopy(*this, sb2, sz);
#ifdef DEBUG
if (getenv("PRINT_DEBUG") != NULL) {
std::clog << "operator=(sizebounded const &)" << std::endl; }
#endif
return *this;
}
template <typename T, std::size_t sz>
const T& sizebounded<T,sz>::operator[](std::size_t i) const
{
if (i >= 0 && i < sz) {
return _buffer[i];
} else {
#if __cpp_exceptions
throw sizebounded_read_exception();
#else
return *_dummy;
#endif
}
}
template <typename T, std::size_t sz>
T& sizebounded<T,sz>::operator[](std::size_t i)
{
if (i >= 0 && i < sz) {
return *(_buffer+i);
} else {
#if __cpp_exceptions
throw sizebounded_write_exception();
#else
return *_dummy;
#endif
}
}
template <typename T, std::size_t sz>
std::string sizebounded<T,sz>::toString() const
{
// this might return quite some nonsense for other T
// we need some partial specialisation here
return std::string((const char*)_buffer, sz);
}
template <typename T, std::size_t sz>
std::vector<T> sizebounded<T,sz>::toVector() const
{
std::vector<T> v(sz); // allocate to size
//for (int i=0; i<sz; i++) {
// v[i] = _buffer[i]; }
map([&v](int i, const T t) { v[i] = t; });
return v;
}
template <typename T, std::size_t sz>
void sizebounded<T,sz>::map(std::function<void(const int, const T)> f) const
{
for (std::size_t i=0; i < sz; i++) {
f(i, _buffer[i]);
}
}
template <typename T, std::size_t sz>
void sizebounded<T,sz>::zip(std::function<void(const T, const T)> f, sizebounded<T,sz> const & other) const
{
for (std::size_t i=0; i < sz; i++) {
f(_buffer[i], other._buffer[i]);
}
}
template <typename T, std::size_t sz>
void sizebounded<T,sz>::transform(std::function<T(const int, const T)> f)
{
for (std::size_t i=0; i < sz; i++) {
_buffer[i] = f(i, _buffer[i]);
}
}
template <typename T1, typename T2, std::size_t sz1, std::size_t sz2>
bool memcopy(sizebounded<T2,sz2> &target, const sizebounded<T1,sz1> &source, std::size_t nsrc)
{
std::size_t b1 = sizeof(T1);
std::size_t b2 = sizeof(T2);
if (nsrc < 1) { return false; }
if (nsrc * b1 > sz2 * b2) { return false; }
std::size_t minbytes = std::min(nsrc*b1, sz1*b1);
if (minbytes % b2 != 0) { // adjust for target size
minbytes = std::min(minbytes, (minbytes / b2) * b2); }
auto res = memcpy((void*)(target.ptr()), (void*)(source.ptr()), minbytes);
#ifdef DEBUG
if (getenv("PRINT_DEBUG") != NULL) {
std::clog << "copied " << minbytes << " bytes" << std::endl;
std::clog << "b2=" << b2 << " minbytes / b2=" << (minbytes / b2) << " minbytes % b2=" << (minbytes % b2) << std::endl;
for (std::size_t i=0; i<minbytes; i++) {
std::clog << "s " << i << " " << int(*(((char*)source.ptr())+i)) << std::endl;
std::clog << "t " << i << " " << int(*(((char*)target.ptr())+i)) << std::endl;
}
}
#endif
return res != nullptr;
}