-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use std::string_view where possible except for words(). Avoid allocating and shuffling data! Adding a header to combine all data together, organized by length once. Functions rewritten to retrieve by length (binary search) if possible later.
- Loading branch information
Showing
11 changed files
with
301 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,199 +1,132 @@ | ||
#include "faker-cxx/Word.h" | ||
|
||
#include <array> | ||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "../../common/StringHelper.h" | ||
#include "data/Adjectives.h" | ||
#include "data/Adverbs.h" | ||
#include "data/Conjunctions.h" | ||
#include "data/Interjections.h" | ||
#include "data/Nouns.h" | ||
#include "data/Prepositions.h" | ||
#include "data/Verbs.h" | ||
#include "data/All.h" | ||
#include "faker-cxx/Helper.h" | ||
|
||
namespace faker | ||
{ | ||
std::string Word::sample(std::optional<unsigned int> length) | ||
template <typename It> | ||
auto sortedSizeArrayElement(std::optional<unsigned int> length, It start, It end) -> decltype(*std::declval<It>()) | ||
{ | ||
std::vector<std::string> allWords{adjectives}; | ||
|
||
allWords.insert(allWords.end(), adverbs.begin(), adverbs.end()); | ||
allWords.insert(allWords.end(), conjunctions.begin(), conjunctions.end()); | ||
allWords.insert(allWords.end(), interjections.begin(), interjections.end()); | ||
allWords.insert(allWords.end(), nouns.begin(), nouns.end()); | ||
allWords.insert(allWords.end(), prepositions.begin(), prepositions.end()); | ||
allWords.insert(allWords.end(), verbs.begin(), verbs.end()); | ||
|
||
if (!length) | ||
{ | ||
return Helper::arrayElement<std::string>(allWords); | ||
return Helper::arrayElement(start, end); | ||
} | ||
|
||
const auto shuffledWords = Helper::shuffle(allWords); | ||
auto lower_it = | ||
::std::lower_bound(start, end, length, [](const auto& lhs, const auto& value) { return lhs.size() < length; }); | ||
|
||
for (const auto& word : shuffledWords) | ||
if (lower_it == end) | ||
{ | ||
if (word.size() == length) | ||
return Helper::arrayElement(start, end); | ||
} | ||
else | ||
{ | ||
if (lower_it->size() != length) | ||
return Helper::arrayElement(start, end); | ||
|
||
auto upper_it = lower_it; | ||
for (; upper_it != end; upper_it++) | ||
{ | ||
return word; | ||
if (upper_it->size() != lower_it->size()) | ||
break; | ||
} | ||
return Helper::arrayElement(lower_it, upper_it); | ||
} | ||
|
||
return Helper::arrayElement<std::string>(shuffledWords); | ||
} | ||
|
||
std::string Word::words(unsigned numberOfWords) | ||
std::string_view Word::sample(std::optional<unsigned int> length) | ||
{ | ||
std::vector<std::string> words; | ||
|
||
for (unsigned i = 0; i < numberOfWords; i++) | ||
{ | ||
words.push_back(sample()); | ||
} | ||
|
||
return StringHelper::joinString(words, " "); | ||
return sortedSizeArrayElement(length, _allWords.cbegin(), _allWords.cend()); | ||
} | ||
|
||
std::string Word::adjective(std::optional<unsigned int> length) | ||
std::string Word::words(unsigned numberOfWords) | ||
{ | ||
if (!length) | ||
if (numberOfWords == 0) | ||
{ | ||
return Helper::arrayElement<std::string>(adjectives); | ||
return ""; | ||
} | ||
|
||
const auto shuffledAdjectives = Helper::shuffle(adjectives); | ||
|
||
for (const auto& adjective : shuffledAdjectives) | ||
std::string combined_words; | ||
if (numberOfWords <= 256) | ||
{ | ||
if (adjective.size() == length) | ||
std::array<unsigned int, 256> tmp; // fitting 1024 bytes worth of integers* | ||
const size_t last_index = _allWords.size() - 1; | ||
size_t reserve_size = 0; | ||
|
||
for (unsigned i = 0; i < numberOfWords; i++) | ||
{ | ||
return adjective; | ||
tmp[i] = Number::integer<unsigned int>(last_index); | ||
auto vw = _allWords[tmp[i]]; | ||
reserve_size += vw.size(); | ||
} | ||
} | ||
|
||
return Helper::arrayElement<std::string>(shuffledAdjectives); | ||
} | ||
|
||
std::string Word::adverb(std::optional<unsigned int> length) | ||
{ | ||
if (!length) | ||
{ | ||
return Helper::arrayElement<std::string>(adverbs); | ||
} | ||
|
||
const auto shuffledAdverbs = Helper::shuffle(adverbs); | ||
|
||
for (const auto& adverb : shuffledAdverbs) | ||
{ | ||
if (adverb.size() == length) | ||
unsigned space_words = (numberOfWords - 1); | ||
combined_words.reserve(reserve_size + (numberOfWords - 1)); | ||
for (unsigned i = 0; i < space_words; i++) | ||
{ | ||
return adverb; | ||
auto vw = _allWords[tmp[i]]; | ||
combined_words.append(vw.begin(), vw.end()); | ||
combined_words.push_back(' '); | ||
} | ||
auto vw = _allWords[tmp[numberOfWords - 1]]; | ||
combined_words.append(vw.begin(), vw.end()); | ||
} | ||
|
||
return Helper::arrayElement<std::string>(shuffledAdverbs); | ||
} | ||
|
||
std::string Word::conjunction(std::optional<unsigned int> length) | ||
{ | ||
if (!length) | ||
else | ||
{ | ||
return Helper::arrayElement<std::string>(conjunctions); | ||
} | ||
|
||
const auto shuffledConjunctions = Helper::shuffle(conjunctions); | ||
|
||
for (const auto& conjunction : shuffledConjunctions) | ||
{ | ||
if (conjunction.size() == length) | ||
unsigned space_words = (numberOfWords - 1); | ||
for (unsigned i = 0; i < space_words; i++) | ||
{ | ||
return conjunction; | ||
auto s = sample(); | ||
combined_words.append(s.begin(), s.end()); | ||
combined_words.push_back(' '); | ||
} | ||
|
||
auto s = sample(); | ||
combined_words.append(s.begin(), s.end()); | ||
} | ||
|
||
return Helper::arrayElement<std::string>(shuffledConjunctions); | ||
return combined_words; | ||
} | ||
|
||
std::string Word::interjection(std::optional<unsigned int> length) | ||
std::string_view Word::adjective(std::optional<unsigned int> length) | ||
{ | ||
if (!length) | ||
{ | ||
return Helper::arrayElement<std::string>(interjections); | ||
} | ||
|
||
const auto shuffledInterjections = Helper::shuffle(interjections); | ||
|
||
for (const auto& interjection : shuffledInterjections) | ||
{ | ||
if (interjection.size() == length) | ||
{ | ||
return interjection; | ||
} | ||
} | ||
|
||
return Helper::arrayElement<std::string>(shuffledInterjections); | ||
return sortedSizeArrayElement(length, _adjectives_sorted.cbegin(), _adjectives_sorted.cend()); | ||
} | ||
|
||
std::string Word::noun(std::optional<unsigned int> length) | ||
std::string_view Word::adverb(std::optional<unsigned int> length) | ||
{ | ||
if (!length) | ||
{ | ||
return Helper::arrayElement<std::string>(nouns); | ||
} | ||
|
||
const auto shuffledNouns = Helper::shuffle(nouns); | ||
|
||
for (const auto& noun : shuffledNouns) | ||
{ | ||
if (noun.size() == length) | ||
{ | ||
return noun; | ||
} | ||
} | ||
|
||
return Helper::arrayElement<std::string>(shuffledNouns); | ||
return sortedSizeArrayElement(length, _adverbs_sorted.cbegin(), _adverbs_sorted.cend()); | ||
} | ||
|
||
std::string Word::preposition(std::optional<unsigned int> length) | ||
std::string_view Word::conjunction(std::optional<unsigned int> length) | ||
{ | ||
if (!length) | ||
{ | ||
return Helper::arrayElement<std::string>(prepositions); | ||
} | ||
|
||
const auto shuffledPrepositions = Helper::shuffle(prepositions); | ||
|
||
for (const auto& preposition : shuffledPrepositions) | ||
{ | ||
if (preposition.size() == length) | ||
{ | ||
return preposition; | ||
} | ||
} | ||
|
||
return Helper::arrayElement<std::string>(shuffledPrepositions); | ||
return sortedSizeArrayElement(length, _conjunctions_sorted.cbegin(), _conjunctions_sorted.cend()); | ||
} | ||
|
||
std::string Word::verb(std::optional<unsigned int> length) | ||
std::string_view Word::interjection(std::optional<unsigned int> length) | ||
{ | ||
if (!length) | ||
{ | ||
return Helper::arrayElement<std::string>(verbs); | ||
} | ||
return sortedSizeArrayElement(length, _interjections_sorted.cbegin(), _interjections_sorted.cend()); | ||
} | ||
|
||
const auto shuffledVerbs = Helper::shuffle(verbs); | ||
std::string_view Word::noun(std::optional<unsigned int> length) | ||
{ | ||
return sortedSizeArrayElement(length, _nouns_sorted.cbegin(), _nouns_sorted.cend()); | ||
} | ||
|
||
for (const auto& verb : shuffledVerbs) | ||
{ | ||
if (verb.size() == length) | ||
{ | ||
return verb; | ||
} | ||
} | ||
std::string_view Word::preposition(std::optional<unsigned int> length) | ||
{ | ||
return sortedSizeArrayElement(length, _prepositions_sorted.cbegin(), _prepositions_sorted.cend()); | ||
} | ||
|
||
return Helper::arrayElement<std::string>(shuffledVerbs); | ||
std::string_view Word::verb(std::optional<unsigned int> length) | ||
{ | ||
return sortedSizeArrayElement(length, _verbs_sorted.cbegin(), _verbs_sorted.cend()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.