1 | /* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | // vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s |
---|
3 | /* linbox/randiter/gf2.h |
---|
4 | * Copyright (C) 2003 Bradford Hovinen |
---|
5 | * |
---|
6 | * Written by Bradford Hovinen <bghovinen@math.uwaterloo.ca> |
---|
7 | * |
---|
8 | * ------------------------------------ |
---|
9 | * |
---|
10 | * See COPYING for license information. |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef __LINBOX_randiter_gf2_H |
---|
14 | #define __LINBOX_randiter_gf2_H |
---|
15 | |
---|
16 | #include <iostream> |
---|
17 | #include <vector> |
---|
18 | |
---|
19 | #include <time.h> |
---|
20 | #include "linbox/linbox-config.h" |
---|
21 | #include "linbox/integer.h" |
---|
22 | // #include "linbox/field/modular.h" |
---|
23 | #include "linbox/element/abstract.h" |
---|
24 | #include "linbox/element/envelope.h" |
---|
25 | #include "linbox/util/commentator.h" |
---|
26 | #include "linbox/randiter/mersenne-twister.h" |
---|
27 | #include "linbox/vector/bit-vector.h" |
---|
28 | |
---|
29 | #ifdef __LINBOX_XMLENABLED |
---|
30 | |
---|
31 | #include "linbox/util/xml/linbox-reader.h" |
---|
32 | #include "linbox/util/xml/linbox-writer.h" |
---|
33 | |
---|
34 | #include <iostream> |
---|
35 | #include <string> |
---|
36 | |
---|
37 | #endif |
---|
38 | |
---|
39 | namespace LinBox |
---|
40 | { |
---|
41 | |
---|
42 | class GF2RandIter { |
---|
43 | public: |
---|
44 | typedef bool Element; |
---|
45 | |
---|
46 | /** Constructor from field, sampling size, and seed. |
---|
47 | * The random field element iterator works in the field F, is seeded |
---|
48 | * by seed, and it returns any one element with probability no more |
---|
49 | * than 1/min (size, F.cardinality (c)). |
---|
50 | * A sampling size of zero means to sample from the entire field. |
---|
51 | * A seed of zero means to use some arbitrary seed for the generator. |
---|
52 | * Purely virtual. |
---|
53 | * @param F LinBox field archetype object in which to do arithmetic |
---|
54 | * @param size constant integer reference of sample size from which to |
---|
55 | * sample (default = modulus of field) |
---|
56 | * @param seed constant integer reference from which to seed random number |
---|
57 | * generator (default = 0) |
---|
58 | */ |
---|
59 | GF2RandIter (const GF2 & F, |
---|
60 | const integer &size = 0 , |
---|
61 | const integer &seed = 0) |
---|
62 | { |
---|
63 | long _seed = seed; |
---|
64 | |
---|
65 | if (_seed == 0) _seed = time (NULL); |
---|
66 | MT.setSeed ((uint32_t)_seed); |
---|
67 | } |
---|
68 | |
---|
69 | /// constructor |
---|
70 | GF2RandIter (const GF2RandIter &) {} |
---|
71 | |
---|
72 | /** Destructor. |
---|
73 | * This destructs the random field element generator object. |
---|
74 | */ |
---|
75 | ~GF2RandIter () {} |
---|
76 | |
---|
77 | /** Assignment operator. |
---|
78 | * Assigns ModularRandIter object R to generator. |
---|
79 | * @param R ModularRandIter object. |
---|
80 | */ |
---|
81 | GF2RandIter &operator = (const GF2RandIter & R) |
---|
82 | { return *this; } |
---|
83 | |
---|
84 | /** Random field element creator. |
---|
85 | * This returns a random field element from the information supplied |
---|
86 | * at the creation of the generator. |
---|
87 | * Required by abstract base class. |
---|
88 | * @return reference to random field element |
---|
89 | */ |
---|
90 | bool &random (bool &a) const |
---|
91 | { return a = MT.randomIntRange (0, 2); } |
---|
92 | |
---|
93 | /** Random field element creator. |
---|
94 | * This returns a random field element from the information supplied |
---|
95 | * at the creation of the generator. |
---|
96 | * Required by abstract base class. |
---|
97 | * @return reference to random field element |
---|
98 | */ |
---|
99 | BitVector::reference random (BitVector::reference a) const |
---|
100 | { return a = MT.randomIntRange (0, 2); } |
---|
101 | |
---|
102 | |
---|
103 | /** Random field element creator. |
---|
104 | * This returns a random field element from the information supplied |
---|
105 | * at the creation of the generator. |
---|
106 | * Required by abstract base class. |
---|
107 | * @return reference to random field element |
---|
108 | */ |
---|
109 | std::_Bit_reference random (std::_Bit_reference a) const |
---|
110 | { return a = MT.randomIntRange (0, 2); } |
---|
111 | |
---|
112 | /** Random field element creator. |
---|
113 | * This returns a random field element from the information supplied |
---|
114 | * at the creation of the generator. |
---|
115 | * Required by abstract base class. |
---|
116 | * @return reference to random field element |
---|
117 | */ |
---|
118 | ElementAbstract &random (ElementAbstract &a) const |
---|
119 | { |
---|
120 | bool tmp; |
---|
121 | |
---|
122 | random (tmp); |
---|
123 | return (a = ElementEnvelope <GF2> (tmp)); |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | uint32_t& random (uint32_t& a) const |
---|
128 | { return a = MT.randomInt(); } |
---|
129 | |
---|
130 | MersenneTwister& getMT() { return MT; } |
---|
131 | const MersenneTwister& getMT() const { return MT; } |
---|
132 | |
---|
133 | private: |
---|
134 | |
---|
135 | MersenneTwister MT; |
---|
136 | |
---|
137 | }; // class GF2RandIter |
---|
138 | |
---|
139 | } // namespace LinBox |
---|
140 | |
---|
141 | namespace LinBox |
---|
142 | { |
---|
143 | |
---|
144 | template<size_t bitsize> |
---|
145 | struct MTrandomInt { |
---|
146 | template<typename M32Twister> |
---|
147 | unsigned __LINBOX_INT32 operator() (M32Twister& MT) const |
---|
148 | { |
---|
149 | return MT.randomInt(); |
---|
150 | } |
---|
151 | }; |
---|
152 | |
---|
153 | template<> |
---|
154 | struct MTrandomInt<64> { |
---|
155 | template<typename M32Twister> |
---|
156 | unsigned __LINBOX_INT64 operator() (M32Twister& MT) const |
---|
157 | { |
---|
158 | unsigned __LINBOX_INT64 tmp = MT.randomInt(); |
---|
159 | tmp <<=32; |
---|
160 | return tmp += MT.randomInt(); |
---|
161 | } |
---|
162 | }; |
---|
163 | |
---|
164 | } // namespace LinBox |
---|
165 | |
---|
166 | #endif // __LINBOX_randiter_gf2_H |
---|
167 | |
---|