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-2022, 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(ndn::span<const ndn::Name> names)
30 {
31  std::transform(names.begin(), names.end(), std::back_inserter(m_names),
32  [] (const auto& name) { return NamePair{name, {""}}; });
33 }
34 
35 std::vector<NamePrefixList::NamePair>::iterator
36 NamePrefixList::get(const ndn::Name& name)
37 {
38  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 
44 std::vector<std::string>::iterator
45 NamePrefixList::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  });
52 }
53 
54 bool
55 NamePrefixList::insert(const ndn::Name& name, const std::string& source)
56 {
57  auto pairItr = get(name);
58  if (pairItr == m_names.end()) {
59  std::vector<std::string> sources{source};
60  m_names.emplace_back(name, sources);
61  return true;
62  }
63  else {
64  auto& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
65  auto sourceItr = getSource(source, pairItr);
66  if (sourceItr == sources.end()) {
67  sources.push_back(source);
68  return true;
69  }
70  }
71  return false;
72 }
73 
74 bool
75 NamePrefixList::remove(const ndn::Name& name, const std::string& source)
76 {
77  auto pairItr = get(name);
78  if (pairItr != m_names.end()) {
79  auto& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
80  auto sourceItr = getSource(source, pairItr);
81  if (sourceItr != sources.end()) {
82  sources.erase(sourceItr);
83  if (sources.empty()) {
84  m_names.erase(pairItr);
85  }
86  return true;
87  }
88  }
89  return false;
90 }
91 
92 bool
94 {
95  return m_names == other.m_names;
96 }
97 
98 void
99 NamePrefixList::sort()
100 {
101  std::sort(m_names.begin(), m_names.end());
102 }
103 
104 std::list<ndn::Name>
105 NamePrefixList::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 
114 uint32_t
115 NamePrefixList::countSources(const ndn::Name& name) const
116 {
117  return getSources(name).size();
118 }
119 
120 const std::vector<std::string>
121 NamePrefixList::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 {
131  return {};
132  }
133 }
134 
135 std::ostream&
136 operator<<(std::ostream& os, const NamePrefixList& list)
137 {
138  os << "Name prefix list: {\n";
139  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  }
145  }
146  os << "}" << std::endl;
147  return os;
148 }
149 
150 } // namespace nlsr
std::tuple< ndn::Name, std::vector< std::string > > NamePair
std::list< ndn::Name > getNames() const
const std::vector< std::string > getSources(const ndn::Name &name) const
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
std::ostream & operator<<(std::ostream &os, const NamePrefixList &list)
bool operator==(const NamePrefixTableEntry &lhs, const NamePrefixTableEntry &rhs)