001package org.maltparser.ml.lib;
002
003import java.util.ArrayList;
004
005
006/**
007 * The feature list is sorted according to the compareTo of the node.
008 * 
009 * @author Johan Hall
010 *
011 */
012public class FeatureList  {
013        private static final long serialVersionUID = 7526471155622776147L;
014        private final ArrayList<MaltFeatureNode> list;
015        
016        /**
017         * Creates a feature list of MaltFeatureNode objects
018         */
019        public FeatureList() {
020                list = new ArrayList<MaltFeatureNode>();
021        }
022        
023        /**
024         * Creates a feature list of MaltFeatureNode objects
025         */
026        public FeatureList(int size) {
027                list = new ArrayList<MaltFeatureNode>(size);
028        }
029        
030        /**
031         * Adds a MaltFeatureNode object to the feature list. The object will be added in the sorted feature list based on 
032         * the compareTo() in MaltFeatureNode.
033         * 
034         * @param x a MaltFeatureNode object
035         */
036        public void add(MaltFeatureNode x) {
037                if (list.size() == 0 || list.get(list.size()-1).compareTo(x) <= 0)  {
038                        list.add(x);
039                } else {
040                int low = 0;
041                int high = list.size() - 1;
042                int mid;
043                MaltFeatureNode y;
044                while (low <= high ) {
045                    mid = (low + high) / 2;
046                    y = list.get(mid); 
047                    if (y.compareTo(x) < 0 ) {
048                        low = mid + 1;
049                    } else if (y.compareTo(x) > 0 ) {
050                        high = mid - 1;
051                    } else {
052                        break;
053                    }
054                }
055                list.add(low,x);
056                }
057        }
058        
059        /**
060         * Adds an index/value pair to the feature list.
061         * 
062         * @param index a binarized feature index
063         * @param value a value 
064         */
065        public void add(int index, double value) {
066                add(new MaltFeatureNode(index,value));
067        }
068        
069        /**
070         * @param i the position in the feature list
071         * @return a MaltFeatureNode object located on the position <i>i</i>
072         */
073        public MaltFeatureNode get(int i) {
074                if (i < 0 || i >= list.size()) {
075                        return null;
076                }
077                return list.get(i);
078        }
079        
080        /**
081         * Clears the feature list
082         */
083        public void clear() {
084                list.clear();
085        }
086        
087        /**
088         * @return the size of the feature list
089         */
090        public int size() {
091                return list.size();
092        }
093        
094        public MaltFeatureNode[] toArray() {
095                final MaltFeatureNode[] nodes = new MaltFeatureNode[list.size()];
096                final int len = nodes.length;
097
098                for (int i = 0; i < len; i++) {
099                        nodes[i] = list.get(i);
100
101                }
102                return nodes;
103        }
104}