00001 // Copyright (C) 2001, Compaq Computer Corporation 00002 // 00003 // This file is part of Vesta. 00004 // 00005 // Vesta is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU Lesser General Public 00007 // License as published by the Free Software Foundation; either 00008 // version 2.1 of the License, or (at your option) any later version. 00009 // 00010 // Vesta is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public 00016 // License along with Vesta; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 // Last modified on Wed Aug 4 18:08:12 EDT 2004 by ken@xorian.net 00020 // modified on Thu Sep 25 18:06:01 PDT 1997 by heydon 00021 00022 /* IntIntTblLR.H -- Like a generic "IntIntTbl", defines a table that maps 00023 non-negative shorts to short, but these tables also have methods to 00024 write/read them from logs and streams. */ 00025 00026 #ifndef _INT_INT_TBL_LR_H 00027 #define _INT_INT_TBL_LR_H 00028 00029 #include <Basics.H> 00030 #include <FS.H> 00031 #include <VestaLog.H> 00032 #include <Recovery.H> 00033 00034 class IntIntTblLR { 00035 public: 00036 // constructors 00037 IntIntTblLR(int sizeHint = 0) throw (); 00038 IntIntTblLR(const IntIntTblLR *tbl) throw (); 00039 IntIntTblLR(RecoveryReader &rd) throw (VestaLog::Error, VestaLog::Eof) 00040 { Recover(rd); } 00041 IntIntTblLR(std::istream &ifs) throw (FS::EndOfFile, FS::Failure) 00042 { Read(ifs); } 00043 ~IntIntTblLR() throw () 00044 { this->elt = (short *)NULL; } 00045 00046 // table operations 00047 bool Get(short k, /*OUT*/ short& v) const throw (); 00048 /* If the key "k" exists in the table, then set "v" to the 00049 associated value and return "true". Otherwise, leave "v" 00050 unchanged and return "false". */ 00051 00052 bool Put(short k, short v) throw (); 00053 /* Set the value associated with key "k" in the table to "v". Return 00054 "true" iff there was already a value associated with "k" in the 00055 table before the method was invoked. */ 00056 00057 bool Delete(short k, /*OUT*/ short& v) throw (); 00058 /* If the key "k" exists in the table, set "v" to its associated 00059 value, remove the association from the table, and return "true". 00060 Otherwise, leave "v" unchanged and return "false". */ 00061 00062 int Size() const throw () { return this->numEntries; } 00063 /* Return the number of <k,v> associations in the table. */ 00064 00065 int ArraySize() const throw () { return this->eltLen; } 00066 /* Return the size of the underlying array. */ 00067 00068 // log/recover 00069 void Log(VestaLog& log) const throw (VestaLog::Error); 00070 /* Append a representation of this table to "log". Requires "log" to 00071 be in the "logging" state. */ 00072 void Recover(RecoveryReader &rd) 00073 throw (VestaLog::Error, VestaLog::Eof); 00074 /* Recover the representation of this table from "rd". */ 00075 00076 // write/read 00077 void Write(std::ostream &ofs) const throw (FS::Failure); 00078 /* Write this table to "ofs". */ 00079 void Read(std::istream &ifs) throw (FS::EndOfFile, FS::Failure); 00080 /* Read this table from "ifs". */ 00081 00082 // print 00083 void Print(std::ostream &os, int indent) const throw (); 00084 00085 private: 00086 int numEntries; // number of elements in "elt" 00087 int eltLen; // size of "elt" array 00088 short *elt; // array of elements; may be NULL if "eltLen == 0" 00089 00090 friend class IntIntTblIter; 00091 }; 00092 00093 class IntIntTblIter { 00094 public: 00095 IntIntTblIter(const IntIntTblLR* tbl) throw () 00096 : tbl(tbl) { Reset(); } 00097 /* Return a new iterator on the "Default" table "tbl". While the 00098 iterator is in use, the client is only allowed to modify the table 00099 by invoking the "Put" and "Delete" methods with "resize = false". 00100 Once the iteration is complete, it is recommended that the "Resize" 00101 method be called to adjust the size of the table if necessary. */ 00102 00103 void Reset() throw () { this->k = 0; this->done = false; } 00104 /* Resets the iterator so that it will iterate over all the elements 00105 in its associated table. This can be used to iterate over the 00106 elements of the same table multiple times. */ 00107 00108 bool Next(/*OUT*/ short& k, /*OUT*/ short& v) throw (); 00109 /* If there is an association in this iterator's table that has not 00110 already been enumerated, select one, set "k" to its key, set "v" to 00111 its value, and return "true". Otherwise, leave "k" and "v" 00112 unchanged, and return "false". */ 00113 private: 00114 const IntIntTblLR* tbl; // corresponding table 00115 short k; // next key to consider 00116 bool done; // true if Next() has returned "false" 00117 00118 // hide copy constructor 00119 IntIntTblIter(const IntIntTblIter&); 00120 }; 00121 00122 #endif // _INT_INT_TBL_LR_H