yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
BigUnsignedInABase Class Reference

#include <BigUnsignedInABase.hh>

+ Inheritance diagram for BigUnsignedInABase:
+ Collaboration diagram for BigUnsignedInABase:

Public Types

typedef unsigned short Digit
 
typedef Digit Base
 

Public Member Functions

 BigUnsignedInABase ()
 
 BigUnsignedInABase (const BigUnsignedInABase &x)
 
void operator= (const BigUnsignedInABase &x)
 
 BigUnsignedInABase (const Digit *d, Index l, Base base)
 
 ~BigUnsignedInABase ()
 
 BigUnsignedInABase (const BigUnsigned &x, Base base)
 
 operator BigUnsigned () const
 
 operator std::string () const
 
 BigUnsignedInABase (const std::string &s, Base base)
 
Base getBase () const
 
Digit getDigit (Index i) const
 
bool isZero () const
 
bool operator== (const BigUnsignedInABase &x) const
 
bool operator!= (const BigUnsignedInABase &x) const
 

Protected Types

typedef unsigned int Index
 

Protected Member Functions

 BigUnsignedInABase (int, Index c)
 
void zapLeadingZeros ()
 
void allocate (Index c)
 
void allocateAndCopy (Index c)
 
Index getCapacity () const
 
Index getLength () const
 
unsigned short getBlock (Index i) const
 
bool isEmpty () const
 
bool operator== (const NumberlikeArray< unsigned short > &x) const
 
bool operator!= (const NumberlikeArray< unsigned short > &x) const
 

Protected Attributes

Base base
 
Index cap
 
Index len
 
unsigned short * blk
 

Static Protected Attributes

static const unsigned int N
 

Detailed Description

Definition at line 32 of file BigUnsignedInABase.hh.

Member Typedef Documentation

Definition at line 38 of file BigUnsignedInABase.hh.

typedef unsigned short BigUnsignedInABase::Digit

Definition at line 36 of file BigUnsignedInABase.hh.

typedef unsigned int NumberlikeArray< unsigned short >::Index
inherited

Definition at line 25 of file NumberlikeArray.hh.

Constructor & Destructor Documentation

BigUnsignedInABase::BigUnsignedInABase ( int  ,
Index  c 
)
inlineprotected

Definition at line 45 of file BigUnsignedInABase.hh.

BigUnsignedInABase::BigUnsignedInABase ( )
inline
BigUnsignedInABase::BigUnsignedInABase ( const BigUnsignedInABase x)
inline
BigUnsignedInABase::BigUnsignedInABase ( const Digit d,
Index  l,
Base  base 
)

Definition at line 3 of file BigUnsignedInABase.cc.

5  // Check the base
6  if (base < 2)
7  throw "BigUnsignedInABase::BigUnsignedInABase(const Digit *, Index, Base): The base must be at least 2";
8 
9  // Validate the digits.
10  for (Index i = 0; i < l; i++)
11  if (blk[i] >= base)
12  throw "BigUnsignedInABase::BigUnsignedInABase(const Digit *, Index, Base): A digit is too large for the specified base";
13 
14  // Eliminate any leading zeros we may have been passed.
16 }

+ Here is the call graph for this function:

BigUnsignedInABase::~BigUnsignedInABase ( )
inline

Definition at line 70 of file BigUnsignedInABase.hh.

70 {}
BigUnsignedInABase::BigUnsignedInABase ( const BigUnsigned x,
Base  base 
)

Definition at line 32 of file BigUnsignedInABase.cc.

32  {
33  // Check the base
34  if (base < 2)
35  throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2";
36  this->base = base;
37 
38  // Get an upper bound on how much space we need
39  int maxBitLenOfX = x.getLength() * BigUnsigned::N;
40  int minBitsPerDigit = bitLen(base) - 1;
41  int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
42  len = maxDigitLenOfX; // Another change to comply with `staying in bounds'.
43  allocate(len); // Get the space
44 
45  BigUnsigned x2(x), buBase(base);
46  Index digitNum = 0;
47 
48  while (!x2.isZero()) {
49  // Get last digit. This is like `lastDigit = x2 % buBase, x2 /= buBase'.
50  BigUnsigned lastDigit(x2);
51  lastDigit.divideWithRemainder(buBase, x2);
52  // Save the digit.
53  blk[digitNum] = lastDigit.toUnsignedShort();
54  // Move on. We can't run out of room: we figured it out above.
55  digitNum++;
56  }
57 
58  // Save the actual length.
59  len = digitNum;
60 }
Index getLength() const
static const unsigned int N

+ Here is the call graph for this function:

BigUnsignedInABase::BigUnsignedInABase ( const std::string &  s,
Base  base 
)

Definition at line 73 of file BigUnsignedInABase.cc.

73  {
74  // Check the base.
75  if (base > 36)
76  throw "BigUnsignedInABase(std::string, Base): The default string conversion routines use the symbol set 0-9, A-Z and therefore support only up to base 36. You tried a conversion with a base over 36; write your own string conversion routine.";
77  // Save the base.
78  // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
79  this->base = base;
80 
81  // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index',
82  // also known as an `unsigned int'. Some compilers warn without this cast.
83  len = Index(s.length());
84  allocate(len);
85 
86  Index digitNum, symbolNumInString;
87  for (digitNum = 0; digitNum < len; digitNum++) {
88  symbolNumInString = len - 1 - digitNum;
89  char theSymbol = s[symbolNumInString];
90  if (theSymbol >= '0' && theSymbol <= '9')
91  blk[digitNum] = theSymbol - '0';
92  else if (theSymbol >= 'A' && theSymbol <= 'Z')
93  blk[digitNum] = theSymbol - 'A' + 10;
94  else if (theSymbol >= 'a' && theSymbol <= 'z')
95  blk[digitNum] = theSymbol - 'a' + 10;
96  else
97  throw "BigUnsignedInABase(std::string, Base): Bad symbol in input. Only 0-9, A-Z, a-z are accepted.";
98 
99  if (blk[digitNum] >= base)
100  throw "BigUnsignedInABase::BigUnsignedInABase(const Digit *, Index, Base): A digit is too large for the specified base";
101  }
102  zapLeadingZeros();
103 }

