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 License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * 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; 019import static com.google.common.base.Preconditions.checkPositionIndexes; 020 021import com.google.common.annotations.GwtCompatible; 022import com.google.errorprone.annotations.CanIgnoreReturnValue; 023import java.math.BigInteger; 024import java.util.Arrays; 025import java.util.Comparator; 026 027/** 028 * Static utility methods pertaining to {@code long} primitives that interpret values as 029 * <i>unsigned</i> (that is, any negative value {@code x} is treated as the positive value {@code 030 * 2^64 + x}). The methods for which signedness is not an issue are in {@link Longs}, as well as 031 * signed versions of methods for which signedness is an issue. 032 * 033 * <p>In addition, this class provides several static methods for converting a {@code long} to a 034 * {@code String} and a {@code String} to a {@code long} that treat the {@code long} as an unsigned 035 * number. 036 * 037 * <p>Users of these utilities must be <i>extremely careful</i> not to mix up signed and unsigned 038 * {@code long} values. When possible, it is recommended that the {@link UnsignedLong} wrapper class 039 * be used, at a small efficiency penalty, to enforce the distinction in the type system. 040 * 041 * <p>See the Guava User Guide article on <a 042 * href="https://github.com/google/guava/wiki/PrimitivesExplained#unsigned-support">unsigned 043 * primitive utilities</a>. 044 * 045 * @author Louis Wasserman 046 * @author Brian Milch 047 * @author Colin Evans 048 * @since 10.0 049 */ 050@GwtCompatible 051@ElementTypesAreNonnullByDefault 052public final class UnsignedLongs { 053 private UnsignedLongs() {} 054 055 public static final long MAX_VALUE = -1L; // Equivalent to 2^64 - 1 056 057 /** 058 * A (self-inverse) bijection which converts the ordering on unsigned longs to the ordering on 059 * longs, that is, {@code a <= b} as unsigned longs if and only if {@code flip(a) <= flip(b)} as 060 * signed longs. 061 */ 062 private static long flip(long a) { 063 return a ^ Long.MIN_VALUE; 064 } 065 066 /** 067 * Compares the two specified {@code long} values, treating them as unsigned values between {@code 068 * 0} and {@code 2^64 - 1} inclusive. 069 * 070 * <p><b>Java 8+ users:</b> use {@link Long#compareUnsigned(long, long)} instead. 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 public static int compare(long a, long b) { 078 return Longs.compare(flip(a), flip(b)); 079 } 080 081 /** 082 * Returns the least value present in {@code array}, treating values as unsigned. 083 * 084 * @param array a <i>nonempty</i> array of unsigned {@code long} values 085 * @return the value present in {@code array} that is less than or equal to every other value in 086 * the array according to {@link #compare} 087 * @throws IllegalArgumentException if {@code array} is empty 088 */ 089 public static long min(long... array) { 090 checkArgument(array.length > 0); 091 long min = flip(array[0]); 092 for (int i = 1; i < array.length; i++) { 093 long next = flip(array[i]); 094 if (next < min) { 095 min = next; 096 } 097 } 098 return flip(min); 099 } 100 101 /** 102 * Returns the greatest value present in {@code array}, treating values as unsigned. 103 * 104 * @param array a <i>nonempty</i> array of unsigned {@code long} values 105 * @return the value present in {@code array} that is greater than or equal to every other value 106 * in the array according to {@link #compare} 107 * @throws IllegalArgumentException if {@code array} is empty 108 */ 109 public static long max(long... array) { 110 checkArgument(array.length > 0); 111 long max = flip(array[0]); 112 for (int i = 1; i < array.length; i++) { 113 long next = flip(array[i]); 114 if (next > max) { 115 max = next; 116 } 117 } 118 return flip(max); 119 } 120 121 /** 122 * Returns a string containing the supplied unsigned {@code long} values separated by {@code 123 * separator}. For example, {@code join("-", 1, 2, 3)} returns the string {@code "1-2-3"}. 124 * 125 * @param separator the text that should appear between consecutive values in the resulting string 126 * (but not at the start or end) 127 * @param array an array of unsigned {@code long} values, possibly empty 128 */ 129 public static String join(String separator, long... array) { 130 checkNotNull(separator); 131 if (array.length == 0) { 132 return ""; 133 } 134 135 // For pre-sizing a builder, just get the right order of magnitude 136 StringBuilder builder = new StringBuilder(array.length * 5); 137 builder.append(toString(array[0])); 138 for (int i = 1; i < array.length; i++) { 139 builder.append(separator).append(toString(array[i])); 140 } 141 return builder.toString(); 142 } 143 144 /** 145 * Returns a comparator that compares two arrays of unsigned {@code long} values <a 146 * href="http://en.wikipedia.org/wiki/Lexicographical_order">lexicographically</a>. That is, it 147 * compares, using {@link #compare(long, long)}), the first pair of values that follow any common 148 * prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For 149 * example, {@code [] < [1L] < [1L, 2L] < [2L] < [1L << 63]}. 150 * 151 * <p>The returned comparator is inconsistent with {@link Object#equals(Object)} (since arrays 152 * support only identity equality), but it is consistent with {@link Arrays#equals(long[], 153 * long[])}. 154 */ 155 public static Comparator<long[]> lexicographicalComparator() { 156 return LexicographicalComparator.INSTANCE; 157 } 158 159 enum LexicographicalComparator implements Comparator<long[]> { 160 INSTANCE; 161 162 @Override 163 public int compare(long[] left, long[] right) { 164 int minLength = Math.min(left.length, right.length); 165 for (int i = 0; i < minLength; i++) { 166 if (left[i] != right[i]) { 167 return UnsignedLongs.compare(left[i], right[i]); 168 } 169 } 170 return left.length - right.length; 171 } 172 173 @Override 174 public String toString() { 175 return "UnsignedLongs.lexicographicalComparator()"; 176 } 177 } 178 179 /** 180 * Sorts the array, treating its elements as unsigned 64-bit integers. 181 * 182 * @since 23.1 183 */ 184 public static void sort(long[] array) { 185 checkNotNull(array); 186 sort(array, 0, array.length); 187 } 188 189 /** 190 * Sorts the array between {@code fromIndex} inclusive and {@code toIndex} exclusive, treating its 191 * elements as unsigned 64-bit integers. 192 * 193 * @since 23.1 194 */ 195 public static void sort(long[] array, int fromIndex, int toIndex) { 196 checkNotNull(array); 197 checkPositionIndexes(fromIndex, toIndex, array.length); 198 for (int i = fromIndex; i < toIndex; i++) { 199 array[i] = flip(array[i]); 200 } 201 Arrays.sort(array, fromIndex, toIndex); 202 for (int i = fromIndex; i < toIndex; i++) { 203 array[i] = flip(array[i]); 204 } 205 } 206 207 /** 208 * Sorts the elements of {@code array} in descending order, interpreting them as unsigned 64-bit 209 * integers. 210 * 211 * @since 23.1 212 */ 213 public static void sortDescending(long[] array) { 214 checkNotNull(array); 215 sortDescending(array, 0, array.length); 216 } 217 218 /** 219 * Sorts the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex} 220 * exclusive in descending order, interpreting them as unsigned 64-bit integers. 221 * 222 * @since 23.1 223 */ 224 public static void sortDescending(long[] array, int fromIndex, int toIndex) { 225 checkNotNull(array); 226 checkPositionIndexes(fromIndex, toIndex, array.length); 227 for (int i = fromIndex; i < toIndex; i++) { 228 array[i] ^= Long.MAX_VALUE; 229 } 230 Arrays.sort(array, fromIndex, toIndex); 231 for (int i = fromIndex; i < toIndex; i++) { 232 array[i] ^= Long.MAX_VALUE; 233 } 234 } 235 236 /** 237 * Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit 238 * quantities. 239 * 240 * <p><b>Java 8+ users:</b> use {@link Long#divideUnsigned(long, long)} instead. 241 * 242 * @param dividend the dividend (numerator) 243 * @param divisor the divisor (denominator) 244 * @throws ArithmeticException if divisor is 0 245 */ 246 public static long divide(long dividend, long divisor) { 247 if (divisor < 0) { // i.e., divisor >= 2^63: 248 if (compare(dividend, divisor) < 0) { 249 return 0; // dividend < divisor 250 } else { 251 return 1; // dividend >= divisor 252 } 253 } 254 255 // Optimization - use signed division if dividend < 2^63 256 if (dividend >= 0) { 257 return dividend / divisor; 258 } 259 260 /* 261 * Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is 262 * guaranteed to be either exact or one less than the correct value. This follows from fact that 263 * floor(floor(x)/i) == floor(x/i) for any real x and integer i != 0. The proof is not quite 264 * trivial. 265 */ 266 long quotient = ((dividend >>> 1) / divisor) << 1; 267 long rem = dividend - quotient * divisor; 268 return quotient + (compare(rem, divisor) >= 0 ? 1 : 0); 269 } 270 271 /** 272 * Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64-bit 273 * quantities. 274 * 275 * <p><b>Java 8+ users:</b> use {@link Long#remainderUnsigned(long, long)} instead. 276 * 277 * @param dividend the dividend (numerator) 278 * @param divisor the divisor (denominator) 279 * @throws ArithmeticException if divisor is 0 280 * @since 11.0 281 */ 282 public static long remainder(long dividend, long divisor) { 283 if (divisor < 0) { // i.e., divisor >= 2^63: 284 if (compare(dividend, divisor) < 0) { 285 return dividend; // dividend < divisor 286 } else { 287 return dividend - divisor; // dividend >= divisor 288 } 289 } 290 291 // Optimization - use signed modulus if dividend < 2^63 292 if (dividend >= 0) { 293 return dividend % divisor; 294 } 295 296 /* 297 * Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is 298 * guaranteed to be either exact or one less than the correct value. This follows from the fact 299 * that floor(floor(x)/i) == floor(x/i) for any real x and integer i != 0. The proof is not 300 * quite trivial. 301 */ 302 long quotient = ((dividend >>> 1) / divisor) << 1; 303 long rem = dividend - quotient * divisor; 304 return rem - (compare(rem, divisor) >= 0 ? divisor : 0); 305 } 306 307 /** 308 * Returns the unsigned {@code long} value represented by the given decimal string. 309 * 310 * <p><b>Java 8+ users:</b> use {@link Long#parseUnsignedLong(String)} instead. 311 * 312 * @throws NumberFormatException if the string does not contain a valid unsigned {@code long} 313 * value 314 * @throws NullPointerException if {@code string} is null (in contrast to {@link 315 * Long#parseLong(String)}) 316 */ 317 @CanIgnoreReturnValue 318 public static long parseUnsignedLong(String string) { 319 return parseUnsignedLong(string, 10); 320 } 321 322 /** 323 * Returns the unsigned {@code long} value represented by a string with the given radix. 324 * 325 * <p><b>Java 8+ users:</b> use {@link Long#parseUnsignedLong(String, int)} instead. 326 * 327 * @param string the string containing the unsigned {@code long} representation to be parsed. 328 * @param radix the radix to use while parsing {@code string} 329 * @throws NumberFormatException if the string does not contain a valid unsigned {@code long} with 330 * the given radix, or if {@code radix} is not between {@link Character#MIN_RADIX} and {@link 331 * Character#MAX_RADIX}. 332 * @throws NullPointerException if {@code string} is null (in contrast to {@link 333 * Long#parseLong(String)}) 334 */ 335 @CanIgnoreReturnValue 336 public static long parseUnsignedLong(String string, int radix) { 337 checkNotNull(string); 338 if (string.length() == 0) { 339 throw new NumberFormatException("empty string"); 340 } 341 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { 342 throw new NumberFormatException("illegal radix: " + radix); 343 } 344 345 int maxSafePos = ParseOverflowDetection.maxSafeDigits[radix] - 1; 346 long value = 0; 347 for (int pos = 0; pos < string.length(); pos++) { 348 int digit = Character.digit(string.charAt(pos), radix); 349 if (digit == -1) { 350 throw new NumberFormatException(string); 351 } 352 if (pos > maxSafePos && ParseOverflowDetection.overflowInParse(value, digit, radix)) { 353 throw new NumberFormatException("Too large for unsigned long: " + string); 354 } 355 value = (value * radix) + digit; 356 } 357 358 return value; 359 } 360 361 /** 362 * Returns the unsigned {@code long} value represented by the given string. 363 * 364 * <p>Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix: 365 * 366 * <ul> 367 * <li>{@code 0x}<i>HexDigits</i> 368 * <li>{@code 0X}<i>HexDigits</i> 369 * <li>{@code #}<i>HexDigits</i> 370 * <li>{@code 0}<i>OctalDigits</i> 371 * </ul> 372 * 373 * @throws NumberFormatException if the string does not contain a valid unsigned {@code long} 374 * value 375 * @since 13.0 376 */ 377 @CanIgnoreReturnValue 378 public static long decode(String stringValue) { 379 ParseRequest request = ParseRequest.fromString(stringValue); 380 381 try { 382 return parseUnsignedLong(request.rawValue, request.radix); 383 } catch (NumberFormatException e) { 384 NumberFormatException decodeException = 385 new NumberFormatException("Error parsing value: " + stringValue); 386 decodeException.initCause(e); 387 throw decodeException; 388 } 389 } 390 391 /* 392 * We move the static constants into this class so ProGuard can inline UnsignedLongs entirely 393 * unless the user is actually calling a parse method. 394 */ 395 private static final class ParseOverflowDetection { 396 private ParseOverflowDetection() {} 397 398 // calculated as 0xffffffffffffffff / radix 399 static final long[] maxValueDivs = new long[Character.MAX_RADIX + 1]; 400 static final int[] maxValueMods = new int[Character.MAX_RADIX + 1]; 401 static final int[] maxSafeDigits = new int[Character.MAX_RADIX + 1]; 402 403 static { 404 BigInteger overflow = new BigInteger("10000000000000000", 16); 405 for (int i = Character.MIN_RADIX; i <= Character.MAX_RADIX; i++) { 406 maxValueDivs[i] = divide(MAX_VALUE, i); 407 maxValueMods[i] = (int) remainder(MAX_VALUE, i); 408 maxSafeDigits[i] = overflow.toString(i).length() - 1; 409 } 410 } 411 412 /** 413 * Returns true if (current * radix) + digit is a number too large to be represented by an 414 * unsigned long. This is useful for detecting overflow while parsing a string representation of 415 * a number. Does not verify whether supplied radix is valid, passing an invalid radix will give 416 * undefined results or an ArrayIndexOutOfBoundsException. 417 */ 418 static boolean overflowInParse(long current, int digit, int radix) { 419 if (current >= 0) { 420 if (current < maxValueDivs[radix]) { 421 return false; 422 } 423 if (current > maxValueDivs[radix]) { 424 return true; 425 } 426 // current == maxValueDivs[radix] 427 return (digit > maxValueMods[radix]); 428 } 429 430 // current < 0: high bit is set 431 return true; 432 } 433 } 434 435 /** 436 * Returns a string representation of x, where x is treated as unsigned. 437 * 438 * <p><b>Java 8+ users:</b> use {@link Long#toUnsignedString(long)} instead. 439 */ 440 public static String toString(long x) { 441 return toString(x, 10); 442 } 443 444 /** 445 * Returns a string representation of {@code x} for the given radix, where {@code x} is treated as 446 * unsigned. 447 * 448 * <p><b>Java 8+ users:</b> use {@link Long#toUnsignedString(long, int)} instead. 449 * 450 * @param x the value to convert to a string. 451 * @param radix the radix to use while working with {@code x} 452 * @throws IllegalArgumentException if {@code radix} is not between {@link Character#MIN_RADIX} 453 * and {@link Character#MAX_RADIX}. 454 */ 455 public static String toString(long x, int radix) { 456 checkArgument( 457 radix >= Character.MIN_RADIX && radix <= Character.MAX_RADIX, 458 "radix (%s) must be between Character.MIN_RADIX and Character.MAX_RADIX", 459 radix); 460 if (x == 0) { 461 // Simply return "0" 462 return "0"; 463 } else if (x > 0) { 464 return Long.toString(x, radix); 465 } else { 466 char[] buf = new char[64]; 467 int i = buf.length; 468 if ((radix & (radix - 1)) == 0) { 469 // Radix is a power of two so we can avoid division. 470 int shift = Integer.numberOfTrailingZeros(radix); 471 int mask = radix - 1; 472 do { 473 buf[--i] = Character.forDigit(((int) x) & mask, radix); 474 x >>>= shift; 475 } while (x != 0); 476 } else { 477 // Separate off the last digit using unsigned division. That will leave 478 // a number that is nonnegative as a signed integer. 479 long quotient; 480 if ((radix & 1) == 0) { 481 // Fast path for the usual case where the radix is even. 482 quotient = (x >>> 1) / (radix >>> 1); 483 } else { 484 quotient = divide(x, radix); 485 } 486 long rem = x - quotient * radix; 487 buf[--i] = Character.forDigit((int) rem, radix); 488 x = quotient; 489 // Simple modulo/division approach 490 while (x > 0) { 491 buf[--i] = Character.forDigit((int) (x % radix), radix); 492 x /= radix; 493 } 494 } 495 // Generate string 496 return new String(buf, i, buf.length - i); 497 } 498 } 499}