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