iterator.hpp

00001 /*00002 This file is part of GraphLab.00003 00004 GraphLab is free software: you can redistribute it and/or modify00005 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 of00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the00012 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_HPP00019 #define GRAPHLAB_SERIALIZE_ITERATOR_HPP00020 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 needs00030 a distance between the begin and end iterator.00031 This function as implemented will work for other input iterators00032 but is extremely inefficient.00033 Returns true on success, false on failure */00034 template <typename ArcType, typename RandomAccessIterator>
00035voidserialize_iterator(ArcType& a, RandomAccessIterator begin,
00036 RandomAccessIterator end){
00037 size_t vsize = std::distance(begin, end);
00038 a << vsize;
00039 //store each element00040 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" for00049 efficiency. This count is checked and will return failure if the number00050 of elements serialized does not match the count00051 Returns true on success, false on failure */00052 template <typename ArcType, typename InputIterator>
00053voidserialize_iterator(ArcType& a, InputIterator begin,
00054 InputIterator end, size_t vsize){
00055 a << vsize;
00056 //store each element00057 size_t count = 0;
00058 for(; begin != end; ++begin) {
00059 ++count;
00060 a << *begin;
00061 }
00062 // fail if count does not match00063 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 the00070 "type of object to deserialize"00071 This is necessary for instance for the map type. The map<T,U>::value_type00072 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>
00077voiddeserialize_iterator(ArcType& a, OutputIterator result) {
00078 // get the number of elements to deserialize00079 size_t length = 0;
00080 a >> length;
00081
00082 // iterate through and send to the output iterator00083 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 prl00093 #endif //PRL_SERIALIZE_ITERATOR_HPP00094

Generated on Tue May 17 2011 15:43:54 for GraphLab: A framework for Parallel Machine Learning by
1.7.1