Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 24 | #include "common.hpp" |
| 25 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 26 | #include "exclude.hpp" |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 28 | namespace ndn { |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 30 | Exclude::Exclude() |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 31 | { |
| 32 | } |
| 33 | |
| 34 | // example: ANY /b /d ANY /f |
| 35 | // |
| 36 | // ordered in map as: |
| 37 | // |
| 38 | // /f (false); /d (true); /b (false); / (true) |
| 39 | // |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 40 | // lower_bound(/) -> / (true) <-- excluded (equal) |
| 41 | // lower_bound(/a) -> / (true) <-- excluded (any) |
| 42 | // lower_bound(/b) -> /b (false) <--- excluded (equal) |
| 43 | // lower_bound(/c) -> /b (false) <--- not excluded (not equal and no ANY) |
| 44 | // lower_bound(/d) -> /d (true) <- excluded |
| 45 | // lower_bound(/e) -> /d (true) <- excluded |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 46 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 47 | Exclude::isExcluded(const name::Component& comp) const |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 48 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 49 | const_iterator lowerBound = m_exclude.lower_bound(comp); |
| 50 | if (lowerBound == end()) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 51 | return false; |
| 52 | |
| 53 | if (lowerBound->second) |
| 54 | return true; |
| 55 | else |
| 56 | return lowerBound->first == comp; |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 61 | Exclude& |
| 62 | Exclude::excludeOne(const name::Component& comp) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 63 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 64 | if (!isExcluded(comp)) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 65 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 66 | m_exclude.insert(std::make_pair(comp, false)); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 67 | } |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | // example: ANY /b0 /d0 ANY /f0 |
| 73 | // |
| 74 | // ordered in map as: |
| 75 | // |
| 76 | // /f0 (false); /d0 (true); /b0 (false); / (true) |
| 77 | // |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 78 | // lower_bound(/) -> / (true) <-- excluded (equal) |
| 79 | // lower_bound(/a0) -> / (true) <-- excluded (any) |
| 80 | // lower_bound(/b0) -> /b0 (false) <--- excluded (equal) |
| 81 | // lower_bound(/c0) -> /b0 (false) <--- not excluded (not equal and no ANY) |
| 82 | // lower_bound(/d0) -> /d0 (true) <- excluded |
| 83 | // lower_bound(/e0) -> /d0 (true) <- excluded |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 84 | |
| 85 | |
| 86 | // examples with desired outcomes |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 87 | // excludeRange(/, /f0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 88 | // /f0 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 89 | // excludeRange(/, /f1) -> ANY /f1 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 90 | // /f1 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 91 | // excludeRange(/a0, /e0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 92 | // /f0 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 93 | // excludeRange(/a0, /e0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 94 | // /f0 (false); / (true) |
| 95 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 96 | // excludeRange(/b1, /c0) -> ANY /b0 /b1 ANY /c0 /d0 ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 97 | // /f0 (false); /d0 (true); /c0 (false); /b1 (true); /b0 (false); / (true) |
| 98 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 99 | Exclude& |
| 100 | Exclude::excludeRange(const name::Component& from, const name::Component& to) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 101 | { |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 102 | if (from >= to) { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 103 | throw Error("Invalid exclude range [" + from.toUri() + ", " + to.toUri() + "] " |
| 104 | "(for single name exclude use Exclude::excludeOne)"); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 105 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 106 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 107 | iterator newFrom = m_exclude.lower_bound(from); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 108 | if (newFrom == end() || !newFrom->second /*without ANY*/) { |
| 109 | std::pair<iterator, bool> fromResult = m_exclude.insert(std::make_pair(from, true)); |
| 110 | newFrom = fromResult.first; |
| 111 | if (!fromResult.second) { |
| 112 | // this means that the lower bound is equal to the item itself. So, just update ANY flag |
| 113 | newFrom->second = true; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 114 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 115 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 116 | // else |
| 117 | // nothing special if start of the range already exists with ANY flag set |
| 118 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 119 | iterator newTo = m_exclude.lower_bound(to); // !newTo cannot be end() |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 120 | if (newTo == newFrom || !newTo->second) { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 121 | std::pair<iterator, bool> toResult = m_exclude.insert(std::make_pair(to, false)); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 122 | newTo = toResult.first; |
| 123 | ++ newTo; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 124 | } |
| 125 | // else |
| 126 | // nothing to do really |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 127 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 128 | m_exclude.erase(newTo, newFrom); // remove any intermediate node, since all of the are excluded |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 129 | |
| 130 | return *this; |
| 131 | } |
| 132 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 133 | Exclude& |
| 134 | Exclude::excludeAfter(const name::Component& from) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 135 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 136 | iterator newFrom = m_exclude.lower_bound(from); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 137 | if (newFrom == end() || !newFrom->second /*without ANY*/) { |
| 138 | std::pair<iterator, bool> fromResult = m_exclude.insert(std::make_pair(from, true)); |
| 139 | newFrom = fromResult.first; |
| 140 | if (!fromResult.second) { |
| 141 | // this means that the lower bound is equal to the item itself. So, just update ANY flag |
| 142 | newFrom->second = true; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 143 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 144 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 145 | // else |
| 146 | // nothing special if start of the range already exists with ANY flag set |
| 147 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 148 | if (newFrom != m_exclude.begin()) { |
| 149 | // remove any intermediate node, since all of the are excluded |
| 150 | m_exclude.erase(m_exclude.begin(), newFrom); |
| 151 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 152 | |
| 153 | return *this; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | std::ostream& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 158 | operator<<(std::ostream& os, const Exclude& exclude) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 159 | { |
| 160 | bool empty = true; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 161 | for (Exclude::const_reverse_iterator i = exclude.rbegin(); i != exclude.rend(); i++) { |
| 162 | if (!i->first.empty()) { |
| 163 | if (!empty) os << ","; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 164 | os << i->first.toUri(); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 165 | empty = false; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 166 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 167 | if (i->second) { |
| 168 | if (!empty) os << ","; |
| 169 | os << "*"; |
| 170 | empty = false; |
| 171 | } |
| 172 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 173 | return os; |
| 174 | } |
| 175 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 177 | } // namespace ndn |