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