-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_cache.cpp
45 lines (40 loc) · 1.07 KB
/
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
36
37
38
39
40
41
42
43
44
45
//
// CRF++ -- Yet Another CRF toolkit
//
// $Id: feature_cache.cpp 1587 2007-02-12 09:00:36Z taku $;
//
// Copyright(C) 2005-2007 Taku Kudo <[email protected]>
//
#include <algorithm>
#include <iostream>
#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]);
}
}
void FeatureCache::dump() {
std::cout << "feature_cache_size: " << size() << std::endl;
for (size_t i = 0; i < size(); ++i) {
for (int *f = (*this)[i]; *f != -1; ++f) {
std::cout << (*f) << ',';
}
std::cout << std::endl;
}
}
}