1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/math/SignedMutableBigInteger.java Sat Sep 07 13:51:24 2013 +0200
1.3 @@ -0,0 +1,135 @@
1.4 +/*
1.5 + * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.math;
1.30 +
1.31 +/**
1.32 + * A class used to represent multiprecision integers that makes efficient
1.33 + * use of allocated space by allowing a number to occupy only part of
1.34 + * an array so that the arrays do not have to be reallocated as often.
1.35 + * When performing an operation with many iterations the array used to
1.36 + * hold a number is only increased when necessary and does not have to
1.37 + * be the same size as the number it represents. A mutable number allows
1.38 + * calculations to occur on the same number without having to create
1.39 + * a new number for every step of the calculation as occurs with
1.40 + * BigIntegers.
1.41 + *
1.42 + * Note that SignedMutableBigIntegers only support signed addition and
1.43 + * subtraction. All other operations occur as with MutableBigIntegers.
1.44 + *
1.45 + * @see BigInteger
1.46 + * @author Michael McCloskey
1.47 + * @since 1.3
1.48 + */
1.49 +
1.50 +class SignedMutableBigInteger extends MutableBigInteger {
1.51 +
1.52 + /**
1.53 + * The sign of this MutableBigInteger.
1.54 + */
1.55 + int sign = 1;
1.56 +
1.57 + // Constructors
1.58 +
1.59 + /**
1.60 + * The default constructor. An empty MutableBigInteger is created with
1.61 + * a one word capacity.
1.62 + */
1.63 + SignedMutableBigInteger() {
1.64 + super();
1.65 + }
1.66 +
1.67 + /**
1.68 + * Construct a new MutableBigInteger with a magnitude specified by
1.69 + * the int val.
1.70 + */
1.71 + SignedMutableBigInteger(int val) {
1.72 + super(val);
1.73 + }
1.74 +
1.75 + /**
1.76 + * Construct a new MutableBigInteger with a magnitude equal to the
1.77 + * specified MutableBigInteger.
1.78 + */
1.79 + SignedMutableBigInteger(MutableBigInteger val) {
1.80 + super(val);
1.81 + }
1.82 +
1.83 + // Arithmetic Operations
1.84 +
1.85 + /**
1.86 + * Signed addition built upon unsigned add and subtract.
1.87 + */
1.88 + void signedAdd(SignedMutableBigInteger addend) {
1.89 + if (sign == addend.sign)
1.90 + add(addend);
1.91 + else
1.92 + sign = sign * subtract(addend);
1.93 +
1.94 + }
1.95 +
1.96 + /**
1.97 + * Signed addition built upon unsigned add and subtract.
1.98 + */
1.99 + void signedAdd(MutableBigInteger addend) {
1.100 + if (sign == 1)
1.101 + add(addend);
1.102 + else
1.103 + sign = sign * subtract(addend);
1.104 +
1.105 + }
1.106 +
1.107 + /**
1.108 + * Signed subtraction built upon unsigned add and subtract.
1.109 + */
1.110 + void signedSubtract(SignedMutableBigInteger addend) {
1.111 + if (sign == addend.sign)
1.112 + sign = sign * subtract(addend);
1.113 + else
1.114 + add(addend);
1.115 +
1.116 + }
1.117 +
1.118 + /**
1.119 + * Signed subtraction built upon unsigned add and subtract.
1.120 + */
1.121 + void signedSubtract(MutableBigInteger addend) {
1.122 + if (sign == 1)
1.123 + sign = sign * subtract(addend);
1.124 + else
1.125 + add(addend);
1.126 + if (intLen == 0)
1.127 + sign = 1;
1.128 + }
1.129 +
1.130 + /**
1.131 + * Print out the first intLen ints of this MutableBigInteger's value
1.132 + * array starting at offset.
1.133 + */
1.134 + public String toString() {
1.135 + return this.toBigInteger(sign).toString();
1.136 + }
1.137 +
1.138 +}