Fleet  0.0.9
Inference in the LOT
Vectors.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 template<typename T>
6 std::vector<T> slice(const std::vector<T>& v, const size_t start, const int len) {
15  std::vector<T> out;
16  if(len > 0) { // in case len=-1
17  out.resize(len);
18  std::copy(v.begin() + start, v.begin()+start+len, out.begin());
19  }
20  return out;
21 }
22 
23 template<typename T>
24 std::vector<T> slice(const std::vector<T> &v, const size_t start) {
32  // just chop off the end
33  return slice(v, start, v.size()-start);
34 }
35 
36 template<typename T>
37 void increment(std::vector<T>& v, const size_t idx, T count=1) {
38  // Increment and potentially increase my vector size if needed
39  if(idx >= v.size()) {
40  v.resize(idx+1,0);
41  }
42 
43  v[idx] += count;
44 }
45 
66 template<typename T>
67 void glom(std::vector<std::vector<T>*>& X, std::vector<T>*& v) {
68 
69  // if v is a prefix of anything in x, then
70  // we can just use that.
71  for(auto& x : X) {
72  if(is_prefix(*v, *x)) {
73  delete v;
74  v = x; // set V to the existing datset
75  return;
76  }
77  }
78 
79  // else check if v extends anything in X, and if it does,
80  // find the longest match and add the remaining elements
81  // to the stuff stored in X
82  size_t mx = 0;
83  std::vector<T>* match = nullptr;
84  for(auto& x : X) {
85  if(is_prefix(*x, *v) and x->size() > mx) {
86  mx = x->size();
87  match = x;
88  }
89  }
90 
91  // if anything matched in X
92  if(match != nullptr) {
93 
94  // append the remaining elements to x
95  for(size_t i=mx;i<v->size();i++)
96  match->push_back(v->at(i));
97 
98  // delete v since its not needed anymore
99  delete v;
100 
101  // and we can use match
102  v = match;
103  }
104  else {
105  // else nothing matches so we have to just return v;
106  X.push_back(v);
107  }
108 
109 
110 }
void glom(std::vector< std::vector< T > *> &X, std::vector< T > *&v)
Glom here takes a vector of pointers to vectors. It searches through X to find if anything matches th...
Definition: Vectors.h:67
size_t count(const std::string &str, const std::string &sub)
Definition: Strings.h:153
std::vector< T > slice(const std::vector< T > &v, const size_t start, const int len)
Definition: Vectors.h:6
bool is_prefix(const T &prefix, const T &x)
Check if prefix is a prefix of x – works with iterables, including strings and vectors.
Definition: Strings.h:39
void increment(std::vector< T > &v, const size_t idx, T count=1)
Definition: Vectors.h:37