blob: 03238964a1c2f15f4b3f4b6ee9b53a34fd7394c0 [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
Junxiao Shi931ca9f2023-08-15 02:59:46 +000029NamePrefixList::NamePrefixList(std::initializer_list<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
Junxiao Shi931ca9f2023-08-15 02:59:46 +000068#ifdef WITH_TESTS
Nick Gordonf14ec352017-07-24 16:09:58 -050069
Junxiao Shi931ca9f2023-08-15 02:59:46 +000070std::set<std::string>
Nick Gordonf14ec352017-07-24 16:09:58 -050071NamePrefixList::getSources(const ndn::Name& name) const
72{
Junxiao Shi931ca9f2023-08-15 02:59:46 +000073 if (auto it = m_namesSources.find(name); it != m_namesSources.end()) {
74 return it->second;
Nick Gordonf14ec352017-07-24 16:09:58 -050075 }
Junxiao Shi931ca9f2023-08-15 02:59:46 +000076 return {};
akmhoque53353462014-04-22 08:43:45 -050077}
78
Junxiao Shi931ca9f2023-08-15 02:59:46 +000079#endif
80
Nick Gordon56d1fae2017-05-26 16:39:25 -050081std::ostream&
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040082operator<<(std::ostream& os, const NamePrefixList& list)
83{
Nick Gordon56d1fae2017-05-26 16:39:25 -050084 os << "Name prefix list: {\n";
Junxiao Shi931ca9f2023-08-15 02:59:46 +000085 for (const auto& [name, sources] : list.m_namesSources) {
86 os << name << "\nSources:\n";
87 for (const auto& source : sources) {
Nick Gordonf14ec352017-07-24 16:09:58 -050088 os << " " << source << "\n";
89 }
Nick Gordon56d1fae2017-05-26 16:39:25 -050090 }
91 os << "}" << std::endl;
92 return os;
93}
94
Nick Gordonfad8e252016-08-11 14:21:38 -050095} // namespace nlsr