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