Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2024, 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_NAME_MAP_HPP |
| 22 | #define NLSR_NAME_MAP_HPP |
| 23 | |
| 24 | #include "common.hpp" |
| 25 | #include "lsa/adj-lsa.hpp" |
| 26 | |
| 27 | #include <boost/bimap.hpp> |
| 28 | #include <boost/bimap/unordered_set_of.hpp> |
| 29 | #include <boost/concept_check.hpp> |
| 30 | |
| 31 | #include <optional> |
| 32 | |
| 33 | namespace nlsr { |
| 34 | |
| 35 | /** |
| 36 | * @brief Assigning numbers to router names. |
| 37 | * |
| 38 | * NameMap assigns a "mapping number" to each inserted router name. It then provides bidirectional |
| 39 | * lookups between router names and their mapping numbers. |
| 40 | * |
| 41 | * These numbers are non-negative integers assigned sequentially, starting from zero. They can |
| 42 | * support constructing a matrix of routers, where the mapping numbers are used as row and column |
| 43 | * indices in place of router names. |
| 44 | */ |
| 45 | class NameMap |
| 46 | { |
| 47 | public: |
| 48 | /** |
| 49 | * @brief Create a NameMap populated with router names in Adjacency LSAs. |
| 50 | * @tparam IteratorType A *LegacyInputIterator* whose value type is convertible to |
| 51 | * `std::shared_ptr<AdjLsa>`. |
| 52 | * @param first Range begin iterator. |
| 53 | * @param last Range past-end iterator. It must be reachable by incrementing @p first . |
| 54 | * @returns NameMap populated with origin and adjacent router names. |
| 55 | */ |
| 56 | template<typename IteratorType> |
| 57 | static NameMap |
| 58 | createFromAdjLsdb(IteratorType first, IteratorType last) |
| 59 | { |
| 60 | BOOST_CONCEPT_ASSERT((boost::InputIterator<IteratorType>)); |
| 61 | NameMap map; |
| 62 | for (auto it = first; it != last; ++it) { |
| 63 | // *it has type std::shared_ptr<Lsa> ; it->get() has type Lsa* |
| 64 | auto lsa = static_cast<const AdjLsa*>(it->get()); |
| 65 | map.addEntry(lsa->getOriginRouter()); |
| 66 | for (const auto& adjacent : lsa->getAdl().getAdjList()) { |
| 67 | map.addEntry(adjacent.getName()); |
| 68 | } |
| 69 | } |
| 70 | return map; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @brief Create a NameMap populated with router names in Coordinate LSAs. |
| 75 | * @tparam IteratorType A *LegacyInputIterator* whose value type is `std::shared_ptr<Lsa>`. |
| 76 | * @param first Range begin iterator. |
| 77 | * @param last Range past-end iterator. It must be reachable by incrementing @p first . |
| 78 | * @returns NameMap populated with origin router names. |
| 79 | */ |
| 80 | template<typename IteratorType> |
| 81 | static NameMap |
| 82 | createFromCoordinateLsdb(IteratorType first, IteratorType last) |
| 83 | { |
| 84 | BOOST_CONCEPT_ASSERT((boost::InputIterator<IteratorType>)); |
| 85 | NameMap map; |
| 86 | for (auto it = first; it != last; ++it) { |
| 87 | map.addEntry((*it)->getOriginRouter()); |
| 88 | } |
| 89 | return map; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @brief Insert a router name. |
| 94 | * @param rtrName Router name. |
| 95 | */ |
| 96 | void |
| 97 | addEntry(const ndn::Name& rtrName); |
| 98 | |
| 99 | /** |
| 100 | * @brief Find router name by its mapping number. |
| 101 | * @param mn Mapping number. |
| 102 | * @returns Router name, or @c std::nullopt if it does not exist. |
| 103 | */ |
| 104 | std::optional<ndn::Name> |
| 105 | getRouterNameByMappingNo(int32_t mn) const; |
| 106 | |
| 107 | /** |
| 108 | * @brief Find mapping number of a router name. |
| 109 | * @param rtrName Router name. |
| 110 | * @returns Mapping number, or @c std::nullopt if it does not exist. |
| 111 | */ |
| 112 | std::optional<int32_t> |
| 113 | getMappingNoByRouterName(const ndn::Name& rtrName) const; |
| 114 | |
| 115 | /** |
| 116 | * @brief Return number of entries in this container. |
| 117 | * @returns Number of entries in this container. |
| 118 | */ |
| 119 | size_t |
| 120 | size() const |
| 121 | { |
| 122 | return m_bimap.size(); |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | struct MappingNo; |
| 127 | boost::bimap< |
| 128 | boost::bimaps::unordered_set_of< |
| 129 | boost::bimaps::tagged<ndn::Name, ndn::Name>, |
| 130 | std::hash<ndn::Name> |
| 131 | >, |
| 132 | boost::bimaps::unordered_set_of< |
| 133 | boost::bimaps::tagged<int32_t, MappingNo> |
| 134 | > |
| 135 | > m_bimap; |
| 136 | |
| 137 | friend std::ostream& |
| 138 | operator<<(std::ostream& os, const NameMap& map); |
| 139 | }; |
| 140 | |
| 141 | std::ostream& |
| 142 | operator<<(std::ostream& os, const NameMap& map); |
| 143 | |
| 144 | } // namespace nlsr |
| 145 | |
| 146 | #endif // NLSR_NAME_MAP_HPP |