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