xrootd
Loading...
Searching...
No Matches
XrdOucTable.hh
Go to the documentation of this file.
1#ifndef __OUC_TABLE__
2#define __OUC_TABLE__
3/******************************************************************************/
4/* */
5/* X r d O u c T a b l e . h h */
6/* */
7/* (c) 2006 by the Board of Trustees of the Leland Stanford, Jr., University */
8/* All Rights Reserved */
9/* Produced by Andrew Hanushevsky for Stanford University under contract */
10/* DE-AC02-76-SFO0515 with the Department of Energy */
11/* */
12/* This file is part of the XRootD software suite. */
13/* */
14/* XRootD is free software: you can redistribute it and/or modify it under */
15/* the terms of the GNU Lesser General Public License as published by the */
16/* Free Software Foundation, either version 3 of the License, or (at your */
17/* option) any later version. */
18/* */
19/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22/* License for more details. */
23/* */
24/* You should have received a copy of the GNU Lesser General Public License */
25/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27/* */
28/* The copyright holder's institutional names and contributor's names may not */
29/* be used to endorse or promote products derived from this software without */
30/* specific prior written permission of the institution or contributor. */
31/******************************************************************************/
32
33#include <cstdlib>
34#include <cstring>
35
36template<class T>
38{
39public:
40
41 XrdOucTable(int maxe)
42 {int i;
43 Table = new OucTable[(unsigned int)maxe];
44 maxnum = maxe; curnum = 0; avlnum = 0;
45 for (i = 1; i < maxe; i++) Table[i-1].Fnum = i;
46 Table[maxe-1].Fnum = -1;
47 }
48
49 ~XrdOucTable() {delete [] Table;}
50
51// Alloc() returns the next free slot number in the table. A negative value
52// indicates that no free slots are left.
53//
54int Alloc() {int i = avlnum;
55 if (i >= 0) {avlnum = Table[i].Fnum;
56 if (i >= curnum) curnum = i+1;
57 }
58 return i;
59 }
60
61// Apply() applies the specified function to every item in the list.
62// An argument may be passed to the function. A null pointer is
63// returned if the list was completely traversed. Otherwise, the
64// pointer to the node on which the applied function returned a
65// non-zero value is returned. An optional starting point may be passed.
66//
67T *Apply(int (*func)(T *, void *), void *Arg, int Start=0)
68 {int i;
69 for (i = Start; i < curnum; i++)
70 if (Table[i].Item && (*func)(Table[i].Item, Arg))
71 return Table[i].Item;
72 return (T *)0;
73 }
74
75// Delete() entry at Tnum and destroy it. The key is destroyed and the slot
76// is placed on the free list. The second variation of Remove, deletes by key.
77//
78void Delete(int Tnum)
79 {T *temp;
80 if ((temp = Remove(Tnum))) delete temp;
81 }
82
83void Delete(const char *key)
84 {T *temp;
85 if ((temp = Remove(key))) delete temp;
86 }
87
88// Find() finds a table entry matching the specified key. It returns the
89// Item associated with the key or zero if it is not found. If the
90// address of an integer is passed, the associated entry number is
91// also returned (it is unchanged if a null is returned).
92//
93T *Find(const char *key, int *Tnum=0)
94 {int i;
95 for (i = 0; i < curnum; i++)
96 if (Table[i].Item && Table[i].Key && !strcmp(Table[i].Key, key))
97 {if (Tnum) *Tnum = i; return Table[i].Item;}
98 return 0;
99 }
100
101// Insert() inserts the specified node at entry Tnum. If Tnum is negative, a free
102// slot is allocated and the item is inserted there. The slot number is
103// returned. A negative slot number indicates the table is full.
104//
105int Insert(T *Item, const char *key=0, int Tnum=-1)
106 {if ((Tnum < 0 && ((Tnum = Alloc()) < 0)) || Tnum >= maxnum) return -1;
107 Table[Tnum].Item = Item; Table[Tnum].Key = strdup(key);
108 return Tnum;
109 }
110
111// Item() supplies the item value associated with entry Tnum; If the address
112// if ikey is not zero, the associated key value is returned.
113//
114T *Item(int Tnum, char **ikey=0)
115 {if (Tnum < 0 || Tnum >= curnum || !Table[Tnum].Item) return (T *)0;
116 if (ikey) *ikey = Table[Tnum].Key;
117 return Table[Tnum].Item;
118 }
119
120// Next() iterates through the table using a cursor. This function is
121// useful for unlocked scanning of the table.
122//
123int Next(int &Tnum) {int i;
124 for (i = Tnum; i < curnum; i++)
125 if (Table[i].Item) {Tnum = i+1; return i;}
126 return -1;
127 }
128
129// Remove() entry at Tnum and returns it. The key is destroyed and the slot
130// is placed on the free list. The second variation of Remove, removes by key.
131//
132T *Remove(int Tnum)
133 {T *temp;
134 if (Tnum < 0 || Tnum >= curnum || !Table[Tnum].Item) return (T *)0;
135 if (Table[Tnum].Key) free(Table[Tnum].Key);
136 temp = Table[Tnum].Item; Table[Tnum].Item = 0;
137 Table[Tnum].Fnum = avlnum;
138 avlnum = Tnum;
139 if (Tnum == (curnum-1))
140 while(curnum && Table[curnum].Item == 0) curnum--;
141 return temp;
142 }
143
144T *Remove(const char *key) {int i;
145 if (Find(key, &i)) return Remove(i);
146 return (T *)0;
147 }
148
149private:
150struct OucTable {T *Item;
151 union {char *Key;
152 int Fnum;};
153 OucTable() {Item = 0; Key = 0;}
154 ~OucTable() {if (Item) {delete Item; if (Key) free(Key);}}
155 };
156
161};
162#endif
Definition XrdOucTable.hh:38
OucTable * Table
Definition XrdOucTable.hh:157
int curnum
Definition XrdOucTable.hh:160
XrdOucTable(int maxe)
Definition XrdOucTable.hh:41
T * Apply(int(*func)(T *, void *), void *Arg, int Start=0)
Definition XrdOucTable.hh:67
int Alloc()
Definition XrdOucTable.hh:54
~XrdOucTable()
Definition XrdOucTable.hh:49
T * Remove(const char *key)
Definition XrdOucTable.hh:144
T * Remove(int Tnum)
Definition XrdOucTable.hh:132
void Delete(const char *key)
Definition XrdOucTable.hh:83
int maxnum
Definition XrdOucTable.hh:159
T * Item(int Tnum, char **ikey=0)
Definition XrdOucTable.hh:114
int Insert(T *Item, const char *key=0, int Tnum=-1)
Definition XrdOucTable.hh:105
T * Find(const char *key, int *Tnum=0)
Definition XrdOucTable.hh:93
void Delete(int Tnum)
Definition XrdOucTable.hh:78
int avlnum
Definition XrdOucTable.hh:158
int Next(int &Tnum)
Definition XrdOucTable.hh:123
Definition XrdOucTable.hh:150
OucTable()
Definition XrdOucTable.hh:153
char * Key
Definition XrdOucTable.hh:151
int Fnum
Definition XrdOucTable.hh:152
T * Item
Definition XrdOucTable.hh:150
~OucTable()
Definition XrdOucTable.hh:154