Wild Life: trees.c File Reference

Wild Life 2.30

- Main Page
- Data Structures
- Files

- File List
- Globals

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

trees More...

Go to the source code of this file.

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

## Detailed Description

trees

Definition in file trees.c.

## Function Documentation

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

bk2_stack_insert

Parameters

  comp | - long comp
  keystr | - char *keystr
  tree | - ptr_node *tree
  info | - GENERIC info

BK2_STACK_INSERT(comp,keystr,tree,info) Insert the pointer INFO under the reference string KEYSTR of length len in the binary tree TREE. Return the pointer to the permanent storage place of KEY. This is used by C_APPLY_LABEL Always trail the change.

Definition at line 377 of file trees.c.

References FALSE, general_insert(), and STACK.

378 {
379
380 return general_insert(comp,keystr,tree,info,STACK,FALSE,2L);
381 }
FALSE
#define FALSE
Standard boolean.
Definition: def_const.h:275
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
ptr_node general_insert
Definition: trees.c:224
STACK
#define STACK
Flag to indicate stack allocation.
Definition: def_const.h:331

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

bk_stack_insert

Parameters

  comp | - long comp
  keystr | - char *keystr
  tree | - ptr_node *tree
  info | - GENERIC info

BK_STACK_INSERT(comp,keystr,tree,info) Insert the pointer INFO under the reference string KEYSTR of length len in the binary tree TREE. Return the pointer to the permanent storage place of KEY. This is used by C_APPLY_LABEL Trail the change with a trail check.

Definition at line 357 of file trees.c.

References FALSE, general_insert(), and STACK.

358 {
359
360 return general_insert(comp,keystr,tree,info,STACK,FALSE,1L);
361 }
FALSE
#define FALSE
Standard boolean.
Definition: def_const.h:275
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
ptr_node general_insert
Definition: trees.c:224
STACK
#define STACK
Flag to indicate stack allocation.
Definition: def_const.h:331

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

delete_attr

Parameters

  s | - char *s
  n | - ptr_node *n

DELETE_ATTR(key,tree) Remove the node addressed by KEY from TREE.

Definition at line 522 of file trees.c.

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

523 {
524 long cmp;
525 ptr_node new,r;
526
527 if (*n) {
528 cmp=featcmp(s,(*n)->key);
529 if (cmp<0)
530 delete_attr(s,&((*n)->left));
531 else if (cmp>0)
532 delete_attr(s,&((*n)->right));
533 else if ((*n)->left) {
534 if ((*n)->right) {
535 r=(*n)->right;
536 new=heap_insert(FEATCMP,r->key,&((*n)->left),r->data);
537 new->left=r->left;
538 new->right=r->right;
539 *n = (*n) -> left;
540 }
541 else
542 *n = (*n)->left;
543 }
544 else
545 *n = (*n)->right;
546 }
547 }
FEATCMP
#define FEATCMP
indicates to use featcmp for comparison (in trees.c)
Definition: def_const.h:979
wl_node::data
GENERIC data
Definition: def_struct.h:201
wl_node::left
ptr_node left
Definition: def_struct.h:199
wl_node::key
char * key
Definition: def_struct.h:198
heap_insert
ptr_node heap_insert(long comp, char *keystr, ptr_node *tree, GENERIC info)
heap_insert
Definition: trees.c:320
featcmp
long featcmp(char *str1, char *str2)
featcmp
Definition: trees.c:106
wl_node
Definition: def_struct.h:197
delete_attr
void delete_attr(char *s, ptr_node *n)
delete_attr
Definition: trees.c:522
wl_node::right
ptr_node right
Definition: def_struct.h:200

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

featcmp

Parameters

  str1 | - char *str1
  str2 | - char *str2

FEATCMP(s1,s2) Compares two strings which represent features, for use in FIND or INSERT. This differs from strcmp for those strings that represent integers. These are compared as integers. In addition, all integers are considered to be less than all strings that do not represent integers.

Definition at line 106 of file trees.c.

References is_int().

