Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

mootBinIO.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*- */
00002 
00003 /*
00004    libmoot : moocow's part-of-speech tagging library
00005    Copyright (C) 2003-2004 by Bryan Jurish <moocow@ling.uni-potsdam.de>
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 */
00021 
00022 /*--------------------------------------------------------------------------
00023  * File: mootBinIO.h
00024  * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
00025  * Description:
00026  *   + moot PoS tagger : abstract templates for binary librarians
00027  *--------------------------------------------------------------------------*/
00028 
00029 #ifndef _MOOT_BINIO_H
00030 #define _MOOT_BINIO_H
00031 
00032 #include <stdlib.h>
00033 
00034 #include <vector>
00035 #include <string>
00036 #include <map>
00037 #include <set>
00038 
00039 #include <mootTypes.h>
00040 #include <mootEnum.h>
00041 #include <mootIO.h>
00042 #include <mootHMM.h>
00043 
00045 namespace mootBinIO {
00046   using namespace std;
00047   using namespace moot;
00048   using namespace mootio;
00049 
00050   /*------------------------------------------------------------
00051    * Generic items
00052    */
00054   template<class T> class Item {
00055   public:
00057     inline bool load(mootio::mistream *is, T &x) const
00058     {
00059       return is->read((char *)&x, sizeof(T)) == sizeof(T);
00060     };
00061 
00063     inline bool save(mootio::mostream *os, const T &x) const
00064     {
00065       return os->write((char *)&x, sizeof(T));
00066     };
00067 
00074     inline bool load_n(mootio::mistream *is, T *&x, size_t &n) const {
00075       //-- get saved size
00076       Item<size_t> size_item;
00077       size_t saved_size;
00078       if (!size_item.load(is, saved_size)) return false;
00079 
00080       //-- re-allocate if necessary
00081       if (saved_size > n) {
00082         if (x) free(x);
00083         x = (T *)malloc(saved_size*sizeof(T));
00084         if (!x) {
00085           n = 0;
00086           return false;
00087         }
00088       }
00089 
00090       //-- read in items
00091       ByteCount wanted = sizeof(T)*saved_size;
00092       if (is->read((char *)x, wanted) != wanted) return false;
00093       n=saved_size;
00094       return true;
00095     };
00096 
00102     inline bool save_n(mootio::mostream *os, const T *x, size_t n) const {
00103       //-- get saved size
00104       Item<size_t> size_item;
00105       if (!size_item.save(os, n)) return false;
00106 
00107       //-- save items
00108       return os->write((char *)x, n*sizeof(T));
00109     };
00110   };
00111 
00112   /*------------------------------------------------------------
00113    * C-strings
00114    */
00119   template<> class Item<char *> {
00120   public:
00121     Item<char> charItem;
00122 
00123   public:
00124     inline bool load(mootio::mistream *is, char *&x) const
00125     {
00126       size_t len=0;
00127       return charItem.load_n(is,x,len);
00128     };
00129  
00130     inline bool save(mootio::mostream *os, const char *x) const
00131     {
00132       if (x) {
00133         size_t len = strlen(x)+1;
00134         return charItem.save_n(os,x,len);
00135       } else {
00136         return charItem.save_n(os,"",1);
00137       }
00138     };
00139   };
00140 
00141   /*------------------------------------------------------------
00142    * C++ strings
00143    */
00148   template<> class Item<string> {
00149   public:
00150     Item<char> charItem;
00151   public:
00152     inline bool load(mootio::mistream *is, string &x) const
00153     {
00154       char *buf=NULL;
00155       size_t len=0;
00156       bool rc = charItem.load_n(is,buf,len);
00157       if (rc && len) x.assign(buf,len);
00158       if (buf) free(buf);
00159       return rc;
00160     };
00161 
00162     inline bool save(mootio::mostream *os, const string &x) const
00163     {
00164       return charItem.save_n(os,x.data(),x.size());
00165     };
00166   };
00167 
00168   /*------------------------------------------------------------
00169    * STL: vectors
00170    */
00172   template<class ValT> class Item<vector<ValT> > {
00173   public:
00174     Item<ValT> val_item;
00175   public:
00176     inline bool load(mootio::mistream *is, vector<ValT> &x) const
00177     {
00178       //-- get saved size
00179       Item<size_t> size_item;
00180       size_t len;
00181       if (!size_item.load(is, len)) return false;
00182 
00183       //-- resize
00184       x.clear();
00185       x.reserve(len);
00186 
00187       //-- read in items
00188       for ( ; len > 0; len--) {
00189         x.push_back(ValT());
00190         if (!val_item.load(is,x.back())) return false;
00191       }
00192       return len==0;
00193     };
00194 
00195     inline bool save(mootio::mostream *os, const vector<ValT> &x) const
00196     {
00197       //-- save size
00198       Item<size_t> size_item;
00199       if (!size_item.save(os, x.size())) return false;
00200 
00201       //-- save items
00202       for (typename vector<ValT>::const_iterator xi = x.begin(); xi != x.end(); xi++) {
00203         if (!val_item.save(os,*xi)) return false;
00204       }
00205       return true;
00206     };
00207   };
00208 
00209 
00210   /*------------------------------------------------------------
00211    * STL: set<>
00212    */
00214   template<class ValT> class Item<set<ValT> > {
00215   public:
00216     Item<ValT> val_item;
00217   public:
00218     inline bool load(mootio::mistream *is, set<ValT> &x) const
00219     {
00220       //-- load size
00221       Item<size_t> size_item;
00222       size_t len;
00223       if (!size_item.load(is, len)) return false;
00224 
00225       //-- clear
00226       x.clear();
00227 
00228       //-- read items
00229       ValT tmp;
00230       for ( ; len > 0; len--) {
00231         if (!val_item.load(is,tmp))
00232           return false;
00233         x.insert(tmp);
00234       }
00235       return len==0;
00236     };
00237 
00238     inline bool save(mootio::mostream *os, const set<ValT> &x) const
00239     {
00240       //-- save size
00241       Item<size_t> size_item;
00242       if (!size_item.save(os, x.size())) return false;
00243 
00244       //-- save items
00245       for (typename set<ValT>::const_iterator xi = x.begin(); xi != x.end(); xi++) {
00246         if (!val_item.save(os,*xi)) return false;
00247       }
00248       return true;
00249     };
00250   };
00251 
00252   /*------------------------------------------------------------
00253    * STL: hash_set<>
00254    */
00256   template<class ValT> class Item<hash_set<ValT> > {
00257   public:
00258     Item<ValT> val_item;
00259   public:
00260     inline bool load(mootio::mistream *is, hash_set<ValT> &x) const
00261     {
00262       //-- load size
00263       Item<size_t> size_item;
00264       size_t len;
00265       if (!size_item.load(is, len)) return false;
00266 
00267       //-- clear & resize
00268       x.clear();
00269       x.resize(len);
00270 
00271       //-- read items
00272       ValT tmp;
00273       for ( ; len > 0; len--) {
00274         if (!val_item.load(is,tmp)) return false;
00275         x.insert(tmp);
00276       }
00277       return len==0;
00278     };
00279 
00280     inline bool save(mootio::mostream *os, const hash_set<ValT> &x) const
00281     {
00282       //-- save size
00283       Item<size_t> size_item;
00284       if (!size_item.save(os, x.size())) return false;
00285 
00286       //-- save items
00287       for (typename hash_set<ValT>::const_iterator xi = x.begin(); xi != x.end(); xi++) {
00288         if (!val_item.save(os,*xi)) return false;
00289       }
00290       return true;
00291     };
00292   };
00293 
00294 
00295   /*------------------------------------------------------------
00296    * STL: map<>
00297    */
00299   template<class KeyT, class ValT> class Item<map<KeyT,ValT> > {
00300   public:
00301     Item<KeyT> key_item;
00302     Item<ValT> val_item;
00303   public:
00304     inline bool load(mootio::mistream *is, map<KeyT,ValT> &x) const
00305     {
00306       //-- load size
00307       Item<size_t> size_item;
00308       size_t len;
00309       if (!size_item.load(is, len)) return false;
00310 
00311       //-- clear
00312       x.clear();
00313 
00314       //-- read items
00315       KeyT key_tmp;
00316       ValT val_tmp;
00317       for ( ; len > 0; len--) {
00318         if (!key_item.load(is,key_tmp) || !val_item.load(is,val_tmp))
00319           return false;
00320         x[key_tmp] = val_tmp;
00321       }
00322       return len==0;
00323     };
00324 
00325     inline bool save(mootio::mostream *os, const map<KeyT,ValT> &x) const
00326     {
00327       //-- save size
00328       Item<size_t> size_item;
00329       if (!size_item.save(os, x.size())) return false;
00330 
00331       //-- save items
00332       for (typename map<KeyT,ValT>::const_iterator xi = x.begin(); xi != x.end(); xi++) {
00333         if (!key_item.save(os,xi->first) || !val_item.save(os,xi->second))
00334           return false;
00335       }
00336       return true;
00337     };
00338   };
00339 
00340 
00341   /*------------------------------------------------------------
00342    * STL: hash_map<>
00343    */
00345   template<class KeyT, class ValT, class HashFuncT, class EqualFuncT>
00346   class Item<hash_map<KeyT,ValT,HashFuncT,EqualFuncT> > {
00347   public:
00348     Item<KeyT> key_item;
00349     Item<ValT> val_item;
00350   public:
00351     inline bool load(mootio::mistream *is, hash_map<KeyT,ValT,HashFuncT,EqualFuncT> &x) const
00352     {
00353       //-- load size
00354       Item<size_t> size_item;
00355       size_t len;
00356       if (!size_item.load(is, len)) return false;
00357 
00358       //-- clear & resize
00359       x.clear();
00360       x.resize(len);
00361 
00362       //-- read items
00363       KeyT key_tmp;
00364       ValT val_tmp;
00365       for ( ; len > 0; len--) {
00366         if (!key_item.load(is,key_tmp) || !val_item.load(is,val_tmp))
00367           return false;
00368         x[key_tmp] = val_tmp;
00369       }
00370       return len==0;
00371     };
00372 
00373     inline bool save(mootio::mostream *os, const hash_map<KeyT,ValT,HashFuncT,EqualFuncT> &x) const
00374     {
00375       //-- save size
00376       Item<size_t> size_item;
00377       if (!size_item.save(os, x.size())) return false;
00378 
00379       //-- save items
00380       for (typename hash_map<KeyT,ValT,HashFuncT,EqualFuncT>::const_iterator xi = x.begin();
00381            xi != x.end();
00382            xi++)
00383         {
00384           if (!key_item.save(os,xi->first) || !val_item.save(os,xi->second))
00385             return false;
00386         }
00387       return true;
00388     };
00389   };
00390 
00391   /*------------------------------------------------------------
00392    * moot types: Trigram
00393    */
00394 #if defined(MOOT_USE_TRIGRAMS) && defined(MOOT_HASH_TRIGRAMS)
00395   template <>
00396   class Item<mootHMM::Trigram> {
00397   public:
00398     Item<mootHMM::TagID> tagid_item;
00399   public:
00400     inline bool load(mootio::mistream *is, mootHMM::Trigram &x) const
00401     {
00402       return (tagid_item.load(is, x.tag1)
00403               && tagid_item.load(is, x.tag2)
00404               && tagid_item.load(is, x.tag3));
00405     };
00406 
00407     inline bool save(mootio::mostream *os, const mootHMM::Trigram &x) const
00408     {
00409       return (tagid_item.save(os, x.tag1)
00410               && tagid_item.save(os, x.tag2)
00411               && tagid_item.save(os, x.tag3));
00412     };
00413   };
00414 #endif // MOOT_USE_TRIGRAMS && MOOT_HASH_TRIGRAMS
00415 
00416   /*------------------------------------------------------------
00417    * moot types: mootEnum
00418    */
00420   template<class NameT, class HashFunc, class NameEqlFunc>
00421   class Item<mootEnum<NameT,HashFunc,NameEqlFunc> > {
00422   public:
00423     Item<typename mootEnum<NameT,HashFunc,NameEqlFunc>::Id2NameMap> i2n_item;
00424   public:
00425     inline bool load(mootio::mistream *is, mootEnum<NameT,HashFunc,NameEqlFunc> &x) const
00426     {
00427       if (i2n_item.load(is, x.ids2names)) {
00428         x.names2ids.resize(x.ids2names.size());
00429         unsigned u;
00430         typename mootEnum<NameT,HashFunc,NameEqlFunc>::Id2NameMap::const_iterator ni;
00431         for (ni = x.ids2names.begin(), u = 0; ni != x.ids2names.end(); ni++, u++)
00432           {
00433             x.names2ids[*ni] = u;
00434           }
00435         return true;
00436       }
00437       return false;
00438     };
00439 
00440     inline bool save(mootio::mostream *os, const mootEnum<NameT,HashFunc,NameEqlFunc> &x) const
00441     {
00442       return i2n_item.save(os, x.ids2names);
00443     };
00444   };
00445 
00446 
00447   /*------------------------------------------------------------
00448    * public typedefs: Generic header information
00449    */
00451   class HeaderInfo {
00452   public:
00454     typedef unsigned int VersionT;
00455     
00457     typedef unsigned int MagicT;
00458     
00460     typedef unsigned long int FlagsT;
00461   public:
00462     MagicT    magic;      
00463     VersionT  version;    
00464     VersionT  revision;   
00465     VersionT  minver;     
00466     VersionT  minrev;     
00467     FlagsT    flags;      
00468   public:
00470     HeaderInfo(MagicT mag=0,
00471                VersionT ver=0, VersionT rev=0,
00472                VersionT mver=0, VersionT mrev=0,
00473                FlagsT f=0)
00474       : magic(mag),
00475         version(ver),
00476         revision(rev),
00477         minver(mver),
00478         minrev(mrev),
00479         flags(f)
00480     {};
00481 
00483     HeaderInfo(const string &IDstring,
00484                VersionT ver=0, VersionT rev=0,
00485                VersionT mver=0, VersionT mrev=0,
00486                FlagsT f=0)
00487       : version(ver),
00488         revision(rev),
00489         minver(mver),
00490         minrev(mrev),
00491         flags(f)
00492     {
00493       magic = 0;
00494       for (string::const_iterator si = IDstring.begin(); si != IDstring.end(); si++) {
00495         magic = (magic<<5)-magic + (MagicT)*si;
00496       }
00497     };
00498   };
00499 
00500 }; //-- mootBinIO
00501 
00502 
00503 #endif /* _MOOT_BINIO_H */

Generated on Wed Jul 28 15:48:02 2004 for libmoot by doxygen1.2.15