00001 #ifndef __OUC_TABLE__
00002 #define __OUC_TABLE__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdlib.h>
00034 #include <string.h>
00035
00036 template<class T>
00037 class XrdOucTable
00038 {
00039 public:
00040
00041 XrdOucTable(int maxe)
00042 {int i;
00043 Table = new OucTable[maxe];
00044 maxnum = maxe; curnum = 0; avlnum = 0;
00045 for (i = 1; i < maxe; i++) Table[i-1].Fnum = i;
00046 Table[maxe-1].Fnum = -1;
00047 }
00048
00049 ~XrdOucTable() {delete [] Table;}
00050
00051
00052
00053
00054 int Alloc() {int i = avlnum;
00055 if (i >= 0) {avlnum = Table[i].Fnum;
00056 if (i >= curnum) curnum = i+1;
00057 }
00058 return i;
00059 }
00060
00061
00062
00063
00064
00065
00066
00067 T *Apply(int (*func)(T *, void *), void *Arg, int Start=0)
00068 {int i;
00069 for (i = Start; i < curnum; i++)
00070 if (Table[i].Item && (*func)(Table[i].Item, Arg))
00071 return Table[i].Item;
00072 return (T *)0;
00073 }
00074
00075
00076
00077
00078 void Delete(int Tnum)
00079 {T *temp;
00080 if ((temp = Remove(Tnum))) delete temp;
00081 }
00082
00083 void Delete(const char *key)
00084 {T *temp;
00085 if ((temp = Remove(key))) delete temp;
00086 }
00087
00088
00089
00090
00091
00092
00093 T *Find(const char *key, int *Tnum=0)
00094 {int i;
00095 for (i = 0; i < curnum; i++)
00096 if (Table[i].Item && Table[i].Key && !strcmp(Table[i].Key, key))
00097 {if (Tnum) *Tnum = i; return Table[i].Item;}
00098 return 0;
00099 }
00100
00101
00102
00103
00104
00105 int Insert(T *Item, const char *key=0, int Tnum=-1)
00106 {if ((Tnum < 0 && ((Tnum = Alloc()) < 0)) || Tnum >= maxnum) return -1;
00107 Table[Tnum].Item = Item; Table[Tnum].Key = strdup(key);
00108 return Tnum;
00109 }
00110
00111
00112
00113
00114 T *Item(int Tnum, char **ikey=0)
00115 {if (Tnum < 0 || Tnum >= curnum || !Table[Tnum].Item) return (T *)0;
00116 if (ikey) *ikey = Table[Tnum].Key;
00117 return Table[Tnum].Item;
00118 }
00119
00120
00121
00122
00123 int Next(int &Tnum) {int i;
00124 for (i = Tnum; i < curnum; i++)
00125 if (Table[i].Item) {Tnum = i+1; return i;}
00126 return -1;
00127 }
00128
00129
00130
00131
00132 T *Remove(int Tnum)
00133 {T *temp;
00134 if (Tnum < 0 || Tnum >= curnum || !Table[Tnum].Item) return (T *)0;
00135 if (Table[Tnum].Key) free(Table[Tnum].Key);
00136 temp = Table[Tnum].Item; Table[Tnum].Item = 0;
00137 Table[Tnum].Fnum = avlnum;
00138 avlnum = Tnum;
00139 if (Tnum == (curnum-1))
00140 while(curnum && Table[curnum].Item == 0) curnum--;
00141 return temp;
00142 }
00143
00144 T *Remove(const char *key) {int i;
00145 if (Find(key, &i)) return Remove(i);
00146 return (T *)0;
00147 }
00148
00149 private:
00150 struct OucTable {T *Item;
00151 union {char *Key;
00152 int Fnum;};
00153 OucTable() {Item = 0; Key = 0;}
00154 ~OucTable() {if (Key) free(Key);
00155 if (Item) delete Item;
00156 }
00157 };
00158
00159 OucTable *Table;
00160 int avlnum;
00161 int maxnum;
00162 int curnum;
00163 };
00164 #endif