/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2017 Barry DeZonia
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package zorbage.type.storage.linear.array;

import zorbage.type.ctor.Allocatable;
import zorbage.type.storage.coder.BitCoder;
import zorbage.type.storage.linear.LinearStorage;

/**
 * 
 * @author Barry DeZonia
 *
 * @param <U>
 */
public class ArrayStorageBit<U extends BitCoder<U> & Allocatable<U>>
	implements LinearStorage<ArrayStorageBit<U>,U>
{
	private final U type;
	private final long[] data;
	private final long size;
	
	public ArrayStorageBit(long size, U type) {
	
		final long totalBits = 64l * Integer.MAX_VALUE / type.bitCount();
		if (size < 0)
			throw new IllegalArgumentException("ArrayStorageBit cannot handle a negative request");
		if (size > totalBits)
			throw new IllegalArgumentException("ArrayStorageBit can handle at most " + (64l * Integer.MAX_VALUE) + " bits");
		int count = (int)(totalBits / 64);
		if (totalBits % 64 > 0) count += 1;
		this.type = type.allocate();
		this.data = new long[count];
		this.size = size;
	}

	@Override
	public void set(long index, U value) {
		final int bucketStart = (int)(index * type.bitCount() / 64l);
		final int bucketOffset = (int)((index * type.bitCount()) % 64l);
		synchronized (data) {
			value.toArray(data, bucketStart, bucketOffset);
		}
	}

	@Override
	public void get(long index, U value) {
		final int bucketStart = (int)(index * type.bitCount() / 64l);
		final int bucketOffset = (int)((index * type.bitCount()) % 64l);
		synchronized (data) {
			value.toValue(data, bucketStart, bucketOffset);
		}
	}
	
	@Override
	public long size() {
		return size;
	}

	@Override
	public ArrayStorageBit<U> duplicate() {
		ArrayStorageBit<U> s = new ArrayStorageBit<U>(size(), type);
		synchronized (data) {
			for (int i = 0; i < data.length; i++)
				s.data[i] = data[i];
		}
		return s;
	}
	
}

@@ -2439,61 +2439,94 @@
 nal 
-int bucketStart = (int)(index * type.bitCount()
+long bitIndex = index * type.bitCount();%0A%09%09final int bucketStart = (int)(bitIndex
  / 64
-l
 );%0A%09
@@ -2561,39 +2561,21 @@
 nt)(
-(i
+bitI
 ndex
- * type.bitCount())
  %25 64
-l
 );%0A%09
@@ -2719,61 +2719,94 @@
 nal 
-int bucketStart = (int)(index * type.bitCount()
+long bitIndex = index * type.bitCount();%0A%09%09final int bucketStart = (int)(bitIndex
  / 64
-l
 );%0A%09
@@ -2841,39 +2841,21 @@
 nt)(
-(i
+bitI
 ndex
- * type.bitCount())
  %25 64
-l
 );%0A%09

Optimize ArrayStorageBit