Clearmatics Libff  0.1
C++ library for Finite Fields and Elliptic Curves
serialization.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3 
4  Implementation of serialization routines.
5 
6  See serialization.hpp .
7 
8  *****************************************************************************
9  * @author This file is part of libff, developed by SCIPR Lab
10  * and contributors (see AUTHORS).
11  * @copyright MIT license (see LICENSE file)
12  *****************************************************************************/
13 
14 #ifndef SERIALIZATION_TCC_
15 #define SERIALIZATION_TCC_
16 
17 #include <cassert>
18 #include <libff/common/utils.hpp>
19 #include <sstream>
20 
21 namespace libff
22 {
23 
24 inline void consume_newline(std::istream &in)
25 {
26  char c;
27  in.read(&c, 1);
28 }
29 
30 inline void consume_OUTPUT_NEWLINE(std::istream &in)
31 {
32 #ifdef BINARY_OUTPUT
33  // nothing to consume
34  UNUSED(in);
35 #else
36  char c;
37  in.read(&c, 1);
38 #endif
39 }
40 
41 inline void consume_OUTPUT_SEPARATOR(std::istream &in)
42 {
43 #ifdef BINARY_OUTPUT
44  // nothing to consume
45  UNUSED(in);
46 #else
47  char c;
48  in.read(&c, 1);
49 #endif
50 }
51 
52 inline void output_bool(std::ostream &out, const bool b)
53 {
54  out << (b ? 1 : 0) << "\n";
55 }
56 
57 inline void input_bool(std::istream &in, bool &b)
58 {
59  size_t tmp;
60  in >> tmp;
61  consume_newline(in);
62  assert(tmp == 0 || tmp == 1);
63 
64  b = (tmp == 1 ? true : false);
65 }
66 
67 inline void output_bool_vector(std::ostream &out, const std::vector<bool> &v)
68 {
69  out << v.size() << "\n";
70  for (const bool b : v) {
71  output_bool(out, b);
72  }
73 }
74 
75 inline void input_bool_vector(std::istream &in, std::vector<bool> &v)
76 {
77  size_t size;
78  in >> size;
79  consume_newline(in);
80  v.resize(size);
81  for (size_t i = 0; i < size; ++i) {
82  bool b;
83  input_bool(in, b);
84  v[i] = b;
85  }
86 }
87 
88 template<typename T> T reserialize(const T &obj)
89 {
90  std::stringstream ss;
91  ss << obj;
92  T tmp;
93  ss >> tmp;
94  assert(obj == tmp);
95  return tmp;
96 }
97 
98 template<typename T>
99 std::ostream &operator<<(std::ostream &out, const std::vector<T> &v)
100 {
101  static_assert(
102  !std::is_same<T, bool>::value,
103  "this does not work for std::vector<bool>");
104  out << v.size() << "\n";
105  for (const T &t : v) {
106  out << t << OUTPUT_NEWLINE;
107  }
108 
109  return out;
110 }
111 
112 template<typename T>
113 std::istream &operator>>(std::istream &in, std::vector<T> &v)
114 {
115  static_assert(
116  !std::is_same<T, bool>::value,
117  "this does not work for std::vector<bool>");
118  size_t size;
119  in >> size;
120  consume_newline(in);
121 
122  v.resize(0);
123  for (size_t i = 0; i < size; ++i) {
124  T elt;
125  in >> elt;
126  consume_OUTPUT_NEWLINE(in);
127  v.push_back(elt);
128  }
129 
130  return in;
131 }
132 
133 template<typename T1, typename T2>
134 std::ostream &operator<<(std::ostream &out, const std::map<T1, T2> &m)
135 {
136  out << m.size() << "\n";
137 
138  for (auto &it : m) {
139  out << it.first << "\n";
140  out << it.second << "\n";
141  }
142 
143  return out;
144 }
145 
146 template<typename T1, typename T2>
147 std::istream &operator>>(std::istream &in, std::map<T1, T2> &m)
148 {
149  m.clear();
150  size_t size;
151  in >> size;
152  consume_newline(in);
153 
154  for (size_t i = 0; i < size; ++i) {
155  T1 k;
156  T2 v;
157  in >> k;
158  consume_newline(in);
159  in >> v;
160  consume_newline(in);
161  m[k] = v;
162  }
163 
164  return in;
165 }
166 
167 template<typename T>
168 std::ostream &operator<<(std::ostream &out, const std::set<T> &s)
169 {
170  out << s.size() << "\n";
171 
172  for (auto &el : s) {
173  out << el << "\n";
174  }
175 
176  return out;
177 }
178 
179 template<typename T> std::istream &operator>>(std::istream &in, std::set<T> &s)
180 {
181  s.clear();
182  size_t size;
183  in >> size;
184  consume_newline(in);
185 
186  for (size_t i = 0; i < size; ++i) {
187  T el;
188  in >> el;
189  consume_newline(in);
190  s.insert(el);
191  }
192 
193  return in;
194 }
195 
196 } // namespace libff
197 
198 #endif // SERIALIZATION_TCC_