common.h

filter: Statementstmt 0.0% 0 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 3 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« CRF.xs ↑ Summary encoder.h »
1 //
2 // CRF++ -- Yet Another CRF toolkit
3 //
4 // $Id: common.h 1588 2007-02-12 09:03:39Z taku $;
5 //
6 // Copyright(C) 2005-2007 Taku Kudo <taku@chasen.org>
7 //
8 #ifndef _CRFPP_COMMON_H
9 #define _CRFPP_COMMON_H
10  
11 #include <setjmp.h>
12 #include <cstdlib>
13 #include <cstdio>
14 #include <cstring>
15 #include <string>
16 #include <strstream>
17 #include <iostream>
18 #include <algorithm>
19 #include <cmath>
20  
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24  
25 #define COPYRIGHT "CRF++: Yet Another CRF Tool Kit\nCopyright(C)" \
26 "2005-2007 Taku Kudo, All rights reserved.\n"
27 #define MODEL_VERSION 100
28  
29 #if defined(_WIN32) && !defined(__CYGWIN__)
30 # define OUTPUT_MODE std::ios::binary|std::ios::out
31 #else
32 # define OUTPUT_MODE std::ios::out
33 #endif
34  
35 #define BUF_SIZE 8192
36  
37 namespace CRFPP {
38 template <class T> inline T _min(T x, T y) { return(x < y) ? x : y; }
39 template <class T> inline T _max(T x, T y) { return(x > y) ? x : y; }
40  
41 template <class Iterator>
42 inline size_t tokenizeCSV(char *str,
43 Iterator out, size_t max) {
44 char *eos = str + std::strlen(str);
45 char *start = 0;
46 char *end = 0;
47 size_t n = 0;
48  
49 for (; str < eos; ++str) {
50 while (*str == ' ' || *str == '\t') ++str; // skip white spaces
51 bool inquote = false;
52 if (*str == '"') {
53 start = ++str;
54 end = start;
55 for (; str < eos; ++str) {
56 if (*str == '"') {
57 str++;
58 if (*str != '"')
59 break;
60 }
61 *end++ = *str;
62 }
63 inquote = true;
64 str = std::find(str, eos, ',');
65 } else {
66 start = str;
67 str = std::find(str, eos, ',');
68 end = str;
69 }
70 if (max-- > 1) *end = '\0';
71 *out++ = start;
72 ++n;
73 if (max == 0) break;
74 }
75  
76 return n;
77 }
78  
79 template <class Iterator>
80 inline size_t tokenize(char *str, const char *del,
81 Iterator out, size_t max) {
82 char *stre = str + std::strlen(str);
83 const char *dele = del + std::strlen(del);
84 size_t size = 0;
85  
86 while (size < max) {
87 char *n = std::find_first_of(str, stre, del, dele);
88 *n = '\0';
89 *out++ = str;
90 ++size;
91 if (n == stre) break;
92 str = n + 1;
93 }
94  
95 return size;
96 }
97  
98 // continus run of space is regarded as one space
99 template <class Iterator>
100 inline size_t tokenize2(char *str, const char *del,
101 Iterator out, size_t max) {
102 char *stre = str + std::strlen(str);
103 const char *dele = del + std::strlen(del);
104 size_t size = 0;
105  
106 while (size < max) {
107 char *n = std::find_first_of(str, stre, del, dele);
108 *n = '\0';
109 if (*str != '\0') {
110 *out++ = str;
111 ++size;
112 }
113 if (n == stre) break;
114 str = n + 1;
115 }
116  
117 return size;
118 }
119  
120 void inline dtoa(double val, char *s) {
121 std::sprintf(s, "%-16f", val);
122 char *p = s;
123 for (; *p != ' '; ++p) {}
124 *p = '\0';
125 return;
126 }
127  
128 template <class T> inline void itoa(T val, char *s) {
129 char *t;
130 T mod;
131  
132 if (val < 0) {
133 *s++ = '-';
134 val = -val;
135 }
136 t = s;
137  
138 while (val) {
139 mod = val % 10;
140 *t++ = static_cast<char>(mod)+ '0';
141 val /= 10;
142 }
143  
144 if (s == t) *t++ = '0';
145 *t = '\0';
146 std::reverse(s, t);
147  
148 return;
149 }
150  
151 template <class T>
152 inline void uitoa(T val, char *s) {
153 char *t;
154 T mod;
155 t = s;
156  
157 while (val) {
158 mod = val % 10;
159 *t++ = static_cast<char>(mod) + '0';
160 val /= 10;
161 }
162  
163 if (s == t) *t++ = '0';
164 *t = '\0';
165 std::reverse(s, t);
166  
167 return;
168 }
169  
170 #define _ITOA(_n) do { \
171 char buf[64]; \
172 itoa(_n, buf); \
173 append(buf); \
174 return *this; } while (0)
175  
176 #define _UITOA(_n) do { \
177 char buf[64]; \
178 uitoa(_n, buf); \
179 append(buf); \
180 return *this; } while (0)
181  
182 #define _DTOA(_n) do { \
183 char buf[64]; \
184 dtoa(_n, buf); \
185 append(buf); \
186 return *this; } while (0)
187  
188 class string_buffer: public std::string {
189 public:
190 string_buffer& operator<<(double _n) { _DTOA(_n); }
191 string_buffer& operator<<(short int _n) { _ITOA(_n); }
192 string_buffer& operator<<(int _n) { _ITOA(_n); }
193 string_buffer& operator<<(long int _n) { _ITOA(_n); }
194 string_buffer& operator<<(unsigned short int _n) { _UITOA(_n); }
195 string_buffer& operator<<(unsigned int _n) { _UITOA(_n); }
196 string_buffer& operator<<(unsigned long int _n) { _UITOA(_n); }
197 string_buffer& operator<<(char _n) {
198 push_back(_n);
199 return *this;
200 }
201 string_buffer& operator<<(const char* _n) {
202 append(_n);
203 return *this;
204 }
205 string_buffer& operator<<(const std::string& _n) {
206 append(_n);
207 return *this;
208 }
209 };
210  
211 class die {
212 public:
213 die() {}
214 virtual ~die() {
215 std::cerr << std::endl;
216 exit(-1);
217 }
218 int operator&(std::ostream&) { return 0; }
219 };
220  
221 class warn {
222 public:
223 warn() {}
224 virtual ~warn() { std::cerr << std::endl; }
225 int operator&(std::ostream&) { return 0; }
226 };
227  
228 struct whatlog {
229 std::ostrstream stream_;
230 0 const char *str() {
231 0 stream_ << std::ends;
232 0 return stream_.str();
233 }
234 jmp_buf cond_;
235 };
236  
237 class wlog {
238 public:
239 whatlog *l_;
240 explicit wlog(whatlog *l): l_(l) { l_->stream_.clear(); }
241 ~wlog() { longjmp(l_->cond_, 1); }
242 int operator&(std::ostream &) { return 0; }
243 };
244 }
245  
246 #define WHAT what_.stream_
247  
248 #define CHECK_RETURN(condition, value) \
249 if (!(condition)) \
250 if (setjmp(what_.cond_) == 1) { \
251 return value; \
252 } else \
253 wlog(&what_) & what_.stream_ << \
254 __FILE__ << "(" << __LINE__ << ") [" << #condition << "] "
255  
256 #define CHECK_0(condition) CHECK_RETURN(condition, 0)
257 #define CHECK_FALSE(condition) CHECK_RETURN(condition, false)
258  
259 #define CHECK_CLOSE_FALSE(condition) \
260 if (!(condition)) \
261 if (setjmp(what_.cond_) == 1) { \
262 close(); \
263 return false; \
264 } else \
265 wlog(&what_) & what_.stream_ << \
266 __FILE__ << "(" << __LINE__ << ") [" << #condition << "] "
267  
268 #define CHECK_DIE(condition) \
269 (condition) ? 0 : die() & std::cerr << __FILE__ << \
270 "(" << __LINE__ << ") [" << #condition << "] "
271  
272 #define CHECK_WARN(condition) \
273 (condition) ? 0 : warn() & std::cerr << __FILE__ << \
274 "(" << __LINE__ << ") [" << #condition << "] "
275 #endif
276  
« CRF.xs ↑ Summary encoder.h »