00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _FP_H
00026 #define _FP_H
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 #include <Basics.H>
00068 #include <FS.H>
00069 #include <SRPC.H>
00070 #include <VestaLog.H>
00071 #include <Recovery.H>
00072 #include "Poly.H"
00073
00074 namespace FP {
00075 class Tag;
00076 }
00077
00078 int Compare(const FP::Tag& fp1, const FP::Tag& fp2) throw ();
00079
00080 namespace FP {
00081 enum { ByteCnt = PolyVal::ByteCnt };
00082 enum { WordCnt = PolyVal::WordCnt };
00083
00084
00085 class Tag {
00086 public:
00087
00088
00089 Tag() throw () { };
00090
00091
00092
00093
00094 Tag(const char *s, int len = -1) throw ()
00095 { if (len < 0) len = strlen(s); Init(s, len); }
00096
00097
00098
00099
00100
00101 Tag(const Text &t) throw () { Init(t.cchars(), t.Length()); }
00102
00103
00104 Tag(RecoveryReader &rd) throw (VestaLog::Error, VestaLog::Eof)
00105 { Recover(rd); }
00106
00107 Tag(std::istream &ifs) throw (FS::Failure, FS::EndOfFile)
00108 { Read(ifs); }
00109
00110 Tag(SRPC &srpc) throw (SRPC::failure)
00111 { Recv(srpc); }
00112
00113
00114
00115 Tag& Extend(const char *s, int len = -1) throw ();
00116
00117
00118
00119
00120 Tag& Extend(const Text &t) throw ()
00121 { return this->Extend(t.cchars()); }
00122
00123
00124 Tag& Extend(const Tag &tag) throw ()
00125 {
00126 unsigned char bytes[FP::ByteCnt];
00127 tag.ToBytes(bytes);
00128 return this->Extend((char *)bytes,
00129 sizeof_assert(bytes, FP::ByteCnt));
00130 }
00131
00132
00133 Tag& Extend(char c) throw ();
00134
00135
00136
00137
00138 void Unpermute( RawFP &fp) const throw ();
00139
00140
00141 static void ExtendRaw( RawFP &fp, const char *s, int len = -1)
00142 throw ();
00143 static void ExtendRaw( RawFP &fp, const Text &t) throw ()
00144 { ExtendRaw( fp, t.cchars()); }
00145 static void ExtendRaw( RawFP &fp, const Tag &tag) throw ()
00146 {
00147 unsigned char bytes[FP::ByteCnt];
00148 tag.ToBytes(bytes);
00149 ExtendRaw( fp, (char *)(bytes),
00150 sizeof_assert(bytes, FP::ByteCnt));
00151 }
00152 static void ExtendRaw( RawFP &fp, char c) throw ();
00153
00154
00155
00156 void Permute(const RawFP &f) throw ();
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 void Log(VestaLog& log) const throw (VestaLog::Error)
00173 {
00174 unsigned char bytes[FP::ByteCnt];
00175 this->ToBytes(bytes);
00176 log.write((char *)bytes, FP::ByteCnt);
00177 }
00178
00179
00180
00181 void Recover(RecoveryReader &rd) throw (VestaLog::Error, VestaLog::Eof)
00182 {
00183 unsigned char bytes[FP::ByteCnt];
00184 rd.readAll((char *)bytes, FP::ByteCnt);
00185 this->FromBytes(bytes);
00186 }
00187
00188
00189
00190
00191
00192 void Write(std::ostream &ofs) const throw (FS::Failure)
00193 {
00194 unsigned char bytes[FP::ByteCnt];
00195 this->ToBytes(bytes);
00196 FS::Write(ofs, (char *)(bytes), sizeof(bytes));
00197 }
00198 void Read(std::istream &ifs) throw (FS::EndOfFile, FS::Failure)
00199 {
00200 unsigned char bytes[FP::ByteCnt];
00201 FS::Read(ifs, (char *)(bytes), sizeof(bytes));
00202 this->FromBytes(bytes);
00203 }
00204
00205
00206
00207
00208 void Send(SRPC &srpc) const throw (SRPC::failure)
00209 {
00210 unsigned char bytes[FP::ByteCnt];
00211 this->ToBytes(bytes);
00212 srpc.send_bytes((char *)(bytes),
00213 sizeof_assert(bytes, FP::ByteCnt));
00214 }
00215 void Recv(SRPC &srpc) throw (SRPC::failure)
00216 {
00217 unsigned char bytes[FP::ByteCnt];
00218 int len = sizeof(bytes);
00219 srpc.recv_bytes_here((char *)(bytes), len);
00220 assert(len == FP::ByteCnt);
00221 this->FromBytes(bytes);
00222 }
00223
00224 Word Hash() const throw ();
00225
00226
00227 void Print(std::ostream &os, const char *word_separator = " ")
00228 const throw ();
00229
00230
00231
00232 bool operator == (const Tag &other) const throw ();
00233 bool operator != (const Tag &other)const throw ();
00234
00235
00236 friend int ::Compare(const Tag& fp1, const Tag& fp2) throw ();
00237
00238
00239
00240
00241 int Size() const throw () { return FP::ByteCnt; }
00242
00243
00244
00245
00246 void ToBytes(unsigned char *buffer) const throw ();
00247 void FromBytes(const unsigned char *buffer) throw ();
00248
00249
00250 Word *Words() throw () { return this->w; }
00251 Word Word0() const throw () { return this->w[0]; }
00252 Word Word1() const throw () { return this->w[1]; }
00253
00254 protected:
00255
00256
00257 Word w[WordCnt];
00258
00259 private:
00260 void Init(const char *s, int len) throw ();
00261
00262 };
00263
00264
00265 class List {
00266 public:
00267
00268 FP::Tag *fp;
00269 int len;
00270
00271
00272 List() throw ()
00273 { len = 0; fp = (FP::Tag *)NULL; }
00274 List(int len) throw ()
00275 {
00276 this->len = len;
00277 fp = NEW_PTRFREE_ARRAY(FP::Tag, len);
00278 }
00279 List(RecoveryReader &rd) throw (VestaLog::Error, VestaLog::Eof)
00280 { Recover(rd); }
00281 List(std::istream &ifs) throw (FS::EndOfFile, FS::Failure)
00282 { Read(ifs); }
00283 List(SRPC &srpc) throw (SRPC::failure)
00284 { Recv(srpc); }
00285
00286
00287 int Size() const throw ()
00288 { return sizeof(this->len) + (this->len * sizeof(FP::Tag)); }
00289
00290
00291 void Log(VestaLog& log) const throw (VestaLog::Error);
00292 void Recover(RecoveryReader &rd)
00293 throw (VestaLog::Error, VestaLog::Eof);
00294
00295
00296 void Write(std::ostream &ofs) const throw (FS::Failure);
00297 void Read(std::istream &ifs) throw (FS::EndOfFile, FS::Failure);
00298
00299
00300 void Send(SRPC &srpc) const throw (SRPC::failure);
00301 void Recv(SRPC &srpc) throw (SRPC::failure);
00302
00303
00304 void Print(std::ostream &os, int indent) const throw ();
00305
00306
00307 private:
00308
00309 List(const List&);
00310 };
00311
00312 void FileContents(std::istream &ifs, FP::Tag &fp)
00313 throw (FS::Failure);
00314
00315
00316 }
00317
00318 inline std::ostream& operator << (std::ostream &os, const FP::Tag &fp) throw ()
00319 {
00320 fp.Print(os);
00321 return os;
00322 }
00323
00324 #endif // _FP_H