Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [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 | * |
| 5 | * BSD license, See the LICENSE file for more information |
| 6 | * |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 7 | * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 10 | #include "common.hpp" |
| 11 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 12 | #include "exclude.hpp" |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 13 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 14 | namespace ndn { |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 15 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 16 | Exclude::Exclude() |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 17 | { |
| 18 | } |
| 19 | |
| 20 | // example: ANY /b /d ANY /f |
| 21 | // |
| 22 | // ordered in map as: |
| 23 | // |
| 24 | // /f (false); /d (true); /b (false); / (true) |
| 25 | // |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 26 | // lower_bound(/) -> / (true) <-- excluded (equal) |
| 27 | // lower_bound(/a) -> / (true) <-- excluded (any) |
| 28 | // lower_bound(/b) -> /b (false) <--- excluded (equal) |
| 29 | // lower_bound(/c) -> /b (false) <--- not excluded (not equal and no ANY) |
| 30 | // lower_bound(/d) -> /d (true) <- excluded |
| 31 | // lower_bound(/e) -> /d (true) <- excluded |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 32 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 33 | Exclude::isExcluded(const name::Component& comp) const |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 34 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 35 | const_iterator lowerBound = m_exclude.lower_bound(comp); |
| 36 | if (lowerBound == end()) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 37 | return false; |
| 38 | |
| 39 | if (lowerBound->second) |
| 40 | return true; |
| 41 | else |
| 42 | return lowerBound->first == comp; |
| 43 | |
| 44 | return false; |
| 45 | } |
| 46 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 47 | Exclude& |
| 48 | Exclude::excludeOne(const name::Component& comp) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 49 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 50 | if (!isExcluded(comp)) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 51 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 52 | m_exclude.insert(std::make_pair(comp, false)); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 53 | } |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | // example: ANY /b0 /d0 ANY /f0 |
| 59 | // |
| 60 | // ordered in map as: |
| 61 | // |
| 62 | // /f0 (false); /d0 (true); /b0 (false); / (true) |
| 63 | // |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 64 | // lower_bound(/) -> / (true) <-- excluded (equal) |
| 65 | // lower_bound(/a0) -> / (true) <-- excluded (any) |
| 66 | // lower_bound(/b0) -> /b0 (false) <--- excluded (equal) |
| 67 | // lower_bound(/c0) -> /b0 (false) <--- not excluded (not equal and no ANY) |
| 68 | // lower_bound(/d0) -> /d0 (true) <- excluded |
| 69 | // lower_bound(/e0) -> /d0 (true) <- excluded |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 70 | |
| 71 | |
| 72 | // examples with desired outcomes |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 73 | // excludeRange(/, /f0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 74 | // /f0 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 75 | // excludeRange(/, /f1) -> ANY /f1 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 76 | // /f1 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 77 | // excludeRange(/a0, /e0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 78 | // /f0 (false); / (true) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 79 | // excludeRange(/a0, /e0) -> ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 80 | // /f0 (false); / (true) |
| 81 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 82 | // excludeRange(/b1, /c0) -> ANY /b0 /b1 ANY /c0 /d0 ANY /f0 |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 83 | // /f0 (false); /d0 (true); /c0 (false); /b1 (true); /b0 (false); / (true) |
| 84 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 85 | Exclude& |
| 86 | Exclude::excludeRange(const name::Component& from, const name::Component& to) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 87 | { |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 88 | if (from >= to) { |
| 89 | throw Error("Invalid exclude range [" + |
| 90 | from.toEscapedString() + ", " + |
| 91 | to.toEscapedString() + |
| 92 | "] (for single name exclude use Exclude::excludeOne)"); |
| 93 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 95 | iterator newFrom = m_exclude.lower_bound(from); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 96 | if (newFrom == end() || !newFrom->second /*without ANY*/) { |
| 97 | std::pair<iterator, bool> fromResult = m_exclude.insert(std::make_pair(from, true)); |
| 98 | newFrom = fromResult.first; |
| 99 | if (!fromResult.second) { |
| 100 | // this means that the lower bound is equal to the item itself. So, just update ANY flag |
| 101 | newFrom->second = true; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 102 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 103 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 104 | // else |
| 105 | // nothing special if start of the range already exists with ANY flag set |
| 106 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 107 | iterator newTo = m_exclude.lower_bound(to); // !newTo cannot be end() |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 108 | if (newTo == newFrom || !newTo->second) { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 109 | 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] | 110 | newTo = toResult.first; |
| 111 | ++ newTo; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 112 | } |
| 113 | // else |
| 114 | // nothing to do really |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 115 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 116 | 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] | 117 | |
| 118 | return *this; |
| 119 | } |
| 120 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 121 | Exclude& |
| 122 | Exclude::excludeAfter(const name::Component& from) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 123 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 124 | iterator newFrom = m_exclude.lower_bound(from); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 125 | if (newFrom == end() || !newFrom->second /*without ANY*/) { |
| 126 | std::pair<iterator, bool> fromResult = m_exclude.insert(std::make_pair(from, true)); |
| 127 | newFrom = fromResult.first; |
| 128 | if (!fromResult.second) { |
| 129 | // this means that the lower bound is equal to the item itself. So, just update ANY flag |
| 130 | newFrom->second = true; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 131 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 132 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 133 | // else |
| 134 | // nothing special if start of the range already exists with ANY flag set |
| 135 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 136 | if (newFrom != m_exclude.begin()) { |
| 137 | // remove any intermediate node, since all of the are excluded |
| 138 | m_exclude.erase(m_exclude.begin(), newFrom); |
| 139 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 140 | |
| 141 | return *this; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | std::ostream& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 146 | operator<<(std::ostream& os, const Exclude& exclude) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 147 | { |
| 148 | bool empty = true; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 149 | for (Exclude::const_reverse_iterator i = exclude.rbegin(); i != exclude.rend(); i++) { |
| 150 | if (!i->first.empty()) { |
| 151 | if (!empty) os << ","; |
| 152 | os << i->first.toEscapedString(); |
| 153 | empty = false; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 154 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 155 | if (i->second) { |
| 156 | if (!empty) os << ","; |
| 157 | os << "*"; |
| 158 | empty = false; |
| 159 | } |
| 160 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 161 | return os; |
| 162 | } |
| 163 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 165 | } // namespace ndn |