src: Fib and FaceMap now use map instead of list as their table data structure.

ref: #2781

Change-Id: I93ad23266ee7b79fd5c53b538c30af76565abc4b
diff --git a/src/route/face-map.cpp b/src/route/face-map.cpp
index d8687e5..108040e 100644
--- a/src/route/face-map.cpp
+++ b/src/route/face-map.cpp
@@ -21,7 +21,6 @@
  *
  **/
 #include <iostream>
-#include <list>
 #include <utility>
 
 #include "common.hpp"
@@ -36,11 +35,10 @@
 FaceMap::writeLog()
 {
   _LOG_DEBUG("------- Face Map-----------");
-  for(std::list<FaceMapEntry>::iterator it = m_table.begin();
-      it != m_table.end(); ++it) {
-    _LOG_DEBUG("Face Map Entry (FaceUri: " << (*it).getFaceUri() << " Face Id: "
-               << (*it).getFaceId() << ")");
+  for (const auto& it : m_table) {
+    _LOG_DEBUG("Face Map Entry (FaceUri: " << (it.second).getFaceUri()
+               << " Face Id: " << (it.second).getFaceId() << ")");
   }
 }
 
-} // namespace NLSR
+} // namespace nlsr
diff --git a/src/route/face-map.hpp b/src/route/face-map.hpp
index 6a2890f..b3fc911 100644
--- a/src/route/face-map.hpp
+++ b/src/route/face-map.hpp
@@ -17,8 +17,6 @@
  * You should have received a copy of the GNU General Public License along with
  * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  *
- * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
- *
  **/
 #ifndef NLSR_FACE_MAP_HPP
 #define NLSR_FACE_MAP_HPP
@@ -26,6 +24,8 @@
 #include <ndn-cxx/common.hpp>
 #include <algorithm>
 
+#include <map>
+
 namespace nlsr {
 
 class FaceMapEntry
@@ -89,15 +89,12 @@
   update(const std::string& faceUri, uint32_t faceId)
   {
     FaceMapEntry fme(faceUri, faceId);
-    std::list<FaceMapEntry>::iterator it = std::find_if(m_table.begin(),
-                                                        m_table.end(),
-                                                        bind(&FaceMapEntry::compare,
-                                                             &fme, _1));
+    std::map<std::string, FaceMapEntry>::iterator it = m_table.find(faceUri);
     if (it == m_table.end()) {
-      m_table.push_back(fme);
+      m_table.emplace(faceUri, fme);
     }
     else {
-      (*it).setFaceId(fme.getFaceId());
+      (it->second).setFaceId(fme.getFaceId());
     }
   }
 
@@ -105,12 +102,9 @@
   getFaceId(const std::string& faceUri)
   {
     FaceMapEntry fme(faceUri, 0);
-    std::list<FaceMapEntry>::iterator it = std::find_if(m_table.begin(),
-                                                        m_table.end(),
-                                                        bind(&FaceMapEntry::compare,
-                                                             &fme, _1));
+    std::map<std::string, FaceMapEntry>::iterator it = m_table.find(faceUri);
     if (it != m_table.end()) {
-      return (*it).getFaceId();
+      return (it->second).getFaceId();
     }
     return 0;
   }
@@ -119,7 +113,7 @@
   writeLog();
 
 private:
-  std::list<FaceMapEntry> m_table;
+  std::map<std::string, FaceMapEntry> m_table;
 };
 
 } // namespace nlsr
diff --git a/src/route/fib.cpp b/src/route/fib.cpp
index 4a82661..a7edf7b 100644
--- a/src/route/fib.cpp
+++ b/src/route/fib.cpp
@@ -17,7 +17,8 @@
  * You should have received a copy of the GNU General Public License along with
  * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  **/
-#include <list>
+#include <map>
+
 #include <cmath>
 #include <ndn-cxx/common.hpp>
 
@@ -38,19 +39,12 @@
 using namespace std;
 using namespace ndn;
 
-static bool
-fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name)
-{
-  return fibEntry.getName() == name;
-}
-
 void
 Fib::cancelScheduledExpiringEvent(EventId eid)
 {
   m_scheduler.cancelEvent(eid);
 }
 
-
 ndn::EventId
 Fib::scheduleEntryExpiration(const ndn::Name& name, int32_t feSeqNum,
                              const ndn::time::seconds& expTime)
