am 198a47a1
: Merge "Add firstOrDefault and lastOrDefault to IntArrayView."
* commit '198a47a1616757500d3a45843c9f1471828702d1': Add firstOrDefault and lastOrDefault to IntArrayView.
This commit is contained in:
commit
3b628b43ee
4 changed files with 37 additions and 3 deletions
|
@ -397,7 +397,7 @@ bool Ver4PatriciaTriePolicy::removeNgramEntry(const PrevWordsInfo *const prevWor
|
|||
WordIdArray<MAX_PREV_WORD_COUNT_FOR_N_GRAM> prevWordIdArray;
|
||||
const WordIdArrayView prevWordIds = prevWordsInfo->getPrevWordIds(this, &prevWordIdArray,
|
||||
false /* tryLowerCaseSerch */);
|
||||
if (prevWordIds.empty() || prevWordIds[0] == NOT_A_WORD_ID) {
|
||||
if (prevWordIds.firstOrDefault(NOT_A_WORD_ID) == NOT_A_WORD_ID) {
|
||||
return false;
|
||||
}
|
||||
const int wordPos = getTerminalPtNodePosFromWordId(getWordId(wordCodePoints,
|
||||
|
|
|
@ -167,7 +167,7 @@ int LanguageModelDictContent::createAndGetBitmapEntryIndex(const WordIdArrayView
|
|||
if (lastBitmapEntryIndex == TrieMap::INVALID_INDEX) {
|
||||
return TrieMap::INVALID_INDEX;
|
||||
}
|
||||
const int oldestPrevWordId = prevWordIds[prevWordIds.size() - 1];
|
||||
const int oldestPrevWordId = prevWordIds.lastOrDefault(NOT_A_WORD_ID);
|
||||
const TrieMap::Result result = mTrieMap.get(oldestPrevWordId, lastBitmapEntryIndex);
|
||||
if (!result.mIsValid) {
|
||||
if (!mTrieMap.put(oldestPrevWordId,
|
||||
|
@ -175,7 +175,7 @@ int LanguageModelDictContent::createAndGetBitmapEntryIndex(const WordIdArrayView
|
|||
return TrieMap::INVALID_INDEX;
|
||||
}
|
||||
}
|
||||
return mTrieMap.getNextLevelBitmapEntryIndex(prevWordIds[prevWordIds.size() - 1],
|
||||
return mTrieMap.getNextLevelBitmapEntryIndex(prevWordIds.lastOrDefault(NOT_A_WORD_ID),
|
||||
lastBitmapEntryIndex);
|
||||
}
|
||||
|
||||
|
|
|
@ -115,6 +115,20 @@ class IntArrayView {
|
|||
memmove(buffer->data() + offset, mPtr, sizeof(int) * mSize);
|
||||
}
|
||||
|
||||
AK_FORCE_INLINE int firstOrDefault(const int defaultValue) const {
|
||||
if (empty()) {
|
||||
return defaultValue;
|
||||
}
|
||||
return mPtr[0];
|
||||
}
|
||||
|
||||
AK_FORCE_INLINE int lastOrDefault(const int defaultValue) const {
|
||||
if (empty()) {
|
||||
return defaultValue;
|
||||
}
|
||||
return mPtr[mSize - 1];
|
||||
}
|
||||
|
||||
private:
|
||||
DISALLOW_ASSIGNMENT_OPERATOR(IntArrayView);
|
||||
|
||||
|
|
|
@ -124,5 +124,25 @@ TEST(IntArrayViewTest, TestCopyToArray) {
|
|||
EXPECT_EQ(70, buffer[6]);
|
||||
}
|
||||
|
||||
TEST(IntArrayViewTest, TestFirstOrDefault) {
|
||||
const std::vector<int> intVector = {3, 2, 1, 0, -1, -2};
|
||||
IntArrayView intArrayView(intVector);
|
||||
|
||||
EXPECT_EQ(3, intArrayView.firstOrDefault(10));
|
||||
EXPECT_EQ(10, intArrayView.limit(0).firstOrDefault(10));
|
||||
EXPECT_EQ(-10, intArrayView.limit(0).firstOrDefault(-10));
|
||||
EXPECT_EQ(10, intArrayView.skip(6).firstOrDefault(10));
|
||||
}
|
||||
|
||||
TEST(IntArrayViewTest, TestLastOrDefault) {
|
||||
const std::vector<int> intVector = {3, 2, 1, 0, -1, -2};
|
||||
IntArrayView intArrayView(intVector);
|
||||
|
||||
EXPECT_EQ(-2, intArrayView.lastOrDefault(10));
|
||||
EXPECT_EQ(10, intArrayView.limit(0).lastOrDefault(10));
|
||||
EXPECT_EQ(-10, intArrayView.limit(0).lastOrDefault(-10));
|
||||
EXPECT_EQ(10, intArrayView.skip(6).lastOrDefault(10));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace latinime
|
||||
|
|
Loading…
Reference in a new issue