Skip to content

Commit

Permalink
route: rename Map to NameMap
Browse files Browse the repository at this point in the history
The `Map` type is renamed to `NameMap` to better reflect what it does.

`createFromAdjLsdb` and `createFromCoordinateLsdb` functions are changed
to static functions, to match how they are used in callsites.

Unnecessary shared_ptr usage in `createFromAdjLsdb` is eliminated.

Doxygen of `NameMap` is added or improved.

refs #5308

Change-Id: I05db235efc1c6719f14b96758e3acde7826aabe7
  • Loading branch information
yoursunny committed Feb 15, 2024
1 parent 288141a commit 4eb4eae
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 162 deletions.
17 changes: 1 addition & 16 deletions src/common.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
Expand Down Expand Up @@ -39,21 +39,6 @@ using namespace ndn::time_literals;

constexpr ndn::time::seconds TIME_ALLOWED_FOR_CANONIZATION = 4_s;

template<typename T, typename = void>
struct is_iterator
{
static constexpr bool value = false;
};

/*! Use C++11 iterator_traits to check if some type is an iterator
*/
template<typename T>
struct is_iterator<T, std::enable_if_t<!std::is_same_v<
typename std::iterator_traits<T>::value_type, void>>>
{
static constexpr bool value = true;
};

} // namespace nlsr

#endif // NLSR_COMMON_HPP
111 changes: 0 additions & 111 deletions src/route/map.hpp

This file was deleted.

18 changes: 9 additions & 9 deletions src/route/map.cpp → src/route/name-map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/

