view src/samer/maths/Vec.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line source
/*
 *	Vec.java	
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.maths;

/**
	Interface for a vector that can be access via either
	an iterator (read only), or a double array (read/write)
	or via a Mat interface.
  */

public interface Vec 
{
	int			size();
	Iterator	iterator(); 
	double[]	array();				
	Mat			mat();

	/** access vector by iterator */
	public interface Iterator {
		boolean	more();
		double	next();
	}

	public interface InputIterator {
		boolean	more();
		void    next(double t);
	}

	/**
		An implementation of Vec that uses an array to
		store the values.
	  */

	public static class ForArray implements Vec, Mat, java.io.Serializable
	{
		double []	x;
		public ForArray( double[] a) { x=a; }
		public ForArray( int n) { x=new double[n]; }

		public int size() { return x.length; }
		public double[] array() { return x; }
		public Vec.Iterator iterator() { return new Iterator(); }
		public Mat mat() { return this; }
		public int width() { return size(); }
		public int height() { return 1; }
		public double get( int i, int j) { return x[j]; }
		public void	  set( int i, int j, double t) { x[j]=t; }

		class Iterator implements Vec.Iterator {
			int			i=0;
			
			public boolean more() { return i<x.length; }
			public double  next() { return x[i++]; }
		};
		
		class InputIterator implements Vec.InputIterator {
			int			i=0;
			
			public boolean more() { return i<x.length; }
			public void    next(double t) { x[i++]=t; }
		};
	}
}