blob: 5af70620a1913ea17bed34086a8ed3417b573550 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04002/*
Junxiao Shi52f16642023-08-15 00:18:35 +00003 * Copyright (c) 2014-2023, The University of Memphis,
alvy297f4162015-03-03 17:15:33 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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 Pesaventoc1d0e8e2022-06-15 14:26:02 -040020 */
alvy297f4162015-03-03 17:15:33 -060021
Nick Gordonf14ec352017-07-24 16:09:58 -050022#include "name-prefix-list.hpp"
Nick Gordonf14ec352017-07-24 16:09:58 -050023#include "common.hpp"
Nick Gordonf14ec352017-07-24 16:09:58 -050024
akmhoque53353462014-04-22 08:43:45 -050025namespace nlsr {
26
dmcoomescf8d0ed2017-02-21 11:39:01 -060027NamePrefixList::NamePrefixList() = default;
akmhoque53353462014-04-22 08:43:45 -050028
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040029NamePrefixList::NamePrefixList(ndn::span<const ndn::Name> names)
Nick Gordon96861ca2017-10-17 18:25:21 -050030{
Junxiao Shi52f16642023-08-15 00:18:35 +000031 for (const auto& name : names) {
32 insert(name);
33 }
akmhoque53353462014-04-22 08:43:45 -050034}
35
alvy297f4162015-03-03 17:15:33 -060036bool
Nick Gordonf14ec352017-07-24 16:09:58 -050037NamePrefixList::insert(const ndn::Name& name, const std::string& source)
akmhoque53353462014-04-22 08:43:45 -050038{
Junxiao Shi52f16642023-08-15 00:18:35 +000039 auto& sources = m_namesSources[name];
40 return sources.insert(source).second;
Nick Gordonf14ec352017-07-24 16:09:58 -050041}
alvy297f4162015-03-03 17:15:33 -060042
Nick Gordonf14ec352017-07-24 16:09:58 -050043bool
Junxiao Shi52f16642023-08-15 00:18:35 +000044NamePrefixList::erase(const ndn::Name& name, const std::string& source)
Nick Gordonf14ec352017-07-24 16:09:58 -050045{
Junxiao Shi52f16642023-08-15 00:18:35 +000046 auto it = m_namesSources.find(name);
47 if (it == m_namesSources.end()) {
48 return false;
Nick Gordonf14ec352017-07-24 16:09:58 -050049 }
akmhoque53353462014-04-22 08:43:45 -050050
Junxiao Shi52f16642023-08-15 00:18:35 +000051 bool isRemoved = it->second.erase(source);
52 if (it->second.empty()) {
53 m_namesSources.erase(it);
54 }
55 return isRemoved;
Nick Gordonf14ec352017-07-24 16:09:58 -050056}
57
58std::list<ndn::Name>
59NamePrefixList::getNames() const
60{
61 std::list<ndn::Name> names;
Junxiao Shi52f16642023-08-15 00:18:35 +000062 for (const auto& [name, sources] : m_namesSources) {
63 names.push_back(name);
Nick Gordonf14ec352017-07-24 16:09:58 -050064 }
65 return names;
66}
67
68uint32_t
69NamePrefixList::countSources(const ndn::Name& name) const
70{
71 return getSources(name).size();
72}
73
74const std::vector<std::string>
75NamePrefixList::getSources(const ndn::Name& name) const
76{
Junxiao Shi52f16642023-08-15 00:18:35 +000077 auto it = m_namesSources.find(name);
78 if (it == m_namesSources.end()) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040079 return {};
Nick Gordonf14ec352017-07-24 16:09:58 -050080 }
Junxiao Shi52f16642023-08-15 00:18:35 +000081
82 std::vector<std::string> result;
83 for (const auto& source : it->second) {
84 result.push_back(source);
85 }
86 return result;
akmhoque53353462014-04-22 08:43:45 -050087}
88
Nick Gordon56d1fae2017-05-26 16:39:25 -050089std::ostream&
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040090operator<<(std::ostream& os, const NamePrefixList& list)
91{
Nick Gordon56d1fae2017-05-26 16:39:25 -050092 os << "Name prefix list: {\n";
Nick Gordonf14ec352017-07-24 16:09:58 -050093 for (const auto& name : list.getNames()) {
94 os << name << "\n"
95 << "Sources:\n";
96 for (const auto& source : list.getSources(name)) {
97 os << " " << source << "\n";
98 }
Nick Gordon56d1fae2017-05-26 16:39:25 -050099 }
100 os << "}" << std::endl;
101 return os;
102}
103
Nick Gordonfad8e252016-08-11 14:21:38 -0500104} // namespace nlsr