@@ -29,48 +29,54 @@ import {MIN_NUMBER, MAX_NUMBER, MAX_BASE} from './_limits.js';
29
29
30
30
export class Integer {
31
31
constructor ( base , is_negative , limbs ) {
32
- this . base = base ;
33
- this . is_negative = is_negative ;
34
- this . limbs = limbs ;
32
+ this . _base = base ;
33
+ this . _is_negative = is_negative ;
34
+ this . _limbs = limbs ;
35
35
}
36
36
37
37
move ( other ) {
38
- other . base = this . base ;
39
- other . is_negative = this . is_negative ;
40
- other . limbs = this . limbs ;
38
+ other . _base = this . _base ;
39
+ other . _is_negative = this . _is_negative ;
40
+ other . _limbs = this . _limbs ;
41
41
return other ;
42
42
}
43
43
44
44
clone ( ) {
45
- return new Integer ( this . base , this . is_negative , this . limbs ) ;
45
+ return new Integer ( this . _base , this . _is_negative , this . _limbs ) ;
46
46
}
47
47
48
48
_limbs_in_base ( base ) {
49
49
// TODO save result for later ? Maybe replace base ?
50
- return this . base === base
51
- ? this . limbs
52
- : convert ( this . base , base , this . limbs , 0 , this . limbs . length ) ;
50
+ return this . _base === base
51
+ ? this . _limbs
52
+ : convert ( this . _base , base , this . _limbs , 0 , this . _limbs . length ) ;
53
53
}
54
54
55
55
toString ( base = DEFAULT_DISPLAY_BASE ) {
56
56
if ( this . iszero ( ) ) return '0' ;
57
57
58
- const digits = stringify ( this . base , base , this . limbs , 0 , this . limbs . length ) ;
58
+ const digits = stringify (
59
+ this . _base ,
60
+ base ,
61
+ this . _limbs ,
62
+ 0 ,
63
+ this . _limbs . length ,
64
+ ) ;
59
65
60
- return this . is_negative ? '-' + digits : digits ;
66
+ return this . _is_negative ? '-' + digits : digits ;
61
67
}
62
68
63
69
add ( other ) {
64
- if ( this . is_negative !== other . is_negative ) {
65
- return other . is_negative
70
+ if ( this . _is_negative !== other . _is_negative ) {
71
+ return other . _is_negative
66
72
? this . sub ( other . opposite ( ) )
67
73
: other . sub ( this . opposite ( ) ) ;
68
74
}
69
75
70
- const result_is_negative = this . is_negative ;
71
- const r = this . base ;
76
+ const result_is_negative = this . _is_negative ;
77
+ const r = this . _base ;
72
78
73
- const a = this . limbs ;
79
+ const a = this . _limbs ;
74
80
75
81
const b = other . _limbs_in_base ( r ) ;
76
82
@@ -97,15 +103,15 @@ export class Integer {
97
103
}
98
104
99
105
sub ( other ) {
100
- if ( this . is_negative !== other . is_negative ) {
101
- return other . is_negative
106
+ if ( this . _is_negative !== other . _is_negative ) {
107
+ return other . _is_negative
102
108
? this . add ( other . opposite ( ) )
103
109
: this . opposite ( ) . add ( other ) . opposite ( ) ;
104
110
}
105
111
// /!\ _sub needs |c| >= |a| >= |b|
106
112
107
- const r = this . base ;
108
- const a = this . limbs ;
113
+ const r = this . _base ;
114
+ const a = this . _limbs ;
109
115
const aj = a . length ;
110
116
const ai = _trim_positive ( a , 0 , aj ) ;
111
117
@@ -122,14 +128,14 @@ export class Integer {
122
128
123
129
_sub ( r , b , bi , bj , a , ai , aj , c , 0 , c . length ) ;
124
130
125
- return new Integer ( r , ~ this . is_negative , c ) ;
131
+ return new Integer ( r , ~ this . _is_negative , c ) ;
126
132
}
127
133
128
134
const c = _zeros ( aj - ai ) ;
129
135
130
136
_sub ( r , a , ai , aj , b , bi , bj , c , 0 , c . length ) ;
131
137
132
- return new Integer ( r , this . is_negative , c ) ;
138
+ return new Integer ( r , this . _is_negative , c ) ;
133
139
}
134
140
135
141
isub ( other ) {
@@ -146,10 +152,10 @@ export class Integer {
146
152
}
147
153
148
154
mul ( other ) {
149
- const result_is_negative = this . is_negative ^ other . is_negative ;
150
- const r = this . base ;
155
+ const result_is_negative = this . _is_negative ^ other . _is_negative ;
156
+ const r = this . _base ;
151
157
152
- const a = this . limbs ;
158
+ const a = this . _limbs ;
153
159
154
160
const b = other . _limbs_in_base ( r ) ;
155
161
@@ -181,14 +187,14 @@ export class Integer {
181
187
* @return {Integer } <code>this ^ x</code>
182
188
*/
183
189
pown ( x ) {
184
- const is_negative = this . is_negative & x & 1 ? - 1 : 0 ;
190
+ const is_negative = this . _is_negative & x & 1 ? - 1 : 0 ;
185
191
186
- const a = this . limbs ;
192
+ const a = this . _limbs ;
187
193
const c = _zeros ( Math . max ( 1 , a . length * x ) ) ;
188
194
189
- _pow_double ( this . base , x , a , 0 , a . length , c , 0 , c . length ) ;
195
+ _pow_double ( this . _base , x , a , 0 , a . length , c , 0 , c . length ) ;
190
196
191
- return new Integer ( this . base , is_negative , c ) ;
197
+ return new Integer ( this . _base , is_negative , c ) ;
192
198
}
193
199
194
200
pow ( other ) {
@@ -254,27 +260,27 @@ export class Integer {
254
260
divround ( other ) {
255
261
const [ q , r ] = this . divmod ( other ) ;
256
262
if ( r . ge ( other . divn ( 2 ) . addn ( other . iseven ( ) ? 0 : 1 ) ) )
257
- increment ( q . base , q . limbs , 0 , q . limbs . length ) ;
263
+ increment ( q . _base , q . _limbs , 0 , q . _limbs . length ) ;
258
264
return q ;
259
265
}
260
266
261
267
divmod ( other ) {
262
268
if ( other . iszero ( ) ) throw new ZeroDivisionError ( 'Integer division by zero' ) ; // Optimize
263
269
264
- const quotient_is_negative = this . is_negative ^ other . is_negative ;
265
- const r = this . base ;
270
+ const quotient_is_negative = this . _is_negative ^ other . _is_negative ;
271
+ const r = this . _base ;
266
272
267
273
// The underlying algorithm does not allow leading 0's so we trim them.
268
- const lj = this . limbs . length ;
269
- const li = _trim_positive ( this . limbs , 0 , lj ) ;
274
+ const lj = this . _limbs . length ;
275
+ const li = _trim_positive ( this . _limbs , 0 , lj ) ;
270
276
271
277
// Dividend is 0
272
278
if ( li >= lj )
273
- return [ new Integer ( this . base , 0 , [ 0 ] ) , new Integer ( this . base , 0 , [ 0 ] ) ] ;
279
+ return [ new Integer ( this . _base , 0 , [ 0 ] ) , new Integer ( this . _base , 0 , [ 0 ] ) ] ;
274
280
275
281
// Dividend (& Remainder)
276
282
const D = _alloc ( lj - li ) ;
277
- _copy ( this . limbs , li , lj , D , 0 ) ;
283
+ _copy ( this . _limbs , li , lj , D , 0 ) ;
278
284
279
285
// Divisor
280
286
const d = other . _limbs_in_base ( r ) ;
@@ -289,9 +295,9 @@ export class Integer {
289
295
const Q = new Integer ( r , quotient_is_negative , q ) ; // Quotient
290
296
const R = new Integer ( r , 0 , D ) ; // Remainder
291
297
292
- if ( ( this . is_negative || other . is_negative ) && ! jz ( D , 0 , D . length ) ) {
293
- if ( other . is_negative ) {
294
- if ( this . is_negative ) {
298
+ if ( ( this . _is_negative || other . _is_negative ) && ! jz ( D , 0 , D . length ) ) {
299
+ if ( other . _is_negative ) {
300
+ if ( this . _is_negative ) {
295
301
R . negate ( ) ; // TODO optimize
296
302
} else {
297
303
increment ( r , q , 0 , q . length ) ;
@@ -322,7 +328,7 @@ export class Integer {
322
328
}
323
329
324
330
opposite ( ) {
325
- return new Integer ( this . base , ~ this . is_negative , this . limbs ) ;
331
+ return new Integer ( this . _base , ~ this . _is_negative , this . _limbs ) ;
326
332
}
327
333
328
334
negate ( ) {
@@ -339,24 +345,24 @@ export class Integer {
339
345
}
340
346
341
347
sign ( ) {
342
- return this . iszero ( ) ? 0 : this . is_negative ? - 1 : 1 ;
348
+ return this . iszero ( ) ? 0 : this . _is_negative ? - 1 : 1 ;
343
349
}
344
350
345
351
iszero ( ) {
346
- return jz ( this . limbs , 0 , this . limbs . length ) ;
352
+ return jz ( this . _limbs , 0 , this . _limbs . length ) ;
347
353
}
348
354
349
355
isone ( ) {
350
- if ( this . is_negative ) return false ;
351
- return eq ( this . limbs , 0 , this . limbs . length , [ 1 ] , 0 , 1 ) ;
356
+ if ( this . _is_negative ) return false ;
357
+ return eq ( this . _limbs , 0 , this . _limbs . length , [ 1 ] , 0 , 1 ) ;
352
358
}
353
359
354
360
isnonzero ( ) {
355
361
return ! this . iszero ( ) ;
356
362
}
357
363
358
364
isnegative ( ) {
359
- return this . is_negative === - 1 ;
365
+ return this . _is_negative === - 1 ;
360
366
}
361
367
362
368
ispositive ( ) {
@@ -404,7 +410,13 @@ export class Integer {
404
410
digits ( base = DEFAULT_DISPLAY_BASE ) {
405
411
// TODO Once #to is implemented we can rewrite this as
406
412
// return this.to(LITTLE_ENDIAN, base, Array) ;
407
- return convert ( this . base , base , this . limbs , 0 , this . limbs . length ) . reverse ( ) ;
413
+ return convert (
414
+ this . _base ,
415
+ base ,
416
+ this . _limbs ,
417
+ 0 ,
418
+ this . _limbs . length ,
419
+ ) . reverse ( ) ;
408
420
}
409
421
410
422
bits ( ) {
@@ -425,17 +437,17 @@ export class Integer {
425
437
426
438
if ( this . iszero ( ) ) {
427
439
if ( other . iszero ( ) ) return 0 ;
428
- if ( other . is_negative ) return 1 ;
440
+ if ( other . _is_negative ) return 1 ;
429
441
return - 1 ;
430
442
}
431
443
432
- if ( this . is_negative < other . is_negative ) return - 1 ;
433
- if ( this . is_negative > other . is_negative ) return 1 ;
444
+ if ( this . _is_negative < other . _is_negative ) return - 1 ;
445
+ if ( this . _is_negative > other . _is_negative ) return 1 ;
434
446
435
- const a = this . limbs ;
436
- const b = other . _limbs_in_base ( this . base ) ;
447
+ const a = this . _limbs ;
448
+ const b = other . _limbs_in_base ( this . _base ) ;
437
449
438
- return this . is_negative === 0
450
+ return this . _is_negative === 0
439
451
? cmp ( a , 0 , a . length , b , 0 , b . length )
440
452
: cmp ( b , 0 , b . length , a , 0 , a . length ) ;
441
453
}
@@ -493,8 +505,8 @@ export class Integer {
493
505
}
494
506
495
507
gcd ( other ) {
496
- const r = this . base ;
497
- const a = this . limbs ;
508
+ const r = this . _base ;
509
+ const a = this . _limbs ;
498
510
const b = other . _limbs_in_base ( r ) ;
499
511
const [ d , di , dj ] = euclidean_algorithm ( r , a , 0 , a . length , b , 0 , b . length ) ;
500
512
const gcd = _alloc ( dj - di ) ;
@@ -503,8 +515,8 @@ export class Integer {
503
515
}
504
516
505
517
egcd ( other ) {
506
- const r = this . base ;
507
- const a = this . limbs ;
518
+ const r = this . _base ;
519
+ const a = this . _limbs ;
508
520
const b = other . _limbs_in_base ( r ) ;
509
521
const [
510
522
R0 ,
@@ -534,19 +546,19 @@ export class Integer {
534
546
gcd : new Integer ( r , 0 , gcd ) ,
535
547
x :
536
548
x . length > 0
537
- ? new Integer ( r , this . is_negative ^ ( ( steps % 2 ) - 1 ) , x )
549
+ ? new Integer ( r , this . _is_negative ^ ( ( steps % 2 ) - 1 ) , x )
538
550
: new Integer ( r , 0 , [ 0 ] ) ,
539
551
y :
540
552
y . length > 0
541
- ? new Integer ( r , other . is_negative ^ - ( steps % 2 ) , y )
553
+ ? new Integer ( r , other . _is_negative ^ - ( steps % 2 ) , y )
542
554
: new Integer ( r , 0 , [ 0 ] ) ,
543
555
u :
544
556
u . length > 0
545
- ? new Integer ( r , this . is_negative ^ - ( steps % 2 ) , u )
557
+ ? new Integer ( r , this . _is_negative ^ - ( steps % 2 ) , u )
546
558
: new Integer ( r , 0 , [ 0 ] ) ,
547
559
v :
548
560
v . length > 0
549
- ? new Integer ( r , other . is_negative ^ ( ( steps % 2 ) - 1 ) , v )
561
+ ? new Integer ( r , other . _is_negative ^ ( ( steps % 2 ) - 1 ) , v )
550
562
: new Integer ( r , 0 , [ 0 ] ) ,
551
563
} ;
552
564
}
@@ -562,14 +574,14 @@ export class Integer {
562
574
) ;
563
575
564
576
const limbs = convert (
565
- this . base ,
577
+ this . _base ,
566
578
MAX_BASE ,
567
- this . limbs ,
579
+ this . _limbs ,
568
580
0 ,
569
- this . limbs . length ,
581
+ this . _limbs . length ,
570
582
) ;
571
583
572
- const sign = this . is_negative ? - 1 : 1 ;
584
+ const sign = this . _is_negative ? - 1 : 1 ;
573
585
574
586
const value =
575
587
limbs . length === 2 ? limbs [ 0 ] * MAX_BASE + limbs [ 1 ] : limbs [ 0 ] ;
0 commit comments