table: NameTree::findLongestPrefixMatch predicate

refs #1313

Change-Id: I759c8ddf9bc0fe5b970c979f69131a98b1ef32e4
diff --git a/daemon/table/fib.cpp b/daemon/table/fib.cpp
index 9c7a611..32aabbf 100644
--- a/daemon/table/fib.cpp
+++ b/daemon/table/fib.cpp
@@ -9,17 +9,25 @@
 #include "measurements-entry.hpp"
 
 namespace nfd {
+
+const shared_ptr<fib::Entry> Fib::m_emptyEntry = make_shared<fib::Entry>(Name());
+
 Fib::Fib(NameTree& nameTree)
   : m_nameTree(nameTree)
   , m_nItems(0)
 {
-  m_rootEntry = (this->insert(Name())).first;
 }
 
 Fib::~Fib()
 {
 }
 
+static inline bool
+predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& entry)
+{
+  return static_cast<bool>(entry.getFibEntry());
+}
+
 std::pair<shared_ptr<fib::Entry>, bool>
 Fib::insert(const Name& prefix)
 {
@@ -36,15 +44,12 @@
 shared_ptr<fib::Entry>
 Fib::findLongestPrefixMatch(const Name& prefix) const
 {
-  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findLongestPrefixMatch(prefix);
-  while (static_cast<bool>(nameTreeEntry))
-  {
-    if (static_cast<bool>(nameTreeEntry->getFibEntry()))
-      return nameTreeEntry->getFibEntry();
-    else
-      nameTreeEntry = nameTreeEntry->getParent();
+  shared_ptr<name_tree::Entry> nameTreeEntry =
+    m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry);
+  if (static_cast<bool>(nameTreeEntry)) {
+    return nameTreeEntry->getFibEntry();
   }
-  return m_rootEntry;
+  return m_emptyEntry;
 }
 
 shared_ptr<fib::Entry>
@@ -82,13 +87,11 @@
 void
 Fib::removeNextHopFromAllEntries(shared_ptr<Face> face)
 {
-  shared_ptr<fib::Entry> entry;
-  shared_ptr<std::vector<shared_ptr<name_tree::Entry > > > res = m_nameTree.fullEnumerate();
-  for (int i = 0; i < res->size(); i++)
-  {
-    entry = (*res)[i]->getFibEntry();
-    if (static_cast<bool>(entry))
-      entry->removeNextHop(face);
+  shared_ptr<std::vector<shared_ptr<name_tree::Entry > > > nameTreeEntries =
+    m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry);
+  for (size_t i = 0; i < nameTreeEntries->size(); ++i) {
+    shared_ptr<fib::Entry> entry = nameTreeEntries->at(i)->getFibEntry();
+    entry->removeNextHop(face);
   }
 }