File size: 7,358 Bytes
69fb171 |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
//
// CRF++ -- Yet Another CRF toolkit
//
// $Id: common.h 1588 2007-02-12 09:03:39Z taku $;
//
// Copyright(C) 2005-2007 Taku Kudo <[email protected]>
//
#ifndef CRFPP_COMMON_H__
#define CRFPP_COMMON_H__
#include <setjmp.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cmath>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define COPYRIGHT "CRF++: Yet Another CRF Tool Kit\nCopyright (C) " \
"2005-2009 Taku Kudo, All rights reserved.\n"
#define MODEL_VERSION 100
#if defined(_WIN32) && !defined(__CYGWIN__)
# define OUTPUT_MODE std::ios::binary|std::ios::out
#else
# define OUTPUT_MODE std::ios::out
#endif
#define BUF_SIZE 8192
namespace CRFPP {
template <class T> inline T _min(T x, T y) { return(x < y) ? x : y; }
template <class T> inline T _max(T x, T y) { return(x > y) ? x : y; }
// helper functions defined in the paper
inline double sigma(double x) {
if (x > 0) return 1.0;
else if (x < 0) return -1.0;
return 0.0;
}
template <class Iterator>
inline size_t tokenizeCSV(char *str,
Iterator out, size_t max) {
char *eos = str + std::strlen(str);
char *start = 0;
char *end = 0;
size_t n = 0;
for (; str < eos; ++str) {
while (*str == ' ' || *str == '\t') ++str; // skip white spaces
bool inquote = false;
if (*str == '"') {
start = ++str;
end = start;
for (; str < eos; ++str) {
if (*str == '"') {
str++;
if (*str != '"')
break;
}
*end++ = *str;
}
inquote = true;
str = std::find(str, eos, ',');
} else {
start = str;
str = std::find(str, eos, ',');
end = str;
}
if (max-- > 1) *end = '\0';
*out++ = start;
++n;
if (max == 0) break;
}
return n;
}
template <class Iterator>
inline size_t tokenize(char *str, const char *del,
Iterator out, size_t max) {
char *stre = str + std::strlen(str);
const char *dele = del + std::strlen(del);
size_t size = 0;
while (size < max) {
char *n = std::find_first_of(str, stre, del, dele);
*n = '\0';
*out++ = str;
++size;
if (n == stre) break;
str = n + 1;
}
return size;
}
// continus run of space is regarded as one space
template <class Iterator>
inline size_t tokenize2(char *str, const char *del,
Iterator out, size_t max) {
char *stre = str + std::strlen(str);
const char *dele = del + std::strlen(del);
size_t size = 0;
while (size < max) {
char *n = std::find_first_of(str, stre, del, dele);
*n = '\0';
if (*str != '\0') {
*out++ = str;
++size;
}
if (n == stre) break;
str = n + 1;
}
return size;
}
void inline dtoa(double val, char *s) {
std::sprintf(s, "%-16f", val);
char *p = s;
for (; *p != ' '; ++p) {}
*p = '\0';
return;
}
template <class T> inline void itoa(T val, char *s) {
char *t;
T mod;
if (val < 0) {
*s++ = '-';
val = -val;
}
t = s;
while (val) {
mod = val % 10;
*t++ = static_cast<char>(mod)+ '0';
val /= 10;
}
if (s == t) *t++ = '0';
*t = '\0';
std::reverse(s, t);
return;
}
template <class T>
inline void uitoa(T val, char *s) {
char *t;
T mod;
t = s;
while (val) {
mod = val % 10;
*t++ = static_cast<char>(mod) + '0';
val /= 10;
}
if (s == t) *t++ = '0';
*t = '\0';
std::reverse(s, t);
return;
}
#define _ITOA(_n) do { \
char buf[64]; \
itoa(_n, buf); \
append(buf); \
return *this; } while (0)
#define _UITOA(_n) do { \
char buf[64]; \
uitoa(_n, buf); \
append(buf); \
return *this; } while (0)
#define _DTOA(_n) do { \
char buf[64]; \
dtoa(_n, buf); \
append(buf); \
return *this; } while (0)
class string_buffer: public std::string {
public:
string_buffer& operator<<(double _n) { _DTOA(_n); }
string_buffer& operator<<(short int _n) { _ITOA(_n); }
string_buffer& operator<<(int _n) { _ITOA(_n); }
string_buffer& operator<<(long int _n) { _ITOA(_n); }
string_buffer& operator<<(unsigned short int _n) { _UITOA(_n); }
string_buffer& operator<<(unsigned int _n) { _UITOA(_n); }
string_buffer& operator<<(unsigned long int _n) { _UITOA(_n); }
string_buffer& operator<<(char _n) {
push_back(_n);
return *this;
}
string_buffer& operator<<(const char* _n) {
append(_n);
return *this;
}
string_buffer& operator<<(const std::string& _n) {
append(_n);
return *this;
}
};
class die {
public:
die() {}
virtual ~die() {
std::cerr << std::endl;
exit(-1);
}
int operator&(std::ostream&) { return 0; }
};
class warn {
public:
warn() {}
virtual ~warn() { std::cerr << std::endl; }
int operator&(std::ostream&) { return 0; }
};
struct whatlog {
std::ostringstream stream_;
std::string str_;
const char* str() {
str_ = stream_.str();
return str_.c_str();
}
jmp_buf cond_;
};
class wlog {
public:
whatlog *l_;
explicit wlog(whatlog *l): l_(l) { l_->stream_.clear(); }
~wlog() { longjmp(l_->cond_, 1); }
int operator&(std::ostream &) { return 0; }
};
}
#define WHAT what_.stream_
#define CHECK_RETURN(condition, value) \
if (condition) {} else \
if (setjmp(what_.cond_) == 1) { \
return value; \
} else \
wlog(&what_) & what_.stream_ << \
__FILE__ << "(" << __LINE__ << ") [" << #condition << "] "
#define CHECK_0(condition) CHECK_RETURN(condition, 0)
#define CHECK_FALSE(condition) CHECK_RETURN(condition, false)
#define CHECK_CLOSE_FALSE(condition) \
if (condition) {} else \
if (setjmp(what_.cond_) == 1) { \
close(); \
return false; \
} else \
wlog(&what_) & what_.stream_ << \
__FILE__ << "(" << __LINE__ << ") [" << #condition << "] "
#define CHECK_DIE(condition) \
(condition) ? 0 : die() & std::cerr << __FILE__ << \
"(" << __LINE__ << ") [" << #condition << "] "
#define CHECK_WARN(condition) \
(condition) ? 0 : warn() & std::cerr << __FILE__ << \
"(" << __LINE__ << ") [" << #condition << "] "
#endif
|