+ Here is the call graph for this function:

Member Function Documentation

void NumberlikeArray< unsigned short >::allocate ( Index  c)
inherited

+ Here is the caller graph for this function:

void NumberlikeArray< unsigned short >::allocateAndCopy ( Index  c)
inherited
Base BigUnsignedInABase::getBase ( ) const
inline

Definition at line 100 of file BigUnsignedInABase.hh.

100 { return base; }
unsigned short NumberlikeArray< unsigned short >::getBlock ( Index  i) const
inlineinherited

Definition at line 74 of file NumberlikeArray.hh.

74 { return blk[i]; }
Index NumberlikeArray< unsigned short >::getCapacity ( ) const
inlineinherited

Definition at line 72 of file NumberlikeArray.hh.

Digit BigUnsignedInABase::getDigit ( Index  i) const
inline

Definition at line 108 of file BigUnsignedInABase.hh.

108 { return i >= len ? 0 : blk[i]; }
Index NumberlikeArray< unsigned short >::getLength ( ) const
inlineinherited

Definition at line 73 of file NumberlikeArray.hh.

bool NumberlikeArray< unsigned short >::isEmpty ( ) const
inlineinherited

Definition at line 75 of file NumberlikeArray.hh.

75 { return len == 0; }
bool BigUnsignedInABase::isZero ( ) const
inline

Definition at line 111 of file BigUnsignedInABase.hh.

bool isEmpty() const

+ Here is the call graph for this function:

BigUnsignedInABase::operator BigUnsigned ( ) const

Definition at line 62 of file BigUnsignedInABase.cc.

62  {
63  BigUnsigned ans(0), buBase(base), temp;
64  Index digitNum = len;
65  while (digitNum > 0) {
66  digitNum--;
67  temp.multiply(ans, buBase);
68  ans.add(temp, BigUnsigned(blk[digitNum]));
69  }
70  return ans;
71 }
void multiply(const BigUnsigned &a, const BigUnsigned &b)
Definition: BigUnsigned.cc:300

+ Here is the call graph for this function:

BigUnsignedInABase::operator std::string ( ) const

Definition at line 105 of file BigUnsignedInABase.cc.

105  {
106  if (base > 36)
107  throw "BigUnsignedInABase ==> std::string: The default string conversion routines use the symbol set 0-9, A-Z and therefore support only up to base 36. You tried a conversion with a base over 36; write your own string conversion routine.";
108  if (len == 0)
109  return std::string("0");
110  // Some compilers don't have push_back, so use a char * buffer instead.
111  char *s = new char[len + 1];
112  s[len] = '\0';
113  Index digitNum, symbolNumInString;
114  for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++) {
115  digitNum = len - 1 - symbolNumInString;
116  Digit theDigit = blk[digitNum];
117  if (theDigit < 10)
118  s[symbolNumInString] = char('0' + theDigit);
119  else
120  s[symbolNumInString] = char('A' + theDigit - 10);
121  }
122  std::string s2(s);
123  delete [] s;
124  return s2;
125 }
bool NumberlikeArray< unsigned short >::operator!= ( const NumberlikeArray< unsigned short > &  x) const
inlineinherited

Definition at line 82 of file NumberlikeArray.hh.

82  {
83  return !operator ==(x);
84  }
bool operator==(const NumberlikeArray< unsigned short > &x) const

+ Here is the call graph for this function:

bool BigUnsignedInABase::operator!= ( const BigUnsignedInABase x) const
inline

Definition at line 118 of file BigUnsignedInABase.hh.

118 { return !operator ==(x); }
bool operator==(const BigUnsignedInABase &x) const

+ Here is the call graph for this function:

void BigUnsignedInABase::operator= ( const BigUnsignedInABase x)
inline

Definition at line 61 of file BigUnsignedInABase.hh.

61  {
63  base = x.base;
64  }
void operator=(const NumberlikeArray< Blk > &x)

+ Here is the call graph for this function:

bool NumberlikeArray< unsigned short >::operator== ( const NumberlikeArray< unsigned short > &  x) const
inherited
bool BigUnsignedInABase::operator== ( const BigUnsignedInABase x) const
inline

Definition at line 115 of file BigUnsignedInABase.hh.

115  {
117  }
bool operator==(const NumberlikeArray< Blk > &x) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void BigUnsignedInABase::zapLeadingZeros ( )
inlineprotected

Definition at line 48 of file BigUnsignedInABase.hh.

48  {
49  while (len > 0 && blk[len - 1] == 0)
50  len--;
51  }

+ Here is the caller graph for this function:

Field Documentation

Base BigUnsignedInABase::base
protected

Definition at line 42 of file BigUnsignedInABase.hh.

unsigned short * NumberlikeArray< unsigned short >::blk
inherited

Definition at line 34 of file NumberlikeArray.hh.

Index NumberlikeArray< unsigned short >::cap
inherited

Definition at line 30 of file NumberlikeArray.hh.

Index NumberlikeArray< unsigned short >::len
inherited

Definition at line 32 of file NumberlikeArray.hh.

const unsigned int NumberlikeArray< unsigned short >::N
staticinherited

Definition at line 27 of file NumberlikeArray.hh.


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