LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
str_index.h
Go to the documentation of this file.
1// Copyright 2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef DEEPLIMA_UTILS_STR_INDEX
7#define DEEPLIMA_UTILS_STR_INDEX
8
9#include <unordered_map>
10#include <codecvt>
11#include <locale>
12
13namespace deeplima
14{
15
16template <typename S=std::string, typename U=std::u32string, typename I=uint32_t>
18{
19public:
20 typedef S string_t;
21 typedef U ustring_t;
22 typedef I idx_t;
23
24 inline idx_t get_idx(const char* p, size_t len)
25 {
26 std::string s = std::string(p, len);
27 return get_idx(s);
28 }
29
30 inline idx_t get_idx(const string_t& str)
31 {
32 assert(m_str2idx.size() == m_idx2str.size());
33
34 auto it = m_str2idx.find(str);
35 if (m_str2idx.end() == it)
36 {
37 uint32_t idx = m_str2idx.size();
38 m_idx2str[idx] = str;
39 m_str2idx[str] = idx;
40
41 assert(m_str2idx.size() == m_idx2str.size());
42
43 return idx;
44 }
45 else
46 {
47 assert(m_str2idx.size() == m_idx2str.size());
48 return it->second;
49 }
50 }
51
52 inline idx_t get_idx(const ustring_t& ustr)
53 {
54 assert(m_str2idx.size() == m_idx2str.size());
55 string_t str = m_cvt.to_bytes(ustr);
56 return get_idx(str);
57 }
58
59 inline const S& get_str(const idx_t idx) const
60 {
61 assert(m_str2idx.size() == m_idx2str.size());
62
63 auto it = m_idx2str.find(idx);
64 if (m_idx2str.end() == it)
65 {
66 throw std::runtime_error("Unknown index in StringIndex.");
67 }
68 else
69 {
70 return it->second;
71 }
72 }
73
76 inline const U& get_ustr(const idx_t idx)
77 {
78 auto it = m_idx2ustr.find(idx);
79 if (m_idx2ustr.end() != it)
80 {
81 return it->second;
82 }
83
84 const S& str = get_str(idx);
85 ustring_t ustr = m_cvt.from_bytes(str);
86 it = m_idx2ustr.insert({idx, ustr}).first;
87 if (m_idx2ustr.end() != it)
88 {
89 return it->second;
90 }
91
92 throw std::runtime_error("StringIndex: can't find recently inserted element.");
93 }
94
95 inline const U& get_ustr(const idx_t idx) const
96 {
97 auto it = m_idx2ustr.find(idx);
98 if (m_idx2ustr.end() != it)
99 {
100 return it->second;
101 }
102
103 throw std::runtime_error("StringIndex: can't find an element by index.");
104 }
105
106 size_t size() const
107 {
108 return m_idx2str.size();
109 }
110
111protected:
112 std::unordered_map<string_t, idx_t> m_str2idx;
113 std::unordered_map<idx_t, string_t> m_idx2str;
114 mutable std::unordered_map<idx_t, ustring_t> m_idx2ustr;
115
116 std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> m_cvt;
117};
118
120
121}
122
123#endif
const S & get_str(const idx_t idx) const
Definition str_index.h:59
std::unordered_map< idx_t, ustring_t > m_idx2ustr
Definition str_index.h:114
idx_t get_idx(const ustring_t &ustr)
Definition str_index.h:52
idx_t get_idx(const char *p, size_t len)
Definition str_index.h:24
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > m_cvt
Definition str_index.h:116
std::unordered_map< idx_t, string_t > m_idx2str
Definition str_index.h:113
const U & get_ustr(const idx_t idx) const
Definition str_index.h:95
std::unordered_map< string_t, idx_t > m_str2idx
Definition str_index.h:112
idx_t get_idx(const string_t &str)
Definition str_index.h:30
const U & get_ustr(const idx_t idx)
accessor with side effect.
Definition str_index.h:76
size_t size() const
Definition str_index.h:106
TStringIndex< std::string, std::u32string, uint32_t > StringIndex
Definition str_index.h:119