blob: e40436dec7fc015d4c67565e3ac883cd7dbb2f35 [file] [log] [blame]
Chris Craikb2cbf152015-07-28 16:26:29 -07001<!DOCTYPE html>
2<!--
3Copyright (c) 2014 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/base/base.html">
9
10<script>
11'use strict';
12
13/**
14 * @fileoverview Quick range computations.
15 */
16tr.exportTo('tr.b', function() {
17
18 function Range() {
19 this.isEmpty_ = true;
20 this.min_ = undefined;
21 this.max_ = undefined;
22 };
23
24 Range.prototype = {
25 __proto__: Object.prototype,
26
27 reset: function() {
28 this.isEmpty_ = true;
29 this.min_ = undefined;
30 this.max_ = undefined;
31 },
32
33 get isEmpty() {
34 return this.isEmpty_;
35 },
36
37 addRange: function(range) {
38 if (range.isEmpty)
39 return;
40 this.addValue(range.min);
41 this.addValue(range.max);
42 },
43
44 addValue: function(value) {
45 if (this.isEmpty_) {
46 this.max_ = value;
47 this.min_ = value;
48 this.isEmpty_ = false;
49 return;
50 }
51 this.max_ = Math.max(this.max_, value);
52 this.min_ = Math.min(this.min_, value);
53 },
54
55 set min(min) {
56 this.isEmpty_ = false;
57 this.min_ = min;
58 },
59
60 get min() {
61 if (this.isEmpty_)
62 return undefined;
63 return this.min_;
64 },
65
66 get max() {
67 if (this.isEmpty_)
68 return undefined;
69 return this.max_;
70 },
71
72 set max(max) {
73 this.isEmpty_ = false;
74 this.max_ = max;
75 },
76
77 get range() {
78 if (this.isEmpty_)
79 return undefined;
80 return this.max_ - this.min_;
81 },
82
83 get center() {
84 return (this.min_ + this.max_) * 0.5;
85 },
86
87 equals: function(that) {
88 if (this.isEmpty && that.isEmpty)
89 return true;
90 if (this.isEmpty != that.isEmpty)
91 return false;
92 return this.min === that.min &&
93 this.max === that.max;
94 },
95
96 containsRange: function(range) {
97 if (this.isEmpty || range.isEmpty)
98 return false;
99 return this.min <= range.min && this.max >= range.max;
100 },
101
102 containsExplicitRange: function(min, max) {
103 if (this.isEmpty)
104 return false;
105 return this.min <= min && this.max >= max;
106 },
107
108 intersectsRange: function(range) {
109 if (this.isEmpty || range.isEmpty)
110 return false;
111 return !(range.max < this.min ||
112 range.min > this.max);
113 },
114
115 intersectsExplicitRange: function(min, max) {
116 if (this.isEmpty)
117 return false;
118 return !(max < this.min ||
119 min > this.max);
120 }
121 };
122
123 Range.fromExplicitRange = function(min, max) {
124 var range = new Range();
125 range.min = min;
126 range.max = max;
127 return range;
128 };
129
130 Range.compareByMinTimes = function(a, b) {
131 if (!a.isEmpty && !b.isEmpty)
132 return a.min_ - b.min_;
133
134 if (a.isEmpty && !b.isEmpty)
135 return -1;
136
137 if (!a.isEmpty && b.isEmpty)
138 return 1;
139
140 return 0;
141 };
142
143 return {
144 Range: Range
145 };
146});
147</script>