Skip to content

Commit

Permalink
C++ fixes: Reduce manual dynamic allocation, use std::algorithm (micr…
Browse files Browse the repository at this point in the history
  • Loading branch information
Thai Nguyen authored and Craig Wittenberg committed Apr 17, 2018
1 parent ba9c702 commit dd699aa
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,4 @@ __pycache__/
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
*.xsd.cs
17 changes: 7 additions & 10 deletions src/BagOValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class BagOValues
this->m_spinlock.Lock();
wstring lowered;
lowered.resize(key.size());
transform(key.begin(), key.end(), lowered.begin(), ::tolower);
m_Values.insert(m_Values.end(), make_pair(lowered, value));
transform(std::begin(key), std::end(key), std::begin(lowered), ::tolower);
m_Values.emplace_back(make_pair(std::move(lowered), value));

m_lastStr.resize(0); // clear this after new data added
this->m_spinlock.Unlock();
Expand All @@ -57,13 +57,13 @@ class BagOValues
// fPrefix = false means that we only return values when an entire key matches and we match substrings of the query
//
// NOTE: returns a newly allocated vector; must delete it
vector<TValue> *Retrieve(const wstring& query, bool fPrefix = true, unsigned maxResults = ULONG_MAX)
vector<TValue> Retrieve(const wstring& query, bool fPrefix = true, unsigned maxResults = ULONG_MAX)
{
wstring lowered;
lowered.resize(query.size());
transform(query.begin(), query.end(), lowered.begin(), ::tolower);
transform(std::cbegin(query), std::cend(query), std::begin(lowered), ::tolower);

vector<TValue> *results = NULL;
vector<TValue> results;
TValue val = TValue();
TPair laspair = make_pair(lowered, val);

Expand Down Expand Up @@ -93,13 +93,10 @@ class BagOValues
continue;
}

if (results == NULL)
results = new vector<TValue>();

if (results->size() >= maxResults)
if (results.size() >= maxResults)
break;

results->insert(results->end(), itr->second);
results.push_back(itr->second);
}
else if (cmp > 0)
{
Expand Down
Loading

0 comments on commit dd699aa

Please sign in to comment.