Date:2010-11-19 23:47:52 (13 years 4 months ago)
Author:Werner Almesberger
Commit:7f05c9e284659b171dabf24bab4ca1683c26ca48
Message:qpkg: remove Jval

- jrb.h (struct jrb_node): changed "key" and "val" from "Jval" to "void *"
- jrb.h, jval.c (jrb_insert_gen, jrb_find_gen, jrb_find_gte_gen, jrb_val):
replaced "Jval" with "void *" or "const void *", respectively
- rbtest.c (cmp, INSERT): updated for Jval removal
- rbtest.c (main): use jrb_val(jrb) instead of jval_v(jrb->val)
- Makefile (OBJS_rbtest): removed jval.o
- jval.h, jval.c: removed
Files: qpkg/Makefile (1 diff)
qpkg/jrb.c (5 diffs)
qpkg/jrb.h (4 diffs)
qpkg/jval.c (1 diff)
qpkg/jval.h (1 diff)
qpkg/rbtest.c (2 diffs)

Change Details

qpkg/Makefile
33#LDFLAGS=-pg
44
55OBJS = gobble.o id.o prereq.o qpkg.o
6OBJS_rbtest = rbtest.o jrb.o jval.o
6OBJS_rbtest = rbtest.o jrb.o
77
88all: qpkg rbtest
99
qpkg/jrb.c
5959#define isext(n) (!isint(n))
6060#define ishead(n) (n->roothead & 2)
6161#define isroot(n) (n->roothead & 1)
62#define getlext(n) ((struct jrb_node *)(n->key.v))
63#define setlext(node, val) node->key.v = (void *) (val)
64#define getrext(n) ((struct jrb_node *)(n->val.v))
65#define setrext(node, value) node->val.v = (void *) (value)
62#define getlext(n) ((struct jrb_node *)(n->key))
63#define setlext(node, val) node->key = (void *) (val)
64#define getrext(n) ((struct jrb_node *)(n->val))
65#define setrext(node, value) node->val = (void *) (value)
6666#define setred(n) n->red = 1
6767#define setblack(n) n->red = 0
6868#define setleft(n) n->left = 1
...... 
160160  head->flink = head;
161161  head->blink = head;
162162  head->parent = head;
163  head->key.s = "";
163  head->key = NULL;
164164  sethead(head);
165165  return head;
166166}
167167
168JRB jrb_find_gte_gen(JRB n, Jval key,int (*fxn)(Jval, Jval), int *fnd)
168JRB jrb_find_gte_gen(JRB n, const void *key,
169    int (*fxn)(const void *, const void *), int *fnd)
169170{
170171  int cmp;
171172
...... 
193194  }
194195}
195196
196JRB jrb_find_gen(JRB n, Jval key, int (*fxn)(Jval, Jval))
197JRB jrb_find_gen(JRB n, const void *key, int (*fxn)(const void *, const void *))
197198{
198199  int fnd;
199200  JRB j;
...... 
202203  if (fnd) return j; else return NULL;
203204}
204205
205static JRB jrb_insert_b(JRB n, Jval key, Jval val)
206static JRB jrb_insert_b(JRB n, void *key, void *val)
206207{
207208  JRB newleft, newright, newnode, p;
208209
...... 
488489  free(n);
489490}
490491
491Jval jrb_val(JRB n)
492void *jrb_val(JRB n)
492493{
493494  return n->val;
494495}
495496
496JRB jrb_insert_gen(JRB tree, Jval key, Jval val,
497                          int (*func)(Jval, Jval))
497JRB jrb_insert_gen(JRB tree, void *key, void *val,
498                          int (*func)(const void *, const void *))
498499{
499500  int fnd;
500501
501502  return jrb_insert_b(jrb_find_gte_gen(tree, key, func, &fnd), key, val);
502503}
503
504
qpkg/jrb.h
3737#ifndef _JRB_H_
3838#define _JRB_H_
3939
40#include "jval.h"
41
4240/* Main jrb_node. You only ever use the fields
4341   flink
4442   blink
...... 
5553  struct jrb_node *flink;
5654  struct jrb_node *blink;
5755  struct jrb_node *parent;
58  Jval key;
59  Jval val;
56  void *key;
57  void *val;
6058} *JRB;
6159
6260
...... 
6765   jrb_insert uses strcmp() as comparison funcion. jrb_inserti uses <>=,
6866   jrb_insertg uses func() */
6967
70extern JRB jrb_insert_gen(JRB tree, Jval key, Jval val, int (*func)(Jval,Jval));
68extern JRB jrb_insert_gen(JRB tree, void *key, void *val,
69    int (*func)(const void *, const void *));
7170
7271/* returns an external node in t whose value is equal k. Returns NULL if
7372   there is no such node in the tree */
7473
75extern JRB jrb_find_gen(JRB root, Jval, int (*func)(Jval, Jval));
76
74extern JRB jrb_find_gen(JRB root, const void *,
75    int (*func)(const void *, const void *));
7776
7877/* returns an external node in t whose value is equal
7978  k or whose value is the smallest value greater than k. Sets found to
8079  1 if the key was found, and 0 otherwise. */
8180
82extern JRB jrb_find_gte_gen(JRB root, Jval key,
83                              int (*func)(Jval, Jval), int *found);
81extern JRB jrb_find_gte_gen(JRB root, const void *key,
82    int (*func)(const void *, const void *), int *found);
8483
8584
8685/* Creates a node with key key and val val and inserts it into the
...... 
9190                                              not the key or val) */
9291extern void jrb_free_tree(JRB root); /* Deletes and frees an entire tree */
9392
94extern Jval jrb_val(JRB node); /* Returns node->v.val -- this is to shut
93extern void *jrb_val(JRB node); /* Returns node->v.val -- this is to shut
9594                                       lint up */
9695
9796extern int jrb_nblack(JRB n); /* returns # of black nodes in path from
qpkg/jval.c
1/*
2Libraries for fields, doubly-linked lists and red-black trees.
3Copyright (C) 2001 James S. Plank
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Lesser General Public
7License as published by the Free Software Foundation; either
8version 2.1 of the License, or (at your option) any later version.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public
16License along with this library; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19Please see http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Libfdr/
20for instruction on how to use this library.
21
22Jim Plank
23plank@cs.utk.edu
24http://www.cs.utk.edu/~plank
25
26Associate Professor
27Department of Computer Science
28University of Tennessee
29203 Claxton Complex
301122 Volunteer Blvd.
31Knoxville, TN 37996-3450
32
33     865-974-4397
34Fax: 865-974-4404
35 */
36#include <stdio.h>
37#include <string.h>
38#include "jval.h"
39
40Jval JNULL;
41
42Jval new_jval_i(int i) {
43  Jval j;
44  j.i = i;
45  return j;
46}
47
48Jval new_jval_l(long l) {
49  Jval j;
50  j.l = l;
51  return j;
52}
53
54Jval new_jval_f(float f) {
55  Jval j;
56  j.f = f;
57  return j;
58}
59
60Jval new_jval_d(double d) {
61  Jval j;
62  j.d = d;
63  return j;
64}
65
66Jval new_jval_v(void *v) {
67  Jval j;
68  j.v = v;
69  return j;
70}
71
72Jval new_jval_s(char *s) {
73  Jval j;
74  j.s = s;
75  return j;
76}
77
78Jval new_jval_c(char c) {
79  Jval j;
80  j.c = c;
81  return j;
82}
83
84Jval new_jval_uc(unsigned char uc) {
85  Jval j;
86  j.uc = uc;
87  return j;
88}
89
90Jval new_jval_sh(short sh) {
91  Jval j;
92  j.sh = sh;
93  return j;
94}
95
96Jval new_jval_ush(unsigned short ush) {
97  Jval j;
98  j.ush = ush;
99  return j;
100}
101
102Jval new_jval_ui(unsigned int i) {
103  Jval j;
104  j.i = i;
105  return j;
106}
107
108Jval new_jval_iarray(int i0, int i1) {
109  Jval j;
110  j.iarray[0] = i0;
111  j.iarray[1] = i1;
112  return j;
113}
114
115Jval new_jval_farray(float f0, float f1) {
116  Jval j;
117  j.farray[0] = f0;
118  j.farray[1] = f1;
119  return j;
120}
121
122Jval new_jval_carray_nt(char *carray) {
123  Jval j;
124  int i;
125
126  for (i = 0; i < 8 && carray[i] != '\0'; i++) {
127    j.carray[i] = carray[i];
128  }
129  if (i < 8) j.carray[i] = carray[i];
130  return j;
131}
132
133Jval new_jval_carray_nnt(char *carray) {
134  Jval j;
135
136  memcpy(j.carray, carray, 8);
137  return j;
138}
139
140int jval_i(Jval j) {
141  return j.i;
142}
143
144long jval_l(Jval j) {
145  return j.l;
146}
147
148float jval_f(Jval j) {
149  return j.f;
150}
151
152double jval_d(Jval j) {
153  return j.d;
154}
155
156void *jval_v(Jval j) {
157  return j.v;
158}
159
160char *jval_s(Jval j) {
161  return j.s;
162}
163
164char jval_c(Jval j) {
165  return j.c;
166}
167
168unsigned char jval_uc(Jval j) {
169  return j.uc;
170}
171
172short jval_sh(Jval j) {
173  return j.sh;
174}
175
176unsigned short jval_ush(Jval j) {
177  return j.ush;
178}
179
180unsigned int jval_ui(Jval j) {
181  return j.ui;
182}
183
184int *jval_iarray(Jval j) {
185  return j.iarray;
186}
187
188float *jval_farray(Jval j) {
189  return j.farray;
190}
191
192char *jval_carray(Jval j) {
193  return j.carray;
194}
195
qpkg/jval.h
1/*
2Libraries for fields, doubly-linked lists and red-black trees.
3Copyright (C) 2001 James S. Plank
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Lesser General Public
7License as published by the Free Software Foundation; either
8version 2.1 of the License, or (at your option) any later version.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public
16License along with this library; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19Please see http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Libfdr/
20for instruction on how to use this library.
21
22Jim Plank
23plank@cs.utk.edu
24http://www.cs.utk.edu/~plank
25
26Associate Professor
27Department of Computer Science
28University of Tennessee
29203 Claxton Complex
301122 Volunteer Blvd.
31Knoxville, TN 37996-3450
32
33     865-974-4397
34Fax: 865-974-4404
35 */
36#ifndef _JVAL_H_
37#define _JVAL_H_
38
39/* The Jval -- a type that can hold any 8-byte type */
40
41typedef union {
42    int i;
43    long l;
44    float f;
45    double d;
46    void *v;
47    char *s;
48    char c;
49    unsigned char uc;
50    short sh;
51    unsigned short ush;
52    unsigned int ui;
53    int iarray[2];
54    float farray[2];
55    char carray[8];
56    unsigned char ucarray[8];
57  } Jval;
58
59extern Jval new_jval_i(int);
60extern Jval new_jval_l(long);
61extern Jval new_jval_f(float);
62extern Jval new_jval_d(double);
63extern Jval new_jval_v(void *);
64extern Jval new_jval_s(char *);
65extern Jval new_jval_c(char);
66extern Jval new_jval_uc(unsigned char);
67extern Jval new_jval_sh(short);
68extern Jval new_jval_ush(unsigned short);
69extern Jval new_jval_ui(unsigned int);
70extern Jval new_jval_iarray(int, int);
71extern Jval new_jval_farray(float, float);
72extern Jval new_jval_carray_nt(char *); /* Carray is null terminated */
73extern Jval new_jval_carray_nnt(char *); /* Carray is not null terminated */
74       /* For ucarray -- use carray, because it uses memcpy */
75
76extern Jval JNULL;
77
78extern int jval_i(Jval);
79extern long jval_l(Jval);
80extern float jval_f(Jval);
81extern double jval_d(Jval);
82extern void *jval_v(Jval);
83extern char *jval_s(Jval);
84extern char jval_c(Jval);
85extern unsigned char jval_uc(Jval);
86extern short jval_sh(Jval);
87extern unsigned short jval_ush(Jval);
88extern unsigned int jval_ui(Jval);
89extern int *jval_iarray(Jval);
90extern float *jval_farray(Jval);
91extern char *jval_carray(Jval);
92
93#endif
qpkg/rbtest.c
55
66
77
8static int cmp(Jval a, Jval b)
8static int cmp(const void *a, const void *b)
99{
10    return strcmp(jval_v(a), jval_v(b));
10    return strcmp(a, b);
1111}
1212
1313
1414#define INSERT(key, val) \
15    jrb_insert_gen(tree, new_jval_v(key), new_jval_v(val), cmp)
15    jrb_insert_gen(tree, key, val, cmp)
1616
1717
1818int main(void)
...... 
3030    INSERT("ff", "!");
3131
3232    jrb_traverse(p, tree)
33        printf("%s ", (char *) jval_v(p->val));
33        printf("%s ", (char *) jrb_val(p));
3434    printf("\n");
3535
3636    return 0;

Archive Download the corresponding diff file

Branches:
master



interactive