-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_cache.cpp
35 lines (32 loc) · 910 Bytes
/
feature_cache.cpp
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
//
// CRF++PIVAJ -- A CRF toolkit derived from CRF++ for
// the PIVAJ project (see http://plair.univ-rouen.fr).
//
//
// Copyright(C) 2014 Julien Lerouge <julien@lerouge.me>
// Copyright(C) 2005-2007 Taku Kudo <taku@chasen.org>
//
#include <algorithm>
#include "feature_cache.h"
namespace CRFPP {
void FeatureCache::add(const std::vector<int> &f) {
int *p = feature_freelist_.alloc(f.size() + 1);
std::copy(f.begin(), f.end(), p);
p[f.size()] = -1; // sentinel
this->push_back(p);
}
void FeatureCache::shrink(std::map<int, int> *old2new) {
for (size_t i = 0; i < size(); ++i) {
std::vector<int> newf;
for (int *f = (*this)[i]; *f != -1; ++f) {
std::map<int, int>::iterator it = old2new->find(*f);
if (it != old2new->end()) {
newf.push_back(it->second);
}
}
newf.push_back(-1);
std::copy(newf.begin(), newf.end(), (*this)[i]);
}
return;
}
}