]> git.gir.st - base65536.git/blob - generate_struct.c
fix -Werror=misleading-indentation
[base65536.git] / generate_struct.c
1 /*
2 this is a helper program to generate c code for a complete binary
3 search tree, containg the codepoints used for base65536, as defined by
4 github user ferno.
5 why a binary search tree? because i am studying compsci - that's why! :P
6 the struct bnode contains the data in the same format as will be used
7 in the 'real' program, but the btree array is not yet in tree form.
8
9 execute `gcc -std=c99 generate_struct.c -lm`
10
11 (C) 2016 Tobias Girstmair, http://isticktoit.net/
12 Released under the GNU GPL v3. See LICENSE for details.
13 */
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <math.h>
17
18 struct bnode {
19 int block; /* index for the blocks, as defined by ferno */
20 int start; /* start of each block, as defined by ferno */
21 };
22
23 /* it is easier to create a full (perfect) tree, and add those extra bits
24 afterwards to the leftmost node in the bottom row. */
25 struct bnode extra [] = {
26 {-1, 5376},
27 {1, 13568}
28 };
29 struct bnode btree [] = {
30 /* {-1, 5376},*/
31 {0, 13312},
32 /* {1, 13568}, */
33 {2, 13824},
34 {3, 14080},
35 {4, 14336},
36 {5, 14592},
37 {6, 14848},
38 {7, 15104},
39 {8, 15360},
40 {9, 15616},
41 {10, 15872},
42 {11, 16128},
43 {12, 16384},
44 {13, 16640},
45 {14, 16896},
46 {15, 17152},
47 {16, 17408},
48 {17, 17664},
49 {18, 17920},
50 {19, 18176},
51 {20, 18432},
52 {21, 18688},
53 {22, 18944},
54 {23, 19200},
55 {24, 19456},
56 {25, 19968},
57 {26, 20224},
58 {27, 20480},
59 {28, 20736},
60 {29, 20992},
61 {30, 21248},
62 {31, 21504},
63 {32, 21760},
64 {33, 22016},
65 {34, 22272},
66 {35, 22528},
67 {36, 22784},
68 {37, 23040},
69 {38, 23296},
70 {39, 23552},
71 {40, 23808},
72 {41, 24064},
73 {42, 24320},
74 {43, 24576},
75 {44, 24832},
76 {45, 25088},
77 {46, 25344},
78 {47, 25600},
79 {48, 25856},
80 {49, 26112},
81 {50, 26368},
82 {51, 26624},
83 {52, 26880},
84 {53, 27136},
85 {54, 27392},
86 {55, 27648},
87 {56, 27904},
88 {57, 28160},
89 {58, 28416},
90 {59, 28672},
91 {60, 28928},
92 {61, 29184},
93 {62, 29440},
94 {63, 29696},
95 {64, 29952},
96 {65, 30208},
97 {66, 30464},
98 {67, 30720},
99 {68, 30976},
100 {69, 31232},
101 {70, 31488},
102 {71, 31744},
103 {72, 32000},
104 {73, 32256},
105 {74, 32512},
106 {75, 32768},
107 {76, 33024},
108 {77, 33280},
109 {78, 33536},
110 {79, 33792},
111 {80, 34048},
112 {81, 34304},
113 {82, 34560},
114 {83, 34816},
115 {84, 35072},
116 {85, 35328},
117 {86, 35584},
118 {87, 35840},
119 {88, 36096},
120 {89, 36352},
121 {90, 36608},
122 {91, 36864},
123 {92, 37120},
124 {93, 37376},
125 {94, 37632},
126 {95, 37888},
127 {96, 38144},
128 {97, 38400},
129 {98, 38656},
130 {99, 38912},
131 {100, 39168},
132 {101, 39424},
133 {102, 39680},
134 {103, 39936},
135 {104, 40192},
136 {105, 40448},
137 {106, 41216},
138 {107, 41472},
139 {108, 41728},
140 {109, 42240},
141 {110, 67072},
142 {111, 73728},
143 {112, 73984},
144 {113, 74240},
145 {114, 77824},
146 {115, 78080},
147 {116, 78336},
148 {117, 78592},
149 {118, 82944},
150 {119, 83200},
151 {120, 92160},
152 {121, 92416},
153 {122, 131072},
154 {123, 131328},
155 {124, 131584},
156 {125, 131840},
157 {126, 132096},
158 {127, 132352},
159 {128, 132608},
160 {129, 132864},
161 {130, 133120},
162 {131, 133376},
163 {132, 133632},
164 {133, 133888},
165 {134, 134144},
166 {135, 134400},
167 {136, 134656},
168 {137, 134912},
169 {138, 135168},
170 {139, 135424},
171 {140, 135680},
172 {141, 135936},
173 {142, 136192},
174 {143, 136448},
175 {144, 136704},
176 {145, 136960},
177 {146, 137216},
178 {147, 137472},
179 {148, 137728},
180 {149, 137984},
181 {150, 138240},
182 {151, 138496},
183 {152, 138752},
184 {153, 139008},
185 {154, 139264},
186 {155, 139520},
187 {156, 139776},
188 {157, 140032},
189 {158, 140288},
190 {159, 140544},
191 {160, 140800},
192 {161, 141056},
193 {162, 141312},
194 {163, 141568},
195 {164, 141824},
196 {165, 142080},
197 {166, 142336},
198 {167, 142592},
199 {168, 142848},
200 {169, 143104},
201 {170, 143360},
202 {171, 143616},
203 {172, 143872},
204 {173, 144128},
205 {174, 144384},
206 {175, 144640},
207 {176, 144896},
208 {177, 145152},
209 {178, 145408},
210 {179, 145664},
211 {180, 145920},
212 {181, 146176},
213 {182, 146432},
214 {183, 146688},
215 {184, 146944},
216 {185, 147200},
217 {186, 147456},
218 {187, 147712},
219 {188, 147968},
220 {189, 148224},
221 {190, 148480},
222 {191, 148736},
223 {192, 148992},
224 {193, 149248},
225 {194, 149504},
226 {195, 149760},
227 {196, 150016},
228 {197, 150272},
229 {198, 150528},
230 {199, 150784},
231 {200, 151040},
232 {201, 151296},
233 {202, 151552},
234 {203, 151808},
235 {204, 152064},
236 {205, 152320},
237 {206, 152576},
238 {207, 152832},
239 {208, 153088},
240 {209, 153344},
241 {210, 153600},
242 {211, 153856},
243 {212, 154112},
244 {213, 154368},
245 {214, 154624},
246 {215, 154880},
247 {216, 155136},
248 {217, 155392},
249 {218, 155648},
250 {219, 155904},
251 {220, 156160},
252 {221, 156416},
253 {222, 156672},
254 {223, 156928},
255 {224, 157184},
256 {225, 157440},
257 {226, 157696},
258 {227, 157952},
259 {228, 158208},
260 {229, 158464},
261 {230, 158720},
262 {231, 158976},
263 {232, 159232},
264 {233, 159488},
265 {234, 159744},
266 {235, 160000},
267 {236, 160256},
268 {237, 160512},
269 {238, 160768},
270 {239, 161024},
271 {240, 161280},
272 {241, 161536},
273 {242, 161792},
274 {243, 162048},
275 {244, 162304},
276 {245, 162560},
277 {246, 162816},
278 {247, 163072},
279 {248, 163328},
280 {249, 163584},
281 {250, 163840},
282 {251, 164096},
283 {252, 164352},
284 {253, 164608},
285 {254, 164864},
286 {255, 165120}
287 };
288
289 struct lnode {
290 struct bnode data;
291 struct lnode* left;
292 struct lnode* right;
293 };
294
295 int find_part (int n) {
296 /* http://stackoverflow.com/a/26896494 */
297 int x = 1;
298 while (x <= n/2) x*=2;
299
300 if (x/2 - 1 <= (n-x)) return x-1;
301 else return n - x/2;
302 }
303
304 struct lnode* gen_tree (struct bnode* a, int start, int end) {
305 if (start > end) return NULL;
306 //int mid = (end-start)/2+start; //will be right-aligned
307 //int mid = ceil ((end-start)/2.0)+start; //will be half left-, half middle-aligned
308 int mid = find_part (end-start)+start;
309
310 struct lnode* b = malloc (sizeof (struct lnode));
311 b->data.block = a[mid].block;
312 b->data.start = a[mid].start;
313 b->left = gen_tree (a, start, mid-1);
314 b->right = gen_tree (a, mid+1, end);
315
316 return b;
317 }
318
319 /* to print it breadth first, we need to implement a queue first :| */
320 struct queue {
321 struct lnode* data;
322 struct queue* next;
323 };
324 int empty (struct queue* q) {return q == NULL;}
325 void prepend (struct queue** q, struct lnode* d) {
326 struct queue* tmp = malloc (sizeof (struct queue));
327 tmp->data = d;
328 tmp->next = *q;
329 *q = tmp;
330 }
331 struct lnode* get_e (struct queue* q, int index) {
332 for (int i = 0; i < index; q = q->next, i++);
333 return q->data;
334 }
335 void del_e (struct queue** q, int index) {
336 if (index == 0) {
337 struct queue* tmp = (*q)->next;
338 free (*q);
339 (*q) = tmp;
340 } else {
341 struct queue* p = (*q);
342 for (int i = 0; i < index-1; p = p->next, i++);
343 free (p->next);
344 p->next = p->next->next;
345 }
346 }
347 void enqueue (struct queue** q, struct lnode* d, int* size) {
348 prepend (q, d);
349 (*size)++;
350 }
351 struct lnode* dequeue (struct queue** q, int* size) {
352 (*size)--;
353 struct lnode* retval = get_e (*q, *size);
354 del_e (q, *size);
355 return retval;
356 }
357 void print (struct lnode* l) {
358 struct queue* q = NULL;
359 int s = 0;
360
361 enqueue (&q, l, &s);
362 while (!empty (q)) {
363 struct lnode* node = dequeue (&q, &s);
364 if (node->data.start != 0) printf ("{%d, %d}, \n", node->data.block, node->data.start);
365 if (node->left != NULL) enqueue (&q, node->left, &s);
366 if (node->right != NULL) enqueue (&q, node->right, &s);
367 }
368 // add the extra bits:
369 printf ("{%d, %d}, \n{%d, %d}", extra[0].block, extra[0].start, extra[1].block, extra[1].start);
370 }
371
372
373 int main () {
374 int len = 255;
375 struct lnode* s = gen_tree (btree, 0, len);
376 print (s);
377
378 //NOTE: memory occupied by the tree-as-linked-list is not freed. deal with it.
379 }
Imprint / Impressum