-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.hpp
78 lines (74 loc) · 2.15 KB
/
Array.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
#ifndef ARRAY_HPP_
#define ARRAY_HPP_
#include <iostream>
#include "stdlib.h"
#define cArray(T) \
class Array_##T { \
public: \
bool reserve() { \
this->_ptr = new T[this->_len]; \
return this->_ptr != NULL; \
} \
bool realloc_memory(int len) { \
if (this->_ptr == NULL) { \
return false; \
} else { \
if (this->_len == len) { \
return true; \
} else { \
this->_ptr = (T*)realloc(this->_ptr, len * sizeof(T)); \
if (this->_ptr == NULL) { \
return false; \
} else { \
this->_len = len; \
return true; \
} \
} \
} \
} \
void del() { \
delete this->_ptr; \
} \
Array_##T(unsigned int len, T error_return) { \
this->_len = len; \
this->error_return = error_return; \
this->reserve(); \
} \
~Array_##T() { \
this->del(); \
} \
T get(unsigned int pos) const { \
if (this->_ptr != NULL) { \
if (pos >= this->_len) { \
std::cout << "get out of bound -> len:" << this->_len << ", pos:" << pos << std::endl; \
return this->error_return; \
} else { \
return this->_ptr[pos]; \
} \
} else { \
printf("your obj is NULL.\n"); \
return this->error_return; \
} \
} \
void set(unsigned int pos, T value) { \
if (this->_ptr != NULL) { \
if (pos >= this->_len) { \
std::cout << "set out of bound -> len:" << this->_len << ", pos:" << pos << std::endl; \
} else { \
this->_ptr[pos] = value; \
} \
} else { \
std::cout << "your obj is NULL." << std::endl; \
} \
} \
int length() { \
return this->_len; \
} \
private: \
T* _ptr; \
T error_return; \
unsigned int _len; \
};
#define Array_Type(T) Array_##T
#define Array(T, l, r) Array_##T(l, r)
#endif