Fleet  0.0.9
Inference in the LOT
CachedCallHypothesis.h
Go to the documentation of this file.
1 #pragma once
2 
3 
13 template<typename this_t, typename datum_t, typename output_t> // the LOTHypothesis_type from which we read outputs and data etc
15 
16  void const* cached_data = nullptr; // when null, we recompute the cache
17 public:
18  std::vector<output_t> cache;
19  bool got_error; // did any data point throw an error?
20 
21  CachedCallHypothesis() : got_error(false) { }
22 
23  void clear_cache() {
24  cached_data = nullptr;
25  got_error = false;
26  }
27 
33  virtual output_t cached_call_wrapper(const datum_t& di) { throw NotImplementedError(); }
34 // {
35 // return static_cast<this_t*>(this)->call(di.input);
36 // }
37 //
38  void cached_call(const std::vector<datum_t>& data, bool break_on_error=true) {
39 
40  // NOTE THIS DOES NOT WORK IF WE HAVE RECURSION
41  // at the level of a lexicon!
42 
43  if(cached_data != &data) {
44  // PRINTN("CACHE MISS", this, cached_data, string());
45  cache.resize(data.size());
46 
47  // store who is cached
48  cached_data = &data;
49 
50  // now process the data
51  for(size_t di=0;di<data.size();di++) {
52  try {
53  cache[di] = this->cached_call_wrapper(data[di]);
54  } catch( std::exception& e){
55  got_error = true;
56  cache[di] = output_t{};
57  if(break_on_error) break; // this break is useful when errors give us -inf, it's much faster
58  }
59  }
60 
61  }
62  }
63 
64 };
void cached_call(const std::vector< datum_t > &data, bool break_on_error=true)
Definition: CachedCallHypothesis.h:38
CachedCallHypothesis()
Definition: CachedCallHypothesis.h:21
Definition: CachedCallHypothesis.h:14
bool got_error
Definition: CachedCallHypothesis.h:19
void clear_cache()
Definition: CachedCallHypothesis.h:23
std::vector< output_t > cache
Definition: CachedCallHypothesis.h:18
Definition: Errors.h:7
virtual output_t cached_call_wrapper(const datum_t &di)
This is how we access the data before calling – needed to say how this interfaces with the data...
Definition: CachedCallHypothesis.h:33