include/lshkit/archive.h

Go to the documentation of this file.
00001 /* 
00002     Copyright (C) 2008 Wei Dong <wdong@princeton.edu>. All Rights Reserved.
00003   
00004     This file is part of LSHKIT.
00005   
00006     LSHKIT is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     LSHKIT is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with LSHKIT.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef __LSHKIT_ARCHIVE__
00021 #define __LSHKIT_ARCHIVE__
00022 
00023 #include <vector>
00024 #include <iostream>
00025 
00049 namespace lshkit {
00050 
00051 static inline std::ostream &operator & (std::ostream &os, unsigned i) {
00052     return os.write((const char *)&i, sizeof(i));
00053 }
00054 
00055 static inline std::ostream &operator & (std::ostream &os, float i) {
00056     return os.write((const char *)&i, sizeof(i));
00057 }
00058 
00059 static inline std::istream &operator & (std::istream &is, unsigned &i) {
00060     return is.read((char *)&i, sizeof(i));
00061 }
00062 
00063 static inline std::istream &operator & (std::istream &is, float &i) {
00064     return is.read((char *)&i, sizeof(i));
00065 }
00066 
00067 static inline std::ostream &operator & (std::ostream &os, std::vector<float> &v) {
00068     unsigned L = v.size();
00069     os & L;
00070     os.write((const char *)&v[0], v.size() * sizeof(float));
00071     return os;
00072 }
00073 
00074 static inline std::istream &operator & (std::istream &is, std::vector<float> &v) {
00075     unsigned L;
00076     is & L;
00077     v.resize(L);
00078     is.read((char *)&v[0], v.size() * sizeof(float));
00079     return is;
00080 }
00081 
00082 static inline std::ostream &operator & (std::ostream &os, std::vector<unsigned> &v) {
00083     unsigned L = v.size();
00084     os & L;
00085     os.write((const char *)&v[0], v.size() * sizeof(unsigned));
00086     return os;
00087 }
00088 
00089 static inline std::istream &operator & (std::istream &is, std::vector<unsigned> &v) {
00090     unsigned L;
00091     is & L;
00092     v.resize(L);
00093     is.read((char *)&v[0], v.size() * sizeof(unsigned));
00094     return is;
00095 }
00096 
00097 template <typename D>
00098 std::ostream &operator & (std::ostream &s, D &t) {
00099     t.serialize(s, 0);
00100     return s;
00101 }
00102 
00103 template <typename D>
00104 std::istream &operator & (std::istream &s, D &t) {
00105     t.serialize(s, 0);
00106     return s;
00107 }
00108 
00109 template <typename D>
00110 std::ostream &operator & (std::ostream &os, std::vector<D> &v) {
00111     unsigned L = v.size();
00112     os & L;
00113     for (unsigned i = 0; i < L; ++i) {
00114         os & v[i];
00115     }
00116     return os;
00117 }
00118 
00119 template <typename D>
00120 std::istream &operator & (std::istream &is, std::vector<D> &v) {
00121     unsigned L;
00122     is & L;
00123     v.resize(L);
00124     for (unsigned i = 0; i < L; ++i) {
00125         is & v[i];
00126     }
00127     return is;
00128 }
00129 
00130 
00131 }
00132 
00133 #endif

Get LSHKIT at SourceForge.net. Fast, secure and Free Open Source software downloads doxygen