src: Sort NextHops on NexthopList insertion

refs: #2721

Change-Id: I39893c5fb6b5fac93220901ab3190090f0d2cc57
diff --git a/src/route/nexthop-list.cpp b/src/route/nexthop-list.cpp
index ed46b55..22046fd 100644
--- a/src/route/nexthop-list.cpp
+++ b/src/route/nexthop-list.cpp
@@ -19,8 +19,6 @@
  * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  **/
 
-#include <iostream>
-
 #include "common.hpp"
 #include "nexthop-list.hpp"
 #include "nexthop.hpp"
@@ -33,64 +31,37 @@
 using namespace std;
 
 static bool
-nexthopCompare(NextHop& nh1, NextHop& nh2)
+nexthopAddCompare(const NextHop& nh1, const NextHop& nh2)
 {
   return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
 }
 
 static bool
-nexthopRemoveCompare(NextHop& nh1, NextHop& nh2)
+nexthopRemoveCompare(const NextHop& nh1, const NextHop& nh2)
 {
   return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
           nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) ;
 }
 
-static bool
-nextHopSortingComparator(const NextHop& nh1, const NextHop& nh2)
-{
-  if (nh1.getRouteCostAsAdjustedInteger() < nh2.getRouteCostAsAdjustedInteger()) {
-    return true;
-  }
-  else if (nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) {
-    return nh1.getConnectingFaceUri() < nh2.getConnectingFaceUri();
-  }
-  else {
-    return false;
-  }
-}
-
-/*!
-Add next hop to the Next Hop list
-If next hop is new it is added
-If next hop already exists in next
-hop list then updates the route
-cost with new next hop's route cost
-*/
-
 void
-NexthopList::addNextHop(NextHop& nh)
+NexthopList::addNextHop(const NextHop& nh)
 {
-  std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
+  std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
                                                  m_nexthopList.end(),
-                                                 ndn::bind(&nexthopCompare, _1, nh));
+                                                 ndn::bind(&nexthopAddCompare, _1, nh));
   if (it == m_nexthopList.end()) {
-    m_nexthopList.push_back(nh);
-    return;
+    m_nexthopList.insert(nh);
   }
-  if ((*it).getRouteCost() > nh.getRouteCost()) {
-    (*it).setRouteCost(nh.getRouteCost());
+  else if (it->getRouteCost() > nh.getRouteCost()) {
+    removeNextHop(*it);
+    m_nexthopList.insert(nh);
   }
 }
 
-/*!
-Remove a next hop only if both next hop face and route cost are same
-
-*/
-
 void
-NexthopList::removeNextHop(NextHop& nh)
+NexthopList::removeNextHop(const NextHop& nh)
 {
-  std::list<NextHop>::iterator it = std::find_if(m_nexthopList.begin(),
+  std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
                                                  m_nexthopList.end(),
                                                  ndn::bind(&nexthopRemoveCompare, _1, nh));
   if (it != m_nexthopList.end()) {
@@ -99,17 +70,11 @@
 }
 
 void
-NexthopList::sort()
-{
-  m_nexthopList.sort(nextHopSortingComparator);
-}
-
-void
 NexthopList::writeLog()
 {
   int i = 1;
-  sort();
-  for (std::list<NextHop>::iterator it = m_nexthopList.begin();
+
+  for (std::set<NextHop, NextHopComparator>::iterator it = m_nexthopList.begin();
        it != m_nexthopList.end() ; it++, i++) {
     _LOG_DEBUG("Nexthop " << i << ": " << (*it).getConnectingFaceUri()
                << " Route Cost: " << (*it).getRouteCost());