00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <Basics.H>
00025 #include <SRPC.H>
00026 #include "FV.H"
00027 #include "PrefixTbl.H"
00028 #include "BitVector.H"
00029 #include "PrefixTbl.H"
00030 #include "CompactFV.H"
00031
00032 using std::ostream;
00033 using std::istream;
00034 using std::endl;
00035
00036 CompactFV::List::List(const FV::List &names) throw (PrefixTbl::Overflow)
00037 : num((short) names.len), tbl(names.len + 5)
00038 {
00039 if (names.len > 0) {
00040 this->idx = NEW_PTRFREE_ARRAY(short, names.len);
00041 this->types = NEW_PTRFREE_ARRAY(char, names.len);
00042 TextIntTbl putTbl( names.len << 1, true);
00043 for (int i = 0; i < names.len; i++) {
00044 const char *str = names.name[i].cchars();
00045
00046
00047 assert(str[0] != '\0' && str[1] == '/');
00048 this->types[i] = str[0];
00049
00050
00051 this->idx[i] = this->tbl.Put(str+2, putTbl);
00052 }
00053 } else {
00054 this->idx = (short *)NULL;
00055 this->types = (char *)NULL;
00056 }
00057 }
00058
00059 void CompactFV::List::ToFVList( FV::ListApp &fvl) const throw ()
00060 {
00061 const int BuffLen = 100;
00062 char buff[BuffLen + 2];
00063
00064 char *str = buff;
00065 int strLen = BuffLen;
00066 str[1] = '/';
00067 for (int i = 0; i < this->num; i++) {
00068 while (! this->tbl.GetString(this->idx[i], str+2, strLen)) {
00069
00070 strLen <<= 1;
00071 str = NEW_PTRFREE_ARRAY(char, strLen + 2);
00072 str[1] = '/';
00073 }
00074 str[0] = this->types[i];
00075 int ix = fvl.Append(FV::T(str)); assert(ix == i);
00076 }
00077 }
00078
00079 void CompactFV::List::Write(ostream &ofs) const throw (FS::Failure)
00080 {
00081 this->tbl.Write(ofs);
00082 FS::Write(ofs, (char *)(&(this->num)), sizeof(this->num));
00083 if (this->num > 0) {
00084 FS::Write(ofs, this->types, this->num * sizeof(this->types[0]));
00085 FS::Write(ofs, (char *)(this->idx), this->num * sizeof(this->idx[0]));
00086 }
00087 }
00088
00089 void CompactFV::List::Read(istream &ifs) throw (FS::EndOfFile, FS::Failure)
00090 {
00091 this->tbl.Read(ifs);
00092 FS::Read(ifs, (char *)(&(this->num)), sizeof(this->num));
00093 if (this->num > 0) {
00094 this->types = NEW_PTRFREE_ARRAY(char, this->num);
00095 this->idx = NEW_PTRFREE_ARRAY(short, this->num);
00096 FS::Read(ifs, this->types, this->num * sizeof(this->types[0]));
00097 FS::Read(ifs, (char *)(this->idx), this->num * sizeof(this->idx[0]));
00098 } else {
00099 this->types = (char *) NULL;
00100 this->idx = (short *) NULL;
00101 }
00102 }
00103
00104 void CompactFV::List::Send(SRPC &srpc) const throw (SRPC::failure)
00105 {
00106 this->tbl.Send(srpc);
00107 srpc.send_bytes((const char *)(this->types), this->num);
00108 srpc.send_short_array(this->idx, this->num);
00109 }
00110
00111 void CompactFV::List::Recv(SRPC &srpc) throw (SRPC::failure)
00112
00113
00114 {
00115 this->tbl.Recv(srpc);
00116 int len;
00117 this->types = srpc.recv_bytes( len);
00118 this->idx = srpc.recv_short_array( len);
00119 this->num = (short) len;
00120 }
00121
00122 inline void Indent(ostream &os, int indent) throw ()
00123 {
00124 for (int i = 0; i < indent; i++) os << " ";
00125 }
00126
00127 void CompactFV::List::Print(ostream &os, int indent) const throw ()
00128 {
00129 for (int i = 0; i < this->num; i++) {
00130 Indent(os, indent);
00131 FV2::T *nm = this->tbl.Get(this->idx[i]);
00132 os << this->types[i] << '/' << *nm << endl;
00133 }
00134 }