Pos.cpp

Ir a la documentación de este archivo.
00001 // vim: set expandtab tabstop=8 shiftwidth=8 foldmethod=marker:
00012 #include <set>
00013 #include <iostream>
00014 #include <iomanip>
00015 #include <list>
00016 #include <vector>
00017 #include <fstream>
00018 #include <istream>
00019 #include <sstream>
00020 
00021 
00022 // using namespace std;
00023 // La anterior comentada por poco portable
00024 
00025 #include "comun.hpp"
00026 #include "Pos.hpp"
00027 #include "Elias.hpp"
00028 
00029 
00030 bool
00031 operator<(Pos p1, Pos p2)
00032 {
00033         return (p1.numd < p2.numd ||
00034                 (p1.numd == p2.numd && p1.numb < p2.numb));
00035 }
00036 
00037 
00038 std::ostream &
00039 operator<<(std::ostream &os, Pos p)
00040 {
00041         os << "(" << p.numd << "," << p.numb << ")";
00042         return os;
00043 }
00044 
00045 
00046 void escribePos(iostream &os, set<Pos> *cpos)
00047 {
00048         //clog << "OJO escribePos os.tellp()=" << os.tellp() << endl;
00049         if (cpos != NULL) {
00050                 string sep = "";
00051                 set<Pos>::iterator i;
00052                 long l = -1;
00053                 long unumb = 0;
00054                 for (i = cpos->begin(); i != cpos->end(); i++) {
00055                         long nd = i->numd;
00056                         ASSERT(nd >= 1);
00057                         //clog << "nd=" << nd << ", numb=" << i->numb << endl;
00058                         if (nd != l) {
00059                                 os << sep;
00060                                 escribe_elias_gama2(os, (unsigned int)nd);
00061                                 //clog << "escribió nd" << endl;
00062                                 unumb = 0;
00063                                 sep = "(";
00064                                 l = nd;
00065                         }
00066                         ASSERT(i->numb - unumb >= 1);
00067                         escribe_elias_gama2(os, i->numb - unumb);
00068                         //clog << "escribió i->numb-unumb=" << i->numb-unumb << endl;
00069                         unumb = i->numb;
00070                 }
00071         }
00072         os << "}";
00073         /*      long pih = os.tellp();
00074                 
00075                 return pih; */
00076 }
00077 
00078 
00079 unsigned long longPos(set<Pos> *cpos)
00080 {
00081 
00082         long r=0;
00083         if (cpos != NULL) {
00084                 long ls = 0;
00085                 set<Pos>::iterator i;
00086                 int l = -1;
00087                 long unumb = 0;
00088                 for (i = cpos->begin(); i != cpos->end(); i++) {
00089                         if (i->numd != l) {
00090                                 r += ls;
00091                                 ASSERT(i->numd >= 1);
00092                                 r += long_elias_gama(i->numd);
00093                                 ls = 1;
00094                                 l = i->numd;
00095                                 unumb = 0;
00096                         }
00097                         ASSERT(i->numb - unumb >= 1);
00098                         r += long_elias_gama(i->numb - unumb);
00099                         unumb = i->numb;
00100                 }
00101         }
00102         r++;
00103         return r;
00104 }
00105 
00106 
00107 set<Pos> *copiaPos(set<Pos> &p, vector<long> *renum)
00108 {
00109         //clog << "OJO copiaPos(p="<< p<< ", renum=" << renum << ")" << endl;
00110         set<Pos> *cpos = new set<Pos>();
00111         ASSERT(cpos != NULL);
00112         string sep = "";
00113         set<Pos>::iterator i;
00114         int c=0;
00115         for (i = p.begin(), c=0; i != p.end(); i++, c++) {
00116                 //clog << "OJO c="<< c << ", i->numd=" << i->numd << "i->numb=" << i->numb << endl;
00117                 long nd = i->numd;
00118                 ASSERT(nd >= 1);
00119                 if (renum != NULL) {
00120                         //clog << "OJO renum->size=" << renum->size() << ", nd=" << nd << ", (*renum)[nd-1]=" << (*renum)[nd-1] << endl;
00121                         ASSERT(nd <= (long)renum->size());
00122                         ASSERT((*renum)[nd-1] >= -1);
00123                         if ((*renum)[nd-1] >= 0) {
00124                                 nd = (*renum)[nd-1] + 1;
00125                         } else {
00126                                 nd = -1;  // eliminar
00127                                 //clog << "OJO por eliminar" << endl;
00128                         }
00129                 }
00130                 if (nd >= 1) {
00132                         cpos->insert(Pos(nd, i->numb));
00133                 }
00134         }
00135 
00136         return cpos;
00137 }
00138 
00139 
00140 
00141 set<Pos> *leePos(istream &is, vector<long> *renum)
00142 {
00143         //clog << "OJO leePos is.tellg=" << is.tellg() << endl;
00144         set<Pos> *cpos = new set<Pos>();
00145         long np = 0;
00146         int c = is.peek();
00147         //clog << "OJO c="<<c<<endl;
00148         while (c != '}' && c != EOF) {
00149                 //clog << "OJO en ciclo c="<<c<<" "<<(char)c<<endl;
00150                 long v1,v2, uv2;
00151                 v1 = lee_elias_gama2(is);
00152                 //clog << "OJO en ciclo c="<<c<<" "<<(char)c<<", v1=" << v1 <<endl;
00153                 ASSERT(v1 >= 1);
00154                 if (renum != NULL) {
00155                         //clog << "OJO renum->size=" << renum->size() << ", v1=" << v1 << ", (*renum)[v1-1]=" << (*renum)[v1-1] << endl;
00156                         ASSERT(v1 <= (long)renum->size());
00157                         ASSERT((*renum)[v1-1] >= -1);
00158                         if ((*renum)[v1-1] >= 0) {
00159                                 v1 = (*renum)[v1-1] + 1;
00160                         } else {
00161                                 v1 = -1;  // eliminar
00162                         }
00163                 }
00164                 //clog << "OJO en ciclo primer hex leido v1="<<v1<<endl;
00165                 uv2=0;
00166                 do {
00167                         v2 = lee_elias_gama2(is);
00168                         //clog << "OJO en ciclo segundo hex leido v2="<<v2<<endl;
00169                         if (v1 >= 0) {
00170                                 uv2 += v2;
00171                                 cpos->insert(Pos(v1, uv2));
00172                                 //clog << "OJO insertado" <<endl;
00173                         }
00174                         c = is.peek();
00175                         //clog << "OJO c=" << (int)c << ", como car=" << (char)c << endl;
00176                         if ((c & 128) == 0 && c != 0 && c != '}'
00177                                         && c != '(') {
00178                                 throw errorFormato(is, "Se esperaba numero en codificación de elias o { o (");
00179                         }
00180 
00181                         np++;
00182                 } while (c != '}' && c != '(' && c != EOF) ;
00183                 // } ascii 125 ( ascii 40, son buenos porque no corresponden
00184                 // a códigos elias ---los ve como uno porque su rep. binaria
00185                 // comienza con 0
00186                 c = is.get();
00187         }
00188         if (c != '}') {
00189                 throw errorFormato(is, "Se esperaba }");
00190         }
00191         if (np == 0) {
00192                 c = is.get();
00193         }
00194         return cpos;
00195 }
00196 

Generado el Wed Jan 6 06:58:22 2010 para Mt77 por  doxygen 1.5.4