Path: blob/master/src/java.base/share/classes/java/math/SignedMutableBigInteger.java
41152 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.math;2627/**28* A class used to represent multiprecision integers that makes efficient29* use of allocated space by allowing a number to occupy only part of30* an array so that the arrays do not have to be reallocated as often.31* When performing an operation with many iterations the array used to32* hold a number is only increased when necessary and does not have to33* be the same size as the number it represents. A mutable number allows34* calculations to occur on the same number without having to create35* a new number for every step of the calculation as occurs with36* BigIntegers.37*38* Note that SignedMutableBigIntegers only support signed addition and39* subtraction. All other operations occur as with MutableBigIntegers.40*41* @see BigInteger42* @author Michael McCloskey43* @since 1.344*/4546class SignedMutableBigInteger extends MutableBigInteger {4748/**49* The sign of this MutableBigInteger.50*/51int sign = 1;5253// Constructors5455/**56* The default constructor. An empty MutableBigInteger is created with57* a one word capacity.58*/59SignedMutableBigInteger() {60super();61}6263/**64* Construct a new MutableBigInteger with a magnitude specified by65* the int val.66*/67SignedMutableBigInteger(int val) {68super(val);69}7071/**72* Construct a new MutableBigInteger with a magnitude equal to the73* specified MutableBigInteger.74*/75SignedMutableBigInteger(MutableBigInteger val) {76super(val);77}7879// Arithmetic Operations8081/**82* Signed addition built upon unsigned add and subtract.83*/84void signedAdd(SignedMutableBigInteger addend) {85if (sign == addend.sign)86add(addend);87else88sign = sign * subtract(addend);8990}9192/**93* Signed addition built upon unsigned add and subtract.94*/95void signedAdd(MutableBigInteger addend) {96if (sign == 1)97add(addend);98else99sign = sign * subtract(addend);100101}102103/**104* Signed subtraction built upon unsigned add and subtract.105*/106void signedSubtract(SignedMutableBigInteger addend) {107if (sign == addend.sign)108sign = sign * subtract(addend);109else110add(addend);111112}113114/**115* Signed subtraction built upon unsigned add and subtract.116*/117void signedSubtract(MutableBigInteger addend) {118if (sign == 1)119sign = sign * subtract(addend);120else121add(addend);122if (intLen == 0)123sign = 1;124}125126/**127* Print out the first intLen ints of this MutableBigInteger's value128* array starting at offset.129*/130public String toString() {131return this.toBigInteger(sign).toString();132}133134}135136137