Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbc3272
commit a8b0ace
Showing
12 changed files
with
199 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (C) 2021 Apertium | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <lttoolbox/alphabet_exe.h> | ||
|
||
#include <lttoolbox/compression.h> | ||
|
||
AlphabetExe::AlphabetExe(StringWriter* sw_) | ||
: sw(sw_), tag_count(0), tags(nullptr) | ||
{} | ||
|
||
AlphabetExe::~AlphabetExe() | ||
{ | ||
delete[] tags; | ||
} | ||
|
||
void | ||
AlphabetExe::read(FILE* input, bool mmap) | ||
{ | ||
if (mmap) { | ||
} else { | ||
tag_count = Compression::multibyte_read(input); | ||
tags = new StringRef[tag_count]; | ||
for (uint32_t i = 0; i < tag_count; i++) { | ||
UString tg; | ||
tg += '<'; | ||
tg += Compression::string_read(input); | ||
tg += '>'; | ||
tags[i] = sw->add(tg); | ||
} | ||
// has to be a separate loop, otherwise the string_views get | ||
// invalidated when the StringWriter buffer expands | ||
for (uint32_t i = 0; i < tag_count; i++) { | ||
symbol_map[sw->get(tags[i])] = -static_cast<int32_t>(i) - 1; | ||
} | ||
int pairs = Compression::multibyte_read(input); | ||
for (int i = 0; i < pairs; i++) { | ||
Compression::multibyte_read(input); | ||
Compression::multibyte_read(input); | ||
} | ||
} | ||
} | ||
|
||
int32_t | ||
AlphabetExe::operator()(UString_view sv) | ||
{ | ||
auto it = symbol_map.find(sv); | ||
if (it != symbol_map.end()) { | ||
return it->second; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
void | ||
AlphabetExe::getSymbol(UString& result, int32_t symbol, bool uppercase) const | ||
{ | ||
if (symbol == 0) { | ||
return; | ||
} else if (symbol < 0) { | ||
result.append(sw->get(tags[-symbol-1])); | ||
} else if (uppercase) { | ||
result += u_toupper(static_cast<UChar32>(symbol)); | ||
} else { | ||
result += static_cast<UChar32>(symbol); | ||
} | ||
} | ||
|
||
bool | ||
AlphabetExe::isTag(const int32_t symbol) const | ||
{ | ||
return symbol < 0; | ||
} | ||
|
||
void | ||
AlphabetExe::clearSymbol(const int32_t symbol) | ||
{ | ||
if (symbol < 0) { | ||
tags[-symbol-1].start = 0; | ||
tags[-symbol-1].count = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (C) 2021 Apertium | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef _LT_ALPHABET_EXE_ | ||
#define _LT_ALPHABET_EXE_ | ||
|
||
#include <lttoolbox/string_writer.h> | ||
#include <map> | ||
|
||
class AlphabetExe { | ||
private: | ||
StringWriter* sw; | ||
uint64_t tag_count; | ||
StringRef* tags; | ||
std::map<UString_view, int32_t> symbol_map; | ||
public: | ||
AlphabetExe(StringWriter* sw_); | ||
~AlphabetExe(); | ||
void read(FILE* in, bool mmap); | ||
int32_t operator()(UString_view sv); | ||
void getSymbol(UString& result, int32_t symbol, bool uppercase = false) const; | ||
bool isTag(const int32_t symbol) const; | ||
void clearSymbol(const int32_t symbol); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.