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