1
0
mirror of git://projects.qi-hardware.com/wernermisc.git synced 2024-11-22 01:10:36 +02:00

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
This commit is contained in:
Werner Almesberger 2010-11-19 19:47:52 -03:00
parent 769d31581d
commit 7f05c9e284
6 changed files with 26 additions and 318 deletions

View File

@ -3,7 +3,7 @@ CFLAGS = -Wall -Wshadow -g -O
#LDFLAGS=-pg
OBJS = gobble.o id.o prereq.o qpkg.o
OBJS_rbtest = rbtest.o jrb.o jval.o
OBJS_rbtest = rbtest.o jrb.o
all: qpkg rbtest

View File

@ -59,10 +59,10 @@ static void single_rotate(JRB y, int l);
#define isext(n) (!isint(n))
#define ishead(n) (n->roothead & 2)
#define isroot(n) (n->roothead & 1)
#define getlext(n) ((struct jrb_node *)(n->key.v))
#define setlext(node, val) node->key.v = (void *) (val)
#define getrext(n) ((struct jrb_node *)(n->val.v))
#define setrext(node, value) node->val.v = (void *) (value)
#define getlext(n) ((struct jrb_node *)(n->key))
#define setlext(node, val) node->key = (void *) (val)
#define getrext(n) ((struct jrb_node *)(n->val))
#define setrext(node, value) node->val = (void *) (value)
#define setred(n) n->red = 1
#define setblack(n) n->red = 0
#define setleft(n) n->left = 1
@ -160,12 +160,13 @@ JRB make_jrb(void)
head->flink = head;
head->blink = head;
head->parent = head;
head->key.s = "";
head->key = NULL;
sethead(head);
return head;
}
JRB jrb_find_gte_gen(JRB n, Jval key,int (*fxn)(Jval, Jval), int *fnd)
JRB jrb_find_gte_gen(JRB n, const void *key,
int (*fxn)(const void *, const void *), int *fnd)
{
int cmp;
@ -193,7 +194,7 @@ JRB jrb_find_gte_gen(JRB n, Jval key,int (*fxn)(Jval, Jval), int *fnd)
}
}
JRB jrb_find_gen(JRB n, Jval key, int (*fxn)(Jval, Jval))
JRB jrb_find_gen(JRB n, const void *key, int (*fxn)(const void *, const void *))
{
int fnd;
JRB j;
@ -202,7 +203,7 @@ JRB jrb_find_gen(JRB n, Jval key, int (*fxn)(Jval, Jval))
if (fnd) return j; else return NULL;
}
static JRB jrb_insert_b(JRB n, Jval key, Jval val)
static JRB jrb_insert_b(JRB n, void *key, void *val)
{
JRB newleft, newright, newnode, p;
@ -488,17 +489,15 @@ void jrb_free_tree(JRB n)
free(n);
}
Jval jrb_val(JRB n)
void *jrb_val(JRB n)
{
return n->val;
}
JRB jrb_insert_gen(JRB tree, Jval key, Jval val,
int (*func)(Jval, Jval))
JRB jrb_insert_gen(JRB tree, void *key, void *val,
int (*func)(const void *, const void *))
{
int fnd;
return jrb_insert_b(jrb_find_gte_gen(tree, key, func, &fnd), key, val);
}

View File

