OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007 Apple Inc. All rights reserved. |
3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 * Copyright (C) 2012 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * | 8 * |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 1522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1533 * @param {T} defaultValue | 1533 * @param {T} defaultValue |
1534 * @return {!Promise.<T>} | 1534 * @return {!Promise.<T>} |
1535 * @template T | 1535 * @template T |
1536 */ | 1536 */ |
1537 Promise.prototype.catchException = function(defaultValue) { | 1537 Promise.prototype.catchException = function(defaultValue) { |
1538 return this.catch(function (error) { | 1538 return this.catch(function (error) { |
1539 console.error(error); | 1539 console.error(error); |
1540 return defaultValue; | 1540 return defaultValue; |
1541 }); | 1541 }); |
1542 } | 1542 } |
| 1543 |
| 1544 /** |
| 1545 * @constructor |
| 1546 * @param {(function(!Segment, !Segment): ?Segment)=} mergeCallback |
| 1547 */ |
| 1548 function SegmentedRange(mergeCallback) |
| 1549 { |
| 1550 /** @type {!Array<!Segment>} */ |
| 1551 this._segments = []; |
| 1552 this._mergeCallback = mergeCallback; |
| 1553 } |
| 1554 |
| 1555 /** |
| 1556 * @constructor |
| 1557 * @param {number} begin |
| 1558 * @param {number} end |
| 1559 * @param {*} data |
| 1560 */ |
| 1561 function Segment(begin, end, data) |
| 1562 { |
| 1563 if (begin > end) |
| 1564 console.assert(false, "Invalid segment"); |
| 1565 this.begin = begin; |
| 1566 this.end = end; |
| 1567 this.data = data; |
| 1568 } |
| 1569 |
| 1570 Segment.prototype = { |
| 1571 /** |
| 1572 * @param {!Segment} that |
| 1573 * @return {boolean} |
| 1574 */ |
| 1575 intersects: function(that) |
| 1576 { |
| 1577 return this.begin < that.end && that.begin < this.end; |
| 1578 } |
| 1579 }; |
| 1580 |
| 1581 SegmentedRange.prototype = { |
| 1582 /** |
| 1583 * @param {!Segment} newSegment |
| 1584 */ |
| 1585 append: function(newSegment) |
| 1586 { |
| 1587 // 1. Find the proper insertion point for new segment |
| 1588 var startIndex = this._segments.lowerBound(newSegment, (a, b) => a.begin
- b.begin); |
| 1589 var endIndex = startIndex; |
| 1590 var merged = null; |
| 1591 if (startIndex > 0) { |
| 1592 // 2. Try mering the preceding segment |
| 1593 var precedingSegment = this._segments[startIndex - 1]; |
| 1594 merged = this._tryMerge(precedingSegment, newSegment); |
| 1595 if (merged) { |
| 1596 --startIndex; |
| 1597 newSegment = merged; |
| 1598 } else if (this._segments[startIndex - 1].end >= newSegment.begin) { |
| 1599 // 2a. If merge failed and segments overlap, adjust preceding se
gment. |
| 1600 // If an old segment entirely contains new one, split it in two. |
| 1601 if (newSegment.end < precedingSegment.end) |
| 1602 this._segments.splice(startIndex, 0, new Segment(newSegment.
end, precedingSegment.end, precedingSegment.data)); |
| 1603 precedingSegment.end = newSegment.begin; |
| 1604 } |
| 1605 } |
| 1606 // 3. Consume all segments that are entirely covered by the new one. |
| 1607 while (endIndex < this._segments.length && this._segments[endIndex].end
<= newSegment.end) |
| 1608 ++endIndex; |
| 1609 // 4. Merge or adjust the succeeding segment if it overlaps. |
| 1610 if (endIndex < this._segments.length) { |
| 1611 merged = this._tryMerge(newSegment, this._segments[endIndex]); |
| 1612 if (merged) { |
| 1613 endIndex++; |
| 1614 newSegment = merged; |
| 1615 } else if (newSegment.intersects(this._segments[endIndex])) |
| 1616 this._segments[endIndex].begin = newSegment.end; |
| 1617 } |
| 1618 this._segments.splice(startIndex, endIndex - startIndex, newSegment); |
| 1619 }, |
| 1620 |
| 1621 /** |
| 1622 * @param {!SegmentedRange} that |
| 1623 */ |
| 1624 appendRange: function(that) |
| 1625 { |
| 1626 that.segments().forEach(segment => this.append(segment)); |
| 1627 }, |
| 1628 |
| 1629 /** |
| 1630 * @return {!Array<!Segment>} |
| 1631 */ |
| 1632 segments: function() |
| 1633 { |
| 1634 return this._segments; |
| 1635 }, |
| 1636 |
| 1637 /** |
| 1638 * @param {!Segment} first |
| 1639 * @param {!Segment} second |
| 1640 * @return {?Segment} |
| 1641 */ |
| 1642 _tryMerge: function(first, second) |
| 1643 { |
| 1644 var merged = this._mergeCallback && this._mergeCallback(first, second); |
| 1645 if (!merged) |
| 1646 return null; |
| 1647 merged.begin = first.begin; |
| 1648 merged.end = Math.max(first.end, second.end); |
| 1649 return merged; |
| 1650 } |
| 1651 } |
OLD | NEW |