Skip to content

Commit 012bb0c

Browse files
r-barnesfacebook-github-bot
authored andcommitted
c10::string_view -> std::string_view in
Summary: Public message about pytorch/text/torchtext/csrc/vocab.h here Reviewed By: ezyang Differential Revision: D65543740 fbshipit-source-id: a504a36197b3f4b6cbf5840313bfcc2011347a5c
1 parent 45fa27d commit 012bb0c

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

torchtext/csrc/vocab.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Vocab::Vocab(StringList tokens, const std::optional<int64_t>& default_index)
1212
: stoi_(MAX_VOCAB_SIZE, -1), default_index_{default_index} {
1313
for (auto& token : tokens) {
1414
// throw error if duplicate token is found
15-
auto id = _find(c10::string_view{token});
15+
auto id = _find(std::string_view{token});
1616
TORCH_CHECK(
1717
stoi_[id] == -1, "Duplicate token found in tokens list: " + token);
1818

@@ -26,15 +26,15 @@ int64_t Vocab::__len__() const {
2626
return itos_.size();
2727
}
2828

29-
bool Vocab::__contains__(const c10::string_view& token) const {
29+
bool Vocab::__contains__(const std::string_view& token) const {
3030
int64_t id = _find(token);
3131
if (stoi_[id] != -1) {
3232
return true;
3333
}
3434
return false;
3535
}
3636

37-
int64_t Vocab::__getitem__(const c10::string_view& token) const {
37+
int64_t Vocab::__getitem__(const std::string_view& token) const {
3838
int64_t id = _find(token);
3939
if (stoi_[id] != -1)
4040
return stoi_[id];
@@ -59,7 +59,7 @@ std::optional<int64_t> Vocab::get_default_index() const {
5959

6060
void Vocab::append_token(std::string token) {
6161
// throw error if token already exist in vocab
62-
auto id = _find(c10::string_view{token});
62+
auto id = _find(std::string_view{token});
6363
TORCH_CHECK(
6464
stoi_[id] == -1,
6565
"Token " + token + " already exists in the Vocab with index: " +
@@ -80,10 +80,10 @@ void Vocab::insert_token(std::string token, const int64_t& index) {
8080

8181
// need to offset all tokens greater than or equal index by 1
8282
for (size_t i = index; i < __len__(); i++) {
83-
stoi_[_find(c10::string_view{itos_[i]})] = i + 1;
83+
stoi_[_find(std::string_view{itos_[i]})] = i + 1;
8484
}
8585

86-
stoi_[_find(c10::string_view{token})] = index;
86+
stoi_[_find(std::string_view{token})] = index;
8787
itos_.insert(itos_.begin() + index, std::move(token));
8888
}
8989

@@ -115,7 +115,7 @@ StringList Vocab::lookup_tokens(const std::vector<int64_t>& indices) {
115115
}
116116

117117
std::vector<int64_t> Vocab::lookup_indices(
118-
const std::vector<c10::string_view>& tokens) {
118+
const std::vector<std::string_view>& tokens) {
119119
std::vector<int64_t> indices(tokens.size());
120120
for (size_t i = 0; i < tokens.size(); i++) {
121121
indices[i] = __getitem__(tokens[i]);
@@ -127,7 +127,7 @@ std::unordered_map<std::string, int64_t> Vocab::get_stoi() const {
127127
std::unordered_map<std::string, int64_t> stoi;
128128
// construct tokens and index list
129129
for (const auto& item : itos_) {
130-
stoi[item] = __getitem__(c10::string_view{item});
130+
stoi[item] = __getitem__(std::string_view{item});
131131
}
132132
return stoi;
133133
}

torchtext/csrc/vocab.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ struct Vocab : torch::CustomClassHolder {
4646
StringList tokens,
4747
const std::optional<int64_t>& default_index);
4848
TORCHTEXT_API int64_t __len__() const;
49-
TORCHTEXT_API int64_t __getitem__(const c10::string_view& token) const;
50-
TORCHTEXT_API bool __contains__(const c10::string_view& token) const;
49+
TORCHTEXT_API int64_t __getitem__(const std::string_view& token) const;
50+
TORCHTEXT_API bool __contains__(const std::string_view& token) const;
5151
TORCHTEXT_API void set_default_index(std::optional<int64_t> index);
5252
TORCHTEXT_API std::optional<int64_t> get_default_index() const;
5353
TORCHTEXT_API void insert_token(std::string token, const int64_t& index);
@@ -56,12 +56,12 @@ struct Vocab : torch::CustomClassHolder {
5656
TORCHTEXT_API std::vector<std::string> lookup_tokens(
5757
const std::vector<int64_t>& indices);
5858
std::vector<int64_t> lookup_indices(
59-
const std::vector<c10::string_view>& tokens);
59+
const std::vector<std::string_view>& tokens);
6060
TORCHTEXT_API std::unordered_map<std::string, int64_t> get_stoi() const;
6161
TORCHTEXT_API std::vector<std::string> get_itos() const;
6262

6363
protected:
64-
uint32_t _hash(const c10::string_view& str) const {
64+
uint32_t _hash(const std::string_view& str) const {
6565
uint32_t h = 2166136261;
6666
for (size_t i = 0; i < str.size(); i++) {
6767
h = h ^ uint32_t(uint8_t(str[i]));
@@ -70,7 +70,7 @@ struct Vocab : torch::CustomClassHolder {
7070
return h;
7171
}
7272

73-
uint32_t _find(const c10::string_view& w) const {
73+
uint32_t _find(const std::string_view& w) const {
7474
uint32_t stoi_size = stoi_.size();
7575
uint32_t id = _hash(w) % stoi_size;
7676
while (stoi_[id] != -1 && itos_[stoi_[id]] != w) {
@@ -80,7 +80,7 @@ struct Vocab : torch::CustomClassHolder {
8080
}
8181

8282
void _add(std::string w) {
83-
uint32_t h = _find(c10::string_view{w.data(), w.size()});
83+
uint32_t h = _find(std::string_view{w.data(), w.size()});
8484
if (stoi_[h] == -1) {
8585
itos_.emplace_back(std::move(w));
8686
stoi_[h] = itos_.size() - 1;

0 commit comments

Comments
 (0)