001 /**
002 *
003 */
004 package calhoun.analysis.crf;
005
006 import java.util.ArrayList;
007
008 import calhoun.util.Assert;
009
010 public class CacheStrategySpec {
011 public enum CacheStrategy { COMPOSITE, CONSTANT, SPARSE, UNSPECIFIED, DENSE_NODE_BOUNDARY, DENSE, LENGTHFUNCTION /*, INTERVAL, LENGTH, NO_CACHE */}
012
013 public CacheStrategy strategy;
014 public Object details;
015
016 public CacheStrategySpec(CacheStrategy strategy, Object details) {
017 this.strategy = strategy;
018 this.details = details;
019 }
020
021 public CacheStrategySpec(CacheStrategy strategy) {
022 this(strategy,null);
023 }
024
025 /** Used in cases where the feature will return a value at every edge and/or node. */
026 public static class DenseCachingDetails {
027
028 public int nTables;
029 public int nEvals;
030 public int[] potential;
031 public int[] tableNum;
032 public short[] featureIndex;
033
034 public void check() {
035 Assert.a(nTables >= 0);
036 Assert.a(potential.length == nEvals);
037 Assert.a(tableNum.length == nEvals);
038 Assert.a(featureIndex.length == nEvals);
039
040 for (int j=0; j<nEvals; j++) {
041 Assert.a(potential[j] >=0);
042 Assert.a(tableNum[j]>=0);
043 Assert.a(tableNum[j] < nTables);
044 Assert.a(featureIndex[j] >=0);
045 }
046 }
047 }
048
049 // Used when a FeatureManagerNodeBoundaries is an explicit length node feature and the value it returns is obtained by subtracting two cumulative sums.
050 public static class DenseBoundaryCachingDetails {
051
052 public int nTables;
053 public ArrayList<DenseBoundaryEntry> entries;
054
055 public DenseBoundaryCachingDetails (int nTables) {
056 this.nTables = nTables;
057 entries = new ArrayList<DenseBoundaryEntry>();
058 }
059
060 public void add(int potential, int tableNum, int featureIndex, int rightPad, int leftPad) {
061 entries.add(new DenseBoundaryEntry(potential,tableNum,featureIndex,rightPad,leftPad));
062 }
063
064 public void check() {
065 int maxTable = 0;
066 for (int j=0; j<entries.size(); j++) {
067 DenseBoundaryEntry dbe = entries.get(j);
068 dbe.check();
069 int tnum = dbe.tableNum;
070 if (tnum > maxTable) {
071 maxTable = tnum;
072 }
073 }
074 Assert.a(maxTable == (nTables-1));
075 }
076 }
077
078 public static class DenseBoundaryEntry {
079 public int potential;
080 public int tableNum;
081 public int featureIndex;
082 public int rightPad;
083 public int leftPad;
084
085 public DenseBoundaryEntry(int potential, int tableNum, int featureIndex, int rightPad, int leftPad) {
086 this.potential = potential;
087 this.tableNum = tableNum;
088 this.featureIndex = featureIndex;
089 this.rightPad = rightPad;
090 this.leftPad = leftPad;
091 check();
092 }
093
094 private void check() {
095 Assert.a(potential >= 0);
096 Assert.a(tableNum >= 0);
097 Assert.a(featureIndex >= 0);
098 Assert.a(rightPad >= 0);
099 Assert.a(leftPad >= 0);
100 }
101 }
102
103 }