blob: 0bbcbaae42c45a0be823351f0d9262731420f0dd [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
akmhoque53353462014-04-22 08:43:45 -050022#include <iostream>
23#include <algorithm>
24
akmhoquec8a10f72014-04-25 18:42:55 -050025#include <ndn-cxx/common.hpp>
26
Vince Lehman0a7da612014-10-29 14:39:29 -050027#include "common.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050028#include "name-prefix-list.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050029#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050030
31namespace nlsr {
32
akmhoque674b0b12014-05-20 14:33:28 -050033INIT_LOGGER("NamePrefixList");
34
akmhoque53353462014-04-22 08:43:45 -050035using namespace std;
36
akmhoquec8a10f72014-04-25 18:42:55 -050037NamePrefixList::NamePrefixList()
akmhoque53353462014-04-22 08:43:45 -050038{
39}
40
akmhoquec8a10f72014-04-25 18:42:55 -050041NamePrefixList::~NamePrefixList()
akmhoque53353462014-04-22 08:43:45 -050042{
43}
44
45static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050046nameCompare(const ndn::Name& name1, const ndn::Name& name2)
akmhoque53353462014-04-22 08:43:45 -050047{
akmhoque31d1d4b2014-05-05 22:08:14 -050048 return name1 == name2;
akmhoque53353462014-04-22 08:43:45 -050049}
50
alvy297f4162015-03-03 17:15:33 -060051bool
akmhoque31d1d4b2014-05-05 22:08:14 -050052NamePrefixList::insert(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050053{
akmhoque31d1d4b2014-05-05 22:08:14 -050054 std::list<ndn::Name>::iterator it = std::find_if(m_nameList.begin(),
55 m_nameList.end(),
dmcoomes9f936662017-03-02 10:33:09 -060056 std::bind(&nameCompare, _1 ,
57 std::cref(name)));
akmhoque157b0a42014-05-13 00:26:37 -050058 if (it != m_nameList.end()) {
alvy297f4162015-03-03 17:15:33 -060059 return false;
akmhoque53353462014-04-22 08:43:45 -050060 }
61 m_nameList.push_back(name);
alvy297f4162015-03-03 17:15:33 -060062 return true;
akmhoque53353462014-04-22 08:43:45 -050063}
64
alvy297f4162015-03-03 17:15:33 -060065bool
akmhoque31d1d4b2014-05-05 22:08:14 -050066NamePrefixList::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050067{
akmhoque31d1d4b2014-05-05 22:08:14 -050068 std::list<ndn::Name>::iterator it = std::find_if(m_nameList.begin(),
69 m_nameList.end(),
dmcoomes9f936662017-03-02 10:33:09 -060070 std::bind(&nameCompare, _1 ,
71 std::cref(name)));
akmhoque157b0a42014-05-13 00:26:37 -050072 if (it != m_nameList.end()) {
akmhoque53353462014-04-22 08:43:45 -050073 m_nameList.erase(it);
alvy297f4162015-03-03 17:15:33 -060074 return true;
akmhoque53353462014-04-22 08:43:45 -050075 }
alvy297f4162015-03-03 17:15:33 -060076
77 return false;
akmhoque53353462014-04-22 08:43:45 -050078}
79
Nick Gordon56d1fae2017-05-26 16:39:25 -050080bool
81NamePrefixList::operator==(const NamePrefixList& other) const
82{
83 return m_nameList == other.getNameList();
84}
85
akmhoque53353462014-04-22 08:43:45 -050086void
akmhoquec8a10f72014-04-25 18:42:55 -050087NamePrefixList::sort()
akmhoque53353462014-04-22 08:43:45 -050088{
89 m_nameList.sort();
90}
91
Nick Gordon56d1fae2017-05-26 16:39:25 -050092std::ostream&
93operator<<(std::ostream& os, const NamePrefixList& list) {
94 os << "Name prefix list: {\n";
95 for (const auto& name : list.getNameList()) {
96 os << name << "\n";
97 }
98 os << "}" << std::endl;
99 return os;
100}
101
Nick Gordonfad8e252016-08-11 14:21:38 -0500102} // namespace nlsr