-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfInt.hpp
901 lines (809 loc) · 21.6 KB
/
InfInt.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
#ifndef INFINT_HPP
#define INFINT_HPP
// C++ std lib
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <limits>
#include <exception>
// C std lib
#include <cmath>
class InfIntFullDivResult;
class InfInt {
public:
typedef std::vector<bool>::size_type size_type;
InfInt(void); // init to +0
InfInt(const InfInt& other); // copy
InfInt(InfInt&& other); // move
virtual ~InfInt(void) = default;
// from ints
InfInt(int other);
InfInt(unsigned long long other);
template <typename T>
InfInt(T other);
// from strings
InfInt(std::string other); // base 10
InfInt(std::string other, int base); // custom base from 2 to 62
bool sign(void) const;
bool get(const size_type& pos) const;
size_type size(void) const;
// complements //
InfInt& ones_complement(void);
InfInt& twos_complement(void);
// casting //
std::string Bstr(void) const;
std::string Bstr(size_type str_size) const;
std::string str(int base) const;
std::string str(void) const;
template <class T> T to_int(void) const;
template <class T> T to_int_safe(void) const;
// 2 in 1 operator //
static InfIntFullDivResult fulldiv(const InfInt& a, const InfInt& b);
// operator //
// equal
InfInt& operator=(const InfInt& other);
// cmp
bool operator==(const InfInt& other) const;
bool operator!=(const InfInt& other) const;
bool operator>(const InfInt& other) const;
bool operator<=(const InfInt& other) const;
bool operator<(const InfInt& other) const;
bool operator>=(const InfInt& other) const;
// unary
InfInt operator+(void) const;
InfInt operator-(void) const;
// add
InfInt operator+(const InfInt& other) const;
InfInt& operator+=(const InfInt& other);
InfInt& operator++(void);
InfInt operator++(int);
// sub
InfInt operator-(const InfInt& other) const;
InfInt& operator-=(const InfInt& other);
InfInt& operator--(void);
InfInt operator--(int);
// mul
InfInt operator*(const InfInt& other) const;
InfInt& operator*=(const InfInt& other);
// div
InfInt operator/(const InfInt& other) const;
InfInt& operator/=(const InfInt& other);
// mod
InfInt operator%(const InfInt& other) const;
InfInt& operator%=(const InfInt& other);
// bitwise
InfInt operator&(const InfInt& other) const;
InfInt& operator&=(const InfInt& other);
InfInt operator|(const InfInt& other) const;
InfInt& operator|=(const InfInt& other);
InfInt operator^(const InfInt& other) const;
InfInt& operator^=(const InfInt& other);
InfInt operator<<(size_type other) const;
InfInt& operator<<=(size_type other);
InfInt operator>>(size_type other) const;
InfInt& operator>>=(size_type other);
// Static Attributes //
static const InfInt neg_one;
static const InfInt zero;
static const InfInt pos_one;
protected:
InfInt& clean(void);
// Attributes //
std::vector<bool> m_number;
bool m_sign;
};
template <class T>
T InfInt::to_int(void) const {
size_type bits_in_T = sizeof(T) * 8;
T tmp = 0;
for (size_type i = 0; i < bits_in_T; i++)
tmp |= static_cast<T>(this->get(i)) << i;
return tmp;
}
template <class T>
T InfInt::to_int_safe(void) const {
size_type bits_in_T = sizeof(T) * 8;
if (bits_in_T < this->size())
throw std::overflow_error("template <class T> T InfInt::to_int_safe(void) const: Given type is too litle!");
if (this->sign())
if (static_cast<T>(-1) >= static_cast<T>(0))
throw std::overflow_error("template <class T> T InfInt::to_int_safe(void) const: Given type do not have the right signess!");
T tmp = 0;
for (size_type i = 0; i < bits_in_T; i++)
tmp |= static_cast<T>(this->get(i)) << i;
return tmp;
}
// Outside the class //
InfInt operator "" _infint(unsigned long long other); // alows the use of the macro _infint to transform a unsigned long long to an InfInt
std::ostream& operator<<(std::ostream& out, const InfInt& infint);
std::istream& operator>>(std::istream& in, InfInt& infint);
namespace std {
template<>
class numeric_limits<InfInt> {
public:
static constexpr bool is_specialized = true;
static constexpr bool is_signed = true;
static constexpr bool is_integer = true;
static constexpr bool is_exact = true;
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
static constexpr std::float_denorm_style has_denorm = std::denorm_absent;
static constexpr bool has_denorm_loss = false;
static constexpr std::float_round_style round_style = std::round_toward_zero;
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = false;
static constexpr bool is_modulo = false;
static constexpr int digits = std::numeric_limits<int>::max();
static constexpr int digits10 = std::numeric_limits<InfInt>::digits * std::log10(2);
static constexpr int max_digits10 = 0;
static constexpr int min_exponent = 0;
static constexpr int min_exponent10 = 0;
static constexpr int max_exponent = 0;
static constexpr int max_exponent10 = 0;
static constexpr bool traps = true;
static constexpr bool tinyness_before = false;
};
} // std namespace
// InfInt libs
#include "InfIntResult.hpp"
InfInt::InfInt(void):
m_number({false}),
m_sign(false)
{
//
}
InfInt::InfInt(const InfInt& other):
m_number(other.m_number),
m_sign(other.m_sign)
{
//
}
InfInt::InfInt(InfInt&& other):
m_number(std::move(other.m_number)),
m_sign(std::move(other.m_sign))
{
other.m_number = {0};
other.m_sign = false;
}
InfInt::InfInt(int other) {
if (other >= 0) {
if (other == 0) {
this->m_sign = false;
this->m_number.push_back(false);
return;
}
this->m_sign = false;
while (other != 0) {
this->m_number.push_back(static_cast<bool>(other & 1));
other >>= 1;
}
} else {
if (other == -1) {
this->m_sign = true;
this->m_number.push_back(true);
return;
}
this->m_sign = true;
while (other != -1) {
this->m_number.push_back(static_cast<bool>(other & 1));
other >>= 1;
}
}
}
InfInt::InfInt(unsigned long long other) {
if (other == 0ull) {
this->m_sign = false;
this->m_number.push_back(false);
return;
}
this->m_sign = false;
while (other != 0ull) {
this->m_number.push_back(static_cast<bool>(other & 1ull));
other >>= 1ull;
}
}
template <typename T>
InfInt::InfInt(T other) {
static_assert(std::numeric_limits<T>::is_integer);
T zero = 0;
if (other >= zero) {
if (other == zero) {
this->m_sign = false;
this->m_number.push_back(false);
return;
}
this->m_sign = false;
while (other != zero) {
this->m_number.push_back(static_cast<bool>(other & 1));
other >>= 1;
}
} else {
T minus_one = -1;
if (other == minus_one) {
this->m_sign = true;
this->m_number.push_back(true);
return;
}
this->m_sign = true;
while (other != minus_one) {
this->m_number.push_back(static_cast<bool>(other & 1));
other >>= 1;
}
}
}
InfInt::InfInt(std::string other) {
if (other.empty()) {
this->m_sign = false;
this->m_number.push_back(false);
return;
} if (other.front() == '-') {
this->m_sign = true;
other.assign(other, 1, other.size());
} else if (other.front() == '+') {
this->m_sign = false;
other.assign(other, 1, other.size());
} else
this->m_sign = false;
this->m_number.reserve(other.size());
while (other.size() > 0) {
this->m_number.push_back(other.back() != '0');
other.pop_back();
}
this->clean();
}
InfInt::InfInt(std::string other, int base) {
if (base < 2 || base > 62)
throw std::domain_error("InfInt::InfInt(std::string other, int base): base must be beetween 2 and 62");
if (other.empty()) {
this->m_sign = false;
this->m_number.push_back(false);
return;
}
bool sign = false;
if (other.front() == '-') {
sign = true;
other.assign(other, 1, other.size());
} else if (other.front() == '+') {
sign = false;
other.assign(other, 1, other.size());
}
this->m_sign = false;
this->m_number.push_back(false);
InfInt p = InfInt::pos_one;
InfInt infint_base = base;
while (other.size() > 0) {
unsigned char c = other.back();
if ('0' <= c && c <= '9')
c -= '0';
else if ('a' <= c && c <= 'z')
c -= 'a' - 10;
else if ('A' <= c && c <= 'Z')
c -= 'A' - 36;
if (c < base)
*this += InfInt(c) * p;
other.pop_back();
p *= infint_base;
}
this->clean();
if (sign)
this->twos_complement();
}
bool InfInt::sign(void) const {
return this->m_sign;
}
bool InfInt::get(const size_type& pos) const {
if (pos < this->size())
return this->m_number[pos];
else
return this->m_sign;
}
typename InfInt::size_type InfInt::size(void) const {
return this->m_number.size();
}
InfInt& InfInt::ones_complement(void) {
this->m_sign = !sign();
this->m_number.flip();
return *this;
}
InfInt& InfInt::twos_complement(void) {
if (*this == InfInt::zero)
return *this;
this->ones_complement();
InfInt tmp;
tmp.m_sign = this->sign();
tmp.m_number.resize(this->size() + 1, false);
tmp.m_number[1] = this->get(0);
tmp.m_number[0] = !this->get(0);
for (size_type i = 1; i < this->size(); ++i) {
tmp.m_number[i + 1] = this->get(i) && tmp.get(i);
tmp.m_number[i] = this->get(i) != tmp.get(i);
}
if (!tmp.m_number.back())
tmp.m_number.pop_back();
tmp.clean();
*this = tmp;
return *this;
}
std::string InfInt::Bstr(void) const {
std::string str;
str.reserve(this->size());
if (this->m_sign)
str.push_back('-');
else
str.push_back('+');
for (size_t i = size() - 1; i > 0; --i) {
str.push_back('0' + this->get(i));
}
if (this->size() > 0) {
str.push_back('0' + this->get(0));
}
return str;
}
std::string InfInt::Bstr(size_type str_size) const {
if (str_size == 0)
str_size = this->size() + 1;
if (str_size <= this->size())
throw std::logic_error("std::string InfInt::b_str(size_type str_size) const: str_size <= size()");
std::string str;
str.reserve(str_size);
for (; str_size > this->size(); --str_size)
str.push_back('0' + this->m_sign);
for (size_t i = this->size() - 1; i > 0; --i) {
str.push_back('0' + get(i));
}
if (this->size() > 0) {
str.push_back('0' + get(0));
}
return str;
}
std::string InfInt::str(int base) const {
if (base < 2 || base > 62)
throw std::domain_error("std::string InfInt::str(int base) const: base must be beetween 2 and 62");
std::string str;
InfInt tmp(*this);
if (tmp == InfInt::zero)
str.push_back('0');
if (tmp.sign())
tmp.twos_complement();
InfInt remainder;
InfInt infint_base(base);
while (tmp != InfInt::zero) {
auto result = InfInt::fulldiv(tmp, infint_base);
tmp = result.quotient();
remainder = result.remainder();
if (remainder < 10)
str.push_back('0' + remainder.to_int<char>());
else if (remainder < 36)
str.push_back('a' + remainder.to_int<char>() - 10);
else
str.push_back('A' + remainder.to_int<char>() - 36);
}
if (this->sign())
str.push_back('-');
std::reverse(str.begin(), str.end());
return str;
}
std::string InfInt::str(void) const {
InfInt powten = 10000000000000000000ull;
InfInt::size_type size = 19;
std::string str;
InfInt tmp(*this);
if (tmp.sign())
tmp.twos_complement();
while (true) { // do {...} while (temp != InfInt::zero);
auto result = InfInt::fulldiv(tmp, powten);
tmp = result.quotient();
if (tmp == InfInt::zero) {
str = std::to_string(result.remainder().to_int<unsigned long long>()) + str;
break;
}
std::string tmpstr = std::to_string(result.remainder().to_int<unsigned long long>());
InfInt::size_type tmpsize = size - tmpstr.size();
str = tmpstr + str;
tmpstr.clear();
for (InfInt::size_type i = 0; i < tmpsize; ++i)
tmpstr.push_back('0');
str = tmpstr + str;
}
if (this->sign())
return '-' + str;
else
return str;
}
InfIntFullDivResult InfInt::fulldiv(const InfInt& _a, const InfInt& _b) {
if (_b == InfInt::zero)
throw std::domain_error("static InfIntFulldivResult InfInt::fulldiv(const InfInt& _a, const InfInt& _b): Cannot divide by 0");
InfInt q;
InfInt a(_a);
if (a.sign())
a.twos_complement();
InfInt abs_b(_b);
if (abs_b.sign())
abs_b.twos_complement();
InfInt b(abs_b);
if (b >= a) {
InfInt remainder, quotient;
if (b == a) {
if (_a.sign() == _b.sign())
quotient = InfInt::pos_one;
else
quotient = InfInt::neg_one;
} else {
if (a.sign() != _a.sign())
a.twos_complement();
remainder = a;
}
return InfIntFullDivResult(quotient, remainder);
}
InfInt::size_type size_diff = a.size() - b.size();
InfInt mask = InfInt::pos_one << size_diff;
b <<= size_diff;
while (a >= abs_b) {
while (a < b) {
mask >>= 1;
b >>= 1;
}
a -= b;
q |= mask;
mask >>= b.size() - a.size();
b >>= b.size() - a.size();
}
InfInt quotient = q;
InfInt remainder = a;
if (_a.sign() != _b.sign())
quotient.twos_complement();
if (remainder.sign() != _a.sign())
remainder.twos_complement();
return InfIntFullDivResult(quotient, remainder);
}
InfInt& InfInt::operator=(const InfInt& other) {
this->m_sign = other.sign();
this->m_number = other.m_number;
return *this;
}
bool InfInt::operator==(const InfInt& other) const {
if (this->size() != other.size())
return false;
if (this->sign() != other.sign())
return false;
for (unsigned long long i = 0; i < this->size(); ++i)
if (this->m_number[i] != other.get(i))
return false;
return true;
}
bool InfInt::operator!=(const InfInt& other) const {
return !(*this == other);
}
bool InfInt::operator>(const InfInt& other) const {
if (!this->sign() && other.sign())
return true;
if (this->sign() && !other.sign())
return false;
if (this->size() > other.size())
return !this->sign();
if (this->size() < other.size())
return this->sign();
if (this->size() > 1) {
for (size_type i = this->size() - 2; i > 0; --i) {
if (this->get(i) == other.get(i))
continue;
return this->get(i);
}
}
if (this->size() != 0)
return this->get(0) != other.get(0) && this->get(0);
return false;
}
bool InfInt::operator<=(const InfInt& other) const {
return !(*this > other);
}
bool InfInt::operator<(const InfInt& other) const {
if (this->sign() && !other.sign())
return true;
if (!this->sign() && other.sign())
return false;
if (this->size() < other.size())
return !this->sign();
if (this->size() > other.size())
return this->sign();
if (this->size() > 1) {
for (size_type i = this->size() - 2; i > 0; --i) {
if (this->get(i) == other.get(i))
continue;
return other.get(i);
}
}
if (this->size() != 0)
return this->get(0) != other.get(0) && other.get(0);
return false;
}
bool InfInt::operator>=(const InfInt& other) const {
return !(*this < other);
}
InfInt InfInt::operator+(void) const {
return *this;
}
InfInt InfInt::operator-(void) const {
InfInt tmp(*this);
tmp.twos_complement();
return tmp;
}
InfInt InfInt::operator+(const InfInt& other) const {
return InfInt(*this) += other;
}
InfInt& InfInt::operator+=(const InfInt& other) {
size_type max_size;
if (this->m_number.size() < other.m_number.size())
max_size = other.size();
else
max_size = this->size();
this->m_number.resize(max_size, this->sign());
bool old_carry = false;
for (size_type i = 0; i < max_size; ++i) {
bool new_carry = ((this->get(i) != other.get(i)) && old_carry) || (this->get(i) && other.get(i));
this->m_number[i] = (this->get(i) != other.get(i)) != old_carry;
old_carry = new_carry;
}
if (this->sign() == other.sign())
this->m_number.push_back(old_carry);
else
this->m_sign = !old_carry;
return this->clean();
}
InfInt& InfInt::operator++(void) {
*this += InfInt::pos_one;
return *this;
}
InfInt InfInt::operator++(int) {
InfInt tmp(*this);
++(*this);
return tmp;
}
InfInt InfInt::operator-(const InfInt& other) const {
return InfInt(*this) -= other;
}
InfInt& InfInt::operator-=(const InfInt& other) {
return (*this) += (-other);
}
InfInt& InfInt::operator--(void) {
return *this += InfInt::neg_one;
}
InfInt InfInt::operator--(int) {
InfInt tmp(*this);
--(*this);
return tmp;
}
InfInt InfInt::operator*(const InfInt& other) const {
if (other.size() < this->size())
return other * (*this);
InfInt temp;
InfInt a(*this);
if (a.sign())
a.twos_complement();
InfInt b(other);
if (b.sign())
b.twos_complement();
for (size_type i = 0; i < a.size(); ++i)
if (a.get(i))
temp += b << i;
if (this->sign() != other.sign())
temp.twos_complement();
return temp;
}
InfInt& InfInt::operator*=(const InfInt& other) {
*this = (*this) * other;
return *this;
}
InfInt InfInt::operator/(const InfInt& other) const {
if (other == InfInt::zero)
throw std::domain_error("InfInt InfInt::operator/(const InfInt& other) const: Cannot divide by 0");
InfInt q;
InfInt a(*this);
if (a.sign())
a.twos_complement();
InfInt abs_other(other);
if (abs_other.sign())
abs_other.twos_complement();
InfInt b = abs_other;
if (b >= a) {
if (b == a) {
if (this->sign() == other.sign())
return InfInt::pos_one;
else
return InfInt::neg_one;
} else
return InfInt::zero;
}
size_type size_diff = a.size() - b.size();
InfInt mask = InfInt::pos_one << size_diff;
b <<= size_diff;
while (a >= abs_other) {
while (a < b) {
mask >>= 1;
b >>= 1;
}
a -= b;
q |= mask;
mask >>= b.size() - a.size();
b >>= b.size() - a.size();
}
if (this->sign() != other.sign())
q.twos_complement();
return q;
}
InfInt& InfInt::operator/=(const InfInt& other) {
*this = *this / other;
return *this;
}
InfInt InfInt::operator%(const InfInt& other) const {
if (other == InfInt::zero)
throw std::domain_error("InfInt InfInt::operator%(const InfInt& other) const: Cannot divide by 0");
InfInt a(*this);
if (a.sign())
a.twos_complement();
InfInt abs_other(other);
if (abs_other.sign())
abs_other.twos_complement();
InfInt b = abs_other;
if (b >= a) {
if (b == a)
return InfInt::zero;
else {
if (a.sign() != this->sign())
a.twos_complement();
return a;
}
}
size_type size_diff = a.size() - b.size();
b <<= size_diff;
while (a >= abs_other) {
while (a < b)
b >>= 1;
a -= b;
b >>= b.size() - a.size();
}
if (a.sign() != this->sign())
a.twos_complement();
return a;
}
InfInt& InfInt::operator%=(const InfInt& other) {
*this = *this % other;
return *this;
}
InfInt InfInt::operator&(const InfInt& other) const {
size_type max_size = 1;
if (this->size() < other.size())
max_size = other.size();
else
max_size = this->size();
InfInt temp;
temp.m_sign = this->sign() && other.sign();
temp.m_number.resize(max_size, temp.sign());
for (size_type i = 0; i < max_size; ++i)
temp.m_number[i] = this->get(i) && other.get(i);
return temp.clean();
}
InfInt& InfInt::operator&=(const InfInt& other) {
size_type max_size = 1;
if (this->size() < other.size())
max_size = other.size();
else
max_size = this->size();
this->m_sign = this->sign() && other.sign();
this->m_number.resize(max_size, this->sign());
for (size_type i = 0; i < max_size; ++i)
this->m_number[i] = this->get(i) && other.get(i);
return this->clean();
}
InfInt InfInt::operator|(const InfInt& other) const {
size_type max_size;
if (this->size() < other.size())
max_size = other.size();
else
max_size = this->size();
InfInt temp;
temp.m_sign = this->sign() || other.sign();
temp.m_number.resize(max_size, temp.sign());
for (size_type i = 0; i < max_size; ++i)
temp.m_number[i] = this->get(i) || other.get(i);
return temp.clean();
}
InfInt& InfInt::operator|=(const InfInt& other) {
size_type max_size;
if (this->size() < other.size())
max_size = other.size();
else
max_size = this->size();
this->m_sign = this->sign() || other.sign();
this->m_number.resize(max_size, this->sign());
for (size_type i = 0; i < max_size; ++i)
this->m_number[i] = this->get(i) || other.get(i);
return this->clean();
}
InfInt InfInt::operator^(const InfInt& other) const {
size_type max_size;
if (this->size() < other.size())
max_size = other.size();
else
max_size = this->size();
InfInt temp;
temp.m_sign = this->sign() != other.sign();
temp.m_number.resize(max_size, temp.sign());
for (size_type i = 0; i < max_size; ++i)
temp.m_number[i] = this->get(i) != other.get(i);
return temp.clean();
}
InfInt& InfInt::operator^=(const InfInt& other) {
size_type max_size;
if (this->size() < other.size())
max_size = other.size();
else
max_size = this->size();
this->m_sign = this->sign() != other.sign();
this->m_number.resize(max_size, this->sign());
for (size_type i = 0; i < max_size; ++i)
this->m_number[i] = this->get(i) != other.get(i);
return this->clean();
}
InfInt InfInt::operator<<(size_type other) const {
if (other == 0 || (*this) == InfInt::zero)
return *this;
InfInt temp(*this);
temp.m_number.insert(temp.m_number.begin(), other, false);
return temp;
}
InfInt& InfInt::operator<<=(size_type other) {
if (other == 0 || (*this) == InfInt::zero)
return *this;
this->m_number.insert(this->m_number.begin(), other, false);
return *this;
}
InfInt InfInt::operator>>(size_type other) const {
if (other == 0)
return *this;
InfInt temp(*this);
if (other < size())
temp.m_number.erase(temp.m_number.begin(), temp.m_number.begin() + static_cast<long long>(other));
else
temp.m_number.clear();
temp.clean();
return temp;
}
InfInt& InfInt::operator>>=(size_type other) {
if (other == 0)
return *this;
if (other < this->size())
this->m_number.erase(this->m_number.begin(), this->m_number.begin() + static_cast<long long>(other));
else
this->m_number.clear();
this->clean();
return *this;
}
InfInt& InfInt::clean(void) {
while (this->size() > 1 && this->m_number.back() == this->m_sign)
this->m_number.pop_back();
if (this->size() == 0)
this->m_number.push_back(this->m_sign);
return *this;
}
const InfInt InfInt::neg_one(-1);
const InfInt InfInt::zero(0);
const InfInt InfInt::pos_one(1);
// Outside the class //
// alows the use of the macro _inf to transform a unsigned long long to an InfInt
InfInt operator "" _infint(unsigned long long other) {
return static_cast<InfInt>(other);
}
std::ostream& operator<<(std::ostream& out, const InfInt& infint) {
out << infint.str();
return out;
}
std::istream& operator>>(std::istream& in, InfInt& infint) {
std::string str;
in >> str;
infint = InfInt(str, 10);
return in;
}
#endif // INFINT_HPP