summaryrefslogtreecommitdiff
path: root/decoder/feature_accum.h
blob: 851b29dbd9645372fc039973dad872595e8255ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef FEATURE_ACCUM_H
#define FEATURE_ACCUM_H

#include "ff.h"
#include "sparse_vector.h"
#include "value_array.h"

struct SparseFeatureAccumulator : public FeatureVector {
  typedef FeatureVector State;
  SparseFeatureAccumulator() {  }
  template <class FF>
  FeatureVector const& describe(FF const& ) { return *this; }
  void Store(FeatureVector *fv) const {
    fv->set_from(*this);
  }
  template <class FF>
  void Store(FF const& /* ff */,FeatureVector *fv) const {
    fv->set_from(*this);
  }
  template <class FF>
  void Add(FF const& /* ff */,FeatureVector const& fv) {
    (*this)+=fv;
  }
  void Add(FeatureVector const& fv) {
    (*this)+=fv;
  }
  /*
  SparseFeatureAccumulator(FeatureVector const& fv) : State(fv) {}
  FeatureAccumulator(Features const& fids) {}
  FeatureAccumulator(Features const& fids,FeatureVector const& fv) : State(fv) {}
  void Add(Features const& fids,FeatureVector const& fv) {
    *this += fv;
  }
  */
  void Add(int i,Featval v) {
    (*this)[i]+=v;
  }
  void Add(Features const& fids,int i,Featval v) {
    (*this)[i]+=v;
  }
};

struct SingleFeatureAccumulator {
  typedef Featval State;
  typedef SingleFeatureAccumulator Self;
  State v;
  /*
  void operator +=(State const& o) {
    v+=o;
  }
  */
  void operator +=(Self const& s) {
    v+=s.v;
  }
  SingleFeatureAccumulator() : v() {}
  template <class FF>
  State const& describe(FF const& ) const { return v; }

  template <class FF>
  void Store(FF const& ff,FeatureVector *fv) const {
    fv->set_value(ff.fid_,v);
  }
  void Store(Features const& fids,FeatureVector *fv) const {
    assert(fids.size()==1);
    fv->set_value(fids[0],v);
  }
  /*
  SingleFeatureAccumulator(Features const& fids) { assert(fids.size()==1); }
  SingleFeatureAccumulator(Features const& fids,FeatureVector const& fv)
  {
    assert(fids.size()==1);
    v=fv.get_singleton();
  }
  */

  template <class FF>
  void Add(FF const& ff,FeatureVector const& fv) {
    v+=fv.get(ff.fid_);
  }
  void Add(FeatureVector const& fv) {
    v+=fv.get_singleton();
  }

  void Add(Features const& fids,FeatureVector const& fv) {
    v += fv.get(fids[0]);
  }
  void Add(Featval dv) {
    v+=dv;
  }
  void Add(int,Featval dv) {
    v+=dv;
  }
  void Add(FeatureVector const& fids,int i,Featval dv) {
    assert(fids.size()==1 && i==0);
    v+=dv;
  }
};


#if 0
// omitting this so we can default construct an accum.  might be worth resurrecting in the future
struct ArrayFeatureAccumulator : public ValueArray<Featval> {
  typedef ValueArray<Featval> State;
  template <class Fsa>
  ArrayFeatureAccumulator(Fsa const& fsa) : State(fsa.features_.size()) { }
  ArrayFeatureAccumulator(Features const& fids) : State(fids.size()) {  }
  ArrayFeatureAccumulator(Features const& fids) : State(fids.size()) {  }
  ArrayFeatureAccumulator(Features const& fids,FeatureVector const& fv) : State(fids.size()) {
    for (int i=0,e=i<fids.size();i<e;++i)
      (*this)[i]=fv.get(i);
  }
  State const& describe(Features const& fids) const { return *this; }
  void Store(Features const& fids,FeatureVector *fv) const {
    assert(fids.size()==size());
    for (int i=0,e=i<fids.size();i<e;++i)
      fv->set_value(fids[i],(*this)[i]);
  }
  void Add(Features const& fids,FeatureVector const& fv) {
    for (int i=0,e=i<fids.size();i<e;++i)
      (*this)[i]+=fv.get(i);
  }
  void Add(FeatureVector const& fids,int i,Featval v) {
    (*this)[i]+=v;
  }
};
#endif


#endif