107 {
108 long len1,len2,sgn1,sgn2;
109 char *s1,*s2;
110
111 if(str1==str2)
112 return 0;
113
114 /* if (*str1==0 && *str2==0) return 0; "" bug is unaffected -- PVR 23.2.94 */
115
116 if(*(str1+1)==0 && *(str2+1)==0)
117 return *str1 - *str2;
118
119
120 s1=str1; /* Local copies of the pointers */
121 s2=str2;
122
123 if (is_int(&s1,&len1,&sgn1)) {
124 if (is_int(&s2,&len2,&sgn2)) {
125 if (sgn1!=sgn2) return (sgn2-sgn1); /* Check signs first */
126 if (len1!=len2) return (len1-len2); /* Then check lengths */
127 return strcmp(s1,s2); /* Use strcmp only if same sign and length */
128 }
129 else
130 return -1;
131 }
132 else {
133 if (is_int(&s2,&len2,&sgn2))
134 return 1;
135 else
136 return strcmp(s1,s2);
137 }
138 }
is_int
long is_int(char **s, long *len, long *sgn)
is_int
Definition: trees.c:41

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

find

Parameters

  comp | - long comp
  keystr | - char *keystr
  tree | - ptr_node tree

FIND(comp,keystr,tree) Return the NODE address corresponding to key KEYSTR in TREE using function COMP to compare keys.

Definition at line 394 of file trees.c.

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

395 {
396 ptr_node result;
397 long cmp;
398 long to_do=TRUE;
399
400 /*
401 if(comp==strcmp)
402 printf("%s ",keystr);
403 */
404
405 do {
406 if (tree==NULL) {
407 result=NULL;
408 to_do=FALSE;
409 }
410 else {
411 if (comp == INTCMP)
412 cmp = intcmp((long)keystr,(long) (tree)->key);
413 else if (comp == FEATCMP)
414 cmp = featcmp(keystr,(tree)->key);
415 else if (comp == STRCMP)
416 cmp = strcmp(keystr,(tree)->key);
417 else
418 Errorline("Bad comp in general_insert.\n");
419
420 if (cmp<0)
421 tree=tree->left;
422 else
423 if (cmp==0) {
424 result=tree;
425 to_do=FALSE;
426 }
427 else
428 tree=tree->right;
429 }
430 } while (to_do);
431
432
433 /* RM: Jan 27 1993
434 if(comp==strcmp)
435 printf("Find: '%s' -> %x\n",keystr,result);
436 */
437
438 return result;
439 }
FEATCMP
#define FEATCMP
indicates to use featcmp for comparison (in trees.c)
Definition: def_const.h:979
INTCMP
#define INTCMP
indicates to use intcmp for comparison (in trees.c)
Definition: def_const.h:971
NULL
#define NULL
Definition: def_const.h:533
wl_node::left
ptr_node left
Definition: def_struct.h:199
Errorline
void Errorline(char *format,...)
Errorline.
Definition: error.c:465
TRUE
#define TRUE
Standard boolean.
Definition: def_const.h:268
STRCMP
#define STRCMP
indicates to use strcmp for comparison (c function)
Definition: def_const.h:963
FALSE
#define FALSE
Standard boolean.
Definition: def_const.h:275
intcmp
long intcmp(long a, long b)
intcmp
Definition: trees.c:21
featcmp
long featcmp(char *str1, char *str2)
featcmp
Definition: trees.c:106
wl_node
Definition: def_struct.h:197
wl_node::right
ptr_node right
Definition: def_struct.h:200

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

find_data

Parameters

  p | - GENERIC p
  t | - ptr_node t

FIND_DATA(p,t) Return the node containing the data P in tree T. This is a linear search and can be used to find the key to some data if it is unkown. Return NULL if no key corresponds to data P.

Definition at line 452 of file trees.c.

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

