Skip to content

Commit 0e83d70

Browse files
committed
Convert std::bigint to explicit self.
1 parent c6a8778 commit 0e83d70

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

src/libstd/bigint.rs

+52-45
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,10 @@ pub impl BigUint {
275275

276276
static pub pure fn zero() -> BigUint { BigUint::from_at_vec(@[]) }
277277
static pub pure fn one() -> BigUint { BigUint::from_at_vec(@[1]) }
278-
279-
pure fn abs() -> BigUint { self }
278+
pure fn abs(&self) -> BigUint { *self }
280279

281280
/// Compare two BigUint value.
282-
pure fn cmp(other: &BigUint) -> int {
281+
pure fn cmp(&self, other: &BigUint) -> int {
283282
let s_len = self.data.len(), o_len = other.data.len();
284283
if s_len < o_len { return -1; }
285284
if s_len > o_len { return 1; }
@@ -294,13 +293,13 @@ pub impl BigUint {
294293
return 0;
295294
}
296295

297-
pure fn divmod(other: &BigUint) -> (BigUint, BigUint) {
296+
pure fn divmod(&self, other: &BigUint) -> (BigUint, BigUint) {
298297
if other.is_zero() { fail }
299298
if self.is_zero() { return (BigUint::zero(), BigUint::zero()); }
300-
if *other == BigUint::one() { return (self, BigUint::zero()); }
299+
if *other == BigUint::one() { return (*self, BigUint::zero()); }
301300

302301
match self.cmp(other) {
303-
s if s < 0 => return (BigUint::zero(), self),
302+
s if s < 0 => return (BigUint::zero(), *self),
304303
0 => return (BigUint::one(), BigUint::zero()),
305304
_ => {} // Do nothing
306305
}
@@ -363,20 +362,24 @@ pub impl BigUint {
363362
}
364363
}
365364

366-
pure fn quot(other: &BigUint) -> BigUint { self.quotrem(other).first() }
367-
pure fn rem(other: &BigUint) -> BigUint { self.quotrem(other).second() }
368-
pure fn quotrem(other: &BigUint) -> (BigUint, BigUint) {
365+
pure fn quot(&self, other: &BigUint) -> BigUint {
366+
self.quotrem(other).first()
367+
}
368+
pure fn rem(&self, other: &BigUint) -> BigUint {
369+
self.quotrem(other).second()
370+
}
371+
pure fn quotrem(&self, other: &BigUint) -> (BigUint, BigUint) {
369372
self.divmod(other)
370373
}
371374

372-
pure fn is_zero() -> bool { self.data.is_empty() }
373-
pure fn is_not_zero() -> bool { self.data.is_not_empty() }
374-
pure fn is_positive() -> bool { self.is_not_zero() }
375-
pure fn is_negative() -> bool { false }
376-
pure fn is_nonpositive() -> bool { self.is_zero() }
377-
pure fn is_nonnegative() -> bool { true }
375+
pure fn is_zero(&self) -> bool { self.data.is_empty() }
376+
pure fn is_not_zero(&self) -> bool { self.data.is_not_empty() }
377+
pure fn is_positive(&self) -> bool { self.is_not_zero() }
378+
pure fn is_negative(&self) -> bool { false }
379+
pure fn is_nonpositive(&self) -> bool { self.is_zero() }
380+
pure fn is_nonnegative(&self) -> bool { true }
378381

379-
pure fn to_uint() -> uint {
382+
pure fn to_uint(&self) -> uint {
380383
match self.data.len() {
381384
0 => 0,
382385
1 => self.data[0] as uint,
@@ -385,14 +388,14 @@ pub impl BigUint {
385388
}
386389
}
387390

388-
pure fn to_str_radix(radix: uint) -> ~str {
391+
pure fn to_str_radix(&self, radix: uint) -> ~str {
389392
assert 1 < radix && radix <= 16;
390393

391-
pure fn convert_base(n: BigUint, base: uint) -> @[BigDigit] {
394+
pure fn convert_base(n: &BigUint, base: uint) -> @[BigDigit] {
392395
if base == BigDigit::base { return n.data; }
393396
let divider = BigUint::from_uint(base);
394397
let mut result = @[];
395-
let mut r = n;
398+
let mut r = *n;
396399
while r > divider {
397400
let (d, r0) = r.divmod(&divider);
398401
result += [r0.to_uint() as BigDigit];
@@ -416,14 +419,14 @@ pub impl BigUint {
416419
return fill_concat(convert_base(self, base), radix, max_len);
417420
}
418421

419-
priv pure fn shl_unit(n_unit: uint) -> BigUint {
420-
if n_unit == 0 || self.is_zero() { return self; }
422+
priv pure fn shl_unit(&self, n_unit: uint) -> BigUint {
423+
if n_unit == 0 || self.is_zero() { return *self; }
421424

422425
return BigUint::from_at_vec(at_vec::from_elem(n_unit, 0) + self.data);
423426
}
424427

425-
priv pure fn shl_bits(n_bits: uint) -> BigUint {
426-
if n_bits == 0 || self.is_zero() { return self; }
428+
priv pure fn shl_bits(&self, n_bits: uint) -> BigUint {
429+
if n_bits == 0 || self.is_zero() { return *self; }
427430

428431
let mut carry = 0;
429432
let shifted = do at_vec::map(self.data) |elem| {
@@ -437,16 +440,16 @@ pub impl BigUint {
437440
return BigUint::from_at_vec(shifted + [carry]);
438441
}
439442

440-
priv pure fn shr_unit(n_unit: uint) -> BigUint {
441-
if n_unit == 0 { return self; }
443+
priv pure fn shr_unit(&self, n_unit: uint) -> BigUint {
444+
if n_unit == 0 { return *self; }
442445
if self.data.len() < n_unit { return BigUint::zero(); }
443446
return BigUint::from_slice(
444447
vec::view(self.data, n_unit, self.data.len())
445448
);
446449
}
447450

448-
priv pure fn shr_bits(n_bits: uint) -> BigUint {
449-
if n_bits == 0 || self.data.is_empty() { return self; }
451+
priv pure fn shr_bits(&self, n_bits: uint) -> BigUint {
452+
if n_bits == 0 || self.data.is_empty() { return *self; }
450453

451454
let mut borrow = 0;
452455
let mut shifted = @[];
@@ -499,17 +502,17 @@ impl Sign : Ord {
499502

500503
pub impl Sign {
501504
/// Compare two Sign.
502-
pure fn cmp(other: &Sign) -> int {
503-
match (self, *other) {
505+
pure fn cmp(&self, other: &Sign) -> int {
506+
match (*self, *other) {
504507
(Minus, Minus) | (Zero, Zero) | (Plus, Plus) => 0,
505508
(Minus, Zero) | (Minus, Plus) | (Zero, Plus) => -1,
506509
_ => 1
507510
}
508511
}
509512

510513
/// Negate Sign value.
511-
pure fn neg() -> Sign {
512-
match(self) {
514+
pure fn neg(&self) -> Sign {
515+
match *self {
513516
Minus => Plus,
514517
Zero => Zero,
515518
Plus => Minus
@@ -682,9 +685,9 @@ pub impl BigInt {
682685
BigInt::from_biguint(Plus, BigUint::one())
683686
}
684687

685-
pure fn abs() -> BigInt { BigInt::from_biguint(Plus, self.data) }
688+
pure fn abs(&self) -> BigInt { BigInt::from_biguint(Plus, self.data) }
686689

687-
pure fn cmp(other: &BigInt) -> int {
690+
pure fn cmp(&self, other: &BigInt) -> int {
688691
let ss = self.sign, os = other.sign;
689692
if ss < os { return -1; }
690693
if ss > os { return 1; }
@@ -697,7 +700,7 @@ pub impl BigInt {
697700
}
698701
}
699702

700-
pure fn divmod(other: &BigInt) -> (BigInt, BigInt) {
703+
pure fn divmod(&self, other: &BigInt) -> (BigInt, BigInt) {
701704
// m.sign == other.sign
702705
let (d_ui, m_ui) = self.data.divmod(&other.data);
703706
let d = BigInt::from_biguint(Plus, d_ui),
@@ -719,10 +722,14 @@ pub impl BigInt {
719722
}
720723
}
721724

722-
pure fn quot(other: &BigInt) -> BigInt { self.quotrem(other).first() }
723-
pure fn rem(other: &BigInt) -> BigInt { self.quotrem(other).second() }
725+
pure fn quot(&self, other: &BigInt) -> BigInt {
726+
self.quotrem(other).first()
727+
}
728+
pure fn rem(&self, other: &BigInt) -> BigInt {
729+
self.quotrem(other).second()
730+
}
724731

725-
pure fn quotrem(other: &BigInt) -> (BigInt, BigInt) {
732+
pure fn quotrem(&self, other: &BigInt) -> (BigInt, BigInt) {
726733
// r.sign == self.sign
727734
let (q_ui, r_ui) = self.data.quotrem(&other.data);
728735
let q = BigInt::from_biguint(Plus, q_ui);
@@ -736,22 +743,22 @@ pub impl BigInt {
736743
}
737744
}
738745

739-
pure fn is_zero() -> bool { self.sign == Zero }
740-
pure fn is_not_zero() -> bool { self.sign != Zero }
741-
pure fn is_positive() -> bool { self.sign == Plus }
742-
pure fn is_negative() -> bool { self.sign == Minus }
743-
pure fn is_nonpositive() -> bool { self.sign != Plus }
744-
pure fn is_nonnegative() -> bool { self.sign != Minus }
746+
pure fn is_zero(&self) -> bool { self.sign == Zero }
747+
pure fn is_not_zero(&self) -> bool { self.sign != Zero }
748+
pure fn is_positive(&self) -> bool { self.sign == Plus }
749+
pure fn is_negative(&self) -> bool { self.sign == Minus }
750+
pure fn is_nonpositive(&self) -> bool { self.sign != Plus }
751+
pure fn is_nonnegative(&self) -> bool { self.sign != Minus }
745752

746-
pure fn to_uint() -> uint {
753+
pure fn to_uint(&self) -> uint {
747754
match self.sign {
748755
Plus => self.data.to_uint(),
749756
Zero => 0,
750757
Minus => 0
751758
}
752759
}
753760

754-
pure fn to_str_radix(radix: uint) -> ~str {
761+
pure fn to_str_radix(&self, radix: uint) -> ~str {
755762
match self.sign {
756763
Plus => self.data.to_str_radix(radix),
757764
Zero => ~"0",

0 commit comments

Comments
 (0)