Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Making PointerVectorSet to be always ordered and unique #12077

Closed
62 changes: 21 additions & 41 deletions kratos/containers/pointer_vector_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,29 +178,18 @@ class PointerVectorSet final
*/
TDataType& operator[](const key_type& Key)
{
ptr_iterator sorted_part_end;

if (mData.size() - mSortedPartSize >= mMaxBufferSize) {
Sort();
sorted_part_end = mData.end();
auto itr_pos = std::lower_bound(mData.begin(), mData.end(), Key, CompareKey());
if (itr_pos == mData.end()) {
// insert a new element with id.
mData.push_back(TPointerType(new TDataType(Key)));
return back();
} else if (EqualKeyTo(Key)(*itr_pos)) {
roigcarlo marked this conversation as resolved.
Show resolved Hide resolved
// already found existing element with the same key, hence returning the existing element.
return **itr_pos;
} else {
sorted_part_end = mData.begin() + mSortedPartSize;
}

ptr_iterator i(std::lower_bound(mData.begin(), sorted_part_end, Key, CompareKey()));
if (i == sorted_part_end) {
mSortedPartSize++;
return **mData.insert(sorted_part_end, TPointerType(new TDataType(Key)));
}

if (!EqualKeyTo(Key)(*i)) {
if ((i = std::find_if(sorted_part_end, mData.end(), EqualKeyTo(Key))) == mData.end()) {
mData.push_back(TPointerType(new TDataType(Key)));
return **(mData.end() - 1);
}
// insert the new value before the itr_pos.
return **(mData.insert(itr_pos, TPointerType(new TDataType(Key))));
}

return **i;
}

/**
Expand All @@ -213,27 +202,18 @@ class PointerVectorSet final
*/
pointer& operator()(const key_type& Key)
{
ptr_iterator sorted_part_end;

if (mData.size() - mSortedPartSize >= mMaxBufferSize) {
Sort();
sorted_part_end = mData.end();
} else
sorted_part_end = mData.begin() + mSortedPartSize;

ptr_iterator i(std::lower_bound(mData.begin(), sorted_part_end, Key, CompareKey()));
if (i == sorted_part_end) {
mSortedPartSize++;
return *mData.insert(sorted_part_end, TPointerType(new TDataType(Key)));
auto itr_pos = std::lower_bound(mData.begin(), mData.end(), Key, CompareKey());
if (itr_pos == mData.end()) {
// insert a new element with id.
mData.push_back(TPointerType(new TDataType(Key)));
return mData.back();
} else if (EqualKeyTo(Key)(*itr_pos)) {
roigcarlo marked this conversation as resolved.
Show resolved Hide resolved
// already found existing element with the same key, hence returning the existing element.
return *itr_pos;
} else {
// insert the new value before the itr_pos.
return *(mData.insert(itr_pos, TPointerType(new TDataType(Key))));
}

if (!EqualKeyTo(Key)(*i))
if ((i = std::find_if(sorted_part_end, mData.end(), EqualKeyTo(Key))) == mData.end()) {
mData.push_back(TPointerType(new TDataType(Key)));
return *(mData.end() - 1);
}

return *i;
}

/**
Expand Down
Loading