Skip to content

Instantly share code, notes, and snippets.

@rajkumar-p
Created March 14, 2020 05:03
Show Gist options
  • Save rajkumar-p/f7c60f4a98dba2a84b7c3d708bd5b57f to your computer and use it in GitHub Desktop.
Save rajkumar-p/f7c60f4a98dba2a84b7c3d708bd5b57f to your computer and use it in GitHub Desktop.
Check for an element in fine-grained list
template<typename T>
bool SortedList<T>::exists(T elem)
{
NodeListWithLock<T> *prev = this->_head;
prev->lock_node();
NodeListWithLock<T> *curr = prev->_next;
while (curr != nullptr &&
curr->data() < elem) {
curr->lock_node();
prev->unlock_node();
prev = curr;
curr = curr->_next;
}
if (curr == nullptr || curr->data() != elem) {
prev->unlock_node();
return false;
} else {
prev->unlock_node();
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment