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