name-prefix-list.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "name-prefix-list.hpp"
23 #include "common.hpp"
24 
25 namespace nlsr {
26 
28 
29 NamePrefixList::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 
39 NamePrefixList::NamePrefixList(const std::initializer_list<NamePrefixList::NamePair>& namesAndSources)
40  : m_names(namesAndSources)
41 {
42 }
43 
45 {
46 }
47 
48 std::vector<NamePrefixList::NamePair>::iterator
49 NamePrefixList::get(const ndn::Name& name)
50 {
51  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 
57 std::vector<std::string>::iterator
58 NamePrefixList::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  });
65 }
66 
67 bool
68 NamePrefixList::insert(const ndn::Name& name, const std::string& source)
69 {
70  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));
74  return true;
75  }
76  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 }
86 
87 bool
88 NamePrefixList::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  }
102  return false;
103 }
104 
105 bool
107 {
108  return m_names == other.m_names;
109 }
110 
111 void
113 {
114  std::sort(m_names.begin(), m_names.end());
115 }
116 
117 std::list<ndn::Name>
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 
127 uint32_t
128 NamePrefixList::countSources(const ndn::Name& name) const
129 {
130  return getSources(name).size();
131 }
132 
133 const std::vector<std::string>
134 NamePrefixList::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  }
146 }
147 
148 std::ostream&
149 operator<<(std::ostream& os, const NamePrefixList& list) {
150  os << "Name prefix list: {\n";
151  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  }
157  }
158  os << "}" << std::endl;
159  return os;
160 }
161 
162 } // namespace nlsr
bool operator==(const NamePrefixList &other) const
bool remove(const ndn::Name &name, const std::string &source="")
removes name from NamePrefixList
bool insert(const ndn::Name &name, const std::string &source="")
inserts name into NamePrefixList
std::tuple< ndn::Name, std::vector< std::string > > NamePair
uint32_t countSources(const ndn::Name &name) const
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 Adjacent &adjacent)
Definition: adjacent.cpp:179