LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
StdBitset.cpp
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#include "common/LimaCommon.h"
7
8namespace Lima
9{
10std::hash<std::bitset<LC_WIDTH>> LinguisticCode::hash_fn;
11LIMA_COMMON_EXPORT const char LinguisticCode::alphabet[] = "0123456789ABCDEF";
12Q_GLOBAL_STATIC_WITH_ARGS(std::string, odds, ("13579"));
14
16{
17 std::string s;
18 LinguisticCode x = *this;
19 while (x.base.any())
20 {
21 unsigned char r = 0;
23 size_t i = LC_WIDTH - 1;
24 while (i > 0 && !x.base.test(i))
25 {
26 i--;
27 }
28 while (true)
29 {
30 r <<= 1;
31 r |= x.base.test(i) ? 1 : 0;
32 if (r >= 10)
33 {
34 t.base[i] = true;
35 r -= 10;
36 }
37 if (0 == i)
38 {
39 break;
40 }
41 i--;
42 }
43 s += alphabet[r];
44 x = t;
45 }
46 if (s.size() == 0)
47 {
48 return std::string("0");
49 }
50 reverse(s.begin(), s.end());
51 return s;
52}
53
55{
56 std::string num = str;
58 size_t pos = 0;
59 while (num.size() > 0 && num != std::string("0"))
60 {
61 char last = num[num.size() - 1];
62 if (std::string::npos != odds->find(last))
63 {
64 x.base[pos] = true;
65 }
66
67 std::string new_num;
68 char add = 0;
69 for (size_t i = 0; i < num.size(); i++)
70 {
71 char ch = (num[i] - '0') / 2 + add;
72 new_num += (ch + '0');
73 if (std::string::npos != odds->find(num[i]))
74 {
75 add = 5;
76 }
77 else
78 {
79 add = 0;
80 }
81 }
82
83 size_t first_non_zero = 0;
84 while (first_non_zero < new_num.size() - 1 && new_num[first_non_zero] == '0')
85 {
86 first_non_zero++;
87 }
88
89 if (first_non_zero > 0 && first_non_zero <= new_num.size() - 1)
90 {
91 new_num = new_num.substr(first_non_zero);
92 }
93
94 num = new_num;
95
96 pos++;
97 if (pos >= LC_WIDTH && num.size() > 0 && num != std::string("0"))
98 {
99 throw std::overflow_error(std::string("LinguisticCode::fromDecString: input string is too long: ") + str);
100 }
101 }
102
103 return x;
104}
105
106LIMA_COMMON_EXPORT QDebug& operator<<(QDebug& os, const LinguisticCode& value)
107{
108 std::stringstream ss;
109 ss << value.toHexString();
110 return os << ss.str();
111}
112
113LIMA_COMMON_EXPORT QTextStream& operator<<(QTextStream& os, const LinguisticCode& value)
114{
115 std::stringstream ss;
116 ss << value.toHexString();
117 return os << ss.str().c_str();
118}
119
120}
#define LIMA_COMMON_EXPORT
#define LC_WIDTH
Definition StdBitset.h:21
std::string toHexString() const
Definition StdBitset.h:135
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
LIMA_COMMON_EXPORT std::string toDecString() const
Definition StdBitset.cpp:15
NAUTITIA.
std::ostream & operator<<(ostream &os, const Node &node)
Q_GLOBAL_STATIC_WITH_ARGS(std::string, odds,("13579"))
PUGI__FN void reverse(I begin, I end)
Definition pugixml.cpp:7457