package coll;
/* Immutable Collection
 * by Martin Robinson
 * \__________________/
 *       /
 * \ ('<
 * (<=)
 * */

/*
 * bit list, array of
 * boolean optimised for
 * size, while it is not
 * possible to store a
 * single bit in memory
 * without wasting an
 * entire bytes, an array
 * of bit can be mapped in
 * bytes, shorts, integers
 * or long. this one map in
 * long.
 * \_______________
 *      /
 * \ ('<
 * (<=)
 * */
/**
 * @author martin
 *list of boolean
 */
public final class BitLst extends Coll<Boolean>{
	private final long [] _lst;
	private int _size;
	/**
	 * private constructor
	 * @param lst as array of long
	 * @param size as int
	 */
	private BitLst(final long [] lst, final int size){
		_lst = lst;
		_size = size;
	}
	/**
	 * static factory from given size
	 * @param size as int
	 * @return new list
	 */
	public static Lst<Boolean> fromSize(int size){
		if (size < 0)
			throw new IllegalArgumentException();
		return new BitLst(null, size);
	}
	/**
	 * static factory from another list
	 * @param lst as list of bool
	 * @return new list
	 */
	public static Lst<Boolean> fromBoolLst(final Lst<Boolean> lst){
		if (lst == null)
			throw new NullPointerException();
		long [] tmp = new long[lst.size()/64 + (lst.size()%64!=0?1:0)];
		for (int i=0;i<lst.size();i++)
			if (lst.get(i))
				tmp[i/64] |= (1L<<(i%64));
		return new BitLst(tmp, lst.size());
	}
	/* (non-Javadoc)
	 * @see coll.Lst#get(int)
	 */
	public Boolean get(int i) {
		if (i<_size){
			if (_lst != null)
				return (_lst[i/64]&(1L<<(i%64)))!=0;
			else
				return false;
		}
		throw new ArrayIndexOutOfBoundsException();
	}
	/* (non-Javadoc)
	 * @see coll.Lst#size()
	 */
	public int size() {
		return _size;
	}
	/* (non-Javadoc)
	 * @see coll.Lst#undo()
	 */
	public Lst<Boolean> undo() {
		return null;
	}
}
