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