StatMech
Loading...
Searching...
No Matches
BaseConverter< Integer > Class Template Reference

Provides utilities for converting an integer to the corresponding expression in the positional notation with given base and number of digits. More...

#include <BaseNnumber.hpp>

Collaboration diagram for BaseConverter< Integer >:
Collaboration graph

Public Member Functions

__host__ __device__ BaseConverter ()
 
__host__ __device__ BaseConverter (Integer Base, Integer Length)
 Construct a new BaseConverter object.
 
__host__ __device__ BaseConverter (BaseConverter< Integer > const &other)
 
__host__ void status (void) const
 Show the status of the instance.
 
__host__ __device__ Integer base () const
 
__host__ __device__ Integer length () const
 
__host__ __device__ Integer maxVal () const
 
__host__ __device__ Integer value () const
 
template<typename Input , typename std::enable_if_t< !std::is_integral_v< Input > > * = nullptr>
__host__ __device__ BaseConverter const & set (Input config) const
 
template<typename Input >
__host__ __device__ BaseConverter const & set (Input num) const
 
__host__ __device__ BaseConvertersetParam (Integer Base, Integer Length)
 Set the base and the length of the positional notation to which the input number will converted.
 
__host__ __device__ Integer digit (Integer const num, Integer const pos, Integer const base, Integer const length) const
 Returns the (pos)-th digit of the input (num) in given base and length.
 
__host__ __device__ Integer digit (Integer const num, Integer const pos) const
 
__host__ __device__ Integer digit (Integer const pos) const
 
__host__ __device__ Integer shiftDigits (Integer const num, Integer const trans, Integer const base, Integer const length) const
 Shifts a number (num) to right by (trans) in terms of the positional notation with given base and length under the periodic boundary condition.
 
__host__ __device__ Integer shiftDigits (Integer const num, Integer const trans) const
 
__host__ __device__ BaseConverter const & shiftDigits (Integer const trans) const
 
template<typename Input >
__host__ __device__ Integer digitsToNumber (Input const &digits, Integer const base, Integer const length) const
 Converts an array of integers to the corresponding integer in terms of the positional notation with given (base) and (length)
 
template<typename Input >
__host__ __device__ Integer digitsToNumber (Input const &digits) const
 
template<typename Output >
__host__ __device__ auto printInDigits (Output &output, Integer const num, Integer const base, Integer const length) const -> typename std::enable_if< std::is_integral< Output >::value==false, BaseConverter const & >::type
 Expresses an integer (num) in the positional notation with given (base) and (length)
 
template<typename Output >
__host__ __device__ auto printInDigits (Output &output, Integer const num) const -> typename std::enable_if< std::is_integral< Output >::value==false, BaseConverter const & >::type
 
template<typename Output , typename std::enable_if_t< !std::is_integral_v< Output > > * = nullptr>
__host__ __device__ BaseConverter const & printInDigits (Output &output) const
 
__host__ BaseConverter const & printInDigits (Integer const num, Integer const base, Integer const length) const
 
__host__ BaseConverter const & printInDigits (Integer const num) const
 
__host__ BaseConverter const & printInDigits () const
 

Private Attributes

Integer m_base = 0
 
Integer m_length = 0
 
Integer m_max = 0
 
Integer m_register
 

Detailed Description

template<typename Integer = int>
class BaseConverter< Integer >

Provides utilities for converting an integer to the corresponding expression in the positional notation with given base and number of digits.

Template Parameters
IntegerInteger class.

Constructor & Destructor Documentation

◆ BaseConverter() [1/3]

template<typename Integer = int>
__host__ __device__ BaseConverter< Integer >::BaseConverter ( )
inline
27{}

◆ BaseConverter() [2/3]

template<typename Integer = int>
__host__ __device__ BaseConverter< Integer >::BaseConverter ( Integer  Base,
Integer  Length 
)
inline

Construct a new BaseConverter object.

Parameters
[in]Base
[in]Length
35 : m_base(Base), m_length(Length), m_max(powi(Base, Length)) {}
Integer m_max
Definition BaseNnumber.hpp:23
Integer m_length
Definition BaseNnumber.hpp:22
Integer m_base
Definition BaseNnumber.hpp:21

