Wild Life: trees.c File Reference

Wild Life 2.29

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros
Functions
trees.c File Reference

Go to the source code of this file.

## Functions
long | intcmp (long a, long b)
long | is_int (char **s, long *len, long *sgn)
long | featcmp (char *str1, char *str2)
char * | heap_ncopy_string (char *s, int n)
char * | heap_copy_string (char *s)
char * | stack_copy_string (char *s)
ptr_node | general_insert (long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
void | heap_insert_copystr (char *keystr, ptr_node *tree, GENERIC info)
void | stack_insert_copystr (char *keystr, ptr_node *tree, GENERIC info)
ptr_node | heap_insert (long comp, char *keystr, ptr_node *tree, GENERIC info)
ptr_node | stack_insert (long comp, char *keystr, ptr_node *tree, GENERIC info)
ptr_node | bk_stack_insert (long comp, char *keystr, ptr_node *tree, GENERIC info)
ptr_node | bk2_stack_insert (long comp, char *keystr, ptr_node *tree, GENERIC info)
ptr_node | find (long comp, char *keystr, ptr_node tree)
ptr_node | find_data (GENERIC p, ptr_node t)
void | delete_attr (char *s, ptr_node *n)

## Function Documentation

ptr_node bk2_stack_insert | ( | long | comp,
|  | char * | keystr,
|  | ptr_node * | tree,
|  | GENERIC | info
| ) |  |

Definition at line 327 of file trees.c.

References FALSE, general_insert(), and STACK.

332 {
333
334 return general_insert(comp,keystr,tree,info,STACK,FALSE,2L);
335 }
FALSE
#define FALSE
Definition: def_const.h:128
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
Definition: trees.c:184
STACK
#define STACK
Definition: def_const.h:148

ptr_node bk_stack_insert | ( | long | comp,
|  | char * | keystr,
|  | ptr_node * | tree,
|  | GENERIC | info
| ) |  |

Definition at line 309 of file trees.c.

References FALSE, general_insert(), and STACK.

314 {
315
316 return general_insert(comp,keystr,tree,info,STACK,FALSE,1L);
317 }
FALSE
#define FALSE
Definition: def_const.h:128
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
Definition: trees.c:184
STACK
#define STACK
Definition: def_const.h:148

void delete_attr | ( | char * | s,
|  | ptr_node * | n
| ) |  |

Definition at line 466 of file trees.c.

References wl_node::data, featcmp(), FEATCMP, heap_insert(), wl_node::key, wl_node::left, and wl_node::right.

469 {
470 long cmp;
471 ptr_node new,r;
472
473 if (*n) {
474 cmp=featcmp(s,(*n)->key);
475 if (cmp<0)
476 delete_attr(s,&((*n)->left));
477 else if (cmp>0)
478 delete_attr(s,&((*n)->right));
479 else if ((*n)->left) {
480 if ((*n)->right) {
481 r=(*n)->right;
482 new=heap_insert(FEATCMP,r->key,&((*n)->left),r->data);
483 new->left=r->left;
484 new->right=r->right;
485 *n = (*n) -> left;
486 }
487 else
488 *n = (*n)->left;
489 }
490 else
491 *n = (*n)->right;
492 }
493 }
FEATCMP
#define FEATCMP
Definition: def_const.h:257
wl_node::data
GENERIC data
Definition: def_struct.h:185
wl_node::left
ptr_node left
Definition: def_struct.h:183
wl_node::key
char * key
Definition: def_struct.h:182
heap_insert
ptr_node heap_insert(long comp, char *keystr, ptr_node *tree, GENERIC info)
Definition: trees.c:276
featcmp
long featcmp(char *str1, char *str2)
Definition: trees.c:89
wl_node
Definition: def_struct.h:181
delete_attr
void delete_attr(char *s, ptr_node *n)
Definition: trees.c:466
wl_node::right
ptr_node right
Definition: def_struct.h:184

long featcmp | ( | char * | str1,
|  | char * | str2
| ) |  |

Definition at line 89 of file trees.c.

References is_int().

91 {
92 long len1,len2,sgn1,sgn2;
93 char *s1,*s2;
94
95 if(str1==str2)
96 return 0;
97
98 /* if (*str1==0 && *str2==0) return 0; "" bug is unaffected -- PVR 23.2.94 */
99
100 if(*(str1+1)==0 && *(str2+1)==0)
101 return *str1 - *str2;
102
103
104 s1=str1; /* Local copies of the pointers */
105 s2=str2;
106
107 if (is_int(&s1,&len1,&sgn1)) {
108 if (is_int(&s2,&len2,&sgn2)) {
109 if (sgn1!=sgn2) return (sgn2-sgn1); /* Check signs first */
110 if (len1!=len2) return (len1-len2); /* Then check lengths */
111 return strcmp(s1,s2); /* Use strcmp only if same sign and length */
112 }
113 else
114 return -1;
115 }
116 else {
117 if (is_int(&s2,&len2,&sgn2))
118 return 1;
119 else
120 return strcmp(s1,s2);
121 }
122 }
is_int
long is_int(char **s, long *len, long *sgn)
Definition: trees.c:27

ptr_node find | ( | long | comp,
|  | char * | keystr,
|  | ptr_node | tree
| ) |  |

Definition at line 341 of file trees.c.

References Errorline(), FALSE, featcmp(), FEATCMP, intcmp(), INTCMP, wl_node::left, NULL, wl_node::right, STRCMP, and TRUE.

345 {
346 ptr_node result;
347 long cmp;
348 long to_do=TRUE;
349
350 /*
351 if(comp==strcmp)
352 printf("%s ",keystr);
353 */
354
355 do {
356 if (tree==NULL) {
357 result=NULL;
358 to_do=FALSE;
359 }
360 else {
361 if (comp == INTCMP)
362 cmp = intcmp((long)keystr,(long) (tree)->key);
363 else if (comp == FEATCMP)
364 cmp = featcmp(keystr,(tree)->key);
365 else if (comp == STRCMP)
366 cmp = strcmp(keystr,(tree)->key);
367 else
368 Errorline("Bad comp in general_insert.\n");
369
370 if (cmp<0)
371 tree=tree->left;
372 else
373 if (cmp==0) {
374 result=tree;
375 to_do=FALSE;
376 }
377 else
378 tree=tree->right;
379 }
380 } while (to_do);
381
382
383 /* RM: Jan 27 1993
384 if(comp==strcmp)
385 printf("Find: '%s' -> %x\n",keystr,result);
386 */
387
388 return result;
389 }
FEATCMP
#define FEATCMP
Definition: def_const.h:257
INTCMP
#define INTCMP
Definition: def_const.h:256
NULL
#define NULL
Definition: def_const.h:203
wl_node::left
ptr_node left
Definition: def_struct.h:183
Errorline
void Errorline(char *format,...)
Definition: error.c:414
TRUE
#define TRUE
Definition: def_const.h:127
STRCMP
#define STRCMP
Definition: def_const.h:255
FALSE
#define FALSE
Definition: def_const.h:128
intcmp
long intcmp(long a, long b)
Definition: trees.c:12
featcmp
long featcmp(char *str1, char *str2)
Definition: trees.c:89
wl_node
Definition: def_struct.h:181
wl_node::right
ptr_node right
Definition: def_struct.h:184

ptr_node find_data | ( | GENERIC | p,
|  | ptr_node | t
| ) |  |

Definition at line 399 of file trees.c.

References wl_node::data, wl_node::left, NULL, and wl_node::right.

402 {
403 ptr_node r=NULL;
404
405 if(t)
406 if(t->data==p)
407 r=t;
408 else {
409 r=find_data(p,t->left);
410 if(r==NULL)
411 r=find_data(p,t->right);
412 }
413
414 return r;
415 }
find_data
ptr_node find_data(GENERIC p, ptr_node t)
Definition: trees.c:399
wl_node::data
GENERIC data
Definition: def_struct.h:185
NULL
#define NULL
Definition: def_const.h:203
wl_node::left
ptr_node left
Definition: def_struct.h:183
wl_node
Definition: def_struct.h:181
wl_node::right
ptr_node right
Definition: def_struct.h:184

ptr_node general_insert | ( | long | comp,
|  | char * | keystr,
|  | ptr_node * | tree,
|  | GENERIC | info,
|  | long | heapflag,
|  | long | copystr,
|  | long | bkflag
| ) |  |

Definition at line 184 of file trees.c.

References Errorline(), FALSE, featcmp(), FEATCMP, HEAP, HEAP_ALLOC, heap_copy_string(), int_ptr, intcmp(), INTCMP, wl_node::key, NULL, push_ptr_value(), push_ptr_value_global(), STACK_ALLOC, STRCMP, and TRUE.

190 {
191 long cmp;
192 ptr_node result;
193 long to_do=TRUE;
194
195
196 do {
197 if (*tree==NULL) {
198 if (bkflag==1) push_ptr_value(int_ptr,(GENERIC *)tree);
199 else if (bkflag==2) push_ptr_value_global(int_ptr,(GENERIC *)tree);
200 *tree = (heapflag==HEAP) ? HEAP_ALLOC(node): STACK_ALLOC(node);
201 result= *tree;
202 (*tree)->key = copystr ? heap_copy_string(keystr) : keystr;
203 (*tree)->left=NULL;
204 (*tree)->right=NULL;
205 (*tree)->data=info;
206 to_do=FALSE;
207 }
208 else {
209 if (comp == INTCMP)
210 cmp = intcmp((long)keystr,(long) (*tree)->key);
211 else if (comp == FEATCMP)
212 cmp = featcmp(keystr,(*tree)->key);
213 else if (comp == STRCMP)
214 cmp = strcmp(keystr,(*tree)->key);
215 else
216 Errorline("Bad comp in general_insert.\n");
217
218 if (cmp<0)
219 tree=(&((*tree)->left));
220 else
221 if (cmp==0) {
222 if (bkflag)
223 Errorline("attempt to overwrite an existing feature; ignored.\n");
224 else
225 (*tree)->data=info;
226 result= *tree;
227 to_do=FALSE;
228 }
229 else
230 tree=(&((*tree)->right));
231 }
232 } while (to_do);
233
234 return result;
235 }
HEAP
#define HEAP
Definition: def_const.h:147
FEATCMP
#define FEATCMP
Definition: def_const.h:257
push_ptr_value
void push_ptr_value(type_ptr t, GENERIC *p)
Definition: login.c:360
INTCMP
#define INTCMP
Definition: def_const.h:256
push_ptr_value_global
void push_ptr_value_global(type_ptr t, GENERIC *p)
Definition: login.c:488
heap_copy_string
char * heap_copy_string(char *s)
Definition: trees.c:147
NULL
#define NULL
Definition: def_const.h:203
Errorline
void Errorline(char *format,...)
Definition: error.c:414
wl_node::key
char * key
Definition: def_struct.h:182
TRUE
#define TRUE
Definition: def_const.h:127
STRCMP
#define STRCMP
Definition: def_const.h:255
FALSE
#define FALSE
Definition: def_const.h:128
intcmp
long intcmp(long a, long b)
Definition: trees.c:12
STACK_ALLOC
#define STACK_ALLOC(A)
Definition: def_macro.h:16
featcmp
long featcmp(char *str1, char *str2)
Definition: trees.c:89
wl_node
Definition: def_struct.h:181
GENERIC
unsigned long * GENERIC
Definition: def_struct.h:17
HEAP_ALLOC
#define HEAP_ALLOC(A)
Definition: def_macro.h:15
int_ptr
#define int_ptr
Definition: def_const.h:172

char* heap_copy_string | ( | char * | s | ) |

Definition at line 147 of file trees.c.

References heap_ncopy_string().

149 { return heap_ncopy_string(s,strlen(s)); }
heap_ncopy_string
char * heap_ncopy_string(char *s, int n)
Definition: trees.c:128

ptr_node heap_insert | ( | long | comp,
|  | char * | keystr,
|  | ptr_node * | tree,
|  | GENERIC | info
| ) |  |

Definition at line 276 of file trees.c.

References FALSE, general_insert(), and HEAP.

281 {
282
283 return general_insert(comp,keystr,tree,info,HEAP,FALSE,0L);
284 }
HEAP
#define HEAP
Definition: def_const.h:147
FALSE
#define FALSE
Definition: def_const.h:128
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
Definition: trees.c:184

void heap_insert_copystr | ( | char * | keystr,
|  | ptr_node * | tree,
|  | GENERIC | info
| ) |  |

Definition at line 245 of file trees.c.

References FEATCMP, general_insert(), HEAP, and TRUE.

249 {
250 (void)general_insert(FEATCMP,keystr,tree,info,HEAP,TRUE,0L);
251 }
HEAP
#define HEAP
Definition: def_const.h:147
FEATCMP
#define FEATCMP
Definition: def_const.h:257
TRUE
#define TRUE
Definition: def_const.h:127
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
Definition: trees.c:184

char* heap_ncopy_string | ( | char * | s,
|  | int | n
| ) |  |

Definition at line 128 of file trees.c.

References heap_alloc(), one, and two.

131 {
132 char *p;
133
134 if (s==one || s==two) return s;
135
136 p=(char *)heap_alloc(n+1);
137 strncpy(p,s,n);
138 p[n]='\0';
139
140 return p;
141 }
two
char * two
Definition: def_glob.h:251
one
char * one
Definition: def_glob.h:250
heap_alloc
GENERIC heap_alloc(long s)
Definition: memory.c:1518

long intcmp | ( | long | a,
|  | long | b
| ) |  |

Definition at line 12 of file trees.c.

16 {
17 #ifdef CMPDBG
18 printf("intcmp a = %ld b = %ld a - b = %ld\n", a ,b , a - b);
19 #endif
20 return a - b;
21 }

long is_int | ( | char ** | s,
|  | long * | len,
|  | long * | sgn
| ) |  |

Definition at line 27 of file trees.c.

References FALSE, and TRUE.

31 {
32 char *sint; /* Ptr to first non-zero digit */
33 char *stmp; /* Scratchpad for string ptr */
34 #ifdef CMPDBG
35 printf("is_int *s = %s\n",*s);
36 #endif
37 /*
38 { register char *p= *s;
39 register char c= *p;
40 if(c>'0' && c<='9' && *(p+1)==0) return TRUE;
41 }
42 */
43
44 stmp=(*s);
45 if ((*sgn=(*stmp=='-'))) {
46 stmp++;
47 if (!*stmp)
48 {
49 #ifdef CMPDBG
50 printf("is_int = FALSE\n");
51 #endif
52 return FALSE;
53 }
54 }
55 if (!*stmp)
56 {
57 #ifdef CMPDBG
58 printf("is_int = FALSE\n");
59 #endif
60 return FALSE; /* 6.10 */
61 }
62 while (*stmp=='0') stmp++;
63 sint=stmp;
64 while (*stmp) {
65 if (*stmp<'0' || *stmp>'9')
66 {
67 #ifdef CMPDBG
68 printf("is_int = FALSE\n");
69 #endif
70 return FALSE;
71 }
72 stmp++;
73 }
74 *len=stmp-sint;
75 *s=sint;
76 #ifdef CMPDBG
77 printf("is_int = TRUE *len = %ld *sgn = %ld *s = %s\n",*len,*sgn,*s);
78 #endif
79 return TRUE;
80 }
TRUE
#define TRUE
Definition: def_const.h:127
FALSE
#define FALSE
Definition: def_const.h:128

char* stack_copy_string | ( | char * | s | ) |

Definition at line 155 of file trees.c.

References one, stack_alloc(), and two.

157 {
158 char *p;
159
160 if (s==one || s==two) return s;
161
162 p=(char *)stack_alloc(strlen(s)+1);
163 strcpy(p,s);
164
165 return p;
166 }
two
char * two
Definition: def_glob.h:251
one
char * one
Definition: def_glob.h:250
stack_alloc
GENERIC stack_alloc(long s)
Definition: memory.c:1542

ptr_node stack_insert | ( | long | comp,
|  | char * | keystr,
|  | ptr_node * | tree,
|  | GENERIC | info
| ) |  |

Definition at line 291 of file trees.c.

References FALSE, general_insert(), and STACK.

296 {
297
298 return general_insert(comp,keystr,tree,info,STACK,FALSE,0L);
299 }
FALSE
#define FALSE
Definition: def_const.h:128
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
Definition: trees.c:184
STACK
#define STACK
Definition: def_const.h:148

void stack_insert_copystr | ( | char * | keystr,
|  | ptr_node * | tree,
|  | GENERIC | info
| ) |  |

Definition at line 260 of file trees.c.

References FEATCMP, general_insert(), STACK, and TRUE.

264 {
265
266 (void)general_insert(FEATCMP,keystr,tree,info,STACK,TRUE,0L);
267 }
FEATCMP
#define FEATCMP
Definition: def_const.h:257
TRUE
#define TRUE
Definition: def_const.h:127
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
Definition: trees.c:184
STACK
#define STACK
Definition: def_const.h:148