From 54e8e37d2db141158e2cf10572ad96c57462e140 Mon Sep 17 00:00:00 2001 From: Daniel Jour Date: Sat, 30 Nov 2019 11:32:52 +0100 Subject: [PATCH] Keyframe::Contains(): Use binary search instead of linear search --- src/KeyFrame.cpp | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/KeyFrame.cpp b/src/KeyFrame.cpp index 8f89d1da..686626b0 100644 --- a/src/KeyFrame.cpp +++ b/src/KeyFrame.cpp @@ -114,20 +114,9 @@ int64_t Keyframe::FindIndex(Point p) const { // Determine if point already exists bool Keyframe::Contains(Point p) const { - // loop through points, and find a matching coordinate - for (int64_t x = 0; x < Points.size(); x++) { - // Get each point - Point existing_point = Points[x]; - - // find a match - if (p.co.X == existing_point.co.X) { - // Remove the matching point, and break out of loop - return true; - } - } - - // no matching point found - return false; + std::vector::const_iterator i = + std::lower_bound(begin(Points), end(Points), p.co.X, IsPointBeforeX); + return i != end(Points) && i->co.X == p.co.X; } // Get current point (or closest point) from the X coordinate (i.e. the frame number)