blob: 68e6b8180337701b49f90b207fd0eaeb77cff925 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04002/*
Junxiao Shi52f16642023-08-15 00:18:35 +00003 * Copyright (c) 2014-2023, 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/>.
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040020 */
alvy297f4162015-03-03 17:15:33 -060021
akmhoquefdbddb12014-05-02 18:35:19 -050022#ifndef NLSR_NAME_PREFIX_LIST_HPP
23#define NLSR_NAME_PREFIX_LIST_HPP
akmhoque53353462014-04-22 08:43:45 -050024
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080025#include "test-access-control.hpp"
26
Junxiao Shi52f16642023-08-15 00:18:35 +000027#include <ndn-cxx/name.hpp>
28
29#include <boost/operators.hpp>
30
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040031#include <initializer_list>
akmhoque53353462014-04-22 08:43:45 -050032#include <list>
Junxiao Shi52f16642023-08-15 00:18:35 +000033#include <map>
34#include <set>
akmhoque53353462014-04-22 08:43:45 -050035#include <string>
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040036#include <vector>
37
akmhoque53353462014-04-22 08:43:45 -050038namespace nlsr {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080039
Junxiao Shi52f16642023-08-15 00:18:35 +000040class NamePrefixList : private boost::equality_comparable<NamePrefixList>
akmhoque53353462014-04-22 08:43:45 -050041{
akmhoque53353462014-04-22 08:43:45 -050042public:
Nick Gordonf14ec352017-07-24 16:09:58 -050043 using NamePair = std::tuple<ndn::Name, std::vector<std::string>>;
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040044
Nick Gordonf14ec352017-07-24 16:09:58 -050045 enum NamePairIndex {
46 NAME,
47 SOURCES
48 };
49
akmhoquec8a10f72014-04-25 18:42:55 -050050 NamePrefixList();
akmhoque53353462014-04-22 08:43:45 -050051
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040052 explicit
53 NamePrefixList(ndn::span<const ndn::Name> names);
Nick Gordon96861ca2017-10-17 18:25:21 -050054
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040055#ifdef WITH_TESTS
56 NamePrefixList(std::initializer_list<ndn::Name> names)
57 : NamePrefixList(ndn::span<const ndn::Name>(names))
Nick Gordon96861ca2017-10-17 18:25:21 -050058 {
Nick Gordon96861ca2017-10-17 18:25:21 -050059 }
60
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040061 NamePrefixList(std::initializer_list<NamePair> namesAndSources)
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040062 {
Junxiao Shi52f16642023-08-15 00:18:35 +000063 for (const auto& np : namesAndSources) {
64 for (const auto& source : std::get<NamePrefixList::NamePairIndex::SOURCES>(np)) {
65 insert(std::get<NamePrefixList::NamePairIndex::NAME>(np), source);
66 }
67 }
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040068 }
69#endif
akmhoque53353462014-04-22 08:43:45 -050070
Nick G97e34942016-07-11 14:46:27 -050071 /*! \brief inserts name into NamePrefixList
72 \retval true If the name was successfully inserted.
73 \retval false If the name could not be inserted.
alvy297f4162015-03-03 17:15:33 -060074 */
75 bool
Nick Gordonf14ec352017-07-24 16:09:58 -050076 insert(const ndn::Name& name, const std::string& source = "");
akmhoque53353462014-04-22 08:43:45 -050077
Nick G97e34942016-07-11 14:46:27 -050078 /*! \brief removes name from NamePrefixList
79 \retval true If the name is removed
80 \retval false If the name failed to be removed.
alvy297f4162015-03-03 17:15:33 -060081 */
82 bool
Junxiao Shi52f16642023-08-15 00:18:35 +000083 erase(const ndn::Name& name, const std::string& source = "");
akmhoque53353462014-04-22 08:43:45 -050084
akmhoque31d1d4b2014-05-05 22:08:14 -050085 size_t
Nick Gordonff9a6272017-10-12 13:38:29 -050086 size() const
akmhoque53353462014-04-22 08:43:45 -050087 {
Junxiao Shi52f16642023-08-15 00:18:35 +000088 return m_namesSources.size();
akmhoque53353462014-04-22 08:43:45 -050089 }
90
Nick Gordonf14ec352017-07-24 16:09:58 -050091 std::list<ndn::Name>
92 getNames() const;
Nick Gordon56d1fae2017-05-26 16:39:25 -050093
Nick Gordonf14ec352017-07-24 16:09:58 -050094 /*! Returns how many unique sources this name has.
akmhoque53353462014-04-22 08:43:45 -050095
Nick Gordonf14ec352017-07-24 16:09:58 -050096 \retval 0 if the name is not in the list, else the number of sources.
97 */
98 uint32_t
99 countSources(const ndn::Name& name) const;
100
101 /*! Returns the sources that this name has.
102
103 \retval an empty vector if the name is not in the list, else a
104 vector containing the sources.
105 */
106 const std::vector<std::string>
107 getSources(const ndn::Name& name) const;
108
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800109 void
110 clear()
111 {
Junxiao Shi52f16642023-08-15 00:18:35 +0000112 m_namesSources.clear();
113 }
114
115private: // non-member operators
116 // NOTE: the following "hidden friend" operators are available via
117 // argument-dependent lookup only and must be defined inline.
118 // boost::equality_comparable provides != operators.
119
120 friend bool
121 operator==(const NamePrefixList& lhs, const NamePrefixList& rhs)
122 {
123 return lhs.getNames() == rhs.getNames();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800124 }
125
Nick Gordonf14ec352017-07-24 16:09:58 -0500126private:
Junxiao Shi52f16642023-08-15 00:18:35 +0000127 std::map<ndn::Name, std::set<std::string>> m_namesSources;
akmhoque53353462014-04-22 08:43:45 -0500128};
129
Nick Gordon56d1fae2017-05-26 16:39:25 -0500130std::ostream&
131operator<<(std::ostream& os, const NamePrefixList& list);
132
Nick Gordonfad8e252016-08-11 14:21:38 -0500133} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500134
Nick Gordon56d1fae2017-05-26 16:39:25 -0500135#endif // NLSR_NAME_PREFIX_LIST_HPP