-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk.h++
99 lines (77 loc) · 1.9 KB
/
chunk.h++
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
#ifndef CHUNK_H
#define CHUNK_H
#include "shared.h++"
#include <exception>
#include <stddef.h>
class chunk_exception: public std::exception{
public:
explicit chunk_exception(const char* message,const int i);
virtual ~chunk_exception() noexcept;
virtual const char* what() const noexcept;
const char* msg;
const int _idx;
};
template<typename T> class line;
template<typename T> class plane;
template<typename T>
class chunk{
public:
size_t xsize,ysize,zsize;
private:
T *data;
public:
chunk(size_t xsize,size_t ysize,size_t zsize);
plane<T> &operator[](size_t x);
};
template<typename T>
class line{
public:
size_t zsize;
private:
T *data;
line(T *data,size_t zsize);
public:
T &operator[](size_t z);
friend line<T> &plane<T>::operator[](size_t);
};
template<typename T>
class plane{
public:
size_t ysize,zsize;
private:
T *data;
plane(T *data,size_t ysize,size_t zsize);
public:
line<T> &operator[](size_t y);
friend plane<T> &chunk<T>::operator[](size_t);
};
template<typename T>
line<T>::line(T *data,size_t zsize):zsize(zsize),data(data){}
template<typename T>
plane<T>::plane(T *data,size_t ysize,size_t zsize):ysize(ysize),zsize(zsize),data(data){}
template<typename T>
chunk<T>::chunk(size_t xsize,size_t ysize,size_t zsize):xsize(xsize),ysize(ysize),zsize(zsize),data((T*)malloc(xsize*ysize*zsize*sizeof(T))){}
template<typename T>
T &line<T>::operator[](size_t z){
if(z>=zsize){
throw new chunk_exception("z index out of range",z);
}
return data[z];
}
template<typename T>
line<T> &plane<T>::operator[](size_t y){
if(y>=ysize){
throw new chunk_exception("y index out of range",y);
}
line<T> *l=new line(data+(y*zsize),zsize);
return *l;
}
template<typename T>
plane<T> &chunk<T>::operator[](size_t x){
if(x>=xsize){
throw new chunk_exception("x index out of range",x);
}
plane<T> *p=new plane(data+(x*ysize*zsize),ysize,zsize);
return *p;
}
#endif