001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the
010 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
011 * express or implied. See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package com.google.common.primitives;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;
019
020import com.google.common.annotations.Beta;
021import com.google.common.annotations.GwtCompatible;
022
023import java.math.BigInteger;
024import java.util.Arrays;
025import java.util.Comparator;
026
027import javax.annotation.CheckReturnValue;
028
029/**
030 * Static utility methods pertaining to {@code long} primitives that interpret values as
031 * <i>unsigned</i> (that is, any negative value {@code x} is treated as the positive value
032 * {@code 2^64 + x}). The methods for which signedness is not an issue are in {@link Longs}, as
033 * well as signed versions of methods for which signedness is an issue.
034 *
035 * <p>In addition, this class provides several static methods for converting a {@code long} to a
036 * {@code String} and a {@code String} to a {@code long} that treat the {@code long} as an unsigned
037 * number.
038 *
039 * <p>Users of these utilities must be <i>extremely careful</i> not to mix up signed and unsigned
040 * {@code long} values. When possible, it is recommended that the {@link UnsignedLong} wrapper
041 * class be used, at a small efficiency penalty, to enforce the distinction in the type system.
042 *
043 * <p>See the Guava User Guide article on <a href=
044 * "https://github.com/google/guava/wiki/PrimitivesExplained#unsigned-support">
045 * unsigned primitive utilities</a>.
046 *
047 * @author Louis Wasserman
048 * @author Brian Milch
049 * @author Colin Evans
050 * @since 10.0
051 */
052@Beta
053@GwtCompatible
054public final class UnsignedLongs {
055  private UnsignedLongs() {}
056
057  public static final long MAX_VALUE = -1L; // Equivalent to 2^64 - 1
058
059  /**
060   * A (self-inverse) bijection which converts the ordering on unsigned longs to the ordering on
061   * longs, that is, {@code a <= b} as unsigned longs if and only if {@code flip(a) <= flip(b)}
062   * as signed longs.
063   */
064  private static long flip(long a) {
065    return a ^ Long.MIN_VALUE;
066  }
067
068  /**
069   * Compares the two specified {@code long} values, treating them as unsigned values between
070   * {@code 0} and {@code 2^64 - 1} inclusive.
071   *
072   * @param a the first unsigned {@code long} to compare
073   * @param b the second unsigned {@code long} to compare
074   * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
075   *         greater than {@code b}; or zero if they are equal
076   */
077  @CheckReturnValue
078  public static int compare(long a, long b) {
079    return Longs.compare(flip(a), flip(b));
080  }
081
082  /**
083   * Returns the least value present in {@code array}, treating values as unsigned.
084   *
085   * @param array a <i>nonempty</i> array of unsigned {@code long} values
086   * @return the value present in {@code array} that is less than or equal to every other value in
087   *         the array according to {@link #compare}
088   * @throws IllegalArgumentException if {@code array} is empty
089   */
090  @CheckReturnValue
091  public static long min(long... array) {
092    checkArgument(array.length > 0);
093    long min = flip(array[0]);
094    for (int i = 1; i < array.length; i++) {
095      long next = flip(array[i]);
096      if (next < min) {
097        min = next;
098      }
099    }
100    return flip(min);
101  }
102
103  /**
104   * Returns the greatest value present in {@code array}, treating values as unsigned.
105   *
106   * @param array a <i>nonempty</i> array of unsigned {@code long} values
107   * @return the value present in {@code array} that is greater than or equal to every other value
108   *         in the array according to {@link #compare}
109   * @throws IllegalArgumentException if {@code array} is empty
110   */
111  @CheckReturnValue
112  public static long max(long... array) {
113    checkArgument(array.length > 0);
114    long max = flip(array[0]);
115    for (int i = 1; i < array.length; i++) {
116      long next = flip(array[i]);
117      if (next > max) {
118        max = next;
119      }
120    }
121    return flip(max);
122  }
123
124  /**
125   * Returns a string containing the supplied unsigned {@code long} values separated by
126   * {@code separator}. For example, {@code join("-", 1, 2, 3)} returns the string {@code "1-2-3"}.
127   *
128   * @param separator the text that should appear between consecutive values in the resulting
129   *        string (but not at the start or end)
130   * @param array an array of unsigned {@code long} values, possibly empty
131   */
132  @CheckReturnValue
133  public static String join(String separator, long... array) {
134    checkNotNull(separator);
135    if (array.length == 0) {
136      return "";
137    }
138
139    // For pre-sizing a builder, just get the right order of magnitude
140    StringBuilder builder = new StringBuilder(array.length * 5);
141    builder.append(toString(array[0]));
142    for (int i = 1; i < array.length; i++) {
143      builder.append(separator).append(toString(array[i]));
144    }
145    return builder.toString();
146  }
147
148  /**
149   * Returns a comparator that compares two arrays of unsigned {@code long} values
150   * lexicographically. That is, it compares, using {@link #compare(long, long)}), the first pair of
151   * values that follow any common prefix, or when one array is a prefix of the other, treats the
152   * shorter array as the lesser. For example, {@code [] < [1L] < [1L, 2L] < [2L] < [1L << 63]}.
153   *
154   * <p>The returned comparator is inconsistent with {@link Object#equals(Object)} (since arrays
155   * support only identity equality), but it is consistent with
156   * {@link Arrays#equals(long[], long[])}.
157   *
158   * @see <a href="http://en.wikipedia.org/wiki/Lexicographical_order">Lexicographical order
159   *      article at Wikipedia</a>
160   */
161  @CheckReturnValue
162  public static Comparator<long[]> lexicographicalComparator() {
163    return LexicographicalComparator.INSTANCE;
164  }
165
166  enum LexicographicalComparator implements Comparator<long[]> {
167    INSTANCE;
168
169    @Override
170    public int compare(long[] left, long[] right) {
171      int minLength = Math.min(left.length, right.length);
172      for (int i = 0; i < minLength; i++) {
173        if (left[i] != right[i]) {
174          return UnsignedLongs.compare(left[i], right[i]);
175        }
176      }
177      return left.length - right.length;
178    }
179  }
180
181  /**
182   * Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit
183   * quantities.
184   *
185   * @param dividend the dividend (numerator)
186   * @param divisor the divisor (denominator)
187   * @throws ArithmeticException if divisor is 0
188   */
189  @CheckReturnValue
190  public static long divide(long dividend, long divisor) {
191    if (divisor < 0) { // i.e., divisor >= 2^63:
192      if (compare(dividend, divisor) < 0) {
193        return 0; // dividend < divisor
194      } else {
195        return 1; // dividend >= divisor
196      }
197    }
198
199    // Optimization - use signed division if dividend < 2^63
200    if (dividend >= 0) {
201      return dividend / divisor;
202    }
203
204    /*
205     * Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is
206     * guaranteed to be either exact or one less than the correct value. This follows from fact
207     * that floor(floor(x)/i) == floor(x/i) for any real x and integer i != 0. The proof is not
208     * quite trivial.
209     */
210    long quotient = ((dividend >>> 1) / divisor) << 1;
211    long rem = dividend - quotient * divisor;
212    return quotient + (compare(rem, divisor) >= 0 ? 1 : 0);
213  }
214
215  /**
216   * Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64-bit
217   * quantities.
218   *
219   * @param dividend the dividend (numerator)
220   * @param divisor the divisor (denominator)
221   * @throws ArithmeticException if divisor is 0
222   * @since 11.0
223   */
224  @CheckReturnValue
225  public static long remainder(long dividend, long divisor) {
226    if (divisor < 0) { // i.e., divisor >= 2^63:
227      if (compare(dividend, divisor) < 0) {
228        return dividend; // dividend < divisor
229      } else {
230        return dividend - divisor; // dividend >= divisor
231      }
232    }
233
234    // Optimization - use signed modulus if dividend < 2^63
235    if (dividend >= 0) {
236      return dividend % divisor;
237    }
238
239    /*
240     * Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is
241     * guaranteed to be either exact or one less than the correct value. This follows from fact
242     * that floor(floor(x)/i) == floor(x/i) for any real x and integer i != 0. The proof is not
243     * quite trivial.
244     */
245    long quotient = ((dividend >>> 1) / divisor) << 1;
246    long rem = dividend - quotient * divisor;
247    return rem - (compare(rem, divisor) >= 0 ? divisor : 0);
248  }
249
250  /**
251   * Returns the unsigned {@code long} value represented by the given decimal string.
252   *
253   * @throws NumberFormatException if the string does not contain a valid unsigned {@code long}
254   *         value
255   * @throws NullPointerException if {@code s} is null
256   *         (in contrast to {@link Long#parseLong(String)})
257   */
258  public static long parseUnsignedLong(String s) {
259    return parseUnsignedLong(s, 10);
260  }
261
262  /**
263   * Returns the unsigned {@code long} value represented by the given string.
264   *
265   * Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix:
266   *
267   * <ul>
268   * <li>{@code 0x}<i>HexDigits</i>
269   * <li>{@code 0X}<i>HexDigits</i>
270   * <li>{@code #}<i>HexDigits</i>
271   * <li>{@code 0}<i>OctalDigits</i>
272   * </ul>
273   *
274   * @throws NumberFormatException if the string does not contain a valid unsigned {@code long}
275   *         value
276   * @since 13.0
277   */
278  public static long decode(String stringValue) {
279    ParseRequest request = ParseRequest.fromString(stringValue);
280
281    try {
282      return parseUnsignedLong(request.rawValue, request.radix);
283    } catch (NumberFormatException e) {
284      NumberFormatException decodeException =
285          new NumberFormatException("Error parsing value: " + stringValue);
286      decodeException.initCause(e);
287      throw decodeException;
288    }
289  }
290
291  /**
292   * Returns the unsigned {@code long} value represented by a string with the given radix.
293   *
294   * @param s the string containing the unsigned {@code long} representation to be parsed.
295   * @param radix the radix to use while parsing {@code s}
296   * @throws NumberFormatException if the string does not contain a valid unsigned {@code long}
297   *         with the given radix, or if {@code radix} is not between {@link Character#MIN_RADIX}
298   *         and {@link Character#MAX_RADIX}.
299   * @throws NullPointerException if {@code s} is null
300   *         (in contrast to {@link Long#parseLong(String)})
301   */
302  public static long parseUnsignedLong(String s, int radix) {
303    checkNotNull(s);
304    if (s.length() == 0) {
305      throw new NumberFormatException("empty string");
306    }
307    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
308      throw new NumberFormatException("illegal radix: " + radix);
309    }
310
311    int max_safe_pos = maxSafeDigits[radix] - 1;
312    long value = 0;
313    for (int pos = 0; pos < s.length(); pos++) {
314      int digit = Character.digit(s.charAt(pos), radix);
315      if (digit == -1) {
316        throw new NumberFormatException(s);
317      }
318      if (pos > max_safe_pos && overflowInParse(value, digit, radix)) {
319        throw new NumberFormatException("Too large for unsigned long: " + s);
320      }
321      value = (value * radix) + digit;
322    }
323
324    return value;
325  }
326
327  /**
328   * Returns true if (current * radix) + digit is a number too large to be represented by an
329   * unsigned long. This is useful for detecting overflow while parsing a string representation of
330   * a number. Does not verify whether supplied radix is valid, passing an invalid radix will give
331   * undefined results or an ArrayIndexOutOfBoundsException.
332   */
333  private static boolean overflowInParse(long current, int digit, int radix) {
334    if (current >= 0) {
335      if (current < maxValueDivs[radix]) {
336        return false;
337      }
338      if (current > maxValueDivs[radix]) {
339        return true;
340      }
341      // current == maxValueDivs[radix]
342      return (digit > maxValueMods[radix]);
343    }
344
345    // current < 0: high bit is set
346    return true;
347  }
348
349  /**
350   * Returns a string representation of x, where x is treated as unsigned.
351   */
352  @CheckReturnValue
353  public static String toString(long x) {
354    return toString(x, 10);
355  }
356
357  /**
358   * Returns a string representation of {@code x} for the given radix, where {@code x} is treated
359   * as unsigned.
360   *
361   * @param x the value to convert to a string.
362   * @param radix the radix to use while working with {@code x}
363   * @throws IllegalArgumentException if {@code radix} is not between {@link Character#MIN_RADIX}
364   *         and {@link Character#MAX_RADIX}.
365   */
366  @CheckReturnValue
367  public static String toString(long x, int radix) {
368    checkArgument(
369        radix >= Character.MIN_RADIX && radix <= Character.MAX_RADIX,
370        "radix (%s) must be between Character.MIN_RADIX and Character.MAX_RADIX",
371        radix);
372    if (x == 0) {
373      // Simply return "0"
374      return "0";
375    } else {
376      char[] buf = new char[64];
377      int i = buf.length;
378      if (x < 0) {
379        // Separate off the last digit using unsigned division. That will leave
380        // a number that is nonnegative as a signed integer.
381        long quotient = divide(x, radix);
382        long rem = x - quotient * radix;
383        buf[--i] = Character.forDigit((int) rem, radix);
384        x = quotient;
385      }
386      // Simple modulo/division approach
387      while (x > 0) {
388        buf[--i] = Character.forDigit((int) (x % radix), radix);
389        x /= radix;
390      }
391      // Generate string
392      return new String(buf, i, buf.length - i);
393    }
394  }
395
396  // calculated as 0xffffffffffffffff / radix
397  private static final long[] maxValueDivs = new long[Character.MAX_RADIX + 1];
398  private static final int[] maxValueMods = new int[Character.MAX_RADIX + 1];
399  private static final int[] maxSafeDigits = new int[Character.MAX_RADIX + 1];
400
401  static {
402    BigInteger overflow = new BigInteger("10000000000000000", 16);
403    for (int i = Character.MIN_RADIX; i <= Character.MAX_RADIX; i++) {
404      maxValueDivs[i] = divide(MAX_VALUE, i);
405      maxValueMods[i] = (int) remainder(MAX_VALUE, i);
406      maxSafeDigits[i] = overflow.toString(i).length() - 1;
407    }
408  }
409}