Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef __OUC_RASH__
0002 #define __OUC_RASH__
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                         X r d O u c R a s h . h h                          */
0006 /*                                                                            */
0007 /* (c) 2005 by the Board of Trustees of the Leland Stanford, Jr., University  */
0008 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
0009 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
0010 /*                                                                            */
0011 /* This file is part of the XRootD software suite.                            */
0012 /*                                                                            */
0013 /* XRootD is free software: you can redistribute it and/or modify it under    */
0014 /* the terms of the GNU Lesser General Public License as published by the     */
0015 /* Free Software Foundation, either version 3 of the License, or (at your     */
0016 /* option) any later version.                                                 */
0017 /*                                                                            */
0018 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
0019 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
0020 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
0021 /* License for more details.                                                  */
0022 /*                                                                            */
0023 /* You should have received a copy of the GNU Lesser General Public License   */
0024 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
0025 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
0026 /*                                                                            */
0027 /* The copyright holder's institutional names and contributor's names may not */
0028 /* be used to endorse or promote products derived from this software without  */
0029 /* specific prior written permission of the institution or contributor.       */
0030 /******************************************************************************/
0031 
0032 // This templated class implements a radix tree to remap binary quantities using
0033 // a hash-like <key,Value> interface. Define the object as:
0034 
0035 // XrdOucRash<key_type, value_type> myobject;
0036 
0037 // Where: key_type   is the binary type of the key (short, int, long, long long)
0038 //        value_type is the binary type of the value (one of the types above).
0039 
0040 // The binary types may be signed or unsigned. Use the methods defined in
0041 // class XrdOucRash to Add(), Del(), Find(), and Rep() items in the table.
0042 // Use Apply() to scan through all of the items in the table and Purge() to
0043 // remove all items in the table (indices are not removed). Several options
0044 // exist to manage the items (see individual methods and XrdOucRash_Options).
0045 
0046 // Warning! This class is not MT-safe and should be protected by an external
0047 //          mutex when used in a multi-threaded environment.
0048  
0049 #include <sys/types.h>
0050 #include <ctime>
0051 
0052 enum XrdOucRash_Options {Rash_default     = 0x0000,
0053                          Rash_replace     = 0x0002,
0054                          Rash_count       = 0x0004
0055                        };
0056 
0057 template<typename K, typename V>
0058 class XrdOucRash_Item
0059 {
0060 public:
0061 int                  Count() {return keycount;}
0062 
0063 V                   *Data() {return &keydata;}
0064 
0065 K                    Key()  {return keyval;}
0066 
0067 time_t               Time() {return keytime;}
0068 
0069 void                 Update(int newcount, time_t newtime)
0070                             {keycount = newcount; 
0071                              if (newtime) keytime = newtime;
0072                             }
0073 
0074 void                 Set(V &keyData, time_t newtime)
0075                             {keydata = keyData;
0076                              keytime = newtime;
0077                             }
0078 
0079      XrdOucRash_Item(K                  &KeyVal,
0080                      V                  &KeyData,
0081                      time_t             KeyTime)
0082           {keyval  = KeyVal;
0083            keydata = KeyData;
0084            keytime = KeyTime;
0085            keycount= 0;
0086           }
0087 
0088     ~XrdOucRash_Item() {}
0089 
0090 private:
0091 
0092 K                  keyval;
0093 V                  keydata;
0094 time_t             keytime;
0095 int                keycount;
0096 };
0097 
0098 template<typename K, typename V>
0099 class XrdOucRash_Tent
0100 {
0101 public:
0102 XrdOucRash_Tent<K,V> *Table;
0103 XrdOucRash_Item<K,V> *Item;
0104 
0105       XrdOucRash_Tent() {Table = 0; Item = 0;}
0106      ~XrdOucRash_Tent() {if (Table) delete[] Table;
0107                          if (Item)  delete(Item);
0108                          }
0109 };
0110 
0111 template<typename K, typename V>
0112 class XrdOucRash
0113 {
0114 public:
0115 
0116 // Add() adds a new item to the table. If it exists and repl = 0 then the old
0117 //       entry is returned and the new data is not added. Otherwise the current
0118 //       entry is replaced (see Rep()) and 0 is returned. If we have no memory
0119 //       to add the new entry, an ENOMEM exception is thrown. The
0120 //       LifeTime value is the number of seconds this entry is to be considered
0121 //       valid. When the time has passed, the entry may be deleted. A value
0122 //       of zero, keeps the entry until explicitly deleted. The Hash_count 
0123 //       option keeps track of duplicate key entries for Del. Thus the key must
0124 //       be deleted as many times as it is added before it is physically deleted.
0125 //
0126 V           *Add(K KeyVal, V &KeyData, time_t LifeTime=0,
0127                  XrdOucRash_Options opt=Rash_default);
0128 
0129 // Del() deletes the item from the table. If it doesn't exist, it returns
0130 //       -ENOENT. If it was deleted it returns 0. If it was created with
0131 //       Rash_Count then the count is decremented and count+1 is returned.
0132 //
0133 int          Del(K KeyVal);
0134 
0135 // Find() simply looks up an entry in the cache. It can optionally return the
0136 //        lifetime associated with the entry. If the
0137 //
0138 V           *Find(K KeyVal, time_t *KeyTime=0);
0139 
0140 // Num() returns the number of items in the table
0141 //
0142 int          Num() {return rashnum;}
0143 
0144 // Purge() simply deletes all of the appendages to the table.
0145 //
0146 void         Purge();
0147 
0148 // Rep() is simply Add() that allows replacement.
0149 //
0150 V           *Rep(K KeyVal, V &KeyData, const int LifeTime=0,
0151                  XrdOucRash_Options opt=Rash_default)
0152                 {return Add(KeyVal, KeyData, LifeTime, 
0153                            (XrdOucRash_Options)(opt | Rash_replace));}
0154 
0155 // Apply() applies the specified function to every item in the table. The
0156 //         first argument is the key value, the second is the associated data,
0157 //         the third argument is whatever is the passed in void *variable, The
0158 //         following actions occur for values returned by the applied function:
0159 //         <0 - The table item is deleted.
0160 //         =0 - The next table item is processed.
0161 //         >0 - Processing stops and the address of item is returned.
0162 //
0163 V           *Apply(int (*func)(K, V, void *), void *Arg)
0164                   {return Apply(rashTable, func, Arg);}
0165 
0166     XrdOucRash() {rashnum = 0;}
0167    ~XrdOucRash() {Purge();}
0168 
0169 private:
0170 V                    *Apply(XrdOucRash_Tent<K,V> *tab,
0171                             int (*func)(K, V, void *), void *Arg);
0172 XrdOucRash_Item<K,V> *Lookup(K theKey, XrdOucRash_Tent<K,V> **tloc);
0173 void                  Insert(K theKey, XrdOucRash_Item<K,V> *theItem);
0174 unsigned long long    key2ull(K theKey);
0175 
0176 XrdOucRash_Tent<K,V> rashTable[16];
0177 int                  rashnum;
0178 };
0179 
0180 /******************************************************************************/
0181 /*                 A c t u a l   I m p l e m e n t a t i o n                  */
0182 /******************************************************************************/
0183   
0184 #include "XrdOuc/XrdOucRash.icc"
0185 #endif