route: refactor Fib::removeOldNextHopsFromFibEntryAndNfd()

Change-Id: I6ef61fcec6b934c0238ecb697c14564c02cacfe2
refs: #3820
diff --git a/src/route/fib.cpp b/src/route/fib.cpp
index c4ddad8..73cde75 100644
--- a/src/route/fib.cpp
+++ b/src/route/fib.cpp
@@ -26,6 +26,8 @@
 
 #include <map>
 #include <cmath>
+#include <algorithm>
+#include <iterator>
 
 namespace nlsr {
 
@@ -82,43 +84,6 @@
 }
 
 void
-Fib::removeOldNextHopsFromFibEntryAndNfd(FibEntry& entry, const NexthopList& installedHops)
-{
-  _LOG_DEBUG("Fib::removeOldNextHopsFromFibEntryAndNfd Called");
-
-  const ndn::Name& name = entry.getName();
-  NexthopList& entryHopList = entry.getNexthopList();
-
-  for (NexthopList::iterator it = entryHopList.begin(); it != entryHopList.end();) {
-
-    const std::string& faceUri = it->getConnectingFaceUri();
-
-    // See if the nexthop is installed in NFD's FIB
-    NexthopList::const_iterator foundIt = std::find_if(installedHops.cbegin(),
-                                                       installedHops.cend(),
-                                                       std::bind(&compareFaceUri, _1, faceUri));
-
-    // The next hop is not installed
-    if (foundIt == installedHops.cend()) {
-
-      if (isPrefixUpdatable(name)) {
-        // Remove the nexthop from NDN's FIB
-        unregisterPrefix(name, it->getConnectingFaceUri());
-      }
-
-      // Remove the next hop from the FIB entry
-      _LOG_DEBUG("Removing " << it->getConnectingFaceUri() << " from " << name);
-      // Since the iterator will be invalidated on removal, dereference the original
-      // and increment the copy
-      entryHopList.removeNextHop(*(it++));
-    }
-    else {
-      ++it;
-    }
-  }
-}
-
-void
 Fib::update(const ndn::Name& name, NexthopList& allHops)
 {
   FibEntry* entry = processUpdate(name, allHops);
@@ -176,7 +141,20 @@
     FibEntry& entry = (entryIt->second);
     addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
 
-    removeOldNextHopsFromFibEntryAndNfd(entry, hopsToAdd);
+    std::set<NextHop, NextHopComparator> hopsToRemove;
+    std::set_difference(entry.getNexthopList().begin(), entry.getNexthopList().end(),
+                        hopsToAdd.begin(), hopsToAdd.end(),
+                        std::inserter(hopsToRemove, hopsToRemove.end()), NextHopComparator());
+
+    bool isUpdatable = isPrefixUpdatable(entry.getName());
+    // Remove the uninstalled next hops from NFD and FIB entry
+    for (const auto& hop: hopsToRemove){
+      if (isUpdatable) {
+        unregisterPrefix(entry.getName(), hop.getConnectingFaceUri());
+      }
+      _LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.getName());
+      entry.getNexthopList().removeNextHop(hop);
+    }
 
     // Increment sequence number
     entry.setSeqNo(entry.getSeqNo() + 1);