/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2018 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.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Barry DeZonia
 *
 * @param <T>
 */
public class BigList<T> {
	
	private long MAX_ITEMS = 1L * Integer.MAX_VALUE * Integer.MAX_VALUE;

	private List<List<T>> lists;
	
	public BigList() {
		lists = new ArrayList<List<T>>();
	}
	
	public BigList(long size) {
		if (size < 0)
			throw new IllegalArgumentException("negative index error");
		if (size > MAX_ITEMS)
			throw new IllegalArgumentException("BigList can only handle "+MAX_ITEMS+" items");
		lists = new ArrayList<List<T>>();
		while (size > 0) {
			List<T> l;
			if ((size / Integer.MAX_VALUE) > 0) {
				l = new ArrayList<T>(Integer.MAX_VALUE);
			}
			else {
				l = new ArrayList<T>((int)size);
			}
			lists.add(l);
			size -= Integer.MAX_VALUE;
		}
	}
	
	public long size() {
		if (lists.size() == 0)
			return 0;
		long size = 1;
		size *= lists.size()-1;
		size *= Integer.MAX_VALUE;
		size += lists.get(lists.size()-1).size();
		return size;
	}
	
	public T get(long i) {
		if (i < 0)
			throw new IllegalArgumentException("negative index error");
		if (i >= MAX_ITEMS)
			throw new IllegalArgumentException("BigList can only handle "+MAX_ITEMS+" items");
		int list = (int)(i / Integer.MAX_VALUE);
		int offset = (int)(i % Integer.MAX_VALUE);
		List<T> l = lists.get(list);
		return l.get(offset);
	}

	public void set(long i, T v) {
		if (i < 0)
			throw new IllegalArgumentException("negative index error");
		if (i >= MAX_ITEMS)
			throw new IllegalArgumentException("BigList can only handle "+MAX_ITEMS+" items");
		int list = (int)(i / Integer.MAX_VALUE);
		int offset = (int)(i % Integer.MAX_VALUE);
		List<T> l = lists.get(list);
		l.set(offset, v);
	}
	
	public void add(T val) {
		if ((lists.size() == 0) ||
			(lists.get(lists.size()-1).size() == Integer.MAX_VALUE))
		{
			lists.add(new ArrayList<T>());
		}
		lists.get(lists.size()-1).add(val);
	}

}

@@ -2140,56 +2140,207 @@
 %0A%09%09%09
-%7D%0A%09%09%09else %7B%0A%09%09%09%09l = new ArrayList%3CT%3E((int)size);
+%09for (int i = 0; i %3C Integer.MAX_VALUE; i++) %7B%0A%09%09%09%09%09l.add(null);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09int sz = (int) size;%0A%09%09%09%09l = new ArrayList%3CT%3E(sz);%0A%09%09%09%09for (int i = 0; i %3C sz; i++) %7B%0A%09%09%09%09%09l.add(null);%0A%09%09%09%09%7D
 %0A%09%09%09

Improve BigList constructor