/*
 * Copyright 2012 Takao Nakaguchi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.trie4j.louds.bvtree;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import org.trie4j.bv.BytesSuccinctBitVector;
import org.trie4j.bv.SuccinctBitVector;
import org.trie4j.util.Range;

public class LOUDSBvTree implements Externalizable, BvTree{
	public LOUDSBvTree() {
		this(0);
	}

	public LOUDSBvTree(int initialCapacity) {
		sbv = new BytesSuccinctBitVector(initialCapacity);
	}

	public LOUDSBvTree(SuccinctBitVector sbv) {
		this.sbv = sbv;
	}

	public SuccinctBitVector getSbv() {
		return sbv;
	}

	@Override
	public String toString() {
		String bvs = sbv.toString();
		return "bitvec: " + ((bvs.length() > 100) ? bvs.substring(0, 100) : bvs);
	}

	@Override
	public void appendChild() {
		sbv.append1();
	}
	
	@Override
	public void appendSelf() {
		sbv.append0();
	}

	@Override
	public void getChildNodeIds(int selfNodeId, Range range) {
		int s = sbv.select0(selfNodeId) + 1;
		int e = sbv.next0(s);
		int startNodeId = sbv.rank1(s);
		range.set(startNodeId, startNodeId + e - s);
	}

	@Override
	public void trimToSize() {
		sbv.trimToSize();
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		sbv = (SuccinctBitVector)in.readObject();
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeObject(sbv);
	}

	private SuccinctBitVector sbv;
}

@@ -981,24 +981,28 @@
 (int initial
+Node
 Capacity) %7B%0A
@@ -1043,24 +1043,28 @@
 (initial
+Node
 Capacity
 );%0A%09%7D%0A%0A%09
@@ -1055,16 +1055,20 @@
 Capacity
+ * 2
 );%0A%09%7D%0A%0A%09

clarify the meaning of argument.