6#ifndef DEEPLIMA_SRC_STATIC_GRAPH_DICT_H
7#define DEEPLIMA_SRC_STATIC_GRAPH_DICT_H
10#include <unordered_map>
21 #define DICT_EXPORT __declspec(dllexport)
23 #define DICT_EXPORT __declspec(dllimport)
35 typedef typename std::vector<T>::size_type
key_t;
37 Dict(
const std::vector<T>& v1,
const std::vector<T>& v2 = {})
39 i2v.reserve(v1.size() + v2.size());
52 Dict(
const T& UNK,
const std::vector<T>& v1,
const std::vector<T>& v2 = {})
54 i2v.reserve(v1.size() + v2.size() + 1);
68 template <
class InputIt>
69 explicit Dict(InputIt begin, InputIt end)
70 :
Dict(begin, end, [](uint64_t ){
return true; })
73 template<
class InputIt,
typename F>
74 Dict(
const T& UNK, InputIt begin, InputIt end, F f)
77 for (
auto it = begin; it != end; ++it)
87 for (
auto it = begin; it != end; ++it)
96 template<
class InputIt,
typename F>
97 Dict(
const T& UNK,
const T& EOS, InputIt begin, InputIt end, F f)
100 for (
auto it = begin; it != end; ++it)
111 for (
auto it = begin; it != end; ++it)
120 template<
class InputIt,
typename F>
121 Dict(
const T& UNK,
const T& EOS,
const T& OTHER, InputIt begin, InputIt end, F f)
124 for (
auto it = begin; it != end; ++it)
136 for (
auto it = begin; it != end; ++it)
145 template<
class InputIt,
typename F>
146 Dict(InputIt begin, InputIt end, F f)
149 for (
auto it = begin; it != end; ++it)
158 for (
auto it = begin; it != end; ++it)
171 if (idx >= i2v.size())
173 throw std::range_error(
"Index too big in Dict::get_value.");
180 typename std::unordered_map<T, key_t>::const_iterator i = v2i.find(val);
200 std::stringstream ss;
202 for (
size_t i = 0; i < i2v.size(); i++)
204 ss << i2v[i] << std::endl;
210 const std::unordered_map<T, key_t>&
get_v2i()
const
223 char32_t v[2] = { ch, 0 };
224 return std::u32string(v);
227 static inline void from_wstring(
const std::wstring& src, std::wstring& k)
234 if (src.length() < 1)
236 throw std::runtime_error(
"Dict::from_wstring empty input.");
253 virtual c10::IValue toIValue()
const;
254 virtual void fromIValue(
const c10::IValue& v);
257 inline void add(
const T& val)
260 if (v2i.cend() != v2i.find(val))
262 throw std::logic_error(
"Elements must be unique");
264 v2i[val] = i2v.size() - 1;
267 std::unordered_map<T, key_t>
v2i;
const T & get_value(key_t idx) const
virtual uint64_t size() const
virtual const std::string & get_class_id() const
Dict(const T &UNK, const std::vector< T > &v1, const std::vector< T > &v2={})
Dict(const std::vector< T > &v1, const std::vector< T > &v2={})
std::vector< T >::size_type key_t
static std::wstring to_wstring(const std::wstring &s)
const std::unordered_map< T, key_t > & get_v2i() const
static void from_wstring(const std::wstring &src, wchar_t &k)
Dict(const T &UNK, InputIt begin, InputIt end, F f)
Dict(InputIt begin, InputIt end, F f)
static std::u32string to_wstring(char32_t ch)
Dict(const T &UNK, const T &EOS, InputIt begin, InputIt end, F f)
static const std::string & class_id()
Dict(InputIt begin, InputIt end)
Dict(const T &UNK, const T &EOS, const T &OTHER, InputIt begin, InputIt end, F f)
static const std::string m_class_id
static void from_wstring(const std::wstring &src, std::wstring &k)
key_t get_idx(const T &val) const
std::unordered_map< T, key_t > v2i
Dict< char32_t > Char32Dict
Dict< uint64_t > UInt64Dict
Dict< std::wstring > WstringDict
Dict< std::string > StringDict