|
| 1 | +/** |
| 2 | +* Copyright 2012-2018, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +var isArray = Array.isArray; |
| 12 | + |
| 13 | +// IE9 fallbacks |
| 14 | + |
| 15 | +var ab = (typeof ArrayBuffer === 'undefined' || !ArrayBuffer.isView) ? |
| 16 | + {isView: function() { return false; }} : |
| 17 | + ArrayBuffer; |
| 18 | + |
| 19 | +var dv = (typeof DataView === 'undefined') ? |
| 20 | + function() {} : |
| 21 | + DataView; |
| 22 | + |
| 23 | +function isTypedArray(a) { |
| 24 | + return ab.isView(a) && !(a instanceof dv); |
| 25 | +} |
| 26 | +exports.isTypedArray = isTypedArray; |
| 27 | + |
| 28 | +function isArrayOrTypedArray(a) { |
| 29 | + return isArray(a) || isTypedArray(a); |
| 30 | +} |
| 31 | +exports.isArrayOrTypedArray = isArrayOrTypedArray; |
| 32 | + |
| 33 | +/* |
| 34 | + * Test whether an input object is 1D. |
| 35 | + * |
| 36 | + * Assumes we already know the object is an array. |
| 37 | + * |
| 38 | + * Looks only at the first element, if the dimensionality is |
| 39 | + * not consistent we won't figure that out here. |
| 40 | + */ |
| 41 | +function isArray1D(a) { |
| 42 | + return !isArrayOrTypedArray(a[0]); |
| 43 | +} |
| 44 | +exports.isArray1D = isArray1D; |
| 45 | + |
| 46 | +/* |
| 47 | + * Ensures an array has the right amount of storage space. If it doesn't |
| 48 | + * exist, it creates an array. If it does exist, it returns it if too |
| 49 | + * short or truncates it in-place. |
| 50 | + * |
| 51 | + * The goal is to just reuse memory to avoid a bit of excessive garbage |
| 52 | + * collection. |
| 53 | + */ |
| 54 | +exports.ensureArray = function(out, n) { |
| 55 | + // TODO: typed array support here? This is only used in |
| 56 | + // traces/carpet/compute_control_points |
| 57 | + if(!isArray(out)) out = []; |
| 58 | + |
| 59 | + // If too long, truncate. (If too short, it will grow |
| 60 | + // automatically so we don't care about that case) |
| 61 | + out.length = n; |
| 62 | + |
| 63 | + return out; |
| 64 | +}; |
| 65 | + |
| 66 | +/* |
| 67 | + * TypedArray-compatible concatenation of n arrays |
| 68 | + * if all arrays are the same type it will preserve that type, |
| 69 | + * otherwise it falls back on Array. |
| 70 | + * Also tries to avoid copying, in case one array has zero length |
| 71 | + * But never mutates an existing array |
| 72 | + */ |
| 73 | +exports.concat = function() { |
| 74 | + var args = []; |
| 75 | + var allArray = true; |
| 76 | + var totalLen = 0; |
| 77 | + |
| 78 | + var _constructor, arg0, i, argi, posi, leni, out, j; |
| 79 | + |
| 80 | + for(i = 0; i < arguments.length; i++) { |
| 81 | + argi = arguments[i]; |
| 82 | + leni = argi.length; |
| 83 | + if(leni) { |
| 84 | + if(arg0) args.push(argi); |
| 85 | + else { |
| 86 | + arg0 = argi; |
| 87 | + posi = leni; |
| 88 | + } |
| 89 | + |
| 90 | + if(isArray(argi)) { |
| 91 | + _constructor = false; |
| 92 | + } |
| 93 | + else { |
| 94 | + allArray = false; |
| 95 | + if(!totalLen) { |
| 96 | + _constructor = argi.constructor; |
| 97 | + } |
| 98 | + else if(_constructor !== argi.constructor) { |
| 99 | + // TODO: in principle we could upgrade here, |
| 100 | + // ie keep typed array but convert all to Float64Array? |
| 101 | + _constructor = false; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + totalLen += leni; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if(!totalLen) return []; |
| 110 | + if(!args.length) return arg0; |
| 111 | + |
| 112 | + if(allArray) return arg0.concat.apply(arg0, args); |
| 113 | + if(_constructor) { |
| 114 | + // matching typed arrays |
| 115 | + out = new _constructor(totalLen); |
| 116 | + out.set(arg0); |
| 117 | + for(i = 0; i < args.length; i++) { |
| 118 | + argi = args[i]; |
| 119 | + out.set(argi, posi); |
| 120 | + posi += argi.length; |
| 121 | + } |
| 122 | + return out; |
| 123 | + } |
| 124 | + |
| 125 | + // mismatched types or Array + typed |
| 126 | + out = new Array(totalLen); |
| 127 | + for(j = 0; j < arg0.length; j++) out[j] = arg0[j]; |
| 128 | + for(i = 0; i < args.length; i++) { |
| 129 | + argi = args[i]; |
| 130 | + for(j = 0; j < argi.length; j++) out[posi + j] = argi[j]; |
| 131 | + posi += j; |
| 132 | + } |
| 133 | + return out; |
| 134 | +}; |
0 commit comments