453 {
454 ptr_node r=NULL;
455
456 if(t)
457 if(t->data==p)
458 r=t;
459 else {
460 r=find_data(p,t->left);
461 if(r==NULL)
462 r=find_data(p,t->right);
463 }
464
465 return r;
466 }
find_data
ptr_node find_data(GENERIC p, ptr_node t)
find_data
Definition: trees.c:452
wl_node::data
GENERIC data
Definition: def_struct.h:201
NULL
#define NULL
Definition: def_const.h:533
wl_node::left
ptr_node left
Definition: def_struct.h:199
wl_node
Definition: def_struct.h:197
wl_node::right
ptr_node right
Definition: def_struct.h:200

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

ptr_node general_insert

Parameters

  comp | - long comp
  keystr | - char *keystr
  tree | - ptr_node *tree
  info | - GENERIC info
  heapflag | - long heapflag
  copystr | - long copystr
  bkflag | - long bkflag

GENERAL_INSERT(comp,keystr,tree,info,heapflag,copystr,bkflag) General tree insertion routine. comp = comparison routine for insertion. keystr = the insertion key. tree = the tree to insert in. info = the information to insert. heapflag = HEAP or STACK for heap or stack allocation of insertion node. copystr = TRUE iff copy the keystr to the heap on insertion. bkflag = 1 iff the insertion is backtrackable (trailed with trail check). 2 iff the insertion must always be trailed. Returns a pointer to the node containing the pair (keystr,info).

Here KEYSTR can be either a pointer to a string, an integer, or a feature. COMP is the function to call to compare 2 keys so it has three possible values: COMP==strcmp(), COMP==intcmp(), or COMP==featcmp(). COMP(a,b) should return n where: n=0 if a=b; n>0 if a>b; n<0 if a<b.

Definition at line 224 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.

225 {
226 long cmp;
227 ptr_node result;
228 long to_do=TRUE;
229
230
231 do {
232 if (*tree==NULL) {
233 if (bkflag==1) push_ptr_value(int_ptr,(GENERIC *)tree);
234 else if (bkflag==2) push_ptr_value_global(int_ptr,(GENERIC *)tree);
235 *tree = (heapflag==HEAP) ? HEAP_ALLOC(node): STACK_ALLOC(node);
236 result= *tree;
237 (*tree)->key = copystr ? heap_copy_string(keystr) : keystr;
238 (*tree)->left=NULL;
239 (*tree)->right=NULL;
240 (*tree)->data=info;
241 to_do=FALSE;
242 }
243 else {
244 if (comp == INTCMP)
245 cmp = intcmp((long)keystr,(long) (*tree)->key);
246 else if (comp == FEATCMP)
247 cmp = featcmp(keystr,(*tree)->key);
248 else if (comp == STRCMP)
249 cmp = strcmp(keystr,(*tree)->key);
250 else
251 Errorline("Bad comp in general_insert.\n");
252
253 if (cmp<0)
254 tree=(&((*tree)->left));
255 else
256 if (cmp==0) {
257 if (bkflag)
258 Errorline("attempt to overwrite an existing feature; ignored.\n");
259 else
260 (*tree)->data=info;
261 result= *tree;
262 to_do=FALSE;
263 }
264 else
265 tree=(&((*tree)->right));
266 }
267 } while (to_do);
268
269 return result;
270 }
push_ptr_value
void push_ptr_value(type_ptr t, GENERIC *p)
push_ptr_value
Definition: login.c:383
HEAP
#define HEAP
Flag to indicate heap allocation.
Definition: def_const.h:324
FEATCMP
#define FEATCMP
indicates to use featcmp for comparison (in trees.c)
Definition: def_const.h:979
INTCMP
#define INTCMP
indicates to use intcmp for comparison (in trees.c)
Definition: def_const.h:971
push_ptr_value_global
void push_ptr_value_global(type_ptr t, GENERIC *p)
push_ptr_value_global
Definition: login.c:523
heap_copy_string
char * heap_copy_string(char *s)
heap_copy_string
Definition: trees.c:172
NULL
#define NULL
Definition: def_const.h:533
Errorline
void Errorline(char *format,...)
Errorline.
Definition: error.c:465
GENERIC
unsigned long * GENERIC
unsigned long *GENERIC
Definition: def_struct.h:35
wl_node::key
char * key
Definition: def_struct.h:198
TRUE
#define TRUE
Standard boolean.
Definition: def_const.h:268
STRCMP
#define STRCMP
indicates to use strcmp for comparison (c function)
Definition: def_const.h:963
FALSE
#define FALSE
Standard boolean.
Definition: def_const.h:275
intcmp
long intcmp(long a, long b)
intcmp
Definition: trees.c:21
STACK_ALLOC
#define STACK_ALLOC(A)
Definition: def_macro.h:21
featcmp
long featcmp(char *str1, char *str2)
featcmp
Definition: trees.c:106
wl_node
Definition: def_struct.h:197
HEAP_ALLOC
#define HEAP_ALLOC(A)
Definition: def_macro.h:20
int_ptr
#define int_ptr
values of type_ptr
Definition: def_const.h:397

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

