Fix a couple of valgrind errors.

This commit is contained in:
Kishore Nallan 2017-09-16 20:38:50 +05:30
parent 58bc73312d
commit f946f25c62
2 changed files with 23 additions and 23 deletions

View File

@ -32,6 +32,7 @@ size_t ArrayUtils::and_scalar(const uint32_t *A, const size_t lenA,
return (out - initout); // NOTREACHED
}
// merges two sorted arrays and also removes duplicates
size_t ArrayUtils::or_scalar(const uint32_t *A, const size_t lenA,
const uint32_t *B, const size_t lenB, uint32_t **out) {
size_t indexA = 0, indexB = 0, res_index = 0;
@ -45,23 +46,24 @@ size_t ArrayUtils::or_scalar(const uint32_t *A, const size_t lenA,
uint32_t* results = new uint32_t[lenA+lenB];
while (indexA < lenA && indexB < lenB) {
if (A[indexA] < B[indexB]) {
if(res_index == 0 || results[res_index-1] != A[indexA]) {
results[res_index] = A[indexA];
res_index++;
if (A[indexA] < B[indexB]) {
// check for duplicate
if(res_index == 0 || results[res_index-1] != A[indexA]) {
results[res_index] = A[indexA];
res_index++;
}
indexA++;
} else {
if(res_index == 0 || results[res_index-1] != B[indexB]) {
results[res_index] = B[indexB];
res_index++;
}
indexB++;
}
indexA++;
} else {
if(res_index == 0 || results[res_index-1] != B[indexB]) {
results[res_index] = B[indexB];
res_index++;
}
indexB++;
}
}
while (indexA < lenA) {
if(results[res_index-1] != A[indexA]) {
if(res_index == 0 || results[res_index-1] != A[indexA]) {
results[res_index] = A[indexA];
res_index++;
}
@ -70,7 +72,7 @@ size_t ArrayUtils::or_scalar(const uint32_t *A, const size_t lenA,
}
while (indexB < lenB) {
if(results[res_index-1] != B[indexB]) {
if(res_index == 0 || results[res_index-1] != B[indexB]) {
results[res_index] = B[indexB];
res_index++;
}

View File

@ -819,6 +819,7 @@ Option<nlohmann::json> Collection::search(std::string query, const std::vector<s
MatchScore mscore = MatchScore::match_score(field_order_kv.second.key, token_positions);
// unpack `mscore.offset_diffs` into `token_indices`
std::vector<size_t> token_indices;
char num_tokens_found = mscore.offset_diffs[0];
for(size_t i = 1; i <= num_tokens_found; i++) {
@ -835,20 +836,17 @@ Option<nlohmann::json> Collection::search(std::string query, const std::vector<s
const size_t end_index = (tokens.size() <= SNIPPET_STR_ABOVE_LEN) ? tokens.size() :
std::min((int)tokens.size(), (int)(*(minmax.second)+5));
std::stringstream snippet_stream;
size_t token_index = 0;
for(const size_t token_index: token_indices) {
tokens[token_index] = "<mark>" + tokens[token_index] + "</mark>";
}
std::stringstream snippet_stream;
for(size_t snippet_index = start_index; snippet_index < end_index; snippet_index++) {
if(snippet_index != start_index) {
snippet_stream << " ";
}
if(snippet_index == token_indices[token_index]) {
token_index++;
snippet_stream << "<mark>" + tokens[snippet_index] + "</mark>";
} else {
snippet_stream << tokens[snippet_index];
}
snippet_stream << tokens[snippet_index];
}
document["_snippets"] = nlohmann::json::object();
@ -1191,7 +1189,7 @@ inline std::vector<art_leaf *> Collection::next_suggestion(const std::vector<std
// generate the next combination from `token_leaves` and store it in `query_suggestion`
ldiv_t q { n, 0 };
for(long long i=token_leaves.size()-1 ; 0<=i ; --i ) {
for(long long i = 0 ; i < token_leaves.size(); i++) {
q = ldiv(q.quot, token_leaves[i].size());
query_suggestion[i] = token_leaves[i][q.rem];
}