blob: 486e350b9f7302dbb151702f184e48c6ea5c0414 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
alvy297f4162015-03-03 17:15:33 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
alvy297f4162015-03-03 17:15:33 -060021
Nick Gordonf14ec352017-07-24 16:09:58 -050022#include "name-prefix-list.hpp"
Nick Gordonf14ec352017-07-24 16:09:58 -050023#include "common.hpp"
24#include "logger.hpp"
25
akmhoque53353462014-04-22 08:43:45 -050026#include <iostream>
27#include <algorithm>
28
akmhoque53353462014-04-22 08:43:45 -050029namespace nlsr {
30
akmhoque674b0b12014-05-20 14:33:28 -050031INIT_LOGGER("NamePrefixList");
32
akmhoquec8a10f72014-04-25 18:42:55 -050033NamePrefixList::NamePrefixList()
akmhoque53353462014-04-22 08:43:45 -050034{
35}
36
Nick Gordon96861ca2017-10-17 18:25:21 -050037NamePrefixList::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
47NamePrefixList::NamePrefixList(const std::initializer_list<NamePrefixList::NamePair>& namesAndSources)
48 : m_names(namesAndSources)
49{
50}
51
akmhoquec8a10f72014-04-25 18:42:55 -050052NamePrefixList::~NamePrefixList()
akmhoque53353462014-04-22 08:43:45 -050053{
54}
55
Nick Gordonf14ec352017-07-24 16:09:58 -050056std::vector<NamePrefixList::NamePair>::iterator
57NamePrefixList::get(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050058{
Nick Gordonf14ec352017-07-24 16:09:58 -050059 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
65std::vector<std::string>::iterator
66NamePrefixList::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 });
akmhoque53353462014-04-22 08:43:45 -050073}
74
alvy297f4162015-03-03 17:15:33 -060075bool
Nick Gordonf14ec352017-07-24 16:09:58 -050076NamePrefixList::insert(const ndn::Name& name, const std::string& source)
akmhoque53353462014-04-22 08:43:45 -050077{
Nick Gordonf14ec352017-07-24 16:09:58 -050078 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));
alvy297f4162015-03-03 17:15:33 -060082 return true;
akmhoque53353462014-04-22 08:43:45 -050083 }
Nick Gordonf14ec352017-07-24 16:09:58 -050084 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}
alvy297f4162015-03-03 17:15:33 -060094
Nick Gordonf14ec352017-07-24 16:09:58 -050095bool
96NamePrefixList::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 }
alvy297f4162015-03-03 17:15:33 -0600110 return false;
akmhoque53353462014-04-22 08:43:45 -0500111}
112
Nick Gordon56d1fae2017-05-26 16:39:25 -0500113bool
114NamePrefixList::operator==(const NamePrefixList& other) const
115{
Nick Gordonf14ec352017-07-24 16:09:58 -0500116 return m_names == other.m_names;
Nick Gordon56d1fae2017-05-26 16:39:25 -0500117}
118
akmhoque53353462014-04-22 08:43:45 -0500119void
akmhoquec8a10f72014-04-25 18:42:55 -0500120NamePrefixList::sort()
akmhoque53353462014-04-22 08:43:45 -0500121{
Nick Gordonf14ec352017-07-24 16:09:58 -0500122 std::sort(m_names.begin(), m_names.end());
123}
124
125std::list<ndn::Name>
126NamePrefixList::getNames() const
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
135uint32_t
136NamePrefixList::countSources(const ndn::Name& name) const
137{
138 return getSources(name).size();
139}
140
141const std::vector<std::string>
142NamePrefixList::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 }
akmhoque53353462014-04-22 08:43:45 -0500154}
155
Nick Gordon56d1fae2017-05-26 16:39:25 -0500156std::ostream&
157operator<<(std::ostream& os, const NamePrefixList& list) {
158 os << "Name prefix list: {\n";
Nick Gordonf14ec352017-07-24 16:09:58 -0500159 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 }
Nick Gordon56d1fae2017-05-26 16:39:25 -0500165 }
166 os << "}" << std::endl;
167 return os;
168}
169
Nick Gordonfad8e252016-08-11 14:21:38 -0500170} // namespace nlsr