Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:41

0001 #ifndef __OUC_TABLE__
0002 #define __OUC_TABLE__
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                        X r d O u c T a b l e . h h                         */
0006 /*                                                                            */
0007 /* (c) 2006 by the Board of Trustees of the Leland Stanford, Jr., University  */
0008 /*                            All Rights Reserved                             */
0009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
0010 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
0011 /*                                                                            */
0012 /* This file is part of the XRootD software suite.                            */
0013 /*                                                                            */
0014 /* XRootD is free software: you can redistribute it and/or modify it under    */
0015 /* the terms of the GNU Lesser General Public License as published by the     */
0016 /* Free Software Foundation, either version 3 of the License, or (at your     */
0017 /* option) any later version.                                                 */
0018 /*                                                                            */
0019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
0020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
0021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
0022 /* License for more details.                                                  */
0023 /*                                                                            */
0024 /* You should have received a copy of the GNU Lesser General Public License   */
0025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
0026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
0027 /*                                                                            */
0028 /* The copyright holder's institutional names and contributor's names may not */
0029 /* be used to endorse or promote products derived from this software without  */
0030 /* specific prior written permission of the institution or contributor.       */
0031 /******************************************************************************/
0032 
0033 #include <cstdlib>
0034 #include <cstring>
0035 
0036 template<class T>
0037 class XrdOucTable
0038 {
0039 public:
0040 
0041          XrdOucTable(int maxe)
0042                     {int i;
0043                      Table = new OucTable[(unsigned int)maxe];
0044                      maxnum = maxe; curnum = 0; avlnum = 0;
0045                      for (i = 1; i < maxe; i++) Table[i-1].Fnum = i;
0046                      Table[maxe-1].Fnum = -1;
0047                     }
0048 
0049         ~XrdOucTable() {delete [] Table;}
0050 
0051 // Alloc() returns the next free slot number in the table. A negative value
0052 //         indicates that no free slots are left.
0053 //
0054 int  Alloc() {int i = avlnum;
0055               if (i >= 0) {avlnum = Table[i].Fnum;
0056                            if (i >= curnum) curnum = i+1;
0057                           }
0058               return i;
0059              }
0060 
0061 // Apply() applies the specified function to every item in the list.
0062 //         An argument may be passed to the function. A null pointer is 
0063 //         returned if the list was completely traversed. Otherwise, the 
0064 //         pointer to the node on which the applied function returned a 
0065 //         non-zero value is returned. An optional starting point may be passed.
0066 //
0067 T       *Apply(int (*func)(T *, void *), void *Arg, int Start=0)
0068          {int i;
0069           for (i = Start; i < curnum; i++)
0070               if (Table[i].Item && (*func)(Table[i].Item, Arg))
0071                  return Table[i].Item;
0072           return (T *)0;
0073          }
0074 
0075 // Delete() entry at Tnum and destroy it. The key is destroyed and the slot
0076 // is placed on the free list. The second variation of Remove, deletes by key.
0077 //
0078 void Delete(int Tnum)
0079            {T *temp;
0080             if ((temp = Remove(Tnum))) delete temp;
0081            }
0082 
0083 void Delete(const char *key) 
0084            {T *temp;
0085             if ((temp = Remove(key))) delete temp;
0086            }
0087 
0088 // Find() finds a table entry matching the specified key. It returns the
0089 //        Item associated with the key or zero if it is not found. If the
0090 //        address of an integer is passed, the associated entry number is
0091 //        also returned (it is unchanged if a null is returned).
0092 //
0093 T       *Find(const char *key, int *Tnum=0)
0094          {int i;
0095           for (i = 0; i < curnum; i++)
0096               if (Table[i].Item && Table[i].Key && !strcmp(Table[i].Key, key))
0097                  {if (Tnum) *Tnum = i; return Table[i].Item;}
0098           return 0;
0099          }
0100 
0101 // Insert() inserts the specified node at entry Tnum. If Tnum is negative, a free
0102 //          slot is allocated and the item is inserted there. The slot number is
0103 //          returned. A negative slot number indicates the table is full.
0104 //
0105 int Insert(T *Item, const char *key=0, int Tnum=-1)
0106           {if ((Tnum < 0 && ((Tnum = Alloc()) < 0)) || Tnum >= maxnum) return -1;
0107            Table[Tnum].Item = Item; Table[Tnum].Key = strdup(key);
0108            return Tnum;
0109           }
0110 
0111 // Item() supplies the item value associated with entry Tnum; If the address
0112 //        if ikey is not zero, the associated key value is returned.
0113 //
0114 T  *Item(int Tnum, char **ikey=0) 
0115         {if (Tnum < 0 || Tnum >= curnum || !Table[Tnum].Item) return (T *)0;
0116          if (ikey) *ikey = Table[Tnum].Key;
0117          return Table[Tnum].Item;
0118         }
0119 
0120 // Next() iterates through the table using a cursor. This function is
0121 //        useful for unlocked scanning of the table.
0122 //
0123 int Next(int &Tnum) {int i;
0124                      for (i = Tnum; i < curnum; i++)
0125                          if (Table[i].Item) {Tnum = i+1; return i;}
0126                      return -1;
0127                     }
0128 
0129 // Remove() entry at Tnum and returns it. The key is destroyed and the slot
0130 // is placed on the free list. The second variation of Remove, removes by key.
0131 //
0132 T  *Remove(int Tnum)
0133           {T *temp;
0134            if (Tnum < 0 || Tnum >= curnum || !Table[Tnum].Item) return (T *)0;
0135            if (Table[Tnum].Key) free(Table[Tnum].Key);
0136            temp = Table[Tnum].Item; Table[Tnum].Item = 0;
0137            Table[Tnum].Fnum = avlnum;
0138            avlnum = Tnum;
0139            if (Tnum == (curnum-1))
0140               while(curnum && Table[curnum].Item == 0) curnum--;
0141            return temp;
0142           }
0143 
0144 T  *Remove(const char *key) {int i; 
0145                              if (Find(key, &i)) return Remove(i);
0146                              return (T *)0;
0147                             }
0148 
0149 private:
0150 struct OucTable {T           *Item;
0151                 union {char *Key;
0152                         int   Fnum;};
0153                  OucTable() {Item = 0; Key = 0;}
0154                 ~OucTable() {if (Item) {delete Item; if (Key) free(Key);}}
0155                 };
0156 
0157 OucTable *Table;
0158 int       avlnum;
0159 int       maxnum;
0160 int       curnum;
0161 };
0162 #endif