Werner's Miscellanea
Sign in or create your account | Project List | Help
Werner's Miscellanea Commit Details
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 | ||
---|---|---|
3 | 3 | #LDFLAGS=-pg |
4 | 4 | |
5 | 5 | OBJS = gobble.o id.o prereq.o qpkg.o |
6 | OBJS_rbtest = rbtest.o jrb.o jval.o | |
6 | OBJS_rbtest = rbtest.o jrb.o | |
7 | 7 | |
8 | 8 | all: qpkg rbtest |
9 | 9 |
qpkg/jrb.c | ||
---|---|---|
59 | 59 | #define isext(n) (!isint(n)) |
60 | 60 | #define ishead(n) (n->roothead & 2) |
61 | 61 | #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) | |
66 | 66 | #define setred(n) n->red = 1 |
67 | 67 | #define setblack(n) n->red = 0 |
68 | 68 | #define setleft(n) n->left = 1 |
... | ... | |
160 | 160 | head->flink = head; |
161 | 161 | head->blink = head; |
162 | 162 | head->parent = head; |
163 | head->key.s = ""; | |
163 | head->key = NULL; | |
164 | 164 | sethead(head); |
165 | 165 | return head; |
166 | 166 | } |
167 | 167 | |
168 | JRB jrb_find_gte_gen(JRB n, Jval key,int (*fxn)(Jval, Jval), int *fnd) | |
168 | JRB jrb_find_gte_gen(JRB n, const void *key, | |
169 | int (*fxn)(const void *, const void *), int *fnd) | |
169 | 170 | { |
170 | 171 | int cmp; |
171 | 172 | |
... | ... | |
193 | 194 | } |
194 | 195 | } |
195 | 196 | |
196 | JRB jrb_find_gen(JRB n, Jval key, int (*fxn)(Jval, Jval)) | |
197 | JRB jrb_find_gen(JRB n, const void *key, int (*fxn)(const void *, const void *)) | |
197 | 198 | { |
198 | 199 | int fnd; |
199 | 200 | JRB j; |
... | ... | |
202 | 203 | if (fnd) return j; else return NULL; |
203 | 204 | } |
204 | 205 | |
205 | static JRB jrb_insert_b(JRB n, Jval key, Jval val) | |
206 | static JRB jrb_insert_b(JRB n, void *key, void *val) | |
206 | 207 | { |
207 | 208 | JRB newleft, newright, newnode, p; |
208 | 209 | |
... | ... | |
488 | 489 | free(n); |
489 | 490 | } |
490 | 491 | |
491 | Jval jrb_val(JRB n) | |
492 | void *jrb_val(JRB n) | |
492 | 493 | { |
493 | 494 | return n->val; |
494 | 495 | } |
495 | 496 | |
496 | JRB jrb_insert_gen(JRB tree, Jval key, Jval val, | |
497 | int (*func)(Jval, Jval)) | |
497 | JRB jrb_insert_gen(JRB tree, void *key, void *val, | |
498 | int (*func)(const void *, const void *)) | |
498 | 499 | { |
499 | 500 | int fnd; |
500 | 501 | |
501 | 502 | return jrb_insert_b(jrb_find_gte_gen(tree, key, func, &fnd), key, val); |
502 | 503 | } |
503 | ||
504 |
qpkg/jrb.h | ||
---|---|---|
37 | 37 | #ifndef _JRB_H_ |
38 | 38 | #define _JRB_H_ |
39 | 39 | |
40 | #include "jval.h" | |
41 | ||
42 | 40 | /* Main jrb_node. You only ever use the fields |
43 | 41 | flink |
44 | 42 | blink |
... | ... | |
55 | 53 | struct jrb_node *flink; |
56 | 54 | struct jrb_node *blink; |
57 | 55 | struct jrb_node *parent; |
58 | Jval key; | |
59 | Jval val; | |
56 | void *key; | |
57 | void *val; | |
60 | 58 | } *JRB; |
61 | 59 | |
62 | 60 | |
... | ... | |
67 | 65 | jrb_insert uses strcmp() as comparison funcion. jrb_inserti uses <>=, |
68 | 66 | jrb_insertg uses func() */ |
69 | 67 | |
70 | extern JRB jrb_insert_gen(JRB tree, Jval key, Jval val, int (*func)(Jval,Jval)); | |
68 | extern JRB jrb_insert_gen(JRB tree, void *key, void *val, | |
69 | int (*func)(const void *, const void *)); | |
71 | 70 | |
72 | 71 | /* returns an external node in t whose value is equal k. Returns NULL if |
73 | 72 | there is no such node in the tree */ |
74 | 73 | |
75 | extern JRB jrb_find_gen(JRB root, Jval, int (*func)(Jval, Jval)); | |
76 | ||
74 | extern JRB jrb_find_gen(JRB root, const void *, | |
75 | int (*func)(const void *, const void *)); | |
77 | 76 | |
78 | 77 | /* returns an external node in t whose value is equal |
79 | 78 | k or whose value is the smallest value greater than k. Sets found to |
80 | 79 | 1 if the key was found, and 0 otherwise. */ |
81 | 80 | |
82 | extern JRB jrb_find_gte_gen(JRB root, Jval key, | |
83 | int (*func)(Jval, Jval), int *found); | |
81 | extern JRB jrb_find_gte_gen(JRB root, const void *key, | |
82 | int (*func)(const void *, const void *), int *found); | |
84 | 83 | |
85 | 84 | |
86 | 85 | /* Creates a node with key key and val val and inserts it into the |
... | ... | |
91 | 90 | not the key or val) */ |
92 | 91 | extern void jrb_free_tree(JRB root); /* Deletes and frees an entire tree */ |
93 | 92 | |
94 | extern Jval jrb_val(JRB node); /* Returns node->v.val -- this is to shut | |
93 | extern void *jrb_val(JRB node); /* Returns node->v.val -- this is to shut | |
95 | 94 | lint up */ |
96 | 95 | |
97 | 96 | extern int jrb_nblack(JRB n); /* returns # of black nodes in path from |
qpkg/jval.c | ||
---|---|---|
1 | /* | |
2 | Libraries for fields, doubly-linked lists and red-black trees. | |
3 | Copyright (C) 2001 James S. Plank | |
4 | ||
5 | This library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | This library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with this library; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | ||
19 | Please see http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Libfdr/ | |
20 | for instruction on how to use this library. | |
21 | ||
22 | Jim Plank | |
23 | plank@cs.utk.edu | |
24 | http://www.cs.utk.edu/~plank | |
25 | ||
26 | Associate Professor | |
27 | Department of Computer Science | |
28 | University of Tennessee | |
29 | 203 Claxton Complex | |
30 | 1122 Volunteer Blvd. | |
31 | Knoxville, TN 37996-3450 | |
32 | ||
33 | 865-974-4397 | |
34 | Fax: 865-974-4404 | |
35 | */ | |
36 | #include <stdio.h> | |
37 | #include <string.h> | |
38 | #include "jval.h" | |
39 | ||
40 | Jval JNULL; | |
41 | ||
42 | Jval new_jval_i(int i) { | |
43 | Jval j; | |
44 | j.i = i; | |
45 | return j; | |
46 | } | |
47 | ||
48 | Jval new_jval_l(long l) { | |
49 | Jval j; | |
50 | j.l = l; | |
51 | return j; | |
52 | } | |
53 | ||
54 | Jval new_jval_f(float f) { | |
55 | Jval j; | |
56 | j.f = f; | |
57 | return j; | |
58 | } | |
59 | ||
60 | Jval new_jval_d(double d) { | |
61 | Jval j; | |
62 | j.d = d; | |
63 | return j; | |
64 | } | |
65 | ||
66 | Jval new_jval_v(void *v) { | |
67 | Jval j; | |
68 | j.v = v; | |
69 | return j; | |
70 | } | |
71 | ||
72 | Jval new_jval_s(char *s) { | |
73 | Jval j; | |
74 | j.s = s; | |
75 | return j; | |
76 | } | |
77 | ||
78 | Jval new_jval_c(char c) { | |
79 | Jval j; | |
80 | j.c = c; | |
81 | return j; | |
82 | } | |
83 | ||
84 | Jval new_jval_uc(unsigned char uc) { | |
85 | Jval j; | |
86 | j.uc = uc; | |
87 | return j; | |
88 | } | |
89 | ||
90 | Jval new_jval_sh(short sh) { | |
91 | Jval j; | |
92 | j.sh = sh; | |
93 | return j; | |
94 | } | |
95 | ||
96 | Jval new_jval_ush(unsigned short ush) { | |
97 | Jval j; | |
98 | j.ush = ush; | |
99 | return j; | |
100 | } | |
101 | ||
102 | Jval new_jval_ui(unsigned int i) { | |
103 | Jval j; | |
104 | j.i = i; | |
105 | return j; | |
106 | } | |
107 | ||
108 | Jval 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 | ||
115 | Jval 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 | ||
122 | Jval 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 | ||
133 | Jval new_jval_carray_nnt(char *carray) { | |
134 | Jval j; | |
135 | ||
136 | memcpy(j.carray, carray, 8); | |
137 | return j; | |
138 | } | |
139 | ||
140 | int jval_i(Jval j) { | |
141 | return j.i; | |
142 | } | |
143 | ||
144 | long jval_l(Jval j) { | |
145 | return j.l; | |
146 | } | |
147 | ||
148 | float jval_f(Jval j) { | |
149 | return j.f; | |
150 | } | |
151 | ||
152 | double jval_d(Jval j) { | |
153 | return j.d; | |
154 | } | |
155 | ||
156 | void *jval_v(Jval j) { | |
157 | return j.v; | |
158 | } | |
159 | ||
160 | char *jval_s(Jval j) { | |
161 | return j.s; | |
162 | } | |
163 | ||
164 | char jval_c(Jval j) { | |
165 | return j.c; | |
166 | } | |
167 | ||
168 | unsigned char jval_uc(Jval j) { | |
169 | return j.uc; | |
170 | } | |
171 | ||
172 | short jval_sh(Jval j) { | |
173 | return j.sh; | |
174 | } | |
175 | ||
176 | unsigned short jval_ush(Jval j) { | |
177 | return j.ush; | |
178 | } | |
179 | ||
180 | unsigned int jval_ui(Jval j) { | |
181 | return j.ui; | |
182 | } | |
183 | ||
184 | int *jval_iarray(Jval j) { | |
185 | return j.iarray; | |
186 | } | |
187 | ||
188 | float *jval_farray(Jval j) { | |
189 | return j.farray; | |
190 | } | |
191 | ||
192 | char *jval_carray(Jval j) { | |
193 | return j.carray; | |
194 | } | |
195 |
qpkg/jval.h | ||
---|---|---|
1 | /* | |
2 | Libraries for fields, doubly-linked lists and red-black trees. | |
3 | Copyright (C) 2001 James S. Plank | |
4 | ||
5 | This library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | This library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with this library; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | ||
19 | Please see http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Libfdr/ | |
20 | for instruction on how to use this library. | |
21 | ||
22 | Jim Plank | |
23 | plank@cs.utk.edu | |
24 | http://www.cs.utk.edu/~plank | |
25 | ||
26 | Associate Professor | |
27 | Department of Computer Science | |
28 | University of Tennessee | |
29 | 203 Claxton Complex | |
30 | 1122 Volunteer Blvd. | |
31 | Knoxville, TN 37996-3450 | |
32 | ||
33 | 865-974-4397 | |
34 | Fax: 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 | ||
41 | typedef 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 | ||
59 | extern Jval new_jval_i(int); | |
60 | extern Jval new_jval_l(long); | |
61 | extern Jval new_jval_f(float); | |
62 | extern Jval new_jval_d(double); | |
63 | extern Jval new_jval_v(void *); | |
64 | extern Jval new_jval_s(char *); | |
65 | extern Jval new_jval_c(char); | |
66 | extern Jval new_jval_uc(unsigned char); | |
67 | extern Jval new_jval_sh(short); | |
68 | extern Jval new_jval_ush(unsigned short); | |
69 | extern Jval new_jval_ui(unsigned int); | |
70 | extern Jval new_jval_iarray(int, int); | |
71 | extern Jval new_jval_farray(float, float); | |
72 | extern Jval new_jval_carray_nt(char *); /* Carray is null terminated */ | |
73 | extern Jval new_jval_carray_nnt(char *); /* Carray is not null terminated */ | |
74 | /* For ucarray -- use carray, because it uses memcpy */ | |
75 | ||
76 | extern Jval JNULL; | |
77 | ||
78 | extern int jval_i(Jval); | |
79 | extern long jval_l(Jval); | |
80 | extern float jval_f(Jval); | |
81 | extern double jval_d(Jval); | |
82 | extern void *jval_v(Jval); | |
83 | extern char *jval_s(Jval); | |
84 | extern char jval_c(Jval); | |
85 | extern unsigned char jval_uc(Jval); | |
86 | extern short jval_sh(Jval); | |
87 | extern unsigned short jval_ush(Jval); | |
88 | extern unsigned int jval_ui(Jval); | |
89 | extern int *jval_iarray(Jval); | |
90 | extern float *jval_farray(Jval); | |
91 | extern char *jval_carray(Jval); | |
92 | ||
93 | #endif |
qpkg/rbtest.c | ||
---|---|---|
5 | 5 | |
6 | 6 | |
7 | 7 | |
8 | static int cmp(Jval a, Jval b) | |
8 | static int cmp(const void *a, const void *b) | |
9 | 9 | { |
10 | return strcmp(jval_v(a), jval_v(b)); | |
10 | return strcmp(a, b); | |
11 | 11 | } |
12 | 12 | |
13 | 13 | |
14 | 14 | #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) | |
16 | 16 | |
17 | 17 | |
18 | 18 | int main(void) |
... | ... | |
30 | 30 | INSERT("ff", "!"); |
31 | 31 | |
32 | 32 | jrb_traverse(p, tree) |
33 | printf("%s ", (char *) jval_v(p->val)); | |
33 | printf("%s ", (char *) jrb_val(p)); | |
34 | 34 | printf("\n"); |
35 | 35 | |
36 | 36 | return 0; |
Branches:
master