harpoon.Util
Class Util

java.lang.Object
  |
  +--harpoon.Util.Util

public abstract class Util
extends Object

Miscellaneous static utility functions.

Version:
$Id: Util.java,v 1.15 2002/04/02 23:59:17 salcianu Exp $
Author:
C. Scott Ananian <cananian@alumni.princeton.edu>

Constructor Summary
Util()
           
 
Method Summary
static String adjust_quotes(String str)
          Returns a string that is identical to str, except that every " character has been replaced with the sequence \".
static void ASSERT(boolean val)
          Assertion facility.
static void ASSERT(boolean val, Object msg)
          Assertion facility, with a Object as an explanation string.
static Object[] copy(ArrayFactory factory, Object[] src)
          Deprecated. The clone() method on arrays works much better, and should be used instead of this method.
static String escape(String str)
          Escape the contents of a String so they are safe to print.
static int ffs(int v)
          Find first set (least significant bit).
static int ffs(long v)
          Find first set (least significant bit).
static int fls(int v)
          Find last set (most significant bit).
static int fls(long v)
          Find last set (most significant bit).
static int gcd(int u, int v)
          Returns the greatest common divisor of a pair of numbers.
static long gcd(long u, long v)
          Returns the greatest common divisor of a pair of numbers.
static ArrayFactory genericFactory(Object[] src)
          Deprecated. requires java language reflection.
static FOOTER getFOOTER(HCode hcode)
          Returns the unique FOOTER quad from hcode.
static METHOD getMETHOD(HCode hcode)
          Returns the unique METHOD quad from hcode.
static Object[] grow(ArrayFactory factory, Object[] src, Object o, int n)
          Insert element o before src[n].
static String jasminEscape(String str)
          Escape the contents of a String so they are safe to print.
static int log2c(int v)
          Returns ceil(log2(n))
static int popcount(int v)
          Return the number of ones in the binary representation of the value of the argument.
static int popcount(long v)
          Return the number of ones in the binary representation of the value of the argument.
static void print_collection(Collection c, String c_name)
          Even more simplified version of print_collection.
static void print_collection(Collection c, String c_name, String indent)
          Simplified version of print_collection.
static void print_collection(Collection c, String c_name, String indent, PrintWriter pw)
          Pretty printer for collections.
static String print(Collection collection)
          Return a String representing the elements of collection in a human readable format.
static String repeatString(String s, int n)
          Repeat a given string a certain number of times.
static Object[] safeCopy(ArrayFactory factory, Object[] src)
          Copy an array type to prevent modification.
static Set set_diff(Set a, Set b)
          Computes the difference of two sets: a-b.
static Object[] shrink(ArrayFactory factory, Object[] src, int n)
          Remove element 'n' from array 'src'.
static int zerocount(int v)
          Return the number of zeros in the binary representation of the value of the argument.
static int zerocount(long v)
          Return the number of zeros in the binary representation of the value of the argument.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Util

public Util()
Method Detail

genericFactory

public static final ArrayFactory genericFactory(Object[] src)
Deprecated. requires java language reflection.

Returns an ArrayFactory which makes an array of the same type as the src parameter (which should be an array of some object type). This method uses run-time type information and thus should not be used.


safeCopy

public static final Object[] safeCopy(ArrayFactory factory,
                                      Object[] src)
Copy an array type to prevent modification. Does not bother to copy array types of length 0, because they're already immutable. After code has been debugged, this method can skip the copy for efficiency.


copy

public static final Object[] copy(ArrayFactory factory,
                                  Object[] src)
Deprecated. The clone() method on arrays works much better, and should be used instead of this method.

Copy an array type to prevent modification.


shrink

public static final Object[] shrink(ArrayFactory factory,
                                    Object[] src,
                                    int n)
Remove element 'n' from array 'src'.


grow

public static final Object[] grow(ArrayFactory factory,
                                  Object[] src,
                                  Object o,
                                  int n)
Insert element o before src[n].

After return, src[n]==o.


escape

