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

import nom.bdezonia.zorbage.algebra.Allocatable;
import nom.bdezonia.zorbage.algebra.StorageConstruction;
import nom.bdezonia.zorbage.datasource.IndexedDataSource;
import nom.bdezonia.zorbage.storage.array.ArrayStorage;
import nom.bdezonia.zorbage.storage.file.FileStorage;
import nom.bdezonia.zorbage.storage.sparse.SparseStorage;

/**
 * 
 * @author Barry DeZonia
 *
 */
public class Storage {

	/**
	 * 
	 * @param numElements
	 * @param type
	 * @return
	 */
	public static <U extends Allocatable<U>> IndexedDataSource<U>
		allocate(long numElements, U type)
	{
		try {

			return ArrayStorage.allocate(numElements, type);

		}
		// not enough mem avail
		catch (OutOfMemoryError e) {

			return FileStorage.allocate(numElements, type);

		}
		// probably numElements bigger than will fit in an array
		catch (IllegalArgumentException e) {

			return FileStorage.allocate(numElements, type);

		}
	}
	
	/**
	 * 
	 * @param strategy
	 * @param numElements
	 * @param type
	 * @return
	 */
	public static <U extends Allocatable<U>> IndexedDataSource<U>
		allocate(StorageConstruction strategy, long numElements, U type)
	{
		if (strategy == StorageConstruction.MEM_ARRAY)
			return ArrayStorage.allocate(numElements, type);
		else if (strategy == StorageConstruction.MEM_SPARSE)
			return SparseStorage.allocate(numElements, type);
		else if (strategy == StorageConstruction.MEM_VIRTUAL)
			return FileStorage.allocate(numElements, type);
		throw new IllegalArgumentException("Unknown storage strategy "+strategy);
	}
}

@@ -1569,16 +1569,74 @@
 uction;%0A
+import nom.bdezonia.zorbage.datasource.BigListDataSource;%0A
 import n
@@ -2095,17 +2095,16 @@
 %09%09try %7B%0A
-%0A
 %09%09%09retur
@@ -2151,227 +2151,162 @@
 e);%0A
-%0A
 %09%09%7D%0A%09%09
-// not enough mem avail%0A%09%09catch (OutOfMemoryError
+catch (Exception
  e) %7B%0A
-%0A
 %09%09%09
-return FileStorage.allocate(numElements, type);%0A%0A%09%09%7D%0A%09%09// probably numElements bigger than will fit i
+// out of memory%0A%09%09%09// or requested size is larger tha
 n an
+y
  array
-%0A%09%09catch (IllegalArgumentException e) %7B%0A%0A%09
+ can hold%0A%09%09%09// or others I might be too accepting of%0A%09%09%7D%0A
 %09%09re
@@ -2354,13 +2354,8 @@
 pe);
-%0A%0A%09%09%7D
 %0A%09%7D%0A
@@ -2623,16 +2623,18 @@
 M_ARRAY)
+ %7B
 %0A%09%09%09retu
@@ -2670,32 +2670,409 @@
 lements, type);%0A
+%09%09%09// if would be nice if we tested size and if too big for array we tried to%0A%09%09%09//   allocate a BigListDataSource. But that ctor needs an Algebra. At one%0A%09%09%09//   time I tried changing these signatures to take (numElem, algebra)%0A%09%09%09//   which was pretty elegant but it killed the ability to support the%0A%09%09%09//   Point type with varying sized elements (2d or 3d or 4d etc.).%0A%09%09%7D%0A
 %09%09else if (strat

Simplify the Storage allocator's code flow