heap_copy_string

Parameters

  s | - char *s

HEAP_COPY_STRING(string) Make a copy of the string in the heap, and return a pointer to that. Exceptions: "1" and "2" are unique (and in the heap).

Definition at line 172 of file trees.c.

References heap_ncopy_string().

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

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

heap_insert

Parameters

  comp | - long comp
  keystr | - char *keystr
  tree | - ptr_node *tree
  info | - GENERIC info

HEAP_INSERT(comp,keystr,tree,info) Insert the pointer INFO under the reference KEYSTR in the binary tree TREE stored in the heap. Return the pointer to the node of KEYSTR.

Definition at line 320 of file trees.c.

References FALSE, general_insert(), and HEAP.

321 {
322
323 return general_insert(comp,keystr,tree,info,HEAP,FALSE,0L);
324 }
HEAP
#define HEAP
Flag to indicate heap allocation.
Definition: def_const.h:324
FALSE
#define FALSE
Standard boolean.
Definition: def_const.h:275
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
ptr_node general_insert
Definition: trees.c:224

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

heap_insert_copystr

Parameters

  keystr | - char *keystr
  tree | - ptr_node *tree
  info | - GENERIC info

HEAP_INSERT_COPYSTR(keystr,tree,info) Insert the pointer INFO under the reference string KEYSTR (which is a feature name) in the binary tree TREE. KEYSTR is copied to the heap. A potential additional node allocated to TREE is put on the heap.

Definition at line 284 of file trees.c.

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

285 {
286 (void)general_insert(FEATCMP,keystr,tree,info,HEAP,TRUE,0L);
287 }
HEAP
#define HEAP
Flag to indicate heap allocation.
Definition: def_const.h:324
FEATCMP
#define FEATCMP
indicates to use featcmp for comparison (in trees.c)
Definition: def_const.h:979
TRUE
#define TRUE
Standard boolean.
Definition: def_const.h:268
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
ptr_node general_insert
Definition: trees.c:224

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

heap_ncopy_string

Parameters

  s | - char *s
  n | - int n

HEAP_NCOPY_STRING(string,length) Make a copy of the string in the heap, and return a pointer to that. Exceptions: "1" and "2" are unique (and in the heap).

Definition at line 150 of file trees.c.

References heap_alloc(), one, and two.

151 {
152 char *p;
153
154 if (s==one || s==two) return s;
155
156 p=(char *)heap_alloc(n+1);
157 strncpy(p,s,n);
158 p[n]='\0';
159
160 return p;
161 }
two
char * two
Definition: def_glob.h:892
one
char * one
Definition: def_glob.h:891
heap_alloc
GENERIC heap_alloc(long s)
heap_alloc
Definition: memory.c:1616

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

intcmp

Parameters

  a | - long a
  b | - long b

INTCMP(a,b) Compares two integers, for use in FIND or INSERT.

Definition at line 21 of file trees.c.

22 {
23 #ifdef CMPDBG
24 printf("intcmp a = %ld b = %ld a - b = %ld\n", a ,b , a - b);
25 #endif
26 return a - b;
27 }

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

is_int

Parameters

  s | = char **s
  len | - long *len
  sgn | - long *sgn

