001    // Copyright 2012, 2013 Brad Block, Pawjaw, LLC. (an Ohio Limited Liability Company)
002    // 
003    // This file is part of JBTCRF.
004    // 
005    // JBTCRF is free software: you can redistribute it and/or modify
006    // it under the terms of the GNU General Public License as published by
007    // the Free Software Foundation, either version 3 of the License, or
008    // (at your option) any later version.
009    // 
010    // JBTCRF is distributed in the hope that it will be useful,
011    // but WITHOUT ANY WARRANTY; without even the implied warranty of
012    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013    // GNU General Public License for more details.
014    // 
015    // You should have received a copy of the GNU General Public License
016    // along with JBTCRF.  If not, see <http://www.gnu.org/licenses/>.
017    
018    package com.pawjaw.classification.crf.lmcbt.points;
019    
020    import com.pawjaw.classification.crf.lmcbt.configurations.Configuration;
021    import java.util.Arrays;
022    
023    public class SparsePoint implements Point {
024        private int[] posfs;
025    
026        public void setFeatures(boolean[] bs) {
027            int f, F = bs.length, n = 0, N = 0;
028            for(f = 0;f < F;f++)
029                if(bs[f])
030                    N++;
031            posfs = new int[N];
032            for(f = 0;f < F;f++)
033                if(bs[f])
034                    posfs[n++] = f;
035        }
036    
037        public void setFeatures(int[] positive_features) {
038            posfs = positive_features;
039            Arrays.sort(posfs);
040        }
041    
042        public boolean hasTrueFeature(Configuration c, int feature_index) {
043            int l = 0, h = posfs.length - 1, m;
044            while(l <= h) {
045                if(feature_index < posfs[m = (l + h) / 2])
046                    h = m - 1;
047                else if(feature_index > posfs[m])
048                    l = m + 1;
049                else
050                    return true;
051            }
052            return false;
053        }
054    
055        public int nextTrueFeature(Configuration c, int offset) {
056            if(offset < 0 || offset >= posfs.length)
057                return -1;
058            else
059                return posfs[offset];
060        }
061    
062        public int trueFeatureCount() {
063            return posfs.length;
064        }
065    }