-
Notifications
You must be signed in to change notification settings - Fork 1
/
BigInt.hpp
4301 lines (3839 loc) · 125 KB
/
BigInt.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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _BIG_NUM_HPP_
#error "This header must be included through BigNum.hpp"
#endif // _BIG_NUM_HPP_
#ifndef _BIG_INT_HPP_
#define _BIG_INT_HPP_
#include <memory>
#include <utility>
#include <cstddef>
#include <cmath>
#include <stdexcept>
#include <type_traits>
#include <cassert>
#include <sstream>
#include <climits>
#include <ostream>
#include <iterator>
#include <locale>
#include <istream>
#include <string>
#include <tuple>
#include "Libs/BigNumTypeTrait.hpp"
#include "Libs/BigNumMemory.hpp"
#include "Libs/BigNumFFT.hpp"
#include "Libs/BigNumModularRing.hpp"
#include "Libs/BigNumGenerics.hpp"
#include "BigInt/BigIntOutput.hpp"
#include "BigInt/BigIntInput.hpp"
namespace bignum{
using _type::isSigned;
using _utility::destroyAll;
using _type::isRLRef;
using _utility::fft1DPower2;
template <class Allocator = std::allocator<std::uint32_t>>
class BigInt{
private:
using Alloc = Allocator;
using Ptr = typename Alloc::pointer;
using AllocTrait = std::allocator_traits<Alloc>;
using SizeT = std::uint32_t;
using LogSizeT = std::uint8_t;
using Ele = typename Alloc::value_type;
static constexpr SizeT MAX_LEN = 32768;
static constexpr SizeT PRI_ORDER = 134217728;
//static constexpr SizeT MAX_LEN = 134217728; // 2 ^ 27
static constexpr Ele P = 2013265921; // 15 * MAX_LEN + 1
static constexpr Ele OMEGA = 440564289; // 31 ^ 15 mod P
static constexpr LogSizeT ENTRY_SIZE = 8;
//static constexpr LogSizeT ENTRY_SIZE = 15; // floor(log(p) / 2)
static constexpr Ele TWO_INV = 1006632961; // 2 ^ (-1) mod P
using ModularP_T = _utility::ModularP<SizeT, Ele, P, OMEGA>;
// wrapper for Ptr to simpfy array operations
struct DigitBuffer{
public:
DigitBuffer(Alloc *_alloc, SizeT _len)
:alloc(_alloc), len(_len), cap(static_cast<SizeT>(std::pow(2.0, std::ceil(std::log2(len))))){
data = alloc->allocate(static_cast<std::size_t>(cap));
}
DigitBuffer(Alloc *_alloc, SizeT _len, SizeT _cap)
:alloc(_alloc), len(_len), cap(_cap){
assert(cap == static_cast<SizeT>(std::pow(2.0, std::ceil(std::log2(len)))));
data = alloc->allocate(static_cast<SizeT>(cap));
}
DigitBuffer(Alloc *_alloc, std::nullptr_t)
:alloc(_alloc), data(nullptr), len(0), cap(0){}
DigitBuffer(Alloc *_alloc, std::nullptr_t, SizeT _len, SizeT _cap)
:alloc(_alloc), data(nullptr), len(_len), cap(_cap){}
DigitBuffer(Alloc *_alloc, const DigitBuffer &_rhs)
:alloc(_alloc), len(_rhs.len), cap(_rhs.cap){
data = alloc->allocate(static_cast<std::size_t>(cap));
SizeT i(0);
try{
for(;i < len;++i){
alloc->construct(data + i, _rhs.data[i]);
}
}
catch(...){
destroyAll(data, data + i, *alloc);
alloc->deallocate(data, static_cast<std::size_t>(cap));
data = nullptr;
throw ;
}
}
DigitBuffer(Alloc *_alloc, DigitBuffer &&_rhs)
:alloc(_alloc), data(std::move(_rhs.data)), len(std::move(_rhs.len)), cap(std::move(_rhs.cap)){}
~DigitBuffer(){
if(alloc == nullptr){
assert(data == nullptr);
return ;
}
if(data != nullptr){
alloc->deallocate(data, static_cast<std::size_t>(cap));
data = nullptr;
}
len = 0;
cap = 0;
alloc = nullptr;
}
DigitBuffer(const DigitBuffer &_rhs)
:DigitBuffer(_rhs.alloc, _rhs){}
DigitBuffer(DigitBuffer &&_rhs)
:DigitBuffer(std::move(_rhs.alloc), std::move(_rhs)){}
DigitBuffer &operator=(const DigitBuffer &) = delete;
DigitBuffer &operator=(DigitBuffer &&) = delete;
inline DigitBuffer &operator=(std::nullptr_t){
destroyAll(data, data + len, *alloc);
alloc->deallocate(data, static_cast<std::size_t>(cap));
data = nullptr;
len = 0;
cap = 0;
alloc = nullptr;
return *this;
}
inline friend bool operator==(const DigitBuffer &_lhs, std::nullptr_t){
if(_lhs.data != nullptr){
return false;
}
//assert(len == 0);
//assert(cap == 0);
return true;
}
inline friend bool operator==(std::nullptr_t, const DigitBuffer &_rhs){
return _rhs == nullptr;
}
inline friend bool operator!=(const DigitBuffer &_lhs, std::nullptr_t){
return !(_lhs == nullptr);
}
inline friend bool operator!=(std::nullptr_t, const DigitBuffer &_rhs){
return !(_rhs == nullptr);
}
inline void zeroLen() noexcept{
len = 0;
cap = 0;
}
inline DigitBuffer realloc(SizeT _len){
SizeT _cap = static_cast<SizeT>(std::pow(2.0, std::ceil(std::log2(_len))));
if(_len == len){
return DigitBuffer(alloc, nullptr, _len, _cap);
}
if(_cap == cap){
return DigitBuffer(alloc, nullptr, _len, _cap);
}
if(_cap > MAX_LEN){
// maybe use some compile time techiques to caculate the string
// literals needed
throw std::out_of_range("BigInt::DigitBuffer::realloc");
}
return DigitBuffer(alloc, _len, _cap);
}
inline void reconstruct(DigitBuffer &tmp, SizeT _len, SizeT _cap){
assert(static_cast<SizeT>(std::pow(2.0, std::ceil(std::log2(tmp.len)))) == tmp.cap);
if(len == tmp.len){
assert(tmp == nullptr);
return ;
}
if(nullptr == tmp){
// no realloc
assert(_cap == cap);
if(_len < len){
// shrink
destroyAll(data + _len, data + len, *alloc);
}
else{
// expand
SizeT i = len;
try{
for(;i < _len;++i){
alloc->construct(data + i, Ele());
}
}
catch(...){
destroyAll(data + len, data + i, *alloc);
throw;
}
}
}
else{
assert(tmp.len == _len);
assert(tmp.cap == _cap);
assert(cap != _cap);
if(_len < len){
// shrink
SizeT i(0);
try{
for(;i < _len;++i){
tmp.alloc->construct(tmp.data + i, std::move(data[i]));
}
}
catch(...){
destroyAll(tmp.data, tmp.data + i, *(tmp.alloc));
tmp.alloc->deallocate(tmp.data, tmp.cap);
tmp.data = nullptr;
throw ;
}
}
else{
// expand
SizeT i(0);
try{
for(;i < len;++i){
tmp.alloc->construct(tmp.data + i, std::move(data[i]));
}
for(;i < _len;++i){
tmp.alloc->construct(tmp.data + i, Ele());
}
}
catch(...){
destroyAll(tmp.data, tmp.data + i, *(tmp.alloc));
tmp.alloc->deallocate(tmp.data, tmp.cap);
tmp.data = nullptr;
throw ;
}
}
}
}
inline void reassign(DigitBuffer &&tmp){
if(nullptr != tmp){
destroyAll(data, data + len, *alloc);
alloc->deallocate(data, cap);
data = std::move(tmp.data);
alloc = tmp.alloc;
tmp.data = nullptr;
}
len = std::move(tmp.len);
cap = std::move(tmp.cap);
tmp.zeroLen();
}
inline void resize(SizeT _len){
DigitBuffer tmp = realloc(_len);
reconstruct(tmp, _len, tmp.cap);
reassign(std::move(tmp));
}
inline void setLen(SizeT _len) noexcept{
len = _len;
cap = static_cast<SizeT>(std::pow(2.0, std::ceil(std::log2(len))));
}
inline void setCap() noexcept{
cap = static_cast<SizeT>(std::pow(2.0, std::ceil(std::log2(len))));
}
// 1 - thisRaw > _rhsRaw
// 0 - thisRaw == _rhsRaw
// -1 - thisRaw < _rhsRaw
std::int8_t compareRaw(const DigitBuffer &_rhs) const{
if(len > _rhs.len){
return 1;
}
if(len < _rhs.len){
return -1;
}
assert(len == _rhs.len);
for(SizeT i(1);i <= len;++i){
if(data[len - i] > _rhs.data[len - i]){
return 1;
}
if(data[len - i] < _rhs.data[len - i]){
return -1;
}
}
return 0;
}
template <typename UnsignedInt, SizeT dLen>
inline std::int8_t compareUnsignedIntBuffer(UnsignedInt _rhs, std::integral_constant<SizeT, dLen>) const{
if(len > dLen){
return 1;
}
return compareUnsignedIntRaw(_rhs, std::integral_constant<SizeT, dLen>());
}
// overload for small unsigned types
template <typename UnsignedInt>
inline std::int8_t compareUnsignedIntBuffer(UnsignedInt _rhs, std::integral_constant<SizeT, 1>) const{
if(data[0] < _rhs){
return -1;
}
if(data[0] > _rhs){
return 1;
}
return 0;
}
Ele propagateCarryRange(SizeT st, SizeT en){
Ele carry(0);
for(SizeT i(st);i < en;++i){
data[i] += carry;
carry = data[i] >> ENTRY_SIZE;
data[i] -= carry << ENTRY_SIZE;
}
return carry;
}
inline void propagateCarry(){
Ele carry = propagateCarryRange(0, len);
SizeT expand(0);
for(Ele tmp = carry;tmp > 0;tmp >>= ENTRY_SIZE, ++expand);
SizeT _len = len;
// simple hack. TODO: manually resize and construct carries
resize(len + expand);
for(SizeT i = _len;i < len;++i){
data[i] = carry & ((1 << ENTRY_SIZE) - 1);
carry >>= ENTRY_SIZE;
}
}
// thisRaw += _rhsRaw
inline void addRaw(const DigitBuffer &_rhs){
// simple hack
if(len < _rhs.len){
resize(_rhs.len);
}
for(SizeT i(0);i < _rhs.len;++i){
data[i] += _rhs.data[i];
}
propagateCarry();
}
inline void addRaw(DigitBuffer &&_rhs){
if(len < _rhs.len){
resize(_rhs.len);
}
for(SizeT i = 0;i < _rhs.len;++i){
data[i] += std::move(_rhs.data[i]);
}
propagateCarry();
}
inline void shrinkToFit(){
SizeT _len = len - 1;
for(;true;--_len){
if(Ele(0) != data[_len]){
break;
}
if(_len == 0){
break;
}
}
++_len;
resize(_len);
}
// thisRaw -= _rhsRaw
// assert(thisRaw >= _rhsRaw)
void subRaw(const DigitBuffer &_rhs){
Ele carry(0);
assert(len >= _rhs.len);
for(SizeT i(0);i < _rhs.len;++i){
assert(data[i] + (1 << ENTRY_SIZE) >= _rhs.data[i] + carry);
if(data[i] < (_rhs.data[i] + carry)){
data[i] += (1 << ENTRY_SIZE) - (_rhs.data[i] + carry);
carry = Ele(1);
}
else{
data[i] -= _rhs.data[i] + carry;
carry = Ele(0);
}
}
for(SizeT i = _rhs.len;i < len;++i){
assert(data[i] + (1 << ENTRY_SIZE) >= carry);
if(data[i] < carry){
data[i] += (1 << ENTRY_SIZE) - carry;
carry = Ele(1);
}
else{
data[i] -= carry;
carry = Ele(0);
break;
}
}
if(Ele(0) != carry){
throw std::underflow_error("left operand less than right operand.");
}
shrinkToFit();
}
void subRaw(DigitBuffer &&_rhs){
Ele carry(0);
assert(len >= _rhs.len);
for(SizeT i(0);i < _rhs.len;++i){
assert(data[i] + (1 << ENTRY_SIZE) >= _rhs.data[i] + carry);
if(data[i] < (_rhs.data[i] + carry)){
data[i] += (1 << ENTRY_SIZE) - (std::move(_rhs.data[i]) + carry);
carry = Ele(1);
}
else{
data[i] -= std::move(_rhs.data[i]) + carry;
carry = Ele(0);
}
}
for(SizeT i = _rhs.len;i < len;++i){
assert(data[i] + (1 << ENTRY_SIZE) >= carry);
if(data[i] < carry){
data[i] += (1 << ENTRY_SIZE) - carry;
carry = Ele(1);
}
else{
data[i] -= carry;
carry = Ele(0);
break;
}
}
if(Ele(0) != carry){
throw std::underflow_error("left operand less than right operand.");
}
shrinkToFit();
}
private:
// TODO: rewite this function using std::integer_sequence instead of raw
// "recursion"
template <typename UnsignedInt, SizeT dLen>
inline std::int8_t compareUnsignedIntRaw(UnsignedInt _rhs, std::integral_constant<SizeT, dLen>) const{
Ele digit = (_rhs >> ((dLen - 1) * ENTRY_SIZE)) & ((1 << ENTRY_SIZE) - 1);
if((digit > 0)){
if(len < dLen){
return -1;
}
if(data[dLen - 1] < digit){
return -1;
}
if(data[dLen - 1] > digit){
return 1;
}
return compareUnsignedIntRaw(_rhs, std::integral_constant<SizeT, dLen - 1>());
}
else{
if(len < dLen){
return compareUnsignedIntRaw(_rhs, std::integral_constant<SizeT, dLen - 1>());
}
if(data[dLen - 1] > 0){
return 1;
}
return compareUnsignedIntRaw(_rhs, std::integral_constant<SizeT, dLen - 1>());
}
}
// note: this is an overload.
template <typename UnsignedInt>
inline std::int8_t compareUnsignedIntRaw(UnsignedInt _rhs, std::integral_constant<SizeT, 0>) const{
if(data[0] < _rhs){
return -1;
}
if(data[0] > _rhs){
return 1;
}
return 0;
}
public:
Alloc *alloc;
Ptr data;
SizeT len, cap;
};// struct DigitBuffer
struct NullTag{};
public:
// I want a more specialized version...
template <typename, class, typename>
friend class _GenericRadix;
template <typename, class, typename>
friend class _DecimalRadix;
template <typename, class, typename>
friend class _SmallPower2Radix;
template <typename, class, typename>
friend class _LargePower2Radix;
template <typename, class>
friend class _ExactDigitExtract;
template <typename, class>
friend class RadixConvertEnumer;
template <typename, class, typename>
friend class _DecimalAutomatic;
template <typename, class, typename>
friend class _GenericAutomatic;
template <typename, class, typename, class>
friend class _SmallPower2RadixAutomatic;
template <typename, class, typename, class>
friend class _LargePower2RadixAutomatic;
template <typename, class, typename, class>
friend class _ExactDigitAutomatic;
template <typename, class>
friend class RadixConvertRecver;
template <typename, class>
friend class _type::DigitRecvIterator;
template <class, class>
friend class _type::LiteralParser;
template <typename Digit>
using RadixConvertEnumer = RadixConvertEnumer<Digit, BigInt>;
template <typename Digit>
using RadixConvertRecver = RadixConvertRecver<Digit, BigInt>;
template <typename Digit>
using DigitRecvIterator = _type::DigitRecvIterator<Digit, BigInt>;
// default zero instead of nullptr, since BigInt should behave as normal
// numerial, not accepting null state
explicit BigInt()
:allocator(), buf(&allocator, 1), positive(true){
allocator.construct(buf.data + 0, Ele(0));
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
explicit BigInt(Integer _rhs)
:allocator(), buf(&allocator, nullptr){
assignIntegral(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
explicit BigInt(NullTag)
:buf(&allocator, nullptr), positive(false){}
// copy constructor
BigInt(const BigInt &_rhs)
:allocator(AllocTrait::select_on_container_copy_construction(_rhs.allocator)), buf(&allocator, nullptr, _rhs.buf.len, _rhs.buf.cap), positive(_rhs.positive){
assert(buf.cap <= MAX_LEN);
buf.data = allocator.allocate(static_cast<std::size_t>(buf.cap));
SizeT i(0);
try{
for(;i < buf.len;++i){
allocator.construct(buf.data + i, _rhs.buf.data[i]);
}
}
catch(...){
destroyAll(buf.data, buf.data + i, allocator);
allocator.deallocate(buf.data, buf.cap);
buf.data = nullptr;
throw ;
}
}
// move constructor
BigInt(BigInt &&_rhs)
:allocator(std::move(_rhs.allocator)), buf(&allocator, std::move(_rhs.buf)), positive(std::move(_rhs.positive)){
_rhs.buf.data = nullptr;
_rhs.buf.len = 0;
_rhs.buf.cap = 0;
}
template <typename Digit,
typename std::enable_if<std::is_integral<Digit>::value>::type * = nullptr>
explicit BigInt(const DigitRecvIterator<Digit> &_rhs)
:BigInt(_rhs.receiver->_finish()){}
template <typename Digit,
typename std::enable_if<std::is_integral<Digit>::value>::type * = nullptr>
explicit BigInt(DigitRecvIterator<Digit> &&_rhs)
:BigInt(std::move(*(_rhs.receiver))._finish()){}
// destructor
~BigInt(){
if(nullptr == buf.data){
// This object has been moved
buf.len = 0;
buf.cap = 0;
return ;
}
destroyAll(buf.data, buf.data + buf.len, allocator);
allocator.deallocate(buf.data, static_cast<std::size_t>(buf.cap));
buf.data = nullptr;
buf.len = 0;
buf.cap = 0;
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
BigInt &operator=(Integer _rhs){
if(nullptr != buf.data){
destroyAll(buf.data, buf.data + buf.len, allocator);
allocator.deallocate(buf.data, buf.cap);
buf.data = nullptr;
buf.len = 0;
buf.cap = 0;
}
assignIntegral(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
return *this;
}
// copy assignment operator
BigInt &operator=(const BigInt &_rhs){
if(this != &_rhs){
assignLv(_rhs, typename AllocTrait::propagate_on_container_copy_assignment());
assert(buf.cap <= MAX_LEN);
}
return *this;
}
// move assignment operator
BigInt &operator=(BigInt &&_rhs){
if(this != &_rhs){
assignRv(std::move(_rhs), typename AllocTrait::propagate_on_container_move_assignment());
assert(buf.cap <= MAX_LEN);
}
return *this;
}
inline const BigInt &operator+() const{
return *this;
}
inline BigInt operator-() const &{
BigInt tmp = *this;
tmp.changeSign();
return tmp;
}
inline BigInt operator-() &&{
changeSign();
return std::move(*this);
}
inline BigInt abs() &&{
if(!positive){
changeSign();
}
return std::move(*this);
}
inline BigInt abs() const &{
BigInt tmp = *this;
if(!positive){
tmp.changeSign();
}
return tmp;
}
// swap two BigInts
inline void swap(BigInt &_rhs){
if(this != &_rhs){
swapImpl(_rhs, typename AllocTrait::propagate_on_container_swap());
}
return ;
}
// is equal
inline friend bool operator==(const BigInt &_lhs, const BigInt &_rhs){
return 0 == _lhs.compare(_rhs);
}
// less than
inline friend bool operator<(const BigInt &_lhs, const BigInt &_rhs){
return -1 == _lhs.compare(_rhs);
}
// greater than
inline friend bool operator>(const BigInt &_lhs, const BigInt &_rhs){
return 1 == _lhs.compare(_rhs);
}
inline friend bool operator!=(const BigInt &_lhs, const BigInt &_rhs){
return 0 != _lhs.compare(_rhs);
}
inline friend bool operator<=(const BigInt &_lhs, const BigInt &_rhs){
return _lhs.compare(_rhs) <= 0;
}
inline friend bool operator>=(const BigInt &_lhs, const BigInt &_rhs){
return _lhs.compare(_rhs) >= 0;
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator==(const BigInt &_lhs, Integer _rhs){
return 0 == _lhs.compareInt(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator<(const BigInt &_lhs, Integer _rhs){
return -1 == _lhs.compareInt(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator>(const BigInt &_lhs, Integer _rhs){
return 1 == _lhs.compareInt(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator!=(const BigInt &_lhs, Integer _rhs){
return 0 != _lhs.compareInt(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator<=(const BigInt &_lhs, Integer _rhs){
return 0 > _lhs.compareInt(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator>=(const BigInt &_lhs, Integer _rhs){
return 0 <= _lhs.compareInt(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator==(Integer &_lhs, const BigInt &_rhs){
return 0 == _rhs.compareInt(_lhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator<(Integer &_lhs, const BigInt &_rhs){
return 1 == _rhs.compareInt(_lhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator>(Integer &_lhs, const BigInt &_rhs){
return -1 == _rhs.compareInt(_lhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator!=(Integer &_lhs, const BigInt &_rhs){
return 0 != _rhs.compareInt(_lhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator<=(Integer &_lhs, const BigInt &_rhs){
return 0 <= _rhs.compareInt(_lhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline friend bool operator>=(Integer &_lhs, const BigInt &_rhs){
return 0 >= _rhs.compareInt(_lhs, std::integral_constant<bool, isSigned<Integer>::value>());
}
// shl
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
BigInt &operator<<=(Integer _rhs){
shl(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
return *this;
}
template <class BigIntRef, typename Integer,
typename std::enable_if<isRLRef<BigInt, BigIntRef &&>::value && std::is_integral<Integer>::value>::type * = nullptr>
friend BigInt operator<<(BigIntRef &&_lhs, Integer _rhs){
BigInt tmp = std::forward<BigIntRef &&>(_lhs);
tmp.shl(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
return tmp;
}
// shr
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
BigInt &operator>>=(Integer _rhs){
shl(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
return *this;
}
template <typename BigIntRef, typename Integer,
typename std::enable_if<isRLRef<BigInt, BigIntRef &&>::value && std::is_integral<Integer>::value>::type * = nullptr>
friend BigInt operator>>(BigIntRef &&_lhs, Integer _rhs){
BigInt tmp = std::forward<BigIntRef>(_lhs);
tmp.shr(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
return tmp;
}
// self add
template <typename BigIntRef,
typename std::enable_if<isRLRef<BigInt, BigIntRef &&>::value>::type * = nullptr>
BigInt &operator+=(BigIntRef &&_rhs){
if(this != &_rhs){
add(std::forward<BigIntRef>(_rhs));
return *this;
}
else{
// this <<= 1
shl(static_cast<unsigned int>(1), std::false_type());
return *this;
}
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline BigInt &operator+=(Integer _rhs){
operator+=(BigInt(_rhs));
return *this;
}
inline friend BigInt operator+(const BigInt &_lhs, const BigInt &_rhs){
BigInt tmp = _lhs;
tmp += _rhs;
return tmp;
}
inline friend BigInt operator+(const BigInt &_lhs, BigInt &&_rhs){
BigInt tmp = std::move(_rhs);
tmp += _lhs;
return tmp;
}
inline friend BigInt operator+(BigInt &&_lhs, const BigInt &_rhs){
BigInt tmp = std::move(_lhs);
tmp += _rhs;
return tmp;
}
inline friend BigInt operator+(BigInt &&_lhs, BigInt &&_rhs){
BigInt tmp = std::move(_lhs);
tmp += _rhs;
return tmp;
}
template <class BigIntRef, typename Integer,
typename std::enable_if<isRLRef<BigInt, BigIntRef &&>::value && std::is_integral<Integer>::value>::type * = nullptr>
inline friend BigInt operator+(BigIntRef &&_lhs, Integer _rhs){
BigInt tmp = std::forward<BigIntRef>(_lhs);
tmp += _rhs;
return tmp;
}
template <class BigIntRef, typename Integer,
typename std::enable_if<isRLRef<BigInt, BigIntRef &&>::value && std::is_integral<Integer>::value>::type * = nullptr>
inline friend BigInt operator+(Integer _lhs, BigIntRef &&_rhs){
BigInt tmp = std::forward<BigIntRef>(_rhs);
tmp += _lhs;
return tmp;
}
// self substract
template <class BigIntRef,
typename std::enable_if<isRLRef<BigInt, BigIntRef &&>::value>::type * = nullptr>
inline BigInt &operator-=(BigIntRef &&_rhs){
if(this != &_rhs){
sub(std::forward<BigIntRef>(_rhs));
}
else{
zerolize();
}
return *this;
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline BigInt &operator-=(Integer _rhs){
sub(BigInt(_rhs));
return *this;
}
inline friend BigInt operator-(const BigInt &_lhs, const BigInt &_rhs){
BigInt tmp = _lhs;
tmp -= _rhs;
return tmp;
}
inline friend BigInt operator-(const BigInt &_lhs, BigInt &&_rhs){
BigInt tmp = std::move(_rhs);
tmp.changeSign();
tmp += _lhs;
return tmp;
}
inline friend BigInt operator-(BigInt &&_lhs, const BigInt &_rhs){
BigInt tmp = std::move(_lhs);
tmp -= _rhs;
return tmp;
}
inline friend BigInt operator-(BigInt &&_lhs, BigInt &&_rhs){
BigInt tmp = std::move(_lhs);
tmp -= std::move(_rhs);
return tmp;
}
template <class BigIntRef, typename Integer,
typename std::enable_if<isRLRef<BigInt, BigIntRef &&>::value && std::is_integral<Integer>::value>::type * = nullptr>
inline friend BigInt operator-(BigIntRef &&_lhs, Integer _rhs){
BigInt tmp = std::forward<BigIntRef>(_lhs);
tmp -= _rhs;
return tmp;
}
template <class BigIntRef, typename Integer,
typename std::enable_if<isRLRef<BigInt, BigIntRef &&>::value && std::is_integral<Integer>::value>::type * = nullptr>
inline friend BigInt operator-(Integer _lhs, BigIntRef &&_rhs){
BigInt tmp = std::forward<BigIntRef>(_rhs);
tmp += _lhs;
return tmp;
}
// self multiply
template <class BigIntRef,
typename std::enable_if<isRLRef<BigInt, BigIntRef &&>::value>::type * = nullptr>
inline BigInt &operator*=(BigIntRef &&_rhs){
multiply(std::forward<BigIntRef>(_rhs));
return *this;
}
template <typename Integer,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr>
inline BigInt &operator*=(Integer _rhs){
multiplyInt(_rhs, std::integral_constant<bool, isSigned<Integer>::value>());
return *this;
}
inline friend BigInt operator*(const BigInt &_lhs, const BigInt &_rhs){
BigInt tmp = _lhs;
tmp *= _rhs;
return tmp;
}
inline friend BigInt operator*(const BigInt &_lhs, BigInt &&_rhs){
BigInt tmp = std::move(_rhs);
tmp *= _lhs;
return tmp;
}
inline friend BigInt operator*(BigInt &&_lhs, const BigInt &_rhs){
BigInt tmp = std::move(_lhs);
tmp *= _rhs;
return tmp;
}
inline friend BigInt operator*(BigInt &&_lhs, BigInt &&_rhs){
BigInt tmp = std::move(_lhs);
tmp *= std::move(_rhs);
return tmp;
}
// A = q * B + r no matter whether A or B is negative or not
// self divide
inline BigInt &operator/=(BigInt &_rhs){
if(this != &_rhs){
bool _positive1 = positive;
bool _positive2 = _rhs.positive;
positive = true;
_rhs.positive = true;
std::tie(*this, std::ignore) = std::move(*this).divideBy(_rhs);
positive = _positive1 == _positive2;
_rhs.positive = _positive2;
return *this;
}
else{
destroyAll(buf.data, buf.data + buf.len, allocator);
allocator.deallocate(buf.data, static_cast<std::size_t>(buf.cap));
buf.data = allocator.allocate(1);
allocator.construct(buf.data + 0, 1);
buf.len = 1;
buf.cap = 1;
return *this;
}
}
inline BigInt &operator/=(BigInt &&_rhs){
bool _positive = positive == _rhs.positive;
positive = true;
_rhs.positive = true;
std::tie(*this, std::ignore) = std::move(*this).divideBy(std::move(_rhs));
positive = _positive;
return *this;
}
inline BigInt &operator/=(const BigInt &_rhs){
if(this != &_rhs){
if(_rhs.positive){
bool _positive = positive;
positive = true;
std::tie(*this, std::ignore) = std::move(*this).divideBy(_rhs);
positive = _positive;
return *this;
}
else{
bool _positive = !positive;
positive = true;
BigInt tmp = _rhs;
tmp.changeSign();
std::tie(*this, std::ignore) = std::move(*this).divideBy(tmp);