SparseLibrary  Version 1.6.0
BigInt.hpp
1 /*
2  * Copyright (c) 2007-2014, A. N. Yzelman, Utrecht University 2007-2011;
3  * KU Leuven 2011-2014.
4  * R. H. Bisseling, Utrecht University 2007-2014.
5  *
6  * This file is part of the Sparse Library.
7  *
8  * This library was developed under supervision of Prof. dr. Rob H. Bisseling at
9  * Utrecht University, from 2007 until 2011. From 2011-2014, development continued
10  * at KU Leuven, where Prof. dr. Dirk Roose contributed significantly to the ideas
11  * behind the newer parts of the library code.
12  *
13  * The Sparse Library is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by the
15  * Free Software Foundation, either version 3 of the License, or (at your
16  * option) any later version.
17  *
18  * The Sparse Library is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with the Sparse Library. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 
28 /* @file
29  * File created by:
30  * A. N. Yzelman, Dept. of Computer Science, KU Leuven, 2012.
31  */
32 
33 
34 #ifndef _H_BIG_INT
35 #define _H_BIG_INT
36 
38 struct BigInt {
39 
41  unsigned long int high;
42 
44  unsigned long int low;
45 
47  BigInt(): high( 0 ), low( 0 ) {}
48 
55  BigInt( unsigned long int a, unsigned long int b ): high( a ), low( b ) {}
56 
58  void operator=( const BigInt &other ) {
59  high = other.high;
60  low = other.low;
61  }
62 
64  operator unsigned long int() {
65  if( high == 0ul )
66  return low;
67  else
68  return high;
69  }
70 
72  operator unsigned int() {
73  if( high == 0ul )
74  return static_cast< unsigned int >( low );
75  else
76  return static_cast< unsigned int >( high );
77  }
78 
80  operator unsigned short int() {
81  if( high == 0ul )
82  return static_cast< unsigned short int >( low );
83  else
84  return static_cast< unsigned short int >( high );
85  }
86 
88  operator unsigned char() {
89  if( high == 0ul )
90  return static_cast< unsigned char >( low );
91  else
92  return static_cast< unsigned char >( high );
93  }
94 
95 };
96 
105 int bigint_compare( const void *a, const void *b ) {
106  const BigInt left = *(BigInt*)a;
107  const BigInt right = *(BigInt*)b;
108  if( left.high == right.high ) {
109  if( left.low == right.low ) {
110  return 0;
111  } else {
112  return (left.low > right.low) ? 1 : -1;
113  }
114  } else {
115  return (left.high > right.high) ? 1 : -1;
116  }
117 }
118 
126 bool operator>( const BigInt &left, const BigInt &right ) {
127  return bigint_compare( &left, &right ) > 0;
128 }
129 
137 bool operator<( const BigInt &left, const BigInt &right ) {
138  return bigint_compare( &left, &right ) < 0;
139 }
140 
148 bool operator==( const BigInt &left, const BigInt &right ) {
149  return left.high == right.high && left.low == right.low;
150 }
151 
159 bool operator!=( const BigInt &left, const BigInt &right ) {
160  return left.high != right.high || left.low != right.low;
161 }
162 
170 bool operator>=( const BigInt &left, const BigInt &right ) {
171  return left == right || ( left > right );
172 }
173 
181 bool operator<=( const BigInt &left, const BigInt &right ) {
182  return left == right || ( left < right );
183 }
184 
185 #endif
186 
void operator=(const BigInt &other)
Assignment operator.
Definition: BigInt.hpp:58
BigInt()
Default constructor (sets fields to zero).
Definition: BigInt.hpp:47
unsigned long int high
Most significant bits.
Definition: BigInt.hpp:41
A 128-bit integer, with overloaded comparison operators.
Definition: BigInt.hpp:38
BigInt(unsigned long int a, unsigned long int b)
Direct constructor.
Definition: BigInt.hpp:55
unsigned long int low
Least significant bits.
Definition: BigInt.hpp:44