public static final String escape(String str)
Escape the contents of a String so they are safe to print.


jasminEscape

public static final String jasminEscape(String str)
Escape the contents of a String so they are safe to print.


ASSERT

public static final void ASSERT(boolean val)
Assertion facility. Throws a RuntimeException if the boolean parameter is false.


ASSERT

public static final void ASSERT(boolean val,
                                Object msg)
Assertion facility, with a Object as an explanation string. Throws a RuntimeException including the output of msg.toString() if the boolean parameter is false.

Note that this can be used as a Lazy Evaluation hack; since it only evaluates msg.toString() if val is false), it can be used for code where evaluating the msg string takes too long to do everytime the assertion is checked or the explanation string can't be generated if val is true.


repeatString

public static final String repeatString(String s,
                                        int n)
Repeat a given string a certain number of times.

Returns:
a string consisting of s repeated n times.

ffs

public static final int ffs(int v)
Find first set (least significant bit).

Returns:
the first bit set in the argument. ffs(0)==0 and ffs(1)==1.

ffs

public static final int ffs(long v)
Find first set (least significant bit).

Returns:
the first bit set in the argument. ffs(0)==0 and ffs(1)==1.

fls

public static final int fls(int v)
Find last set (most significant bit).

Returns:
the last bit set in the argument. fls(0)==0 and fls(1)==1.

fls

public static final int fls(long v)
Find last set (most significant bit).

Returns:
the last bit set in the argument. fls(0)==0 and fls(1)==1.

log2c

public static final int log2c(int v)
Returns ceil(log2(n))


zerocount

public static final int zerocount(int v)
Return the number of zeros in the binary representation of the value of the argument.


zerocount

public static final int zerocount(long v)
Return the number of zeros in the binary representation of the value of the argument.


popcount

public static final int popcount(int v)
Return the number of ones in the binary representation of the value of the argument.


popcount

public static final int popcount(long v)
Return the number of ones in the binary representation of the value of the argument.


gcd

public static final long gcd(long u,
                             long v)
Returns the greatest common divisor of a pair of numbers.


gcd

public static final int gcd(int u,
                            int v)
Returns the greatest common divisor of a pair of numbers.


print

public static final String print(Collection collection)
Return a String representing the elements of collection in a human readable format.
effects: Iterates over collection, calling toString() on each element and appending the result to a String. The format of the returned String is { elem1, elem2, ... , elemN }

Returns:
A String representing collection.

print_collection

public static final void print_collection(Collection c,
                                          String c_name,
                                          String indent,
                                          PrintWriter pw)
Pretty printer for collections.

Parameters:
c - The Collection to be printed.
c_name - The name of the collection.
indent - String used for indentation (eg "\t".
pw - The PrintWriter used to do the printing itself. The collection is printed in the following format: the name of the collection followed by the collection size (in parans) and one opening curly bracet on the first line, the elements of the collection, one by one, each on a separate line and indented with a tab; on the last line, one closing curly bracet. In addition, each line is prefixed with the string indent.

print_collection

public static final void print_collection(Collection c,
                                          String c_name,
                                          String indent)
Simplified version of print_collection. Uses the default value new java.io.PrintWriter(System.out, true) for the pw parameter.


print_collection

public static final void print_collection(Collection c,
                                          String c_name)
Even more simplified version of print_collection. Uses the default value "" for indent and new java.io.PrintWriter(System.out, true) for the pw parameter.


set_diff

public static final Set set_diff(Set a,
                                 Set b)
Computes the difference of two sets: a-b.


getMETHOD

public static final METHOD getMETHOD(HCode hcode)
Returns the unique METHOD quad from hcode.


getFOOTER

public static final FOOTER getFOOTER(HCode hcode)
Returns the unique FOOTER quad from hcode.


adjust_quotes

public static final String adjust_quotes(String str)
Returns a string that is identical to str, except that every " character has been replaced with the sequence \". This is useful when we generate output for tools like VCG which expects the value of some attributes to be put between quotes (and of course any quote inside has to be given using an escape sequence).