Skip to content

Commit

Permalink
const vector<T>& -> absl::Span<const T>.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 653510133
  • Loading branch information
tomokinat authored and hiroyuki-komatsu committed Jul 18, 2024
1 parent 8b71c77 commit 49ed94a
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/converter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ mozc_cc_test(
":segments",
":segments_matchers",
"//testing:gunit_main",
"@com_google_absl//absl/types:span",
],
)

Expand Down
4 changes: 2 additions & 2 deletions src/converter/segments_matchers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
#include "converter/segments_matchers.h"

#include <string>
#include <vector>

#include "absl/types/span.h"
#include "converter/segments.h"
#include "testing/gmock.h"
#include "testing/gunit.h"
Expand All @@ -55,7 +55,7 @@ Segment::Candidate MakeCandidate(const std::string &key,
}

Segment MakeSegment(const std::string &key,
const std::vector<std::string> &values) {
absl::Span<const std::string> values) {
Segment seg;
seg.set_key(key);
for (const std::string &val : values) {
Expand Down
1 change: 1 addition & 0 deletions src/dictionary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ mozc_cc_test(
"//testing:mozctest",
"//testing:testing_util",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
)

Expand Down
3 changes: 2 additions & 1 deletion src/dictionary/user_dictionary_session_handler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <vector>

#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/file_util.h"
#include "base/protobuf/repeated_ptr_field.h"
#include "base/system_util.h"
Expand Down Expand Up @@ -154,7 +155,7 @@ class UserDictionarySessionHandlerTest

RepeatedPtrField<UserDictionary::Entry> GetUserDictionaryEntries(
uint64_t session_id, uint64_t dictionary_id,
const std::vector<int> &indices) {
absl::Span<const int> indices) {
Clear();
command_->set_type(UserDictionaryCommand::GET_ENTRIES);
command_->set_session_id(session_id);
Expand Down
2 changes: 2 additions & 0 deletions src/prediction/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ mozc_cc_test(
"//request:request_test_util",
"//testing:gunit_main",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
)

Expand Down Expand Up @@ -501,6 +502,7 @@ mozc_cc_binary(
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/log",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
)

Expand Down
13 changes: 7 additions & 6 deletions src/prediction/gen_suggestion_filter_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "absl/flags/flag.h"
#include "absl/log/log.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/codegen_bytearray_stream.h"
#include "base/file_stream.h"
#include "base/hash.h"
Expand Down Expand Up @@ -91,19 +92,19 @@ void ReadSafeWords(const absl::string_view safe_list_files,
constexpr size_t kMinimumFilterBytes = 100 * 1000;

ExistenceFilterBuilder GetFilter(const size_t num_bytes,
const std::vector<uint64_t> &hash_list) {
absl::Span<const uint64_t> hash_list) {
LOG(INFO) << "num_bytes: " << num_bytes;

ExistenceFilterBuilder filter(
ExistenceFilterBuilder::CreateOptimal(num_bytes, hash_list.size()));
for (size_t i = 0; i < hash_list.size(); ++i) {
filter.Insert(hash_list[i]);
for (uint64_t hash : hash_list) {
filter.Insert(hash);
}
return filter;
}

bool TestFilter(const ExistenceFilterBuilder &builder,
const std::vector<std::string> &safe_word_list) {
absl::Span<const std::string> safe_word_list) {
ExistenceFilter filter = builder.Build();
for (const std::string &word : safe_word_list) {
if (filter.Exists(mozc::Fingerprint(word))) {
Expand All @@ -116,8 +117,8 @@ bool TestFilter(const ExistenceFilterBuilder &builder,
}

ExistenceFilterBuilder SetupFilter(
const size_t num_bytes, const std::vector<uint64_t> &hash_list,
const std::vector<std::string> &safe_word_list) {
const size_t num_bytes, absl::Span<const uint64_t> hash_list,
absl::Span<const std::string> safe_word_list) {
constexpr int kNumRetryMax = 10;
constexpr int kSizeOffset = 8;
// Prevent filtering of common words by false positive.
Expand Down
3 changes: 2 additions & 1 deletion src/prediction/single_kanji_prediction_aggregator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <vector>

#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/strings/unicode.h"
#include "composer/composer.h"
#include "composer/table.h"
Expand All @@ -58,7 +59,7 @@ void SetUpInputWithKey(absl::string_view key, composer::Composer *composer,
seg->set_segment_type(Segment::FREE);
}

bool FindResultByKey(const std::vector<Result> &results,
bool FindResultByKey(absl::Span<const Result> results,
const absl::string_view key) {
for (const auto &result : results) {
if (result.key == key && !result.removed) {
Expand Down
6 changes: 6 additions & 0 deletions src/rewriter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ mozc_cc_library(
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
alwayslink = 1,
)
Expand Down Expand Up @@ -391,6 +392,7 @@ mozc_cc_library(
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
alwayslink = 1,
)
Expand Down Expand Up @@ -1006,6 +1008,7 @@ mozc_cc_library(
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
alwayslink = 1,
)
Expand Down Expand Up @@ -1228,6 +1231,7 @@ mozc_cc_library(
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
)

Expand All @@ -1250,6 +1254,7 @@ mozc_cc_test(
"//testing:mozctest",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
)

Expand Down Expand Up @@ -1293,6 +1298,7 @@ mozc_cc_binary(
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
)

Expand Down
5 changes: 3 additions & 2 deletions src/rewriter/environmental_filter_rewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/container/serialized_string_array.h"
#include "base/text_normalizer.h"
#include "base/util.h"
Expand Down Expand Up @@ -203,7 +204,7 @@ EmojiDataIterator end(const absl::string_view token_array_data) {

absl::flat_hash_map<EmojiVersion, std::vector<std::u32string>>
ExtractTargetEmojis(
const std::vector<EmojiVersion> &target_versions,
absl::Span<const EmojiVersion> target_versions,
const std::pair<EmojiDataIterator, EmojiDataIterator> &range,
const SerializedStringArray &string_array) {
absl::flat_hash_map<EmojiVersion, std::vector<std::u32string>> results;
Expand Down Expand Up @@ -237,7 +238,7 @@ std::u32string SortAndUnique(std::u32string_view codepoints) {
} // namespace

void CharacterGroupFinder::Initialize(
const std::vector<std::u32string> &target_codepoints) {
absl::Span<const std::u32string> target_codepoints) {
std::u32string single_codepoints;
for (const auto &codepoints : target_codepoints) {
const size_t size = codepoints.size();
Expand Down
3 changes: 2 additions & 1 deletion src/rewriter/environmental_filter_rewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include <string_view>
#include <vector>

#include "absl/types/span.h"
#include "base/text_normalizer.h"
#include "converter/segments.h"
#include "data_manager/data_manager_interface.h"
Expand All @@ -70,7 +71,7 @@ class CharacterGroupFinder {
~CharacterGroupFinder() = default;

// Sets target_codepoints, which represents target group.
void Initialize(const std::vector<std::u32string> &target_codepoints);
void Initialize(absl::Span<const std::u32string> target_codepoints);
// Finds targeted character in given target codepoints. If found, returns
// true. If not found, returns false.
bool FindMatch(std::u32string_view target) const;
Expand Down
3 changes: 2 additions & 1 deletion src/rewriter/environmental_filter_rewriter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "absl/container/btree_map.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/container/serialized_string_array.h"
#include "base/text_normalizer.h"
#include "base/util.h"
Expand Down Expand Up @@ -65,7 +66,7 @@ void AddSegment(const absl::string_view key, const absl::string_view value,
}

void AddSegment(const absl::string_view key,
const std::vector<std::string> &values, Segments *segments) {
absl::Span<const std::string> values, Segments *segments) {
Segment *seg = segments->add_segment();
seg->set_key(key);
for (const std::string &value : values) {
Expand Down
3 changes: 2 additions & 1 deletion src/rewriter/gen_usage_rewriter_dictionary_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
#include "absl/strings/str_replace.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/container/serialized_string_array.h"
#include "base/file_stream.h"
#include "base/init_mozc.h"
Expand Down Expand Up @@ -326,7 +327,7 @@ void Convert() {
std::ios_base::out | std::ios_base::binary);
int out_count = 0;
for (size_t i = 0; i < conjugation_list.size(); ++i) {
const std::vector<ConjugationType> &conjugations =
absl::Span<const ConjugationType> conjugations =
inflection_map[conjugation_list[i]];
conjugation_index[i] = out_count;
if (conjugations.empty()) {
Expand Down
3 changes: 2 additions & 1 deletion src/rewriter/single_kanji_rewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/strings/assign.h"
#include "base/vlog.h"
#include "converter/segments.h"
Expand Down Expand Up @@ -200,7 +201,7 @@ void SingleKanjiRewriter::AddDescriptionForExistingCandidates(
// Insert SingleKanji into segment.
bool SingleKanjiRewriter::InsertCandidate(
bool is_single_segment, uint16_t single_kanji_id,
const std::vector<std::string> &kanji_list, Segment *segment) const {
absl::Span<const std::string> kanji_list, Segment *segment) const {
DCHECK(segment);
DCHECK(!kanji_list.empty());
if (segment->candidates_size() == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/rewriter/single_kanji_rewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
#include <cstdint>
#include <memory>
#include <string>
#include <vector>

#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "converter/segments.h"
#include "data_manager/data_manager_interface.h"
#include "dictionary/pos_matcher.h"
Expand All @@ -58,7 +58,7 @@ class SingleKanjiRewriter : public RewriterInterface {
private:
void AddDescriptionForExistingCandidates(Segment *segment) const;
bool InsertCandidate(bool is_single_segment, uint16_t single_kanji_id,
const std::vector<std::string> &kanji_list,
absl::Span<const std::string> kanji_list,
Segment *segment) const;
void FillCandidate(absl::string_view key, absl::string_view value, int cost,
uint16_t single_kanji_id, Segment::Candidate *cand) const;
Expand Down
7 changes: 4 additions & 3 deletions src/rewriter/transliteration_rewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/japanese_util.h"
#include "base/number_util.h"
#include "base/text_normalizer.h"
Expand Down Expand Up @@ -183,12 +184,12 @@ void ModifyT13nsForGodan(const absl::string_view key,
(*t13ns)[transliteration::FULL_ASCII_CAPITALIZED] = full_ascii_capitalized;
}

bool IsTransliterated(const std::vector<std::string> &t13ns) {
bool IsTransliterated(absl::Span<const std::string> t13ns) {
if (t13ns.empty() || t13ns[0].empty()) {
return false;
}

const std::string &base_candidate = t13ns[0];
absl::string_view base_candidate = t13ns[0];
for (size_t i = 1; i < t13ns.size(); ++i) {
if (t13ns[i] != base_candidate) {
return true;
Expand Down Expand Up @@ -433,7 +434,7 @@ void TransliterationRewriter::InitT13nCandidate(
}

bool TransliterationRewriter::SetTransliterations(
const std::vector<std::string> &t13ns, const absl::string_view key,
absl::Span<const std::string> t13ns, const absl::string_view key,
Segment *segment) const {
if (t13ns.size() != transliteration::NUM_T13N_TYPES ||
!IsTransliterated(t13ns)) {
Expand Down
4 changes: 2 additions & 2 deletions src/rewriter/transliteration_rewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@

#include <cstdint>
#include <string>
#include <vector>

#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "converter/segments.h"
#include "dictionary/pos_matcher.h"
#include "request/conversion_request.h"
Expand Down Expand Up @@ -62,7 +62,7 @@ class TransliterationRewriter : public RewriterInterface {
Segment::Candidate *cand) const;
// Sets transliteration values into segment. If t13ns is invalid,
// false is returned.
bool SetTransliterations(const std::vector<std::string> &t13ns,
bool SetTransliterations(absl::Span<const std::string> t13ns,
absl::string_view key, Segment *segment) const;
bool FillT13nsFromComposer(const ConversionRequest &request,
Segments *segments) const;
Expand Down
3 changes: 2 additions & 1 deletion src/rewriter/user_segment_history_rewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/config_file_stream.h"
#include "base/file_util.h"
#include "base/number_util.h"
Expand Down Expand Up @@ -403,7 +404,7 @@ bool IsT13NCandidate(const Segment::Candidate &cand) {
} // namespace

bool UserSegmentHistoryRewriter::SortCandidates(
const std::vector<ScoreCandidate> &sorted_scores, Segment *segment) const {
absl::Span<const ScoreCandidate> sorted_scores, Segment *segment) const {
const uint32_t top_score = sorted_scores[0].score;
const size_t size = std::min(sorted_scores.size(), kMaxRerankSize);
constexpr uint32_t kScoreGap = 20; // TODO(taku): no justification
Expand Down
4 changes: 2 additions & 2 deletions src/rewriter/user_segment_history_rewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
#include <cstddef>
#include <cstdint>
#include <memory>
#include <vector>

#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "converter/segments.h"
#include "dictionary/pos_group.h"
#include "dictionary/pos_matcher.h"
Expand Down Expand Up @@ -103,7 +103,7 @@ class UserSegmentHistoryRewriter : public RewriterInterface {
void InsertTriggerKey(const Segment &segment);
bool IsPunctuation(const Segment &seg,
const Segment::Candidate &candidate) const;
bool SortCandidates(const std::vector<ScoreCandidate> &sorted_scores,
bool SortCandidates(absl::Span<const ScoreCandidate> sorted_scores,
Segment *segment) const;
Score Fetch(absl::string_view key, uint32_t weight) const;
void Insert(absl::string_view key, bool force);
Expand Down
1 change: 1 addition & 0 deletions src/session/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ mozc_cc_library(
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
)

Expand Down
Loading

0 comments on commit 49ed94a

Please sign in to comment.