-
Notifications
You must be signed in to change notification settings - Fork 193
/
freelist.h
72 lines (61 loc) · 1.41 KB
/
freelist.h
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
//
// CRF++ -- Yet Another CRF toolkit
//
// $Id: freelist.h 1588 2007-02-12 09:03:39Z taku $;
//
// Copyright(C) 2005-2007 Taku Kudo <taku@chasen.org>
//
#ifndef CRFPP_FREELIST_H_
#define CRFPP_FREELIST_H_
#include <vector>
#include <cstring>
namespace CRFPP {
template <class T>
class Length {
public:
size_t operator()(const T *str) const { return 1; }
};
class charLength {
public:
size_t operator()(const char *str) const { return strlen(str) + 1; }
};
template <class T, class LengthFunc = Length<T> >
class FreeList {
private:
std::vector <T *> freeList;
size_t pi;
size_t li;
size_t size;
public:
void free() { li = pi = 0; }
T* alloc(size_t len = 1) {
if ((pi + len) >= size) {
li++;
pi = 0;
}
if (li == freeList.size()) {
freeList.push_back(new T[size]);
}
T* r = freeList[li] + pi;
pi += len;
return r;
}
T* dup(T *src, size_t len = 0) {
if (!len) len = LengthFunc () (src);
T *p = alloc(len);
if (src == 0) memset (p, 0, len * sizeof (T));
else memcpy(p, src, len * sizeof(T));
return p;
}
void set_size(size_t n) { size = n; }
explicit FreeList(size_t _size): pi(0), li(0), size(_size) {}
explicit FreeList(): pi(0), li(0), size(0) {}
virtual ~FreeList() {
for (li = 0; li < freeList.size(); ++li) {
delete [] freeList[li];
}
}
};
typedef FreeList<char, charLength> StrFreeList;
}
#endif