-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcommon.h
More file actions
executable file
·267 lines (220 loc) · 4.78 KB
/
Copy pathcommon.h
File metadata and controls
executable file
·267 lines (220 loc) · 4.78 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// *******************************
// FILE: common.h
// AUTHOR: SharpCoder
// DATE: 2012-03-18
// ABOUT: This is the include file for all pages. It defines
// data types and memory locations that should be
// accessible all around.
//
// LICENSE: Provided "AS IS". USE AT YOUR OWN RISK.
// *******************************
#ifndef __COMMON_H_
#define __COMMON_H_
#include "./libs/mem.h"
#define NULL 0
typedef unsigned long ulong;
typedef unsigned int uint32;
typedef volatile unsigned long int uint32_t;
typedef unsigned short uint16;
typedef unsigned char byte;
// Stack data structure.
template<class T>
class iterator {
public:
T val;
iterator* prev;
iterator(iterator* previous, T value ) {
this->val = value;
this->prev = previous;
}
volatile iterator* next() {
if ( this->prev == NULL ) return NULL;
return this->prev;
}
T getVal() {
return this->val;
}
};
template<class T>
class Stack {
private:
iterator<T>* top;
int length;
public:
void push(T* val) {
iterator<T>* next = new iterator<T>( this->top, val );
this->top = next;
this->length++;
}
T* pop( void ) {
// NULL Checking
if ( this->top == NULL ) return NULL;
// Do the pop
T* result = this->top->getVal();
this->top = this->top->next();
this->length--;
// Return the result
return result;
}
int getLength() {
return this->length;
}
iterator<T>* getIterator() {
return this->top;
}
};
template<class T>
class List {
public:
iterator<T>* first;
uint32 length;
List() {
this->first = NULL;
this->length = 0;
}
void add(T val) {
// Allocate it.
void* ptrN = malloc(sizeof(iterator<T>));
iterator<T>* newItem = new (ptrN) iterator<T> (NULL, val);
iterator<T>* last = this->first;
if ( last == NULL ) {
this->first = newItem;
} else if ( last->prev == NULL ) {
this->first->prev = newItem;
} else {
do {
last = last->prev;
} while ( last->prev != NULL );
last->prev = newItem;
}
this->length++;
}
T getAt(int index) {
if ( this->first == NULL ) return (T)NULL;
if ( index == 0 ) {
return this->first->val;
} else {
iterator<T>* last = this->first;
do {
last = last->prev;
index--;
} while ( last->prev != NULL && index > 0 );
return last->val;
}
}
T pop() {
volatile iterator<T>* last = this->first;
do {
last = last->prev;
} while ( last->prev != NULL );
volatile iterator<T>* result = last->prev;
last->prev = NULL;
return result->val;
}
int getLength() {
return this->length;
}
iterator<T>* getIterator() {
return this->first;
}
};
// Linked list structure/class
class LinkedList {
public:
LinkedList* next;
LinkedList* previous;
uint32 value;
// Public
LinkedList() {
}
LinkedList* GetNext() {
return this->next;
}
LinkedList* GetPrev() {
return this->previous;
}
void Add( LinkedList* item ) {
LinkedList* end;
// Traverse the linked list.
while( end->next ) { end = end->next; }
// Add to the end.
end->next = item;
}
void Remove() {
if ( this->next && this->previous ) {
// Link em.
this->previous->next = this->next;
this->next->previous = this->previous;
// If there is a previous link...
} else if ( this->previous ) {
// We don't have any forward link.
this->previous->next = 0;
} else if ( this->next ) {
// well...
this->value = this->next->value;
this->next = this->next->next;
this->previous = 0;
}
}
};
int abs( int x ) {
if ( x >= 0 ) return x;
return x * -1;
}
int getNumberLength( int number, int base, int* count ) {
int size = 1;
*count = 0;
while(true) {
if ( number > (size*base) ) {
size *= base;
*(count)++;
} else {
break;
}
}
return size;
}
int getNumberLength( int number, int base ) {
int count = 0;
return getNumberLength( number, base, &count );
}
int getNumberLength( int number ) {
return getNumberLength( number, 10 );
}
void divide ( int top, int bottom, int* result, int* remainder ) {
// Cannot divide by zero.
if ( bottom == 0 ) return;
if ( bottom == 1 ) {
*result = top;
*remainder = 0;
return;
}
// Get the absolute values of everything.
top = abs(top);
bottom = abs(bottom);
// Reset the pointer values.
*result = 0;
*remainder = 0;
// Iterate until we hit zero.
int i;
for ( i = 0; i < top; i++ ) {
if ( (top - bottom) >= 0 ) {
*result = *result + 1;
} else {
*remainder = top;
break;
}
top = top - bottom;
}
}
int divide( int top, int bottom ) {
int result = 0, remainder = 0;
divide( top, bottom, &result, &remainder );
return result;
}
int modulo( int top, int bottom ) {
int result = 0, remainder = 0;
divide( top, bottom, &result, &remainder );
return remainder;
}
#endif