nexthop-list.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2021, The University of Memphis,
4  * Regents of the University of California
5  *
6  * This file is part of NLSR (Named-data Link State Routing).
7  * See AUTHORS.md for complete list of NLSR authors and contributors.
8  *
9  * NLSR is free software: you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License as published by the Free Software Foundation,
11  * either version 3 of the License, or (at your option) any later version.
12  *
13  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef NLSR_NEXTHOP_LIST_HPP
22 #define NLSR_NEXTHOP_LIST_HPP
23 
24 #include "nexthop.hpp"
25 #include "adjacent.hpp"
26 
27 #include <ndn-cxx/face.hpp>
28 #include <ndn-cxx/util/ostream-joiner.hpp>
29 
30 #include <set>
31 
32 namespace nlsr {
33 
35  bool
36  operator() (const NextHop& nh1, const NextHop& nh2) const {
38  return true;
39  }
41  return nh1.getConnectingFaceUri() < nh2.getConnectingFaceUri();
42  }
43  else {
44  return false;
45  }
46  }
47 };
48 
50  bool
51  operator() (const NextHop& nh1, const NextHop& nh2) const {
52  return nh1.getConnectingFaceUri() < nh2.getConnectingFaceUri();
53  }
54 };
55 
56 static inline bool
57 nexthopAddCompare(const NextHop& nh1, const NextHop& nh2)
58 {
59  return nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri();
60 }
61 
62 static inline bool
63 nexthopRemoveCompare(const NextHop& nh1, const NextHop& nh2)
64 {
65  return (nh1.getConnectingFaceUri() == nh2.getConnectingFaceUri() &&
67 }
68 
69 template<typename T = NextHopComparator>
71 {
72 public:
73  NexthopListT() = default;
74 
82  void
83  addNextHop(const NextHop& nh)
84  {
85  auto it = std::find_if(m_nexthopList.begin(), m_nexthopList.end(),
86  std::bind(&nexthopAddCompare, _1, nh));
87  if (it == m_nexthopList.end()) {
88  m_nexthopList.insert(nh);
89  }
90  else if (it->getRouteCost() > nh.getRouteCost()) {
91  removeNextHop(*it);
92  m_nexthopList.insert(nh);
93  }
94  }
95 
101  void
103  {
104  auto it = std::find_if(m_nexthopList.begin(), m_nexthopList.end(),
105  std::bind(&nexthopRemoveCompare, _1, nh));
106  if (it != m_nexthopList.end()) {
107  m_nexthopList.erase(it);
108  }
109  }
110 
111  size_t
112  size() const
113  {
114  return m_nexthopList.size();
115  }
116 
117  void
119  {
120  m_nexthopList.clear();
121  }
122 
123  const std::set<NextHop, T>&
124  getNextHops() const
125  {
126  return m_nexthopList;
127  }
128 
129  typedef T value_type;
130  typedef typename std::set<NextHop, T>::iterator iterator;
131  typedef typename std::set<NextHop, T>::const_iterator const_iterator;
132  typedef typename std::set<NextHop, T>::reverse_iterator reverse_iterator;
133 
134  iterator
135  begin() const
136  {
137  return m_nexthopList.begin();
138  }
139 
140  iterator
141  end() const
142  {
143  return m_nexthopList.end();
144  }
145 
147  cbegin() const
148  {
149  return m_nexthopList.begin();
150  }
151 
153  cend() const
154  {
155  return m_nexthopList.end();
156  }
157 
159  rbegin() const
160  {
161  return m_nexthopList.rbegin();
162  }
163 
165  rend() const
166  {
167  return m_nexthopList.rend();
168  }
169 
170 private:
171  std::set<NextHop, T> m_nexthopList;
172 };
173 
175 
176 template<typename T>
177 bool
179 {
180  return lhs.getNextHops() == rhs.getNextHops();
181 }
182 
183 template<typename T>
184 bool
186 {
187  return !(lhs == rhs);
188 }
189 
190 template<typename T>
191 std::ostream&
192 operator<<(std::ostream& os, const NexthopListT<T>& nhl)
193 {
194  os << " ";
195  std::copy(nhl.cbegin(), nhl.cend(), ndn::make_ostream_joiner(os, "\n "));
196  return os;
197 }
198 
199 } // namespace nlsr
200 
201 #endif // NLSR_NEXTHOP_LIST_HPP
Data abstraction for Nexthop.
Definition: nexthop.hpp:44
const std::string & getConnectingFaceUri() const
Definition: nexthop.hpp:68
double getRouteCost() const
Definition: nexthop.hpp:94
uint64_t getRouteCostAsAdjustedInteger() const
Definition: nexthop.hpp:80
std::set< NextHop, T >::iterator iterator
NexthopListT()=default
reverse_iterator rbegin() const
void removeNextHop(const NextHop &nh)
Remove a next hop from the Next Hop list.
iterator end() const
const_iterator cend() const
const_iterator cbegin() const
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
reverse_iterator rend() const
iterator begin() const
const std::set< NextHop, T > & getNextHops() const
size_t size() const
std::set< NextHop, T >::const_iterator const_iterator
std::set< NextHop, T >::reverse_iterator reverse_iterator
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
static bool nexthopAddCompare(const NextHop &nh1, const NextHop &nh2)
static bool nexthopRemoveCompare(const NextHop &nh1, const NextHop &nh2)
bool operator!=(const NexthopListT< T > &lhs, const NexthopListT< T > &rhs)
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:176
NexthopListT NexthopList
bool operator==(const NamePrefixTableEntry &lhs, const NamePrefixTableEntry &rhs)
bool operator()(const NextHop &nh1, const NextHop &nh2) const
bool operator()(const NextHop &nh1, const NextHop &nh2) const