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 | /** |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 24 | #include "exclude.hpp" |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 25 | #include "encoding/block-helpers.hpp" |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 27 | #include <boost/range/adaptor/reversed.hpp> |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 29 | namespace ndn { |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 30 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 31 | Exclude::ExcludeComponent::ExcludeComponent(const name::Component& component1) |
| 32 | : isNegInf(false) |
| 33 | , component(component1) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | Exclude::ExcludeComponent::ExcludeComponent(bool isNegInf1) |
| 38 | : isNegInf(true) |
| 39 | { |
| 40 | BOOST_ASSERT(isNegInf1 == true); |
| 41 | } |
| 42 | |
| 43 | bool |
Junxiao Shi | b80e947 | 2016-07-20 13:45:24 +0000 | [diff] [blame] | 44 | operator==(const Exclude::ExcludeComponent& a, const Exclude::ExcludeComponent& b) |
| 45 | { |
| 46 | return (a.isNegInf && b.isNegInf) || |
| 47 | (a.isNegInf == b.isNegInf && a.component == b.component); |
| 48 | } |
| 49 | |
| 50 | bool |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 51 | operator>(const Exclude::ExcludeComponent& a, const Exclude::ExcludeComponent& b) |
| 52 | { |
| 53 | return a.isNegInf < b.isNegInf || |
| 54 | (a.isNegInf == b.isNegInf && a.component > b.component); |
| 55 | } |
| 56 | |
Junxiao Shi | b80e947 | 2016-07-20 13:45:24 +0000 | [diff] [blame] | 57 | bool |
| 58 | Exclude::Range::operator==(const Exclude::Range& other) const |
| 59 | { |
| 60 | return this->fromInfinity == other.fromInfinity && this->toInfinity == other.toInfinity && |
| 61 | (this->fromInfinity || this->from == other.from) && |
| 62 | (this->toInfinity || this->to == other.to); |
| 63 | } |
| 64 | |
| 65 | std::ostream& |
| 66 | operator<<(std::ostream& os, const Exclude::Range& range) |
| 67 | { |
| 68 | if (range.isSingular()) { |
| 69 | return os << '{' << range.from << '}'; |
| 70 | } |
| 71 | |
| 72 | if (range.fromInfinity) { |
| 73 | os << "(-∞"; |
| 74 | } |
| 75 | else { |
| 76 | os << '[' << range.from; |
| 77 | } |
| 78 | |
| 79 | os << ","; |
| 80 | |
| 81 | if (range.toInfinity) { |
| 82 | os << "+∞)"; |
| 83 | } |
| 84 | else { |
| 85 | os << range.to << ']'; |
| 86 | } |
| 87 | |
| 88 | return os; |
| 89 | } |
| 90 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 91 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Exclude>)); |
| 92 | BOOST_CONCEPT_ASSERT((WireEncodable<Exclude>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 93 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Exclude>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 94 | BOOST_CONCEPT_ASSERT((WireDecodable<Exclude>)); |
| 95 | static_assert(std::is_base_of<tlv::Error, Exclude::Error>::value, |
| 96 | "Exclude::Error must inherit from tlv::Error"); |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 97 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 98 | Exclude::Exclude() = default; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 99 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 100 | Exclude::Exclude(const Block& wire) |
| 101 | { |
| 102 | wireDecode(wire); |
| 103 | } |
| 104 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 105 | template<encoding::Tag TAG> |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 106 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 107 | Exclude::wireEncode(EncodingImpl<TAG>& encoder) const |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 108 | { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 109 | if (m_entries.empty()) { |
| 110 | BOOST_THROW_EXCEPTION(Error("cannot encode empty Exclude selector")); |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 113 | size_t totalLength = 0; |
| 114 | |
| 115 | // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+ |
| 116 | // Any ::= ANY-TYPE TLV-LENGTH(=0) |
| 117 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 118 | for (const Entry& entry : m_entries) { |
| 119 | if (entry.second) { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 120 | totalLength += prependEmptyBlock(encoder, tlv::Any); |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 121 | } |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 122 | if (!entry.first.isNegInf) { |
| 123 | totalLength += entry.first.component.wireEncode(encoder); |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 127 | totalLength += encoder.prependVarNumber(totalLength); |
| 128 | totalLength += encoder.prependVarNumber(tlv::Exclude); |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 129 | return totalLength; |
| 130 | } |
| 131 | |
| 132 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 133 | Exclude::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 134 | |
| 135 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 136 | Exclude::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 137 | |
| 138 | const Block& |
| 139 | Exclude::wireEncode() const |
| 140 | { |
| 141 | if (m_wire.hasWire()) |
| 142 | return m_wire; |
| 143 | |
| 144 | EncodingEstimator estimator; |
| 145 | size_t estimatedSize = wireEncode(estimator); |
| 146 | |
| 147 | EncodingBuffer buffer(estimatedSize, 0); |
| 148 | wireEncode(buffer); |
| 149 | |
| 150 | m_wire = buffer.block(); |
| 151 | return m_wire; |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | Exclude::wireDecode(const Block& wire) |
| 156 | { |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 157 | clear(); |
| 158 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 159 | if (wire.type() != tlv::Exclude) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 160 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Exclude")); |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 161 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 162 | m_wire = wire; |
| 163 | m_wire.parse(); |
| 164 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 165 | if (m_wire.elements_size() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 166 | BOOST_THROW_EXCEPTION(Error("Exclude element cannot be empty")); |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 169 | // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+ |
| 170 | // Any ::= ANY-TYPE TLV-LENGTH(=0) |
| 171 | |
| 172 | Block::element_const_iterator i = m_wire.elements_begin(); |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 173 | if (i->type() == tlv::Any) { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 174 | this->appendEntry(true, true); |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 175 | ++i; |
| 176 | } |
| 177 | |
| 178 | while (i != m_wire.elements_end()) { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 179 | name::Component component; |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 180 | try { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 181 | component = name::Component(*i); |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 182 | } |
| 183 | catch (const name::Component::Error&) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 184 | BOOST_THROW_EXCEPTION(Error("Incorrect format of Exclude filter")); |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 185 | } |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 186 | ++i; |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 187 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 188 | if (i != m_wire.elements_end() && i->type() == tlv::Any) { |
| 189 | this->appendEntry(component, true); |
| 190 | ++i; |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 191 | } |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 192 | else { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 193 | this->appendEntry(component, false); |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 198 | template<typename T> |
| 199 | void |
| 200 | Exclude::appendEntry(const T& component, bool hasAny) |
| 201 | { |
| 202 | m_entries.emplace_hint(m_entries.begin(), std::piecewise_construct, |
| 203 | std::forward_as_tuple(component), |
| 204 | std::forward_as_tuple(hasAny)); |
| 205 | } |
| 206 | |
| 207 | // example: ANY "b" "d" ANY "f" |
| 208 | // ordered in map as: "f" (false); "d" (true); "b" (false); -Inf (true) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 209 | // |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 210 | // lower_bound("") -> -Inf (true) <-- excluded (ANY) |
| 211 | // lower_bound("a") -> -Inf (true) <-- excluded (ANY) |
| 212 | // lower_bound("b") -> "b" (false) <--- excluded (equal) |
| 213 | // lower_bound("c") -> "b" (false) <--- not excluded (not equal and no ANY) |
| 214 | // lower_bound("d") -> "d" (true) <- excluded |
| 215 | // lower_bound("e") -> "d" (true) <- excluded |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 216 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 217 | Exclude::isExcluded(const name::Component& comp) const |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 218 | { |
Junxiao Shi | b80e947 | 2016-07-20 13:45:24 +0000 | [diff] [blame] | 219 | ExcludeMap::const_iterator lb = m_entries.lower_bound(comp); |
| 220 | return lb != m_entries.end() && // if false, comp is less than the first excluded component |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 221 | (lb->second || // comp matches an ANY range |
| 222 | (!lb->first.isNegInf && lb->first.component == comp)); // comp equals an exact excluded component |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 225 | Exclude& |
| 226 | Exclude::excludeOne(const name::Component& comp) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 227 | { |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 228 | if (!isExcluded(comp)) { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 229 | this->appendEntry(comp, false); |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 230 | m_wire.reset(); |
| 231 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 232 | return *this; |
| 233 | } |
| 234 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 235 | Exclude& |
| 236 | Exclude::excludeBefore(const name::Component& to) |
| 237 | { |
| 238 | return excludeRange(ExcludeComponent(true), to); |
| 239 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 240 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 241 | Exclude& |
| 242 | Exclude::excludeRange(const name::Component& from, const name::Component& to) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 243 | { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 244 | return excludeRange(ExcludeComponent(from), to); |
| 245 | } |
| 246 | |
| 247 | // example: ANY "b" "d" ANY "g" |
| 248 | // ordered in map as: "f" (false); "d" (true); "b" (false); -Inf (true) |
| 249 | // possible sequence of operations: |
| 250 | // excludeBefore("a") -> excludeRange(-Inf, "a") -> ANY "a" |
| 251 | // "a" (false); -Inf (true) |
| 252 | // excludeBefore("b") -> excludeRange(-Inf, "b") -> ANY "b" |
| 253 | // "b" (false); -Inf (true) |
| 254 | // excludeRange("e", "g") -> ANY "b" "e" ANY "g" |
| 255 | // "g" (false); "e" (true); "b" (false); -Inf (true) |
| 256 | // excludeRange("d", "f") -> ANY "b" "d" ANY "g" |
| 257 | // "g" (false); "d" (true); "b" (false); -Inf (true) |
| 258 | |
| 259 | Exclude& |
| 260 | Exclude::excludeRange(const ExcludeComponent& from, const name::Component& to) |
| 261 | { |
| 262 | if (!from.isNegInf && from.component >= to) { |
| 263 | BOOST_THROW_EXCEPTION(Error("Invalid exclude range [" + from.component.toUri() + ", " + to.toUri() + "] " |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 264 | "(for single name exclude use Exclude::excludeOne)")); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 265 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 266 | |
Junxiao Shi | b80e947 | 2016-07-20 13:45:24 +0000 | [diff] [blame] | 267 | ExcludeMap::iterator newFrom = m_entries.lower_bound(from); |
| 268 | if (newFrom == m_entries.end() || !newFrom->second /*without ANY*/) { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 269 | bool isNewEntry = false; |
| 270 | std::tie(newFrom, isNewEntry) = m_entries.emplace(from, true); |
| 271 | if (!isNewEntry) { |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 272 | // this means that the lower bound is equal to the item itself. So, just update ANY flag |
| 273 | newFrom->second = true; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 274 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 275 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 276 | // else |
| 277 | // nothing special if start of the range already exists with ANY flag set |
| 278 | |
Junxiao Shi | b80e947 | 2016-07-20 13:45:24 +0000 | [diff] [blame] | 279 | ExcludeMap::iterator newTo = m_entries.lower_bound(to); |
| 280 | BOOST_ASSERT(newTo != m_entries.end()); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 281 | if (newTo == newFrom || !newTo->second) { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 282 | newTo = m_entries.emplace_hint(newTo, to, false); |
| 283 | ++newTo; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 284 | } |
| 285 | // else |
| 286 | // nothing to do really |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 287 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 288 | // remove any intermediate entries, since all of the are excluded |
| 289 | m_entries.erase(newTo, newFrom); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 290 | |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 291 | m_wire.reset(); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 292 | return *this; |
| 293 | } |
| 294 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 295 | Exclude& |
| 296 | Exclude::excludeAfter(const name::Component& from) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 297 | { |
Junxiao Shi | b80e947 | 2016-07-20 13:45:24 +0000 | [diff] [blame] | 298 | ExcludeMap::iterator newFrom = m_entries.lower_bound(from); |
| 299 | if (newFrom == m_entries.end() || !newFrom->second /*without ANY*/) { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 300 | bool isNewEntry = false; |
| 301 | std::tie(newFrom, isNewEntry) = m_entries.emplace(from, true); |
| 302 | if (!isNewEntry) { |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 303 | // this means that the lower bound is equal to the item itself. So, just update ANY flag |
| 304 | newFrom->second = true; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 305 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 306 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 307 | // else |
| 308 | // nothing special if start of the range already exists with ANY flag set |
| 309 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 310 | // remove any intermediate node, since all of the are excluded |
| 311 | m_entries.erase(m_entries.begin(), newFrom); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 312 | |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 313 | m_wire.reset(); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 314 | return *this; |
| 315 | } |
| 316 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 317 | std::ostream& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 318 | operator<<(std::ostream& os, const Exclude& exclude) |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 319 | { |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 320 | auto join = make_ostream_joiner(os, ','); |
Junxiao Shi | b80e947 | 2016-07-20 13:45:24 +0000 | [diff] [blame] | 321 | for (const Exclude::Entry& entry : exclude.m_entries | boost::adaptors::reversed) { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 322 | if (!entry.first.isNegInf) { |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 323 | join = entry.first.component; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 324 | } |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 325 | if (entry.second) { |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 326 | join = '*'; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 329 | return os; |
| 330 | } |
| 331 | |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 332 | std::string |
| 333 | Exclude::toUri() const |
| 334 | { |
| 335 | std::ostringstream os; |
| 336 | os << *this; |
| 337 | return os.str(); |
| 338 | } |
| 339 | |
| 340 | bool |
| 341 | Exclude::operator==(const Exclude& other) const |
| 342 | { |
Junxiao Shi | b80e947 | 2016-07-20 13:45:24 +0000 | [diff] [blame] | 343 | return m_entries == other.m_entries; |
| 344 | } |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 345 | |
Junxiao Shi | b80e947 | 2016-07-20 13:45:24 +0000 | [diff] [blame] | 346 | size_t |
| 347 | Exclude::size() const |
| 348 | { |
| 349 | return std::distance(begin(), end()); |
| 350 | } |
| 351 | |
| 352 | void |
| 353 | Exclude::clear() |
| 354 | { |
| 355 | m_entries.clear(); |
| 356 | m_wire.reset(); |
| 357 | } |
| 358 | |
| 359 | Exclude::const_iterator::const_iterator(ExcludeMap::const_reverse_iterator it, |
| 360 | ExcludeMap::const_reverse_iterator rend) |
| 361 | : m_it(it) |
| 362 | , m_rend(rend) |
| 363 | { |
| 364 | this->update(); |
| 365 | } |
| 366 | |
| 367 | Exclude::const_iterator& |
| 368 | Exclude::const_iterator::operator++() |
| 369 | { |
| 370 | bool wasInRange = m_it->second; |
| 371 | ++m_it; |
| 372 | if (wasInRange && m_it != m_rend) { |
| 373 | BOOST_ASSERT(m_it->second == false); // consecutive ranges should have been combined |
| 374 | ++m_it; // skip over range high limit |
| 375 | } |
| 376 | this->update(); |
| 377 | return *this; |
| 378 | } |
| 379 | |
| 380 | Exclude::const_iterator |
| 381 | Exclude::const_iterator::operator++(int) |
| 382 | { |
| 383 | const_iterator i = *this; |
| 384 | this->operator++(); |
| 385 | return i; |
| 386 | } |
| 387 | |
| 388 | void |
| 389 | Exclude::const_iterator::update() |
| 390 | { |
| 391 | if (m_it == m_rend) { |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | if (m_it->second) { // range |
| 396 | if (m_it->first.isNegInf) { |
| 397 | m_range.fromInfinity = true; |
| 398 | } |
| 399 | else { |
| 400 | m_range.fromInfinity = false; |
| 401 | m_range.from = m_it->first.component; |
| 402 | } |
| 403 | |
| 404 | auto next = std::next(m_it); |
| 405 | if (next == m_rend) { |
| 406 | m_range.toInfinity = true; |
| 407 | } |
| 408 | else { |
| 409 | m_range.toInfinity = false; |
| 410 | m_range.to = next->first.component; |
| 411 | } |
| 412 | } |
| 413 | else { // single |
| 414 | BOOST_ASSERT(!m_it->first.isNegInf); |
| 415 | m_range.fromInfinity = m_range.toInfinity = false; |
| 416 | m_range.from = m_range.to = m_it->first.component; |
| 417 | } |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 420 | } // namespace ndn |