-
Notifications
You must be signed in to change notification settings - Fork 0
/
CeTu.h
235 lines (183 loc) · 4.65 KB
/
CeTu.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef CETU_H
#define CETU_H
#include <stdio.h> // might be substituted with ___asm calls for linux kernel calls, nobody asked for this thx God!
#include "string.h"
#include "stdlib.h"
// v-- move this down --v
//#include "cetuhashmap.h"
#define std CeTu
#define endl "\r\n"
#define std CeTu
#define nullptr NULL;
namespace CeTu
{
// === the string ===
class string
{
private:
static const int MAX_CHARS = 16;
public:
char chars[MAX_CHARS];
string (const char* buf)
{
char* s = ((char*)buf);
char* d = &(chars[0]);
do {
*d = *s;
d++;
s++;
} while (*s != '\0');
*d = '\0';
}
bool operator==(const string& other) {
char* a = &( chars[0]);
char* b = (char*)(&(other.chars[0]));
while (true) {
if (*a != *b) { return false; }
if (*a == '\0' && *b != '\0') { return false; }
if (*a != '\0' && *b == '\0') { return false; }
if (*a == '\0' && *b == '\0') { break; }
a++;
b++;
}
return true;
}
bool operator!=(const string& other) {
return !( string(chars) == other );
}
};
// === standard output ===
class coutt
{
public:
// coutt& operator << (int val)
// {
// printf("%d", val);
// return (*this);
// }
coutt& operator << (double val)
{
printf("%f", val);
return (*this);
}
coutt& operator << (const char* val)
{
printf("%s", val);
return (*this);
}
coutt& operator << (string val)
{
printf("%s", &(val.chars[0]));
return (*this);
}
};
// === vector container ===
// * memory alignment issues?
template<typename T>
class vector
{
public:
vector() { }
~vector() {
if (buffer) { free(buffer); }
}
// alloc new buffer (size+1), copy old contents and the val, free old buffer
void push_back(T val) {
void* newBuffer = malloc( (sizeof(T)*(m_size+1)) );
if (m_size) {
memcpy(newBuffer, buffer, (sizeof(T)*(m_size)));
free(buffer);
}
buffer = newBuffer;
memcpy(newBuffer + (sizeof(T)*(m_size)),
(T*)(&val),
(sizeof(T)));
m_size++;
}
void pop_back() {
void* newBuffer = malloc( (sizeof(T)*(m_size-1)) );
memcpy(newBuffer, buffer, (sizeof(T)*(m_size-1)));
free(buffer);
buffer = newBuffer;
m_size--;
}
T operator[] (int idx)
{
T* obj = (T*)((buffer) + idx * (sizeof(T)));
return (*obj);
}
int size() { return m_size; }
private:
int m_size = 0;
void* buffer = 0;
};
// TODO: being developed in main.cpp currently
// // === optional ===
//
// template<typename V>
// class optional
// {
// V val;
//
// public:
//
// optional(V _val)
// {
// val = _val;
// }
//
// };
//
//
// class nullopt
// {
// // overload dereferencing? (ret nullptr)
// };
}
using namespace std;
static coutt cout;
#endif // CETU_H
// === TESTS ===
//======================================
// vector test:
// int* a = new int(123);
// vector<int*> v;
// v.push_back(a);
// cout << ( (*v[0]) ) << endl;
//
// vector<int> v;
//
// v.push_back(1);
// v.push_back(2);
// v.push_back(3);
//
// cout << ( v[0] ) << endl;
// cout << ( v[1] ) << endl;
// cout << ( v[2] ) << endl;
//
// cout << ( v.size() ) << endl;
// v.pop_back(); cout << ( v.size() ) << endl;
// v.pop_back(); cout << ( v.size() ) << endl;
// v.pop_back(); cout << ( v.size() ) << endl;
//
// cout << ( v[0] ) << endl;
// cout << ( v[1] ) << endl;
// cout << ( v[2] ) << endl;
// cout << "TEST" << endl;
//======================================
//======================================
// vector<string> vs;
//
// vs.push_back("hello");
// vs.push_back("world");
//
// for (int i = 0; i < vs.size(); i++) {
// cout << vs[i] << endl;
// }
//======================================
//======================================
// string a("abc");
// string b("ab!!");
//
// cout << (int)(a != b) << endl;
//======================================