Repository navigation
Expand file tree
/
Copy pathInteger.h
More file actions
1118 lines (1003 loc) · 36.3 KB
/
Copy pathInteger.h
File metadata and controls
1118 lines (1003 loc) · 36.3 KB
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
// --------------------------
// projects/integer/Integer.h
// Copyright (C) 2013
// Glenn P. Downing
// --------------------------
#ifndef Integer_h
#define Integer_h
// --------
// includes
// --------
#include <cassert> // assert
#include <iostream> // ostream
#include <stdexcept> // invalid_argument
#include <string> // string
#include <vector> // vector
#include <algorithm>
using namespace std;
// -----------------
// shift_left_digits
// -----------------
/**
* @param b an iterator to the beginning of an input sequence (inclusive)
* @param e an iterator to the end of an input sequence (exclusive)
* @param x an iterator to the beginning of an output sequence (inclusive)
* @return an iterator to the end of an output sequence (exclusive)
* the sequence are of decimal digits with LSD stored first
* output the shift left of the input sequence into the output sequence
* ([b, e) << n) => x
*/
template <typename II, typename OI>
OI shift_left_digits (II b, II e, int n, OI x) {
assert(n>=0);
while (n!=0) {
*x = 0;
++x;
--n;}
while (b != e) {
*x = *b;
++x;
++b;}
return x;}
// ------------------
// shift_right_digits
// ------------------
/**
* @param b an iterator to the beginning of an input sequence (inclusive)
* @param e an iterator to the end of an input sequence (exclusive)
* @param x an iterator to the beginning of an output sequence (inclusive)
* @return an iterator to the end of an output sequence (exclusive)
* the sequence are of decimal digits with LSD stored first
* output the shift right of the input sequence into the output sequence
* ([b, e) >> n) => x
*/
template <typename II, typename OI>
OI shift_right_digits (II b, II e, int n, OI x) {
assert(n>=0);
while (n) {
++b;
--n;}
while(b!=e) {
*x = *b;
++b;
++x;}
return x;}
// -----------
// plus_digits
// -----------
/**
* @param b an iterator to the beginning of an input sequence (inclusive)
* @param e an iterator to the end of an input sequence (exclusive)
* @param b2 an iterator to the beginning of an input sequence (inclusive)
* @param e2 an iterator to the end of an input sequence (exclusive)
* @param x an iterator to the beginning of an output sequence (inclusive)
* @return an iterator to the end of an output sequence (exclusive)
* the sequence are of decimal digits with LSD stored first
* output the sum of the two input sequences into the output sequence
* ([b1, e1) + [b2, e2)) => x
*/
template <typename II1, typename II2, typename OI>
OI plus_digits (II1 b1, II1 e1, II2 b2, II2 e2, OI x) {
unsigned int carry = 0;
{
II1 b1Copy = b1;
II2 b2Copy = b2;
if(*b1Copy == 0 && ++b1Copy == e1){
while(b2!=e2){
*x = *b2;
++b2;
++x;}
return x;}
if(*b2Copy == 0 && ++b2Copy == e2){
while(b1!=e1){
*x = *b1;
++b1;
++x;}
return x;}}
while(true){
int result = 0;
if(b1 != e1 && b2 != e2){
result = carry + *b1 + *b2;
*x = result % 10;
++x;
++b1;
++b2;
}else if(b1 == e1 && b2 != e2){
result = carry + *b2;
*x = result % 10;
++x;
++b2;
}else if(b2 == e2 && b1 != e1){
result = carry + *b1;
*x = result % 10;
++x;
++b1;
} else {
if(carry > 0){
*x = carry;
++x;}
break;
}
carry = result/10;}
return x;}
// ------------
// minus_digits
// ------------
/**
* @param b an iterator to the beginning of an input sequence (inclusive)
* @param e an iterator to the end of an input sequence (exclusive)
* @param b2 an iterator to the beginning of an input sequence (inclusive)
* @param e2 an iterator to the end of an input sequence (exclusive)
* @param x an iterator to the beginning of an output sequence (inclusive)
* @return an iterator to the end of an output sequence (exclusive)
* the sequence are of decimal digits with LSD stored first
* output the difference of the two input sequences into the output sequence
* ([b1, e1) - [b2, e2)) => x
*/
template <typename II1, typename II2, typename OI>
OI minus_digits (II1 b1, II1 e1, II2 b2, II2 e2, OI x) {
unsigned int borrow = 0;
{
II2 b2Copy = b2;
if(*b2Copy == 0 && ++b2Copy == e2){
while(b1!=e1){
*x = *b1;
++b1;
++x;}
return x;}}
OI xCopy = x;
int length = 0;
int zeroCount = 0;
int lastRead = 1;
while(true){
if(b1 != e1 && b2 != e2){
*x = *b1 - *b2 - borrow;
++b1;
++b2;
}else if(b2 == e2 && b1 != e1){
*x = *b1 - borrow;
++b1;
} else {
break;}
if(*x<0){
*x += 10;
borrow = 1;
} else {
borrow = 0;}
if(*x == 0){
++zeroCount;
} else {
zeroCount = 0;}
++length;
lastRead = *x;
++x;}
if(lastRead == 0){
bool wasHere = false;
for (int i = 0; i<length - zeroCount; ++i){
++xCopy;
wasHere = true;}
if(!wasHere)
++xCopy;
return xCopy;
}
return x;}
// -----------------
// multiply_digit
// -----------------
/**
* @param b1 an iterator to the beginning of an input sequence (inclusive)
* @param e2 an iterator to the end of an input sequence (exclusive)
* @param digit an int to multiply
* @param x an iterator to the beginning of an output sequence (inclusive)
* @return an iterator to the end of an output sequence (exclusive)
* the sequence are of decimal digits with LSD stored first
* output the product of an input sequences and a digit into the output sequence
* ([b1, e1) * [b2, e2)) => x
*/
template <typename II1, typename OI>
OI multiply_digit (II1 b1, II1 e1, int digit, OI x){
assert(digit!=0);
unsigned int table [9][9] = {{1,2,3,4,5,6,7,8,9},
{2,4,6,8,10,12,14,16,18},
{3,6,9,12,15,18,21,24,27},
{4,8,12,16,20,24,28,32,36},
{5,10,15,20,25,30,35,40,45},
{6,12,18,24,30,36,42,48,54},
{7,14,21,28,35,42,49,56,63},
{8,16,24,32,40,48,56,64,72},
{9,18,27,36,45,54,63,72,81}};
unsigned int carry = 0;
while(b1!=e1){
unsigned int tempResult = carry + (*b1 == 0? 0 :table[digit - 1][*b1 - 1]);
*x = tempResult % 10;
carry = tempResult/10;
++x;
++b1;}
if(carry>0){
*x = carry;
++x;}
return x;}
// -----------------
// multiplies_digits
// -----------------
/**
* @param b an iterator to the beginning of an input sequence (inclusive)
* @param e an iterator to the end of an input sequence (exclusive)
* @param b2 an iterator to the beginning of an input sequence (inclusive)
* @param e2 an iterator to the end of an input sequence (exclusive)
* @param x an iterator to the beginning of an output sequence (inclusive)
* @return an iterator to the end of an output sequence (exclusive)
* the sequence are of decimal digits with LSD stored first
* output the product of the two input sequences into the output sequence
* ([b1, e1) * [b2, e2)) => x
*/
template <typename II1, typename II2, typename OI>
OI multiplies_digits (II1 b1, II1 e1, II2 b2, II2 e2, OI x) {
{
II1 b1Copy = b1;
II2 b2Copy = b2;
if((*b1Copy == 0 && ++b1Copy == e1) || (*b2Copy == 0 && ++b2Copy == e2)){
*x = 0;
++x;
return x;}}
{
II1 b1Copy = b1;
II2 b2Copy = b2;
if(*b1Copy == 1 && ++b1Copy == e1){
while(b2!=e2){
*x = *b2;
++b2;
++x;}
return x;}
if(*b2Copy == 1 && ++b2Copy == e2){
while(b1!=e1){
*x = *b1;
++b1;
++x;}
return x;}}
unsigned int size = 0;
{
II1 b1Copy = b1;
II2 b2Copy = b2;
while(b1Copy!=e1 && b2Copy!=e2){
++b1Copy;
++b2Copy;
++size;}
while(b1Copy!=e1){
++b1Copy;
++size;}
while(b2Copy!=e2){
++b2Copy;
++size;}}
assert(size!=0);
deque<int> runningSum(2*size);
runningSum.push_back(0);
deque<int>::iterator rsEnd = runningSum.begin();
++rsEnd;
unsigned int leftShift = 0;
while(b2!=e2){
if(*b2 == 0){
++leftShift;
++b2;
continue;}
deque<int> currentProduct(2*size);
deque<int>::iterator cpEnd = multiply_digit(b1,e1,*b2,currentProduct.begin());
deque<int> shiftedProduct(2*size);
cpEnd = shift_left_digits(currentProduct.begin(),cpEnd,leftShift,shiftedProduct.begin());
rsEnd = plus_digits(runningSum.begin(),rsEnd,shiftedProduct.begin(),cpEnd,runningSum.begin());
++leftShift;
++b2;}
for(deque<int>::iterator it = runningSum.begin();it!=rsEnd;++it){
*x = *it;
++x;}
return x;}
// --------------
// myCompare
// --------------
/**
* @param b1 an iterator to the beginning of an input sequence (inclusive)
* @param e1 an iterator to the end of an input sequence (exclusive)
* @param b2 an iterator to the beginning of an input sequence (inclusive)
* @param e2 an iterator to the end of an input sequence (exclusive)
* @param skip an int to make comparision with digits of the same order
* @return bool returing true if first sequence greater
* the sequence are of decimal digits with MSD stored first
* output boolean indicating result of comparision
* ([b1, e1) > [b2, e2)) => x
*/
template <typename II1, typename II2>
bool myCompare (II1 b1, II1 e1, II2 b2, II2 e2, unsigned int skip){
assert(skip>=0);
while(b1!=e1){
if(skip != 0)
return true;
if(*b1<*b2){
return false;
}else if(*b1>*b2){
return true;}
++b1;
++b2;
if(b2 == e2)
return true;}
return false;}
// --------------
// divides_digits
// --------------
/**
* @param b an iterator to the beginning of an input sequence (inclusive)
* @param e an iterator to the end of an input sequence (exclusive)
* @param b2 an iterator to the beginning of an input sequence (inclusive)
* @param e2 an iterator to the end of an input sequence (exclusive)
* @param x an iterator to the beginning of an output sequence (inclusive)
* @return an iterator to the end of an output sequence (exclusive)
* the sequence are of decimal digits with LSD stored first
* output the division of the two input sequences into the output sequence
* ([b1, e1) / [b2, e2)) => x
*/
template <typename II1, typename II2, typename OI>
OI divides_digits (II1 b1, II1 e1, II2 b2, II2 e2, OI x) {
{
II1 b1Copy = b1;
II2 b2Copy = b2;
assert(!(*b2Copy == 0 && ++b2Copy == e2));
if((*b1Copy == 0 && ++b1Copy == e1) ){
*x = 0;
++x;
return x;}}
{
II2 b2Copy = b2;
if(*b2Copy == 1 && ++b2Copy == e2){
while(b1!=e1){
*x = *b1;
++b1;
++x;}
return x;}}
unsigned int size = 0;
{
II1 b1Copy = b1;
II2 b2Copy = b2;
while(b1Copy!=e1 && b2Copy!=e2){
++b1Copy;
++b2Copy;
++size;}
while(b1Copy!=e1){
++b1Copy;
++size;}
while(b2Copy!=e2){
++b2Copy;
++size;}}
assert(size > 0);
deque<int> num(size);
deque<int> num2(size);
deque<int> num4(size);
deque<int> num8(size);
deque<int> numRev(size);
deque<int> num2Rev(size);
deque<int> num4Rev(size);
deque<int> num8Rev(size);
deque<int> dividend(2*size);
deque<int> dividendRev(2*size);
int mul1[] = {1};
int mul2[] = {2};
int mul4[] = {4};
int mul8[] = {8};
deque<int>::iterator numEnd = multiplies_digits(mul1,mul1+1,b2,e2,num.begin());
deque<int>::iterator num2End = multiplies_digits(mul2,mul2+1,b2,e2,num2.begin());
deque<int>::iterator num4End = multiplies_digits(mul4,mul4+1,b2,e2,num4.begin());
deque<int>::iterator num8End = multiplies_digits(mul8,mul8+1,b2,e2,num8.begin());
std::reverse_copy(num.begin(),numEnd,numRev.begin());
std::reverse_copy(num2.begin(),num2End,num2Rev.begin());
std::reverse_copy(num4.begin(),num4End,num4Rev.begin());
std::reverse_copy(num8.begin(),num8End,num8Rev.begin());
unsigned int numSize = numEnd - num.begin();
unsigned int num2Size = num2End - num2.begin();
unsigned int num4Size = num4End - num4.begin();
unsigned int num8Size = num8End - num8.begin();
numRev.resize(numSize);
num2Rev.resize(num2Size);
num4Rev.resize(num4Size);
num8Rev.resize(num8Size);
deque<int>::iterator dividendEnd = multiplies_digits(mul1,mul1+1,b1,e1,dividend.begin());
deque<int> runningSum(size);
runningSum.push_back(0);
deque<int>::iterator rsEnd = runningSum.begin();
++rsEnd;
while(true){
unsigned int dividendSize = dividendEnd - dividend.begin();
std::reverse_copy(dividend.begin(),dividendEnd,dividendRev.begin());
dividendRev.resize(dividendSize);
deque<int> shiftedNum(size);
deque<int>::iterator shiftEnd;
if(dividendSize>=num4Size && myCompare(dividendRev.begin(),dividendRev.end(),num4Rev.begin(),num4Rev.end(),dividendSize==num4Size?0:num8Size - num4Size)){
if(dividendSize>=num8Size && myCompare(dividendRev.begin(),dividendRev.end(),num8Rev.begin(),num8Rev.end(),dividendSize==num8Size?0:num8Size - num8Size)){
shiftEnd = shift_left_digits(num8.begin(),num8End,dividendSize - num8Size,shiftedNum.begin());
dividendEnd = minus_digits(dividend.begin(),dividendEnd,shiftedNum.begin(),shiftEnd,dividend.begin());
deque<int> partialQ(size);
deque<int>::iterator partialQEnd = shift_left_digits(mul8,mul8+1,dividendSize - num8Size,partialQ.begin());
rsEnd = plus_digits(runningSum.begin(),rsEnd,partialQ.begin(),partialQEnd,runningSum.begin());
} else {
unsigned int shiftSize = (num8Size<=dividendSize?num8Size:num4Size);
shiftEnd = shift_left_digits(num4.begin(),num4End,dividendSize - shiftSize,shiftedNum.begin());
dividendEnd = minus_digits(dividend.begin(),dividendEnd,shiftedNum.begin(),shiftEnd,dividend.begin());
deque<int> partialQ(size);
deque<int>::iterator partialQEnd = shift_left_digits(mul4,mul4+1, dividendSize - shiftSize,partialQ.begin());
rsEnd = plus_digits(runningSum.begin(),rsEnd,partialQ.begin(),partialQEnd,runningSum.begin());}
} else {
if(dividendSize>=num2Size && myCompare(dividendRev.begin(),dividendRev.end(),num2Rev.begin(),num2Rev.end(),dividendSize==num2Size?0:num8Size - num2Size)){
unsigned int shiftSize = (num8Size<=dividendSize?num8Size:(num4Size<=dividendSize?num4Size:num2Size));
shiftEnd = shift_left_digits(num2.begin(),num2End,dividendSize - shiftSize,shiftedNum.begin());
dividendEnd = minus_digits(dividend.begin(),dividendEnd,shiftedNum.begin(),shiftEnd,dividend.begin());
deque<int> partialQ(size);
deque<int>::iterator partialQEnd = shift_left_digits(mul2,mul2+1,dividendSize - shiftSize,partialQ.begin());
rsEnd = plus_digits(runningSum.begin(),rsEnd,partialQ.begin(),partialQEnd,runningSum.begin());
} else if(dividendSize >= numSize && myCompare(dividendRev.begin(),dividendRev.end(),numRev.begin(),numRev.end(),dividendSize==numSize?0:num8Size - numSize)) {
unsigned int shiftSize = (num8Size<=dividendSize?num8Size:(num4Size<=dividendSize?num4Size:(num2Size<=dividendSize?num2Size:numSize)));
shiftEnd = shift_left_digits(num.begin(),numEnd,dividendSize - shiftSize,shiftedNum.begin());
dividendEnd = minus_digits(dividend.begin(),dividendEnd,shiftedNum.begin(),shiftEnd,dividend.begin());
deque<int> partialQ(size);
deque<int>::iterator partialQEnd = shift_left_digits(mul1,mul1+1, dividendSize - shiftSize,partialQ.begin());
rsEnd = plus_digits(runningSum.begin(),rsEnd,partialQ.begin(),partialQEnd,runningSum.begin());
} else {
break;}}}
for(deque<int>::iterator it = runningSum.begin();it!=rsEnd;++it){
*x = *it;
++x;}
return x;}
// -------
// Integer
// -------
template < typename T, typename C = std::vector<T> >
class Integer {
// -----------
// operator ==
// -----------
/**
* @param Integer by reference
* @param Integer by reference
* @return boolean indicating equality of the two input sequences
*/
friend bool operator == (const Integer& lhs, const Integer& rhs) {
assert(lhs._size != 0);
assert(rhs._size != 0);
if(lhs._sign!=rhs._sign)
return false;
if(lhs._size!=rhs._size)
return false;
typename C::const_iterator itFirst = lhs._integer.cbegin();
typename C::const_iterator itSecond = rhs._integer.cbegin();
while(itFirst!=lhs._integer.cend() && itSecond!=rhs._integer.cend()){
if(*itFirst != *itSecond)
return false;
++itFirst;
++itSecond;}
if(itFirst!=lhs._integer.cend())
return false;
if(itSecond!=rhs._integer.cend())
return false;
return true;}
// -----------
// operator !=
// -----------
/**
* @param Integer by reference
* @param Integer by reference
* @return boolean indicating non equality of the two input sequences
*/
friend bool operator != (const Integer& lhs, const Integer& rhs) {
assert(lhs._size != 0);
assert(rhs._size != 0);
return !(lhs == rhs);}
// ----------
// operator <
// ----------
/**
* @param Integer by reference
* @param Integer by reference
* @return boolean indicating lhs < rhs
*/
friend bool operator < (const Integer& lhs, const Integer& rhs) {
assert(lhs._size != 0);
assert(rhs._size != 0);
if(lhs._sign != rhs._sign){
if(rhs._sign){
return false;
} else {
return true;}}
if(rhs._size > lhs._size)
return true;
if(lhs._size > rhs._size)
return false;
bool result = true;
if(lhs._sign)
result = !result;
typename C::const_reverse_iterator itFirst = lhs._integer.crbegin();
typename C::const_reverse_iterator itSecond = rhs._integer.crbegin();
while(true){
if(itFirst!=lhs._integer.crend() && itSecond!=rhs._integer.crend()){
if(*itFirst<*itSecond){
return result;
} else if (*itFirst>*itSecond){
return !result;}
++itFirst;
++itSecond;
}else{
if(itSecond!=rhs._integer.crend())
return result;
break;}}
return !result;}
// -----------
// operator <=
// -----------
/**
* @param Integer by reference
* @param Integer by reference
* @return boolean indicating lhs <= rhs
*/
friend bool operator <= (const Integer& lhs, const Integer& rhs) {
assert(lhs._size != 0);
assert(rhs._size != 0);
return !(rhs < lhs);}
// ----------
// operator >
// ----------
/**
* @param Integer by reference
* @param Integer by reference
* @return boolean indicating lhs > rhs
*/
friend bool operator > (const Integer& lhs, const Integer& rhs) {
assert(lhs._size != 0);
assert(rhs._size != 0);
return (rhs < lhs);}
// -----------
// operator >=
// -----------
/**
* @param Integer by reference
* @param Integer by reference
* @return boolean indicating lhs >= rhs
*/
friend bool operator >= (const Integer& lhs, const Integer& rhs) {
assert(lhs._size != 0);
assert(rhs._size != 0);
return !(lhs < rhs);}
// ----------
// operator +
// ----------
/**
* @param lhs Integer by value
* @param rhs Integer by referance
* @return Integer holding lhs + rhs
*/
friend Integer operator + (Integer lhs, const Integer& rhs) {
return lhs += rhs;}
// ----------
// operator -
// ----------
/**
* @param lhs Integer by value
* @param rhs Integer by referance
* @return Integer holding lhs - rhs
*/
friend Integer operator - (Integer lhs, const Integer& rhs) {
return lhs -= rhs;}
// ----------
// operator *
// ----------
/**
* @param lhs Integer by value
* @param rhs Integer by referance
* @return Integer holding lhs * rhs
*/
friend Integer operator * (Integer lhs, const Integer& rhs) {
return lhs *= rhs;}
// ----------
// operator /
// ----------
/**
* @param lhs Integer by value
* @param rhs Integer by referance
* @return Integer holding lhs / rhs
* @throws invalid_argument if (rhs == 0)
*/
friend Integer operator / (Integer lhs, const Integer& rhs) {
return lhs /= rhs;}
// ----------
// operator %
// ----------
/**
* @param lhs Integer by value
* @param rhs Integer by referance
* @return Integer holding lhs % rhs
* @throws invalid_argument if (rhs <= 0)
*/
friend Integer operator % (Integer lhs, const Integer& rhs) {
return lhs %= rhs;}
// -----------
// operator <<
// -----------
/**
* @param lhs Integer by value
* @param rhs int by value
* @return Integer left shifted by rhs
* @throws invalid_argument if (rhs < 0)
*/
friend Integer operator << (Integer lhs, int rhs) {
return lhs <<= rhs;}
// -----------
// operator >>
// -----------
/**
* @param lhs Integer by value
* @param rhs int by value
* @return Integer right shifted by rhs
* @throws invalid_argument if (rhs < 0)
*/
friend Integer operator >> (Integer lhs, int rhs) {
return lhs >>= rhs;}
// -----------
// operator <<
// -----------
/**
* @param lhs Ostream by referance
* @param rhs Integer by referance
* @return Output stream buffered with Integer (MSD first)
*/
friend std::ostream& operator << (std::ostream& lhs, const Integer& rhs) {
assert(rhs._size>0);
if(rhs._sign)
lhs<<"-";
for(typename C::const_reverse_iterator rIt = rhs._integer.crbegin(); rIt!=rhs._integer.crend(); ++rIt)
lhs<<*rIt;
return lhs;}
// ---
// abs
// ---
/**
* absolute value
* does NOT modify the argument
* @param x Integer by value
* @return Integer
*/
friend Integer abs (Integer x) {
return x.abs();}
// ---
// pow
// ---
/**
* power
* does NOT modify the argument
* @param x an Integer by value
* @param e an int by value
* @return an Integer x^e
* @throws invalid_argument if (x == 0) && (e == 0)
* @throws invalid_argument if (e < 0)
*/
friend Integer pow (Integer x, int e) {
return x.pow(e);}
private:
// ----
// data
// ----
C _integer;
bool _sign; //true -> negative
unsigned int _size;
private:
// -----
// valid
// -----
bool valid () const {
// <your code>
return true;}
public:
// ------------
// constructors
// ------------
/**
* @param value an int to initialize Integer
*/
Integer (int value) {
_size = 0;
if(value<0){
_sign = true;
value *= -1;
} else {
_sign = false;}
if(value == 0){
_integer.push_back(0);
++_size;
}else{
while(value!=0){
_integer.push_back(value % 10);
value = value/10;
++_size;}}
_integer.resize(_size);
assert(valid());}
/**
* @param value a c_str to initialize Integer
* @throws invalid_argument if value is not a valid representation of an Integer
*/
explicit Integer (const std::string& value) {
string::const_iterator vBegin = value.cbegin();
string::const_reverse_iterator vRevBegin = value.crbegin();
string::const_reverse_iterator vRevEnd = value.crend();
_size = 0;
if(*vBegin == '-'){
_sign = true;
++vBegin;
--vRevEnd;
} else {
_sign = false;}
for(;vRevBegin!=vRevEnd; ++vRevBegin){
T digit = static_cast<T>(*vRevBegin) - '0';
if(!(digit>=0 && digit<=9))
throw std::invalid_argument("Integer::Integer()");
_integer.push_back(digit);
++_size;}
_integer.resize(_size);
if(_size ==1)
if(_integer[0] == 0)
_sign = false;
if (!valid())
throw std::invalid_argument("Integer::Integer()");}
// Default copy, destructor, and copy assignment.
// Integer (const Integer&);
// ~Integer ();
// Integer& operator = (const Integer&);
// ----------
// operator -
// ----------
/**
* Operator to negate
*/
Integer operator - () const {
Integer negateInt = *this;
negateInt._sign = !negateInt._sign;
return negateInt;}
// -----------
// operator ++
// -----------
/**
* Operator to self increment(pre)
*/
Integer& operator ++ () {
*this += 1;
return *this;}
/**
* Operator to self increment(post)
*/
Integer operator ++ (int) {
Integer x = *this;
++(*this);
return x;}
// -----------
// operator --
// -----------
/**
* Operator to self decrement(pre)
*/
Integer& operator -- () {
*this -= 1;
return *this;}
/**
* Operator to self decrement(post)
*/
Integer operator -- (int) {
Integer x = *this;
--(*this);
return x;}
// -----------
// operator +=
// -----------
/**
* @param rhs an Integer by reference
* Operator +=
*/
Integer& operator += (const Integer& rhs) {
C newInt(this->_size+rhs._size);
if(this->_sign == rhs._sign){
typename C::iterator plusEnd = plus_digits(this->_integer.begin(),this->_integer.end(),rhs._integer.begin(),rhs._integer.end(),newInt.begin());
this->_size = plusEnd - newInt.begin();
newInt.resize(this->_size);
}else{
Integer<T,C> absLhs = *this;
Integer<T,C> absRhs = rhs;
bool lhsGreater = absLhs.abs() >= absRhs.abs();
if(lhsGreater){
typename C::iterator minusEnd = minus_digits(this->_integer.begin(),this->_integer.end(),rhs._integer.begin(),rhs._integer.end(),newInt.begin());
this->_size = minusEnd - newInt.begin();
newInt.resize(this->_size);
} else {
typename C::iterator minusEnd = minus_digits(rhs._integer.begin(),rhs._integer.end(),this->_integer.begin(),this->_integer.end(),newInt.begin());
this->_sign = !this->_sign;
this->_size = minusEnd - newInt.begin();
newInt.resize(this->_size);}}
if(this->_size == 1)
if(newInt[0] == 0)
this->_sign = false;
this->_integer = newInt;
return *this;}
// -----------
// operator -=
// -----------
/**
* @param rhs an Integer by reference
*/
Integer& operator -= (const Integer& rhs) {
C newInt(this->_size+rhs._size);
if(this->_sign != rhs._sign){
typename C::iterator plusEnd = plus_digits(this->_integer.begin(),this->_integer.end(),rhs._integer.begin(),rhs._integer.end(),newInt.begin());
this->_size = plusEnd - newInt.begin();
newInt.resize(this->_size);
}else{
Integer<T,C> absLhs = *this;
Integer<T,C> absRhs = rhs;
bool lhsGreater = absLhs.abs() > absRhs.abs();
if(this->_sign){
if(lhsGreater){
typename C::iterator minusEnd = minus_digits(this->_integer.begin(),this->_integer.end(),rhs._integer.begin(),rhs._integer.end(),newInt.begin());
this->_size = minusEnd - newInt.begin();
newInt.resize(this->_size);
} else {
typename C::iterator minusEnd = minus_digits(rhs._integer.begin(),rhs._integer.end(),this->_integer.begin(),this->_integer.end(),newInt.begin());
this->_sign = !this->_sign;
this->_size = minusEnd - newInt.begin();
newInt.resize(this->_size);}
} else {
if(lhsGreater){
typename C::iterator minusEnd = minus_digits(this->_integer.begin(),this->_integer.end(),rhs._integer.begin(),rhs._integer.end(),newInt.begin());
this->_size = minusEnd - newInt.begin();
newInt.resize(this->_size);
} else {
typename C::iterator minusEnd = minus_digits(rhs._integer.begin(),rhs._integer.end(),this->_integer.begin(),this->_integer.end(),newInt.begin());
this->_sign = !this->_sign;
this->_size = minusEnd - newInt.begin();
newInt.resize(this->_size);}}}
if(this->_size == 1)
if(newInt[0] == 0)
this->_sign = false;
this->_integer = newInt;
return *this;}
// -----------
// operator *=
// -----------
/**
* @param rhs an Integer by reference
*/
Integer& operator *= (const Integer& rhs) {
if(this->_sign == rhs._sign){
if(this->_sign)
this->_sign=!this->_sign;
} else {
if(rhs._sign)
this->_sign = true;}
typename C::iterator oldEnd = this->_integer.end();
C newInt(10*(this->_size+rhs._size));
typename C::iterator newEnd = multiplies_digits(this->_integer.begin(),oldEnd,rhs._integer.begin(),rhs._integer.end(),newInt.begin());
this->_size = newEnd - newInt.begin();
newInt.resize(this->_size);
if(this->_size == 1)
if(newInt[0] == 0)
this->_sign = false;
this->_integer = newInt;
return *this;}
// -----------
// operator /=
// -----------
/**
* @param rhs an Integer by reference
* @throws invalid_argument if (rhs == 0)
*/
Integer& operator /= (const Integer& rhs) {
if(rhs == 0)
throw std::invalid_argument("Integer::Integer()");
if(this->_sign == rhs._sign){
if(this->_sign)
this->_sign=!this->_sign;
} else {
if(rhs._sign)
this->_sign = true;}
typename C::iterator oldEnd = this->_integer.end();