    001    package org.maltparser.ml.lib;
    002
    003    import 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     */
    012    public class FeatureList  {
    013            private static final long serialVersionUID = 7526471155622776147L;
    014            private 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             * Adds a MaltFeatureNode object to the feature list. The object will be added in the sorted feature list based on
    025             * the compareTo() in MaltFeatureNode.
    026             *
    027             * @param x a MaltFeatureNode object
    028             */
    029            public void add(MaltFeatureNode x) {
    030                    if (list.size() == 0) {
    031                            list.add(x);
    032                    } else {
    033                    int low = 0;
    034                    int high = list.size() - 1;
    035                    int mid;
    036
    037                    while( low <= high ) {
    038                        mid = (low + high) / 2;
    039
    040                        if( list.get(mid).compareTo(x) < 0 ) {
    041                            low = mid + 1;
    042                        } else if( list.get(mid).compareTo(x) > 0 ) {
    043                            high = mid - 1;
    044                        } else {
    045                            break;
    046                        }
    047                    }
    048                    list.add(low,x);
    049                    }
    050            }
    051
    052            /**
    053             * Adds an index/value pair to the feature list.
    054             *
    055             * @param index a binarized feature index
    056             * @param value a value
    057             */
    058            public void add(int index, double value) {
    059                    add(new MaltFeatureNode(index,value));
    060            }
    061
    062            /**
    063             * @param i the position in the feature list
    064             * @return a MaltFeatureNode object located on the position <i>i</i>
    065             */
    066            public MaltFeatureNode get(int i) {
    067                    if (i < 0 || i >= size()) {
    068                            return null;
    069                    }
    070                    return list.get(i);
    071            }
    072
    073            /**
    074             * Clears the feature list
    075             */
    076            public void clear() {
    077                    list.clear();
    078            }
    079
    080            /**
    081             * @return the size of the feature list
    082             */
    083            public int size() {
    084                    return list.size();
    085            }
    086
    087            public MaltFeatureNode[] toArray() {
    088                    final MaltFeatureNode[] nodes = new MaltFeatureNode[list.size()];
    089                    int len = nodes.length;
    090                    for (int i = 0; i < len; i++) {
    091                            nodes[i] = list.get(i);
    092                    }
    093                    return nodes;
    094            }
    095    }