blob: 26f54d41708849c52b418b425f5583e8e779bbbb [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, 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"
Nick Gordonf14ec352017-07-24 16:09:58 -050024
akmhoque53353462014-04-22 08:43:45 -050025#include <iostream>
26#include <algorithm>
27
akmhoque53353462014-04-22 08:43:45 -050028namespace nlsr {
29
dmcoomescf8d0ed2017-02-21 11:39:01 -060030NamePrefixList::NamePrefixList() = default;
akmhoque53353462014-04-22 08:43:45 -050031
Nick Gordon96861ca2017-10-17 18:25:21 -050032NamePrefixList::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
42NamePrefixList::NamePrefixList(const std::initializer_list<NamePrefixList::NamePair>& namesAndSources)
43 : m_names(namesAndSources)
44{
45}
46
akmhoquec8a10f72014-04-25 18:42:55 -050047NamePrefixList::~NamePrefixList()
akmhoque53353462014-04-22 08:43:45 -050048{
49}
50
Nick Gordonf14ec352017-07-24 16:09:58 -050051std::vector<NamePrefixList::NamePair>::iterator
52NamePrefixList::get(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050053{
Nick Gordonf14ec352017-07-24 16:09:58 -050054 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
60std::vector<std::string>::iterator
61NamePrefixList::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 });
akmhoque53353462014-04-22 08:43:45 -050068}
69
alvy297f4162015-03-03 17:15:33 -060070bool
Nick Gordonf14ec352017-07-24 16:09:58 -050071NamePrefixList::insert(const ndn::Name& name, const std::string& source)
akmhoque53353462014-04-22 08:43:45 -050072{
Nick Gordonf14ec352017-07-24 16:09:58 -050073 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));
alvy297f4162015-03-03 17:15:33 -060077 return true;
akmhoque53353462014-04-22 08:43:45 -050078 }
Nick Gordonf14ec352017-07-24 16:09:58 -050079 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}
alvy297f4162015-03-03 17:15:33 -060089
Nick Gordonf14ec352017-07-24 16:09:58 -050090bool
91NamePrefixList::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 }
alvy297f4162015-03-03 17:15:33 -0600105 return false;
akmhoque53353462014-04-22 08:43:45 -0500106}
107
Nick Gordon56d1fae2017-05-26 16:39:25 -0500108bool
109NamePrefixList::operator==(const NamePrefixList& other) const
110{
Nick Gordonf14ec352017-07-24 16:09:58 -0500111 return m_names == other.m_names;
Nick Gordon56d1fae2017-05-26 16:39:25 -0500112}
113
akmhoque53353462014-04-22 08:43:45 -0500114void
akmhoquec8a10f72014-04-25 18:42:55 -0500115NamePrefixList::sort()
akmhoque53353462014-04-22 08:43:45 -0500116{
Nick Gordonf14ec352017-07-24 16:09:58 -0500117 std::sort(m_names.begin(), m_names.end());
118}
119
120std::list<ndn::Name>
121NamePrefixList::getNames() const
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
130uint32_t
131NamePrefixList::countSources(const ndn::Name& name) const
132{
133 return getSources(name).size();
134}
135
136const std::vector<std::string>
137NamePrefixList::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 }
akmhoque53353462014-04-22 08:43:45 -0500149}
150
Nick Gordon56d1fae2017-05-26 16:39:25 -0500151std::ostream&
152operator<<(std::ostream& os, const NamePrefixList& list) {
153 os << "Name prefix list: {\n";
Nick Gordonf14ec352017-07-24 16:09:58 -0500154 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 }
Nick Gordon56d1fae2017-05-26 16:39:25 -0500160 }
161 os << "}" << std::endl;
162 return os;
163}
164
Nick Gordonfad8e252016-08-11 14:21:38 -0500165} // namespace nlsr