jrb.h

Go to the documentation of this file.
00001 /*
00002 Libraries for fields, doubly-linked lists and red-black trees.
00003 Copyright (C) 2001 James S. Plank
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 ---------------------------------------------------------------------------
00020 Please see http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Libfdr/
00021 for instruction on how to use this library.
00022 
00023 Jim Plank
00024 plank@cs.utk.edu
00025 http://www.cs.utk.edu/~plank
00026 
00027 Associate Professor
00028 Department of Computer Science
00029 University of Tennessee
00030 203 Claxton Complex
00031 1122 Volunteer Blvd.
00032 Knoxville, TN 37996-3450
00033 
00034      865-974-4397
00035 Fax: 865-974-4404
00036  */
00037 #ifndef _JRB_H_
00038 #define _JRB_H_
00039 
00040 #include "jval.h"
00041 
00042 /* Main jrb_node.  You only ever use the fields
00043    flink
00044    blink
00045    k.key or k.ikey
00046    v.val
00047 */
00048 
00049 
00050 typedef struct jrb_node {
00051   unsigned char red;
00052   unsigned char internal;
00053   unsigned char left;
00054   unsigned char roothead;  /* (bit 1 is root, bit 2 is head) */
00055   struct jrb_node *flink;
00056   struct jrb_node *blink;
00057   struct jrb_node *parent;
00058   Jval key;
00059   Jval val;
00060 } *JRB;
00061 
00062 
00063 extern JRB make_jrb();   /* Creates a new rb-tree */
00064 
00065 
00066 /* Creates a node with key key and val val and inserts it into the tree.
00067    jrb_insert uses strcmp() as comparison funcion.  jrb_inserti uses <>=,
00068    jrb_insertg uses func() */
00069 
00070 extern JRB jrb_insert_str(JRB tree, char *key, Jval val);
00071 extern JRB jrb_insert_int(JRB tree, int ikey, Jval val);
00072 extern JRB jrb_insert_dbl(JRB tree, double dkey, Jval val);
00073 extern JRB jrb_insert_gen(JRB tree, Jval key, Jval val, int (*func)(Jval,Jval));
00074 
00075 /* returns an external node in t whose value is equal k. Returns NULL if
00076    there is no such node in the tree */
00077 
00078 extern JRB jrb_find_str(JRB root, char *key);
00079 extern JRB jrb_find_int(JRB root, int ikey);
00080 extern JRB jrb_find_dbl(JRB root, double dkey);
00081 extern JRB jrb_find_gen(JRB root, Jval, int (*func)(Jval, Jval));
00082 
00083 
00084 /* returns an external node in t whose value is equal
00085   k or whose value is the smallest value greater than k. Sets found to
00086   1 if the key was found, and 0 otherwise.  */
00087 
00088 extern JRB jrb_find_gte_str(JRB root, char *key, int *found);
00089 extern JRB jrb_find_gte_int(JRB root, int ikey, int *found);
00090 extern JRB jrb_find_gte_dbl(JRB root, double dkey, int *found);
00091 extern JRB jrb_find_gte_gen(JRB root, Jval key, 
00092                               int (*func)(Jval, Jval), int *found);
00093 
00094 
00095 /* Creates a node with key key and val val and inserts it into the 
00096    tree before/after node nd.  Does not check to ensure that you are 
00097    keeping the correct order */
00098 
00099 extern void jrb_delete_node(JRB node);  /* Deletes and frees a node (but 
00100                                               not the key or val) */
00101 extern void jrb_free_tree(JRB root);  /* Deletes and frees an entire tree */
00102 
00103 extern Jval jrb_val(JRB node);  /* Returns node->v.val -- this is to shut
00104                                        lint up */
00105 
00106 extern int jrb_nblack(JRB n); /* returns # of black nodes in path from
00107                                     n to the root */
00108 int jrb_plength(JRB n);       /* returns the # of nodes in path from
00109                                     n to the root */
00110  
00111 #define jrb_first(n) (n->flink)
00112 #define jrb_last(n) (n->blink)
00113 #define jrb_next(n) (n->flink)
00114 #define jrb_prev(n) (n->blink)
00115 #define jrb_empty(t) (t->flink == t)
00116 #ifndef jrb_nil
00117 #define jrb_nil(t) (t)
00118 #endif
00119  
00120 #define jrb_traverse(ptr, lst) \
00121   for(ptr = jrb_first(lst); ptr != jrb_nil(lst); ptr = jrb_next(ptr))
00122  
00123 #define jrb_rtraverse(ptr, lst) \
00124   for(ptr = jrb_last(lst); ptr != jrb_nil(lst); ptr = jrb_prev(ptr))
00125  
00126 #endif

Generated on Sat Nov 12 16:41:16 2005 for Chimera by  doxygen 1.4.5