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