| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // IntervalSet<T> is a data structure used to represent a sorted set of | |
| 6 // non-empty, non-adjacent, and mutually disjoint intervals. Mutations to an | |
| 7 // interval set preserve these properties, altering the set as needed. For | |
| 8 // example, adding [2, 3) to a set containing only [1, 2) would result in the | |
| 9 // set containing the single interval [1, 3). | |
| 10 // | |
| 11 // Supported operations include testing whether an Interval is contained in the | |
| 12 // IntervalSet, comparing two IntervalSets, and performing IntervalSet union, | |
| 13 // intersection, and difference. | |
| 14 // | |
| 15 // IntervalSet maintains the minimum number of entries needed to represent the | |
| 16 // set of underlying intervals. When the IntervalSet is modified (e.g. due to an | |
| 17 // Add operation), other interval entries may be coalesced, removed, or | |
| 18 // otherwise modified in order to maintain this invariant. The intervals are | |
| 19 // maintained in sorted order, by ascending min() value. | |
| 20 // | |
| 21 // The reader is cautioned to beware of the terminology used here: this library | |
| 22 // uses the terms "min" and "max" rather than "begin" and "end" as is | |
| 23 // conventional for the STL. The terminology [min, max) refers to the half-open | |
| 24 // interval which (if the interval is not empty) contains min but does not | |
| 25 // contain max. An interval is considered empty if min >= max. | |
| 26 // | |
| 27 // T is required to be default- and copy-constructible, to have an assignment | |
| 28 // operator, a difference operator (operator-()), and the full complement of | |
| 29 // comparison operators (<, <=, ==, !=, >=, >). These requirements are inherited | |
| 30 // from Interval<T>. | |
| 31 // | |
| 32 // IntervalSet has constant-time move operations. | |
| 33 // | |
| 34 // This class is thread-compatible if T is thread-compatible. (See | |
| 35 // go/thread-compatible). | |
| 36 // | |
| 37 // Examples: | |
| 38 // IntervalSet<int> intervals; | |
| 39 // intervals.Add(Interval<int>(10, 20)); | |
| 40 // intervals.Add(Interval<int>(30, 40)); | |
| 41 // // intervals contains [10,20) and [30,40). | |
| 42 // intervals.Add(Interval<int>(15, 35)); | |
| 43 // // intervals has been coalesced. It now contains the single range [10,40). | |
| 44 // EXPECT_EQ(1, intervals.Size()); | |
| 45 // EXPECT_TRUE(intervals.Contains(Interval<int>(10, 40))); | |
| 46 // | |
| 47 // intervals.Difference(Interval<int>(10, 20)); | |
| 48 // // intervals should now contain the single range [20, 40). | |
| 49 // EXPECT_EQ(1, intervals.Size()); | |
| 50 // EXPECT_TRUE(intervals.Contains(Interval<int>(20, 40))); | |
| 51 | |
| 52 #ifndef NET_QUIC_INTERVAL_SET_H_ | |
| 53 #define NET_QUIC_INTERVAL_SET_H_ | |
| 54 | |
| 55 #include <stddef.h> | |
| 56 | |
| 57 #include <algorithm> | |
| 58 #include <set> | |
| 59 #include <string> | |
| 60 #include <utility> | |
| 61 #include <vector> | |
| 62 | |
| 63 #include "base/logging.h" | |
| 64 #include "net/quic/interval.h" | |
| 65 | |
| 66 namespace net { | |
| 67 | |
| 68 template <typename T> | |
| 69 class IntervalSet { | |
| 70 private: | |
| 71 struct IntervalComparator { | |
| 72 bool operator()(const Interval<T>& a, const Interval<T>& b) const; | |
| 73 }; | |
| 74 typedef std::set<Interval<T>, IntervalComparator> Set; | |
| 75 | |
| 76 public: | |
| 77 typedef typename Set::value_type value_type; | |
| 78 typedef typename Set::const_iterator const_iterator; | |
| 79 typedef typename Set::const_reverse_iterator const_reverse_iterator; | |
| 80 | |
| 81 // Instantiates an empty IntervalSet. | |
| 82 IntervalSet() {} | |
| 83 | |
| 84 // Instantiates an IntervalSet containing exactly one initial half-open | |
| 85 // interval [min, max), unless the given interval is empty, in which case the | |
| 86 // IntervalSet will be empty. | |
| 87 explicit IntervalSet(const Interval<T>& interval) { Add(interval); } | |
| 88 | |
| 89 // Instantiates an IntervalSet containing the half-open interval [min, max). | |
| 90 IntervalSet(const T& min, const T& max) { Add(min, max); } | |
| 91 | |
| 92 // TODO(rtenneti): Implement after suupport for std::initializer_list. | |
| 93 #if 0 | |
| 94 IntervalSet(std::initializer_list<value_type> il) { assign(il); } | |
| 95 #endif | |
| 96 | |
| 97 // Clears this IntervalSet. | |
| 98 void Clear() { intervals_.clear(); } | |
| 99 | |
| 100 // Returns the number of disjoint intervals contained in this IntervalSet. | |
| 101 size_t Size() const { return intervals_.size(); } | |
| 102 | |
| 103 // Returns the smallest interval that contains all intervals in this | |
| 104 // IntervalSet, or the empty interval if the set is empty. | |
| 105 Interval<T> SpanningInterval() const; | |
| 106 | |
| 107 // Adds "interval" to this IntervalSet. Adding the empty interval has no | |
| 108 // effect. | |
| 109 void Add(const Interval<T>& interval); | |
| 110 | |
| 111 // Adds the interval [min, max) to this IntervalSet. Adding the empty interval | |
| 112 // has no effect. | |
| 113 void Add(const T& min, const T& max) { Add(Interval<T>(min, max)); } | |
| 114 | |
| 115 // DEPRECATED(kosak). Use Union() instead. This method merges all of the | |
| 116 // values contained in "other" into this IntervalSet. | |
| 117 void Add(const IntervalSet& other); | |
| 118 | |
| 119 // Returns true if this IntervalSet represents exactly the same set of | |
| 120 // intervals as the ones represented by "other". | |
| 121 bool Equals(const IntervalSet& other) const; | |
| 122 | |
| 123 // Returns true if this IntervalSet is empty. | |
| 124 bool Empty() const { return intervals_.empty(); } | |
| 125 | |
| 126 // Returns true if any interval in this IntervalSet contains the indicated | |
| 127 // value. | |
| 128 bool Contains(const T& value) const; | |
| 129 | |
| 130 // Returns true if there is some interval in this IntervalSet that wholly | |
| 131 // contains the given interval. An interval O "wholly contains" a non-empty | |
| 132 // interval I if O.Contains(p) is true for every p in I. This is the same | |
| 133 // definition used by Interval<T>::Contains(). This method returns false on | |
| 134 // the empty interval, due to a (perhaps unintuitive) convention inherited | |
| 135 // from Interval<T>. | |
| 136 // Example: | |
| 137 // Assume an IntervalSet containing the entries { [10,20), [30,40) }. | |
| 138 // Contains(Interval(15, 16)) returns true, because [10,20) contains | |
| 139 // [15,16). However, Contains(Interval(15, 35)) returns false. | |
| 140 bool Contains(const Interval<T>& interval) const; | |
| 141 | |
| 142 // Returns true if for each interval in "other", there is some (possibly | |
| 143 // different) interval in this IntervalSet which wholly contains it. See | |
| 144 // Contains(const Interval<T>& interval) for the meaning of "wholly contains". | |
| 145 // Perhaps unintuitively, this method returns false if "other" is the empty | |
| 146 // set. The algorithmic complexity of this method is O(other.Size() * | |
| 147 // log(this->Size())), which is not efficient. The method could be rewritten | |
| 148 // to run in O(other.Size() + this->Size()). | |
| 149 bool Contains(const IntervalSet<T>& other) const; | |
| 150 | |
| 151 // Returns true if there is some interval in this IntervalSet that wholly | |
| 152 // contains the interval [min, max). See Contains(const Interval<T>&). | |
| 153 bool Contains(const T& min, const T& max) const { | |
| 154 return Contains(Interval<T>(min, max)); | |
| 155 } | |
| 156 | |
| 157 // Returns true if for some interval in "other", there is some interval in | |
| 158 // this IntervalSet that intersects with it. See Interval<T>::Intersects() | |
| 159 // for the definition of interval intersection. | |
| 160 bool Intersects(const IntervalSet& other) const; | |
| 161 | |
| 162 // Returns an iterator to the Interval<T> in the IntervalSet that contains the | |
| 163 // given value. In other words, returns an iterator to the unique interval | |
| 164 // [min, max) in the IntervalSet that has the property min <= value < max. If | |
| 165 // there is no such interval, this method returns end(). | |
| 166 const_iterator Find(const T& value) const; | |
| 167 | |
| 168 // Returns an iterator to the Interval<T> in the IntervalSet that wholly | |
| 169 // contains the given interval. In other words, returns an iterator to the | |
| 170 // unique interval outer in the IntervalSet that has the property that | |
| 171 // outer.Contains(interval). If there is no such interval, or if interval is | |
| 172 // empty, returns end(). | |
| 173 const_iterator Find(const Interval<T>& interval) const; | |
| 174 | |
| 175 // Returns an iterator to the Interval<T> in the IntervalSet that wholly | |
| 176 // contains [min, max). In other words, returns an iterator to the unique | |
| 177 // interval outer in the IntervalSet that has the property that | |
| 178 // outer.Contains(Interval<T>(min, max)). If there is no such interval, or if | |
| 179 // interval is empty, returns end(). | |
| 180 const_iterator Find(const T& min, const T& max) const { | |
| 181 return Find(Interval<T>(min, max)); | |
| 182 } | |
| 183 | |
| 184 // Returns true if every value within the passed interval is not Contained | |
| 185 // within the IntervalSet. | |
| 186 bool IsDisjoint(const Interval<T>& interval) const; | |
| 187 | |
| 188 // Merges all the values contained in "other" into this IntervalSet. | |
| 189 void Union(const IntervalSet& other); | |
| 190 | |
| 191 // Modifies this IntervalSet so that it contains only those values that are | |
| 192 // currently present both in *this and in the IntervalSet "other". | |
| 193 void Intersection(const IntervalSet& other); | |
| 194 | |
| 195 // Mutates this IntervalSet so that it contains only those values that are | |
| 196 // currently in *this but not in "interval". | |
| 197 void Difference(const Interval<T>& interval); | |
| 198 | |
| 199 // Mutates this IntervalSet so that it contains only those values that are | |
| 200 // currently in *this but not in the interval [min, max). | |
| 201 void Difference(const T& min, const T& max); | |
| 202 | |
| 203 // Mutates this IntervalSet so that it contains only those values that are | |
| 204 // currently in *this but not in the IntervalSet "other". | |
| 205 void Difference(const IntervalSet& other); | |
| 206 | |
| 207 // Mutates this IntervalSet so that it contains only those values that are | |
| 208 // in [min, max) but not currently in *this. | |
| 209 void Complement(const T& min, const T& max); | |
| 210 | |
| 211 // IntervalSet's begin() iterator. The invariants of IntervalSet guarantee | |
| 212 // that for each entry e in the set, e.min() < e.max() (because the entries | |
| 213 // are non-empty) and for each entry f that appears later in the set, | |
| 214 // e.max() < f.min() (because the entries are ordered, pairwise-disjoint, and | |
| 215 // non-adjacent). Modifications to this IntervalSet invalidate these | |
| 216 // iterators. | |
| 217 const_iterator begin() const { return intervals_.begin(); } | |
| 218 | |
| 219 // IntervalSet's end() iterator. | |
| 220 const_iterator end() const { return intervals_.end(); } | |
| 221 | |
| 222 // IntervalSet's rbegin() and rend() iterators. Iterator invalidation | |
| 223 // semantics are the same as those for begin() / end(). | |
| 224 const_reverse_iterator rbegin() const { return intervals_.rbegin(); } | |
| 225 | |
| 226 const_reverse_iterator rend() const { return intervals_.rend(); } | |
| 227 | |
| 228 // Appends the intervals in this IntervalSet to the end of *out. | |
| 229 void Get(std::vector<Interval<T>>* out) const { | |
| 230 out->insert(out->end(), begin(), end()); | |
| 231 } | |
| 232 | |
| 233 // Copies the intervals in this IntervalSet to the given output iterator. | |
| 234 template <typename Iter> | |
| 235 Iter Get(Iter out_iter) const { | |
| 236 return std::copy(begin(), end(), out_iter); | |
| 237 } | |
| 238 | |
| 239 template <typename Iter> | |
| 240 void assign(Iter first, Iter last) { | |
| 241 Clear(); | |
| 242 for (; first != last; ++first) | |
| 243 Add(*first); | |
| 244 } | |
| 245 | |
| 246 // TODO(rtenneti): Implement after suupport for std::initializer_list. | |
| 247 #if 0 | |
| 248 void assign(std::initializer_list<value_type> il) { | |
| 249 assign(il.begin(), il.end()); | |
| 250 } | |
| 251 #endif | |
| 252 | |
| 253 // Returns a human-readable representation of this set. This will typically be | |
| 254 // (though is not guaranteed to be) of the form | |
| 255 // "[a1, b1) [a2, b2) ... [an, bn)" | |
| 256 // where the intervals are in the same order as given by traversal from | |
| 257 // begin() to end(). This representation is intended for human consumption; | |
| 258 // computer programs should not rely on the output being in exactly this form. | |
| 259 std::string ToString() const; | |
| 260 | |
| 261 // Equality for IntervalSet<T>. Delegates to Equals(). | |
| 262 bool operator==(const IntervalSet& other) const { return Equals(other); } | |
| 263 | |
| 264 // Inequality for IntervalSet<T>. Delegates to Equals() (and returns its | |
| 265 // negation). | |
| 266 bool operator!=(const IntervalSet& other) const { return !Equals(other); } | |
| 267 | |
| 268 // TODO(rtenneti): Implement after suupport for std::initializer_list. | |
| 269 #if 0 | |
| 270 IntervalSet& operator=(std::initializer_list<value_type> il) { | |
| 271 assign(il.begin(), il.end()); | |
| 272 return *this; | |
| 273 } | |
| 274 #endif | |
| 275 | |
| 276 // Swap this IntervalSet with *other. This is a constant-time operation. | |
| 277 void Swap(IntervalSet<T>* other) { intervals_.swap(other->intervals_); } | |
| 278 | |
| 279 private: | |
| 280 // Removes overlapping ranges and coalesces adjacent intervals as needed. | |
| 281 void Compact(const typename Set::iterator& begin, | |
| 282 const typename Set::iterator& end); | |
| 283 | |
| 284 // Returns true if this set is valid (i.e. all intervals in it are non-empty, | |
| 285 // non-adjacent, and mutually disjoint). Currently this is used as an | |
| 286 // integrity check by the Intersection() and Difference() methods, but is only | |
| 287 // invoked for debug builds (via DCHECK). | |
| 288 bool Valid() const; | |
| 289 | |
| 290 // Finds the first interval that potentially intersects 'other'. | |
| 291 const_iterator FindIntersectionCandidate(const IntervalSet& other) const; | |
| 292 | |
| 293 // Finds the first interval that potentially intersects 'interval'. | |
| 294 const_iterator FindIntersectionCandidate(const Interval<T>& interval) const; | |
| 295 | |
| 296 // Helper for Intersection() and Difference(): Finds the next pair of | |
| 297 // intervals from 'x' and 'y' that intersect. 'mine' is an iterator | |
| 298 // over x->intervals_. 'theirs' is an iterator over y.intervals_. 'mine' | |
| 299 // and 'theirs' are advanced until an intersecting pair is found. | |
| 300 // Non-intersecting intervals (aka "holes") from x->intervals_ can be | |
| 301 // optionally erased by "on_hole". | |
| 302 template <typename X, typename Func> | |
| 303 static bool FindNextIntersectingPairImpl(X* x, | |
| 304 const IntervalSet& y, | |
| 305 const_iterator* mine, | |
| 306 const_iterator* theirs, | |
| 307 Func on_hole); | |
| 308 | |
| 309 // The variant of the above method that doesn't mutate this IntervalSet. | |
| 310 bool FindNextIntersectingPair(const IntervalSet& other, | |
| 311 const_iterator* mine, | |
| 312 const_iterator* theirs) const { | |
| 313 return FindNextIntersectingPairImpl( | |
| 314 this, other, mine, theirs, | |
| 315 [](const IntervalSet*, const_iterator, const_iterator) {}); | |
| 316 } | |
| 317 | |
| 318 // The variant of the above method that mutates this IntervalSet by erasing | |
| 319 // holes. | |
| 320 bool FindNextIntersectingPairAndEraseHoles(const IntervalSet& other, | |
| 321 const_iterator* mine, | |
| 322 const_iterator* theirs) { | |
| 323 return FindNextIntersectingPairImpl( | |
| 324 this, other, mine, theirs, | |
| 325 [](IntervalSet* x, const_iterator from, const_iterator to) { | |
| 326 x->intervals_.erase(from, to); | |
| 327 }); | |
| 328 } | |
| 329 | |
| 330 // The representation for the intervals. The intervals in this set are | |
| 331 // non-empty, pairwise-disjoint, non-adjacent and ordered in ascending order | |
| 332 // by min(). | |
| 333 Set intervals_; | |
| 334 }; | |
| 335 | |
| 336 template <typename T> | |
| 337 std::ostream& operator<<(std::ostream& out, const IntervalSet<T>& seq); | |
| 338 | |
| 339 template <typename T> | |
| 340 void swap(IntervalSet<T>& x, IntervalSet<T>& y); | |
| 341 | |
| 342 //============================================================================== | |
| 343 // Implementation details: Clients can stop reading here. | |
| 344 | |
| 345 template <typename T> | |
| 346 Interval<T> IntervalSet<T>::SpanningInterval() const { | |
| 347 Interval<T> result; | |
| 348 if (!intervals_.empty()) { | |
| 349 result.SetMin(intervals_.begin()->min()); | |
| 350 result.SetMax(intervals_.rbegin()->max()); | |
| 351 } | |
| 352 return result; | |
| 353 } | |
| 354 | |
| 355 template <typename T> | |
| 356 void IntervalSet<T>::Add(const Interval<T>& interval) { | |
| 357 if (interval.Empty()) | |
| 358 return; | |
| 359 std::pair<typename Set::iterator, bool> ins = intervals_.insert(interval); | |
| 360 if (!ins.second) { | |
| 361 // This interval already exists. | |
| 362 return; | |
| 363 } | |
| 364 // Determine the minimal range that will have to be compacted. We know that | |
| 365 // the IntervalSet was valid before the addition of the interval, so only | |
| 366 // need to start with the interval itself (although Compact takes an open | |
| 367 // range so begin needs to be the interval to the left). We don't know how | |
| 368 // many ranges this interval may cover, so we need to find the appropriate | |
| 369 // interval to end with on the right. | |
| 370 typename Set::iterator begin = ins.first; | |
| 371 if (begin != intervals_.begin()) | |
| 372 --begin; | |
| 373 const Interval<T> target_end(interval.max(), interval.max()); | |
| 374 const typename Set::iterator end = intervals_.upper_bound(target_end); | |
| 375 Compact(begin, end); | |
| 376 } | |
| 377 | |
| 378 template <typename T> | |
| 379 void IntervalSet<T>::Add(const IntervalSet& other) { | |
| 380 for (const_iterator it = other.begin(); it != other.end(); ++it) { | |
| 381 Add(*it); | |
| 382 } | |
| 383 } | |
| 384 | |
| 385 template <typename T> | |
| 386 bool IntervalSet<T>::Equals(const IntervalSet& other) const { | |
| 387 if (intervals_.size() != other.intervals_.size()) | |
| 388 return false; | |
| 389 for (typename Set::iterator i = intervals_.begin(), | |
| 390 j = other.intervals_.begin(); | |
| 391 i != intervals_.end(); ++i, ++j) { | |
| 392 // Simple member-wise equality, since all intervals are non-empty. | |
| 393 if (i->min() != j->min() || i->max() != j->max()) | |
| 394 return false; | |
| 395 } | |
| 396 return true; | |
| 397 } | |
| 398 | |
| 399 template <typename T> | |
| 400 bool IntervalSet<T>::Contains(const T& value) const { | |
| 401 Interval<T> tmp(value, value); | |
| 402 // Find the first interval with min() > value, then move back one step | |
| 403 const_iterator it = intervals_.upper_bound(tmp); | |
| 404 if (it == intervals_.begin()) | |
| 405 return false; | |
| 406 --it; | |
| 407 return it->Contains(value); | |
| 408 } | |
| 409 | |
| 410 template <typename T> | |
| 411 bool IntervalSet<T>::Contains(const Interval<T>& interval) const { | |
| 412 // Find the first interval with min() > value, then move back one step. | |
| 413 const_iterator it = intervals_.upper_bound(interval); | |
| 414 if (it == intervals_.begin()) | |
| 415 return false; | |
| 416 --it; | |
| 417 return it->Contains(interval); | |
| 418 } | |
| 419 | |
| 420 template <typename T> | |
| 421 bool IntervalSet<T>::Contains(const IntervalSet<T>& other) const { | |
| 422 if (!SpanningInterval().Contains(other.SpanningInterval())) { | |
| 423 return false; | |
| 424 } | |
| 425 | |
| 426 for (const_iterator i = other.begin(); i != other.end(); ++i) { | |
| 427 // If we don't contain the interval, can return false now. | |
| 428 if (!Contains(*i)) { | |
| 429 return false; | |
| 430 } | |
| 431 } | |
| 432 return true; | |
| 433 } | |
| 434 | |
| 435 // This method finds the interval that Contains() "value", if such an interval | |
| 436 // exists in the IntervalSet. The way this is done is to locate the "candidate | |
| 437 // interval", the only interval that could *possibly* contain value, and test it | |
| 438 // using Contains(). The candidate interval is the interval with the largest | |
| 439 // min() having min() <= value. | |
| 440 // | |
| 441 // Determining the candidate interval takes a couple of steps. First, since the | |
| 442 // underlying std::set stores intervals, not values, we need to create a "probe | |
| 443 // interval" suitable for use as a search key. The probe interval used is | |
| 444 // [value, value). Now we can restate the problem as finding the largest | |
| 445 // interval in the IntervalSet that is <= the probe interval. | |
| 446 // | |
| 447 // This restatement only works if the set's comparator behaves in a certain way. | |
| 448 // In particular it needs to order first by ascending min(), and then by | |
| 449 // descending max(). The comparator used by this library is defined in exactly | |
| 450 // this way. To see why descending max() is required, consider the following | |
| 451 // example. Assume an IntervalSet containing these intervals: | |
| 452 // | |
| 453 // [0, 5) [10, 20) [50, 60) | |
| 454 // | |
| 455 // Consider searching for the value 15. The probe interval [15, 15) is created, | |
| 456 // and [10, 20) is identified as the largest interval in the set <= the probe | |
| 457 // interval. This is the correct interval needed for the Contains() test, which | |
| 458 // will then return true. | |
| 459 // | |
| 460 // Now consider searching for the value 30. The probe interval [30, 30) is | |
| 461 // created, and again [10, 20] is identified as the largest interval <= the | |
| 462 // probe interval. This is again the correct interval needed for the Contains() | |
| 463 // test, which in this case returns false. | |
| 464 // | |
| 465 // Finally, consider searching for the value 10. The probe interval [10, 10) is | |
| 466 // created. Here the ordering relationship between [10, 10) and [10, 20) becomes | |
| 467 // vitally important. If [10, 10) were to come before [10, 20), then [0, 5) | |
| 468 // would be the largest interval <= the probe, leading to the wrong choice of | |
| 469 // interval for the Contains() test. Therefore [10, 10) needs to come after | |
| 470 // [10, 20). The simplest way to make this work in the general case is to order | |
| 471 // by ascending min() but descending max(). In this ordering, the empty interval | |
| 472 // is larger than any non-empty interval with the same min(). The comparator | |
| 473 // used by this library is careful to induce this ordering. | |
| 474 // | |
| 475 // Another detail involves the choice of which std::set method to use to try to | |
| 476 // find the candidate interval. The most appropriate entry point is | |
| 477 // set::upper_bound(), which finds the smallest interval which is > the probe | |
| 478 // interval. The semantics of upper_bound() are slightly different from what we | |
| 479 // want (namely, to find the largest interval which is <= the probe interval) | |
| 480 // but they are close enough; the interval found by upper_bound() will always be | |
| 481 // one step past the interval we are looking for (if it exists) or at begin() | |
| 482 // (if it does not). Getting to the proper interval is a simple matter of | |
| 483 // decrementing the iterator. | |
| 484 template <typename T> | |
| 485 typename IntervalSet<T>::const_iterator IntervalSet<T>::Find( | |
| 486 const T& value) const { | |
| 487 Interval<T> tmp(value, value); | |
| 488 const_iterator it = intervals_.upper_bound(tmp); | |
| 489 if (it == intervals_.begin()) | |
| 490 return intervals_.end(); | |
| 491 --it; | |
| 492 if (it->Contains(value)) | |
| 493 return it; | |
| 494 else | |
| 495 return intervals_.end(); | |
| 496 } | |
| 497 | |
| 498 // This method finds the interval that Contains() the interval "probe", if such | |
| 499 // an interval exists in the IntervalSet. The way this is done is to locate the | |
| 500 // "candidate interval", the only interval that could *possibly* contain | |
| 501 // "probe", and test it using Contains(). The candidate interval is the largest | |
| 502 // interval that is <= the probe interval. | |
| 503 // | |
| 504 // The search for the candidate interval only works if the comparator used | |
| 505 // behaves in a certain way. In particular it needs to order first by ascending | |
| 506 // min(), and then by descending max(). The comparator used by this library is | |
| 507 // defined in exactly this way. To see why descending max() is required, | |
| 508 // consider the following example. Assume an IntervalSet containing these | |
| 509 // intervals: | |
| 510 // | |
| 511 // [0, 5) [10, 20) [50, 60) | |
| 512 // | |
| 513 // Consider searching for the probe [15, 17). [10, 20) is the largest interval | |
| 514 // in the set which is <= the probe interval. This is the correct interval | |
| 515 // needed for the Contains() test, which will then return true, because [10, 20) | |
| 516 // contains [15, 17). | |
| 517 // | |
| 518 // Now consider searching for the probe [30, 32). Again [10, 20] is the largest | |
| 519 // interval <= the probe interval. This is again the correct interval needed for | |
| 520 // the Contains() test, which in this case returns false, because [10, 20) does | |
| 521 // not contain [30, 32). | |
| 522 // | |
| 523 // Finally, consider searching for the probe [10, 12). Here the ordering | |
| 524 // relationship between [10, 12) and [10, 20) becomes vitally important. If | |
| 525 // [10, 12) were to come before [10, 20), then [0, 5) would be the largest | |
| 526 // interval <= the probe, leading to the wrong choice of interval for the | |
| 527 // Contains() test. Therefore [10, 12) needs to come after [10, 20). The | |
| 528 // simplest way to make this work in the general case is to order by ascending | |
| 529 // min() but descending max(). In this ordering, given two intervals with the | |
| 530 // same min(), the wider one goes before the narrower one. The comparator used | |
| 531 // by this library is careful to induce this ordering. | |
| 532 // | |
| 533 // Another detail involves the choice of which std::set method to use to try to | |
| 534 // find the candidate interval. The most appropriate entry point is | |
| 535 // set::upper_bound(), which finds the smallest interval which is > the probe | |
| 536 // interval. The semantics of upper_bound() are slightly different from what we | |
| 537 // want (namely, to find the largest interval which is <= the probe interval) | |
| 538 // but they are close enough; the interval found by upper_bound() will always be | |
| 539 // one step past the interval we are looking for (if it exists) or at begin() | |
| 540 // (if it does not). Getting to the proper interval is a simple matter of | |
| 541 // decrementing the iterator. | |
| 542 template <typename T> | |
| 543 typename IntervalSet<T>::const_iterator IntervalSet<T>::Find( | |
| 544 const Interval<T>& probe) const { | |
| 545 const_iterator it = intervals_.upper_bound(probe); | |
| 546 if (it == intervals_.begin()) | |
| 547 return intervals_.end(); | |
| 548 --it; | |
| 549 if (it->Contains(probe)) | |
| 550 return it; | |
| 551 else | |
| 552 return intervals_.end(); | |
| 553 } | |
| 554 | |
| 555 template <typename T> | |
| 556 bool IntervalSet<T>::IsDisjoint(const Interval<T>& interval) const { | |
| 557 Interval<T> tmp(interval.min(), interval.min()); | |
| 558 // Find the first interval with min() > interval.min() | |
| 559 const_iterator it = intervals_.upper_bound(tmp); | |
| 560 if (it != intervals_.end() && interval.max() > it->min()) | |
| 561 return false; | |
| 562 if (it == intervals_.begin()) | |
| 563 return true; | |
| 564 --it; | |
| 565 return it->max() <= interval.min(); | |
| 566 } | |
| 567 | |
| 568 template <typename T> | |
| 569 void IntervalSet<T>::Union(const IntervalSet& other) { | |
| 570 intervals_.insert(other.begin(), other.end()); | |
| 571 Compact(intervals_.begin(), intervals_.end()); | |
| 572 } | |
| 573 | |
| 574 template <typename T> | |
| 575 typename IntervalSet<T>::const_iterator | |
| 576 IntervalSet<T>::FindIntersectionCandidate(const IntervalSet& other) const { | |
| 577 return FindIntersectionCandidate(*other.intervals_.begin()); | |
| 578 } | |
| 579 | |
| 580 template <typename T> | |
| 581 typename IntervalSet<T>::const_iterator | |
| 582 IntervalSet<T>::FindIntersectionCandidate(const Interval<T>& interval) const { | |
| 583 // Use upper_bound to efficiently find the first interval in intervals_ | |
| 584 // where min() is greater than interval.min(). If the result | |
| 585 // isn't the beginning of intervals_ then move backwards one interval since | |
| 586 // the interval before it is the first candidate where max() may be | |
| 587 // greater than interval.min(). | |
| 588 // In other words, no interval before that can possibly intersect with any | |
| 589 // of other.intervals_. | |
| 590 const_iterator mine = intervals_.upper_bound(interval); | |
| 591 if (mine != intervals_.begin()) { | |
| 592 --mine; | |
| 593 } | |
| 594 return mine; | |
| 595 } | |
| 596 | |
| 597 template <typename T> | |
| 598 template <typename X, typename Func> | |
| 599 bool IntervalSet<T>::FindNextIntersectingPairImpl(X* x, | |
| 600 const IntervalSet& y, | |
| 601 const_iterator* mine, | |
| 602 const_iterator* theirs, | |
| 603 Func on_hole) { | |
| 604 CHECK(x != nullptr); | |
| 605 if ((*mine == x->intervals_.end()) || (*theirs == y.intervals_.end())) { | |
| 606 return false; | |
| 607 } | |
| 608 while (!(**mine).Intersects(**theirs)) { | |
| 609 const_iterator erase_first = *mine; | |
| 610 // Skip over intervals in 'mine' that don't reach 'theirs'. | |
| 611 while (*mine != x->intervals_.end() && (**mine).max() <= (**theirs).min()) { | |
| 612 ++(*mine); | |
| 613 } | |
| 614 on_hole(x, erase_first, *mine); | |
| 615 // We're done if the end of intervals_ is reached. | |
| 616 if (*mine == x->intervals_.end()) { | |
| 617 return false; | |
| 618 } | |
| 619 // Skip over intervals 'theirs' that don't reach 'mine'. | |
| 620 while (*theirs != y.intervals_.end() && | |
| 621 (**theirs).max() <= (**mine).min()) { | |
| 622 ++(*theirs); | |
| 623 } | |
| 624 // If the end of other.intervals_ is reached, we're done. | |
| 625 if (*theirs == y.intervals_.end()) { | |
| 626 on_hole(x, *mine, x->intervals_.end()); | |
| 627 return false; | |
| 628 } | |
| 629 } | |
| 630 return true; | |
| 631 } | |
| 632 | |
| 633 template <typename T> | |
| 634 void IntervalSet<T>::Intersection(const IntervalSet& other) { | |
| 635 if (!SpanningInterval().Intersects(other.SpanningInterval())) { | |
| 636 intervals_.clear(); | |
| 637 return; | |
| 638 } | |
| 639 | |
| 640 const_iterator mine = FindIntersectionCandidate(other); | |
| 641 // Remove any intervals that cannot possibly intersect with other.intervals_. | |
| 642 intervals_.erase(intervals_.begin(), mine); | |
| 643 const_iterator theirs = other.FindIntersectionCandidate(*this); | |
| 644 | |
| 645 while (FindNextIntersectingPairAndEraseHoles(other, &mine, &theirs)) { | |
| 646 // OK, *mine and *theirs intersect. Now, we find the largest | |
| 647 // span of intervals in other (starting at theirs) - say [a..b] | |
| 648 // - that intersect *mine, and we replace *mine with (*mine | |
| 649 // intersect x) for all x in [a..b] Note that subsequent | |
| 650 // intervals in this can't intersect any intervals in [a..b) -- | |
| 651 // they may only intersect b or subsequent intervals in other. | |
| 652 Interval<T> i(*mine); | |
| 653 intervals_.erase(mine); | |
| 654 mine = intervals_.end(); | |
| 655 Interval<T> intersection; | |
| 656 while (theirs != other.intervals_.end() && | |
| 657 i.Intersects(*theirs, &intersection)) { | |
| 658 std::pair<typename Set::iterator, bool> ins = | |
| 659 intervals_.insert(intersection); | |
| 660 DCHECK(ins.second); | |
| 661 mine = ins.first; | |
| 662 ++theirs; | |
| 663 } | |
| 664 DCHECK(mine != intervals_.end()); | |
| 665 --theirs; | |
| 666 ++mine; | |
| 667 } | |
| 668 DCHECK(Valid()); | |
| 669 } | |
| 670 | |
| 671 template <typename T> | |
| 672 bool IntervalSet<T>::Intersects(const IntervalSet& other) const { | |
| 673 if (!SpanningInterval().Intersects(other.SpanningInterval())) { | |
| 674 return false; | |
| 675 } | |
| 676 | |
| 677 const_iterator mine = FindIntersectionCandidate(other); | |
| 678 if (mine == intervals_.end()) { | |
| 679 return false; | |
| 680 } | |
| 681 const_iterator theirs = other.FindIntersectionCandidate(*mine); | |
| 682 | |
| 683 return FindNextIntersectingPair(other, &mine, &theirs); | |
| 684 } | |
| 685 | |
| 686 template <typename T> | |
| 687 void IntervalSet<T>::Difference(const Interval<T>& interval) { | |
| 688 if (!SpanningInterval().Intersects(interval)) { | |
| 689 return; | |
| 690 } | |
| 691 Difference(IntervalSet<T>(interval)); | |
| 692 } | |
| 693 | |
| 694 template <typename T> | |
| 695 void IntervalSet<T>::Difference(const T& min, const T& max) { | |
| 696 Difference(Interval<T>(min, max)); | |
| 697 } | |
| 698 | |
| 699 template <typename T> | |
| 700 void IntervalSet<T>::Difference(const IntervalSet& other) { | |
| 701 if (!SpanningInterval().Intersects(other.SpanningInterval())) { | |
| 702 return; | |
| 703 } | |
| 704 | |
| 705 const_iterator mine = FindIntersectionCandidate(other); | |
| 706 // If no interval in mine reaches the first interval of theirs then we're | |
| 707 // done. | |
| 708 if (mine == intervals_.end()) { | |
| 709 return; | |
| 710 } | |
| 711 const_iterator theirs = other.FindIntersectionCandidate(*this); | |
| 712 | |
| 713 while (FindNextIntersectingPair(other, &mine, &theirs)) { | |
| 714 // At this point *mine and *theirs overlap. Remove mine from | |
| 715 // intervals_ and replace it with the possibly two intervals that are | |
| 716 // the difference between mine and theirs. | |
| 717 Interval<T> i(*mine); | |
| 718 intervals_.erase(mine++); | |
| 719 Interval<T> lo; | |
| 720 Interval<T> hi; | |
| 721 i.Difference(*theirs, &lo, &hi); | |
| 722 | |
| 723 if (!lo.Empty()) { | |
| 724 // We have a low end. This can't intersect anything else. | |
| 725 std::pair<typename Set::iterator, bool> ins = intervals_.insert(lo); | |
| 726 DCHECK(ins.second); | |
| 727 } | |
| 728 | |
| 729 if (!hi.Empty()) { | |
| 730 std::pair<typename Set::iterator, bool> ins = intervals_.insert(hi); | |
| 731 DCHECK(ins.second); | |
| 732 mine = ins.first; | |
| 733 } | |
| 734 } | |
| 735 DCHECK(Valid()); | |
| 736 } | |
| 737 | |
| 738 template <typename T> | |
| 739 void IntervalSet<T>::Complement(const T& min, const T& max) { | |
| 740 IntervalSet<T> span(min, max); | |
| 741 span.Difference(*this); | |
| 742 intervals_.swap(span.intervals_); | |
| 743 } | |
| 744 | |
| 745 template <typename T> | |
| 746 std::string IntervalSet<T>::ToString() const { | |
| 747 std::ostringstream os; | |
| 748 os << *this; | |
| 749 return os.str(); | |
| 750 } | |
| 751 | |
| 752 // This method compacts the IntervalSet, merging pairs of overlapping intervals | |
| 753 // into a single interval. In the steady state, the IntervalSet does not contain | |
| 754 // any such pairs. However, the way the Union() and Add() methods work is to | |
| 755 // temporarily put the IntervalSet into such a state and then to call Compact() | |
| 756 // to "fix it up" so that it is no longer in that state. | |
| 757 // | |
| 758 // Compact() needs the interval set to allow two intervals [a,b) and [a,c) | |
| 759 // (having the same min() but different max()) to briefly coexist in the set at | |
| 760 // the same time, and be adjacent to each other, so that they can be efficiently | |
| 761 // located and merged into a single interval. This state would be impossible | |
| 762 // with a comparator which only looked at min(), as such a comparator would | |
| 763 // consider such pairs equal. Fortunately, the comparator used by IntervalSet | |
| 764 // does exactly what is needed, ordering first by ascending min(), then by | |
| 765 // descending max(). | |
| 766 template <typename T> | |
| 767 void IntervalSet<T>::Compact(const typename Set::iterator& begin, | |
| 768 const typename Set::iterator& end) { | |
| 769 if (begin == end) | |
| 770 return; | |
| 771 typename Set::iterator next = begin; | |
| 772 typename Set::iterator prev = begin; | |
| 773 typename Set::iterator it = begin; | |
| 774 ++it; | |
| 775 ++next; | |
| 776 while (it != end) { | |
| 777 ++next; | |
| 778 if (prev->max() >= it->min()) { | |
| 779 // Overlapping / coalesced range; merge the two intervals. | |
| 780 T min = prev->min(); | |
| 781 T max = std::max(prev->max(), it->max()); | |
| 782 Interval<T> i(min, max); | |
| 783 intervals_.erase(prev); | |
| 784 intervals_.erase(it); | |
| 785 std::pair<typename Set::iterator, bool> ins = intervals_.insert(i); | |
| 786 DCHECK(ins.second); | |
| 787 prev = ins.first; | |
| 788 } else { | |
| 789 prev = it; | |
| 790 } | |
| 791 it = next; | |
| 792 } | |
| 793 } | |
| 794 | |
| 795 template <typename T> | |
| 796 bool IntervalSet<T>::Valid() const { | |
| 797 const_iterator prev = end(); | |
| 798 for (const_iterator it = begin(); it != end(); ++it) { | |
| 799 // invalid or empty interval. | |
| 800 if (it->min() >= it->max()) | |
| 801 return false; | |
| 802 // Not sorted, not disjoint, or adjacent. | |
| 803 if (prev != end() && prev->max() >= it->min()) | |
| 804 return false; | |
| 805 prev = it; | |
| 806 } | |
| 807 return true; | |
| 808 } | |
| 809 | |
| 810 template <typename T> | |
| 811 inline std::ostream& operator<<(std::ostream& out, const IntervalSet<T>& seq) { | |
| 812 // TODO(rtenneti): Implement << method of IntervalSet. | |
| 813 #if 0 | |
| 814 util::gtl::LogRangeToStream(out, seq.begin(), seq.end(), | |
| 815 util::gtl::LogLegacy()); | |
| 816 #endif // 0 | |
| 817 return out; | |
| 818 } | |
| 819 | |
| 820 template <typename T> | |
| 821 void swap(IntervalSet<T>& x, IntervalSet<T>& y) { | |
| 822 x.Swap(&y); | |
| 823 } | |
| 824 | |
| 825 // This comparator orders intervals first by ascending min() and then by | |
| 826 // descending max(). Readers who are satisified with that explanation can stop | |
| 827 // reading here. The remainder of this comment is for the benefit of future | |
| 828 // maintainers of this library. | |
| 829 // | |
| 830 // The reason for this ordering is that this comparator has to serve two | |
| 831 // masters. First, it has to maintain the intervals in its internal set in the | |
| 832 // order that clients expect to see them. Clients see these intervals via the | |
| 833 // iterators provided by begin()/end() or as a result of invoking Get(). For | |
| 834 // this reason, the comparator orders intervals by ascending min(). | |
| 835 // | |
| 836 // If client iteration were the only consideration, then ordering by ascending | |
| 837 // min() would be good enough. This is because the intervals in the IntervalSet | |
| 838 // are non-empty, non-adjacent, and mutually disjoint; such intervals happen to | |
| 839 // always have disjoint min() values, so such a comparator would never even have | |
| 840 // to look at max() in order to work correctly for this class. | |
| 841 // | |
| 842 // However, in addition to ordering by ascending min(), this comparator also has | |
| 843 // a second responsibility: satisfying the special needs of this library's | |
| 844 // peculiar internal implementation. These needs require the comparator to order | |
| 845 // first by ascending min() and then by descending max(). The best way to | |
| 846 // understand why this is so is to check out the comments associated with the | |
| 847 // Find() and Compact() methods. | |
| 848 template <typename T> | |
| 849 inline bool IntervalSet<T>::IntervalComparator::operator()( | |
| 850 const Interval<T>& a, | |
| 851 const Interval<T>& b) const { | |
| 852 return (a.min() < b.min() || (a.min() == b.min() && a.max() > b.max())); | |
| 853 } | |
| 854 | |
| 855 } // namespace net | |
| 856 | |
| 857 #endif // NET_QUIC_INTERVAL_SET_H_ | |
| OLD | NEW |