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 #include <iostream>
26 #include <algorithm>
27 
28 namespace nlsr {
29 
31 
32 NamePrefixList::NamePrefixList(const std::initializer_list<ndn::Name>& names)
33 {
34  std::vector<NamePrefixList::NamePair> namePairs;
35  std::transform(names.begin(), names.end(), std::back_inserter(namePairs),
36  [] (const ndn::Name& name) {
37  return NamePrefixList::NamePair{name, {""}};
38  });
39  m_names = std::move(namePairs);
40 }
41 
42 NamePrefixList::NamePrefixList(const std::initializer_list<NamePrefixList::NamePair>& namesAndSources)
43  : m_names(namesAndSources)
44 {
45 }
46 
48 {
49 }
50 
51 std::vector<NamePrefixList::NamePair>::iterator
52 NamePrefixList::get(const ndn::Name& name)
53 {
54  return std::find_if(m_names.begin(), m_names.end(),
55  [&] (const NamePrefixList::NamePair& pair) {
56  return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair);
57  });
58 }
59 
60 std::vector<std::string>::iterator
61 NamePrefixList::getSource(const std::string& source, std::vector<NamePair>::iterator& entry)
62 {
63  return std::find_if(std::get<NamePairIndex::SOURCES>(*entry).begin(),
64  std::get<NamePairIndex::SOURCES>(*entry).end(),
65  [&] (const std::string& containerSource) {
66  return source == containerSource;
67  });
68 }
69 
70 bool
71 NamePrefixList::insert(const ndn::Name& name, const std::string& source)
72 {
73  auto pairItr = get(name);
74  if (pairItr == m_names.end()) {
75  std::vector<std::string> sources{source};
76  m_names.push_back(std::tie(name, sources));
77  return true;
78  }
79  else {
80  std::vector<std::string>& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
81  auto sourceItr = getSource(source, pairItr);
82  if (sourceItr == sources.end()) {
83  sources.push_back(source);
84  return true;
85  }
86  }
87  return false;
88 }
89 
90 bool
91 NamePrefixList::remove(const ndn::Name& name, const std::string& source)
92 {
93  auto pairItr = get(name);
94  if (pairItr != m_names.end()) {
95  std::vector<std::string>& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
96  auto sourceItr = getSource(source, pairItr);
97  if (sourceItr != sources.end()) {
98  sources.erase(sourceItr);
99  if (sources.size() == 0) {
100  m_names.erase(pairItr);
101  }
102  return true;
103  }
104  }
105  return false;
106 }
107 
108 bool
110 {
111  return m_names == other.m_names;
112 }
113 
114 void
116 {
117  std::sort(m_names.begin(), m_names.end());
118 }
119 
120 std::list<ndn::Name>
122 {
123  std::list<ndn::Name> names;
124  for (const auto& namePair : m_names) {
125  names.push_back(std::get<NamePrefixList::NamePairIndex::NAME>(namePair));
126  }
127  return names;
128 }
129 
130 uint32_t
131 NamePrefixList::countSources(const ndn::Name& name) const
132 {
133  return getSources(name).size();
134 }
135 
136 const std::vector<std::string>
137 NamePrefixList::getSources(const ndn::Name& name) const
138 {
139  auto it = std::find_if(m_names.begin(), m_names.end(),
140  [&] (const NamePrefixList::NamePair& pair) {
141  return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair);
142  });
143  if (it != m_names.end()) {
144  return std::get<NamePrefixList::NamePairIndex::SOURCES>(*it);
145  }
146  else {
147  return std::vector<std::string>{};
148  }
149 }
150 
151 std::ostream&
152 operator<<(std::ostream& os, const NamePrefixList& list) {
153  os << "Name prefix list: {\n";
154  for (const auto& name : list.getNames()) {
155  os << name << "\n"
156  << "Sources:\n";
157  for (const auto& source : list.getSources(name)) {
158  os << " " << source << "\n";
159  }
160  }
161  os << "}" << std::endl;
162  return os;
163 }
164 
165 } // namespace nlsr
bool remove(const ndn::Name &name, const std::string &source="")
removes name from NamePrefixList
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:99
std::list< ndn::Name > getNames() const
bool operator==(const NamePrefixList &other) const
std::tuple< ndn::Name, std::vector< std::string >> NamePair
bool insert(const ndn::Name &name, const std::string &source="")
inserts name into NamePrefixList
const std::vector< std::string > getSources(const ndn::Name &name) const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
uint32_t countSources(const ndn::Name &name) const