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 | /* |
| 3 | * Copyright (c) 2014-2022, 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 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 29 | NamePrefixList::NamePrefixList(ndn::span<const ndn::Name> names) |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 30 | { |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 31 | std::transform(names.begin(), names.end(), std::back_inserter(m_names), |
| 32 | [] (const auto& name) { return NamePair{name, {""}}; }); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | } |
| 34 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 35 | std::vector<NamePrefixList::NamePair>::iterator |
| 36 | NamePrefixList::get(const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 38 | return std::find_if(m_names.begin(), m_names.end(), |
| 39 | [&] (const NamePrefixList::NamePair& pair) { |
| 40 | return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair); |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | std::vector<std::string>::iterator |
| 45 | NamePrefixList::getSource(const std::string& source, std::vector<NamePair>::iterator& entry) |
| 46 | { |
| 47 | return std::find_if(std::get<NamePairIndex::SOURCES>(*entry).begin(), |
| 48 | std::get<NamePairIndex::SOURCES>(*entry).end(), |
| 49 | [&] (const std::string& containerSource) { |
| 50 | return source == containerSource; |
| 51 | }); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | } |
| 53 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 54 | bool |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 55 | NamePrefixList::insert(const ndn::Name& name, const std::string& source) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 56 | { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 57 | auto pairItr = get(name); |
| 58 | if (pairItr == m_names.end()) { |
| 59 | std::vector<std::string> sources{source}; |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 60 | m_names.emplace_back(name, sources); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 61 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 62 | } |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 63 | else { |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 64 | auto& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr); |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 65 | auto sourceItr = getSource(source, pairItr); |
| 66 | if (sourceItr == sources.end()) { |
| 67 | sources.push_back(source); |
| 68 | return true; |
| 69 | } |
| 70 | } |
| 71 | return false; |
| 72 | } |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 73 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 74 | bool |
| 75 | NamePrefixList::remove(const ndn::Name& name, const std::string& source) |
| 76 | { |
| 77 | auto pairItr = get(name); |
| 78 | if (pairItr != m_names.end()) { |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 79 | auto& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr); |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 80 | auto sourceItr = getSource(source, pairItr); |
| 81 | if (sourceItr != sources.end()) { |
| 82 | sources.erase(sourceItr); |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 83 | if (sources.empty()) { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 84 | m_names.erase(pairItr); |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | } |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 89 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 92 | bool |
| 93 | NamePrefixList::operator==(const NamePrefixList& other) const |
| 94 | { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 95 | return m_names == other.m_names; |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 96 | } |
| 97 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 98 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 99 | NamePrefixList::sort() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 100 | { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 101 | std::sort(m_names.begin(), m_names.end()); |
| 102 | } |
| 103 | |
| 104 | std::list<ndn::Name> |
| 105 | NamePrefixList::getNames() const |
| 106 | { |
| 107 | std::list<ndn::Name> names; |
| 108 | for (const auto& namePair : m_names) { |
| 109 | names.push_back(std::get<NamePrefixList::NamePairIndex::NAME>(namePair)); |
| 110 | } |
| 111 | return names; |
| 112 | } |
| 113 | |
| 114 | uint32_t |
| 115 | NamePrefixList::countSources(const ndn::Name& name) const |
| 116 | { |
| 117 | return getSources(name).size(); |
| 118 | } |
| 119 | |
| 120 | const std::vector<std::string> |
| 121 | NamePrefixList::getSources(const ndn::Name& name) const |
| 122 | { |
| 123 | auto it = std::find_if(m_names.begin(), m_names.end(), |
| 124 | [&] (const NamePrefixList::NamePair& pair) { |
| 125 | return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair); |
| 126 | }); |
| 127 | if (it != m_names.end()) { |
| 128 | return std::get<NamePrefixList::NamePairIndex::SOURCES>(*it); |
| 129 | } |
| 130 | else { |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 131 | return {}; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 132 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 135 | std::ostream& |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 136 | operator<<(std::ostream& os, const NamePrefixList& list) |
| 137 | { |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 138 | os << "Name prefix list: {\n"; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 139 | for (const auto& name : list.getNames()) { |
| 140 | os << name << "\n" |
| 141 | << "Sources:\n"; |
| 142 | for (const auto& source : list.getSources(name)) { |
| 143 | os << " " << source << "\n"; |
| 144 | } |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 145 | } |
| 146 | os << "}" << std::endl; |
| 147 | return os; |
| 148 | } |
| 149 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 150 | } // namespace nlsr |