File: xorshift64star.h

package info (click to toggle)
contextfree 3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,260 kB
  • sloc: cpp: 37,992; lex: 414; makefile: 123; sh: 43; python: 34
file content (148 lines) | stat: -rw-r--r-- 4,603 bytes parent folder | download
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
// xorshift64star.h
// this file is part of Context Free
// ---------------------
// Copyright (C) 2015 John Horigan - john@glyphic.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// John Horigan can be contacted at john@glyphic.com or at
// John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
//


#ifndef INCLUDE_XORSHIFT64STAR_H
#define INCLUDE_XORSHIFT64STAR_H

#include <cstdint>
#include <limits>
#include <random>
#include <istream>
#include <ostream>

#if defined(_MSC_VER) && _MSC_VER < 1900
#define myConstExpr
#else
#define myConstExpr constexpr
#endif

// This class  implements the xorshift64* PRNG.
class XORshift64star {
public:
    using result_type = std::uint64_t;
    
    enum e_consts : result_type {
        RAND64_MULT = 2685821657736338717ULL,
        RAND64_SEED = 0x3DF41234ABCD330EULL
    };
    
    result_type mSeed;
    
    explicit XORshift64star(result_type _s = RAND64_SEED) : mSeed(_s ? _s : RAND64_SEED) { }
    template <class SSeq>
    explicit XORshift64star(SSeq& q) { seed(q); }
    
    void seed(result_type _s = RAND64_SEED) { mSeed = _s ? _s : RAND64_SEED; }
    template <class SSeq>
    void seed(SSeq& q)
    {
        std::uint32_t seeds[2];
        q.generate(seeds, seeds+2);
        mSeed = static_cast<result_type>(seeds[0]) << 32 |
                static_cast<result_type>(seeds[1]);
        if (mSeed == 0)
            mSeed = RAND64_SEED;
    }
    
    void discard(unsigned long long z)
    {
        for (unsigned long long i = 0; i < z; ++i) {
            // This is the xorshift64* PRNG.
            mSeed ^= mSeed >> 12;
            mSeed ^= mSeed << 25;
            mSeed ^= mSeed >> 27;
        }
    }
    
    result_type operator()()
    {
        result_type result = mSeed * RAND64_MULT;
        discard(1);
        return result;
    }
    
    static myConstExpr result_type min() { return 1; }
    static myConstExpr result_type max() { return static_cast<result_type>(-1LL); }
    
    bool operator==(const XORshift64star& rhs) const { return mSeed == rhs.mSeed; }
    bool operator!=(const XORshift64star& rhs) const { return mSeed != rhs.mSeed; }
    
    
    template <class CharT, class Traits>
    friend std::basic_ostream<CharT,Traits>& operator<<(std::basic_ostream<CharT,Traits>& ost, const XORshift64star& rhs);
    template <class CharT, class Traits>
    friend std::basic_istream<CharT,Traits>& operator>>(std::basic_istream<CharT,Traits>& ist, XORshift64star& rhs);
    
private:
    template <class _CharT, class _Traits>
    class _save_flags
    {
        using _stream_type = std::basic_ios<_CharT, _Traits>;
        using fmtflags = typename _stream_type::fmtflags;
        
        _stream_type& _stream_;
        fmtflags       _fmtflags_;
        _CharT         _fill_;
        
        _save_flags(const _save_flags&);
        _save_flags& operator=(const _save_flags&);
    public:
        explicit _save_flags(_stream_type& _stream)
        : _stream_(_stream),
        _fmtflags_(_stream.flags()),
        _fill_(_stream.fill())
        {}
        ~_save_flags()
        {
            _stream_.flags(_fmtflags_);
            _stream_.fill(_fill_);
        }
    };
};

template <class CharT, class Traits>
std::basic_ostream<CharT,Traits>& operator<<(std::basic_ostream<CharT,Traits>& ost, const XORshift64star& rhs)
{
    XORshift64star::_save_flags<CharT, Traits> lx(ost);
    ost.flags(std::ios_base::dec | std::ios_base::left);
    ost.fill(ost.widen(' '));
    return ost << rhs.mSeed;
}

template <class CharT, class Traits>
std::basic_istream<CharT,Traits>& operator>>(std::basic_istream<CharT,Traits>& ist, XORshift64star& rhs)
{
    XORshift64star::_save_flags<CharT, Traits> lx(ist);
    ist.flags(std::ios_base::dec | std::ios_base::skipws);
    XORshift64star::result_type tmp;
    ist >> tmp;
    if (!ist.fail())
        rhs.mSeed = tmp;
    return ist;
}

#endif  // INCLUDE_XORSHIFT64STAR_H