jaroslav@1258: /* jaroslav@1258: * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved. jaroslav@1258: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1258: * jaroslav@1258: * This code is free software; you can redistribute it and/or modify it jaroslav@1258: * under the terms of the GNU General Public License version 2 only, as jaroslav@1258: * published by the Free Software Foundation. Oracle designates this jaroslav@1258: * particular file as subject to the "Classpath" exception as provided jaroslav@1258: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1258: * jaroslav@1258: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1258: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1258: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1258: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1258: * accompanied this code). jaroslav@1258: * jaroslav@1258: * You should have received a copy of the GNU General Public License version jaroslav@1258: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1258: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1258: * jaroslav@1258: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1258: * or visit www.oracle.com if you need additional information or have any jaroslav@1258: * questions. jaroslav@1258: */ jaroslav@1258: jaroslav@1258: package java.math; jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * A class used to represent multiprecision integers that makes efficient jaroslav@1258: * use of allocated space by allowing a number to occupy only part of jaroslav@1258: * an array so that the arrays do not have to be reallocated as often. jaroslav@1258: * When performing an operation with many iterations the array used to jaroslav@1258: * hold a number is only increased when necessary and does not have to jaroslav@1258: * be the same size as the number it represents. A mutable number allows jaroslav@1258: * calculations to occur on the same number without having to create jaroslav@1258: * a new number for every step of the calculation as occurs with jaroslav@1258: * BigIntegers. jaroslav@1258: * jaroslav@1258: * Note that SignedMutableBigIntegers only support signed addition and jaroslav@1258: * subtraction. All other operations occur as with MutableBigIntegers. jaroslav@1258: * jaroslav@1258: * @see BigInteger jaroslav@1258: * @author Michael McCloskey jaroslav@1258: * @since 1.3 jaroslav@1258: */ jaroslav@1258: jaroslav@1258: class SignedMutableBigInteger extends MutableBigInteger { jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * The sign of this MutableBigInteger. jaroslav@1258: */ jaroslav@1258: int sign = 1; jaroslav@1258: jaroslav@1258: // Constructors jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * The default constructor. An empty MutableBigInteger is created with jaroslav@1258: * a one word capacity. jaroslav@1258: */ jaroslav@1258: SignedMutableBigInteger() { jaroslav@1258: super(); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Construct a new MutableBigInteger with a magnitude specified by jaroslav@1258: * the int val. jaroslav@1258: */ jaroslav@1258: SignedMutableBigInteger(int val) { jaroslav@1258: super(val); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Construct a new MutableBigInteger with a magnitude equal to the jaroslav@1258: * specified MutableBigInteger. jaroslav@1258: */ jaroslav@1258: SignedMutableBigInteger(MutableBigInteger val) { jaroslav@1258: super(val); jaroslav@1258: } jaroslav@1258: jaroslav@1258: // Arithmetic Operations jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Signed addition built upon unsigned add and subtract. jaroslav@1258: */ jaroslav@1258: void signedAdd(SignedMutableBigInteger addend) { jaroslav@1258: if (sign == addend.sign) jaroslav@1258: add(addend); jaroslav@1258: else jaroslav@1258: sign = sign * subtract(addend); jaroslav@1258: jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Signed addition built upon unsigned add and subtract. jaroslav@1258: */ jaroslav@1258: void signedAdd(MutableBigInteger addend) { jaroslav@1258: if (sign == 1) jaroslav@1258: add(addend); jaroslav@1258: else jaroslav@1258: sign = sign * subtract(addend); jaroslav@1258: jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Signed subtraction built upon unsigned add and subtract. jaroslav@1258: */ jaroslav@1258: void signedSubtract(SignedMutableBigInteger addend) { jaroslav@1258: if (sign == addend.sign) jaroslav@1258: sign = sign * subtract(addend); jaroslav@1258: else jaroslav@1258: add(addend); jaroslav@1258: jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Signed subtraction built upon unsigned add and subtract. jaroslav@1258: */ jaroslav@1258: void signedSubtract(MutableBigInteger addend) { jaroslav@1258: if (sign == 1) jaroslav@1258: sign = sign * subtract(addend); jaroslav@1258: else jaroslav@1258: add(addend); jaroslav@1258: if (intLen == 0) jaroslav@1258: sign = 1; jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Print out the first intLen ints of this MutableBigInteger's value jaroslav@1258: * array starting at offset. jaroslav@1258: */ jaroslav@1258: public String toString() { jaroslav@1258: return this.toBigInteger(sign).toString(); jaroslav@1258: } jaroslav@1258: jaroslav@1258: }