#include "map.hpp"
#include "name-map.hpp"
#include "nlsr.hpp"
#include "adjacent.hpp"
#include "lsa/lsa.hpp"
Expand All @@ -27,14 +27,14 @@
namespace nlsr {

void
Map::addEntry(const ndn::Name& rtrName)
NameMap::addEntry(const ndn::Name& rtrName)
{
int32_t mappingNo = static_cast<int32_t>(m_bimap.size());
m_bimap.by<ndn::Name>().insert({rtrName, mappingNo});
}

std::optional<ndn::Name>
Map::getRouterNameByMappingNo(int32_t mn) const
NameMap::getRouterNameByMappingNo(int32_t mn) const
{
auto it = m_bimap.by<MappingNo>().find(mn);
if (it == m_bimap.by<MappingNo>().end()) {
Expand All @@ -44,22 +44,22 @@ Map::getRouterNameByMappingNo(int32_t mn) const
}

std::optional<int32_t>
Map::getMappingNoByRouterName(const ndn::Name& rName)
NameMap::getMappingNoByRouterName(const ndn::Name& rtrName) const
{
auto it = m_bimap.by<ndn::Name>().find(rName);
auto it = m_bimap.by<ndn::Name>().find(rtrName);
if (it == m_bimap.by<ndn::Name>().end()) {
return std::nullopt;
}
return it->get<Map::MappingNo>();
return it->get<MappingNo>();
}

std::ostream&
operator<<(std::ostream& os, const Map& map)
operator<<(std::ostream& os, const NameMap& map)
{
os << "---------------Map----------------------";
os << "---------------NameMap---------------";
for (const auto& entry : map.m_bimap) {
os << "\nMapEntry: ( Router: " << entry.get<ndn::Name>()
<< " Mapping No: " << entry.get<Map::MappingNo>() << " )";
<< " Mapping No: " << entry.get<NameMap::MappingNo>() << " )";
}
return os;
}
Expand Down
146 changes: 146 additions & 0 deletions src/route/name-map.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
* See AUTHORS.md for complete list of NLSR authors and contributors.
*
* NLSR is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* 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/>.
*/

#ifndef NLSR_NAME_MAP_HPP
#define NLSR_NAME_MAP_HPP

#include "common.hpp"
#include "lsa/adj-lsa.hpp"

#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/concept_check.hpp>

#include <optional>

namespace nlsr {

/**
* @brief Assigning numbers to router names.
*
* NameMap assigns a "mapping number" to each inserted router name. It then provides bidirectional
* lookups between router names and their mapping numbers.
*
* These numbers are non-negative integers assigned sequentially, starting from zero. They can
* support constructing a matrix of routers, where the mapping numbers are used as row and column
* indices in place of router names.
*/
class NameMap
{
public:
/**
* @brief Create a NameMap populated with router names in Adjacency LSAs.
* @tparam IteratorType A *LegacyInputIterator* whose value type is convertible to
* `std::shared_ptr<AdjLsa>`.
* @param first Range begin iterator.
* @param last Range past-end iterator. It must be reachable by incrementing @p first .
* @returns NameMap populated with origin and adjacent router names.
*/
template<typename IteratorType>
static NameMap
createFromAdjLsdb(IteratorType first, IteratorType last)
{
BOOST_CONCEPT_ASSERT((boost::InputIterator<IteratorType>));
NameMap map;
for (auto it = first; it != last; ++it) {
// *it has type std::shared_ptr<Lsa> ; it->get() has type Lsa*
auto lsa = static_cast<const AdjLsa*>(it->get());
map.addEntry(lsa->getOriginRouter());
for (const auto& adjacent : lsa->getAdl().getAdjList()) {
map.addEntry(adjacent.getName());
}
}
return map;
}

/**
* @brief Create a NameMap populated with router names in Coordinate LSAs.
* @tparam IteratorType A *LegacyInputIterator* whose value type is `std::shared_ptr<Lsa>`.
* @param first Range begin iterator.
* @param last Range past-end iterator. It must be reachable by incrementing @p first .
* @returns NameMap populated with origin router names.
*/
template<typename IteratorType>
static NameMap
createFromCoordinateLsdb(IteratorType first, IteratorType last)
{
BOOST_CONCEPT_ASSERT((boost::InputIterator<IteratorType>));
NameMap map;
for (auto it = first; it != last; ++it) {
map.addEntry((*it)->getOriginRouter());
}
return map;
}

/**
* @brief Insert a router name.
* @param rtrName Router name.
*/
void
addEntry(const ndn::Name& rtrName);

/**
* @brief Find router name by its mapping number.
* @param mn Mapping number.
* @returns Router name, or @c std::nullopt if it does not exist.
*/
std::optional<ndn::Name>
getRouterNameByMappingNo(int32_t mn) const;

/**
* @brief Find mapping number of a router name.
* @param rtrName Router name.
* @returns Mapping number, or @c std::nullopt if it does not exist.
*/
std::optional<int32_t>
getMappingNoByRouterName(const ndn::Name& rtrName) const;

/**
* @brief Return number of entries in this container.
* @returns Number of entries in this container.
*/
size_t
size() const
{
return m_bimap.size();
}

private:
struct MappingNo;
boost::bimap<
boost::bimaps::unordered_set_of<
boost::bimaps::tagged<ndn::Name, ndn::Name>,
std::hash<ndn::Name>
>,
boost::bimaps::unordered_set_of<
boost::bimaps::tagged<int32_t, MappingNo>
>
> m_bimap;

friend std::ostream&
operator<<(std::ostream& os, const NameMap& map);
};

std::ostream&
operator<<(std::ostream& os, const NameMap& map);

} // namespace nlsr

#endif // NLSR_NAME_MAP_HPP
12 changes: 6 additions & 6 deletions src/route/routing-table-calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "routing-table-calculator.hpp"
#include "lsdb.hpp"
#include "map.hpp"
#include "name-map.hpp"
#include "nexthop.hpp"
#include "nlsr.hpp"
#include "logger.hpp"
Expand Down Expand Up @@ -63,7 +63,7 @@ RoutingTableCalculator::initMatrix()
}

void
RoutingTableCalculator::makeAdjMatrix(const Lsdb& lsdb, Map& pMap)
RoutingTableCalculator::makeAdjMatrix(const Lsdb& lsdb, NameMap& pMap)
{
// For each LSA represented in the map
auto lsaRange = lsdb.getLsdbIterator<AdjLsa>();
Expand Down Expand Up @@ -120,7 +120,7 @@ RoutingTableCalculator::makeAdjMatrix(const Lsdb& lsdb, Map& pMap)
}

void
RoutingTableCalculator::writeAdjMatrixLog(const Map& map) const
RoutingTableCalculator::writeAdjMatrixLog(const NameMap& map) const
{
if (!ndn_cxx_getLogger().isLevelEnabled(ndn::util::LogLevel::DEBUG)) {
return;
Expand Down Expand Up @@ -232,7 +232,7 @@ RoutingTableCalculator::freeLinksCosts()
}

void
LinkStateRoutingTableCalculator::calculatePath(Map& pMap, RoutingTable& rt,
LinkStateRoutingTableCalculator::calculatePath(NameMap& pMap, RoutingTable& rt,
ConfParameter& confParam,
const Lsdb& lsdb)
{
Expand Down Expand Up @@ -327,7 +327,7 @@ LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter)

void
LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(AdjacencyList& adjacencies,
RoutingTable& rt, Map& pMap,
RoutingTable& rt, NameMap& pMap,
uint32_t sourceRouter)
{
NLSR_LOG_DEBUG("LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called");
Expand Down Expand Up @@ -426,7 +426,7 @@ void LinkStateRoutingTableCalculator::freeDistance()
}

void
HyperbolicRoutingCalculator::calculatePath(Map& map, RoutingTable& rt,
HyperbolicRoutingCalculator::calculatePath(NameMap& map, RoutingTable& rt,
Lsdb& lsdb, AdjacencyList& adjacencies)
{
NLSR_LOG_TRACE("Calculating hyperbolic paths");
Expand Down
Loading

0 comments on commit 4eb4eae

Please sign in to comment.