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