emul/compact/src/main/java/java/math/SignedMutableBigInteger.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Sep 2013 13:51:24 +0200
branchjdk7-b147
changeset 1258 724f3e1ea53e
permissions -rw-r--r--
Additional set of classes to make porting of lookup library more easier
jaroslav@1258
     1
/*
jaroslav@1258
     2
 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
jaroslav@1258
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1258
     4
 *
jaroslav@1258
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1258
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1258
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1258
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1258
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1258
    10
 *
jaroslav@1258
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1258
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1258
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1258
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1258
    15
 * accompanied this code).
jaroslav@1258
    16
 *
jaroslav@1258
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1258
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1258
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1258
    20
 *
jaroslav@1258
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1258
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1258
    23
 * questions.
jaroslav@1258
    24
 */
jaroslav@1258
    25
jaroslav@1258
    26
package java.math;
jaroslav@1258
    27
jaroslav@1258
    28
/**
jaroslav@1258
    29
 * A class used to represent multiprecision integers that makes efficient
jaroslav@1258
    30
 * use of allocated space by allowing a number to occupy only part of
jaroslav@1258
    31
 * an array so that the arrays do not have to be reallocated as often.
jaroslav@1258
    32
 * When performing an operation with many iterations the array used to
jaroslav@1258
    33
 * hold a number is only increased when necessary and does not have to
jaroslav@1258
    34
 * be the same size as the number it represents. A mutable number allows
jaroslav@1258
    35
 * calculations to occur on the same number without having to create
jaroslav@1258
    36
 * a new number for every step of the calculation as occurs with
jaroslav@1258
    37
 * BigIntegers.
jaroslav@1258
    38
 *
jaroslav@1258
    39
 * Note that SignedMutableBigIntegers only support signed addition and
jaroslav@1258
    40
 * subtraction. All other operations occur as with MutableBigIntegers.
jaroslav@1258
    41
 *
jaroslav@1258
    42
 * @see     BigInteger
jaroslav@1258
    43
 * @author  Michael McCloskey
jaroslav@1258
    44
 * @since   1.3
jaroslav@1258
    45
 */
jaroslav@1258
    46
jaroslav@1258
    47
class SignedMutableBigInteger extends MutableBigInteger {
jaroslav@1258
    48
jaroslav@1258
    49
   /**
jaroslav@1258
    50
     * The sign of this MutableBigInteger.
jaroslav@1258
    51
     */
jaroslav@1258
    52
    int sign = 1;
jaroslav@1258
    53
jaroslav@1258
    54
    // Constructors
jaroslav@1258
    55
jaroslav@1258
    56
    /**
jaroslav@1258
    57
     * The default constructor. An empty MutableBigInteger is created with
jaroslav@1258
    58
     * a one word capacity.
jaroslav@1258
    59
     */
jaroslav@1258
    60
    SignedMutableBigInteger() {
jaroslav@1258
    61
        super();
jaroslav@1258
    62
    }
jaroslav@1258
    63
jaroslav@1258
    64
    /**
jaroslav@1258
    65
     * Construct a new MutableBigInteger with a magnitude specified by
jaroslav@1258
    66
     * the int val.
jaroslav@1258
    67
     */
jaroslav@1258
    68
    SignedMutableBigInteger(int val) {
jaroslav@1258
    69
        super(val);
jaroslav@1258
    70
    }
jaroslav@1258
    71
jaroslav@1258
    72
    /**
jaroslav@1258
    73
     * Construct a new MutableBigInteger with a magnitude equal to the
jaroslav@1258
    74
     * specified MutableBigInteger.
jaroslav@1258
    75
     */
jaroslav@1258
    76
    SignedMutableBigInteger(MutableBigInteger val) {
jaroslav@1258
    77
        super(val);
jaroslav@1258
    78
    }
jaroslav@1258
    79
jaroslav@1258
    80
   // Arithmetic Operations
jaroslav@1258
    81
jaroslav@1258
    82
   /**
jaroslav@1258
    83
     * Signed addition built upon unsigned add and subtract.
jaroslav@1258
    84
     */
jaroslav@1258
    85
    void signedAdd(SignedMutableBigInteger addend) {
jaroslav@1258
    86
        if (sign == addend.sign)
jaroslav@1258
    87
            add(addend);
jaroslav@1258
    88
        else
jaroslav@1258
    89
            sign = sign * subtract(addend);
jaroslav@1258
    90
jaroslav@1258
    91
    }
jaroslav@1258
    92
jaroslav@1258
    93
   /**
jaroslav@1258
    94
     * Signed addition built upon unsigned add and subtract.
jaroslav@1258
    95
     */
jaroslav@1258
    96
    void signedAdd(MutableBigInteger addend) {
jaroslav@1258
    97
        if (sign == 1)
jaroslav@1258
    98
            add(addend);
jaroslav@1258
    99
        else
jaroslav@1258
   100
            sign = sign * subtract(addend);
jaroslav@1258
   101
jaroslav@1258
   102
    }
jaroslav@1258
   103
jaroslav@1258
   104
   /**
jaroslav@1258
   105
     * Signed subtraction built upon unsigned add and subtract.
jaroslav@1258
   106
     */
jaroslav@1258
   107
    void signedSubtract(SignedMutableBigInteger addend) {
jaroslav@1258
   108
        if (sign == addend.sign)
jaroslav@1258
   109
            sign = sign * subtract(addend);
jaroslav@1258
   110
        else
jaroslav@1258
   111
            add(addend);
jaroslav@1258
   112
jaroslav@1258
   113
    }
jaroslav@1258
   114
jaroslav@1258
   115
   /**
jaroslav@1258
   116
     * Signed subtraction built upon unsigned add and subtract.
jaroslav@1258
   117
     */
jaroslav@1258
   118
    void signedSubtract(MutableBigInteger addend) {
jaroslav@1258
   119
        if (sign == 1)
jaroslav@1258
   120
            sign = sign * subtract(addend);
jaroslav@1258
   121
        else
jaroslav@1258
   122
            add(addend);
jaroslav@1258
   123
        if (intLen == 0)
jaroslav@1258
   124
             sign = 1;
jaroslav@1258
   125
    }
jaroslav@1258
   126
jaroslav@1258
   127
    /**
jaroslav@1258
   128
     * Print out the first intLen ints of this MutableBigInteger's value
jaroslav@1258
   129
     * array starting at offset.
jaroslav@1258
   130
     */
jaroslav@1258
   131
    public String toString() {
jaroslav@1258
   132
        return this.toBigInteger(sign).toString();
jaroslav@1258
   133
    }
jaroslav@1258
   134
jaroslav@1258
   135
}