@ -37,8 +37,6 @@ Fax: 865-974-4404
#ifndef _JRB_H_
#define _JRB_H_
#include "jval.h"
/* Main jrb_node. You only ever use the fields
flink
blink
@ -55,8 +53,8 @@ typedef struct jrb_node {
struct jrb_node *flink;
struct jrb_node *blink;
struct jrb_node *parent;
Jval key;
Jval val;
void *key;
void *val;
} *JRB;
@ -67,20 +65,21 @@ extern JRB make_jrb(void); /* Creates a new rb-tree */
jrb_insert uses strcmp() as comparison funcion. jrb_inserti uses <>=,
jrb_insertg uses func() */
extern JRB jrb_insert_gen(JRB tree, Jval key, Jval val, int (*func)(Jval,Jval));
extern JRB jrb_insert_gen(JRB tree, void *key, void *val,
int (*func)(const void *, const void *));
/* returns an external node in t whose value is equal k. Returns NULL if
there is no such node in the tree */
extern JRB jrb_find_gen(JRB root, Jval, int (*func)(Jval, Jval));
extern JRB jrb_find_gen(JRB root, const void *,
int (*func)(const void *, const void *));
/* returns an external node in t whose value is equal
k or whose value is the smallest value greater than k. Sets found to
1 if the key was found, and 0 otherwise. */
extern JRB jrb_find_gte_gen(JRB root, Jval key,
int (*func)(Jval, Jval), int *found);
extern JRB jrb_find_gte_gen(JRB root, const void *key,
int (*func)(const void *, const void *), int *found);
/* Creates a node with key key and val val and inserts it into the
@ -91,7 +90,7 @@ extern void jrb_delete_node(JRB node); /* Deletes and frees a node (but
not the key or val) */
extern void jrb_free_tree(JRB root); /* Deletes and frees an entire tree */
extern Jval jrb_val(JRB node); /* Returns node->v.val -- this is to shut
extern void *jrb_val(JRB node); /* Returns node->v.val -- this is to shut
lint up */
extern int jrb_nblack(JRB n); /* returns # of black nodes in path from

View File

@ -1,196 +0,0 @@
/*
Libraries for fields, doubly-linked lists and red-black trees.
Copyright (C) 2001 James S. Plank
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
---------------------------------------------------------------------------
Please see http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Libfdr/
for instruction on how to use this library.
Jim Plank
plank@cs.utk.edu
http://www.cs.utk.edu/~plank
Associate Professor
Department of Computer Science
University of Tennessee
203 Claxton Complex
1122 Volunteer Blvd.
Knoxville, TN 37996-3450
865-974-4397
Fax: 865-974-4404
*/
#include <stdio.h>
#include <string.h>
#include "jval.h"
Jval JNULL;
Jval new_jval_i(int i) {
Jval j;
j.i = i;
return j;
}
Jval new_jval_l(long l) {
Jval j;
j.l = l;
return j;
}
Jval new_jval_f(float f) {
Jval j;
j.f = f;
return j;
}
Jval new_jval_d(double d) {
Jval j;
j.d = d;
return j;
}
Jval new_jval_v(void *v) {
Jval j;
j.v = v;
return j;
}
Jval new_jval_s(char *s) {
Jval j;
j.s = s;
return j;
}
Jval new_jval_c(char c) {
Jval j;
j.c = c;
return j;
}
Jval new_jval_uc(unsigned char uc) {
Jval j;
j.uc = uc;
return j;
}
Jval new_jval_sh(short sh) {
Jval j;
j.sh = sh;
return j;
}
Jval new_jval_ush(unsigned short ush) {
Jval j;
j.ush = ush;
return j;
}
Jval new_jval_ui(unsigned int i) {
Jval j;
j.i = i;
return j;
}
Jval new_jval_iarray(int i0, int i1) {
Jval j;
j.iarray[0] = i0;
j.iarray[1] = i1;
return j;
}
Jval new_jval_farray(float f0, float f1) {
Jval j;
j.farray[0] = f0;
j.farray[1] = f1;
return j;
}
Jval new_jval_carray_nt(char *carray) {
Jval j;
int i;
for (i = 0; i < 8 && carray[i] != '\0'; i++) {
j.carray[i] = carray[i];
}
if (i < 8) j.carray[i] = carray[i];
return j;
}
Jval new_jval_carray_nnt(char *carray) {
Jval j;
memcpy(j.carray, carray, 8);
return j;
}
int jval_i(Jval j) {
return j.i;
}
long jval_l(Jval j) {
return j.l;
}
float jval_f(Jval j) {
return j.f;
}
double jval_d(Jval j) {
return j.d;
}
void *jval_v(Jval j) {
return j.v;
}
char *jval_s(Jval j) {
return j.s;
}
char jval_c(Jval j) {
return j.c;
}
unsigned char jval_uc(Jval j) {
return j.uc;
}
short jval_sh(Jval j) {
return j.sh;
}
unsigned short jval_ush(Jval j) {
return j.ush;
}
unsigned int jval_ui(Jval j) {
return j.ui;
}
int *jval_iarray(Jval j) {
return j.iarray;
}
float *jval_farray(Jval j) {
return j.farray;
}
char *jval_carray(Jval j) {
return j.carray;
}

View File

@ -1,94 +0,0 @@
/*
Libraries for fields, doubly-linked lists and red-black trees.
Copyright (C) 2001 James S. Plank
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
---------------------------------------------------------------------------
Please see http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Libfdr/
for instruction on how to use this library.
Jim Plank
plank@cs.utk.edu
http://www.cs.utk.edu/~plank
Associate Professor
Department of Computer Science
University of Tennessee
203 Claxton Complex
1122 Volunteer Blvd.
Knoxville, TN 37996-3450
865-974-4397
Fax: 865-974-4404
*/
#ifndef _JVAL_H_
#define _JVAL_H_
/* The Jval -- a type that can hold any 8-byte type */
typedef union {
int i;
long l;
float f;
double d;
void *v;
char *s;
char c;
unsigned char uc;
short sh;
unsigned short ush;
unsigned int ui;
int iarray[2];
float farray[2];
char carray[8];
unsigned char ucarray[8];
} Jval;
extern Jval new_jval_i(int);
extern Jval new_jval_l(long);
extern Jval new_jval_f(float);
extern Jval new_jval_d(double);
extern Jval new_jval_v(void *);
extern Jval new_jval_s(char *);
extern Jval new_jval_c(char);
extern Jval new_jval_uc(unsigned char);
extern Jval new_jval_sh(short);
extern Jval new_jval_ush(unsigned short);
extern Jval new_jval_ui(unsigned int);
extern Jval new_jval_iarray(int, int);
extern Jval new_jval_farray(float, float);
extern Jval new_jval_carray_nt(char *); /* Carray is null terminated */
extern Jval new_jval_carray_nnt(char *); /* Carray is not null terminated */
/* For ucarray -- use carray, because it uses memcpy */
extern Jval JNULL;
extern int jval_i(Jval);
extern long jval_l(Jval);
extern float jval_f(Jval);
extern double jval_d(Jval);
extern void *jval_v(Jval);
extern char *jval_s(Jval);
extern char jval_c(Jval);
extern unsigned char jval_uc(Jval);
extern short jval_sh(Jval);
extern unsigned short jval_ush(Jval);
extern unsigned int jval_ui(Jval);
extern int *jval_iarray(Jval);
extern float *jval_farray(Jval);
extern char *jval_carray(Jval);
#endif

View File

@ -5,14 +5,14 @@
static int cmp(Jval a, Jval b)
static int cmp(const void *a, const void *b)
{
return strcmp(jval_v(a), jval_v(b));
return strcmp(a, b);
}
#define INSERT(key, val) \
jrb_insert_gen(tree, new_jval_v(key), new_jval_v(val), cmp)
jrb_insert_gen(tree, key, val, cmp)
int main(void)
@ -30,7 +30,7 @@ int main(void)
INSERT("ff", "!");
jrb_traverse(p, tree)
printf("%s ", (char *) jval_v(p->val));
printf("%s ", (char *) jrb_val(p));
printf("\n");
return 0;