forked from shaxquan/CppClassUtil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.h
146 lines (117 loc) · 1.81 KB
/
Array.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
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
//Basic 1D array class
#ifndef ARRAY_H
#define ARRAY_H
#include <stdio.h>
template <typename Datatype>
class Array
{
public:
Array(int p_size)
{
m_array = new Datatype[p_size];
m_size = p_size;
}
~Array()
{
if (m_array != 0)
{
delete [] m_array;
}
m_array = 0;
}
void Resize(int p_size)
{
Datatype *newArray = new Datatype[p_size];
if (newArray == 0)
{
return;
}
int min;
if (p_size < m_size)
{
min = p_size;
}
else
{
min = m_size;
}
int index;
for (index = 0; index < min; index++)
{
m_array[i] = newArray[i];
}
m_size = p_size;
if (m_array != 0)
{
delete [] m_array;
}
m_array = newArray;
}
Datatype &operator[](int p_index)
{
return m_array[p_index];
}
void Insert(Datatype p_item, int p_index)
{
int index;
for (index = m_size - 1; index > p_index; index--)
{
m_array[index] = m_array[index - 1];
}
m_array[p_index] = p_item;
}
void Remove(int p_index)
{
int index;
for (index = p_index + 1; index < m_size; index++)
{
m_array[index - 1] = m_array[index];
}
}
int Size()
{
return m_size;
}
operator Datatype *()
{
return m_array;
}
bool WriteFile(const char *p_filename)
{
FILE *outfile = 0;
int written = 0;
outfile = fopen(p_filename, "wb");
if (outfile == 0)
{
return false;
}
written = fwrite(m_array, sizeof(Datatype), m_size, outfile);
fclose(outfile);
if (written != m_size)
{
return false;
}
return true;
}
bool ReadFile(const char *p_filename)
{
FILE *infile = 0;
int read = 0;
infile = fopen(p_filename, "rb");
if (infile == 0)
{
return false;
}
read = fread(m_array, sizeof(Datatype), m_size, infile);
fclose(infile);
if (read != m_size)
{
return false;
}
return true;
}
private:
Datatype *m_array;
int m_size;
};
#endif