|
xrootd
|
00001 #ifndef __OUC_TABLE__ 00002 #define __OUC_TABLE__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d O u c T a b l e . h h */ 00006 /* */ 00007 /* (c) 2006 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved */ 00009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00011 /* */ 00012 /* This file is part of the XRootD software suite. */ 00013 /* */ 00014 /* XRootD is free software: you can redistribute it and/or modify it under */ 00015 /* the terms of the GNU Lesser General Public License as published by the */ 00016 /* Free Software Foundation, either version 3 of the License, or (at your */ 00017 /* option) any later version. */ 00018 /* */ 00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00022 /* License for more details. */ 00023 /* */ 00024 /* You should have received a copy of the GNU Lesser General Public License */ 00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00027 /* */ 00028 /* The copyright holder's institutional names and contributor's names may not */ 00029 /* be used to endorse or promote products derived from this software without */ 00030 /* specific prior written permission of the institution or contributor. */ 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 // Alloc() returns the next free slot number in the table. A negative value 00052 // indicates that no free slots are left. 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 // Apply() applies the specified function to every item in the list. 00062 // An argument may be passed to the function. A null pointer is 00063 // returned if the list was completely traversed. Otherwise, the 00064 // pointer to the node on which the applied function returned a 00065 // non-zero value is returned. An optional starting point may be passed. 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 // Delete() entry at Tnum and destroy it. The key is destroyed and the slot 00076 // is placed on the free list. The second variation of Remove, deletes by key. 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 // Find() finds a table entry matching the specified key. It returns the 00089 // Item associated with the key or zero if it is not found. If the 00090 // address of an integer is passed, the associated entry number is 00091 // also returned (it is unchanged if a null is returned). 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 // Insert() inserts the specified node at entry Tnum. If Tnum is negative, a free 00102 // slot is allocated and the item is inserted there. The slot number is 00103 // returned. A negative slot number indicates the table is full. 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 // Item() supplies the item value associated with entry Tnum; If the address 00112 // if ikey is not zero, the associated key value is returned. 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 // Next() iterates through the table using a cursor. This function is 00121 // useful for unlocked scanning of the table. 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 // Remove() entry at Tnum and returns it. The key is destroyed and the slot 00130 // is placed on the free list. The second variation of Remove, removes by key. 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
1.7.6.1