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-2023, 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 {
36  bool
37  operator()(const NextHop& lhs, const NextHop& rhs) const
38  {
39  return lhs.getConnectingFaceUri() < rhs.getConnectingFaceUri();
40  }
41 };
42 
43 template<typename T = std::less<NextHop>>
45 {
46 public:
47  NexthopListT() = default;
48 
56  void
57  addNextHop(const NextHop& nh)
58  {
59  auto it = std::find_if(m_nexthopList.begin(), m_nexthopList.end(),
60  [&nh] (const auto& item) {
61  return item.getConnectingFaceUri() == nh.getConnectingFaceUri();
62  });
63  if (it == m_nexthopList.end()) {
64  m_nexthopList.insert(nh);
65  }
66  else if (it->getRouteCost() > nh.getRouteCost()) {
67  m_nexthopList.erase(it);
68  m_nexthopList.insert(nh);
69  }
70  }
71 
77  void
79  {
80  auto it = std::find(m_nexthopList.begin(), m_nexthopList.end(), nh);
81  if (it != m_nexthopList.end()) {
82  m_nexthopList.erase(it);
83  }
84  }
85 
86  size_t
87  size() const
88  {
89  return m_nexthopList.size();
90  }
91 
92  void
94  {
95  m_nexthopList.clear();
96  }
97 
98  const std::set<NextHop, T>&
99  getNextHops() const
100  {
101  return m_nexthopList;
102  }
103 
104  typedef T value_type;
105  typedef typename std::set<NextHop, T>::iterator iterator;
106  typedef typename std::set<NextHop, T>::const_iterator const_iterator;
107  typedef typename std::set<NextHop, T>::reverse_iterator reverse_iterator;
108 
109  iterator
110  begin() const
111  {
112  return m_nexthopList.begin();
113  }
114 
115  iterator
116  end() const
117  {
118  return m_nexthopList.end();
119  }
120 
122  cbegin() const
123  {
124  return m_nexthopList.begin();
125  }
126 
128  cend() const
129  {
130  return m_nexthopList.end();
131  }
132 
134  rbegin() const
135  {
136  return m_nexthopList.rbegin();
137  }
138 
140  rend() const
141  {
142  return m_nexthopList.rend();
143  }
144 
145 private:
146  std::set<NextHop, T> m_nexthopList;
147 };
148 
150 
151 template<typename T>
152 bool
154 {
155  return lhs.getNextHops() == rhs.getNextHops();
156 }
157 
158 template<typename T>
159 bool
161 {
162  return !(lhs == rhs);
163 }
164 
165 template<typename T>
166 std::ostream&
167 operator<<(std::ostream& os, const NexthopListT<T>& nhl)
168 {
169  os << " ";
170  std::copy(nhl.cbegin(), nhl.cend(), ndn::make_ostream_joiner(os, "\n "));
171  return os;
172 }
173 
174 } // namespace nlsr
175 
176 #endif // NLSR_NEXTHOP_LIST_HPP
Data abstraction for Nexthop.
Definition: nexthop.hpp:47
const ndn::FaceUri & getConnectingFaceUri() const
Definition: nexthop.hpp:66
double getRouteCost() const
Definition: nexthop.hpp:92
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.
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 &lhs, const NextHop &rhs) const