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