00001 /* 00002 This file is part of GraphLab. 00003 00004 GraphLab is free software: you can redistribute it and/or modify 00005 it under the terms of the GNU Lesser General Public License as 00006 published by the Free Software Foundation, either version 3 of 00007 the License, or (at your option) any later version. 00008 00009 GraphLab is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public 00015 License along with GraphLab. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef GRAPHLAB_SERIALIZE_ITERATOR_HPP 00019 #define GRAPHLAB_SERIALIZE_ITERATOR_HPP 00020 00021 #include <iterator> 00022 #include <graphlab/serialization/oarchive.hpp> 00023 #include <graphlab/serialization/iarchive.hpp> 00024 00025 namespace graphlab { 00026 00027 /** 00028 Serializes the contents between the iterators begin and end. 00029 This version prefers the availability of RandomAccessIterator since it needs 00030 a distance between the begin and end iterator. 00031 This function as implemented will work for other input iterators 00032 but is extremely inefficient. 00033 Returns true on success, false on failure */ 00034 template <typename ArcType, typename RandomAccessIterator> 00035 void serialize_iterator(ArcType& a, RandomAccessIterator begin, 00036 RandomAccessIterator end){ 00037 size_t vsize = std::distance(begin, end); 00038 a << vsize; 00039 //store each element 00040 for(; begin != end; ++begin) { 00041 a << *begin; 00042 } 00043 } 00044 00045 00046 /** 00047 Serializes the contents between the iterators begin and end. 00048 This version takes all InputIterator types, but takes a "count" for 00049 efficiency. This count is checked and will return failure if the number 00050 of elements serialized does not match the count 00051 Returns true on success, false on failure */ 00052 template <typename ArcType, typename InputIterator> 00053 void serialize_iterator(ArcType& a, InputIterator begin, 00054 InputIterator end, size_t vsize){ 00055 a << vsize; 00056 //store each element 00057 size_t count = 0; 00058 for(; begin != end; ++begin) { 00059 ++count; 00060 a << *begin; 00061 } 00062 // fail if count does not match 00063 assert(count == vsize); 00064 } 00065 00066 /** 00067 The accompanying function to serialize_iterator() 00068 Reads elements from the stream and send it to the output iterator. 00069 Note that this requires an additional template parameter T which is the 00070 "type of object to deserialize" 00071 This is necessary for instance for the map type. The map<T,U>::value_type 00072 is pair<const T,U> which is not useful since I cannot assign to it. 00073 In this case, T=pair<T,U> 00074 00075 Returns true on success, false on failure */ 00076 template <typename ArcType, typename T, typename OutputIterator> 00077 void deserialize_iterator(ArcType& a, OutputIterator result) { 00078 // get the number of elements to deserialize 00079 size_t length = 0; 00080 a >> length; 00081 00082 // iterate through and send to the output iterator 00083 for (size_t x = 0; x < length ; ++x){ 00084 T v; 00085 a >> v; 00086 (*result) = v; 00087 result++; 00088 } 00089 } 00090 00091 00092 } // namespace prl 00093 #endif //PRL_SERIALIZE_ITERATOR_HPP 00094
1.7.1