/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2019 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 nom.bdezonia.zorbage.type.storage;

import nom.bdezonia.zorbage.algebras.G;
import nom.bdezonia.zorbage.type.data.bool.BooleanMember;

/**
 * 
 * @author Barry DeZonia
 *
 */
public class MaskedDataSource<U>
	implements
		IndexedDataSource<U>
{
	private final IndexedDataSource<U> list;
	private final IndexedDataSource<BooleanMember> mask;
	private final long listSize;
	private final long maskSize;
	private final long trueCount;
	private final ThreadLocal<BooleanMember> bTmp;
	
	/**
	 * 
	 * @param list
	 * @param mask
	 */
	public MaskedDataSource(IndexedDataSource<U> list, IndexedDataSource<BooleanMember> mask) {
		this.list = list;
		this.mask = mask;
		this.listSize = list.size();
		long mSz = mask.size();
		if (mSz > listSize)
			this.maskSize = listSize;
		else
			this.maskSize = mSz;
		if (maskSize == 0)
			throw new IllegalArgumentException("mask must not be of length 0");
		this.bTmp = new ThreadLocal<BooleanMember>() {
			@Override
			protected BooleanMember initialValue() {
				return G.BOOL.construct();
			}
		};
		BooleanMember b = bTmp.get();
		long count = 0;
		for (long i = 0; i < maskSize; i++) {
			mask.get(i, b);
			if (b.v())
				count++;
		}
		this.trueCount = count;
	}
	
	@Override
	public MaskedDataSource<U> duplicate() {
		// shallow copy
		return new MaskedDataSource<>(list, mask);
	}

	@Override
	public void set(long index, U value) {
		if (index < 0)
			throw new IllegalArgumentException("negative index exception");
		long pos = findPosition(index);
		if (pos >= 0)
			list.set(pos, value);
		else
			throw new IllegalArgumentException("accessing masked data source off the end");
	}

	@Override
	public void get(long index, U value) {
		if (index < 0)
			throw new IllegalArgumentException("negative index exception");
		long pos = findPosition(index);
		if (pos >= 0)
			list.get(pos, value);
		else
			throw new IllegalArgumentException("accessing masked data source off the end");
	}

	@Override
	public long size() {
		BooleanMember b = bTmp.get();
		long numFullReps = listSize / maskSize;
		long sz = numFullReps * trueCount;
		long partialMaskSize = listSize % maskSize;
		for (long i = 0; i < partialMaskSize; i++) {
			mask.get(i, b);
			if (b.v())
				sz++;
		}
		return sz;
	}

	private long findPosition(long index) {
		BooleanMember b = bTmp.get();
		long numFullReps = index / trueCount;
		long pos = numFullReps * maskSize;
		long found = 0;
		long mustFind = (index % trueCount) + 1;
		//long mustFind = index % trueCount;
		//if (mustFind == 0) mustFind = 1;
		long i = 0;
		while (found < mustFind) {
			mask.get(i, b);
			if (b.v())
				found++;
			if (found != mustFind)
				i++;
		}
		pos += i;
		//System.out.println("in " + index + "  out " + pos);
		return pos;
	}
}

@@ -1869,24 +1869,31 @@
 t;%0A%09private 
+static 
 final Thread
@@ -1917,16 +1917,158 @@
 er%3E bTmp
+ =%0A%09%09%09new ThreadLocal%3CBooleanMember%3E() %7B%0A%09%09%09%09@Override%0A%09%09%09%09protected BooleanMember initialValue() %7B%0A%09%09%09%09%09return G.BOOL.construct();%0A%09%09%09%09%7D%0A%09%09%09%7D
 ;%0A%09%0A%09/**
@@ -2478,155 +2478,8 @@
 %22);%0A
-%09%09this.bTmp = new ThreadLocal%3CBooleanMember%3E() %7B%0A%09%09%09@Override%0A%09%09%09protected BooleanMember initialValue() %7B%0A%09%09%09%09return G.BOOL.construct();%0A%09%09%09%7D%0A%09%09%7D;%0A
 %09%09Bo

Make one ThreadLocal variable a static member of a class