akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, The University of Memphis, |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 21 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 22 | #ifndef NLSR_NAME_PREFIX_LIST_HPP |
| 23 | #define NLSR_NAME_PREFIX_LIST_HPP |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 25 | #include "test-access-control.hpp" |
| 26 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 27 | #include <list> |
| 28 | #include <string> |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/name.hpp> |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 30 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | namespace nlsr { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 32 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 33 | class NamePrefixList |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 34 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | public: |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 36 | using NamePair = std::tuple<ndn::Name, std::vector<std::string>>; |
| 37 | enum NamePairIndex { |
| 38 | NAME, |
| 39 | SOURCES |
| 40 | }; |
| 41 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 42 | NamePrefixList(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 43 | |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 44 | NamePrefixList(const std::initializer_list<ndn::Name>& names); |
| 45 | |
| 46 | NamePrefixList(const std::initializer_list<NamePrefixList::NamePair>& namesAndSources); |
| 47 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 48 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 49 | template<class ContainerType> |
| 50 | NamePrefixList(const ContainerType& names) |
| 51 | { |
| 52 | for (const auto& elem : names) { |
| 53 | m_names.push_back(NamePair{elem, {""}}); |
| 54 | } |
| 55 | } |
| 56 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 57 | public: |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 58 | ~NamePrefixList(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 59 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 60 | /*! \brief inserts name into NamePrefixList |
| 61 | \retval true If the name was successfully inserted. |
| 62 | \retval false If the name could not be inserted. |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 63 | */ |
| 64 | bool |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 65 | insert(const ndn::Name& name, const std::string& source = ""); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 67 | /*! \brief removes name from NamePrefixList |
| 68 | \retval true If the name is removed |
| 69 | \retval false If the name failed to be removed. |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 70 | */ |
| 71 | bool |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 72 | remove(const ndn::Name& name, const std::string& source = ""); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 73 | |
| 74 | void |
| 75 | sort(); |
| 76 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 77 | size_t |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 78 | size() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 79 | { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 80 | return m_names.size(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 83 | std::list<ndn::Name> |
| 84 | getNames() const; |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 85 | |
| 86 | bool |
| 87 | operator==(const NamePrefixList& other) const; |
| 88 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 89 | /*! Returns how many unique sources this name has. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 90 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 91 | \retval 0 if the name is not in the list, else the number of sources. |
| 92 | */ |
| 93 | uint32_t |
| 94 | countSources(const ndn::Name& name) const; |
| 95 | |
| 96 | /*! Returns the sources that this name has. |
| 97 | |
| 98 | \retval an empty vector if the name is not in the list, else a |
| 99 | vector containing the sources. |
| 100 | */ |
| 101 | const std::vector<std::string> |
| 102 | getSources(const ndn::Name& name) const; |
| 103 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 104 | void |
| 105 | clear() |
| 106 | { |
| 107 | m_names.clear(); |
| 108 | } |
| 109 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 110 | private: |
| 111 | /*! Obtain an iterator to the entry matching name. |
| 112 | |
| 113 | \note We could do this quite easily inline with a lambda, but this |
| 114 | is slightly more efficient. |
| 115 | */ |
| 116 | std::vector<NamePair>::iterator |
| 117 | get(const ndn::Name& name); |
| 118 | |
| 119 | /*! Obtain an iterator to a specific source in an entry |
| 120 | */ |
| 121 | std::vector<std::string>::iterator |
| 122 | getSource(const std::string& source, std::vector<NamePair>::iterator& entry); |
| 123 | |
| 124 | std::vector<NamePair> m_names; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 125 | }; |
| 126 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 127 | std::ostream& |
| 128 | operator<<(std::ostream& os, const NamePrefixList& list); |
| 129 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 130 | } // namespace nlsr |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 131 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 132 | #endif // NLSR_NAME_PREFIX_LIST_HPP |