001/* 002 AUTOMATICALLY GENERATED BY jTemp FROM 003 /Users/jsh2/Work/openimaj/target/checkout/core/core/src/main/jtemp/org/openimaj/data/#T#ArrayBackedDataSource.jtemp 004*/ 005/** 006 * Copyright (c) 2011, The University of Southampton and the individual contributors. 007 * All rights reserved. 008 * 009 * Redistribution and use in source and binary forms, with or without modification, 010 * are permitted provided that the following conditions are met: 011 * 012 * * Redistributions of source code must retain the above copyright notice, 013 * this list of conditions and the following disclaimer. 014 * 015 * * Redistributions in binary form must reproduce the above copyright notice, 016 * this list of conditions and the following disclaimer in the documentation 017 * and/or other materials provided with the distribution. 018 * 019 * * Neither the name of the University of Southampton nor the names of its 020 * contributors may be used to endorse or promote products derived from this 021 * software without specific prior written permission. 022 * 023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 025 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 027 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 030 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 033 */ 034package org.openimaj.data; 035 036import java.util.Iterator; 037import java.util.Random; 038 039import org.openimaj.util.array.ArrayIterator; 040 041/** 042 * A {@link DataSource} backed by a 2D array of doubles. 043 * 044 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 045 * 046 */ 047public class DoubleArrayBackedDataSource implements DataSource<double[]> { 048 protected double[][] data; 049 protected Random rng; 050 051 /** 052 * Construct with data 053 * @param data the data 054 */ 055 public DoubleArrayBackedDataSource(double[][] data) { 056 this.data = data; 057 this.rng = new Random(); 058 } 059 060 /** 061 * Construct with data and a random generator for random sampling 062 * @param data the data 063 * @param rng the random generator 064 */ 065 public DoubleArrayBackedDataSource(double[][] data, Random rng) { 066 this.data = data; 067 this.rng = rng; 068 } 069 070 @Override 071 public final void getData(int startRow, int stopRow, double[][] output) { 072 for (int i=startRow, j=0; i<stopRow; i++, j++) 073 System.arraycopy(data[i], 0, output[j], 0, data[i].length); 074 } 075 076 @Override 077 public final void getRandomRows(double[][] output) { 078 final int k = output.length; 079 final int [] ind = RandomData.getUniqueRandomInts(k, 0, data.length, rng); 080 081 for (int i=0; i<k; i++) 082 System.arraycopy(data[ind[i]], 0, output[i], 0, data[ind[i]].length); 083 } 084 085 @Override 086 public int numDimensions() { 087 return data[0].length; 088 } 089 090 @Override 091 public int size() { 092 return data.length; 093 } 094 095 @Override 096 public double[] getData(int row) { 097 return data[row]; 098 } 099 100 @Override 101 public Iterator<double[]> iterator() { 102 return new ArrayIterator<double[]>(data); 103 } 104 105 @Override 106 public double[][] createTemporaryArray(int size) { 107 return new double[size][data[0].length]; 108 } 109}