-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgyges_ysort.c
511 lines (468 loc) · 18.2 KB
/
gyges_ysort.c
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*============================----beg-of-source---============================*/
#include "gyges.h"
/*
* cells are created once, updated sometimes, and calculated frequently
*
*
*
*
*
*
*
*
*/
static char s_levels = 0;
static char s_result = 0;
static char s_path [LEN_DESC] = "";
static char s_depth = 0;
static char s_last [LEN_LABEL] = "";
static tCELL *s_save = NULL;
/*====================------------------------------------====================*/
/*===---- shared helpers ----===*/
/*====================------------------------------------====================*/
static void o___HELPERS_________o () { return; }
long
api_ysort_label2key (char *a_label)
{
int x_tab, x_col, x_row;
char rc;
rc = ystr2gyges (a_label, &x_tab, &x_col, &x_row, NULL, NULL, 0, YSTR_USABLE);
if (rc < 0) return -1;
return x_row + (x_col * 10000) + (x_tab * 10000000);
}
long
api_ysort_coord2key (int a_tab, int a_col, int a_row, char *a_label)
{
char rc;
rc = ystr4gyges (a_tab, a_col, a_row, NULL, NULL, a_label, YSTR_USABLE);
if (rc < 0) return -1;
if (a_tab < 0 && a_col < 0 && a_row < 0) return -1;
return a_row + (a_col * 10000) + (a_tab * 10000000);
}
/*====================------------------------------------====================*/
/*===---- sort the cells ----===*/
/*====================------------------------------------====================*/
static void o___YSORT___________o () { return; }
char
api_ysort__common (char a_type, void *a_one , void *a_two)
{
/*---(locals)-----------+-----------+-*/
char rce = -10;
/*---(header)-------------------------*/
DEBUG_YSORT yLOG_senter (__FUNCTION__);
/*---(check pointers)-----------------*/
DEBUG_YSORT yLOG_spoint (a_one);
--rce; if (a_one == NULL && a_type != 'i') {
return rce;
}
DEBUG_YSORT yLOG_spoint (a_two);
--rce; if (a_two == NULL) {
return rce;
}
--rce; if (a_one == a_two) {
DEBUG_YSORT yLOG_snote ("same, no action");
return rce;
}
/*---(complete)-----------------------*/
DEBUG_YSORT yLOG_sexit (__FUNCTION__);
return 0;
}
char
api_ysort__cursor (uchar a_type, void *a_head, void *a_tail, void *a_beg, void **a_new, char a_action)
{
/*---(locals)-----------+-----------+-*/
char rce = -10;
tCELL *x_beg = NULL;
tCELL *x_new = NULL;
/*---(header)-------------------------*/
DEBUG_YSORT yLOG_enter (__FUNCTION__);
/*---(check pointers)-----------------*/
DEBUG_YSORT yLOG_point ("a_beg" , a_beg);
DEBUG_YSORT yLOG_point ("a_new" , a_new);
--rce; if (a_new == NULL) {
DEBUG_YSORT yLOG_exitr (__FUNCTION__, rce);
return rce;
}
x_new = (tCELL *) a_head;
/*---(prepare)------------------------*/
DEBUG_YSORT yLOG_note ("cast");
if (a_beg != NULL) {
x_beg = (tCELL *) a_beg;
DEBUG_YSORT yLOG_info ("x_beg" , x_beg->label);
}
/*---(update)-------------------------*/
DEBUG_YSORT yLOG_char ("a_action" , a_action);
switch (a_action) {
case YDLST_HEAD : x_new = (tCELL *) a_head; break;
case YDLST_PREV : if (x_beg != NULL) x_new = x_beg->m_prev; break;
case YDLST_NEXT : if (x_beg != NULL) x_new = x_beg->m_next; break;
case YDLST_TAIL : x_new = (tCELL *) a_tail; break;
}
/*---(save back)----------------------*/
if (x_new != NULL) {
DEBUG_YSORT yLOG_note (x_new->label);
}
*a_new = x_new;
/*---(complete)-----------------------*/
DEBUG_YSORT yLOG_exit (__FUNCTION__);
return 0;
}
char
api_ysort__checker (uchar a_type, uchar a_lvl, void *a_one, void *a_two, uchar a_order)
{
/*---(locals)-----------+-----------+-*/
char rce = -10;
char rc = 0;
tCELL *x_one = NULL;
tCELL *x_two = NULL;
long x_key = -1;
/*---(header)-------------------------*/
DEBUG_YSORT yLOG_enter (__FUNCTION__);
DEBUG_YSORT yLOG_complex ("args" , "%c, %2d, %p, %p, %c", a_type, a_lvl, a_one, a_two, a_order);
/*---(defense)------------------------*/
rc = api_ysort__common ('c', a_one, a_two);
--rce; if (rc < 0) {
DEBUG_YSORT yLOG_exitr (__FUNCTION__, rce);
return 0;
}
/*---(prepare)------------------------*/
x_one = (tCELL *) a_one;
/*---(handle object)------------------*/
if (a_type == tolower (a_type)) {
DEBUG_YSORT yLOG_note ("normal, sorting compare");
x_two = (tCELL *) a_two;
x_key = x_two->key;
} else {
DEBUG_YSORT yLOG_note ("search compare to value");
x_key = * ((long *) a_two);
}
DEBUG_YSORT yLOG_value ("x_one->key", x_one->key);
DEBUG_YSORT yLOG_value ("x_key" , x_key);
if (x_one->key < x_key) rc = -1;
else if (x_one->key > x_key) rc = 1;
else if (x_one->key == x_key) rc = 0;
DEBUG_YSORT yLOG_value ("compare" , rc);
/*---(complete)-----------------------*/
DEBUG_YSORT yLOG_exit (__FUNCTION__);
return rc;
}
char
api_ysort__unlinker (uchar a_type, void **a_head, void **a_tail, void *a_two)
{
/*---(locals)-----------+-----------+-*/
char rce = -10;
char rc = 0;
tCELL *x_head = NULL;
tCELL *x_tail = NULL;
tCELL *x_two = NULL;
/*---(header)-------------------------*/
DEBUG_YSORT yLOG_enter (__FUNCTION__);
/*---(defense)------------------------*/
rc = api_ysort__common ('i', NULL, a_two);
--rce; if (rc < 0) {
DEBUG_YSORT yLOG_exitr (__FUNCTION__, rce);
return 0;
}
/*---(prepare)------------------------*/
x_head = * ((tCELL **) a_head);
x_tail = * ((tCELL **) a_tail);
DEBUG_YSORT yLOG_complex ("pointers" , "head %p, tail %p", x_head, x_tail);
x_two = (tCELL *) a_two;
/*---(unlink current from list)-------*/
DEBUG_YSORT yLOG_note ("unlink");
if (x_two->m_next != NULL) x_two->m_next->m_prev = x_two->m_prev;
else x_tail = x_two->m_prev;
if (x_two->m_prev != NULL) x_two->m_prev->m_next = x_two->m_next;
else x_head = x_two->m_next;
/*---(ground pointers)----------------*/
DEBUG_YSORT yLOG_note ("ground pointers");
x_two->m_next = NULL;
x_two->m_prev = NULL;
/*---(save back)----------------------*/
DEBUG_YSORT yLOG_note ("save back");
*a_head = x_head;
*a_tail = x_tail;
/*---(complete)-----------------------*/
DEBUG_YSORT yLOG_exit (__FUNCTION__);
return 0;
}
char
api_ysort__linker (uchar a_type, void **a_head, void **a_tail, void *a_one, void *a_two)
{
/*---(locals)-----------+-----------+-*/
char rce = -10;
char rc = 0;
tCELL *x_head = NULL;
tCELL *x_tail = NULL;
tCELL *x_one = NULL;
tCELL *x_two = NULL;
/*---(header)-------------------------*/
DEBUG_YSORT yLOG_enter (__FUNCTION__);
/*---(defense)------------------------*/
rc = api_ysort__common ('i', a_one, a_two);
--rce; if (rc < 0) {
DEBUG_YSORT yLOG_exitr (__FUNCTION__, rce);
return 0;
}
/*---(prepare)------------------------*/
x_head = * ((tCELL **) a_head);
x_tail = * ((tCELL **) a_tail);
x_one = (tCELL *) a_one;
x_two = (tCELL *) a_two;
/*---(insert back in)-----------------*/
if (a_one == NULL) {
if (x_head == NULL) {
DEBUG_YSORT yLOG_note ("add first");
x_head = x_two;
x_two->m_prev = NULL;
} else {
DEBUG_YSORT yLOG_note ("append to tail");
x_tail->m_next = x_two;
x_two->m_prev = x_tail;
}
x_tail = x_two;
x_two->m_next = NULL;
} else {
DEBUG_YSORT yLOG_note ("insert before");
if (x_one->m_prev != NULL) x_one->m_prev->m_next = x_two;
else x_head = x_two;
x_two->m_prev = x_one->m_prev;
x_two->m_next = x_one;
x_one->m_prev = x_two;
}
/*---(save back)----------------------*/
DEBUG_YSORT yLOG_note ("save back");
*a_head = x_head;
*a_tail = x_tail;
/*---(output)-------------------------*/
DEBUG_YSORT yLOG_exit (__FUNCTION__);
/*---(complete)-----------------------*/
return 0;
}
char
api_ysort__forker (uchar a_type, void *a_node, void **a_left, void **a_right)
{
/*---(locals)-----------+-----------+-*/
char rce = -10;
char rc = 0;
tCELL *x_curr = NULL;
tCELL *x_left = NULL;
tCELL *x_right = NULL;
/*---(header)-------------------------*/
DEBUG_YSORT yLOG_enter (__FUNCTION__);
DEBUG_YSORT yLOG_complex ("args" , "%c, %p, %p, %p", a_type, a_node, a_left, a_right);
/*---(prepare)------------------------*/
x_curr = (tCELL *) a_node;
DEBUG_YSORT yLOG_point ("x_curr" , x_curr);
DEBUG_YSORT yLOG_info ("label" , x_curr->label);
/*---(set)----------------------------*/
if (a_type == tolower (a_type)) {
DEBUG_YSORT yLOG_note ("saving run");
x_left = * ((tCELL **) a_left);
x_right = * ((tCELL **) a_right);
if (a_left != NULL) x_curr->b_left = x_left;
if (a_right != NULL) x_curr->b_right = x_right;
}
/*---(get)----------------------------*/
else {
if (a_left != NULL) *a_left = x_curr->b_left;
if (a_right != NULL) *a_right = x_curr->b_right;
}
/*---(output)-------------------------*/
DEBUG_YSORT yLOG_exit (__FUNCTION__);
/*---(complete)-----------------------*/
return 0;
}
/*====================------------------------------------====================*/
/*===---- searching the btree ----===*/
/*====================------------------------------------====================*/
static void o___SEARCH__________o () { return; }
char
api_ysort_by_label (tCELL **a_found, char *a_label)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
char rc = 0;
void *x_node = NULL;
long x_key = -1;
/*---(header)-------------------------*/
DEBUG_YSORT yLOG_enter (__FUNCTION__);
x_key = api_ysort_label2key (a_label);
DEBUG_YSORT yLOG_value ("x_key" , x_key);
--rce; if (x_key < 0) {
DEBUG_YSORT yLOG_note ("bad label");
s_result = -1;
DEBUG_YSORT yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(search)-------------------------*/
DEBUG_YSORT yLOG_note ("dive into btree");
rc = ySORT_search (YSORT_NONE, rcell, &x_key, &x_node);
/*---(return)-------------------------*/
DEBUG_YSORT yLOG_note ("found");
if (a_found != NULL) *a_found = (tCELL *) x_node;
/*---(complete)-----------------------*/
DEBUG_YSORT yLOG_exit (__FUNCTION__);
return rc;
}
char
api_ysort_by_coord (tCELL **a_found, int a_tab, int a_col, int a_row)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
char rc = 0;
void *x_node = NULL;
char x_label [LEN_LABEL] = "";
long x_key = -1;
/*---(header)-------------------------*/
DEBUG_YSORT yLOG_enter (__FUNCTION__);
/*---(short-cut)----------------------*/
x_key = api_ysort_coord2key (a_tab, a_col, a_row, x_label);
DEBUG_YSORT yLOG_value ("x_key" , x_key);
--rce; if (x_key < 0) {
DEBUG_YSORT yLOG_note ("bad coordinates");
s_result = -1;
DEBUG_YSORT yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(search)-------------------------*/
DEBUG_YSORT yLOG_note ("dive into btree");
rc = ySORT_search (YSORT_NONE, rcell, &x_key, &x_node);
/*---(return)-------------------------*/
DEBUG_YSORT yLOG_note ("found");
if (a_found != NULL) *a_found = (tCELL *) x_node;
/*---(complete)-----------------------*/
DEBUG_YSORT yLOG_exit (__FUNCTION__);
return rc;
}
/*====================------------------------------------====================*/
/*===---- reporting out ----===*/
/*====================------------------------------------====================*/
static void o___REPORT__________o () { return; }
static FILE *s_file = NULL;
llong s_prev = -999;
char
api_ysort_dumper (uchar a_type, int a_lvl, tCELL *a_node, char *a_path, char *a_pre)
{
char x_pre [LEN_RECD] = "";
int i = 0;
int x_len = 0;
if (a_node == NULL) return 0;
/*---(prefix and label)---------------*/
x_len = 10 - strlen (a_node->label);
fprintf (s_file, "%s%s%*.*s", a_pre, a_node->label, x_len, x_len, "··········");
/*---(spacer and suffix)--------------*/
ystrlcpy (x_pre, "", LEN_RECD);
for (i = 15; i > a_lvl; --i) strcat (x_pre, "···");
fprintf (s_file, "%s %2d %-20.20s %2dt, %3dc, %4dr, %20lld", x_pre, a_lvl, a_path, a_node->tab, a_node->col, a_node->row, a_node->key);
if (a_node->key < 0) fprintf (s_file, " buffer/register");
else if (s_prev == a_node->key) fprintf (s_file, " DUP");
else fprintf (s_file, " -");
s_prev = a_node->key;
fprintf (s_file, "\n");
return 0;
}
char
api_ysort_btree_dump (FILE *a_file)
{
/*---(locals)-----------+-----+-----+-*/
int c = 0;
tCELL *x_curr = NULL;
/*---(header)-------------------------*/
s_file = a_file;
s_prev = -999;
fprintf (s_file, "yVIKEYS, btree cell structure (:dump btree)\n");
fprintf (s_file, "count = %d all, %d linked\n", ACEL, NCEL);
ySORT_walk (YSORT_NONE, rcell, api_ysort_dumper);
fprintf (s_file, "\n");
/*---(complete)-----------------------*/
return 0;
}
/*====================------------------------------------====================*/
/*===---- simple driver ----===*/
/*====================------------------------------------====================*/
static void o___DRIVER__________o () { return; }
char
api_ysort_update (void)
{
char rc = 0;
/*---(header)-------------------------*/
DEBUG_YSORT yLOG_enter (__FUNCTION__);
DEBUG_YSORT yLOG_char ("my.btree" , my.btree);
if (my.btree != 'y') {
DEBUG_YSORT yLOG_note ("nothing to do");
DEBUG_YSORT yLOG_exit (__FUNCTION__);
return 0;
}
if (rc == 0) {
rc = ySORT_gnome ('-', YSORT_ASCEND, &hcell, &tcell);
DEBUG_YSORT yLOG_value ("gnome" , rc);
}
if (rc == 0) {
rc = ySORT_treeify ('-', hcell, tcell, ncell, &rcell);
DEBUG_YSORT yLOG_value ("treeify" , rc);
}
DEBUG_YSORT yLOG_exit (__FUNCTION__);
return rc;
}
/*====================------------------------------------====================*/
/*===---- program level ----===*/
/*====================------------------------------------====================*/
static void o___PROGRAM_________o () { return; }
char
api_ysort_init (void)
{
rcell = NULL;
s_levels = 0;
ySORT_config (YSORT_GNOME, api_ysort__cursor, api_ysort__checker, api_ysort__unlinker, api_ysort__linker, NULL, NULL, api_ysort__forker);
return 0;
}
/*====================------------------------------------====================*/
/*===---- unit testing ----===*/
/*====================------------------------------------====================*/
static void o___UNITTEST________o () { return; }
char* /*-> unit test accessor -----------------[ light [us.B60.2A3.F2]*/ /*-[01.0000.00#.#]-*/ /*-[--.---.---.--]-*/
api_ysort__unit (char *a_question, int n)
{
/*---(locals)-------------------------*/
tCELL *x_curr = NULL;
int c = 0;
int x_buckets = 0;
int x_slots = 0;
int x_max = 0;
int x_sorts = 0;
int x_comps = 0;
int x_swaps = 0;
int x_teles = 0;
int x_result = 0;
void *x_void = NULL;
tCELL *x_last = NULL;
int x_depth = 0;
char x_path [LEN_LABEL] = "";
char t [LEN_LABEL] = "";
/*---(parse location)-----------------*/
strcpy (unit_answer, "YSORT : label could not be parsed");
/*---(overall)------------------------*/
if (strcmp (a_question, "gnome" ) == 0) {
ySORT_gnome_stats (&x_sorts, &x_comps, &x_swaps, &x_teles);
snprintf (unit_answer, LEN_FULL, "YSORT gnome : %4d#, %4dc, %4ds, %4dt", x_sorts, x_comps, x_swaps, x_teles);
}
else if (strcmp (a_question, "troll" ) == 0) {
ySORT_troll_stats (&x_buckets, &x_slots, &x_max, &x_sorts, &x_comps, &x_swaps, &x_teles);
snprintf (unit_answer, LEN_FULL, "YSORT troll : %2db, %4ds, %2dx, %4d#, %4dc, %4ds, %4dt", x_buckets, x_slots, x_max, x_sorts, x_comps, x_swaps, x_teles);
}
else if (strcmp (a_question, "treeify" ) == 0) {
/*> ySORT_treeify_stats (&x_sorts, &x_comps, &x_swaps, &x_teles); <*
*> snprintf (unit_answer, LEN_FULL, "YSORT treeify : %4d#, %4dl, %4dc, %4ds, %4dt", x_sorts, x_levels, x_comps, x_swaps, x_teles); <*/
}
else if (strcmp (a_question, "result" ) == 0) {
/*> ySORT_search_stats (&x_result, &x_last, &x_depth, x_path); <*/
ySORT_search_stats (&x_result, &x_last , &x_depth, x_path);
/*> x_last = (tCELL *) x_void; <*/
if (x_last != NULL) ystrlcpy (t, x_last->label, LEN_LABEL);
else ystrlcpy (t, "-" , LEN_LABEL);
snprintf (unit_answer, LEN_FULL, "YSORT result : %2d %-8.8s %2d %s", x_result, t, x_depth, x_path);
}
/*---(complete)-----------------------*/
return unit_answer;
}