00001
00002
00003
00004
00005
00006 #ifndef lint
00007 static char vcid[] = "$Id: trees.c,v 1.3 1995/07/27 21:22:21 duchier Exp $";
00008 #endif
00009
00010 #include "extern.h"
00011 #include "print.h"
00012 #include "memory.h"
00013 #include "login.h"
00014
00015
00016
00017
00018
00019
00020 long intcmp(a,b)
00021 long a;
00022 long b;
00023 {
00024 return a-b;
00025 }
00026
00027
00028
00029
00030
00031 long is_int(s, len, sgn)
00032 char **s;
00033 long *len;
00034 long *sgn;
00035 {
00036 char *sint;
00037 char *stmp;
00038
00039
00040
00041
00042
00043
00044
00045
00046 stmp=(*s);
00047 if (*sgn=(*stmp=='-')) {
00048 stmp++;
00049 if (!*stmp) return FALSE;
00050 }
00051 if (!*stmp) return FALSE;
00052 while (*stmp=='0') stmp++;
00053 sint=stmp;
00054 while (*stmp) {
00055 if (*stmp<'0' || *stmp>'9') return FALSE;
00056 stmp++;
00057 }
00058 *len=stmp-sint;
00059 *s=sint;
00060 return TRUE;
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 long featcmp(str1,str2)
00074 char *str1, *str2;
00075 {
00076 long len1,len2,sgn1,sgn2;
00077 char *s1,*s2;
00078
00079 if(str1==str2)
00080 return 0;
00081
00082
00083
00084 if(*(str1+1)==0 && *(str2+1)==0)
00085 return *str1 - *str2;
00086
00087
00088 s1=str1;
00089 s2=str2;
00090
00091 if (is_int(&s1,&len1,&sgn1)) {
00092 if (is_int(&s2,&len2,&sgn2)) {
00093 if (sgn1!=sgn2) return (sgn2-sgn1);
00094 if (len1!=len2) return (len1-len2);
00095 return strcmp(s1,s2);
00096 }
00097 else
00098 return -1;
00099 }
00100 else {
00101 if (is_int(&s2,&len2,&sgn2))
00102 return 1;
00103 else
00104 return strcmp(s1,s2);
00105 }
00106 }
00107
00108
00109
00110
00111
00112
00113 char *heap_ncopy_string(s,n)
00114 char *s;
00115 int n;
00116 {
00117 char *p;
00118
00119 if (s==one || s==two) return s;
00120
00121 p=(char *)heap_alloc(n+1);
00122 strncpy(p,s,n);
00123 p[n]='\0';
00124
00125 return p;
00126 }
00127
00128
00129
00130
00131
00132 char *heap_copy_string(s)
00133 char *s;
00134 { return heap_ncopy_string(s,strlen(s)); }
00135
00136
00137
00138
00139
00140
00141
00142 char *stack_copy_string(s)
00143 char *s;
00144 {
00145 char *p;
00146
00147 if (s==one || s==two) return s;
00148
00149 p=(char *)stack_alloc(strlen(s)+1);
00150 strcpy(p,s);
00151
00152 return p;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 ptr_node general_insert(comp,keystr,tree,info,heapflag,copystr,bkflag)
00175 long (*comp)();
00176 char *keystr;
00177 ptr_node *tree;
00178 GENERIC info;
00179 long heapflag, copystr, bkflag;
00180 {
00181 long cmp;
00182 ptr_node result;
00183 long to_do=TRUE;
00184
00185
00186 do {
00187 if (*tree==NULL) {
00188 if (bkflag==1) push_ptr_value(int_ptr,tree);
00189 else if (bkflag==2) push_ptr_value_global(int_ptr,tree);
00190 *tree = (heapflag==HEAP) ? HEAP_ALLOC(node): STACK_ALLOC(node);
00191 result= *tree;
00192 (*tree)->key = copystr ? heap_copy_string(keystr) : keystr;
00193 (*tree)->left=NULL;
00194 (*tree)->right=NULL;
00195 (*tree)->data=info;
00196 to_do=FALSE;
00197 }
00198 else {
00199 cmp=(*comp)(keystr,(*tree)->key);
00200 if (cmp<0)
00201 tree=(&((*tree)->left));
00202 else
00203 if (cmp==0) {
00204 if (bkflag)
00205 Errorline("attempt to overwrite an existing feature; ignored.\n");
00206 else
00207 (*tree)->data=info;
00208 result= *tree;
00209 to_do=FALSE;
00210 }
00211 else
00212 tree=(&((*tree)->right));
00213 }
00214 } while (to_do);
00215
00216 return result;
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226 void heap_insert_copystr(keystr,tree,info)
00227 char *keystr;
00228 ptr_node *tree;
00229 GENERIC info;
00230 {
00231 general_insert(featcmp,keystr,tree,info,HEAP,TRUE,0);
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241 void stack_insert_copystr(keystr,tree,info)
00242 char *keystr;
00243 ptr_node *tree;
00244 GENERIC info;
00245 {
00246 general_insert(featcmp,keystr,tree,info,STACK,TRUE,0);
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256 ptr_node heap_insert(comp,keystr,tree,info)
00257 long (*comp)();
00258 char *keystr;
00259 ptr_node *tree;
00260 GENERIC info;
00261 {
00262 return general_insert(comp,keystr,tree,info,HEAP,FALSE,0);
00263 }
00264
00265
00266
00267
00268
00269
00270 ptr_node stack_insert(comp,keystr,tree,info)
00271 long (*comp)();
00272 char *keystr;
00273 ptr_node *tree;
00274 GENERIC info;
00275 {
00276 return general_insert(comp,keystr,tree,info,STACK,FALSE,0);
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 ptr_node bk_stack_insert(comp,keystr,tree,info)
00288 long (*comp)();
00289 char *keystr;
00290 ptr_node *tree;
00291 GENERIC info;
00292 {
00293 return general_insert(comp,keystr,tree,info,STACK,FALSE,1);
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 ptr_node bk2_stack_insert(comp,keystr,tree,info)
00305 long (*comp)();
00306 char *keystr;
00307 ptr_node *tree;
00308 GENERIC info;
00309 {
00310 return general_insert(comp,keystr,tree,info,STACK,FALSE,2);
00311 }
00312
00313
00314
00315
00316
00317
00318
00319 ptr_node find(comp,keystr,tree)
00320 long (*comp)();
00321 char *keystr;
00322 ptr_node tree;
00323 {
00324 ptr_node result;
00325 long cmp;
00326 long to_do=TRUE;
00327
00328
00329
00330
00331
00332
00333 do {
00334 if (tree==NULL) {
00335 result=NULL;
00336 to_do=FALSE;
00337 }
00338 else {
00339 cmp=(*comp)(keystr,tree->key);
00340 if (cmp<0)
00341 tree=tree->left;
00342 else
00343 if (cmp==0) {
00344 result=tree;
00345 to_do=FALSE;
00346 }
00347 else
00348 tree=tree->right;
00349 }
00350 } while (to_do);
00351
00352
00353
00354
00355
00356
00357
00358 return result;
00359 }
00360
00361
00362
00363
00364
00365
00366
00367
00368 ptr_node find_data(p,t)
00369 GENERIC p;
00370 ptr_node t;
00371 {
00372 ptr_node r=NULL;
00373
00374 if(t)
00375 if(t->data==p)
00376 r=t;
00377 else {
00378 r=find_data(p,t->left);
00379 if(r==NULL)
00380 r=find_data(p,t->right);
00381 }
00382
00383 return r;
00384 }
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 void delete_attr(s,n)
00436 char *s;
00437 ptr_node *n;
00438 {
00439 long cmp;
00440 ptr_node new,r;
00441
00442 if (*n) {
00443 cmp=featcmp(s,(*n)->key);
00444 if (cmp<0)
00445 delete_attr(s,&((*n)->left));
00446 else if (cmp>0)
00447 delete_attr(s,&((*n)->right));
00448 else if ((*n)->left) {
00449 if ((*n)->right) {
00450 r=(*n)->right;
00451 new=heap_insert(featcmp,r->key,&((*n)->left),r->data);
00452 new->left=r->left;
00453 new->right=r->right;
00454 *n = (*n) -> left;
00455 }
00456 else
00457 *n = (*n)->left;
00458 }
00459 else
00460 *n = (*n)->right;
00461 }
00462 }