blob: b5822c5f8058dfff6c9d118894dabcba71abe8da [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, 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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
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
Nick Gordon96861ca2017-10-17 18:25:21 -050029NamePrefixList::NamePrefixList(const std::initializer_list<ndn::Name>& names)
30{
31 std::vector<NamePrefixList::NamePair> namePairs;
32 std::transform(names.begin(), names.end(), std::back_inserter(namePairs),
33 [] (const ndn::Name& name) {
34 return NamePrefixList::NamePair{name, {""}};
35 });
36 m_names = std::move(namePairs);
37}
38
39NamePrefixList::NamePrefixList(const std::initializer_list<NamePrefixList::NamePair>& namesAndSources)
40 : m_names(namesAndSources)
41{
42}
43
akmhoquec8a10f72014-04-25 18:42:55 -050044NamePrefixList::~NamePrefixList()
akmhoque53353462014-04-22 08:43:45 -050045{
46}
47
Nick Gordonf14ec352017-07-24 16:09:58 -050048std::vector<NamePrefixList::NamePair>::iterator
49NamePrefixList::get(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050050{
Nick Gordonf14ec352017-07-24 16:09:58 -050051 return std::find_if(m_names.begin(), m_names.end(),
52 [&] (const NamePrefixList::NamePair& pair) {
53 return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair);
54 });
55}
56
57std::vector<std::string>::iterator
58NamePrefixList::getSource(const std::string& source, std::vector<NamePair>::iterator& entry)
59{
60 return std::find_if(std::get<NamePairIndex::SOURCES>(*entry).begin(),
61 std::get<NamePairIndex::SOURCES>(*entry).end(),
62 [&] (const std::string& containerSource) {
63 return source == containerSource;
64 });
akmhoque53353462014-04-22 08:43:45 -050065}
66
alvy297f4162015-03-03 17:15:33 -060067bool
Nick Gordonf14ec352017-07-24 16:09:58 -050068NamePrefixList::insert(const ndn::Name& name, const std::string& source)
akmhoque53353462014-04-22 08:43:45 -050069{
Nick Gordonf14ec352017-07-24 16:09:58 -050070 auto pairItr = get(name);
71 if (pairItr == m_names.end()) {
72 std::vector<std::string> sources{source};
73 m_names.push_back(std::tie(name, sources));
alvy297f4162015-03-03 17:15:33 -060074 return true;
akmhoque53353462014-04-22 08:43:45 -050075 }
Nick Gordonf14ec352017-07-24 16:09:58 -050076 else {
77 std::vector<std::string>& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
78 auto sourceItr = getSource(source, pairItr);
79 if (sourceItr == sources.end()) {
80 sources.push_back(source);
81 return true;
82 }
83 }
84 return false;
85}
alvy297f4162015-03-03 17:15:33 -060086
Nick Gordonf14ec352017-07-24 16:09:58 -050087bool
88NamePrefixList::remove(const ndn::Name& name, const std::string& source)
89{
90 auto pairItr = get(name);
91 if (pairItr != m_names.end()) {
92 std::vector<std::string>& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
93 auto sourceItr = getSource(source, pairItr);
94 if (sourceItr != sources.end()) {
95 sources.erase(sourceItr);
96 if (sources.size() == 0) {
97 m_names.erase(pairItr);
98 }
99 return true;
100 }
101 }
alvy297f4162015-03-03 17:15:33 -0600102 return false;
akmhoque53353462014-04-22 08:43:45 -0500103}
104
Nick Gordon56d1fae2017-05-26 16:39:25 -0500105bool
106NamePrefixList::operator==(const NamePrefixList& other) const
107{
Nick Gordonf14ec352017-07-24 16:09:58 -0500108 return m_names == other.m_names;
Nick Gordon56d1fae2017-05-26 16:39:25 -0500109}
110
akmhoque53353462014-04-22 08:43:45 -0500111void
akmhoquec8a10f72014-04-25 18:42:55 -0500112NamePrefixList::sort()
akmhoque53353462014-04-22 08:43:45 -0500113{
Nick Gordonf14ec352017-07-24 16:09:58 -0500114 std::sort(m_names.begin(), m_names.end());
115}
116
117std::list<ndn::Name>
118NamePrefixList::getNames() const
119{
120 std::list<ndn::Name> names;
121 for (const auto& namePair : m_names) {
122 names.push_back(std::get<NamePrefixList::NamePairIndex::NAME>(namePair));
123 }
124 return names;
125}
126
127uint32_t
128NamePrefixList::countSources(const ndn::Name& name) const
129{
130 return getSources(name).size();
131}
132
133const std::vector<std::string>
134NamePrefixList::getSources(const ndn::Name& name) const
135{
136 auto it = std::find_if(m_names.begin(), m_names.end(),
137 [&] (const NamePrefixList::NamePair& pair) {
138 return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair);
139 });
140 if (it != m_names.end()) {
141 return std::get<NamePrefixList::NamePairIndex::SOURCES>(*it);
142 }
143 else {
144 return std::vector<std::string>{};
145 }
akmhoque53353462014-04-22 08:43:45 -0500146}
147
Nick Gordon56d1fae2017-05-26 16:39:25 -0500148std::ostream&
149operator<<(std::ostream& os, const NamePrefixList& list) {
150 os << "Name prefix list: {\n";
Nick Gordonf14ec352017-07-24 16:09:58 -0500151 for (const auto& name : list.getNames()) {
152 os << name << "\n"
153 << "Sources:\n";
154 for (const auto& source : list.getSources(name)) {
155 os << " " << source << "\n";
156 }
Nick Gordon56d1fae2017-05-26 16:39:25 -0500157 }
158 os << "}" << std::endl;
159 return os;
160}
161
Nick Gordonfad8e252016-08-11 14:21:38 -0500162} // namespace nlsr