LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
StdBitset.h
Go to the documentation of this file.
1// Copyright 2002-2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef LIMA_COMMON_STDBITSET
7#define LIMA_COMMON_STDBITSET
8
9#include <bitset>
10#include <deque>
11#include <ostream>
12#include <istream>
13
14#ifdef WIN32
15#include <cctype>
16#endif
17
18namespace Lima
19{
20
21// Bits of a LinguisticCode. The UD property codes (ud/code-ud.xml) of the
22// UD 2.18 features need about 225 bits. Codes are stored as decimal strings or
23// with a variable length binary encoding, so resources compiled with a smaller
24// width remain readable.
25#define LC_WIDTH 256
26class LinguisticCode
27{
28 std::bitset<LC_WIDTH> base;
29
30 static std::hash<std::bitset<LC_WIDTH>> hash_fn;
31 LIMA_COMMON_EXPORT static const char alphabet[];
32
33 template <size_t N> // N - bits per character
34 inline std::string toString_() const
35 {
36 char chars[LC_WIDTH / N + 2];
37 char *p = chars + (LC_WIDTH / N + 1);
38 *p = 0;
39 LinguisticCode n = *this;
40
41 do
42 {
43 p--;
44 unsigned char c = n.lsb() & ((1 << N) - 1);
45 *p = alphabet[c];
46 n >>= N;
47 } while (n.toBool());
48
49 return std::string(p);
50 }
51
52 inline std::size_t hash() const
53 {
54 return hash_fn(base);
55 }
56
57 LinguisticCode(uint64_t val)
58 : base((unsigned long long)(val))
59 {
60 }
61
62public:
63
64 LinguisticCode() noexcept
65 : base(0)
66 {
67 }
68
69 inline static LinguisticCode fromUInt(uint64_t v)
70 {
71 return LinguisticCode(v);
72 }
73
74 inline static LinguisticCode fromOctString(const std::string& str)
75 {
76 const char *p = str.c_str();
77 const char *e = p + str.size();
78 LinguisticCode lc;
79
80 while (p != e)
81 {
82 char c = *p - 0x30;
83 lc <<= 3;
84 lc |= c;
85 p++;
86 }
87
88 return lc;
89 }
90
91 LIMA_COMMON_EXPORT static LinguisticCode fromDecString(const std::string& str);
92
93 inline static LinguisticCode fromString(const std::string& str)
94 {
96 }
97
98 static std::size_t size() noexcept
99 {
100 return LC_WIDTH;
101 }
102
103 inline bool toBool() const noexcept
104 {
105 return base.any();
106 }
107
108 inline bool operator!() const noexcept
109 {
110 return ! toBool();
111 }
112
113 inline LinguisticCode operator<<(std::size_t pos) const noexcept
114 {
115 LinguisticCode x = *this;
116 x <<= pos;
117 return x;
118 }
119
120 inline LinguisticCode operator<<=(std::size_t pos) noexcept
121 {
122 base <<= pos;
123 return *this;
124 }
125
126 inline LinguisticCode operator>>(std::size_t pos) const noexcept
127 {
128 LinguisticCode x = *this;
129 x >>= pos;
130 return x;
131 }
132
133 inline LinguisticCode operator>>=(std::size_t pos) noexcept
134 {
135 base >>= pos;
136 return *this;
137 }
138
139 inline std::string toHexString() const
140 {
141 return toString_<4>();
142 }
143
144 inline std::string toOctString() const
145 {
146 return toString_<3>();
147 }
148
149 inline std::string toBinString() const
150 {
151 return toString_<1>();
152 }
153
154 LIMA_COMMON_EXPORT std::string toDecString() const;
155
156 inline std::string toString() const
157 {
158 return toDecString();
159 }
160
161 inline unsigned char lsb()
162 {
163 unsigned char c = 0;
164 std::size_t p = 0;
165 while (p < 8)
166 {
167 if (base.test(p))
168 {
169 c |= ( 0x1 << p );
170 }
171 p++;
172 }
173
174 return c;
175 }
176
177 inline static std::ostream& encodeToBinary(std::ostream& os, const LinguisticCode& value)
178 {
179 std::deque<char> chars;
180 LinguisticCode n = value;
181
182 do
183 {
184 unsigned c = (n.lsb() & 0x7F) << 1;
185 chars.push_back(c);
186 n >>= 7;
187 } while (n.toBool());
188
189 for (std::deque<char>::size_type i=chars.size()-1;i>0;i--)
190 {
191 chars[i] |= 0x1;
192 os.put(chars[i]);
193 os.flush();
194 }
195 os.put(chars[0]);
196 os.flush();
197
198 return os;
199 }
200
201 inline static LinguisticCode decodeFromBinary(std::istream& is)
202 {
203 char c = 0x1;
204 LinguisticCode tmp(0);
205
206 while((c & 0x1) && is.good())
207 {
208 is.read((char*)&c, sizeof(char));
209 tmp <<= 7;
210 tmp |= LinguisticCode((c >> 1) & 0x7F);
211 }
212
213 return tmp;
214 }
215
216 inline static LinguisticCode decodeFromBinary(unsigned char* &p)
217 {
218 char c = 0x1;
219 LinguisticCode tmp(0);
220
221 while(c & 0x1)
222 {
223 c = *p; p++;
224 tmp <<= 7;
225 tmp |= LinguisticCode((c >> 1) & 0x7F);
226 }
227
228 return tmp;
229 }
230
231 inline LinguisticCode operator~() const noexcept
232 {
233 LinguisticCode x = *this;
234 x.base.flip();
235 return x;
236 }
237
238 inline LinguisticCode operator&=(const LinguisticCode& other) noexcept
239 {
240 base &= other.base;
241 return *this;
242 }
243
244 inline LinguisticCode operator|=(const LinguisticCode& other) noexcept
245 {
246 base |= other.base;
247 return *this;
248 }
249
250 inline LinguisticCode operator^=(const LinguisticCode& other) noexcept
251 {
252 base ^= other.base;
253 return *this;
254 }
255
256 inline bool operator==(const LinguisticCode& other) const noexcept
257 {
258 return base == other.base;
259 }
260
261 inline bool operator!=(const LinguisticCode& other) const noexcept
262 {
263 return base != other.base;
264 }
265
266 friend inline LinguisticCode operator&(const LinguisticCode& lhs, const LinguisticCode& rhs) noexcept;
267 friend inline LinguisticCode operator|(const LinguisticCode& lhs, const LinguisticCode& rhs) noexcept;
268 friend inline LinguisticCode operator^(const LinguisticCode& lhs, const LinguisticCode& rhs) noexcept;
269 friend inline bool operator<(const LinguisticCode& lhs, const LinguisticCode& rhs);
270 friend inline bool operator>(const LinguisticCode& lhs, const LinguisticCode& rhs);
271
272 LIMA_COMMON_EXPORT static const LinguisticCode ZERO;
273};
274
275inline LinguisticCode operator&(const LinguisticCode& lhs, const LinguisticCode& rhs) noexcept
276{
277 LinguisticCode x = lhs;
278 return x &= rhs;
279}
280
281inline LinguisticCode operator|(const LinguisticCode& lhs, const LinguisticCode& rhs) noexcept
282{
283 LinguisticCode x = lhs;
284 return x |= rhs;
285}
286
287inline LinguisticCode operator^(const LinguisticCode& lhs, const LinguisticCode& rhs) noexcept
288{
289 LinguisticCode x = lhs;
290 return x ^= rhs;
291}
292
293inline bool operator<(const LinguisticCode& lhs, const LinguisticCode& rhs)
294{
295 return lhs.hash() < rhs.hash();
296}
297
298inline bool operator>(const LinguisticCode& lhs, const LinguisticCode& rhs)
299{
300 return lhs.hash() > rhs.hash();
301}
302
303inline std::ostream& operator<<(std::ostream& os, const LinguisticCode& value)
304{
305 os << value.toString();
306 return os;
307}
308
309inline std::istream& operator>>(std::istream& is, LinguisticCode& value)
310{
311 std::string tmp;
312 char c = 0;
313
314 while (is.get(c))
315 {
316 if (!std::isspace(c))
317 {
318 is.unget();
319 break;
320 }
321 }
322
323 while (is.get(c))
324 {
325 if (!std::isdigit(c))
326 {
327 is.unget();
328 break;
329 }
330 tmp.push_back(c);
331 }
332
333 value = LinguisticCode::fromString(tmp);
334 return is;
335}
336
337LIMA_COMMON_EXPORT QDebug& operator<<(QDebug& os, const LinguisticCode& value);
338LIMA_COMMON_EXPORT QTextStream& operator<<(QTextStream& os, const LinguisticCode& value);
339
340}
341
342#define L_NONE Lima::LinguisticCode::ZERO
343#define NONE_1 Lima::LinguisticCode::ZERO
344
345#endif
#define LIMA_COMMON_EXPORT
#define LC_WIDTH
Definition StdBitset.h:25
std::string toString() const
Definition StdBitset.h:156
LinguisticCode operator>>=(std::size_t pos) noexcept
Definition StdBitset.h:133
bool operator!=(const LinguisticCode &other) const noexcept
Definition StdBitset.h:261
bool toBool() const noexcept
Definition StdBitset.h:103
friend LinguisticCode operator^(const LinguisticCode &lhs, const LinguisticCode &rhs) noexcept
Definition StdBitset.h:287
unsigned char lsb()
Definition StdBitset.h:161
LinguisticCode operator<<=(std::size_t pos) noexcept
Definition StdBitset.h:120
static LinguisticCode fromString(const std::string &str)
Definition StdBitset.h:93
std::string toHexString() const
Definition StdBitset.h:139
static LIMA_COMMON_EXPORT LinguisticCode fromDecString(const std::string &str)
Definition StdBitset.cpp:54
static LIMA_COMMON_EXPORT const LinguisticCode ZERO
Definition StdBitset.h:272
friend bool operator>(const LinguisticCode &lhs, const LinguisticCode &rhs)
Definition StdBitset.h:298
LinguisticCode operator<<(std::size_t pos) const noexcept
Definition StdBitset.h:113
LinguisticCode operator~() const noexcept
Definition StdBitset.h:231
friend LinguisticCode operator&(const LinguisticCode &lhs, const LinguisticCode &rhs) noexcept
Definition StdBitset.h:275
std::string toOctString() const
Definition StdBitset.h:144
static std::size_t size() noexcept
Definition StdBitset.h:98
static LinguisticCode fromUInt(uint64_t v)
Definition StdBitset.h:69
static LinguisticCode fromOctString(const std::string &str)
Definition StdBitset.h:74
static std::ostream & encodeToBinary(std::ostream &os, const LinguisticCode &value)
Definition StdBitset.h:177
LIMA_COMMON_EXPORT std::string toDecString() const
Definition StdBitset.cpp:15
friend LinguisticCode operator|(const LinguisticCode &lhs, const LinguisticCode &rhs) noexcept
Definition StdBitset.h:281
LinguisticCode() noexcept
Definition StdBitset.h:64
friend bool operator<(const LinguisticCode &lhs, const LinguisticCode &rhs)
Definition StdBitset.h:293
static LinguisticCode decodeFromBinary(std::istream &is)
Definition StdBitset.h:201
LinguisticCode operator^=(const LinguisticCode &other) noexcept
Definition StdBitset.h:250
LinguisticCode operator&=(const LinguisticCode &other) noexcept
Definition StdBitset.h:238
bool operator==(const LinguisticCode &other) const noexcept
Definition StdBitset.h:256
LinguisticCode operator>>(std::size_t pos) const noexcept
Definition StdBitset.h:126
LinguisticCode operator|=(const LinguisticCode &other) noexcept
Definition StdBitset.h:244
std::string toBinString() const
Definition StdBitset.h:149
bool operator!() const noexcept
Definition StdBitset.h:108
static LinguisticCode decodeFromBinary(unsigned char *&p)
Definition StdBitset.h:216
NAUTITIA.
LinguisticCode operator|(const LinguisticCode &lhs, const LinguisticCode &rhs) noexcept
Definition StdBitset.h:281
bool operator<(const LinguisticCode &lhs, const LinguisticCode &rhs)
Definition StdBitset.h:293
std::ostream & operator<<(ostream &os, const Node &node)
bool operator>(const LinguisticCode &lhs, const LinguisticCode &rhs)
Definition StdBitset.h:298
LinguisticCode operator&(const LinguisticCode &lhs, const LinguisticCode &rhs) noexcept
Definition StdBitset.h:275
LIMA_COMMON_EXPORT std::istream & operator>>(std::istream &is, uint128_t &value)
LinguisticCode operator^(const LinguisticCode &lhs, const LinguisticCode &rhs) noexcept
Definition StdBitset.h:287