◆ BaseConverter() [3/3]

template<typename Integer = int>
__host__ __device__ BaseConverter< Integer >::BaseConverter ( BaseConverter< Integer > const &  other)
inline
38 : m_base(other.base()), m_length(other.length()), m_max(other.maxVal()) {}
__host__ __device__ Integer maxVal() const
Definition BaseNnumber.hpp:57
__host__ __device__ Integer base() const
Definition BaseNnumber.hpp:55
__host__ __device__ Integer length() const
Definition BaseNnumber.hpp:56

Member Function Documentation

◆ base()

template<typename Integer = int>
__host__ __device__ Integer BaseConverter< Integer >::base ( ) const
inline
55{ return this->m_base; }

◆ digit() [1/3]

template<typename Integer = int>
__host__ __device__ Integer BaseConverter< Integer >::digit ( Integer const  num,
Integer const  pos 
) const
inline
119 {
120 return this->digit(num, pos, m_base, m_length);
121 }
__host__ __device__ Integer digit(Integer const num, Integer const pos, Integer const base, Integer const length) const
Returns the (pos)-th digit of the input (num) in given base and length.
Definition BaseNnumber.hpp:113

◆ digit() [2/3]

template<typename Integer = int>
__host__ __device__ Integer BaseConverter< Integer >::digit ( Integer const  num,
Integer const  pos,
Integer const  base,
Integer const  length 
) const
inline

Returns the (pos)-th digit of the input (num) in given base and length.

Parameters
[in]num[default = this->value()] An integer the (pos)-th digit of which is to be returned.
[in]posThe position of digit starting from zero. Negative value specifies the position in the reverse order. For example, setting pos=-1 returns the (length-1)-th digit.
[in]base[default = this->base()] Base of the positional notation.
[in]length[default = this->length()] Length of the positional notation with respect to with a negative value of (pos) is interpreted.
Returns
Integer The (pos)-th digit of (num) in the positional notation with given (base) and (length).
114 {
115 Integer Pos
116 = (pos % length + length) % length; // taking care of negative values of 'trans'
117 return (num / powi(base, Pos % length)) % base;
118 }

◆ digit() [3/3]

template<typename Integer = int>
__host__ __device__ Integer BaseConverter< Integer >::digit ( Integer const  pos) const
inline
122 {
123 return this->digit(m_register, pos, m_base, m_length);
124 }
Integer m_register
Definition BaseNnumber.hpp:24

◆ digitsToNumber() [1/2]

template<typename Integer = int>
template<typename Input >
__host__ __device__ Integer BaseConverter< Integer >::digitsToNumber ( Input const &  digits) const
inline
176 {
177 return this->digitsToNumber(digits, m_base, m_length);
178 }
__host__ __device__ Integer digitsToNumber(Input const &digits, Integer const base, Integer const length) const
Converts an array of integers to the corresponding integer in terms of the positional notation with g...
Definition BaseNnumber.hpp:166

◆ digitsToNumber() [2/2]

template<typename Integer = int>
template<typename Input >
__host__ __device__ Integer BaseConverter< Integer >::digitsToNumber ( Input const &  digits,
Integer const  base,
Integer const  length 
) const
inline

Converts an array of integers to the corresponding integer in terms of the positional notation with given (base) and (length)

Template Parameters
OutputContainer of Integers
Parameters
[in]digitsAn array of Integers representing a number in the positional notation with given (base) and (length)
[in]base[default = this->base()] Base of the positional notation.
[in]length[default = this->length()] Length of the positional notation.
Returns
Integer
167 {
168 Integer res = 0, p = 1;
169 for(int l = length - 1; l >= 0; --l) {
170 res += p * digits[l];
171 p *= base;
172 }
173 return res;
174 }

◆ length()

template<typename Integer = int>
__host__ __device__ Integer BaseConverter< Integer >::length ( ) const
inline
56{ return this->m_length; }

◆ maxVal()

template<typename Integer = int>
__host__ __device__ Integer BaseConverter< Integer >::maxVal ( ) const
inline
57{ return this->m_max; }

◆ printInDigits() [1/6]

