RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
RandomType.hpp
Go to the documentation of this file.
1 /**
2  * \file RandomType.hpp
3  * \brief Class to hold bit-width and unsigned type
4  *
5  * This provides a simple class to couple a bit-width and an unsigned type
6  * capable of holding all the bits. In addition is offers static methods for
7  * I/O and checksumming.
8  *
9  * Copyright (c) Charles Karney (2006-2011) <charles@karney.com> and licensed
10  * under the MIT/X11 License. For more information, see
11  * http://randomlib.sourceforge.net/
12  **********************************************************************/
13 #if !defined(RANDOMLIB_RANDOMTYPE_HPP)
14 #define RANDOMLIB_RANDOMTYPE_HPP 1
15 
16 #include <limits>
17 #include <string>
18 #include <iostream>
19 
20 namespace RandomLib {
21  /**
22  * \brief Class to hold bit-width and unsigned type
23  *
24  * This provides a simple class to couple a bit-width and an unsigned type
25  * capable of holding all the bits. In addition is offers static methods for
26  * I/O and checksumming.
27  *
28  * @tparam bits the number of significant bits.
29  * @tparam UIntType the C++ unsigned integer type capable of holding the bits.
30  **********************************************************************/
31  template<int bits, typename UIntType>
33  public:
34  /**
35  * The unsigned C++ type
36  **********************************************************************/
37  typedef UIntType type;
38  /**
39  * The number of significant bits
40  **********************************************************************/
41  static const unsigned width = bits;
42  /**
43  * A mask for the significant bits.
44  **********************************************************************/
45  static const type mask =
46  ~type(0) >> (std::numeric_limits<type>::digits - width);
47  /**
48  * The minimum representable value
49  **********************************************************************/
50  static const type min = type(0);
51  /**
52  * The maximum representable value
53  **********************************************************************/
54  static const type max = mask;
55  /**
56  * A combined masking and casting operation
57  *
58  * @tparam IntType the integer type of the \e x.
59  * @param[in] x the input integer.
60  * @return the masked and casted result.
61  **********************************************************************/
62  template<typename IntType> static type cast(IntType x) throw()
63  { return type(x) & mask; }
64  /**
65  * Read a data value from a stream of 32-bit quantities (binary or text)
66  *
67  * @param[in,out] is the input stream.
68  * @param[in] bin true if the stream is binary.
69  * @param[out] x the data value read from the stream.
70  **********************************************************************/
71  static void Read32(std::istream& is, bool bin, type& x);
72  /**
73  * Write the data value to a stream of 32-bit quantities (binary or text)
74  *
75  * @param[in,out] os the output stream.
76  * @param[in] bin true if the stream is binary.
77  * @param[in,out] cnt controls the use of spaces and newlines for text
78  * output.
79  * @param[in] x the data value to be written to the stream.
80  *
81  * \e cnt should be zero on the first invocation of a series of writes.
82  * This function increments it by one on each call.
83  **********************************************************************/
84  static void Write32(std::ostream& os, bool bin, int& cnt, type x);
85  /**
86  * Accumulate a checksum of a integer into a 32-bit check. This implements
87  * a very simple checksum and is intended to avoid accidental corruption
88  * only.
89  *
90  * @param[in] n the number to be included in the checksum.
91  * @param[in,out] check the running checksum.
92  **********************************************************************/
93  static void CheckSum(type n, uint32_t& check) throw();
94  };
95 
96  /**
97  * The standard unit for 32-bit quantities
98  **********************************************************************/
100  /**
101  * The standard unit for 64-bit quantities
102  **********************************************************************/
104 
105  /**
106  * The integer type of constructing bitsets. This used to be unsigned long.
107  * C++11 has made this unsigned long long.
108  **********************************************************************/
109 #if defined(_MSC_VER) && _MSC_VER >= 1600
110  typedef unsigned long long bitset_uint_t;
111 #else
112  typedef unsigned long bitset_uint_t;
113 #endif
114 
115  /// \cond SKIP
116 
117  // Accumulate a checksum of a 32-bit quantity into check
118  template<>
120  throw() {
121  // Circular shift left by one bit and add new word.
122  check = (check << 1 | (check >> 31 & Random_u32::type(1))) + n;
123  check &= Random_u32::mask;
124  }
125 
126  // Accumulate a checksum of a 64-bit quantity into check
127  template<>
129  throw() {
130  Random_u32::CheckSum(Random_u32::cast(n >> 32), check);
132  }
133  /// \endcond
134 
135 } // namespace RandomLib
136 
137 #endif // RANDOMLIB_RANDOMTYPE_HPP
unsigned long bitset_uint_t
Definition: RandomType.hpp:112
Class to hold bit-width and unsigned type.
Definition: RandomType.hpp:32
static const type mask
Definition: RandomType.hpp:45
RandomType< 32, uint32_t > Random_u32
Definition: RandomType.hpp:99
#define RANDOMLIB_EXPORT
Definition: Random.hpp:83
static void CheckSum(type n, uint32_t &check)
RandomType< 64, uint64_t > Random_u64
Definition: RandomType.hpp:103
static type cast(IntType x)
Definition: RandomType.hpp:62