blob: c858c96d3c1fc1a060f40fae09fb4dae09b317bb [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/*
3 * Copyright (c) 2014-2022, 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{
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040031 std::transform(names.begin(), names.end(), std::back_inserter(m_names),
32 [] (const auto& name) { return NamePair{name, {""}}; });
akmhoque53353462014-04-22 08:43:45 -050033}
34
Nick Gordonf14ec352017-07-24 16:09:58 -050035std::vector<NamePrefixList::NamePair>::iterator
36NamePrefixList::get(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050037{
Nick Gordonf14ec352017-07-24 16:09:58 -050038 return std::find_if(m_names.begin(), m_names.end(),
39 [&] (const NamePrefixList::NamePair& pair) {
40 return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair);
41 });
42}
43
44std::vector<std::string>::iterator
45NamePrefixList::getSource(const std::string& source, std::vector<NamePair>::iterator& entry)
46{
47 return std::find_if(std::get<NamePairIndex::SOURCES>(*entry).begin(),
48 std::get<NamePairIndex::SOURCES>(*entry).end(),
49 [&] (const std::string& containerSource) {
50 return source == containerSource;
51 });
akmhoque53353462014-04-22 08:43:45 -050052}
53
alvy297f4162015-03-03 17:15:33 -060054bool
Nick Gordonf14ec352017-07-24 16:09:58 -050055NamePrefixList::insert(const ndn::Name& name, const std::string& source)
akmhoque53353462014-04-22 08:43:45 -050056{
Nick Gordonf14ec352017-07-24 16:09:58 -050057 auto pairItr = get(name);
58 if (pairItr == m_names.end()) {
59 std::vector<std::string> sources{source};
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040060 m_names.emplace_back(name, sources);
alvy297f4162015-03-03 17:15:33 -060061 return true;
akmhoque53353462014-04-22 08:43:45 -050062 }
Nick Gordonf14ec352017-07-24 16:09:58 -050063 else {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040064 auto& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
Nick Gordonf14ec352017-07-24 16:09:58 -050065 auto sourceItr = getSource(source, pairItr);
66 if (sourceItr == sources.end()) {
67 sources.push_back(source);
68 return true;
69 }
70 }
71 return false;
72}
alvy297f4162015-03-03 17:15:33 -060073
Nick Gordonf14ec352017-07-24 16:09:58 -050074bool
75NamePrefixList::remove(const ndn::Name& name, const std::string& source)
76{
77 auto pairItr = get(name);
78 if (pairItr != m_names.end()) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040079 auto& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
Nick Gordonf14ec352017-07-24 16:09:58 -050080 auto sourceItr = getSource(source, pairItr);
81 if (sourceItr != sources.end()) {
82 sources.erase(sourceItr);
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040083 if (sources.empty()) {
Nick Gordonf14ec352017-07-24 16:09:58 -050084 m_names.erase(pairItr);
85 }
86 return true;
87 }
88 }
alvy297f4162015-03-03 17:15:33 -060089 return false;
akmhoque53353462014-04-22 08:43:45 -050090}
91
Nick Gordon56d1fae2017-05-26 16:39:25 -050092bool
93NamePrefixList::operator==(const NamePrefixList& other) const
94{
Nick Gordonf14ec352017-07-24 16:09:58 -050095 return m_names == other.m_names;
Nick Gordon56d1fae2017-05-26 16:39:25 -050096}
97
akmhoque53353462014-04-22 08:43:45 -050098void
akmhoquec8a10f72014-04-25 18:42:55 -050099NamePrefixList::sort()
akmhoque53353462014-04-22 08:43:45 -0500100{
Nick Gordonf14ec352017-07-24 16:09:58 -0500101 std::sort(m_names.begin(), m_names.end());
102}
103
104std::list<ndn::Name>
105NamePrefixList::getNames() const
106{
107 std::list<ndn::Name> names;
108 for (const auto& namePair : m_names) {
109 names.push_back(std::get<NamePrefixList::NamePairIndex::NAME>(namePair));
110 }
111 return names;
112}
113
114uint32_t
115NamePrefixList::countSources(const ndn::Name& name) const
116{
117 return getSources(name).size();
118}
119
120const std::vector<std::string>
121NamePrefixList::getSources(const ndn::Name& name) const
122{
123 auto it = std::find_if(m_names.begin(), m_names.end(),
124 [&] (const NamePrefixList::NamePair& pair) {
125 return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair);
126 });
127 if (it != m_names.end()) {
128 return std::get<NamePrefixList::NamePairIndex::SOURCES>(*it);
129 }
130 else {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400131 return {};
Nick Gordonf14ec352017-07-24 16:09:58 -0500132 }
akmhoque53353462014-04-22 08:43:45 -0500133}
134
Nick Gordon56d1fae2017-05-26 16:39:25 -0500135std::ostream&
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400136operator<<(std::ostream& os, const NamePrefixList& list)
137{
Nick Gordon56d1fae2017-05-26 16:39:25 -0500138 os << "Name prefix list: {\n";
Nick Gordonf14ec352017-07-24 16:09:58 -0500139 for (const auto& name : list.getNames()) {
140 os << name << "\n"
141 << "Sources:\n";
142 for (const auto& source : list.getSources(name)) {
143 os << " " << source << "\n";
144 }
Nick Gordon56d1fae2017-05-26 16:39:25 -0500145 }
146 os << "}" << std::endl;
147 return os;
148}
149
Nick Gordonfad8e252016-08-11 14:21:38 -0500150} // namespace nlsr