-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvec_GF2.cpp.html
256 lines (167 loc) · 14.2 KB
/
vec_GF2.cpp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>/Volumes/Unix/unix-files.noindex/ntl-new/ntl-9.6.0/doc/vec_GF2.cpp.html</title>
<meta name="Generator" content="Vim/7.3">
<meta name="plugin-version" content="vim7.3_v6">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css">
<style type="text/css">
<!--
pre { font-family: monospace; color: #000000; background-color: #ffffff; }
body { font-family: monospace; color: #000000; background-color: #ffffff; }
.Statement { color: #b03060; font-weight: bold; }
.Type { color: #008b00; font-weight: bold; }
.Comment { color: #0000ee; font-style: italic; }
-->
</style>
</head>
<body>
<pre>
<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>
<span class="Comment">MODULE: vec_GF2</span>
<span class="Comment">SUMMARY:</span>
<span class="Comment">The class Vec<GF2> is explicitly specialized.</span>
<span class="Comment">It behaves much like a generic Vec<T> (see vector.txt), </span>
<span class="Comment">but there are some differences.</span>
<span class="Comment">For efficiency, elements of a Vec<GF2> are "packed" into a word.</span>
<span class="Comment">You can still use subscript notation v[i] or v(i).</span>
<span class="Comment">For const vectors, these evaluate to values of type const GF2.</span>
<span class="Comment">For non-const vectors, these evaluate to values of the</span>
<span class="Comment">special type ref_GF2, which is defined in the GF2 header file.</span>
<span class="Comment">There are implicit conversions from ref_GF2 to const GF2</span>
<span class="Comment">and from GF2& to ref_GF2. Therefore, if you want to declare</span>
<span class="Comment">a function that takes a non-const reference to a GF2, you</span>
<span class="Comment">should declare the parameter of type ref_GF2: this will</span>
<span class="Comment">allow you to pass variables of type GF2 as well as </span>
<span class="Comment">elements of vec_GF2's obtained through indexing.</span>
<span class="Comment">As an alternative, one can use the get and put methods below to access</span>
<span class="Comment">vector elements.</span>
<span class="Comment">There is one subtle but important difference in the semantics</span>
<span class="Comment">of Vec<GF2> and that of generic NTL vectors. With a Vec<GF2>, whenever its</span>
<span class="Comment">length is increased (via SetLength), the "new" bits are always 0.</span>
<span class="Comment">For example, if v.length() == 20, then </span>
<span class="Comment"> v.SetLength(10); v.setLength(20);</span>
<span class="Comment">will effectively clear bits 10..19 of v.</span>
<span class="Comment">This is quite different from the semantics of generic NTL vectors, where</span>
<span class="Comment">the above sequence would not change the value of v at all.</span>
<span class="Comment">One has to be aware of this difference, but it will not matter</span>
<span class="Comment">in most ordinary circumstances.</span>
<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>
<span class="Type">template</span><>
<span class="Type">class</span> Vec<GF2> {
<span class="Statement">public</span>:
Vec(); <span class="Comment">// 0 length vector</span>
Vec(INIT_SIZE_TYPE, <span class="Type">long</span> n); <span class="Comment">// initialize to length n</span>
<span class="Comment">// usage: Vec(INIT_SIZE, n)</span>
Vec(<span class="Type">const</span> Vec<GF2>& a); <span class="Comment">// copy constructor</span>
Vec& <span class="Statement">operator</span>=(<span class="Type">const</span> Vec<GF2>& a); <span class="Comment">// assignment</span>
~Vec(); <span class="Comment">// destructor</span>
<span class="Type">void</span> SetLength(<span class="Type">long</span> n); <span class="Comment">// set length to n bits</span>
<span class="Type">void</span> SetLength(<span class="Type">long</span> n, GF2 a);
<span class="Comment">// set length to n, if length increases, initialize new bits to a</span>
<span class="Type">void</span> SetMaxLength(<span class="Type">long</span> n); <span class="Comment">// allocate space for n bits</span>
<span class="Type">long</span> length() <span class="Type">const</span>; <span class="Comment">// current length, in bits</span>
<span class="Type">long</span> MaxLength() <span class="Type">const</span>; <span class="Comment">// maximum length, i.e., the maximum</span>
<span class="Comment">// value passed to either SetLength or SetMaxLength</span>
<span class="Comment">// since creation or last kill</span>
<span class="Type">long</span> allocated() <span class="Type">const</span>; <span class="Comment">// number of bits for which space is allocated;</span>
<span class="Comment">// if n <= v.allocated(), then v.SetLength(n)</span>
<span class="Comment">// will not result in any memory re-allocation.</span>
<span class="Comment">// INVARIANT: </span>
<span class="Comment">// length() <= MaxLength() <= allocated() < 2^(NTL_BITS_PER_LONG-4)</span>
<span class="Type">void</span> FixLength(<span class="Type">long</span> n); <span class="Comment">// fix length to n bits</span>
<span class="Comment">// can only be applied after default initialization or kill</span>
<span class="Type">void</span> FixAtCurrentLength();
<span class="Comment">// fixes the length at the cuurent length and prohibits</span>
<span class="Comment">// all future length changes. </span>
<span class="Comment">// It is required that length() == MaxLength() when called.</span>
<span class="Comment">// EXCEPTIONS: if length() != MaxLength() and error is raised;</span>
<span class="Comment">// Strong ES.</span>
<span class="Type">long</span> fixed() <span class="Type">const</span>; <span class="Comment">// test if length has been fixed</span>
<span class="Type">void</span> kill(); <span class="Comment">// free space and make length 0</span>
<span class="Type">const</span> GF2 get(<span class="Type">long</span> i) <span class="Type">const</span>; <span class="Comment">// fetch value at index i (indexing from 0)</span>
<span class="Type">void</span> put(<span class="Type">long</span> i, GF2 a); <span class="Comment">// write value a to index i (indexing from 0)</span>
<span class="Type">void</span> put(<span class="Type">long</span> i, <span class="Type">long</span> a);
<span class="Comment">// Here are the subscripting operators, defined using the</span>
<span class="Comment">// "helper" class ref_GF2</span>
ref_GF2 <span class="Statement">operator</span>[](<span class="Type">long</span> i);
ref_GF2 <span class="Statement">operator</span>()(<span class="Type">long</span> i);
<span class="Type">const</span> GF2 <span class="Statement">operator</span>[](<span class="Type">long</span> i) <span class="Type">const</span>;
<span class="Type">const</span> GF2 <span class="Statement">operator</span>()(<span class="Type">long</span> i) <span class="Type">const</span>;
<span class="Type">void</span> swap(Vec<GF2>& y);
<span class="Comment">// swap with y (fast: just swaps pointers)</span>
<span class="Type">void</span> append(GF2 a);
<span class="Comment">// append a to end of vector</span>
<span class="Type">void</span> append(<span class="Type">const</span> Vec<GF2>& w);
<span class="Comment">// append w to end of vector</span>
<span class="Comment">// Some partial STL compatibility...also used</span>
<span class="Comment">// to interface with the Matrix template class</span>
<span class="Type">typedef</span> GF2 value_type;
<span class="Type">typedef</span> ref_GF2 reference;
<span class="Type">typedef</span> <span class="Type">const</span> GF2 const_reference;
};
<span class="Type">void</span> swap(Vec<GF2>& x, Vec<GF2>& y);
<span class="Comment">// swap x and y (fast pointer swap)</span>
<span class="Type">void</span> append(Vec<GF2>& v, GF2 a);
<span class="Comment">// append a to v</span>
<span class="Type">void</span> append(Vec<GF2>& v, <span class="Type">const</span> Vec<GF2>& a);
<span class="Comment">// append a to v</span>
<span class="Comment">// equality operators:</span>
<span class="Type">long</span> <span class="Statement">operator</span>==(<span class="Type">const</span> Vec<GF2>& a, <span class="Type">const</span> Vec<GF2>& b);
<span class="Type">long</span> <span class="Statement">operator</span>!=(<span class="Type">const</span> Vec<GF2>& a, <span class="Type">const</span> Vec<GF2>& b);
<span class="Comment">// I/O operators:</span>
ostream& <span class="Statement">operator</span><<(ostream& s, <span class="Type">const</span> Vec<GF2>& a);
istream& <span class="Statement">operator</span>>>(istream& s, Vec<GF2>& a);
<span class="Comment">// The I/O format is [a_0 a_1 ... a_{n-1}], where each a_i is "0" or "1".</span>
<span class="Comment">// On input, the a_i may be arbitrary integers, which are reduced mod 2.</span>
<span class="Type">typedef</span> Vec<GF2> vec_GF2; <span class="Comment">// backward compatibility</span>
<span class="Comment">// utility routines:</span>
<span class="Type">void</span> clear(vec_GF2& x); <span class="Comment">// clear all bits--length unchanged</span>
<span class="Type">long</span> IsZero(<span class="Type">const</span> vec_GF2& a); <span class="Comment">// test if all bits are zero</span>
<span class="Type">void</span> shift(vec_GF2& x, <span class="Type">const</span> vec_GF2& a, <span class="Type">long</span> n);
vec_GF2 shift(<span class="Type">const</span> vec_GF2& a, <span class="Type">long</span> n);
<span class="Comment">// x = a shifted n places, where n may be positive or negative.</span>
<span class="Comment">// Generally, x[i] = a[i-n], so positive n shifts to a higher index.</span>
<span class="Comment">// The length of x is set to the length of a, and bits </span>
<span class="Comment">// are zero-filled or discarded as necessary.</span>
<span class="Type">void</span> reverse(vec_GF2& x, <span class="Type">const</span> vec_GF2& a); <span class="Comment">// c = a reversed</span>
vec_GF2 reverse(<span class="Type">const</span> vec_GF2& a);
<span class="Type">long</span> weight(<span class="Type">const</span> vec_GF2& a); <span class="Comment">// return number of 1 bits in a</span>
<span class="Type">void</span> random(vec_GF2& x, <span class="Type">long</span> n); <span class="Comment">// x = random vector of length n</span>
vec_GF2 random_vec_GF2(<span class="Type">long</span> n);
<span class="Comment">// arithmetic operations over GF(2):</span>
<span class="Type">void</span> add(vec_GF2& x, <span class="Type">const</span> vec_GF2& a, <span class="Type">const</span> vec_GF2& b);
<span class="Type">void</span> sub(vec_GF2& x, <span class="Type">const</span> vec_GF2& a, <span class="Type">const</span> vec_GF2& b);
<span class="Type">void</span> negate(vec_GF2& x, <span class="Type">const</span> vec_GF2& a);
<span class="Type">void</span> mul(vec_GF2& x, <span class="Type">const</span> vec_GF2& a, GF2 b);
<span class="Type">void</span> mul(vec_GF2& x, <span class="Type">const</span> vec_GF2& a, <span class="Type">long</span> b);
<span class="Type">void</span> mul(vec_GF2& x, GF2 a, <span class="Type">const</span> vec_GF2& b);
<span class="Type">void</span> mul(vec_GF2& x, <span class="Type">long</span> a, <span class="Type">const</span> vec_GF2& b);
<span class="Comment">// x = a * b</span>
<span class="Type">void</span> InnerProduct(ref_GF2 x, <span class="Type">const</span> vec_GF2& a, <span class="Type">const</span> vec_GF2& b);
<span class="Comment">// vectors may differ in length</span>
<span class="Type">void</span> VectorCopy(vec_GF2& x, <span class="Type">const</span> vec_GF2& a, <span class="Type">long</span> n);
vec_GF2 VectorCopy(<span class="Type">const</span> vec_GF2& a, <span class="Type">long</span> n);
<span class="Comment">// x = a copy of a of length exactly n.</span>
<span class="Comment">// The input is truncated or padded with zeroes, as necessary.</span>
<span class="Comment">// arithmetic operator notation:</span>
vec_GF2 <span class="Statement">operator</span>+(<span class="Type">const</span> vec_GF2& a, <span class="Type">const</span> vec_GF2& b);
vec_GF2 <span class="Statement">operator</span>-(<span class="Type">const</span> vec_GF2& a, <span class="Type">const</span> vec_GF2& b);
vec_GF2 <span class="Statement">operator</span>-(<span class="Type">const</span> vec_GF2& a);
<span class="Comment">// scalar mul:</span>
vec_GF2 <span class="Statement">operator</span>*(<span class="Type">const</span> vec_GF2& a, GF2 b);
vec_GF2 <span class="Statement">operator</span>*(<span class="Type">const</span> vec_GF2& a, <span class="Type">long</span> b);
vec_GF2 <span class="Statement">operator</span>*(GF2 a, <span class="Type">const</span> vec_GF2& b);
vec_GF2 <span class="Statement">operator</span>*(<span class="Type">long</span> a, <span class="Type">const</span> vec_GF2& b);
<span class="Comment">// inner product: </span>
<span class="Type">inline</span> GF2 <span class="Statement">operator</span>*(<span class="Type">const</span> vec_GF2& a, <span class="Type">const</span> vec_GF2& b);
<span class="Comment">// assignment operator notation:</span>
vec_GF2& <span class="Statement">operator</span>+=(vec_GF2& x, <span class="Type">const</span> vec_GF2& a);
vec_GF2& <span class="Statement">operator</span>-=(vec_GF2& x, <span class="Type">const</span> vec_GF2& a);
vec_GF2& <span class="Statement">operator</span>*=(vec_GF2& x, GF2 a);
vec_GF2& <span class="Statement">operator</span>*=(vec_GF2& x, <span class="Type">long</span> a);
</pre>
</body>
</html>