gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge
0 1 0
merge default
0 files changed with 1 insertions and 1 deletions:
↑ Collapse diff ↑
Ignore white space 192 line context
... ...
@@ -161,193 +161,193 @@
161 161
        initState(seedArray, seedArray + 4);
162 162
      }
163 163

	
164 164
      void initState(Word seed) {
165 165

	
166 166
        static const Word mul = RandomTraits<Word>::mul;
167 167

	
168 168
        current = state; 
169 169

	
170 170
        Word *curr = state + length - 1;
171 171
        curr[0] = seed; --curr;
172 172
        for (int i = 1; i < length; ++i) {
173 173
          curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i);
174 174
          --curr;
175 175
        }
176 176
      }
177 177

	
178 178
      template <typename Iterator>
179 179
      void initState(Iterator begin, Iterator end) {
180 180

	
181 181
        static const Word init = RandomTraits<Word>::arrayInit;
182 182
        static const Word mul1 = RandomTraits<Word>::arrayMul1;
183 183
        static const Word mul2 = RandomTraits<Word>::arrayMul2;
184 184

	
185 185

	
186 186
        Word *curr = state + length - 1; --curr;
187 187
        Iterator it = begin; int cnt = 0;
188 188
        int num;
189 189

	
190 190
        initState(init);
191 191

	
192 192
        num = length > end - begin ? length : end - begin;
193 193
        while (num--) {
194 194
          curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul1)) 
195 195
            + *it + cnt;
196 196
          ++it; ++cnt;
197 197
          if (it == end) {
198 198
            it = begin; cnt = 0;
199 199
          }
200 200
          if (curr == state) {
201 201
            curr = state + length - 1; curr[0] = state[0];
202 202
          }
203 203
          --curr;
204 204
        }
205 205

	
206 206
        num = length - 1; cnt = length - (curr - state) - 1;
207 207
        while (num--) {
208 208
          curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul2))
209 209
            - cnt;
210 210
          --curr; ++cnt;
211 211
          if (curr == state) {
212 212
            curr = state + length - 1; curr[0] = state[0]; --curr;
213 213
            cnt = 1;
214 214
          }
215 215
        }
216 216
        
217 217
        state[length - 1] = Word(1) << (bits - 1);
218 218
      }
219 219
      
220 220
      void copyState(const RandomCore& other) {
221 221
        std::copy(other.state, other.state + length, state);
222 222
        current = state + (other.current - other.state);
223 223
      }
224 224

	
225 225
      Word operator()() {
226 226
        if (current == state) fillState();
227 227
        --current;
228 228
        Word rnd = *current;
229 229
        return RandomTraits<Word>::tempering(rnd);
230 230
      }
231 231

	
232 232
    private:
233 233

	
234 234
  
235 235
      void fillState() {
236 236
        static const Word mask[2] = { 0x0ul, RandomTraits<Word>::mask };
237 237
        static const Word loMask = RandomTraits<Word>::loMask;
238 238
        static const Word hiMask = RandomTraits<Word>::hiMask;
239 239

	
240 240
        current = state + length; 
241 241

	
242 242
        register Word *curr = state + length - 1;
243 243
        register long num;
244 244
      
245 245
        num = length - shift;
246 246
        while (num--) {
247 247
          curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
248 248
            curr[- shift] ^ mask[curr[-1] & 1ul];
249 249
          --curr;
250 250
        }
251 251
        num = shift - 1;
252 252
        while (num--) {
253 253
          curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
254 254
            curr[length - shift] ^ mask[curr[-1] & 1ul];
255 255
          --curr;
256 256
        }
257
        curr[0] = (((curr[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
257
        state[0] = (((state[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
258 258
          curr[length - shift] ^ mask[curr[length - 1] & 1ul];
259 259

	
260 260
      }
261 261

	
262 262
  
263 263
      Word *current;
264 264
      Word state[length];
265 265
      
266 266
    };
267 267

	
268 268

	
269 269
    template <typename Result, 
270 270
              int shift = (std::numeric_limits<Result>::digits + 1) / 2>
271 271
    struct Masker {
272 272
      static Result mask(const Result& result) {
273 273
        return Masker<Result, (shift + 1) / 2>::
274 274
          mask(static_cast<Result>(result | (result >> shift)));
275 275
      }
276 276
    };
277 277
    
278 278
    template <typename Result>
279 279
    struct Masker<Result, 1> {
280 280
      static Result mask(const Result& result) {
281 281
        return static_cast<Result>(result | (result >> 1));
282 282
      }
283 283
    };
284 284

	
285 285
    template <typename Result, typename Word, 
286 286
              int rest = std::numeric_limits<Result>::digits, int shift = 0, 
287 287
              bool last = rest <= std::numeric_limits<Word>::digits>
288 288
    struct IntConversion {
289 289
      static const int bits = std::numeric_limits<Word>::digits;
290 290
    
291 291
      static Result convert(RandomCore<Word>& rnd) {
292 292
        return static_cast<Result>(rnd() >> (bits - rest)) << shift;
293 293
      }
294 294
      
295 295
    }; 
296 296

	
297 297
    template <typename Result, typename Word, int rest, int shift> 
298 298
    struct IntConversion<Result, Word, rest, shift, false> {
299 299
      static const int bits = std::numeric_limits<Word>::digits;
300 300

	
301 301
      static Result convert(RandomCore<Word>& rnd) {
302 302
        return (static_cast<Result>(rnd()) << shift) | 
303 303
          IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd);
304 304
      }
305 305
    };
306 306

	
307 307

	
308 308
    template <typename Result, typename Word,
309 309
              bool one_word = (std::numeric_limits<Word>::digits < 
310 310
			       std::numeric_limits<Result>::digits) >
311 311
    struct Mapping {
312 312
      static Result map(RandomCore<Word>& rnd, const Result& bound) {
313 313
        Word max = Word(bound - 1);
314 314
        Result mask = Masker<Result>::mask(bound - 1);
315 315
        Result num;
316 316
        do {
317 317
          num = IntConversion<Result, Word>::convert(rnd) & mask; 
318 318
        } while (num > max);
319 319
        return num;
320 320
      }
321 321
    };
322 322

	
323 323
    template <typename Result, typename Word>
324 324
    struct Mapping<Result, Word, false> {
325 325
      static Result map(RandomCore<Word>& rnd, const Result& bound) {
326 326
        Word max = Word(bound - 1);
327 327
        Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2>
328 328
          ::mask(max);
329 329
        Word num;
330 330
        do {
331 331
          num = rnd() & mask;
332 332
        } while (num > max);
333 333
        return num;
334 334
      }
335 335
    };
336 336

	
337 337
    template <typename Result, int exp, bool pos = (exp >= 0)>
338 338
    struct ShiftMultiplier {
339 339
      static const Result multiplier() {
340 340
        Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
341 341
        res *= res;
342 342
        if ((exp & 1) == 1) res *= static_cast<Result>(2.0);
343 343
        return res; 
344 344
      }
345 345
    };
346 346

	
347 347
    template <typename Result, int exp>
348 348
    struct ShiftMultiplier<Result, exp, false> {
349 349
      static const Result multiplier() {
350 350
        Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
351 351
        res *= res;
352 352
        if ((exp & 1) == 1) res *= static_cast<Result>(0.5);
353 353
        return res; 
0 comments (0 inline)