template<typename Integer = int>
__host__ BaseConverter const & BaseConverter< Integer >::printInDigits ( ) const
inline
245 {
247 return *this;
248 }
__host__ BaseConverter const & printInDigits() const
Definition BaseNnumber.hpp:245

◆ printInDigits() [2/6]

template<typename Integer = int>
__host__ BaseConverter const & BaseConverter< Integer >::printInDigits ( Integer const  num) const
inline
240 {
241 // static_assert(std::is_integral<Input>::value == true);
242 this->printInDigits(num, m_base, m_length);
243 return *this;
244 }

◆ printInDigits() [3/6]

template<typename Integer = int>
__host__ BaseConverter const & BaseConverter< Integer >::printInDigits ( Integer const  num,
Integer const  base,
Integer const  length 
) const
inline
227 {
228 if(num >= powi(base, length)) {
229 std::cerr << "Warning(" << __func__ << "): Input(" << num
230 << ") cannot be expressed with base " << base << " and length " << length
231 << "." << std::endl;
232 }
233 for(int l = length - 1; l >= 0; --l) {
234 std::cout << this->digit(num, l, base, length) << " ";
235 }
236 std::cout << std::endl;
237 return *this;
238 }

◆ printInDigits() [4/6]

template<typename Integer = int>
template<typename Output , typename std::enable_if_t< !std::is_integral_v< Output > > * = nullptr>
__host__ __device__ BaseConverter const & BaseConverter< Integer >::printInDigits ( Output &  output) const
inline
220 {
221 static_assert(std::is_integral<Output>::value == false);
222 this->printInDigits(output, m_register, m_base, m_length);
223 return *this;
224 }

◆ printInDigits() [5/6]

template<typename Integer = int>
template<typename Output >
__host__ __device__ auto BaseConverter< Integer >::printInDigits ( Output &  output,
Integer const  num 
) const -> typename std::enable_if< std::is_integral<Output>::value == false, BaseConverter const&>::type
inline
213 {
214 static_assert(std::is_integral<Output>::value == false);
215 this->printInDigits(output, num, m_base, m_length);
216 return *this;
217 }

◆ printInDigits() [6/6]

template<typename Integer = int>
template<typename Output >
__host__ __device__ auto BaseConverter< Integer >::printInDigits ( Output &  output,
Integer const  num,
Integer const  base,
Integer const  length 
) const -> typename std::enable_if< std::is_integral<Output>::value == false, BaseConverter const&>::type
inline

Expresses an integer (num) in the positional notation with given (base) and (length)

Template Parameters
OutputContainer of Integers
Parameters
[out]output[default = std::cout] An array to which the resulting expression is stored.
[in]num[default = this->value()] An integer to be expressed in the positional notation with given (base) and (length)
[in]base[default = this->base()] Base of the positional notation.
[in]length[default = this->length()] Length of the positional notation.
Returns
BaseConverter& Reference to the instance itself
194 {
195 static_assert(std::is_integral<Output>::value == false);
196 if(num >= powi(base, length)) {
197#ifndef __CUDA_ARCH__
198 std::cerr << "Warning:" << __PRETTY_FUNCTION__ << "\n\t"
199 << "Input(" << num
200 << ") cannot be expressed with base " << base << " and length " << length
201 << "." << std::endl;
202#endif
203 }
204 if(output.size() < length) output.resize(length);
205 for(int l = 0; l < length; ++l) {
206 output[l] = this->digit(num, length - l - 1, base, length);
207 }
208 return *this;
209 }

◆ set() [1/2]

template<typename Integer = int>
template<typename Input , typename std::enable_if_t< !std::is_integral_v< Input > > * = nullptr>
__host__ __device__ BaseConverter const & BaseConverter< Integer >::set ( Input  config) const
inline
Parameters
numAn integer to be held by the instance
digitsAn array of integers which is to be held by the instance after converted to an integer in terms of the positional notation with base=this->base() and length=this->length()
Returns
BaseConverter& Reference to the instance itself
72 {
73 static_assert(std::is_integral<Input>::value == false);
74 Integer num = 0, base = 1;
75 for(Integer pos = 0; pos < m_length; ++pos) {
76 num += config.at(m_length - 1 - pos) * base;
77 base *= m_base;
78 }
79 this->m_register = num;
80 return *this;
81 }

