akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, The University of Memphis, |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 20 | */ |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 21 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 22 | #ifndef NLSR_NAME_PREFIX_LIST_HPP |
| 23 | #define NLSR_NAME_PREFIX_LIST_HPP |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 25 | #include "test-access-control.hpp" |
| 26 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 27 | #include <ndn-cxx/name.hpp> |
| 28 | |
| 29 | #include <boost/operators.hpp> |
| 30 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 31 | #include <initializer_list> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | #include <list> |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 33 | #include <map> |
| 34 | #include <set> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | #include <string> |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 36 | #include <vector> |
| 37 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 38 | namespace nlsr { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 39 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 40 | class NamePrefixList : private boost::equality_comparable<NamePrefixList> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 42 | public: |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 43 | using NamePair = std::tuple<ndn::Name, std::vector<std::string>>; |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 44 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 45 | enum NamePairIndex { |
| 46 | NAME, |
| 47 | SOURCES |
| 48 | }; |
| 49 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 50 | NamePrefixList(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 51 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 52 | explicit |
| 53 | NamePrefixList(ndn::span<const ndn::Name> names); |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 54 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 55 | #ifdef WITH_TESTS |
| 56 | NamePrefixList(std::initializer_list<ndn::Name> names) |
| 57 | : NamePrefixList(ndn::span<const ndn::Name>(names)) |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 58 | { |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 61 | NamePrefixList(std::initializer_list<NamePair> namesAndSources) |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 62 | { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 63 | for (const auto& np : namesAndSources) { |
| 64 | for (const auto& source : std::get<NamePrefixList::NamePairIndex::SOURCES>(np)) { |
| 65 | insert(std::get<NamePrefixList::NamePairIndex::NAME>(np), source); |
| 66 | } |
| 67 | } |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 68 | } |
| 69 | #endif |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 71 | /*! \brief inserts name into NamePrefixList |
| 72 | \retval true If the name was successfully inserted. |
| 73 | \retval false If the name could not be inserted. |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 74 | */ |
| 75 | bool |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 76 | insert(const ndn::Name& name, const std::string& source = ""); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 78 | /*! \brief removes name from NamePrefixList |
| 79 | \retval true If the name is removed |
| 80 | \retval false If the name failed to be removed. |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 81 | */ |
| 82 | bool |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 83 | erase(const ndn::Name& name, const std::string& source = ""); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 84 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 85 | size_t |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 86 | size() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 87 | { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 88 | return m_namesSources.size(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 91 | std::list<ndn::Name> |
| 92 | getNames() const; |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 93 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 94 | /*! Returns how many unique sources this name has. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 96 | \retval 0 if the name is not in the list, else the number of sources. |
| 97 | */ |
| 98 | uint32_t |
| 99 | countSources(const ndn::Name& name) const; |
| 100 | |
| 101 | /*! Returns the sources that this name has. |
| 102 | |
| 103 | \retval an empty vector if the name is not in the list, else a |
| 104 | vector containing the sources. |
| 105 | */ |
| 106 | const std::vector<std::string> |
| 107 | getSources(const ndn::Name& name) const; |
| 108 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 109 | void |
| 110 | clear() |
| 111 | { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 112 | m_namesSources.clear(); |
| 113 | } |
| 114 | |
| 115 | private: // non-member operators |
| 116 | // NOTE: the following "hidden friend" operators are available via |
| 117 | // argument-dependent lookup only and must be defined inline. |
| 118 | // boost::equality_comparable provides != operators. |
| 119 | |
| 120 | friend bool |
| 121 | operator==(const NamePrefixList& lhs, const NamePrefixList& rhs) |
| 122 | { |
| 123 | return lhs.getNames() == rhs.getNames(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 126 | private: |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 127 | std::map<ndn::Name, std::set<std::string>> m_namesSources; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 128 | }; |
| 129 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 130 | std::ostream& |
| 131 | operator<<(std::ostream& os, const NamePrefixList& list); |
| 132 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 133 | } // namespace nlsr |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 134 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 135 | #endif // NLSR_NAME_PREFIX_LIST_HPP |