rangeset.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A RangeSet is a structure that manages a list of ranges.
  16. * Numeric ranges may be added and removed from the RangeSet, and the set may
  17. * be queried for the presence or absence of individual values or ranges of
  18. * values.
  19. *
  20. * This may be used, for example, to track the availability of sparse elements
  21. * in an array without iterating over the entire array.
  22. *
  23. * @author brenneman@google.com (Shawn Brenneman)
  24. */
  25. goog.provide('goog.math.RangeSet');
  26. goog.require('goog.array');
  27. goog.require('goog.iter.Iterator');
  28. goog.require('goog.iter.StopIteration');
  29. goog.require('goog.math.Range');
  30. /**
  31. * Constructs a new RangeSet, which can store numeric ranges.
  32. *
  33. * Ranges are treated as half-closed: that is, they are exclusive of their end
  34. * value [start, end).
  35. *
  36. * New ranges added to the set which overlap the values in one or more existing
  37. * ranges will be merged.
  38. *
  39. * @struct
  40. * @constructor
  41. * @final
  42. */
  43. goog.math.RangeSet = function() {
  44. /**
  45. * A sorted list of ranges that represent the values in the set.
  46. * @type {!Array<!goog.math.Range>}
  47. * @private
  48. */
  49. this.ranges_ = [];
  50. };
  51. if (goog.DEBUG) {
  52. /**
  53. * @return {string} A debug string in the form [[1, 5], [8, 9], [15, 30]].
  54. * @override
  55. */
  56. goog.math.RangeSet.prototype.toString = function() {
  57. return '[' + this.ranges_.join(', ') + ']';
  58. };
  59. }
  60. /**
  61. * Compares two sets for equality.
  62. *
  63. * @param {goog.math.RangeSet} a A range set.
  64. * @param {goog.math.RangeSet} b A range set.
  65. * @return {boolean} Whether both sets contain the same values.
  66. */
  67. goog.math.RangeSet.equals = function(a, b) {
  68. // Fast check for object equality. Also succeeds if a and b are both null.
  69. return a == b ||
  70. !!(a && b &&
  71. goog.array.equals(a.ranges_, b.ranges_, goog.math.Range.equals));
  72. };
  73. /**
  74. * @return {!goog.math.RangeSet} A new RangeSet containing the same values as
  75. * this one.
  76. */
  77. goog.math.RangeSet.prototype.clone = function() {
  78. var set = new goog.math.RangeSet();
  79. for (var i = this.ranges_.length; i--;) {
  80. set.ranges_[i] = this.ranges_[i].clone();
  81. }
  82. return set;
  83. };
  84. /**
  85. * Adds a range to the set. If the new range overlaps existing values, those
  86. * ranges will be merged.
  87. *
  88. * @param {goog.math.Range} a The range to add.
  89. */
  90. goog.math.RangeSet.prototype.add = function(a) {
  91. if (a.end <= a.start) {
  92. // Empty ranges are ignored.
  93. return;
  94. }
  95. a = a.clone();
  96. // Find the insertion point.
  97. for (var i = 0, b; b = this.ranges_[i]; i++) {
  98. if (a.start <= b.end) {
  99. a.start = Math.min(a.start, b.start);
  100. break;
  101. }
  102. }
  103. var insertionPoint = i;
  104. for (; b = this.ranges_[i]; i++) {
  105. if (a.end < b.start) {
  106. break;
  107. }
  108. a.end = Math.max(a.end, b.end);
  109. }
  110. this.ranges_.splice(insertionPoint, i - insertionPoint, a);
  111. };
  112. /**
  113. * Removes a range of values from the set.
  114. *
  115. * @param {goog.math.Range} a The range to remove.
  116. */
  117. goog.math.RangeSet.prototype.remove = function(a) {
  118. if (a.end <= a.start) {
  119. // Empty ranges are ignored.
  120. return;
  121. }
  122. // Find the insertion point.
  123. for (var i = 0, b; b = this.ranges_[i]; i++) {
  124. if (a.start < b.end) {
  125. break;
  126. }
  127. }
  128. if (!b || a.end < b.start) {
  129. // The range being removed doesn't overlap any existing range. Exit early.
  130. return;
  131. }
  132. var insertionPoint = i;
  133. if (a.start > b.start) {
  134. // There is an overlap with the nearest range. Modify it accordingly.
  135. insertionPoint++;
  136. if (a.end < b.end) {
  137. goog.array.insertAt(
  138. this.ranges_, new goog.math.Range(a.end, b.end), insertionPoint);
  139. }
  140. b.end = a.start;
  141. }
  142. for (i = insertionPoint; b = this.ranges_[i]; i++) {
  143. b.start = Math.max(a.end, b.start);
  144. if (a.end < b.end) {
  145. break;
  146. }
  147. }
  148. this.ranges_.splice(insertionPoint, i - insertionPoint);
  149. };
  150. /**
  151. * Determines whether a given range is in the set. Only succeeds if the entire
  152. * range is available.
  153. *
  154. * @param {goog.math.Range} a The query range.
  155. * @return {boolean} Whether the entire requested range is set.
  156. */
  157. goog.math.RangeSet.prototype.contains = function(a) {
  158. if (a.end <= a.start) {
  159. return false;
  160. }
  161. for (var i = 0, b; b = this.ranges_[i]; i++) {
  162. if (a.start < b.end) {
  163. if (a.end >= b.start) {
  164. return goog.math.Range.contains(b, a);
  165. }
  166. break;
  167. }
  168. }
  169. return false;
  170. };
  171. /**
  172. * Determines whether a given value is set in the RangeSet.
  173. *
  174. * @param {number} value The value to test.
  175. * @return {boolean} Whether the given value is in the set.
  176. */
  177. goog.math.RangeSet.prototype.containsValue = function(value) {
  178. for (var i = 0, b; b = this.ranges_[i]; i++) {
  179. if (value < b.end) {
  180. if (value >= b.start) {
  181. return true;
  182. }
  183. break;
  184. }
  185. }
  186. return false;
  187. };
  188. /**
  189. * Returns the union of this RangeSet with another.
  190. *
  191. * @param {goog.math.RangeSet} set Another RangeSet.
  192. * @return {!goog.math.RangeSet} A new RangeSet containing all values from
  193. * either set.
  194. */
  195. goog.math.RangeSet.prototype.union = function(set) {
  196. // TODO(brenneman): A linear-time merge would be preferable if it is ever a
  197. // bottleneck.
  198. set = set.clone();
  199. for (var i = 0, a; a = this.ranges_[i]; i++) {
  200. set.add(a);
  201. }
  202. return set;
  203. };
  204. /**
  205. * Subtracts the ranges of another set from this one, returning the result
  206. * as a new RangeSet.
  207. *
  208. * @param {!goog.math.RangeSet} set The RangeSet to subtract.
  209. * @return {!goog.math.RangeSet} A new RangeSet containing all values in this
  210. * set minus the values of the input set.
  211. */
  212. goog.math.RangeSet.prototype.difference = function(set) {
  213. var ret = this.clone();
  214. for (var i = 0, a; a = set.ranges_[i]; i++) {
  215. ret.remove(a);
  216. }
  217. return ret;
  218. };
  219. /**
  220. * Intersects this RangeSet with another.
  221. *
  222. * @param {goog.math.RangeSet} set The RangeSet to intersect with.
  223. * @return {!goog.math.RangeSet} A new RangeSet containing all values set in
  224. * both this and the input set.
  225. */
  226. goog.math.RangeSet.prototype.intersection = function(set) {
  227. if (this.isEmpty() || set.isEmpty()) {
  228. return new goog.math.RangeSet();
  229. }
  230. return this.difference(set.inverse(this.getBounds()));
  231. };
  232. /**
  233. * Creates a subset of this set over the input range.
  234. *
  235. * @param {goog.math.Range} range The range to copy into the slice.
  236. * @return {!goog.math.RangeSet} A new RangeSet with a copy of the values in the
  237. * input range.
  238. */
  239. goog.math.RangeSet.prototype.slice = function(range) {
  240. var set = new goog.math.RangeSet();
  241. if (range.start >= range.end) {
  242. return set;
  243. }
  244. for (var i = 0, b; b = this.ranges_[i]; i++) {
  245. if (b.end <= range.start) {
  246. continue;
  247. }
  248. if (b.start > range.end) {
  249. break;
  250. }
  251. set.add(
  252. new goog.math.Range(
  253. Math.max(range.start, b.start), Math.min(range.end, b.end)));
  254. }
  255. return set;
  256. };
  257. /**
  258. * Creates an inverted slice of this set over the input range.
  259. *
  260. * @param {goog.math.Range} range The range to copy into the slice.
  261. * @return {!goog.math.RangeSet} A new RangeSet containing inverted values from
  262. * the original over the input range.
  263. */
  264. goog.math.RangeSet.prototype.inverse = function(range) {
  265. var set = new goog.math.RangeSet();
  266. set.add(range);
  267. for (var i = 0, b; b = this.ranges_[i]; i++) {
  268. if (range.start >= b.end) {
  269. continue;
  270. }
  271. if (range.end < b.start) {
  272. break;
  273. }
  274. set.remove(b);
  275. }
  276. return set;
  277. };
  278. /**
  279. * @return {number} The sum of the lengths of ranges covered in the set.
  280. */
  281. goog.math.RangeSet.prototype.coveredLength = function() {
  282. return /** @type {number} */ (
  283. goog.array.reduce(this.ranges_, function(res, range) {
  284. return res + range.end - range.start;
  285. }, 0));
  286. };
  287. /**
  288. * @return {goog.math.Range} The total range this set covers, ignoring any
  289. * gaps between ranges.
  290. */
  291. goog.math.RangeSet.prototype.getBounds = function() {
  292. if (this.ranges_.length) {
  293. return new goog.math.Range(
  294. this.ranges_[0].start, goog.array.peek(this.ranges_).end);
  295. }
  296. return null;
  297. };
  298. /**
  299. * @return {boolean} Whether any ranges are currently in the set.
  300. */
  301. goog.math.RangeSet.prototype.isEmpty = function() {
  302. return this.ranges_.length == 0;
  303. };
  304. /**
  305. * Removes all values in the set.
  306. */
  307. goog.math.RangeSet.prototype.clear = function() {
  308. this.ranges_.length = 0;
  309. };
  310. /**
  311. * Returns an iterator that iterates over the ranges in the RangeSet.
  312. *
  313. * @param {boolean=} opt_keys Ignored for RangeSets.
  314. * @return {!goog.iter.Iterator} An iterator over the values in the set.
  315. */
  316. goog.math.RangeSet.prototype.__iterator__ = function(opt_keys) {
  317. var i = 0;
  318. var list = this.ranges_;
  319. var iterator = new goog.iter.Iterator();
  320. iterator.next = function() {
  321. if (i >= list.length) {
  322. throw goog.iter.StopIteration;
  323. }
  324. return list[i++].clone();
  325. };
  326. return iterator;
  327. };