Skip to content

Commit 0a2ab9d

Browse files
committed
COMMON: fix hash, code cleanup
1 parent e9ae3ae commit 0a2ab9d

File tree

3 files changed

+45
-54
lines changed

3 files changed

+45
-54
lines changed

src/common/ceval.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ void cev_prim() {
6363
IP += OS_STRLEN;
6464
bc_add_dword(bc_out, len);
6565
bc_add_n(bc_out, bc_in->ptr + bc_in->cp, len);
66-
6766
IP += len;
6867
break;
6968
case kwTYPE_CALL_UDP:

src/common/search.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <sys/cdefs.h>
1414
#include <assert.h>
1515
#include <stdlib.h>
16+
#include <stdio.h>
1617

1718
#include "search.h"
1819

@@ -67,20 +68,22 @@ void *tfind(const void *vkey, void **vrootp, tcompare_cb compar) {
6768

6869
/* find or insert datum into search tree */
6970
void *tsearch(const void *vkey, void **vrootp, tcompare_cb compar) {
70-
node_t **rootp = (node_t **) tfind(vkey, vrootp, compar);
71-
node_t *q;
72-
73-
if (rootp != NULL) {
74-
q = *rootp;
75-
} else {
76-
// key not found. make new node and link to old
77-
q = malloc(sizeof(node_t));
78-
if (q != 0) {
79-
rootp = (node_t **)vrootp;
80-
*rootp = q;
81-
q->key = (void *)vkey;
82-
q->left = q->right = NULL;
71+
node_t **rootp = (node_t **)vrootp;
72+
if (rootp == NULL) {
73+
return NULL;
74+
}
75+
while (*rootp != NULL) {
76+
int r;
77+
if ((r = compar(vkey, (*rootp)->key)) == 0) {
78+
return *rootp;
8379
}
80+
rootp = (r < 0) ? &(*rootp)->left : &(*rootp)->right;
81+
}
82+
node_t *q = malloc(sizeof(node_t));
83+
if (q != 0) {
84+
*rootp = q;
85+
q->key = (void *)vkey;
86+
q->left = q->right = NULL;
8487
}
8588
return q;
8689
}

src/common/var.c

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,12 @@ void v_resize_array(var_t *v, dword size) {
177177
v->v.a.ubound[0] = v->v.a.lbound[0] + (size - 1);
178178
v->v.a.maxdim = 1;
179179

180-
//
181180
if (prev) {
182181
tmp_free(prev);
183182
}
184183
} else if (v->v.a.size < size) {
185184
// resize up
186-
// if there is space do not resize
185+
// if there is space do not resize
187186
if (v->v.a.size == 0) {
188187
prev = v->v.a.ptr;
189188
v->v.a.ptr = tmp_alloc((size + ARR_ALLOC) * sizeof(var_t));
@@ -210,7 +209,6 @@ void v_resize_array(var_t *v, dword size) {
210209
v->v.a.ubound[0] = v->v.a.lbound[0] + (size - 1);
211210
v->v.a.maxdim = 1;
212211

213-
//
214212
if (prev) {
215213
tmp_free(prev);
216214
}
@@ -345,32 +343,36 @@ int v_compare(var_t *a, var_t *b) {
345343
return strcmp(a->v.p.ptr, b->v.p.ptr);
346344
}
347345
if ((a->type == V_STR) && (b->type == V_NUM)) {
348-
if (a->v.p.ptr[0] == '\0' || is_number((char *) a->v.p.ptr)) { // compare
349-
// nums
346+
if (a->v.p.ptr[0] == '\0' || is_number((char *) a->v.p.ptr)) {
347+
// compare nums
350348
dt = v_getval(a);
351-
return (dt < b->v.n) ? -1 : ((dt == b->v.n) ? 0 : 1);}
352-
return 1;
349+
return (dt < b->v.n) ? -1 : ((dt == b->v.n) ? 0 : 1);
350+
}
351+
return 1;
353352
}
354353
if ((a->type == V_NUM) && (b->type == V_STR)) {
355-
if (b->v.p.ptr[0] == '\0' || is_number((char *) b->v.p.ptr)) { // compare
356-
// nums
354+
if (b->v.p.ptr[0] == '\0' || is_number((char *) b->v.p.ptr)) {
355+
// compare nums
357356
dt = v_getval(b);
358-
return (dt < a->v.n) ? 1 : ((dt == a->v.n) ? 0 : -1);}return
359-
- 1;
357+
return (dt < a->v.n) ? 1 : ((dt == a->v.n) ? 0 : -1);
358+
}
359+
return - 1;
360360
}
361361
if ((a->type == V_STR) && (b->type == V_INT)) {
362-
if (a->v.p.ptr[0] == '\0' || is_number((char *) a->v.p.ptr)) { // compare
363-
// nums
362+
if (a->v.p.ptr[0] == '\0' || is_number((char *) a->v.p.ptr)) {
363+
// compare nums
364364
di = v_igetval(a);
365-
return (di < b->v.i) ? -1 : ((di == b->v.i) ? 0 : 1);}
366-
return 1;
365+
return (di < b->v.i) ? -1 : ((di == b->v.i) ? 0 : 1);
366+
}
367+
return 1;
367368
}
368369
if ((a->type == V_INT) && (b->type == V_STR)) {
369-
if (b->v.p.ptr[0] == '\0' || is_number((char *) b->v.p.ptr)) { // compare
370-
// nums
370+
if (b->v.p.ptr[0] == '\0' || is_number((char *) b->v.p.ptr)) {
371+
// compare nums
371372
di = v_igetval(b);
372-
return (di < a->v.i) ? 1 : ((di == a->v.i) ? 0 : -1);}return
373-
- 1;
373+
return (di < a->v.i) ? 1 : ((di == a->v.i) ? 0 : -1);
374+
}
375+
return - 1;
374376
}
375377
if ((a->type == V_ARRAY) && (b->type == V_ARRAY)) {
376378
// check size
@@ -388,8 +390,8 @@ return 1;
388390
return ci;
389391
}
390392
}
391-
392-
return 0; // equal
393+
// equal
394+
return 0;
393395
}
394396

395397
if (a->type == V_UDS && b->type == V_UDS) {
@@ -400,25 +402,10 @@ return 1;
400402
return hash_compare(a, b);
401403
}
402404

403-
err_evtype(); // ndc 01/08/2001
405+
err_evtype();
404406
return 1;
405407
}
406408

407-
/*
408-
*/
409-
int v_addtype(var_t *a, var_t *b) {
410-
if (a->type == V_STR) {
411-
return V_STR;
412-
}
413-
if (a->type == V_NUM || b->type == V_NUM) {
414-
return V_NUM;
415-
}
416-
if (a->type == V_INT || b->type == V_STR) {
417-
return V_NUM;
418-
}
419-
return V_INT;
420-
}
421-
422409
/*
423410
* add two variables
424411
* result = a + b
@@ -512,6 +499,7 @@ void v_set(var_t *dest, const var_t *src) {
512499
return;
513500
} else if (dest->type == V_HASH) {
514501
// lvalue struct assigned to non-struct rvalue
502+
fprintf(stderr, "hash_clear\n");
515503
hash_clear(dest);
516504
return;
517505
}
@@ -585,8 +573,8 @@ void v_inc(var_t *a, var_t *b) {
585573
*/
586574
int v_sign(var_t *x) {
587575
if (x->type == V_INT) {
588-
return (x->v.i < 0) ? -1 : ((x->v.i == 0) ? 0 : 1);}
589-
else if (x->type == V_NUM) {
576+
return (x->v.i < 0) ? -1 : ((x->v.i == 0) ? 0 : 1);
577+
} else if (x->type == V_NUM) {
590578
return (x->v.n < 0) ? -1 : ((x->v.n == 0) ? 0 : 1);
591579
}
592580
err_varnotnum();
@@ -794,7 +782,8 @@ void v_zerostr(var_t *r) {
794782
void v_input2var(const char *str, var_t *var) {
795783
v_free(var);
796784

797-
if (strlen(str) == 0) { // no data
785+
if (strlen(str) == 0) {
786+
// no data
798787
v_setstr(var, str);
799788
} else {
800789
char *np, *sb;

0 commit comments

Comments
 (0)