blob: 0912fb3ea32aa65cdedaaf56a28b7c69f1bd5e76 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonc6a85222017-01-03 16:54:34 -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
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
25#include <list>
26#include <string>
akmhoquefdbddb12014-05-02 18:35:19 -050027#include <boost/cstdint.hpp>
akmhoque31d1d4b2014-05-05 22:08:14 -050028#include <ndn-cxx/name.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -050029
akmhoque53353462014-04-22 08:43:45 -050030
31namespace nlsr {
akmhoquec8a10f72014-04-25 18:42:55 -050032class NamePrefixList
akmhoque53353462014-04-22 08:43:45 -050033{
akmhoque53353462014-04-22 08:43:45 -050034public:
Nick Gordonf14ec352017-07-24 16:09:58 -050035 using NamePair = std::tuple<ndn::Name, std::vector<std::string>>;
36 enum NamePairIndex {
37 NAME,
38 SOURCES
39 };
40
akmhoquec8a10f72014-04-25 18:42:55 -050041 NamePrefixList();
akmhoque53353462014-04-22 08:43:45 -050042
Nick Gordon96861ca2017-10-17 18:25:21 -050043 NamePrefixList(const std::initializer_list<ndn::Name>& names);
44
45 NamePrefixList(const std::initializer_list<NamePrefixList::NamePair>& namesAndSources);
46
47 template<class ContainerType>
48 NamePrefixList(const ContainerType& names)
49 {
50 for (const auto& elem : names) {
51 m_names.push_back(NamePair{elem, {""}});
52 }
53 }
54
akmhoquec8a10f72014-04-25 18:42:55 -050055 ~NamePrefixList();
akmhoque53353462014-04-22 08:43:45 -050056
Nick G97e34942016-07-11 14:46:27 -050057 /*! \brief inserts name into NamePrefixList
58 \retval true If the name was successfully inserted.
59 \retval false If the name could not be inserted.
alvy297f4162015-03-03 17:15:33 -060060 */
61 bool
Nick Gordonf14ec352017-07-24 16:09:58 -050062 insert(const ndn::Name& name, const std::string& source = "");
akmhoque53353462014-04-22 08:43:45 -050063
Nick G97e34942016-07-11 14:46:27 -050064 /*! \brief removes name from NamePrefixList
65 \retval true If the name is removed
66 \retval false If the name failed to be removed.
alvy297f4162015-03-03 17:15:33 -060067 */
68 bool
Nick Gordonf14ec352017-07-24 16:09:58 -050069 remove(const ndn::Name& name, const std::string& source = "");
akmhoque53353462014-04-22 08:43:45 -050070
71 void
72 sort();
73
akmhoque31d1d4b2014-05-05 22:08:14 -050074 size_t
Nick Gordonff9a6272017-10-12 13:38:29 -050075 size() const
akmhoque53353462014-04-22 08:43:45 -050076 {
Nick Gordonf14ec352017-07-24 16:09:58 -050077 return m_names.size();
akmhoque53353462014-04-22 08:43:45 -050078 }
79
Nick Gordonf14ec352017-07-24 16:09:58 -050080 std::list<ndn::Name>
81 getNames() const;
Nick Gordon56d1fae2017-05-26 16:39:25 -050082
83 bool
84 operator==(const NamePrefixList& other) const;
85
Nick Gordonf14ec352017-07-24 16:09:58 -050086 /*! Returns how many unique sources this name has.
akmhoque53353462014-04-22 08:43:45 -050087
Nick Gordonf14ec352017-07-24 16:09:58 -050088 \retval 0 if the name is not in the list, else the number of sources.
89 */
90 uint32_t
91 countSources(const ndn::Name& name) const;
92
93 /*! Returns the sources that this name has.
94
95 \retval an empty vector if the name is not in the list, else a
96 vector containing the sources.
97 */
98 const std::vector<std::string>
99 getSources(const ndn::Name& name) const;
100
101private:
102 /*! Obtain an iterator to the entry matching name.
103
104 \note We could do this quite easily inline with a lambda, but this
105 is slightly more efficient.
106 */
107 std::vector<NamePair>::iterator
108 get(const ndn::Name& name);
109
110 /*! Obtain an iterator to a specific source in an entry
111 */
112 std::vector<std::string>::iterator
113 getSource(const std::string& source, std::vector<NamePair>::iterator& entry);
114
115 std::vector<NamePair> m_names;
akmhoque53353462014-04-22 08:43:45 -0500116};
117
Nick Gordon56d1fae2017-05-26 16:39:25 -0500118std::ostream&
119operator<<(std::ostream& os, const NamePrefixList& list);
120
Nick Gordonfad8e252016-08-11 14:21:38 -0500121} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500122
Nick Gordon56d1fae2017-05-26 16:39:25 -0500123#endif // NLSR_NAME_PREFIX_LIST_HPP