Archive for February, 2014

Feb
21

Binomial coefficient using generic Typ

With generic typ List

/**
	 * random Sampling without repeat (Binomail coefficient: n choose m)
	 * 
	 * @param items
	 *            A list with basic Data
	 * @param m
	 *            the number of desired Samples
	 * @param seed
	 *            the initial seed
	 * @return A list with obtained Samples
	 */
	public static <E> List<E> randomSample(List<E> items, int m, long seed) {

		if (m > items.size()) {
			System.err.println("m should smaller than the items.size():"
					+ items.size());
			System.exit(-1);
		}

		Random rnd = new Random(seed);
		for (int i = 0; i < m; i++) {
			int pos = i + rnd.nextInt(items.size() - i);
			E tmp = items.get(pos);
			items.set(pos, items.get(i));
			items.set(i, tmp);
		}

		return items.subList(0, m);
	}

With generic typ Array

	/**
	 * random Sampling without repeat
	 * 
	 * @param items
	 *            An array with basic Data
	 * @param m
	 *            the number of desired Samples
	 * @param seed
	 *            the initial seed
	 * @return A array with obtained Samples
	 */
	public static <E> E[] randomSample2(E[] items, int m, long seed){
		
		if (m > items.length) {
			System.err.println("m should smaller than the items.size():"
					+ items.length);
			System.exit(-1);
		}else if (items.length <=1) {
			System.err.println("items can't be empty"
					+ items.length);
			System.exit(-1);
		}
		
		Random rnd = new Random(seed);
		for (int i = 0; i < m; i++) {
			int pos = i + rnd.nextInt(items.length - i);
			E tmp = items[pos];
			items[pos] = items[i];
			items[i] = tmp;
		}
		
		E[] result = Arrays.copyOf(items, m);
		
		return result;
		
	}

Feb
07

read a file into List

There are many ways to read file into a List in Java.

Scanner s = new Scanner(new File("filepath"));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
    list.add(s.next());
}
s.close();

commons-io

List<String> lines = FileUtils.readLines(new File("input.txt"), "utf-8");

guava

List<String> lines =  Files.readLines(new File("input.txt"), Charset.forName("utf-8"));

Java 7.0

List<String> list = Files.readAllLines(new File("input.txt").toPath(), Charset.defaultCharset() );
Feb
07

platform independent separators in java

line separator (Windows: “\r\n”, UNIX: “\n”)

1. BufferedWriter newline()
2. System.lineSeparator()
3. System.getProperty(“line.separator”)

file separator file.separator´╝łwindows:”\”; unix:”/”)

1. BufferedWriter newline()
2. System.getProperty(“file.separator”)
3. File.pathSeparator

Annotation: In Windows: “D:\codehamster”, you can change it to “D:/codehamster” or “D:\\codehamster”

Feb
06

the useful subclass for BitSet and FastBitSet

BitSet and FastBit will always change themselves if they do some set operations, just like and, or, andNot. To avoid such cases, you should write a extended classes:

For BitSet: MyBitSet

import java.util.BitSet;

/**
 * @author Conny Gu This Class extends from Class BitSet, it won't change
 *         itself while it does some operations.
 * 
 */

public class MyBitSet extends BitSet {

	public MyBitSet() {
		super(0);
	}

	public MyBitSet(int nbits) {
		super(nbits);
	}

	/**
	 * This myand() funktion will return a BiSet and itself won't be changed.
	 * 
	 * @param b
	 * @return MyBitSet
	 */
	public MyBitSet myand(MyBitSet b) {
		MyBitSet a = (MyBitSet) this.clone();
		a.and(b);
		return a;
	}

	public MyBitSet myor(MyBitSet b) {
		MyBitSet a = (MyBitSet) this.clone();
		a.or(b);
		return a;
	}

	/**
	 * This myandNot() funktion will return a BiSet and itself won't be changed.
	 * 
	 * @param b
	 * @return MyBitSet
	 */

	public MyBitSet myandNot(MyBitSet b) {
		MyBitSet a = (MyBitSet) this.clone();
		a.andNot(b);
		return a;
	}

}

For FastBitSet: MyFastBitSet

import javolution.util.FastBitSet;

/**
 * @author Conny Gu This Class extends from Class FastBitSet, it won't change
 *         itself while it does some operations.
 * 
 */

public class MyFastBitSet extends FastBitSet {

	public MyFastBitSet() {
		super();
	}

	public MyFastBitSet(FastBitSet b) {
		super();
		this.addAll(b);

	}

	
	public MyFastBitSet myand(MyFastBitSet b) {

		MyFastBitSet a = new MyFastBitSet();

		a.addAll(this);

		a.and(b);

		return a;

	}

	public MyFastBitSet myor(MyFastBitSet b) {

		MyFastBitSet a = new MyFastBitSet();

		a.addAll(this);

		a.or(b);

		return a;

	}

	public MyFastBitSet myandNot(MyFastBitSet b) {

		MyFastBitSet a = new MyFastBitSet();

		a.addAll(this);

		a.andNot(b);

		return a;

	}

}
Feb
06

Example for Javolution FastBitSet

Javolution is a High-performence java library, it is not only fast but also very comfortable to use.

FastBitSet of Javolution is similar to BitSet of Java native Bib. But there are still some differences in usage:

1. BitSet is cloneable, but FastBitSet not.

2. FastBitSet has an own Number Class: Index

public static void main(String[] args) {

		FastBitSet fbs = new FastBitSet();
		
		fbs.set(300);
		fbs.set(200);
		fbs.set(450);

//              copy a FastBitSet
		FastBitSet fbs2 = new FastBitSet();
                fbs2.addAll(fbs);
		
//		print all the data in FastBitSet
		for(int i=fbs.nextSetBit(0); i>=0; i=fbs.nextSetBit(i+1)){
			System.out.println(i);
		}
		
//		Example for how to use "toArray"
		Index[] arr = fbs.toArray(new Index[0]);
		
		for(Index i : arr){
			System.out.println(i);
		}

}