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 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 22 | #include "name-prefix-list.hpp" |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 23 | #include "common.hpp" |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 24 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 25 | namespace nlsr { |
| 26 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 27 | NamePrefixList::NamePrefixList() = default; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 28 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 29 | NamePrefixList::NamePrefixList(std::initializer_list<ndn::Name> names) |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 30 | { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 31 | for (const auto& name : names) { |
| 32 | insert(name); |
| 33 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 34 | } |
| 35 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 36 | bool |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 37 | NamePrefixList::insert(const ndn::Name& name, const std::string& source) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 38 | { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 39 | auto& sources = m_namesSources[name]; |
| 40 | return sources.insert(source).second; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 41 | } |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 42 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 43 | bool |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 44 | NamePrefixList::erase(const ndn::Name& name, const std::string& source) |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 45 | { |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 46 | auto it = m_namesSources.find(name); |
| 47 | if (it == m_namesSources.end()) { |
| 48 | return false; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 49 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 50 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 51 | bool isRemoved = it->second.erase(source); |
| 52 | if (it->second.empty()) { |
| 53 | m_namesSources.erase(it); |
| 54 | } |
| 55 | return isRemoved; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | std::list<ndn::Name> |
| 59 | NamePrefixList::getNames() const |
| 60 | { |
| 61 | std::list<ndn::Name> names; |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 62 | for (const auto& [name, sources] : m_namesSources) { |
| 63 | names.push_back(name); |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 64 | } |
| 65 | return names; |
| 66 | } |
| 67 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 68 | #ifdef WITH_TESTS |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 69 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 70 | std::set<std::string> |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 71 | NamePrefixList::getSources(const ndn::Name& name) const |
| 72 | { |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 73 | if (auto it = m_namesSources.find(name); it != m_namesSources.end()) { |
| 74 | return it->second; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 75 | } |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 76 | return {}; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 81 | std::ostream& |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 82 | operator<<(std::ostream& os, const NamePrefixList& list) |
| 83 | { |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 84 | os << "Name prefix list: {\n"; |
Junxiao Shi | 931ca9f | 2023-08-15 02:59:46 +0000 | [diff] [blame] | 85 | for (const auto& [name, sources] : list.m_namesSources) { |
| 86 | os << name << "\nSources:\n"; |
| 87 | for (const auto& source : sources) { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 88 | os << " " << source << "\n"; |
| 89 | } |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 90 | } |
| 91 | os << "}" << std::endl; |
| 92 | return os; |
| 93 | } |
| 94 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 95 | } // namespace nlsr |