package it.unimi.dsi.mg4j.util;
/*
* MG4J: Managing Gigabytes for Java
*
* Copyright (C) 2002-2007 Sebastiano Vigna
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
import java.util.Enumeration;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/** All-purpose optimised static-method container class.
*
* <P>This class contains static optimised utility methods that are used by all
* MG4J classes.
*
* @author Sebastiano Vigna
* @since 0.1
*/
public final class Fast {
private Fast() {}
/** Computes the most significant bit of an integer.
*
* @param x an integer.
* @return the most significant bit of the argument (-1 for 0).
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static int mostSignificantBit( final int x ) {
return
( x < 1<<15 ?
( x < 1<<7 ?
( x < 1<<3 ?
( x < 1<<1 ?
( x < 1<<0 ?
x < 0 ? 31 : -1 /* 6 */
:
0 /* 5 */
)
:
( x < 1<<2 ?
1 /* 5 */
:
2 /* 5 */
)
)
:
( x < 1<<5 ?
( x < 1<<4 ?
3 /* 5 */
:
4 /* 5 */
)
:
( x < 1<<6 ?
5 /* 5 */
:
6 /* 5 */
)
)
)
:
( x < 1<<11 ?
( x < 1<<9 ?
( x < 1<<8 ?
7 /* 5 */
:
8 /* 5 */
)
:
( x < 1<<10 ?
9 /* 5 */
:
10 /* 5 */
)
)
:
( x < 1<<13 ?
( x < 1<<12 ?
11 /* 5 */
:
12 /* 5 */
)
:
( x < 1<<14 ?
13 /* 5 */
:
14 /* 5 */
)
)
)
)
:
( x < 1<<23 ?
( x < 1<<19 ?
( x < 1<<17 ?
( x < 1<<16 ?
15 /* 5 */
:
16 /* 5 */
)
:
( x < 1<<18 ?
17 /* 5 */
:
18 /* 5 */
)
)
:
( x < 1<<21 ?
( x < 1<<20 ?
19 /* 5 */
:
20 /* 5 */
)
:
( x < 1<<22 ?
21 /* 5 */
:
22 /* 5 */
)
)
)
:
( x < 1<<27 ?
( x < 1<<25 ?
( x < 1<<24 ?
23 /* 5 */
:
24 /* 5 */
)
:
( x < 1<<26 ?
25 /* 5 */
:
26 /* 5 */
)
)
:
( x < 1<<29 ?
( x < 1<<28 ?
27 /* 5 */
:
28 /* 5 */
)
:
( x < 1<<30 ?
29 /* 5 */
:
30 /* 5 */
)
)
)
)
);
}
/** Computes the most significant bit of a long integer.
*
* @param x a long integer.
* @return the most significant bit of the argument (-1 for 0).
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static int mostSignificantBit( final long x ) {
return
( x < 1L<<31 ?
( x < 1L<<15 ?
( x < 1L<<7 ?
( x < 1L<<3 ?
( x < 1L<<1 ?
( x < 1L<<0 ?
x < 0 ? 63 : -1 /* 7 */
:
0 /* 6 */
)
:
( x < 1L<<2 ?
1 /* 6 */
:
2 /* 6 */
)
)
:
( x < 1L<<5 ?
( x < 1L<<4 ?
3 /* 6 */
:
4 /* 6 */
)
:
( x < 1L<<6 ?
5 /* 6 */
:
6 /* 6 */
)
)
)
:
( x < 1L<<11 ?
( x < 1L<<9 ?
( x < 1L<<8 ?
7 /* 6 */
:
8 /* 6 */
)
:
( x < 1L<<10 ?
9 /* 6 */
:
10 /* 6 */
)
)
:
( x < 1L<<13 ?
( x < 1L<<12 ?
11 /* 6 */
:
12 /* 6 */
)
:
( x < 1L<<14 ?
13 /* 6 */
:
14 /* 6 */
)
)
)
)
:
( x < 1L<<23 ?
( x < 1L<<19 ?
( x < 1L<<17 ?
( x < 1L<<16 ?
15 /* 6 */
:
16 /* 6 */
)
:
( x < 1L<<18 ?
17 /* 6 */
:
18 /* 6 */
)
)
:
( x < 1L<<21 ?
( x < 1L<<20 ?
19 /* 6 */
:
20 /* 6 */
)
:
( x < 1L<<22 ?
21 /* 6 */
:
22 /* 6 */
)
)
)
:
( x < 1L<<27 ?
( x < 1L<<25 ?
( x < 1L<<24 ?
23 /* 6 */
:
24 /* 6 */
)
:
( x < 1L<<26 ?
25 /* 6 */
:
26 /* 6 */
)
)
:
( x < 1L<<29 ?
( x < 1L<<28 ?
27 /* 6 */
:
28 /* 6 */
)
:
( x < 1L<<30 ?
29 /* 6 */
:
30 /* 6 */
)
)
)
)
)
:
( x < 1L<<47 ?
( x < 1L<<39 ?
( x < 1L<<35 ?
( x < 1L<<33 ?
( x < 1L<<32 ?
31 /* 6 */
:
32 /* 6 */
)
:
( x < 1L<<34 ?
33 /* 6 */
:
34 /* 6 */
)
)
:
( x < 1L<<37 ?
( x < 1L<<36 ?
35 /* 6 */
:
36 /* 6 */
)
:
( x < 1L<<38 ?
37 /* 6 */
:
38 /* 6 */
)
)
)
:
( x < 1L<<43 ?
( x < 1L<<41 ?
( x < 1L<<40 ?
39 /* 6 */
:
40 /* 6 */
)
:
( x < 1L<<42 ?
41 /* 6 */
:
42 /* 6 */
)
)
:
( x < 1L<<45 ?
( x < 1L<<44 ?
43 /* 6 */
:
44 /* 6 */
)
:
( x < 1L<<46 ?
45 /* 6 */
:
46 /* 6 */
)
)
)
)
:
( x < 1L<<55 ?
( x < 1L<<51 ?
( x < 1L<<49 ?
( x < 1L<<48 ?
47 /* 6 */
:
48 /* 6 */
)
:
( x < 1L<<50 ?
49 /* 6 */
:
50 /* 6 */
)
)
:
( x < 1L<<53 ?
( x < 1L<<52 ?
51 /* 6 */
:
52 /* 6 */
)
:
( x < 1L<<54 ?
53 /* 6 */
:
54 /* 6 */
)
)
)
:
( x < 1L<<59 ?
( x < 1L<<57 ?
( x < 1L<<56 ?
55 /* 6 */
:
56 /* 6 */
)
:
( x < 1L<<58 ?
57 /* 6 */
:
58 /* 6 */
)
)
:
( x < 1L<<61 ?
( x < 1L<<60 ?
59 /* 6 */
:
60 /* 6 */
)
:
( x < 1L<<62 ?
61 /* 6 */
:
62 /* 6 */
)
)
)
)
)
);
}
/** Computes the least significant bit of an integer.
*
* @param x an integer.
* @return the least significant bit of the argument (-1 for 0).
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static int leastSignificantBit( final int x ) {
if ( x == 0 ) return -1;
if ( ( x & 0xFF ) != 0 ) return BYTELSB[ x & 0xFF ];
if ( ( x & 0xFFFF ) != 0 ) return BYTELSB[ x >>> 8 & 0xFF ] + 8;
if ( ( x & 0xFFFFFF ) != 0 ) return BYTELSB[ x >>> 16 & 0xFF ]+ 16;
return BYTELSB[ x >>> 24 & 0xFF ] + 24;
}
/** Computes the least significant bit of a long integer.
*
* @param x a long integer.
* @return the least significant bit of the argument (-1 for 0).
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static int leastSignificantBit( final long x ) {
if ( x == 0 ) return -1;
if ( ( x & 0xFF ) != 0 ) return BYTELSB[ (int)( x & 0xFF ) ];
if ( ( x & 0xFFFF ) != 0 ) return BYTELSB[ (int)( x >>> 8 & 0xFF ) ] + 8;
if ( ( x & 0xFFFFFF ) != 0 ) return BYTELSB[ (int)( x >>> 16 & 0xFF ) ] + 16;
if ( ( x & 0xFFFFFFFFL ) != 0 ) return BYTELSB[ (int)( x >>> 24 & 0xFF ) ] + 24;
if ( ( x & 0xFFFFFFFFFFL ) != 0 ) return BYTELSB[ (int)( x >>> 32 & 0xFF ) ] + 32;
if ( ( x & 0xFFFFFFFFFFFFL ) != 0 ) return BYTELSB[ (int)( x >>> 40 & 0xFF ) ] + 40;
if ( ( x & 0xFFFFFFFFFFFFFFL ) != 0 ) return BYTELSB[ (int)( x >>> 48 & 0xFF ) ] + 48;
return BYTELSB[ (int)( x >>> 56 & 0xFF ) ] + 56;
}
/** Maps integers bijectively into natural numbers.
*
* <P>This method will map a negative integer <var>x</var> to -2<var>x</var>-1 and
* a nonnegative integer <var>x</var> to 2<var>x</var>. It can be used to save
* integers in the range [{@link Integer#MIN_VALUE}/2..{@link Integer#MAX_VALUE}/2]
* (i.e., [-2<sup>30</sup>..2<sup>30</sup>-1])
* using the standard coding methods (which all work on natural numbers). Note
* that no range checks are performed.
*
* <P>The inverse of the above map is computed by {@link #nat2int(int)}.
*
* @param x an integer.
* @return the argument mapped into a natural number.
* @see #nat2int(int)
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static int int2nat( final int x ) {
return x >= 0 ? x << 1 : -( ( x << 1 ) + 1 );
}
/** Maps natural numbers bijectively into integers.
*
* <P>This method computes the inverse of {@link #int2nat(int)}.
*
* @param x a natural number.
* @return the argument mapped into an integer.
* @see #int2nat(int)
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static int nat2int( final int x ) {
return x % 2 == 0 ? x >> 1 : -( x >> 1 ) - 1;
}
/** Maps longs bijectively into long natural numbers.
*
* <P>This method will map a negative long <var>x</var> to -2<var>x</var>-1 and
* a nonnegative long <var>x</var> to 2<var>x</var>. It can be used to save
* longs in the range [{@link Long#MIN_VALUE}/2..{@link Long#MAX_VALUE}/2]
* (i.e., [-2<sup>62</sup>..2<sup>62</sup>-1])
* using the standard coding methods (which all work on natural numbers). Note
* that no range checks are performed.
*
* <P>The inverse of the above map is computed by {@link #nat2int(long)}.
*
* @param x a long.
* @return the argument mapped into a long natural number.
* @see #int2nat(int)
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static long int2nat( final long x ) {
return x >= 0 ? x << 1 : -( ( x << 1 ) + 1 );
}
/** Maps long natural numbers bijectively into longs.
*
* <P>This method computes the inverse of {@link #int2nat(long)}.
*
* @param x a long natural number.
* @return the argument mapped into a long.
* @see #nat2int(int)
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static long nat2int( final long x ) {
return x % 2 == 0 ? x >> 1 : -( x >> 1 ) - 1;
}
/** A reasonable format for real numbers. */
private static final java.text.NumberFormat FORMAT_DOUBLE = new java.text.DecimalFormat( "#,##0.00" );
/** Formats a number.
*
* <P>This method formats a double separating thousands and printing just two fractional digits.
* @param d a number.
* @return a string containing a pretty print of the number.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static String format( final double d ) {
final StringBuffer s = new StringBuffer();
return FORMAT_DOUBLE.format( d, s, new java.text.FieldPosition( 0 ) ).toString();
}
/** A reasonable format for integers. */
private static final java.text.NumberFormat FORMAT_LONG = new java.text.DecimalFormat( "#,###" );
/** Formats a number.
*
* <P>This method formats a long separating thousands.
* @param l a number.
* @return a string containing a pretty print of the number.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static String format( final long l ) {
final StringBuffer s = new StringBuffer();
return FORMAT_LONG.format( l, s, new java.text.FieldPosition( 0 ) ).toString();
}
/** Formats a size.
*
* <P>This method formats a long using suitable unit multipliers (e.g., <samp>K</samp>, <samp>M</samp>, <samp>G</samp>, and <samp>T</samp>)
* and printing just two fractional digits.
* @param l a number, representing a size (e.g., memory).
* @return a string containing a pretty print of the number using unit multipliers.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static String formatSize( final long l ) {
if ( l >= 1000000000000L ) return format( l / 1000000000000.0 ) + "T";
if ( l >= 1000000000L ) return format( l / 1000000000.0 ) + "G";
if ( l >= 1000000L ) return format( l / 1000000.0 ) + "M";
if ( l >= 1000L ) return format( l / 1000.0 ) + "K";
return Long.toString( l );
}
/** Formats a binary size.
*
* <P>This method formats a long using suitable unit binary multipliers (e.g., <samp>Ki</samp>, <samp>Mi</samp>, <samp>Gi</samp>, and <samp>Ti</samp>)
* and printing <em>no</em> fractional digits. The argument must be a power of 2.
* @param l a number, representing a binary size (e.g., memory); must be a power of 2.
* @return a string containing a pretty print of the number using binary unit multipliers.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static String formatBinarySize( final long l ) {
if ( ( l & -l ) != l ) throw new IllegalArgumentException( "Not a power of 2: " + l );
if ( l >= ( 1L << 40 ) ) return format( l >> 40 ) + "Ti";
if ( l >= ( 1L << 30 ) ) return format( l >> 30 ) + "Gi";
if ( l >= ( 1L << 20 ) ) return format( l >> 20 ) + "Mi";
if ( l >= ( 1L << 10 ) ) return format( l >> 10 ) + "Ki";
return Long.toString( l );
}
/** Precomputed least significant bits for bytes (-1 for 0 ).
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static final int[] BYTELSB = {
-1, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
/** Precomputed most significant bits for bytes (-1 for 0 ).
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static final int[] BYTEMSB = {
-1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
};
/** Checks whether Log4J is properly configuring by searching for appenders in all loggers.
*
* @return whether Log4J is configured (or, at least, an educated guess).
* @deprecated Moved to <code>dsiutils</code>.
*
*/
@Deprecated
public static boolean log4JIsConfigured() {
if ( Logger.getRootLogger().getAllAppenders().hasMoreElements() ) return true;
Enumeration<?> loggers = LogManager.getCurrentLoggers();
while ( loggers.hasMoreElements() ) {
Logger logger = (Logger)loggers.nextElement();
if ( logger.getAllAppenders().hasMoreElements() ) return true;
}
return false;
}
/** Ensures that Log4J is configured, by invoking, if necessary,
* {@link org.apache.log4j.BasicConfigurator#configure()}, and
* setting the root logger level to {@link Level#INFO}.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static void ensureLog4JIsConfigured() {
ensureLog4JIsConfigured( Level.INFO );
}
/** Ensures that Log4J is configured, by invoking, if necessary,
* {@link org.apache.log4j.BasicConfigurator#configure()}, and
* setting the root logger level to <code>level</code>.
*
* @param level the required logging level.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static void ensureLog4JIsConfigured( final Level level ) {
if ( ! log4JIsConfigured() ) {
System.err.println( "WARNING: MG4J is autoconfiguring Log4J. You should configure Log4J properly instead." );
BasicConfigurator.configure();
LogManager.getRootLogger().setLevel( level );
}
}
/** Calls Log4J's {@link Logger#getLogger(java.lang.Class)} method and then {@link #ensureLog4JIsConfigured()}.
*
* @param klass a class that will be passed to {@link Logger#getLogger(java.lang.Class)}.
* @return the logger returned by {@link Logger#getLogger(java.lang.Class)}.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static Logger getLogger( final Class<?> klass ) {
Logger logger = Logger.getLogger( klass );
ensureLog4JIsConfigured();
return logger;
}
private final static Runtime RUNTIME = Runtime.getRuntime();
/** Returns true if less then 5% of the available memory is free.
*
* @return true if less then 5% of the available memory is free.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static boolean memoryIsLow() {
return availableMemory() * 100 < RUNTIME.totalMemory() * 5;
}
/** Returns the amount of available memory (free memory plus never allocated memory).
*
* @return the amount of available memory, in bytes.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static long availableMemory() {
return RUNTIME.freeMemory() + ( RUNTIME.maxMemory() - RUNTIME.totalMemory() );
}
/** Returns the percentage of available memory (free memory plus never allocated memory).
*
* @return the percentage of available memory.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static int percAvailableMemory() {
return (int)( ( Fast.availableMemory() * 100 ) / Runtime.getRuntime().maxMemory() );
}
/** Tries to compact memory as much as possible by forcing garbage collection.
* @deprecated Moved to <code>dsiutils</code>.
*/
@Deprecated
public static void compactMemory() {
try {
@SuppressWarnings("unused")
final byte[][] unused = new byte[ 128 ][];
for( int i = unused.length; i-- != 0; ) unused[ i ] = new byte[ 2000000000 ];
}
catch ( OutOfMemoryError itsWhatWeWanted ) {}
}
}
|