◆ set() [2/2]

template<typename Integer = int>
template<typename Input >
__host__ __device__ BaseConverter const & BaseConverter< Integer >::set ( Input  num) const
inline
83 {
84 static_assert(std::is_integral<Input>::value == true);
85 this->m_register = num;
86 return *this;
87 }

◆ setParam()

template<typename Integer = int>
__host__ __device__ BaseConverter & BaseConverter< Integer >::setParam ( Integer  Base,
Integer  Length 
)
inline

Set the base and the length of the positional notation to which the input number will converted.

Parameters
BaseBase of the positional notation to which the number will converted
LengthNumber of digits in the resulting expression
94 {
95 this->m_base = Base;
96 this->m_length = Length;
97 this->m_max = powi(Base, Length);
98 return *this;
99 }

◆ shiftDigits() [1/3]

template<typename Integer = int>
__host__ __device__ Integer BaseConverter< Integer >::shiftDigits ( Integer const  num,
Integer const  trans 
) const
inline
147 {
148 return this->shiftDigits(num, trans, m_base, m_length);
149 }
__host__ __device__ Integer shiftDigits(Integer const num, Integer const trans, Integer const base, Integer const length) const
Shifts a number (num) to right by (trans) in terms of the positional notation with given base and len...
Definition BaseNnumber.hpp:139

◆ shiftDigits() [2/3]

template<typename Integer = int>
__host__ __device__ Integer BaseConverter< Integer >::shiftDigits ( Integer const  num,
Integer const  trans,
Integer const  base,
Integer const  length 
) const
inline

Shifts a number (num) to right by (trans) in terms of the positional notation with given base and length under the periodic boundary condition.

Parameters
[in]num[default = this->value()] An integer to be shifted.
[in]transAmount of the shift to right. The j-th digit is to be shifted to [(j+trans) % length]-th digit. Negative value means a shift in the reverse order (i.e. to left).
[in]base[default = this->base()] Base of the positional notation.
[in]length[default = this->length()] Length of the positional notation with respect to with a negative value of (trans) is interpreted.
Returns
Integer An integer corresponding to the shifted notation.
140 {
141 Integer Trans
142 = (trans % length + length) % length; // taking care of negative values of 'trans'
143 Integer temp = powi(base, Trans);
144 return (num / temp) + (num % temp) * powi(base, length - Trans);
145 }

◆ shiftDigits() [3/3]

template<typename Integer = int>
__host__ __device__ BaseConverter const & BaseConverter< Integer >::shiftDigits ( Integer const  trans) const
inline
151 {
152 this->m_register = this->shiftDigits(m_register, trans, m_base, m_length);
153 return *this;
154 }

◆ status()

template<typename Integer = int>
__host__ void BaseConverter< Integer >::status ( void  ) const
inline

Show the status of the instance.

  • Base: Base of the positional notation to which the input number will converted
  • Length: Number of digits in the resulting expression
  • Max: The maximum integer representable with given Base and Length
  • Registered value: An integer currently held by the instance
47 {
48 std::cout << "baseConverter.status()"
49 << "\n"
50 << "\tBase: " << this->m_base << "\n"
51 << "\tLength: " << this->m_length << "\n"
52 << "\tMax: " << this->m_max - 1 << "\n"
53 << "\tRegistered value: " << this->m_register << std::endl;
54 }

◆ value()

template<typename Integer = int>
__host__ __device__ Integer BaseConverter< Integer >::value ( ) const
inline
58{ return this->m_register; }

Member Data Documentation

◆ m_base

template<typename Integer = int>
Integer BaseConverter< Integer >::m_base = 0
private

◆ m_length

template<typename Integer = int>
Integer BaseConverter< Integer >::m_length = 0
private

◆ m_max

template<typename Integer = int>
Integer BaseConverter< Integer >::m_max = 0
private

◆ m_register

template<typename Integer = int>
Integer BaseConverter< Integer >::m_register
mutableprivate

The documentation for this class was generated from the following file: