From 2b6e3d7ded319065ff6ecd2032ea4d369582fcf8 Mon Sep 17 00:00:00 2001 From: Ronak Buch Date: Tue, 20 Mar 2018 13:01:33 -0500 Subject: [PATCH] Cleanup binary search in causal message logging Change-Id: I459fb7a9adcf2c7ecdf8c405008441e4ce57f955 --- src/ck-core/ckcausalmlog.C | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/src/ck-core/ckcausalmlog.C b/src/ck-core/ckcausalmlog.C index 671454b024..74910292c2 100644 --- a/src/ck-core/ckcausalmlog.C +++ b/src/ck-core/ckcausalmlog.C @@ -2976,35 +2976,12 @@ void sortVec(std::vector *TNvec){ } int searchVec(std::vector *TNVec,MCount searchTN){ - if(TNVec->size() == 0){ - return -1; //not found in an empty vec - } - //binary search to find - int left=0; - int right = TNVec->size(); - int mid = (left +right)/2; - while(searchTN != (*TNVec)[mid] && left < right){ - if((*TNVec)[mid] > searchTN){ - right = mid-1; - }else{ - left = mid+1; - } - mid = (left + right)/2; - } - if(left < right){ - //mid is the element to be returned - return mid; - }else{ - if(mid < TNVec->size() && mid >=0){ - if((*TNVec)[mid] == searchTN){ - return mid; - }else{ - return -1; - } - }else{ - return -1; - } - } + // Find first element greater than or equal to searchTN + auto it = std::lower_bound(TNvec->begin(), TNvec->end(), searchTN); + if (it == TNvec.end() || *it != searchTN) + return -1; + else + return it - TNvec->begin(); }; -- 2.11.4.GIT