001/** 
002 * Copyright (c) 2007-2008, Regents of the University of Colorado 
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
009 * 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. 
010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
011 * 
012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE. 
023 */
024package org.cleartk.ml.util.featurevector;
025
026import java.util.ArrayList;
027import java.util.List;
028
029/**
030 * <br>
031 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
032 * All rights reserved.
033 * 
034 * 
035 * @author Philipp Wetzler
036 * 
037 */
038
039public class ArrayFeatureVector extends FeatureVector {
040
041  ArrayList<Double> features;
042
043  public ArrayFeatureVector() {
044    features = new ArrayList<Double>();
045  }
046
047  public double get(int index) {
048    if (index < features.size())
049      return features.get(index);
050    else
051      return 0.0;
052  }
053
054  public java.util.Iterator<Entry> iterator() {
055    return new Iterator(features);
056  }
057
058  public void set(int index, double value) throws InvalidFeatureVectorValueException {
059    if (Double.isInfinite(value) || Double.isNaN(value))
060      throw new InvalidFeatureVectorValueException(index, value);
061
062    for (int i = features.size(); i <= index; i++)
063      features.add(0.0);
064
065    features.set(index, value);
066  }
067
068  public double[] toArray() {
069    double[] result = new double[features.size()];
070    int index = 0;
071
072    for (Double d : features) {
073      result[index] = d;
074      index += 1;
075    }
076
077    return result;
078  }
079
080  static class Iterator implements java.util.Iterator<FeatureVector.Entry> {
081
082    int currentIndex;
083
084    java.util.ListIterator<Double> subIterator;
085
086    public Iterator(List<Double> features) {
087      currentIndex = 0;
088      subIterator = features.listIterator();
089      moveToNext();
090    }
091
092    public boolean hasNext() {
093      return subIterator.hasNext();
094    }
095
096    public Entry next() {
097      int index = subIterator.nextIndex();
098      Double value = subIterator.next();
099      moveToNext();
100
101      return new FeatureVector.Entry(index, value.doubleValue());
102    }
103
104    public void remove() {
105      throw new UnsupportedOperationException();
106    }
107
108    private void moveToNext() {
109      while (subIterator.hasNext()) {
110        Double d = subIterator.next();
111        if (d != null && d.doubleValue() != 0.0) {
112          subIterator.previous();
113          return;
114        }
115      }
116    }
117
118  }
119
120}