name-prefix-list.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2023, The University of Memphis,
4  * Regents of the University of California,
5  * Arizona Board of Regents.
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/>.
20  */
21 
22 #include "name-prefix-list.hpp"
23 #include "common.hpp"
24 
25 namespace nlsr {
26 
28 
29 NamePrefixList::NamePrefixList(std::initializer_list<ndn::Name> names)
30 {
31  for (const auto& name : names) {
32  insert(name);
33  }
34 }
35 
36 bool
37 NamePrefixList::insert(const ndn::Name& name, const std::string& source)
38 {
39  auto& sources = m_namesSources[name];
40  return sources.insert(source).second;
41 }
42 
43 bool
44 NamePrefixList::erase(const ndn::Name& name, const std::string& source)
45 {
46  auto it = m_namesSources.find(name);
47  if (it == m_namesSources.end()) {
48  return false;
49  }
50 
51  bool isRemoved = it->second.erase(source);
52  if (it->second.empty()) {
53  m_namesSources.erase(it);
54  }
55  return isRemoved;
56 }
57 
58 std::list<ndn::Name>
60 {
61  std::list<ndn::Name> names;
62  for (const auto& [name, sources] : m_namesSources) {
63  names.push_back(name);
64  }
65  return names;
66 }
67 
68 #ifdef WITH_TESTS
69 
70 std::set<std::string>
71 NamePrefixList::getSources(const ndn::Name& name) const
72 {
73  if (auto it = m_namesSources.find(name); it != m_namesSources.end()) {
74  return it->second;
75  }
76  return {};
77 }
78 
79 #endif
80 
81 std::ostream&
82 operator<<(std::ostream& os, const NamePrefixList& list)
83 {
84  os << "Name prefix list: {\n";
85  for (const auto& [name, sources] : list.m_namesSources) {
86  os << name << "\nSources:\n";
87  for (const auto& source : sources) {
88  os << " " << source << "\n";
89  }
90  }
91  os << "}" << std::endl;
92  return os;
93 }
94 
95 } // namespace nlsr
bool erase(const ndn::Name &name, const std::string &source="")
Deletes name and source combination.
bool insert(const ndn::Name &name, const std::string &source="")
Inserts name and source combination.
std::list< ndn::Name > getNames() const
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:176