Return TRUE iff the string s represents an integer. Modify s to point to first non-zero digit. Return number of significant digits in the integer and its sign.

Definition at line 41 of file trees.c.

References FALSE, and TRUE.

42 {
43 char *sint; /* Ptr to first non-zero digit */
44 char *stmp; /* Scratchpad for string ptr */
45 #ifdef CMPDBG
46 printf("is_int *s = %s\n",*s);
47 #endif
48 /*
49 { register char *p= *s;
50 register char c= *p;
51 if(c>'0' && c<='9' && *(p+1)==0) return TRUE;
52 }
53 */
54
55 stmp=(*s);
56 if ((*sgn=(*stmp=='-'))) {
57 stmp++;
58 if (!*stmp)
59 {
60 #ifdef CMPDBG
61 printf("is_int = FALSE\n");
62 #endif
63 return FALSE;
64 }
65 }
66 if (!*stmp)
67 {
68 #ifdef CMPDBG
69 printf("is_int = FALSE\n");
70 #endif
71 return FALSE; /* 6.10 */
72 }
73 while (*stmp=='0') stmp++;
74 sint=stmp;
75 while (*stmp) {
76 if (*stmp<'0' || *stmp>'9')
77 {
78 #ifdef CMPDBG
79 printf("is_int = FALSE\n");
80 #endif
81 return FALSE;
82 }
83 stmp++;
84 }
85 *len=stmp-sint;
86 *s=sint;
87 #ifdef CMPDBG
88 printf("is_int = TRUE *len = %ld *sgn = %ld *s = %s\n",*len,*sgn,*s);
89 #endif
90 return TRUE;
91 }
TRUE
#define TRUE
Standard boolean.
Definition: def_const.h:268
FALSE
#define FALSE
Standard boolean.
Definition: def_const.h:275

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

stack_copy_string

Parameters

  s | - char *s

STACK_COPY_STRING(string) Make a copy of the string in the stack, and return a pointer to that. Exceptions: "1" and "2" are unique (and in the heap).

Definition at line 184 of file trees.c.

References one, stack_alloc(), and two.

185 {
186 char *p;
187
188 if (s==one || s==two) return s;
189
190 p=(char *)stack_alloc(strlen(s)+1);
191 strcpy(p,s);
192
193 return p;
194 }
two
char * two
Definition: def_glob.h:892
one
char * one
Definition: def_glob.h:891
stack_alloc
GENERIC stack_alloc(long s)
stack_alloc
Definition: memory.c:1642

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

stack_insert

Parameters

  comp | - long comp
  keystr | - char *keystr
  tree | - ptr_node *tree
  info | - GENERIC info

STACK_INSERT(comp,keystr,tree,info) Exactly the same as heap_insert, only the new node is in the stack.

Definition at line 337 of file trees.c.

References FALSE, general_insert(), and STACK.

338 {
339
340 return general_insert(comp,keystr,tree,info,STACK,FALSE,0L);
341 }
FALSE
#define FALSE
Standard boolean.
Definition: def_const.h:275
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
ptr_node general_insert
Definition: trees.c:224
STACK
#define STACK
Flag to indicate stack allocation.
Definition: def_const.h:331

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

stack_insert_copystr

Parameters

  keystr | - char *keystr
  tree | - ptr_node *tree
  info | - GENERIC info

STACK_INSERT_COPYSTR(keystr,tree,info) Insert the pointer INFO under the reference string KEYSTR (which is a feature name) in the binary tree TREE. KEYSTR is copied to the heap. A potential additional node allocated to TREE is put on the stack.

Definition at line 301 of file trees.c.

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

302 {
303
304 (void)general_insert(FEATCMP,keystr,tree,info,STACK,TRUE,0L);
305 }
FEATCMP
#define FEATCMP
indicates to use featcmp for comparison (in trees.c)
Definition: def_const.h:979
TRUE
#define TRUE
Standard boolean.
Definition: def_const.h:268
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
ptr_node general_insert
Definition: trees.c:224
STACK
#define STACK
Flag to indicate stack allocation.
Definition: def_const.h:331