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