@@ -20,13 +20,82 @@ limitations under the License. */
2020#include < string>
2121#include < unordered_map>
2222#include < vector>
23+ #include " paddle/phi/core/extended_tensor.h"
2324
2425namespace paddle {
2526namespace framework {
2627
28+ class Vocab : public phi ::ExtendedTensor,
29+ public phi::TypeInfoTraits<phi::TensorBase, Vocab> {
30+ public:
31+ Vocab () = default ;
32+
33+ Vocab (Vocab&& other) = default ;
34+
35+ Vocab (const Vocab& other) = default ;
36+
37+ Vocab& operator =(const Vocab& other) = default ;
38+
39+ Vocab& operator =(Vocab&& other) = default ;
40+
41+ Vocab& operator =(
42+ const std::unordered_map<std::wstring, std::int32_t >& other) {
43+ this ->data_ = other;
44+ return *this ;
45+ }
46+
47+ // / \brief Destroy the Vocab and release exclusive resources.
48+ virtual ~Vocab () = default ;
49+
50+ public:
51+ // / \brief Returns the name of the class for type traits.
52+ // / \return The name of the class.
53+ static const char * name () { return " Vocab" ; }
54+
55+ size_t size () const { return data_.size (); }
56+
57+ void clear () { data_.clear (); }
58+
59+ void emplace (const std::wstring& key, std::int32_t value) {
60+ data_.emplace (key, value);
61+ }
62+
63+ std::int32_t at (const std::wstring& key) { return data_.at (key); }
64+
65+ std::int32_t at (const std::wstring& key) const { return data_.at (key); }
66+
67+ std::unordered_map<std::wstring, std::int32_t >::iterator find (
68+ const std::wstring& key) {
69+ return data_.find (key);
70+ }
71+
72+ std::unordered_map<std::wstring, std::int32_t >::const_iterator find (
73+ const std::wstring& key) const {
74+ return data_.find (key);
75+ }
76+
77+ std::unordered_map<std::wstring, std::int32_t >::iterator begin () {
78+ return data_.begin ();
79+ }
80+
81+ std::unordered_map<std::wstring, std::int32_t >::const_iterator begin () const {
82+ return data_.begin ();
83+ }
84+
85+ std::unordered_map<std::wstring, std::int32_t >::iterator end () {
86+ return data_.end ();
87+ }
88+
89+ std::unordered_map<std::wstring, std::int32_t >::const_iterator end () const {
90+ return data_.end ();
91+ }
92+
93+ private:
94+ std::unordered_map<std::wstring, std::int32_t > data_;
95+ };
96+
2797using String = std::string;
2898using Strings = std::vector<std::string>;
29- using Vocab = std::unordered_map<std::wstring, std::int32_t >;
3099
31100// Convert the std::string type to the std::string type.
32101bool ConvertStrToWstr (const std::string& src, std::wstring* res);
0 commit comments