@@ -65,20 +59,18 @@
 Fib::remove(const ndn::Name& name)
 {
   _LOG_DEBUG("Fib::remove called");
-  std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
-                                                  m_table.end(),
-                                                  bind(&fibEntryNameCompare, _1, name));
+  std::map<ndn::Name, FibEntry>::iterator it = m_table.find(name);
   if (it != m_table.end()) {
     for (std::set<NextHop, NextHopComparator>::iterator nhit =
-           (*it).getNexthopList().getNextHops().begin();
-         nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
+           (it->second).getNexthopList().getNextHops().begin();
+         nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) {
       //remove entry from NDN-FIB
-      if (isPrefixUpdatable(it->getName())) {
-        unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
+      if (isPrefixUpdatable((it->second).getName())) {
+        unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri());
       }
     }
     _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
-    cancelScheduledExpiringEvent((*it).getExpiringEventId());
+    cancelScheduledExpiringEvent((it->second).getExpiringEventId());
     m_table.erase(it);
   }
 }
@@ -165,9 +157,7 @@
     hopsToAdd.addNextHop(*it);
   }
 
-  std::list<FibEntry>::iterator entryIt = std::find_if(m_table.begin(),
-                                                       m_table.end(),
-                                                       bind(&fibEntryNameCompare, _1, name));
+  std::map<ndn::Name, FibEntry>::iterator entryIt = m_table.find(name);
 
   // New FIB entry
   if (entryIt == m_table.end()) {
@@ -190,13 +180,12 @@
     // Schedule entry to be refreshed
     entry.setExpiringEventId(scheduleEntryExpiration(name , entry.getSeqNo(),
                                                      ndn::time::seconds(m_refreshTime)));
-    m_table.push_back(entry);
+    m_table.emplace(name, entry);
   }
   else {
     // Existing FIB entry
     _LOG_DEBUG("Existing FIB Entry");
-
-    FibEntry& entry = *entryIt;
+    FibEntry& entry = (entryIt->second);
 
     // Remove empty FIB entry
     if (hopsToAdd.getSize() == 0) {
@@ -227,15 +216,16 @@
 Fib::clean()
 {
   _LOG_DEBUG("Fib::clean called");
-  for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
+  for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
+       it != m_table.end();
        ++it) {
-    _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName());
-    cancelScheduledExpiringEvent((*it).getExpiringEventId());
+    _LOG_DEBUG("Cancelling Scheduled event. Name: " << (it->second).getName());
+    cancelScheduledExpiringEvent((it->second).getExpiringEventId());
     for (std::set<NextHop, NextHopComparator>::iterator nhit =
-         (*it).getNexthopList().getNextHops().begin();
-         nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
+         (it->second).getNexthopList().getNextHops().begin();
+         nhit != (it->second).getNexthopList().getNextHops().end(); nhit++) {
       //Remove entry from NDN-FIB
-      unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
+      unregisterPrefix((it->second).getName(), nhit->getConnectingFaceUri());
     }
   }
   if (m_table.size() > 0) {
@@ -511,9 +501,10 @@
 Fib::writeLog()
 {
   _LOG_DEBUG("-------------------FIB-----------------------------");
-  for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
+  for (std::map<ndn::Name, FibEntry>::iterator it = m_table.begin();
+       it != m_table.end();
        ++it) {
-    (*it).writeLog();
+    (it->second).writeLog();
   }
 }
 
diff --git a/src/route/fib.hpp b/src/route/fib.hpp
index 4cf6d74..06b590e 100644
--- a/src/route/fib.hpp
+++ b/src/route/fib.hpp
@@ -18,12 +18,12 @@
  * You should have received a copy of the GNU General Public License along with
  * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  *
- * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
  **/
 #ifndef NLSR_FIB_HPP
 #define NLSR_FIB_HPP
 
-#include <list>
+#include <map>
+
 #include <boost/cstdint.hpp>
 
 #include <ndn-cxx/mgmt/nfd/controller.hpp>
@@ -185,8 +185,7 @@
 
 private:
   ndn::Scheduler& m_scheduler;
-
-  std::list<FibEntry> m_table;
+  std::map<ndn::Name, FibEntry> m_table;
   int32_t m_refreshTime;
   ndn::nfd::Controller m_controller;
   util::FaceController m_faceController;