@@ -29,48 +29,54 @@ import {MIN_NUMBER, MAX_NUMBER, MAX_BASE} from './_limits.js';
2929
3030export class Integer {
3131 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 ;
3535 }
3636
3737 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 ;
4141 return other ;
4242 }
4343
4444 clone ( ) {
45- return new Integer ( this . base , this . is_negative , this . limbs ) ;
45+ return new Integer ( this . _base , this . _is_negative , this . _limbs ) ;
4646 }
4747
4848 _limbs_in_base ( base ) {
4949 // 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 ) ;
5353 }
5454
5555 toString ( base = DEFAULT_DISPLAY_BASE ) {
5656 if ( this . iszero ( ) ) return '0' ;
5757
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+ ) ;
5965
60- return this . is_negative ? '-' + digits : digits ;
66+ return this . _is_negative ? '-' + digits : digits ;
6167 }
6268
6369 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
6672 ? this . sub ( other . opposite ( ) )
6773 : other . sub ( this . opposite ( ) ) ;
6874 }
6975
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 ;
7278
73- const a = this . limbs ;
79+ const a = this . _limbs ;
7480
7581 const b = other . _limbs_in_base ( r ) ;
7682
@@ -97,15 +103,15 @@ export class Integer {
97103 }
98104
99105 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
102108 ? this . add ( other . opposite ( ) )
103109 : this . opposite ( ) . add ( other ) . opposite ( ) ;
104110 }
105111 // /!\ _sub needs |c| >= |a| >= |b|
106112
107- const r = this . base ;
108- const a = this . limbs ;
113+ const r = this . _base ;
114+ const a = this . _limbs ;
109115 const aj = a . length ;
110116 const ai = _trim_positive ( a , 0 , aj ) ;
111117
@@ -122,14 +128,14 @@ export class Integer {
122128
123129 _sub ( r , b , bi , bj , a , ai , aj , c , 0 , c . length ) ;
124130
125- return new Integer ( r , ~ this . is_negative , c ) ;
131+ return new Integer ( r , ~ this . _is_negative , c ) ;
126132 }
127133
128134 const c = _zeros ( aj - ai ) ;
129135
130136 _sub ( r , a , ai , aj , b , bi , bj , c , 0 , c . length ) ;
131137
132- return new Integer ( r , this . is_negative , c ) ;
138+ return new Integer ( r , this . _is_negative , c ) ;
133139 }
134140
135141 isub ( other ) {
@@ -146,10 +152,10 @@ export class Integer {
146152 }
147153
148154 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 ;
151157
152- const a = this . limbs ;
158+ const a = this . _limbs ;
153159
154160 const b = other . _limbs_in_base ( r ) ;
155161
@@ -181,14 +187,14 @@ export class Integer {
181187 * @return {Integer } <code>this ^ x</code>
182188 */
183189 pown ( x ) {
184- const is_negative = this . is_negative & x & 1 ? - 1 : 0 ;
190+ const is_negative = this . _is_negative & x & 1 ? - 1 : 0 ;
185191
186- const a = this . limbs ;
192+ const a = this . _limbs ;
187193 const c = _zeros ( Math . max ( 1 , a . length * x ) ) ;
188194
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 ) ;
190196
191- return new Integer ( this . base , is_negative , c ) ;
197+ return new Integer ( this . _base , is_negative , c ) ;
192198 }
193199
194200 pow ( other ) {
@@ -254,27 +260,27 @@ export class Integer {
254260 divround ( other ) {
255261 const [ q , r ] = this . divmod ( other ) ;
256262 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 ) ;
258264 return q ;
259265 }
260266
261267 divmod ( other ) {
262268 if ( other . iszero ( ) ) throw new ZeroDivisionError ( 'Integer division by zero' ) ; // Optimize
263269
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 ;
266272
267273 // 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 ) ;
270276
271277 // Dividend is 0
272278 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 ] ) ] ;
274280
275281 // Dividend (& Remainder)
276282 const D = _alloc ( lj - li ) ;
277- _copy ( this . limbs , li , lj , D , 0 ) ;
283+ _copy ( this . _limbs , li , lj , D , 0 ) ;
278284
279285 // Divisor
280286 const d = other . _limbs_in_base ( r ) ;
@@ -289,9 +295,9 @@ export class Integer {
289295 const Q = new Integer ( r , quotient_is_negative , q ) ; // Quotient
290296 const R = new Integer ( r , 0 , D ) ; // Remainder
291297
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 ) {
295301 R . negate ( ) ; // TODO optimize
296302 } else {
297303 increment ( r , q , 0 , q . length ) ;
@@ -322,7 +328,7 @@ export class Integer {
322328 }
323329
324330 opposite ( ) {
325- return new Integer ( this . base , ~ this . is_negative , this . limbs ) ;
331+ return new Integer ( this . _base , ~ this . _is_negative , this . _limbs ) ;
326332 }
327333
328334 negate ( ) {
@@ -339,24 +345,24 @@ export class Integer {
339345 }
340346
341347 sign ( ) {
342- return this . iszero ( ) ? 0 : this . is_negative ? - 1 : 1 ;
348+ return this . iszero ( ) ? 0 : this . _is_negative ? - 1 : 1 ;
343349 }
344350
345351 iszero ( ) {
346- return jz ( this . limbs , 0 , this . limbs . length ) ;
352+ return jz ( this . _limbs , 0 , this . _limbs . length ) ;
347353 }
348354
349355 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 ) ;
352358 }
353359
354360 isnonzero ( ) {
355361 return ! this . iszero ( ) ;
356362 }
357363
358364 isnegative ( ) {
359- return this . is_negative === - 1 ;
365+ return this . _is_negative === - 1 ;
360366 }
361367
362368 ispositive ( ) {
@@ -404,7 +410,13 @@ export class Integer {
404410 digits ( base = DEFAULT_DISPLAY_BASE ) {
405411 // TODO Once #to is implemented we can rewrite this as
406412 // 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 ( ) ;
408420 }
409421
410422 bits ( ) {
@@ -425,17 +437,17 @@ export class Integer {
425437
426438 if ( this . iszero ( ) ) {
427439 if ( other . iszero ( ) ) return 0 ;
428- if ( other . is_negative ) return 1 ;
440+ if ( other . _is_negative ) return 1 ;
429441 return - 1 ;
430442 }
431443
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 ;
434446
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 ) ;
437449
438- return this . is_negative === 0
450+ return this . _is_negative === 0
439451 ? cmp ( a , 0 , a . length , b , 0 , b . length )
440452 : cmp ( b , 0 , b . length , a , 0 , a . length ) ;
441453 }
@@ -493,8 +505,8 @@ export class Integer {
493505 }
494506
495507 gcd ( other ) {
496- const r = this . base ;
497- const a = this . limbs ;
508+ const r = this . _base ;
509+ const a = this . _limbs ;
498510 const b = other . _limbs_in_base ( r ) ;
499511 const [ d , di , dj ] = euclidean_algorithm ( r , a , 0 , a . length , b , 0 , b . length ) ;
500512 const gcd = _alloc ( dj - di ) ;
@@ -503,8 +515,8 @@ export class Integer {
503515 }
504516
505517 egcd ( other ) {
506- const r = this . base ;
507- const a = this . limbs ;
518+ const r = this . _base ;
519+ const a = this . _limbs ;
508520 const b = other . _limbs_in_base ( r ) ;
509521 const [
510522 R0 ,
@@ -534,19 +546,19 @@ export class Integer {
534546 gcd : new Integer ( r , 0 , gcd ) ,
535547 x :
536548 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 )
538550 : new Integer ( r , 0 , [ 0 ] ) ,
539551 y :
540552 y . length > 0
541- ? new Integer ( r , other . is_negative ^ - ( steps % 2 ) , y )
553+ ? new Integer ( r , other . _is_negative ^ - ( steps % 2 ) , y )
542554 : new Integer ( r , 0 , [ 0 ] ) ,
543555 u :
544556 u . length > 0
545- ? new Integer ( r , this . is_negative ^ - ( steps % 2 ) , u )
557+ ? new Integer ( r , this . _is_negative ^ - ( steps % 2 ) , u )
546558 : new Integer ( r , 0 , [ 0 ] ) ,
547559 v :
548560 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 )
550562 : new Integer ( r , 0 , [ 0 ] ) ,
551563 } ;
552564 }
@@ -562,14 +574,14 @@ export class Integer {
562574 ) ;
563575
564576 const limbs = convert (
565- this . base ,
577+ this . _base ,
566578 MAX_BASE ,
567- this . limbs ,
579+ this . _limbs ,
568580 0 ,
569- this . limbs . length ,
581+ this . _limbs . length ,
570582 ) ;
571583
572- const sign = this . is_negative ? - 1 : 1 ;
584+ const sign = this . _is_negative ? - 1 : 1 ;
573585
574586 const value =
575587 limbs . length === 2 ? limbs [ 0 ] * MAX_BASE + limbs [ 1 ] : limbs [ 0 ] ;
0 commit comments