00001
00002
00003
00004
00005 #ifndef dsBintree_h
00006 #define dsBintree_h
00007
00008 #ifdef HAVE_CONFIG_H
00009 # include <config.h>
00010 #endif
00011
00012 #include <dsSmartException.h>
00013 #include <dsDataAccessor.h>
00014
00015 DECLARE_EXCEPTION2(dsBinTree, dsDataAccessor);
00016
00017
00018 class dsBinTreeItem : dsDataAccessorItem
00019 {
00020 dsBinTreeItem *left, *right;
00021
00022 public:
00023 dsBinTreeItem():
00024 dsDataAccessorItem(){ left = 0; right =0; }
00025
00026 dsBinTreeItem(void *xval):
00027 dsDataAccessorItem(xval){ left = 0; right = 0; }
00028
00029 friend class dsBinTreeGeneric;
00030 };
00031
00032
00046 class dsBinTreeGeneric : public dsDataAccessor
00047 {
00048 protected:
00049
00053 dsBinTreeItem* _root;
00054
00059 void cleanup(dsBinTreeItem* n = NULL, int first = 1);
00060
00064 virtual int cf(void * a, void *b) = 0;
00065
00069 virtual void visit(void *){};
00070
00071
00072 public:
00073
00078 dsBinTreeGeneric(DACleanupMode purge = cmPURGE);
00079
00083 virtual ~dsBinTreeGeneric(){ cleanup(); }
00084
00091 void *insert(void * a, bool replace = false);
00092
00099 void *remove(void * a);
00100
00107 void* seek(void *a);
00108
00112 void walk(dsBinTreeItem* n = NULL, int first = 1);
00113
00114 };
00115
00116 class dsBinTree : public dsBinTreeGeneric
00117 {
00118 protected:
00119 virtual int cf(void * a, void *b) { return strcmp(key(a), key(b)); }
00120 const char *key(void *a) { return (((dsDataPair *)a)->key); }
00121
00122 public:
00123 dsBinTree(DACleanupMode purge = cmPURGE):
00124 dsBinTreeGeneric(purge){}
00125
00126
00127 dsDataPair *seek(dsDataPair *dp);
00128 char * seek(const char *key);
00129
00130 dsDataPair *insert(dsDataPair *dp, bool replace);
00131 char *insert(const char *key, char * value, bool replace = false);
00132 };
00133
00134 inline dsDataPair * dsBinTree::seek(dsDataPair *dp)
00135 {
00136 return (dsDataPair *) dsBinTreeGeneric::seek( (void *) dp );
00137 }
00138
00139 inline dsDataPair *dsBinTree::insert(dsDataPair *dp, bool replace)
00140 {
00141 return (dsDataPair *) dsBinTreeGeneric::insert((void *)dp, replace);
00142 }
00143
00144 inline char * dsBinTree::seek(const char *key)
00145 {
00146 dsDataPair dp(key);
00147 dsDataPair *tmp = seek(&dp);
00148 return (tmp) ? (char *) tmp->val : 0;
00149 }
00150
00151 inline char *dsBinTree::insert(const char *key, char * value, bool replace)
00152 {
00153 dsDataPair *dp = new dsDataPair(key, value);
00154 dsDataPair *tmp = insert(dp, replace);
00155 if (tmp && !replace)
00156 delete dp;
00157 return (char *)tmp->val;
00158 }
00159
00160
00161 #endif