LAL: Linear Arrangement Library 21.07.01
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
integer.hpp
1/*********************************************************************
2 *
3 * Linear Arrangement Library - A library that implements a collection
4 * algorithms for linear arrangments of graphs.
5 *
6 * Copyright (C) 2019 - 2021
7 *
8 * This file is part of Linear Arrangement Library. To see the full code
9 * visit the webpage:
10 * https://github.com/lluisalemanypuig/linear-arrangement-library.git
11 *
12 * Linear Arrangement Library is free software: you can redistribute it
13 * and/or modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * Linear Arrangement Library is distributed in the hope that it will be
18 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with Linear Arrangement Library. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Contact:
26 *
27 * LluĂ­s Alemany Puig (lalemany@cs.upc.edu)
28 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
29 * CQL (Complexity and Quantitative Linguistics Lab)
30 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
31 * Webpage: https://cqllab.upc.edu/people/lalemany/
32 *
33 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office S124, Omega building
37 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
38 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
39 *
40 ********************************************************************/
41
42#pragma once
43
44// gmp includes
45#include <gmp.h>
46
47// C++ includes
48#include <cstdint>
49#include <string>
50
51namespace lal {
52namespace numeric {
53
60class integer {
61public:
62 friend class rational;
63public:
64 /* CONSTRUCTORS */
65
67 integer() noexcept { mpz_init(m_val); }
69 integer(mpz_t&& raw);
74 integer(int64_t i) noexcept { mpz_init_set_si(m_val, i); }
79 integer(const std::string& s) noexcept { mpz_init_set_str(m_val, s.c_str(), 10); }
80#ifndef SWIG
85 integer(integer&& i) noexcept;
86#endif
91 integer(const integer& i) noexcept { mpz_init_set(m_val, i.m_val); }
93 ~integer() noexcept { mpz_clear(m_val); }
94
95 /* SETTERS */
96
101 inline void set_si(int64_t i) noexcept {
102 if (not is_initialized()) { mpz_init(m_val); }
103 mpz_set_si(m_val, i);
104 m_initialized = true;
105 }
110 inline void set_ui(uint64_t i) noexcept {
111 if (not is_initialized()) { mpz_init(m_val); }
112 mpz_set_ui(m_val, i);
113 m_initialized = true;
114 }
119 inline void set_integer(const integer& i) noexcept {
120 if (not is_initialized()) { mpz_init(m_val); }
121 mpz_set(m_val, i.m_val);
122 m_initialized = true;
123 }
128 inline void set_str(const std::string& s) noexcept {
129 if (not is_initialized()) { mpz_init(m_val); }
130 mpz_set_str(m_val, s.c_str(), 10);
131 m_initialized = true;
132 }
133
134 /* OPERATORS */
135
136 // -- ASSIGNMENT
137
138#ifndef SWIG
143 inline integer& operator= (int64_t i) noexcept {
144 set_si(i);
145 return *this;
146 }
151 inline integer& operator= (const integer& i) noexcept {
152 set_integer(i);
153 return *this;
154 }
159 integer& operator= (integer&& i) noexcept;
160#endif
161
162 // -- EQUALITY
163
168 inline bool operator== (int64_t i) const noexcept
169 { return mpz_cmp_si(m_val, i) == 0; }
170#ifndef SWIG
176 inline bool friend operator== (int64_t i, const integer& ii) noexcept
177 { return ii == i; }
178#endif
183 inline bool operator== (const integer& i) const noexcept
184 { return mpz_cmp(m_val, i.m_val) == 0; }
185
186 // -- NON-EQUALITY
187
192 inline bool operator!= (int64_t i) const noexcept
193 { return not (*this == i); }
194#ifndef SWIG
200 inline bool friend operator!= (int64_t i, const integer& ii) noexcept
201 { return ii != i; }
202#endif
207 inline bool operator!= (const integer& i) const noexcept
208 { return not (*this == i); }
209
210 // -- LESS THAN
211
216 inline bool operator< (int64_t i) const noexcept
217 { return mpz_cmp_si(m_val, i) < 0; }
218#ifndef SWIG
224 inline friend bool operator< (int64_t i, const integer& ii) noexcept
225 { return ii > i; }
226#endif
231 bool operator< (const integer& i) const noexcept
232 { return mpz_cmp(m_val, i.m_val) < 0; }
233
234 // -- LESS THAN OR EQUAL TO
235
240 inline bool operator<= (int64_t i) const noexcept
241 { return mpz_cmp_si(m_val, i) <= 0; }
242#ifndef SWIG
248 inline friend bool operator<= (int64_t i, const integer& ii) noexcept
249 { return ii >= i; }
250#endif
255 inline bool operator<= (const integer& i) const noexcept
256 { return mpz_cmp(m_val, i.m_val) <= 0; }
257
258 // -- GREATER THAN
259
264 inline bool operator> (int64_t i) const noexcept
265 { return mpz_cmp_si(m_val, i) > 0; }
266#ifndef SWIG
272 inline friend bool operator> (int64_t i, const integer& ii) noexcept
273 { return ii < i; }
274#endif
279 inline bool operator> (const integer& i) const noexcept
280 { return mpz_cmp(m_val, i.m_val) > 0; }
281
282 // -- GREATER THAN OR EQUAL TO
283
288 inline bool operator>= (int64_t i) const noexcept
289 { return mpz_cmp_si(m_val, i) >= 0; }
290#ifndef SWIG
296 inline friend bool operator>= (int64_t i, const integer& ii) noexcept
297 { return ii <= i; }
298#endif
303 inline bool operator>= (const integer& i) const noexcept
304 { return mpz_cmp(m_val, i.m_val) >= 0; }
305
306 /* ARITHMETIC OPERATORS */
307
308 // -- ADDITION
309
314 inline integer operator+ (int64_t i) const noexcept
315 { integer a(*this); a += i; return a; }
316#ifndef SWIG
322 inline friend integer operator+ (int64_t i, const integer& ii) noexcept
323 { return ii + i; }
324#endif
329 inline integer operator+ (const integer& i) const noexcept
330 { integer a(*this); a += i; return a; }
335 integer& operator+= (int64_t i) noexcept;
340 integer& operator+= (const integer& i) noexcept
341 { mpz_add(m_val, m_val, i.m_val); return *this; }
342
343 // -- SUBSTRACTION
344
346 inline integer operator- () const noexcept
347 { integer a(*this); mpz_neg(a.m_val, a.m_val); return a; }
352 inline integer operator- (int64_t i) const noexcept
353 { integer a(*this); a -= i; return a; }
354#ifndef SWIG
360 inline friend integer operator- (int64_t i, const integer& ii) noexcept
361 { return -ii + i; }
362#endif
367 inline integer operator- (const integer& i) const noexcept
368 { integer a(*this); a -= i; return a; }
373 integer& operator-= (int64_t i) noexcept;
378 inline integer& operator-= (const integer& i) noexcept
379 { mpz_sub(m_val, m_val, i.m_val); return *this; }
380
381 // -- MULTIPLICATION
382
387 inline integer operator* (int64_t i) const noexcept
388 { integer a(*this); a *= i; return a; }
389#ifndef SWIG
395 inline friend integer operator* (int64_t i, const integer& ii) noexcept
396 { return ii*i; }
397#endif
402 inline integer operator* (const integer& i) const noexcept
403 { integer a(*this); a *= i; return a; }
408 inline integer& operator*= (int64_t i) noexcept
409 { mpz_mul_si(m_val, m_val, i); return *this; }
414 inline integer& operator*= (const integer& i) noexcept
415 { mpz_mul(m_val, m_val, i.m_val); return *this; }
416
417 // -- DIVISION
418
423 inline integer operator/ (int64_t i) const noexcept
424 { integer a(*this); a /= i; return a; }
425#ifndef SWIG
431 inline friend int64_t operator/ (int64_t i, const integer& ii) noexcept
432 {
433 return i/ii.to_int();
434 }
435#endif
440 inline integer operator/ (const integer& i) const noexcept
441 { integer a(*this); a /= i; return a; }
446 integer& operator/= (int64_t i) noexcept;
451 inline integer& operator/= (const integer& i) noexcept
452 { mpz_div(m_val, m_val, i.m_val); return *this; }
453
454 // -- EXPONENTIATION
455
460 inline integer pow(uint64_t i) const noexcept
461 { integer r(*this); r.powt(i); return r; }
466 inline integer pow(const integer& i) const noexcept
467 { integer r(*this); r.powt(i); return r; }
468
475 inline integer& powt(uint64_t i) noexcept
476 { mpz_pow_ui(m_val, m_val, i); return *this; }
483 integer& powt(const integer& i) noexcept;
484
485 // -- MODULUS
486
491 inline uint64_t operator% (uint64_t i) const noexcept {
492 mpz_t r;
493 mpz_init(r);
494 const uint64_t m = mpz_mod_ui(r, m_val, i);
495 mpz_clear(r);
496 return m;
497 }
502 inline integer operator% (const integer& i) const noexcept {
503 integer r;
504 mpz_mod(r.m_val, m_val, i.m_val);
505 return r;
506 }
507
508 /* GETTERS */
509
511 inline constexpr bool is_initialized() const noexcept { return m_initialized; }
513 inline int32_t get_sign() const noexcept { return mpz_sgn(m_val); }
514
516 size_t bytes() const noexcept;
518 inline const mpz_t& get_raw_value() const noexcept { return m_val; }
519
520 /* CONVERTERS */
521
523 inline int64_t to_int() const noexcept { return mpz_get_si(m_val); }
525 inline uint64_t to_uint() const noexcept { return mpz_get_ui(m_val); }
527 inline double to_double() const noexcept { return mpz_get_d(m_val); }
528
530 inline std::string to_string() const noexcept {
531 std::string k;
532 as_string(k);
533 return k;
534 }
539 inline void as_string(std::string& s) const noexcept {
540 char *buf = nullptr;
541 buf = mpz_get_str(buf, 10, m_val);
542 s = std::string(buf);
543 free(buf);
544 }
545
546 /* OTHERS */
547
559 inline void swap(integer& i) noexcept { mpz_swap(m_val, i.m_val); }
560
561#ifndef SWIG
567 inline friend void swap(integer& i, integer& j) noexcept {
568 i.swap(j);
569 }
570#endif
571
572private:
574 mpz_t m_val;
576 bool m_initialized = true;
577};
578
583inline integer integer_from_ui(uint64_t n) noexcept {
584 integer i;
585 i.set_ui(n);
586 return i;
587}
588
589} // -- namespace numeric
590} // -- namespace lal
Arbitrary precision integer.
Definition integer.hpp:60
int64_t to_int() const noexcept
Converts this integer to a signed 64-bit integer.
Definition integer.hpp:523
void swap(integer &i) noexcept
Swaps the value of this integer with integer i's value.
Definition integer.hpp:559
friend int64_t operator/(int64_t i, const integer &ii) noexcept
Division operator.
Definition integer.hpp:431
void set_si(int64_t i) noexcept
Overwrites the value of this integer with i.
Definition integer.hpp:101
void set_ui(uint64_t i) noexcept
Overwrites the value of this integer with i.
Definition integer.hpp:110
void as_string(std::string &s) const noexcept
Converts this integer to a string.
Definition integer.hpp:539
friend bool operator>(int64_t i, const integer &ii) noexcept
Greater than operator.
Definition integer.hpp:272
integer operator-() const noexcept
Minus unary operator. Returns a new object of type 'integer'.
Definition integer.hpp:346
integer() noexcept
Empty constructor.
Definition integer.hpp:67
friend bool operator>=(int64_t i, const integer &ii) noexcept
Greater than or equal to operator.
Definition integer.hpp:296
integer(const std::string &s) noexcept
Constructor with string.
Definition integer.hpp:79
integer & powt(const integer &i) noexcept
Exponentiation operator.
integer(const integer &i) noexcept
Copy constructor.
Definition integer.hpp:91
bool operator!=(int64_t i) const noexcept
Non-equality operator.
Definition integer.hpp:192
friend void swap(integer &i, integer &j) noexcept
Swaps two integers.
Definition integer.hpp:567
integer & operator-=(int64_t i) noexcept
Substraction operator.
size_t bytes() const noexcept
Returns the amount of bytes this integer occupies.
void set_str(const std::string &s) noexcept
Overwrites the value of this integer with s.
Definition integer.hpp:128
uint64_t operator%(uint64_t i) const noexcept
Modulus operator.
Definition integer.hpp:491
mpz_t m_val
Structure from GMP storing the integer's value.
Definition integer.hpp:574
uint64_t to_uint() const noexcept
Converts this integer to an unsigned 64-bit integer.
Definition integer.hpp:525
bool m_initialized
Is this integer initialised?
Definition integer.hpp:576
integer & operator*=(int64_t i) noexcept
Product operator.
Definition integer.hpp:408
std::string to_string() const noexcept
Converts this integer to a string.
Definition integer.hpp:530
friend bool operator<(int64_t i, const integer &ii) noexcept
Less operator.
Definition integer.hpp:224
int32_t get_sign() const noexcept
Returns the sign of this integer.
Definition integer.hpp:513
friend integer operator+(int64_t i, const integer &ii) noexcept
Addition operator.
Definition integer.hpp:322
integer(integer &&i) noexcept
Move constructor.
integer & powt(uint64_t i) noexcept
Exponentiation operator.
Definition integer.hpp:475
integer pow(uint64_t i) const noexcept
Exponentiation operator.
Definition integer.hpp:460
void set_integer(const integer &i) noexcept
Overwrites the value of this integer with i.
Definition integer.hpp:119
~integer() noexcept
Destructor.
Definition integer.hpp:93
integer & operator+=(int64_t i) noexcept
Addition operator.
integer(mpz_t &&raw)
Constructor with mpz_t.
bool operator==(int64_t i) const noexcept
Equality operator.
Definition integer.hpp:168
double to_double() const noexcept
Converts this integer to a double-precision floating-point value.
Definition integer.hpp:527
const mpz_t & get_raw_value() const noexcept
Returns the underlying gmp data structure.
Definition integer.hpp:518
integer(int64_t i) noexcept
Constructor with unsigned integer value.
Definition integer.hpp:74
integer & operator/=(int64_t i) noexcept
Division operator.
friend integer operator*(int64_t i, const integer &ii) noexcept
Product operator.
Definition integer.hpp:395
constexpr bool is_initialized() const noexcept
Returns whether this object is initialised or not.
Definition integer.hpp:511
integer & operator=(int64_t i) noexcept
Assignment operator.
Definition integer.hpp:143
friend bool operator<=(int64_t i, const integer &ii) noexcept
Less than or equal to operator.
Definition integer.hpp:248
integer pow(const integer &i) const noexcept
Exponentiation operator.
Definition integer.hpp:466
Exact rational number.
Definition rational.hpp:63
integer integer_from_ui(uint64_t n) noexcept
Make an integer from a 64-bit unsigned integer value.
Definition integer.hpp:583
Main namespace of the library.
Definition definitions.hpp:48