1 // Copyright (c) 2012 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 'use strict'; 6 7 /** 8 * @fileoverview Quick range computations. 9 */ 10 base.exportTo('base', function() { 11 12 function Range() { 13 this.isEmpty_ = true; 14 this.min_ = undefined; 15 this.max_ = undefined; 16 }; 17 18 Range.prototype = { 19 __proto__: Object.prototype, 20 21 reset: function() { 22 this.isEmpty_ = true; 23 this.min_ = undefined; 24 this.max_ = undefined; 25 }, 26 27 get isEmpty() { 28 return this.isEmpty_; 29 }, 30 31 addRange: function(range) { 32 if (range.isEmpty) 33 return; 34 this.addValue(range.min); 35 this.addValue(range.max); 36 }, 37 38 addValue: function(value) { 39 if (this.isEmpty_) { 40 this.max_ = value; 41 this.min_ = value; 42 this.isEmpty_ = false; 43 return; 44 } 45 this.max_ = Math.max(this.max_, value); 46 this.min_ = Math.min(this.min_, value); 47 }, 48 49 get min() { 50 if (this.isEmpty_) 51 return undefined; 52 return this.min_; 53 }, 54 55 get max() { 56 if (this.isEmpty_) 57 return undefined; 58 return this.max_; 59 }, 60 61 get range() { 62 if (this.isEmpty_) 63 return undefined; 64 return this.max_ - this.min_; 65 }, 66 67 get center() { 68 return (this.min_ + this.max_) * 0.5; 69 } 70 }; 71 72 Range.compareByMinTimes = function(a, b) { 73 if (!a.isEmpty && !b.isEmpty) 74 return a.min_ - b.min_; 75 76 if (a.isEmpty && !b.isEmpty) 77 return -1; 78 79 if (!a.isEmpty && b.isEmpty) 80 return 1; 81 82 return 0; 83 }; 84 85 return { 86 Range: Range 87 }; 88 89 }); 90