/** * %SVN.HEADER% */ package net.sf.javaml.core; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import java.util.logging.Logger; import Examples.Triangle; import Sequenic.T3.SuiteUtils.Inference.Comment; /** * Implementation of a dense instance. A dense instance is a wrapper around a * double array that provides a value for each attribute index. * * * * @see Dataset * @see Instance * * @version %SVN.REVISION% * * @author Thomas Abeel * */ public class DenseInstance extends AbstractInstance implements Instance { private static final long serialVersionUID = 3284511291715269081L; private final static Comment comment = new Comment(Logger.getLogger(DenseInstance.class.getName())) ; /* Holds values */ private double[] attributes; /** * Creates a new instance with the provide value for the attributes. The * class label will be set to null. * * @param att * the value of the instance */ public DenseInstance(double[] att) { this(att, null); } /** * Creates a new instance with the provided attribute values and the * provided class label. * * @param att * the attribute values * @param classValue * the class label */ public DenseInstance(double[] att, Object classValue) { //super(classValue); this.attributes = att.clone(); } /* Hide argumentless constructor */ private DenseInstance() { } /** * Creates an instance that has space for the supplied number of attributes. * By default all attributes are initialized by zero. * * @param size * the number of attributes */ public DenseInstance(int size) { this(new double[size]); } @Override public double value(int pos) { return attributes[pos]; } @Override public void clear() { attributes = new double[attributes.length]; } @Override public boolean containsKey(Object key) { if (key instanceof Integer) { int i = (Integer) key; return i >= 0 && i < attributes.length; } else { comment.with("invalid key").end() ; return false; } } @Override public boolean containsValue(Object value) { if (value instanceof Number) { double val = ((Number) value).doubleValue(); for (int i = 0; i < attributes.length; i++) { if (Math.abs(val - attributes[i]) < 0.00000001) return true; } } comment.with("not found").end() ; return false; } @Override public Set> entrySet() { HashMap map = new HashMap(); for (int i = 0; i < attributes.length; i++) map.put(i, attributes[i]); return map.entrySet(); } @Override public Double get(Object key) { return attributes[(Integer) key]; } @Override public boolean isEmpty() { return false; } @Override public SortedSet keySet() { TreeSet keys = new TreeSet(); for (int i = 0; i < attributes.length; i++) keys.add(i); return keys; } @Override public Double put(Integer key, Double value) { double val = attributes[key]; attributes[key] = value; return val; } @Override public void putAll(Map m) { for (Integer key : m.keySet()) { attributes[key] = m.get(key); } } @Override public Double remove(Object key) { throw new UnsupportedOperationException("Cannot unset values from a dense instance."); } @Override @Deprecated public int size() { return attributes.length; } @Override public Collection values() { Collection vals = new ArrayList(); for (double v : attributes) vals.add(v); return vals; } @Override public int noAttributes() { return attributes.length; } @Override public String toString() { //return "{" + Arrays.toString(attributes) + ";" + classValue() + "}"; return "{" + Arrays.toString(attributes) + "}"; } @Override public void removeAttribute(int i) { double[] tmp = attributes.clone(); attributes = new double[tmp.length - 1]; System.arraycopy(tmp, 0, attributes, 0, i); System.arraycopy(tmp, i + 1, attributes, i, tmp.length - i - 1); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Arrays.hashCode(attributes); return result; } @Override public boolean equals(Object obj) { if (this == obj) { comment.with("see self").end() ; return true; } if (obj == null) { comment.with("see null").end() ; return false; } if (getClass() != obj.getClass()) { comment.with("see different type").end() ; return false; } final DenseInstance other = (DenseInstance) obj; if (!Arrays.equals(attributes, other.attributes)) { return false; } comment.with("looks equal").end() ; return true; } @Override public DenseInstance copy() { DenseInstance out = new DenseInstance(); out.attributes = this.attributes.clone(); //out.setClassValue(this.classValue()); return out; } @Override public void removeAttributes(Set indices) { double[] tmp = attributes.clone(); attributes = new double[tmp.length - indices.size()]; int index = 0; for (int i = 0; i < tmp.length; i++) { if (!indices.contains(i)) { attributes[index++] = tmp[i]; } } } }