| 1 |
// Copyright 2008 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 |
/** |
| 16 |
* @fileoverview Provides inversion and inversion map functionality for storing |
| 17 |
* integer ranges and corresponding values. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.structs.InversionMap'); |
| 22 |
|
| 23 |
goog.require('goog.array'); |
| 24 |
goog.require('goog.asserts'); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Maps ranges to values. |
| 30 |
* @param {Array<number>} rangeArray An array of monotonically |
| 31 |
* increasing integer values, with at least one instance. |
| 32 |
* @param {Array<T>} valueArray An array of corresponding values. |
| 33 |
* Length must be the same as rangeArray. |
| 34 |
* @param {boolean=} opt_delta If true, saves only delta from previous value. |
| 35 |
* @constructor |
| 36 |
* @template T |
| 37 |
*/ |
| 38 |
goog.structs.InversionMap = function(rangeArray, valueArray, opt_delta) { |
| 39 |
/** |
| 40 |
* @protected {Array<number>} |
| 41 |
*/ |
| 42 |
this.rangeArray = null; |
| 43 |
|
| 44 |
goog.asserts.assert( |
| 45 |
rangeArray.length == valueArray.length, |
| 46 |
'rangeArray and valueArray must have the same length.'); |
| 47 |
this.storeInversion_(rangeArray, opt_delta); |
| 48 |
|
| 49 |
/** @protected {Array<T>} */ |
| 50 |
this.values = valueArray; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Stores the integers as ranges (half-open). |
| 56 |
* If delta is true, the integers are delta from the previous value and |
| 57 |
* will be restored to the absolute value. |
| 58 |
* When used as a set, even indices are IN, and odd are OUT. |
| 59 |
* @param {Array<number>} rangeArray An array of monotonically |
| 60 |
* increasing integer values, with at least one instance. |
| 61 |
* @param {boolean=} opt_delta If true, saves only delta from previous value. |
| 62 |
* @private |
| 63 |
*/ |
| 64 |
goog.structs.InversionMap.prototype.storeInversion_ = function( |
| 65 |
rangeArray, opt_delta) { |
| 66 |
this.rangeArray = rangeArray; |
| 67 |
|
| 68 |
for (var i = 1; i < rangeArray.length; i++) { |
| 69 |
if (rangeArray[i] == null) { |
| 70 |
rangeArray[i] = rangeArray[i - 1] + 1; |
| 71 |
} else if (opt_delta) { |
| 72 |
rangeArray[i] += rangeArray[i - 1]; |
| 73 |
} |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* Splices a range -> value map into this inversion map. |
| 80 |
* @param {Array<number>} rangeArray An array of monotonically |
| 81 |
* increasing integer values, with at least one instance. |
| 82 |
* @param {Array<T>} valueArray An array of corresponding values. |
| 83 |
* Length must be the same as rangeArray. |
| 84 |
* @param {boolean=} opt_delta If true, saves only delta from previous value. |
| 85 |
*/ |
| 86 |
goog.structs.InversionMap.prototype.spliceInversion = function( |
| 87 |
rangeArray, valueArray, opt_delta) { |
| 88 |
// By building another inversion map, we build the arrays that we need |
| 89 |
// to splice in. |
| 90 |
var otherMap = |
| 91 |
new goog.structs.InversionMap(rangeArray, valueArray, opt_delta); |
| 92 |
|
| 93 |
// Figure out where to splice those arrays. |
| 94 |
var startRange = otherMap.rangeArray[0]; |
| 95 |
var endRange = |
| 96 |
/** @type {number} */ (goog.array.peek(otherMap.rangeArray)); |
| 97 |
var startSplice = this.getLeast(startRange); |
| 98 |
var endSplice = this.getLeast(endRange); |
| 99 |
|
| 100 |
// The inversion map works by storing the start points of ranges... |
| 101 |
if (startRange != this.rangeArray[startSplice]) { |
| 102 |
// ...if we're splicing in a start point that isn't already here, |
| 103 |
// then we need to insert it after the insertion point. |
| 104 |
startSplice++; |
| 105 |
} // otherwise we overwrite the insertion point. |
| 106 |
|
| 107 |
this.rangeArray = this.rangeArray.slice(0, startSplice) |
| 108 |
.concat(otherMap.rangeArray) |
| 109 |
.concat(this.rangeArray.slice(endSplice + 1)); |
| 110 |
this.values = this.values.slice(0, startSplice) |
| 111 |
.concat(otherMap.values) |
| 112 |
.concat(this.values.slice(endSplice + 1)); |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* Gets the value corresponding to a number from the inversion map. |
| 118 |
* @param {number} intKey The number for which value needs to be retrieved |
| 119 |
* from inversion map. |
| 120 |
* @return {T|null} Value retrieved from inversion map; null if not found. |
| 121 |
*/ |
| 122 |
goog.structs.InversionMap.prototype.at = function(intKey) { |
| 123 |
var index = this.getLeast(intKey); |
| 124 |
if (index < 0) { |
| 125 |
return null; |
| 126 |
} |
| 127 |
return this.values[index]; |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Gets the largest index such that rangeArray[index] <= intKey from the |
| 133 |
* inversion map. |
| 134 |
* @param {number} intKey The probe for which rangeArray is searched. |
| 135 |
* @return {number} Largest index such that rangeArray[index] <= intKey. |
| 136 |
* @protected |
| 137 |
*/ |
| 138 |
goog.structs.InversionMap.prototype.getLeast = function(intKey) { |
| 139 |
var arr = this.rangeArray; |
| 140 |
var low = 0; |
| 141 |
var high = arr.length; |
| 142 |
while (high - low > 8) { |
| 143 |
var mid = (high + low) >> 1; |
| 144 |
if (arr[mid] <= intKey) { |
| 145 |
low = mid; |
| 146 |
} else { |
| 147 |
high = mid; |
| 148 |
} |
| 149 |
} |
| 150 |
for (; low < high; ++low) { |
| 151 |
if (intKey < arr[low]) { |
| 152 |
break; |
| 153 |
} |
| 154 |
} |
| 155 |
return low - 1; |
| 156 |
}; |