1(function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){ 2// Generated by LiveScript 1.3.1 3var svgMesh, extractSvgPath, loadSvg, threeSimplicialComplex; 4svgMesh = require("svg-mesh-3d"); 5extractSvgPath = require('extract-svg-path'); 6loadSvg = require('load-svg'); 7threeSimplicialComplex = require('three-simplicial-complex'); 8import$(window, { 9 svgMesh: svgMesh, 10 parsePath: extractSvgPath.parse, 11 loadSvg: loadSvg, 12 complex: threeSimplicialComplex(THREE) 13}); 14function import$(obj, src){ 15 var own = {}.hasOwnProperty; 16 for (var key in src) if (own.call(src, key)) obj[key] = src[key]; 17 return obj; 18} 19},{"extract-svg-path":41,"load-svg":46,"svg-mesh-3d":68,"three-simplicial-complex":70}],2:[function(require,module,exports){ 20 21module.exports = absolutize 22 23/** 24 * redefine `path` with absolute coordinates 25 * 26 * @param {Array} path 27 * @return {Array} 28 */ 29 30function absolutize(path){ 31 var startX = 0 32 var startY = 0 33 var x = 0 34 var y = 0 35 36 return path.map(function(seg){ 37 seg = seg.slice() 38 var type = seg[0] 39 var command = type.toUpperCase() 40 41 // is relative 42 if (type != command) { 43 seg[0] = command 44 switch (type) { 45 case 'a': 46 seg[6] += x 47 seg[7] += y 48 break 49 case 'v': 50 seg[1] += y 51 break 52 case 'h': 53 seg[1] += x 54 break 55 default: 56 for (var i = 1; i < seg.length;) { 57 seg[i++] += x 58 seg[i++] += y 59 } 60 } 61 } 62 63 // update cursor state 64 switch (command) { 65 case 'Z': 66 x = startX 67 y = startY 68 break 69 case 'H': 70 x = seg[1] 71 break 72 case 'V': 73 y = seg[1] 74 break 75 case 'M': 76 x = startX = seg[1] 77 y = startY = seg[2] 78 break 79 default: 80 x = seg[seg.length - 2] 81 y = seg[seg.length - 1] 82 } 83 84 return seg 85 }) 86} 87 88},{}],3:[function(require,module,exports){ 89function clone(point) { //TODO: use gl-vec2 for this 90 return [point[0], point[1]] 91} 92 93function vec2(x, y) { 94 return [x, y] 95} 96 97module.exports = function createBezierBuilder(opt) { 98 opt = opt||{} 99 100 var RECURSION_LIMIT = typeof opt.recursion === 'number' ? opt.recursion : 8 101 var FLT_EPSILON = typeof opt.epsilon === 'number' ? opt.epsilon : 1.19209290e-7 102 var PATH_DISTANCE_EPSILON = typeof opt.pathEpsilon === 'number' ? opt.pathEpsilon : 1.0 103 104 var curve_angle_tolerance_epsilon = typeof opt.angleEpsilon === 'number' ? opt.angleEpsilon : 0.01 105 var m_angle_tolerance = opt.angleTolerance || 0 106 var m_cusp_limit = opt.cuspLimit || 0 107 108 return function bezierCurve(start, c1, c2, end, scale, points) { 109 if (!points) 110 points = [] 111 112 scale = typeof scale === 'number' ? scale : 1.0 113 var distanceTolerance = PATH_DISTANCE_EPSILON / scale 114 distanceTolerance *= distanceTolerance 115 begin(start, c1, c2, end, points, distanceTolerance) 116 return points 117 } 118 119 120 ////// Based on: 121 ////// https://github.com/pelson/antigrain/blob/master/agg-2.4/src/agg_curves.cpp 122 123 function begin(start, c1, c2, end, points, distanceTolerance) { 124 points.push(clone(start)) 125 var x1 = start[0], 126 y1 = start[1], 127 x2 = c1[0], 128 y2 = c1[1], 129 x3 = c2[0], 130 y3 = c2[1], 131 x4 = end[0], 132 y4 = end[1] 133 recursive(x1, y1, x2, y2, x3, y3, x4, y4, points, distanceTolerance, 0) 134 points.push(clone(end)) 135 } 136 137 function recursive(x1, y1, x2, y2, x3, y3, x4, y4, points, distanceTolerance, level) { 138 if(level > RECURSION_LIMIT) 139 return 140 141 var pi = Math.PI 142 143 // Calculate all the mid-points of the line segments 144 //---------------------- 145 var x12 = (x1 + x2) / 2 146 var y12 = (y1 + y2) / 2 147 var x23 = (x2 + x3) / 2 148 var y23 = (y2 + y3) / 2 149 var x34 = (x3 + x4) / 2 150 var y34 = (y3 + y4) / 2 151 var x123 = (x12 + x23) / 2 152 var y123 = (y12 + y23) / 2 153 var x234 = (x23 + x34) / 2 154 var y234 = (y23 + y34) / 2 155 var x1234 = (x123 + x234) / 2 156 var y1234 = (y123 + y234) / 2 157 158 if(level > 0) { // Enforce subdivision first time 159 // Try to approximate the full cubic curve by a single straight line 160 //------------------ 161 var dx = x4-x1 162 var dy = y4-y1 163 164 var d2 = Math.abs((x2 - x4) * dy - (y2 - y4) * dx) 165 var d3 = Math.abs((x3 - x4) * dy - (y3 - y4) * dx) 166 167 var da1, da2 168 169 if(d2 > FLT_EPSILON && d3 > FLT_EPSILON) { 170 // Regular care 171 //----------------- 172 if((d2 + d3)*(d2 + d3) <= distanceTolerance * (dx*dx + dy*dy)) { 173 // If the curvature doesn't exceed the distanceTolerance value 174 // we tend to finish subdivisions. 175 //---------------------- 176 if(m_angle_tolerance < curve_angle_tolerance_epsilon) { 177 points.push(vec2(x1234, y1234)) 178 return 179 } 180 181 // Angle & Cusp Condition 182 //---------------------- 183 var a23 = Math.atan2(y3 - y2, x3 - x2) 184 da1 = Math.abs(a23 - Math.atan2(y2 - y1, x2 - x1)) 185 da2 = Math.abs(Math.atan2(y4 - y3, x4 - x3) - a23) 186 if(da1 >= pi) da1 = 2*pi - da1 187 if(da2 >= pi) da2 = 2*pi - da2 188 189 if(da1 + da2 < m_angle_tolerance) { 190 // Finally we can stop the recursion 191 //---------------------- 192 points.push(vec2(x1234, y1234)) 193 return 194 } 195 196 if(m_cusp_limit !== 0.0) { 197 if(da1 > m_cusp_limit) { 198 points.push(vec2(x2, y2)) 199 return 200 } 201 202 if(da2 > m_cusp_limit) { 203 points.push(vec2(x3, y3)) 204 return 205 } 206 } 207 } 208 } 209 else { 210 if(d2 > FLT_EPSILON) { 211 // p1,p3,p4 are collinear, p2 is considerable 212 //---------------------- 213 if(d2 * d2 <= distanceTolerance * (dx*dx + dy*dy)) { 214 if(m_angle_tolerance < curve_angle_tolerance_epsilon) { 215 points.push(vec2(x1234, y1234)) 216 return 217 } 218 219 // Angle Condition 220 //---------------------- 221 da1 = Math.abs(Math.atan2(y3 - y2, x3 - x2) - Math.atan2(y2 - y1, x2 - x1)) 222 if(da1 >= pi) da1 = 2*pi - da1 223 224 if(da1 < m_angle_tolerance) { 225 points.push(vec2(x2, y2)) 226 points.push(vec2(x3, y3)) 227 return 228 } 229 230 if(m_cusp_limit !== 0.0) { 231 if(da1 > m_cusp_limit) { 232 points.push(vec2(x2, y2)) 233 return 234 } 235 } 236 } 237 } 238 else if(d3 > FLT_EPSILON) { 239 // p1,p2,p4 are collinear, p3 is considerable 240 //---------------------- 241 if(d3 * d3 <= distanceTolerance * (dx*dx + dy*dy)) { 242 if(m_angle_tolerance < curve_angle_tolerance_epsilon) { 243 points.push(vec2(x1234, y1234)) 244 return 245 } 246 247 // Angle Condition 248 //---------------------- 249 da1 = Math.abs(Math.atan2(y4 - y3, x4 - x3) - Math.atan2(y3 - y2, x3 - x2)) 250 if(da1 >= pi) da1 = 2*pi - da1 251 252 if(da1 < m_angle_tolerance) { 253 points.push(vec2(x2, y2)) 254 points.push(vec2(x3, y3)) 255 return 256 } 257 258 if(m_cusp_limit !== 0.0) { 259 if(da1 > m_cusp_limit) 260 { 261 points.push(vec2(x3, y3)) 262 return 263 } 264 } 265 } 266 } 267 else { 268 // Collinear case 269 //----------------- 270 dx = x1234 - (x1 + x4) / 2 271 dy = y1234 - (y1 + y4) / 2 272 if(dx*dx + dy*dy <= distanceTolerance) { 273 points.push(vec2(x1234, y1234)) 274 return 275 } 276 } 277 } 278 } 279 280 // Continue subdivision 281 //---------------------- 282 recursive(x1, y1, x12, y12, x123, y123, x1234, y1234, points, distanceTolerance, level + 1) 283 recursive(x1234, y1234, x234, y234, x34, y34, x4, y4, points, distanceTolerance, level + 1) 284 } 285} 286 287},{}],4:[function(require,module,exports){ 288module.exports = require('./function')() 289},{"./function":3}],5:[function(require,module,exports){ 290'use strict' 291 292var rationalize = require('./lib/rationalize') 293 294module.exports = add 295 296function add(a, b) { 297 return rationalize( 298 a[0].mul(b[1]).add(b[0].mul(a[1])),
299 a[1].mul(b[1])) 300} 301 302},{"./lib/rationalize":15}],6:[function(require,module,exports){ 303'use strict' 304 305module.exports = cmp 306 307function cmp(a, b) { 308 return a[0].mul(b[1]).cmp(b[0].mul(a[1])) 309} 310 311},{}],7:[function(require,module,exports){ 312'use strict' 313 314var rationalize = require('./lib/rationalize') 315 316module.exports = div 317 318function div(a, b) { 319 return rationalize(a[0].mul(b[1]), a[1].mul(b[0])) 320} 321 322},{"./lib/rationalize":15}],8:[function(require,module,exports){ 323'use strict' 324 325var isRat = require('./is-rat') 326var isBN = require('./lib/is-bn') 327var num2bn = require('./lib/num-to-bn') 328var str2bn = require('./lib/str-to-bn') 329var rationalize = require('./lib/rationalize') 330var div = require('./div') 331 332module.exports = makeRational 333 334function makeRational(numer, denom) { 335 if(isRat(numer)) { 336 if(denom) { 337 return div(numer, makeRational(denom)) 338 } 339 return [numer[0].clone(), numer[1].clone()] 340 } 341 var shift = 0 342 var a, b 343 if(isBN(numer)) { 344 a = numer.clone() 345 } else if(typeof numer === 'string') { 346 a = str2bn(numer) 347 } else if(numer === 0) { 348 return [num2bn(0), num2bn(1)] 349 } else if(numer === Math.floor(numer)) { 350 a = num2bn(numer) 351 } else { 352 while(numer !== Math.floor(numer)) { 353 numer = numer * Math.pow(2, 256) 354 shift -= 256 355 } 356 a = num2bn(numer) 357 } 358 if(isRat(denom)) { 359 a.mul(denom[1]) 360 b = denom[0].clone() 361 } else if(isBN(denom)) { 362 b = denom.clone() 363 } else if(typeof denom === 'string') { 364 b = str2bn(denom) 365 } else if(!denom) { 366 b = num2bn(1) 367 } else if(denom === Math.floor(denom)) { 368 b = num2bn(denom) 369 } else { 370 while(denom !== Math.floor(denom)) { 371 denom = denom * Math.pow(2, 256) 372 shift += 256 373 } 374 b = num2bn(denom) 375 } 376 if(shift > 0) { 377 a = a.ushln(shift) 378 } else if(shift < 0) { 379 b = b.ushln(-shift) 380 } 381 return rationalize(a, b) 382} 383 384},{"./div":7,"./is-rat":9,"./lib/is-bn":13,"./lib/num-to-bn":14,"./lib/rationalize":15,"./lib/str-to-bn":16}],9:[function(require,module,exports){ 385'use strict' 386 387var isBN = require('./lib/is-bn') 388 389module.exports = isRat 390 391function isRat(x) { 392 return Array.isArray(x) && x.length === 2 && isBN(x[0]) && isBN(x[1]) 393} 394 395},{"./lib/is-bn":13}],10:[function(require,module,exports){ 396'use strict' 397 398var BN = require('bn.js') 399 400module.exports = sign 401 402function sign (x) { 403 return x.cmp(new BN(0)) 404} 405 406},{"bn.js":23}],11:[function(require,module,exports){ 407'use strict' 408 409var sign = require('./bn-sign') 410 411module.exports = bn2num 412 413//TODO: Make this better 414function bn2num(b) { 415 var l = b.length 416 var words = b.words 417 var out = 0 418 if (l === 1) { 419 out = words[0] 420 } else if (l === 2) { 421 out = words[0] + (words[1] * 0x4000000) 422 } else { 423 for (var i = 0; i < l; i++) { 424 var w = words[i] 425 out += w * Math.pow(0x4000000, i) 426 } 427 } 428 return sign(b) * out 429} 430 431},{"./bn-sign":10}],12:[function(require,module,exports){ 432'use strict' 433 434var db = require('double-bits') 435var ctz = require('bit-twiddle').countTrailingZeros 436 437module.exports = ctzNumber 438 439//Counts the number of trailing zeros 440function ctzNumber(x) { 441 var l = ctz(db.lo(x)) 442 if(l < 32) { 443 return l 444 } 445 var h = ctz(db.hi(x)) 446 if(h > 20) { 447 return 52 448 } 449 return h + 32 450} 451 452},{"bit-twiddle":22,"double-bits":39}],13:[function(require,module,exports){ 453'use strict' 454 455var BN = require('bn.js') 456 457module.exports = isBN 458 459//Test if x is a bignumber 460//FIXME: obviously this is the wrong way to do it 461function isBN(x) { 462 return x && typeof x === 'object' && Boolean(x.words) 463} 464 465},{"bn.js":23}],14:[function(require,module,exports){ 466'use strict' 467 468var BN = require('bn.js') 469var db = require('double-bits') 470 471module.exports = num2bn 472 473function num2bn(x) { 474 var e = db.exponent(x) 475 if(e < 52) { 476 return new BN(x) 477 } else { 478 return (new BN(x * Math.pow(2, 52-e))).ushln(e-52) 479 } 480} 481 482},{"bn.js":23,"double-bits":39}],15:[function(require,module,exports){ 483'use strict' 484 485var num2bn = require('./num-to-bn') 486var sign = require('./bn-sign') 487 488module.exports = rationalize 489 490function rationalize(numer, denom) { 491 var snumer = sign(numer) 492 var sdenom = sign(denom) 493 if(snumer === 0) { 494 return [num2bn(0), num2bn(1)] 495 } 496 if(sdenom === 0) { 497 return [num2bn(0), num2bn(0)] 498 } 499 if(sdenom < 0) { 500 numer = numer.neg() 501 denom = denom.neg() 502 }
503 var d = numer.gcd(denom) 504 if(d.cmpn(1)) { 505 return [ numer.div(d), denom.div(d) ] 506 } 507 return [ numer, denom ] 508} 509 510},{"./bn-sign":10,"./num-to-bn":14}],16:[function(require,module,exports){ 511'use strict' 512 513var BN = require('bn.js') 514 515module.exports = str2BN 516 517function str2BN(x) { 518 return new BN(x) 519} 520 521},{"bn.js":23}],17:[function(require,module,exports){ 522'use strict' 523 524var rationalize = require('./lib/rationalize') 525 526module.exports = mul 527 528function mul(a, b) { 529 return rationalize(a[0].mul(b[0]), a[1].mul(b[1])) 530} 531 532},{"./lib/rationalize":15}],18:[function(require,module,exports){ 533'use strict' 534 535var bnsign = require('./lib/bn-sign') 536 537module.exports = sign 538 539function sign(x) { 540 return bnsign(x[0]) * bnsign(x[1]) 541} 542 543},{"./lib/bn-sign":10}],19:[function(require,module,exports){ 544'use strict' 545 546var rationalize = require('./lib/rationalize') 547 548module.exports = sub 549 550function sub(a, b) { 551 return rationalize(a[0].mul(b[1]).sub(a[1].mul(b[0])), a[1].mul(b[1])) 552} 553 554},{"./lib/rationalize":15}],20:[function(require,module,exports){ 555'use strict' 556 557var bn2num = require('./lib/bn-to-num') 558var ctz = require('./lib/ctz') 559 560module.exports = roundRat 561 562// Round a rational to the closest float 563function roundRat (f) { 564 var a = f[0] 565 var b = f[1] 566 if (a.cmpn(0) === 0) { 567 return 0 568 } 569 var h = a.abs().divmod(b.abs()) 570 var iv = h.div 571 var x = bn2num(iv) 572 var ir = h.mod 573 var sgn = (a.negative !== b.negative) ? -1 : 1 574 if (ir.cmpn(0) === 0) { 575 return sgn * x 576 } 577 if (x) { 578 var s = ctz(x) + 4 579 var y = bn2num(ir.ushln(s).divRound(b)) 580 return sgn * (x + y * Math.pow(2, -s)) 581 } else { 582 var ybits = b.bitLength() - ir.bitLength() + 53 583 var y = bn2num(ir.ushln(ybits).divRound(b)) 584 if (ybits < 1023) { 585 return sgn * y * Math.pow(2, -ybits) 586 } 587 y *= Math.pow(2, -1023) 588 return sgn * y * Math.pow(2, 1023 - ybits) 589 } 590} 591 592},{"./lib/bn-to-num":11,"./lib/ctz":12}],21:[function(require,module,exports){ 593"use strict" 594 595function compileSearch(funcName, predicate, reversed, extraArgs, earlyOut) { 596 var code = [ 597 "function ", funcName, "(a,l,h,", extraArgs.join(","), "){", 598 earlyOut ? "" : "var i=", (reversed ? "l-1" : "h+1"), 599 ";while(l<=h){var m=(l+h)>>>1,x=a[m]"] 600 if(earlyOut) { 601 if(predicate.indexOf("c") < 0) { 602 code.push(";if(x===y){return m}else if(x<=y){") 603 } else { 604 code.push(";var p=c(x,y);if(p===0){return m}else if(p<=0){") 605 } 606 } else { 607 code.push(";if(", predicate, "){i=m;") 608 } 609 if(reversed) { 610 code.push("l=m+1}else{h=m-1}") 611 } else { 612 code.push("h=m-1}else{l=m+1}") 613 } 614 code.push("}") 615 if(earlyOut) { 616 code.push("return -1};") 617 } else { 618 code.push("return i};") 619 } 620 return code.join("") 621} 622 623function compileBoundsSearch(predicate, reversed, suffix, earlyOut) { 624 var result = new Function([ 625 compileSearch("A", "x" + predicate + "y", reversed, ["y"], earlyOut), 626 compileSearch("P", "c(x,y)" + predicate + "0", reversed, ["y", "c"], earlyOut), 627"function dispatchBsearch", suffix, "(a,y,c,l,h){\ 628if(typeof(c)==='function'){\ 629return P(a,(l===void 0)?0:l|0,(h===void 0)?a.length-1:h|0,y,c)\ 630}else{\ 631return A(a,(c===void 0)?0:c|0,(l===void 0)?a.length-1:l|0,y)\ 632}}\ 633return dispatchBsearch", suffix].join("")) 634 return result() 635} 636 637module.exports = { 638 ge: compileBoundsSearch(">=", false, "GE"), 639 gt: compileBoundsSearch(">", false, "GT"), 640 lt: compileBoundsSearch("<", true, "LT"), 641 le: compileBoundsSearch("<=", true, "LE"), 642 eq: compileBoundsSearch("-", true, "EQ", true) 643} 644 645},{}],22:[function(require,module,exports){ 646/** 647 * Bit twiddling hacks for JavaScript. 648 * 649 * Author: Mikola Lysenko 650 * 651 * Ported from Stanford bit twiddling hack library: 652 * http://graphics.stanford.edu/~seander/bithacks.html 653 */ 654 655"use strict"; "use restrict"; 656 657//Number of bits in an integer 658var INT_BITS = 32; 659 660//Constants 661exports.INT_BITS = INT_BITS; 662exports.INT_MAX = 0x7fffffff; 663exports.INT_MIN = -1<<(INT_BITS-1); 664 665//Returns -1, 0, +1 depending on sign of x 666exports.sign = function(v) { 667 return (v > 0) - (v < 0); 668} 669 670//Computes absolute value of integer 671exports.abs = function(v) { 672 var mask = v >> (INT_BITS-1); 673 return (v ^ mask) - mask; 674} 675 676//Computes minimum of integers x and y 677exports.min = function(x, y) { 678 return y ^ ((x ^ y) & -(x < y)); 679} 680 681//Computes maximum of integers x and y 682exports.max = function(x, y) { 683 return x ^ ((x ^ y) & -(x < y)); 684} 685 686//Checks if a number is a power of two 687exports.isPow2 = function(v) { 688 return !(v & (v-1)) && (!!v); 689} 690 691//Computes log base 2 of v 692exports.log2 = function(v) { 693 var r, shift; 694 r = (v > 0xFFFF) << 4; v >>>= r; 695 shift = (v > 0xFF ) << 3; v >>>= shift; r |= shift; 696 shift = (v > 0xF ) << 2; v >>>= shift; r |= shift; 697 shift = (v > 0x3 ) << 1; v >>>= shift; r |= shift; 698 return r | (v >> 1); 699} 700 701//Computes log base 10 of v 702exports.log10 = function(v) { 703 return (v >= 1000000000) ? 9 : (v >= 100000000) ? 8 : (v >= 10000000) ? 7 : 704 (v >= 1000000) ? 6 : (v >= 100000) ? 5 : (v >= 10000) ? 4 : 705 (v >= 1000) ? 3 : (v >= 100) ? 2 : (v >= 10) ? 1 : 0; 706} 707 708//Counts number of bits 709exports.popCount = function(v) { 710 v = v - ((v >>> 1) & 0x55555555); 711 v = (v & 0x33333333) + ((v >>> 2) & 0x33333333); 712 return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24; 713} 714 715//Counts number of trailing zeros 716function countTrailingZeros(v) { 717 var c = 32; 718 v &= -v; 719 if (v) c--; 720 if (v & 0x0000FFFF) c -= 16; 721 if (v & 0x00FF00FF) c -= 8; 722 if (v & 0x0F0F0F0F) c -= 4; 723 if (v & 0x33333333) c -= 2; 724 if (v & 0x55555555) c -= 1; 725 return c; 726} 727exports.countTrailingZeros = countTrailingZeros; 728 729//Rounds to next power of 2 730exports.nextPow2 = function(v) { 731 v += v === 0; 732 --v; 733 v |= v >>> 1; 734 v |= v >>> 2; 735 v |= v >>> 4; 736 v |= v >>> 8; 737 v |= v >>> 16; 738 return v + 1; 739} 740 741//Rounds down to previous power of 2 742exports.prevPow2 = function(v) { 743 v |= v >>> 1; 744 v |= v >>> 2; 745 v |= v >>> 4; 746 v |= v >>> 8; 747 v |= v >>> 16; 748 return v - (v>>>1); 749} 750 751//Computes parity of word 752exports.parity = function(v) { 753 v ^= v >>> 16; 754 v ^= v >>> 8; 755 v ^= v >>> 4; 756 v &= 0xf; 757 return (0x6996 >>> v) & 1; 758} 759 760var REVERSE_TABLE = new Array(256); 761 762(function(tab) { 763 for(var i=0; i<256; ++i) { 764 var v = i, r = i, s = 7; 765 for (v >>>= 1; v; v >>>= 1) { 766 r <<= 1; 767 r |= v & 1; 768 --s; 769 } 770 tab[i] = (r << s) & 0xff;
vendor: 9,438 bytes, lines 771-1157
771 } 772})(REVERSE_TABLE); 773 774//Reverse bits in a 32 bit word 775exports.reverse = function(v) { 776 return (REVERSE_TABLE[ v & 0xff] << 24) | 777 (REVERSE_TABLE[(v >>> 8) & 0xff] << 16) | 778 (REVERSE_TABLE[(v >>> 16) & 0xff] << 8) | 779 REVERSE_TABLE[(v >>> 24) & 0xff]; 780} 781 782//Interleave bits of 2 coordinates with 16 bits. Useful for fast quadtree codes 783exports.interleave2 = function(x, y) { 784 x &= 0xFFFF; 785 x = (x | (x << 8)) & 0x00FF00FF; 786 x = (x | (x << 4)) & 0x0F0F0F0F; 787 x = (x | (x << 2)) & 0x33333333; 788 x = (x | (x << 1)) & 0x55555555; 789 790 y &= 0xFFFF; 791 y = (y | (y << 8)) & 0x00FF00FF; 792 y = (y | (y << 4)) & 0x0F0F0F0F; 793 y = (y | (y << 2)) & 0x33333333; 794 y = (y | (y << 1)) & 0x55555555; 795 796 return x | (y << 1); 797} 798 799//Extracts the nth interleaved component 800exports.deinterleave2 = function(v, n) { 801 v = (v >>> n) & 0x55555555; 802 v = (v | (v >>> 1)) & 0x33333333; 803 v = (v | (v >>> 2)) & 0x0F0F0F0F; 804 v = (v | (v >>> 4)) & 0x00FF00FF; 805 v = (v | (v >>> 16)) & 0x000FFFF; 806 return (v << 16) >> 16; 807} 808 809 810//Interleave bits of 3 coordinates, each with 10 bits. Useful for fast octree codes 811exports.interleave3 = function(x, y, z) { 812 x &= 0x3FF; 813 x = (x | (x<<16)) & 4278190335; 814 x = (x | (x<<8)) & 251719695; 815 x = (x | (x<<4)) & 3272356035; 816 x = (x | (x<<2)) & 1227133513; 817 818 y &= 0x3FF; 819 y = (y | (y<<16)) & 4278190335; 820 y = (y | (y<<8)) & 251719695; 821 y = (y | (y<<4)) & 3272356035; 822 y = (y | (y<<2)) & 1227133513; 823 x |= (y << 1); 824 825 z &= 0x3FF; 826 z = (z | (z<<16)) & 4278190335; 827 z = (z | (z<<8)) & 251719695; 828 z = (z | (z<<4)) & 3272356035; 829 z = (z | (z<<2)) & 1227133513; 830 831 return x | (z << 2); 832} 833 834//Extracts nth interleaved component of a 3-tuple 835exports.deinterleave3 = function(v, n) { 836 v = (v >>> n) & 1227133513; 837 v = (v | (v>>>2)) & 3272356035; 838 v = (v | (v>>>4)) & 251719695; 839 v = (v | (v>>>8)) & 4278190335; 840 v = (v | (v>>>16)) & 0x3FF; 841 return (v<<22)>>22; 842} 843 844//Computes next combination in colexicographic order (this is mistakenly called nextPermutation on the bit twiddling hacks page) 845exports.nextCombination = function(v) { 846 var t = v | (v - 1); 847 return (t + 1) | (((~t & -~t) - 1) >>> (countTrailingZeros(v) + 1)); 848} 849 850 851},{}],23:[function(require,module,exports){ 852(function (module, exports) { 853 'use strict'; 854 855 // Utils 856 function assert (val, msg) { 857 if (!val) throw new Error(msg || 'Assertion failed'); 858 } 859 860 // Could use `inherits` module, but don't want to move from single file 861 // architecture yet. 862 function inherits (ctor, superCtor) { 863 ctor.super_ = superCtor; 864 var TempCtor = function () {}; 865 TempCtor.prototype = superCtor.prototype; 866 ctor.prototype = new TempCtor(); 867 ctor.prototype.constructor = ctor; 868 } 869 870 // BN 871 872 function BN (number, base, endian) { 873 if (BN.isBN(number)) { 874 return number; 875 } 876 877 this.negative = 0; 878 this.words = null; 879 this.length = 0; 880 881 // Reduction context 882 this.red = null; 883 884 if (number !== null) { 885 if (base === 'le' || base === 'be') { 886 endian = base; 887 base = 10; 888 } 889 890 this._init(number || 0, base || 10, endian || 'be'); 891 } 892 } 893 if (typeof module === 'object') { 894 module.exports = BN; 895 } else { 896 exports.BN = BN; 897 } 898 899 BN.BN = BN; 900 BN.wordSize = 26; 901 902 var Buffer; 903 try { 904 Buffer = require('buffer').Buffer; 905 } catch (e) { 906 } 907 908 BN.isBN = function isBN (num) { 909 if (num instanceof BN) { 910 return true; 911 } 912 913 return num !== null && typeof num === 'object' && 914 num.constructor.wordSize === BN.wordSize && Array.isArray(num.words); 915 }; 916 917 BN.max = function max (left, right) { 918 if (left.cmp(right) > 0) return left; 919 return right; 920 }; 921 922 BN.min = function min (left, right) { 923 if (left.cmp(right) < 0) return left; 924 return right; 925 }; 926 927 BN.prototype._init = function init (number, base, endian) { 928 if (typeof number === 'number') { 929 return this._initNumber(number, base, endian); 930 } 931 932 if (typeof number === 'object') { 933 return this._initArray(number, base, endian); 934 } 935 936 if (base === 'hex') { 937 base = 16; 938 } 939 assert(base === (base | 0) && base >= 2 && base <= 36); 940 941 number = number.toString().replace(/\s+/g, ''); 942 var start = 0; 943 if (number[0] === '-') { 944 start++; 945 } 946 947 if (base === 16) { 948 this._parseHex(number, start); 949 } else { 950 this._parseBase(number, base, start); 951 } 952 953 if (number[0] === '-') { 954 this.negative = 1; 955 } 956 957 this.strip(); 958 959 if (endian !== 'le') return; 960 961 this._initArray(this.toArray(), base, endian); 962 }; 963 964 BN.prototype._initNumber = function _initNumber (number, base, endian) { 965 if (number < 0) { 966 this.negative = 1; 967 number = -number; 968 } 969 if (number < 0x4000000) { 970 this.words = [ number & 0x3ffffff ]; 971 this.length = 1; 972 } else if (number < 0x10000000000000) { 973 this.words = [ 974 number & 0x3ffffff, 975 (number / 0x4000000) & 0x3ffffff 976 ]; 977 this.length = 2; 978 } else { 979 assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) 980 this.words = [ 981 number & 0x3ffffff, 982 (number / 0x4000000) & 0x3ffffff, 983 1 984 ]; 985 this.length = 3; 986 } 987 988 if (endian !== 'le') return; 989 990 // Reverse the bytes 991 this._initArray(this.toArray(), base, endian); 992 }; 993 994 BN.prototype._initArray = function _initArray (number, base, endian) { 995 // Perhaps a Uint8Array 996 assert(typeof number.length === 'number'); 997 if (number.length <= 0) { 998 this.words = [ 0 ]; 999 this.length = 1; 1000 return this; 1001 } 1002 1003 this.length = Math.ceil(number.length / 3); 1004 this.words = new Array(this.length); 1005 for (var i = 0; i < this.length; i++) { 1006 this.words[i] = 0; 1007 } 1008 1009 var j, w; 1010 var off = 0; 1011 if (endian === 'be') { 1012 for (i = number.length - 1, j = 0; i >= 0; i -= 3) { 1013 w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16); 1014 this.words[j] |= (w << off) & 0x3ffffff; 1015 this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; 1016 off += 24; 1017 if (off >= 26) { 1018 off -= 26; 1019 j++; 1020 } 1021 } 1022 } else if (endian === 'le') { 1023 for (i = 0, j = 0; i < number.length; i += 3) { 1024 w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16); 1025 this.words[j] |= (w << off) & 0x3ffffff; 1026 this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; 1027 off += 24; 1028 if (off >= 26) { 1029 off -= 26; 1030 j++; 1031 } 1032 } 1033 } 1034 return this.strip(); 1035 }; 1036 1037 function parseHex (str, start, end) { 1038 var r = 0; 1039 var len = Math.min(str.length, end); 1040 for (var i = start; i < len; i++) { 1041 var c = str.charCodeAt(i) - 48; 1042 1043 r <<= 4; 1044 1045 // 'a' - 'f' 1046 if (c >= 49 && c <= 54) { 1047 r |= c - 49 + 0xa; 1048 1049 // 'A' - 'F' 1050 } else if (c >= 17 && c <= 22) { 1051 r |= c - 17 + 0xa; 1052 1053 // '0' - '9' 1054 } else { 1055 r |= c & 0xf; 1056 } 1057 } 1058 return r; 1059 } 1060 1061 BN.prototype._parseHex = function _parseHex (number, start) { 1062 // Create possibly bigger array to ensure that it fits the number 1063 this.length = Math.ceil((number.length - start) / 6); 1064 this.words = new Array(this.length); 1065 for (var i = 0; i < this.length; i++) { 1066 this.words[i] = 0; 1067 } 1068 1069 var j, w; 1070 // Scan 24-bit chunks and add them to the number 1071 var off = 0; 1072 for (i = number.length - 6, j = 0; i >= start; i -= 6) { 1073 w = parseHex(number, i, i + 6); 1074 this.words[j] |= (w << off) & 0x3ffffff; 1075 // NOTE: `0x3fffff` is intentional here, 26bits max shift + 24bit hex limb 1076 this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; 1077 off += 24; 1078 if (off >= 26) { 1079 off -= 26; 1080 j++; 1081 } 1082 } 1083 if (i + 6 !== start) { 1084 w = parseHex(number, start, i + 6); 1085 this.words[j] |= (w << off) & 0x3ffffff; 1086 this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; 1087 } 1088 this.strip(); 1089 }; 1090 1091 function parseBase (str, start, end, mul) { 1092 var r = 0; 1093 var len = Math.min(str.length, end); 1094 for (var i = start; i < len; i++) { 1095 var c = str.charCodeAt(i) - 48; 1096 1097 r *= mul; 1098 1099 // 'a' 1100 if (c >= 49) { 1101 r += c - 49 + 0xa; 1102 1103 // 'A' 1104 } else if (c >= 17) { 1105 r += c - 17 + 0xa; 1106 1107 // '0' - '9' 1108 } else { 1109 r += c; 1110 } 1111 } 1112 return r; 1113 } 1114 1115 BN.prototype._parseBase = function _parseBase (number, base, start) { 1116 // Initialize as zero 1117 this.words = [ 0 ]; 1118 this.length = 1; 1119 1120 // Find length of limb in base 1121 for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) { 1122 limbLen++; 1123 } 1124 limbLen--; 1125 limbPow = (limbPow / base) | 0; 1126 1127 var total = number.length - start; 1128 var mod = total % limbLen; 1129 var end = Math.min(total, total - mod) + start; 1130 1131 var word = 0; 1132 for (var i = start; i < end; i += limbLen) { 1133 word = parseBase(number, i, i + limbLen, base); 1134 1135 this.imuln(limbPow); 1136 if (this.words[0] + word < 0x4000000) { 1137 this.words[0] += word; 1138 } else { 1139 this._iaddn(word); 1140 } 1141 } 1142 1143 if (mod !== 0) { 1144 var pow = 1; 1145 word = parseBase(number, i, number.length, base); 1146 1147 for (i = 0; i < mod; i++) { 1148 pow *= base; 1149 } 1150 1151 this.imuln(pow); 1152 if (this.words[0] + word < 0x4000000) { 1153 this.words[0] += word; 1154 } else { 1155 this._iaddn(word); 1156 } 1157 }
vendor: 68,018 bytes, lines 1157-3761
1157 1158 }; 1159 1160 BN.prototype.copy = function copy (dest) { 1161 dest.words = new Array(this.length); 1162 for (var i = 0; i < this.length; i++) { 1163 dest.words[i] = this.words[i]; 1164 } 1165 dest.length = this.length; 1166 dest.negative = this.negative; 1167 dest.red = this.red; 1168 }; 1169 1170 BN.prototype.clone = function clone () { 1171 var r = new BN(null); 1172 this.copy(r); 1173 return r; 1174 }; 1175 1176 BN.prototype._expand = function _expand (size) { 1177 while (this.length < size) { 1178 this.words[this.length++] = 0; 1179 } 1180 return this; 1181 }; 1182 1183 // Remove leading `0` from `this` 1184 BN.prototype.strip = function strip () { 1185 while (this.length > 1 && this.words[this.length - 1] === 0) { 1186 this.length--; 1187 } 1188 return this._normSign(); 1189 }; 1190 1191 BN.prototype._normSign = function _normSign () { 1192 // -0 = 0 1193 if (this.length === 1 && this.words[0] === 0) { 1194 this.negative = 0; 1195 } 1196 return this; 1197 }; 1198 1199 BN.prototype.inspect = function inspect () { 1200 return (this.red ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>'; 1201 }; 1202 1203 /* 1204 1205 var zeros = []; 1206 var groupSizes = []; 1207 var groupBases = []; 1208 1209 var s = ''; 1210 var i = -1; 1211 while (++i < BN.wordSize) { 1212 zeros[i] = s; 1213 s += '0'; 1214 } 1215 groupSizes[0] = 0; 1216 groupSizes[1] = 0; 1217 groupBases[0] = 0; 1218 groupBases[1] = 0; 1219 var base = 2 - 1; 1220 while (++base < 36 + 1) { 1221 var groupSize = 0; 1222 var groupBase = 1; 1223 while (groupBase < (1 << BN.wordSize) / base) { 1224 groupBase *= base; 1225 groupSize += 1; 1226 } 1227 groupSizes[base] = groupSize; 1228 groupBases[base] = groupBase; 1229 } 1230 1231 */ 1232 1233 var zeros = [ 1234 '', 1235 '0', 1236 '00', 1237 '000', 1238 '0000', 1239 '00000', 1240 '000000', 1241 '0000000', 1242 '00000000', 1243 '000000000', 1244 '0000000000', 1245 '00000000000', 1246 '000000000000', 1247 '0000000000000', 1248 '00000000000000', 1249 '000000000000000', 1250 '0000000000000000', 1251 '00000000000000000', 1252 '000000000000000000', 1253 '0000000000000000000', 1254 '00000000000000000000', 1255 '000000000000000000000', 1256 '0000000000000000000000', 1257 '00000000000000000000000', 1258 '000000000000000000000000', 1259 '0000000000000000000000000' 1260 ]; 1261 1262 var groupSizes = [ 1263 0, 0, 1264 25, 16, 12, 11, 10, 9, 8, 1265 8, 7, 7, 7, 7, 6, 6, 1266 6, 6, 6, 6, 6, 5, 5, 1267 5, 5, 5, 5, 5, 5, 5, 1268 5, 5, 5, 5, 5, 5, 5 1269 ]; 1270 1271 var groupBases = [ 1272 0, 0, 1273 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216, 1274 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625, 1275 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632, 1276 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149, 1277 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176 1278 ]; 1279 1280 BN.prototype.toString = function toString (base, padding) { 1281 base = base || 10; 1282 padding = padding | 0 || 1; 1283 1284 var out; 1285 if (base === 16 || base === 'hex') { 1286 out = ''; 1287 var off = 0; 1288 var carry = 0; 1289 for (var i = 0; i < this.length; i++) { 1290 var w = this.words[i]; 1291 var word = (((w << off) | carry) & 0xffffff).toString(16); 1292 carry = (w >>> (24 - off)) & 0xffffff; 1293 if (carry !== 0 || i !== this.length - 1) { 1294 out = zeros[6 - word.length] + word + out; 1295 } else { 1296 out = word + out; 1297 } 1298 off += 2; 1299 if (off >= 26) { 1300 off -= 26; 1301 i--; 1302 } 1303 } 1304 if (carry !== 0) { 1305 out = carry.toString(16) + out; 1306 } 1307 while (out.length % padding !== 0) { 1308 out = '0' + out; 1309 } 1310 if (this.negative !== 0) { 1311 out = '-' + out; 1312 } 1313 return out; 1314 } 1315 1316 if (base === (base | 0) && base >= 2 && base <= 36) { 1317 // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base)); 1318 var groupSize = groupSizes[base]; 1319 // var groupBase = Math.pow(base, groupSize); 1320 var groupBase = groupBases[base]; 1321 out = ''; 1322 var c = this.clone(); 1323 c.negative = 0; 1324 while (!c.isZero()) { 1325 var r = c.modn(groupBase).toString(base); 1326 c = c.idivn(groupBase); 1327 1328 if (!c.isZero()) { 1329 out = zeros[groupSize - r.length] + r + out; 1330 } else { 1331 out = r + out; 1332 } 1333 } 1334 if (this.isZero()) { 1335 out = '0' + out; 1336 } 1337 while (out.length % padding !== 0) { 1338 out = '0' + out; 1339 } 1340 if (this.negative !== 0) { 1341 out = '-' + out; 1342 } 1343 return out; 1344 } 1345 1346 assert(false, 'Base should be between 2 and 36'); 1347 }; 1348 1349 BN.prototype.toNumber = function toNumber () { 1350 var ret = this.words[0]; 1351 if (this.length === 2) { 1352 ret += this.words[1] * 0x4000000; 1353 } else if (this.length === 3 && this.words[2] === 0x01) { 1354 // NOTE: at this stage it is known that the top bit is set 1355 ret += 0x10000000000000 + (this.words[1] * 0x4000000); 1356 } else if (this.length > 2) { 1357 assert(false, 'Number can only safely store up to 53 bits'); 1358 } 1359 return (this.negative !== 0) ? -ret : ret; 1360 }; 1361 1362 BN.prototype.toJSON = function toJSON () { 1363 return this.toString(16); 1364 }; 1365 1366 BN.prototype.toBuffer = function toBuffer (endian, length) { 1367 assert(typeof Buffer !== 'undefined'); 1368 return this.toArrayLike(Buffer, endian, length); 1369 }; 1370 1371 BN.prototype.toArray = function toArray (endian, length) { 1372 return this.toArrayLike(Array, endian, length); 1373 }; 1374 1375 BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) { 1376 var byteLength = this.byteLength(); 1377 var reqLength = length || Math.max(1, byteLength); 1378 assert(byteLength <= reqLength, 'byte array longer than desired length'); 1379 assert(reqLength > 0, 'Requested array length <= 0'); 1380 1381 this.strip(); 1382 var littleEndian = endian === 'le'; 1383 var res = new ArrayType(reqLength); 1384 1385 var b, i; 1386 var q = this.clone(); 1387 if (!littleEndian) { 1388 // Assume big-endian 1389 for (i = 0; i < reqLength - byteLength; i++) { 1390 res[i] = 0; 1391 } 1392 1393 for (i = 0; !q.isZero(); i++) { 1394 b = q.andln(0xff); 1395 q.iushrn(8); 1396 1397 res[reqLength - i - 1] = b; 1398 } 1399 } else { 1400 for (i = 0; !q.isZero(); i++) { 1401 b = q.andln(0xff); 1402 q.iushrn(8); 1403 1404 res[i] = b; 1405 } 1406 1407 for (; i < reqLength; i++) { 1408 res[i] = 0; 1409 } 1410 } 1411 1412 return res; 1413 }; 1414 1415 if (Math.clz32) { 1416 BN.prototype._countBits = function _countBits (w) { 1417 return 32 - Math.clz32(w); 1418 }; 1419 } else { 1420 BN.prototype._countBits = function _countBits (w) { 1421 var t = w; 1422 var r = 0; 1423 if (t >= 0x1000) { 1424 r += 13; 1425 t >>>= 13; 1426 } 1427 if (t >= 0x40) { 1428 r += 7; 1429 t >>>= 7; 1430 } 1431 if (t >= 0x8) { 1432 r += 4; 1433 t >>>= 4; 1434 } 1435 if (t >= 0x02) { 1436 r += 2; 1437 t >>>= 2; 1438 } 1439 return r + t; 1440 }; 1441 } 1442 1443 BN.prototype._zeroBits = function _zeroBits (w) { 1444 // Short-cut 1445 if (w === 0) return 26; 1446 1447 var t = w; 1448 var r = 0; 1449 if ((t & 0x1fff) === 0) { 1450 r += 13; 1451 t >>>= 13; 1452 } 1453 if ((t & 0x7f) === 0) { 1454 r += 7; 1455 t >>>= 7; 1456 } 1457 if ((t & 0xf) === 0) { 1458 r += 4; 1459 t >>>= 4; 1460 } 1461 if ((t & 0x3) === 0) { 1462 r += 2; 1463 t >>>= 2; 1464 } 1465 if ((t & 0x1) === 0) { 1466 r++; 1467 } 1468 return r; 1469 }; 1470 1471 // Return number of used bits in a BN 1472 BN.prototype.bitLength = function bitLength () { 1473 var w = this.words[this.length - 1]; 1474 var hi = this._countBits(w); 1475 return (this.length - 1) * 26 + hi; 1476 }; 1477 1478 function toBitArray (num) { 1479 var w = new Array(num.bitLength()); 1480 1481 for (var bit = 0; bit < w.length; bit++) { 1482 var off = (bit / 26) | 0; 1483 var wbit = bit % 26; 1484 1485 w[bit] = (num.words[off] & (1 << wbit)) >>> wbit; 1486 } 1487 1488 return w; 1489 } 1490 1491 // Number of trailing zero bits 1492 BN.prototype.zeroBits = function zeroBits () { 1493 if (this.isZero()) return 0; 1494 1495 var r = 0; 1496 for (var i = 0; i < this.length; i++) { 1497 var b = this._zeroBits(this.words[i]); 1498 r += b; 1499 if (b !== 26) break; 1500 } 1501 return r; 1502 }; 1503 1504 BN.prototype.byteLength = function byteLength () { 1505 return Math.ceil(this.bitLength() / 8); 1506 }; 1507 1508 BN.prototype.toTwos = function toTwos (width) { 1509 if (this.negative !== 0) { 1510 return this.abs().inotn(width).iaddn(1); 1511 } 1512 return this.clone(); 1513 }; 1514 1515 BN.prototype.fromTwos = function fromTwos (width) { 1516 if (this.testn(width - 1)) { 1517 return this.notn(width).iaddn(1).ineg(); 1518 } 1519 return this.clone(); 1520 }; 1521 1522 BN.prototype.isNeg = function isNeg () { 1523 return this.negative !== 0; 1524 }; 1525 1526 // Return negative clone of `this` 1527 BN.prototype.neg = function neg () { 1528 return this.clone().ineg(); 1529 }; 1530 1531 BN.prototype.ineg = function ineg () { 1532 if (!this.isZero()) { 1533 this.negative ^= 1; 1534 } 1535 1536 return this; 1537 }; 1538 1539 // Or `num` with `this` in-place 1540 BN.prototype.iuor = function iuor (num) { 1541 while (this.length < num.length) { 1542 this.words[this.length++] = 0; 1543 } 1544 1545 for (var i = 0; i < num.length; i++) { 1546 this.words[i] = this.words[i] | num.words[i]; 1547 } 1548 1549 return this.strip(); 1550 }; 1551 1552 BN.prototype.ior = function ior (num) { 1553 assert((this.negative | num.negative) === 0); 1554 return this.iuor(num); 1555 }; 1556 1557 // Or `num` with `this` 1558 BN.prototype.or = function or (num) { 1559 if (this.length > num.length) return this.clone().ior(num); 1560 return num.clone().ior(this); 1561 }; 1562 1563 BN.prototype.uor = function uor (num) { 1564 if (this.length > num.length) return this.clone().iuor(num); 1565 return num.clone().iuor(this); 1566 }; 1567 1568 // And `num` with `this` in-place 1569 BN.prototype.iuand = function iuand (num) { 1570 // b = min-length(num, this) 1571 var b; 1572 if (this.length > num.length) { 1573 b = num; 1574 } else { 1575 b = this; 1576 } 1577 1578 for (var i = 0; i < b.length; i++) { 1579 this.words[i] = this.words[i] & num.words[i]; 1580 } 1581 1582 this.length = b.length; 1583 1584 return this.strip(); 1585 }; 1586 1587 BN.prototype.iand = function iand (num) { 1588 assert((this.negative | num.negative) === 0); 1589 return this.iuand(num); 1590 }; 1591 1592 // And `num` with `this` 1593 BN.prototype.and = function and (num) { 1594 if (this.length > num.length) return this.clone().iand(num); 1595 return num.clone().iand(this); 1596 }; 1597 1598 BN.prototype.uand = function uand (num) { 1599 if (this.length > num.length) return this.clone().iuand(num); 1600 return num.clone().iuand(this); 1601 }; 1602 1603 // Xor `num` with `this` in-place 1604 BN.prototype.iuxor = function iuxor (num) { 1605 // a.length > b.length 1606 var a; 1607 var b; 1608 if (this.length > num.length) { 1609 a = this; 1610 b = num; 1611 } else { 1612 a = num; 1613 b = this; 1614 } 1615 1616 for (var i = 0; i < b.length; i++) { 1617 this.words[i] = a.words[i] ^ b.words[i]; 1618 } 1619 1620 if (this !== a) { 1621 for (; i < a.length; i++) { 1622 this.words[i] = a.words[i]; 1623 } 1624 } 1625 1626 this.length = a.length; 1627 1628 return this.strip(); 1629 }; 1630 1631 BN.prototype.ixor = function ixor (num) { 1632 assert((this.negative | num.negative) === 0); 1633 return this.iuxor(num); 1634 }; 1635 1636 // Xor `num` with `this` 1637 BN.prototype.xor = function xor (num) { 1638 if (this.length > num.length) return this.clone().ixor(num); 1639 return num.clone().ixor(this); 1640 }; 1641 1642 BN.prototype.uxor = function uxor (num) { 1643 if (this.length > num.length) return this.clone().iuxor(num); 1644 return num.clone().iuxor(this); 1645 }; 1646 1647 // Not ``this`` with ``width`` bitwidth 1648 BN.prototype.inotn = function inotn (width) { 1649 assert(typeof width === 'number' && width >= 0); 1650 1651 var bytesNeeded = Math.ceil(width / 26) | 0; 1652 var bitsLeft = width % 26; 1653 1654 // Extend the buffer with leading zeroes 1655 this._expand(bytesNeeded); 1656 1657 if (bitsLeft > 0) { 1658 bytesNeeded--; 1659 } 1660 1661 // Handle complete words 1662 for (var i = 0; i < bytesNeeded; i++) { 1663 this.words[i] = ~this.words[i] & 0x3ffffff; 1664 } 1665 1666 // Handle the residue 1667 if (bitsLeft > 0) { 1668 this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft)); 1669 } 1670 1671 // And remove leading zeroes 1672 return this.strip(); 1673 }; 1674 1675 BN.prototype.notn = function notn (width) { 1676 return this.clone().inotn(width); 1677 }; 1678 1679 // Set `bit` of `this` 1680 BN.prototype.setn = function setn (bit, val) { 1681 assert(typeof bit === 'number' && bit >= 0); 1682 1683 var off = (bit / 26) | 0; 1684 var wbit = bit % 26; 1685 1686 this._expand(off + 1); 1687 1688 if (val) { 1689 this.words[off] = this.words[off] | (1 << wbit); 1690 } else { 1691 this.words[off] = this.words[off] & ~(1 << wbit); 1692 } 1693 1694 return this.strip(); 1695 }; 1696 1697 // Add `num` to `this` in-place 1698 BN.prototype.iadd = function iadd (num) { 1699 var r; 1700 1701 // negative + positive 1702 if (this.negative !== 0 && num.negative === 0) { 1703 this.negative = 0; 1704 r = this.isub(num); 1705 this.negative ^= 1; 1706 return this._normSign(); 1707 1708 // positive + negative 1709 } else if (this.negative === 0 && num.negative !== 0) { 1710 num.negative = 0; 1711 r = this.isub(num); 1712 num.negative = 1; 1713 return r._normSign(); 1714 } 1715 1716 // a.length > b.length 1717 var a, b; 1718 if (this.length > num.length) { 1719 a = this; 1720 b = num; 1721 } else { 1722 a = num; 1723 b = this; 1724 } 1725 1726 var carry = 0; 1727 for (var i = 0; i < b.length; i++) { 1728 r = (a.words[i] | 0) + (b.words[i] | 0) + carry; 1729 this.words[i] = r & 0x3ffffff; 1730 carry = r >>> 26; 1731 } 1732 for (; carry !== 0 && i < a.length; i++) { 1733 r = (a.words[i] | 0) + carry; 1734 this.words[i] = r & 0x3ffffff; 1735 carry = r >>> 26; 1736 } 1737 1738 this.length = a.length; 1739 if (carry !== 0) { 1740 this.words[this.length] = carry; 1741 this.length++; 1742 // Copy the rest of the words 1743 } else if (a !== this) { 1744 for (; i < a.length; i++) { 1745 this.words[i] = a.words[i]; 1746 } 1747 } 1748 1749 return this; 1750 }; 1751 1752 // Add `num` to `this` 1753 BN.prototype.add = function add (num) { 1754 var res; 1755 if (num.negative !== 0 && this.negative === 0) { 1756 num.negative = 0; 1757 res = this.sub(num); 1758 num.negative ^= 1; 1759 return res; 1760 } else if (num.negative === 0 && this.negative !== 0) { 1761 this.negative = 0; 1762 res = num.sub(this); 1763 this.negative = 1; 1764 return res; 1765 } 1766 1767 if (this.length > num.length) return this.clone().iadd(num); 1768 1769 return num.clone().iadd(this); 1770 }; 1771 1772 // Subtract `num` from `this` in-place 1773 BN.prototype.isub = function isub (num) { 1774 // this - (-num) = this + num 1775 if (num.negative !== 0) { 1776 num.negative = 0; 1777 var r = this.iadd(num); 1778 num.negative = 1; 1779 return r._normSign(); 1780 1781 // -this - num = -(this + num) 1782 } else if (this.negative !== 0) { 1783 this.negative = 0; 1784 this.iadd(num); 1785 this.negative = 1; 1786 return this._normSign(); 1787 } 1788 1789 // At this point both numbers are positive 1790 var cmp = this.cmp(num); 1791 1792 // Optimization - zeroify 1793 if (cmp === 0) { 1794 this.negative = 0; 1795 this.length = 1; 1796 this.words[0] = 0; 1797 return this; 1798 } 1799 1800 // a > b 1801 var a, b; 1802 if (cmp > 0) { 1803 a = this; 1804 b = num; 1805 } else { 1806 a = num; 1807 b = this; 1808 } 1809 1810 var carry = 0; 1811 for (var i = 0; i < b.length; i++) { 1812 r = (a.words[i] | 0) - (b.words[i] | 0) + carry; 1813 carry = r >> 26; 1814 this.words[i] = r & 0x3ffffff; 1815 } 1816 for (; carry !== 0 && i < a.length; i++) { 1817 r = (a.words[i] | 0) + carry; 1818 carry = r >> 26; 1819 this.words[i] = r & 0x3ffffff; 1820 } 1821 1822 // Copy rest of the words 1823 if (carry === 0 && i < a.length && a !== this) { 1824 for (; i < a.length; i++) { 1825 this.words[i] = a.words[i]; 1826 } 1827 } 1828 1829 this.length = Math.max(this.length, i); 1830 1831 if (a !== this) { 1832 this.negative = 1; 1833 } 1834 1835 return this.strip(); 1836 }; 1837 1838 // Subtract `num` from `this` 1839 BN.prototype.sub = function sub (num) { 1840 return this.clone().isub(num); 1841 }; 1842 1843 function smallMulTo (self, num, out) { 1844 out.negative = num.negative ^ self.negative; 1845 var len = (self.length + num.length) | 0; 1846 out.length = len; 1847 len = (len - 1) | 0; 1848 1849 // Peel one iteration (compiler can't do it, because of code complexity) 1850 var a = self.words[0] | 0; 1851 var b = num.words[0] | 0; 1852 var r = a * b; 1853 1854 var lo = r & 0x3ffffff; 1855 var carry = (r / 0x4000000) | 0; 1856 out.words[0] = lo; 1857 1858 for (var k = 1; k < len; k++) { 1859 // Sum all words with the same `i + j = k` and accumulate `ncarry`, 1860 // note that ncarry could be >= 0x3ffffff 1861 var ncarry = carry >>> 26; 1862 var rword = carry & 0x3ffffff; 1863 var maxJ = Math.min(k, num.length - 1); 1864 for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { 1865 var i = (k - j) | 0; 1866 a = self.words[i] | 0; 1867 b = num.words[j] | 0; 1868 r = a * b + rword; 1869 ncarry += (r / 0x4000000) | 0; 1870 rword = r & 0x3ffffff; 1871 } 1872 out.words[k] = rword | 0; 1873 carry = ncarry | 0; 1874 } 1875 if (carry !== 0) { 1876 out.words[k] = carry | 0; 1877 } else { 1878 out.length--; 1879 } 1880 1881 return out.strip(); 1882 } 1883 1884 // TODO(indutny): it may be reasonable to omit it for users who don't need 1885 // to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit 1886 // multiplication (like elliptic secp256k1). 1887 var comb10MulTo = function comb10MulTo (self, num, out) { 1888 var a = self.words; 1889 var b = num.words; 1890 var o = out.words; 1891 var c = 0; 1892 var lo; 1893 var mid; 1894 var hi; 1895 var a0 = a[0] | 0; 1896 var al0 = a0 & 0x1fff; 1897 var ah0 = a0 >>> 13; 1898 var a1 = a[1] | 0; 1899 var al1 = a1 & 0x1fff; 1900 var ah1 = a1 >>> 13; 1901 var a2 = a[2] | 0; 1902 var al2 = a2 & 0x1fff; 1903 var ah2 = a2 >>> 13; 1904 var a3 = a[3] | 0; 1905 var al3 = a3 & 0x1fff; 1906 var ah3 = a3 >>> 13; 1907 var a4 = a[4] | 0; 1908 var al4 = a4 & 0x1fff; 1909 var ah4 = a4 >>> 13; 1910 var a5 = a[5] | 0; 1911 var al5 = a5 & 0x1fff; 1912 var ah5 = a5 >>> 13; 1913 var a6 = a[6] | 0; 1914 var al6 = a6 & 0x1fff; 1915 var ah6 = a6 >>> 13; 1916 var a7 = a[7] | 0; 1917 var al7 = a7 & 0x1fff; 1918 var ah7 = a7 >>> 13; 1919 var a8 = a[8] | 0; 1920 var al8 = a8 & 0x1fff; 1921 var ah8 = a8 >>> 13; 1922 var a9 = a[9] | 0; 1923 var al9 = a9 & 0x1fff; 1924 var ah9 = a9 >>> 13; 1925 var b0 = b[0] | 0; 1926 var bl0 = b0 & 0x1fff; 1927 var bh0 = b0 >>> 13; 1928 var b1 = b[1] | 0; 1929 var bl1 = b1 & 0x1fff; 1930 var bh1 = b1 >>> 13; 1931 var b2 = b[2] | 0; 1932 var bl2 = b2 & 0x1fff; 1933 var bh2 = b2 >>> 13; 1934 var b3 = b[3] | 0; 1935 var bl3 = b3 & 0x1fff; 1936 var bh3 = b3 >>> 13; 1937 var b4 = b[4] | 0; 1938 var bl4 = b4 & 0x1fff; 1939 var bh4 = b4 >>> 13; 1940 var b5 = b[5] | 0; 1941 var bl5 = b5 & 0x1fff; 1942 var bh5 = b5 >>> 13; 1943 var b6 = b[6] | 0; 1944 var bl6 = b6 & 0x1fff; 1945 var bh6 = b6 >>> 13; 1946 var b7 = b[7] | 0; 1947 var bl7 = b7 & 0x1fff; 1948 var bh7 = b7 >>> 13; 1949 var b8 = b[8] | 0; 1950 var bl8 = b8 & 0x1fff; 1951 var bh8 = b8 >>> 13; 1952 var b9 = b[9] | 0; 1953 var bl9 = b9 & 0x1fff; 1954 var bh9 = b9 >>> 13; 1955 1956 out.negative = self.negative ^ num.negative; 1957 out.length = 19; 1958 /* k = 0 */ 1959 lo = Math.imul(al0, bl0); 1960 mid = Math.imul(al0, bh0); 1961 mid = (mid + Math.imul(ah0, bl0)) | 0; 1962 hi = Math.imul(ah0, bh0); 1963 var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 1964 c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0; 1965 w0 &= 0x3ffffff; 1966 /* k = 1 */ 1967 lo = Math.imul(al1, bl0); 1968 mid = Math.imul(al1, bh0); 1969 mid = (mid + Math.imul(ah1, bl0)) | 0; 1970 hi = Math.imul(ah1, bh0); 1971 lo = (lo + Math.imul(al0, bl1)) | 0; 1972 mid = (mid + Math.imul(al0, bh1)) | 0; 1973 mid = (mid + Math.imul(ah0, bl1)) | 0; 1974 hi = (hi + Math.imul(ah0, bh1)) | 0; 1975 var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 1976 c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0; 1977 w1 &= 0x3ffffff; 1978 /* k = 2 */ 1979 lo = Math.imul(al2, bl0); 1980 mid = Math.imul(al2, bh0); 1981 mid = (mid + Math.imul(ah2, bl0)) | 0; 1982 hi = Math.imul(ah2, bh0); 1983 lo = (lo + Math.imul(al1, bl1)) | 0; 1984 mid = (mid + Math.imul(al1, bh1)) | 0; 1985 mid = (mid + Math.imul(ah1, bl1)) | 0; 1986 hi = (hi + Math.imul(ah1, bh1)) | 0; 1987 lo = (lo + Math.imul(al0, bl2)) | 0; 1988 mid = (mid + Math.imul(al0, bh2)) | 0; 1989 mid = (mid + Math.imul(ah0, bl2)) | 0; 1990 hi = (hi + Math.imul(ah0, bh2)) | 0; 1991 var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 1992 c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0; 1993 w2 &= 0x3ffffff; 1994 /* k = 3 */ 1995 lo = Math.imul(al3, bl0); 1996 mid = Math.imul(al3, bh0); 1997 mid = (mid + Math.imul(ah3, bl0)) | 0; 1998 hi = Math.imul(ah3, bh0); 1999 lo = (lo + Math.imul(al2, bl1)) | 0; 2000 mid = (mid + Math.imul(al2, bh1)) | 0; 2001 mid = (mid + Math.imul(ah2, bl1)) | 0; 2002 hi = (hi + Math.imul(ah2, bh1)) | 0; 2003 lo = (lo + Math.imul(al1, bl2)) | 0; 2004 mid = (mid + Math.imul(al1, bh2)) | 0; 2005 mid = (mid + Math.imul(ah1, bl2)) | 0; 2006 hi = (hi + Math.imul(ah1, bh2)) | 0; 2007 lo = (lo + Math.imul(al0, bl3)) | 0; 2008 mid = (mid + Math.imul(al0, bh3)) | 0; 2009 mid = (mid + Math.imul(ah0, bl3)) | 0; 2010 hi = (hi + Math.imul(ah0, bh3)) | 0; 2011 var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2012 c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0; 2013 w3 &= 0x3ffffff; 2014 /* k = 4 */ 2015 lo = Math.imul(al4, bl0); 2016 mid = Math.imul(al4, bh0); 2017 mid = (mid + Math.imul(ah4, bl0)) | 0; 2018 hi = Math.imul(ah4, bh0); 2019 lo = (lo + Math.imul(al3, bl1)) | 0; 2020 mid = (mid + Math.imul(al3, bh1)) | 0; 2021 mid = (mid + Math.imul(ah3, bl1)) | 0; 2022 hi = (hi + Math.imul(ah3, bh1)) | 0; 2023 lo = (lo + Math.imul(al2, bl2)) | 0; 2024 mid = (mid + Math.imul(al2, bh2)) | 0; 2025 mid = (mid + Math.imul(ah2, bl2)) | 0; 2026 hi = (hi + Math.imul(ah2, bh2)) | 0; 2027 lo = (lo + Math.imul(al1, bl3)) | 0; 2028 mid = (mid + Math.imul(al1, bh3)) | 0; 2029 mid = (mid + Math.imul(ah1, bl3)) | 0; 2030 hi = (hi + Math.imul(ah1, bh3)) | 0; 2031 lo = (lo + Math.imul(al0, bl4)) | 0; 2032 mid = (mid + Math.imul(al0, bh4)) | 0; 2033 mid = (mid + Math.imul(ah0, bl4)) | 0; 2034 hi = (hi + Math.imul(ah0, bh4)) | 0; 2035 var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2036 c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0; 2037 w4 &= 0x3ffffff; 2038 /* k = 5 */ 2039 lo = Math.imul(al5, bl0); 2040 mid = Math.imul(al5, bh0); 2041 mid = (mid + Math.imul(ah5, bl0)) | 0; 2042 hi = Math.imul(ah5, bh0); 2043 lo = (lo + Math.imul(al4, bl1)) | 0; 2044 mid = (mid + Math.imul(al4, bh1)) | 0; 2045 mid = (mid + Math.imul(ah4, bl1)) | 0; 2046 hi = (hi + Math.imul(ah4, bh1)) | 0; 2047 lo = (lo + Math.imul(al3, bl2)) | 0; 2048 mid = (mid + Math.imul(al3, bh2)) | 0; 2049 mid = (mid + Math.imul(ah3, bl2)) | 0; 2050 hi = (hi + Math.imul(ah3, bh2)) | 0; 2051 lo = (lo + Math.imul(al2, bl3)) | 0; 2052 mid = (mid + Math.imul(al2, bh3)) | 0; 2053 mid = (mid + Math.imul(ah2, bl3)) | 0; 2054 hi = (hi + Math.imul(ah2, bh3)) | 0; 2055 lo = (lo + Math.imul(al1, bl4)) | 0; 2056 mid = (mid + Math.imul(al1, bh4)) | 0; 2057 mid = (mid + Math.imul(ah1, bl4)) | 0; 2058 hi = (hi + Math.imul(ah1, bh4)) | 0; 2059 lo = (lo + Math.imul(al0, bl5)) | 0; 2060 mid = (mid + Math.imul(al0, bh5)) | 0; 2061 mid = (mid + Math.imul(ah0, bl5)) | 0; 2062 hi = (hi + Math.imul(ah0, bh5)) | 0; 2063 var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2064 c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0; 2065 w5 &= 0x3ffffff; 2066 /* k = 6 */ 2067 lo = Math.imul(al6, bl0); 2068 mid = Math.imul(al6, bh0); 2069 mid = (mid + Math.imul(ah6, bl0)) | 0; 2070 hi = Math.imul(ah6, bh0); 2071 lo = (lo + Math.imul(al5, bl1)) | 0; 2072 mid = (mid + Math.imul(al5, bh1)) | 0; 2073 mid = (mid + Math.imul(ah5, bl1)) | 0; 2074 hi = (hi + Math.imul(ah5, bh1)) | 0; 2075 lo = (lo + Math.imul(al4, bl2)) | 0; 2076 mid = (mid + Math.imul(al4, bh2)) | 0; 2077 mid = (mid + Math.imul(ah4, bl2)) | 0; 2078 hi = (hi + Math.imul(ah4, bh2)) | 0; 2079 lo = (lo + Math.imul(al3, bl3)) | 0; 2080 mid = (mid + Math.imul(al3, bh3)) | 0; 2081 mid = (mid + Math.imul(ah3, bl3)) | 0; 2082 hi = (hi + Math.imul(ah3, bh3)) | 0; 2083 lo = (lo + Math.imul(al2, bl4)) | 0; 2084 mid = (mid + Math.imul(al2, bh4)) | 0; 2085 mid = (mid + Math.imul(ah2, bl4)) | 0; 2086 hi = (hi + Math.imul(ah2, bh4)) | 0; 2087 lo = (lo + Math.imul(al1, bl5)) | 0; 2088 mid = (mid + Math.imul(al1, bh5)) | 0; 2089 mid = (mid + Math.imul(ah1, bl5)) | 0; 2090 hi = (hi + Math.imul(ah1, bh5)) | 0; 2091 lo = (lo + Math.imul(al0, bl6)) | 0; 2092 mid = (mid + Math.imul(al0, bh6)) | 0; 2093 mid = (mid + Math.imul(ah0, bl6)) | 0; 2094 hi = (hi + Math.imul(ah0, bh6)) | 0; 2095 var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2096 c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0; 2097 w6 &= 0x3ffffff; 2098 /* k = 7 */ 2099 lo = Math.imul(al7, bl0); 2100 mid = Math.imul(al7, bh0); 2101 mid = (mid + Math.imul(ah7, bl0)) | 0; 2102 hi = Math.imul(ah7, bh0); 2103 lo = (lo + Math.imul(al6, bl1)) | 0; 2104 mid = (mid + Math.imul(al6, bh1)) | 0; 2105 mid = (mid + Math.imul(ah6, bl1)) | 0; 2106 hi = (hi + Math.imul(ah6, bh1)) | 0; 2107 lo = (lo + Math.imul(al5, bl2)) | 0; 2108 mid = (mid + Math.imul(al5, bh2)) | 0; 2109 mid = (mid + Math.imul(ah5, bl2)) | 0; 2110 hi = (hi + Math.imul(ah5, bh2)) | 0; 2111 lo = (lo + Math.imul(al4, bl3)) | 0; 2112 mid = (mid + Math.imul(al4, bh3)) | 0; 2113 mid = (mid + Math.imul(ah4, bl3)) | 0; 2114 hi = (hi + Math.imul(ah4, bh3)) | 0; 2115 lo = (lo + Math.imul(al3, bl4)) | 0; 2116 mid = (mid + Math.imul(al3, bh4)) | 0; 2117 mid = (mid + Math.imul(ah3, bl4)) | 0; 2118 hi = (hi + Math.imul(ah3, bh4)) | 0; 2119 lo = (lo + Math.imul(al2, bl5)) | 0; 2120 mid = (mid + Math.imul(al2, bh5)) | 0; 2121 mid = (mid + Math.imul(ah2, bl5)) | 0; 2122 hi = (hi + Math.imul(ah2, bh5)) | 0; 2123 lo = (lo + Math.imul(al1, bl6)) | 0; 2124 mid = (mid + Math.imul(al1, bh6)) | 0; 2125 mid = (mid + Math.imul(ah1, bl6)) | 0; 2126 hi = (hi + Math.imul(ah1, bh6)) | 0; 2127 lo = (lo + Math.imul(al0, bl7)) | 0; 2128 mid = (mid + Math.imul(al0, bh7)) | 0; 2129 mid = (mid + Math.imul(ah0, bl7)) | 0; 2130 hi = (hi + Math.imul(ah0, bh7)) | 0; 2131 var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2132 c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0; 2133 w7 &= 0x3ffffff; 2134 /* k = 8 */ 2135 lo = Math.imul(al8, bl0); 2136 mid = Math.imul(al8, bh0); 2137 mid = (mid + Math.imul(ah8, bl0)) | 0; 2138 hi = Math.imul(ah8, bh0); 2139 lo = (lo + Math.imul(al7, bl1)) | 0; 2140 mid = (mid + Math.imul(al7, bh1)) | 0; 2141 mid = (mid + Math.imul(ah7, bl1)) | 0; 2142 hi = (hi + Math.imul(ah7, bh1)) | 0; 2143 lo = (lo + Math.imul(al6, bl2)) | 0; 2144 mid = (mid + Math.imul(al6, bh2)) | 0; 2145 mid = (mid + Math.imul(ah6, bl2)) | 0; 2146 hi = (hi + Math.imul(ah6, bh2)) | 0; 2147 lo = (lo + Math.imul(al5, bl3)) | 0; 2148 mid = (mid + Math.imul(al5, bh3)) | 0; 2149 mid = (mid + Math.imul(ah5, bl3)) | 0; 2150 hi = (hi + Math.imul(ah5, bh3)) | 0; 2151 lo = (lo + Math.imul(al4, bl4)) | 0; 2152 mid = (mid + Math.imul(al4, bh4)) | 0; 2153 mid = (mid + Math.imul(ah4, bl4)) | 0; 2154 hi = (hi + Math.imul(ah4, bh4)) | 0; 2155 lo = (lo + Math.imul(al3, bl5)) | 0; 2156 mid = (mid + Math.imul(al3, bh5)) | 0; 2157 mid = (mid + Math.imul(ah3, bl5)) | 0; 2158 hi = (hi + Math.imul(ah3, bh5)) | 0; 2159 lo = (lo + Math.imul(al2, bl6)) | 0; 2160 mid = (mid + Math.imul(al2, bh6)) | 0; 2161 mid = (mid + Math.imul(ah2, bl6)) | 0; 2162 hi = (hi + Math.imul(ah2, bh6)) | 0; 2163 lo = (lo + Math.imul(al1, bl7)) | 0; 2164 mid = (mid + Math.imul(al1, bh7)) | 0; 2165 mid = (mid + Math.imul(ah1, bl7)) | 0; 2166 hi = (hi + Math.imul(ah1, bh7)) | 0; 2167 lo = (lo + Math.imul(al0, bl8)) | 0; 2168 mid = (mid + Math.imul(al0, bh8)) | 0; 2169 mid = (mid + Math.imul(ah0, bl8)) | 0; 2170 hi = (hi + Math.imul(ah0, bh8)) | 0; 2171 var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2172 c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0; 2173 w8 &= 0x3ffffff; 2174 /* k = 9 */ 2175 lo = Math.imul(al9, bl0); 2176 mid = Math.imul(al9, bh0); 2177 mid = (mid + Math.imul(ah9, bl0)) | 0; 2178 hi = Math.imul(ah9, bh0); 2179 lo = (lo + Math.imul(al8, bl1)) | 0; 2180 mid = (mid + Math.imul(al8, bh1)) | 0; 2181 mid = (mid + Math.imul(ah8, bl1)) | 0; 2182 hi = (hi + Math.imul(ah8, bh1)) | 0; 2183 lo = (lo + Math.imul(al7, bl2)) | 0; 2184 mid = (mid + Math.imul(al7, bh2)) | 0; 2185 mid = (mid + Math.imul(ah7, bl2)) | 0; 2186 hi = (hi + Math.imul(ah7, bh2)) | 0; 2187 lo = (lo + Math.imul(al6, bl3)) | 0; 2188 mid = (mid + Math.imul(al6, bh3)) | 0; 2189 mid = (mid + Math.imul(ah6, bl3)) | 0; 2190 hi = (hi + Math.imul(ah6, bh3)) | 0; 2191 lo = (lo + Math.imul(al5, bl4)) | 0; 2192 mid = (mid + Math.imul(al5, bh4)) | 0; 2193 mid = (mid + Math.imul(ah5, bl4)) | 0; 2194 hi = (hi + Math.imul(ah5, bh4)) | 0; 2195 lo = (lo + Math.imul(al4, bl5)) | 0; 2196 mid = (mid + Math.imul(al4, bh5)) | 0; 2197 mid = (mid + Math.imul(ah4, bl5)) | 0; 2198 hi = (hi + Math.imul(ah4, bh5)) | 0; 2199 lo = (lo + Math.imul(al3, bl6)) | 0; 2200 mid = (mid + Math.imul(al3, bh6)) | 0; 2201 mid = (mid + Math.imul(ah3, bl6)) | 0; 2202 hi = (hi + Math.imul(ah3, bh6)) | 0; 2203 lo = (lo + Math.imul(al2, bl7)) | 0; 2204 mid = (mid + Math.imul(al2, bh7)) | 0; 2205 mid = (mid + Math.imul(ah2, bl7)) | 0; 2206 hi = (hi + Math.imul(ah2, bh7)) | 0; 2207 lo = (lo + Math.imul(al1, bl8)) | 0; 2208 mid = (mid + Math.imul(al1, bh8)) | 0; 2209 mid = (mid + Math.imul(ah1, bl8)) | 0; 2210 hi = (hi + Math.imul(ah1, bh8)) | 0; 2211 lo = (lo + Math.imul(al0, bl9)) | 0; 2212 mid = (mid + Math.imul(al0, bh9)) | 0; 2213 mid = (mid + Math.imul(ah0, bl9)) | 0; 2214 hi = (hi + Math.imul(ah0, bh9)) | 0; 2215 var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2216 c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0; 2217 w9 &= 0x3ffffff; 2218 /* k = 10 */ 2219 lo = Math.imul(al9, bl1); 2220 mid = Math.imul(al9, bh1); 2221 mid = (mid + Math.imul(ah9, bl1)) | 0; 2222 hi = Math.imul(ah9, bh1); 2223 lo = (lo + Math.imul(al8, bl2)) | 0; 2224 mid = (mid + Math.imul(al8, bh2)) | 0; 2225 mid = (mid + Math.imul(ah8, bl2)) | 0; 2226 hi = (hi + Math.imul(ah8, bh2)) | 0; 2227 lo = (lo + Math.imul(al7, bl3)) | 0; 2228 mid = (mid + Math.imul(al7, bh3)) | 0; 2229 mid = (mid + Math.imul(ah7, bl3)) | 0; 2230 hi = (hi + Math.imul(ah7, bh3)) | 0; 2231 lo = (lo + Math.imul(al6, bl4)) | 0; 2232 mid = (mid + Math.imul(al6, bh4)) | 0; 2233 mid = (mid + Math.imul(ah6, bl4)) | 0; 2234 hi = (hi + Math.imul(ah6, bh4)) | 0; 2235 lo = (lo + Math.imul(al5, bl5)) | 0; 2236 mid = (mid + Math.imul(al5, bh5)) | 0; 2237 mid = (mid + Math.imul(ah5, bl5)) | 0; 2238 hi = (hi + Math.imul(ah5, bh5)) | 0; 2239 lo = (lo + Math.imul(al4, bl6)) | 0; 2240 mid = (mid + Math.imul(al4, bh6)) | 0; 2241 mid = (mid + Math.imul(ah4, bl6)) | 0; 2242 hi = (hi + Math.imul(ah4, bh6)) | 0; 2243 lo = (lo + Math.imul(al3, bl7)) | 0; 2244 mid = (mid + Math.imul(al3, bh7)) | 0; 2245 mid = (mid + Math.imul(ah3, bl7)) | 0; 2246 hi = (hi + Math.imul(ah3, bh7)) | 0; 2247 lo = (lo + Math.imul(al2, bl8)) | 0; 2248 mid = (mid + Math.imul(al2, bh8)) | 0; 2249 mid = (mid + Math.imul(ah2, bl8)) | 0; 2250 hi = (hi + Math.imul(ah2, bh8)) | 0; 2251 lo = (lo + Math.imul(al1, bl9)) | 0; 2252 mid = (mid + Math.imul(al1, bh9)) | 0; 2253 mid = (mid + Math.imul(ah1, bl9)) | 0; 2254 hi = (hi + Math.imul(ah1, bh9)) | 0; 2255 var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2256 c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0; 2257 w10 &= 0x3ffffff; 2258 /* k = 11 */ 2259 lo = Math.imul(al9, bl2); 2260 mid = Math.imul(al9, bh2); 2261 mid = (mid + Math.imul(ah9, bl2)) | 0; 2262 hi = Math.imul(ah9, bh2); 2263 lo = (lo + Math.imul(al8, bl3)) | 0; 2264 mid = (mid + Math.imul(al8, bh3)) | 0; 2265 mid = (mid + Math.imul(ah8, bl3)) | 0; 2266 hi = (hi + Math.imul(ah8, bh3)) | 0; 2267 lo = (lo + Math.imul(al7, bl4)) | 0; 2268 mid = (mid + Math.imul(al7, bh4)) | 0; 2269 mid = (mid + Math.imul(ah7, bl4)) | 0; 2270 hi = (hi + Math.imul(ah7, bh4)) | 0; 2271 lo = (lo + Math.imul(al6, bl5)) | 0; 2272 mid = (mid + Math.imul(al6, bh5)) | 0; 2273 mid = (mid + Math.imul(ah6, bl5)) | 0; 2274 hi = (hi + Math.imul(ah6, bh5)) | 0; 2275 lo = (lo + Math.imul(al5, bl6)) | 0; 2276 mid = (mid + Math.imul(al5, bh6)) | 0; 2277 mid = (mid + Math.imul(ah5, bl6)) | 0; 2278 hi = (hi + Math.imul(ah5, bh6)) | 0; 2279 lo = (lo + Math.imul(al4, bl7)) | 0; 2280 mid = (mid + Math.imul(al4, bh7)) | 0; 2281 mid = (mid + Math.imul(ah4, bl7)) | 0; 2282 hi = (hi + Math.imul(ah4, bh7)) | 0; 2283 lo = (lo + Math.imul(al3, bl8)) | 0; 2284 mid = (mid + Math.imul(al3, bh8)) | 0; 2285 mid = (mid + Math.imul(ah3, bl8)) | 0; 2286 hi = (hi + Math.imul(ah3, bh8)) | 0; 2287 lo = (lo + Math.imul(al2, bl9)) | 0; 2288 mid = (mid + Math.imul(al2, bh9)) | 0; 2289 mid = (mid + Math.imul(ah2, bl9)) | 0; 2290 hi = (hi + Math.imul(ah2, bh9)) | 0; 2291 var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2292 c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0; 2293 w11 &= 0x3ffffff; 2294 /* k = 12 */ 2295 lo = Math.imul(al9, bl3); 2296 mid = Math.imul(al9, bh3); 2297 mid = (mid + Math.imul(ah9, bl3)) | 0; 2298 hi = Math.imul(ah9, bh3); 2299 lo = (lo + Math.imul(al8, bl4)) | 0; 2300 mid = (mid + Math.imul(al8, bh4)) | 0; 2301 mid = (mid + Math.imul(ah8, bl4)) | 0; 2302 hi = (hi + Math.imul(ah8, bh4)) | 0; 2303 lo = (lo + Math.imul(al7, bl5)) | 0; 2304 mid = (mid + Math.imul(al7, bh5)) | 0; 2305 mid = (mid + Math.imul(ah7, bl5)) | 0; 2306 hi = (hi + Math.imul(ah7, bh5)) | 0; 2307 lo = (lo + Math.imul(al6, bl6)) | 0; 2308 mid = (mid + Math.imul(al6, bh6)) | 0; 2309 mid = (mid + Math.imul(ah6, bl6)) | 0; 2310 hi = (hi + Math.imul(ah6, bh6)) | 0; 2311 lo = (lo + Math.imul(al5, bl7)) | 0; 2312 mid = (mid + Math.imul(al5, bh7)) | 0; 2313 mid = (mid + Math.imul(ah5, bl7)) | 0; 2314 hi = (hi + Math.imul(ah5, bh7)) | 0; 2315 lo = (lo + Math.imul(al4, bl8)) | 0; 2316 mid = (mid + Math.imul(al4, bh8)) | 0; 2317 mid = (mid + Math.imul(ah4, bl8)) | 0; 2318 hi = (hi + Math.imul(ah4, bh8)) | 0; 2319 lo = (lo + Math.imul(al3, bl9)) | 0; 2320 mid = (mid + Math.imul(al3, bh9)) | 0; 2321 mid = (mid + Math.imul(ah3, bl9)) | 0; 2322 hi = (hi + Math.imul(ah3, bh9)) | 0; 2323 var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2324 c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0; 2325 w12 &= 0x3ffffff; 2326 /* k = 13 */ 2327 lo = Math.imul(al9, bl4); 2328 mid = Math.imul(al9, bh4); 2329 mid = (mid + Math.imul(ah9, bl4)) | 0; 2330 hi = Math.imul(ah9, bh4); 2331 lo = (lo + Math.imul(al8, bl5)) | 0; 2332 mid = (mid + Math.imul(al8, bh5)) | 0; 2333 mid = (mid + Math.imul(ah8, bl5)) | 0; 2334 hi = (hi + Math.imul(ah8, bh5)) | 0; 2335 lo = (lo + Math.imul(al7, bl6)) | 0; 2336 mid = (mid + Math.imul(al7, bh6)) | 0; 2337 mid = (mid + Math.imul(ah7, bl6)) | 0; 2338 hi = (hi + Math.imul(ah7, bh6)) | 0; 2339 lo = (lo + Math.imul(al6, bl7)) | 0; 2340 mid = (mid + Math.imul(al6, bh7)) | 0; 2341 mid = (mid + Math.imul(ah6, bl7)) | 0; 2342 hi = (hi + Math.imul(ah6, bh7)) | 0; 2343 lo = (lo + Math.imul(al5, bl8)) | 0; 2344 mid = (mid + Math.imul(al5, bh8)) | 0; 2345 mid = (mid + Math.imul(ah5, bl8)) | 0; 2346 hi = (hi + Math.imul(ah5, bh8)) | 0; 2347 lo = (lo + Math.imul(al4, bl9)) | 0; 2348 mid = (mid + Math.imul(al4, bh9)) | 0; 2349 mid = (mid + Math.imul(ah4, bl9)) | 0; 2350 hi = (hi + Math.imul(ah4, bh9)) | 0; 2351 var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2352 c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0; 2353 w13 &= 0x3ffffff; 2354 /* k = 14 */ 2355 lo = Math.imul(al9, bl5); 2356 mid = Math.imul(al9, bh5); 2357 mid = (mid + Math.imul(ah9, bl5)) | 0; 2358 hi = Math.imul(ah9, bh5); 2359 lo = (lo + Math.imul(al8, bl6)) | 0; 2360 mid = (mid + Math.imul(al8, bh6)) | 0; 2361 mid = (mid + Math.imul(ah8, bl6)) | 0; 2362 hi = (hi + Math.imul(ah8, bh6)) | 0; 2363 lo = (lo + Math.imul(al7, bl7)) | 0; 2364 mid = (mid + Math.imul(al7, bh7)) | 0; 2365 mid = (mid + Math.imul(ah7, bl7)) | 0; 2366 hi = (hi + Math.imul(ah7, bh7)) | 0; 2367 lo = (lo + Math.imul(al6, bl8)) | 0; 2368 mid = (mid + Math.imul(al6, bh8)) | 0; 2369 mid = (mid + Math.imul(ah6, bl8)) | 0; 2370 hi = (hi + Math.imul(ah6, bh8)) | 0; 2371 lo = (lo + Math.imul(al5, bl9)) | 0; 2372 mid = (mid + Math.imul(al5, bh9)) | 0; 2373 mid = (mid + Math.imul(ah5, bl9)) | 0; 2374 hi = (hi + Math.imul(ah5, bh9)) | 0; 2375 var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2376 c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0; 2377 w14 &= 0x3ffffff; 2378 /* k = 15 */ 2379 lo = Math.imul(al9, bl6); 2380 mid = Math.imul(al9, bh6); 2381 mid = (mid + Math.imul(ah9, bl6)) | 0; 2382 hi = Math.imul(ah9, bh6); 2383 lo = (lo + Math.imul(al8, bl7)) | 0; 2384 mid = (mid + Math.imul(al8, bh7)) | 0; 2385 mid = (mid + Math.imul(ah8, bl7)) | 0; 2386 hi = (hi + Math.imul(ah8, bh7)) | 0; 2387 lo = (lo + Math.imul(al7, bl8)) | 0; 2388 mid = (mid + Math.imul(al7, bh8)) | 0; 2389 mid = (mid + Math.imul(ah7, bl8)) | 0; 2390 hi = (hi + Math.imul(ah7, bh8)) | 0; 2391 lo = (lo + Math.imul(al6, bl9)) | 0; 2392 mid = (mid + Math.imul(al6, bh9)) | 0; 2393 mid = (mid + Math.imul(ah6, bl9)) | 0; 2394 hi = (hi + Math.imul(ah6, bh9)) | 0; 2395 var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2396 c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0; 2397 w15 &= 0x3ffffff; 2398 /* k = 16 */ 2399 lo = Math.imul(al9, bl7); 2400 mid = Math.imul(al9, bh7); 2401 mid = (mid + Math.imul(ah9, bl7)) | 0; 2402 hi = Math.imul(ah9, bh7); 2403 lo = (lo + Math.imul(al8, bl8)) | 0; 2404 mid = (mid + Math.imul(al8, bh8)) | 0; 2405 mid = (mid + Math.imul(ah8, bl8)) | 0; 2406 hi = (hi + Math.imul(ah8, bh8)) | 0; 2407 lo = (lo + Math.imul(al7, bl9)) | 0; 2408 mid = (mid + Math.imul(al7, bh9)) | 0; 2409 mid = (mid + Math.imul(ah7, bl9)) | 0; 2410 hi = (hi + Math.imul(ah7, bh9)) | 0; 2411 var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2412 c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0; 2413 w16 &= 0x3ffffff; 2414 /* k = 17 */ 2415 lo = Math.imul(al9, bl8); 2416 mid = Math.imul(al9, bh8); 2417 mid = (mid + Math.imul(ah9, bl8)) | 0; 2418 hi = Math.imul(ah9, bh8); 2419 lo = (lo + Math.imul(al8, bl9)) | 0; 2420 mid = (mid + Math.imul(al8, bh9)) | 0; 2421 mid = (mid + Math.imul(ah8, bl9)) | 0; 2422 hi = (hi + Math.imul(ah8, bh9)) | 0; 2423 var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2424 c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0; 2425 w17 &= 0x3ffffff; 2426 /* k = 18 */ 2427 lo = Math.imul(al9, bl9); 2428 mid = Math.imul(al9, bh9); 2429 mid = (mid + Math.imul(ah9, bl9)) | 0; 2430 hi = Math.imul(ah9, bh9); 2431 var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; 2432 c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0; 2433 w18 &= 0x3ffffff; 2434 o[0] = w0; 2435 o[1] = w1; 2436 o[2] = w2; 2437 o[3] = w3; 2438 o[4] = w4; 2439 o[5] = w5; 2440 o[6] = w6; 2441 o[7] = w7; 2442 o[8] = w8; 2443 o[9] = w9; 2444 o[10] = w10; 2445 o[11] = w11; 2446 o[12] = w12; 2447 o[13] = w13; 2448 o[14] = w14; 2449 o[15] = w15; 2450 o[16] = w16; 2451 o[17] = w17; 2452 o[18] = w18; 2453 if (c !== 0) { 2454 o[19] = c; 2455 out.length++; 2456 } 2457 return out; 2458 }; 2459 2460 // Polyfill comb 2461 if (!Math.imul) { 2462 comb10MulTo = smallMulTo; 2463 } 2464 2465 function bigMulTo (self, num, out) { 2466 out.negative = num.negative ^ self.negative; 2467 out.length = self.length + num.length; 2468 2469 var carry = 0; 2470 var hncarry = 0; 2471 for (var k = 0; k < out.length - 1; k++) { 2472 // Sum all words with the same `i + j = k` and accumulate `ncarry`, 2473 // note that ncarry could be >= 0x3ffffff 2474 var ncarry = hncarry; 2475 hncarry = 0; 2476 var rword = carry & 0x3ffffff; 2477 var maxJ = Math.min(k, num.length - 1); 2478 for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { 2479 var i = k - j; 2480 var a = self.words[i] | 0; 2481 var b = num.words[j] | 0; 2482 var r = a * b; 2483 2484 var lo = r & 0x3ffffff; 2485 ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0; 2486 lo = (lo + rword) | 0; 2487 rword = lo & 0x3ffffff; 2488 ncarry = (ncarry + (lo >>> 26)) | 0; 2489 2490 hncarry += ncarry >>> 26; 2491 ncarry &= 0x3ffffff; 2492 } 2493 out.words[k] = rword; 2494 carry = ncarry; 2495 ncarry = hncarry; 2496 } 2497 if (carry !== 0) { 2498 out.words[k] = carry; 2499 } else { 2500 out.length--; 2501 } 2502 2503 return out.strip(); 2504 } 2505 2506 function jumboMulTo (self, num, out) { 2507 var fftm = new FFTM(); 2508 return fftm.mulp(self, num, out); 2509 } 2510 2511 BN.prototype.mulTo = function mulTo (num, out) { 2512 var res; 2513 var len = this.length + num.length; 2514 if (this.length === 10 && num.length === 10) { 2515 res = comb10MulTo(this, num, out); 2516 } else if (len < 63) { 2517 res = smallMulTo(this, num, out); 2518 } else if (len < 1024) { 2519 res = bigMulTo(this, num, out); 2520 } else { 2521 res = jumboMulTo(this, num, out); 2522 } 2523 2524 return res; 2525 }; 2526 2527 // Cooley-Tukey algorithm for FFT 2528 // slightly revisited to rely on looping instead of recursion 2529 2530 function FFTM (x, y) { 2531 this.x = x; 2532 this.y = y; 2533 } 2534 2535 FFTM.prototype.makeRBT = function makeRBT (N) { 2536 var t = new Array(N); 2537 var l = BN.prototype._countBits(N) - 1; 2538 for (var i = 0; i < N; i++) { 2539 t[i] = this.revBin(i, l, N); 2540 } 2541 2542 return t; 2543 }; 2544 2545 // Returns binary-reversed representation of `x` 2546 FFTM.prototype.revBin = function revBin (x, l, N) { 2547 if (x === 0 || x === N - 1) return x; 2548 2549 var rb = 0; 2550 for (var i = 0; i < l; i++) { 2551 rb |= (x & 1) << (l - i - 1); 2552 x >>= 1; 2553 } 2554 2555 return rb; 2556 }; 2557 2558 // Performs "tweedling" phase, therefore 'emulating' 2559 // behaviour of the recursive algorithm 2560 FFTM.prototype.permute = function permute (rbt, rws, iws, rtws, itws, N) { 2561 for (var i = 0; i < N; i++) { 2562 rtws[i] = rws[rbt[i]]; 2563 itws[i] = iws[rbt[i]]; 2564 } 2565 }; 2566 2567 FFTM.prototype.transform = function transform (rws, iws, rtws, itws, N, rbt) { 2568 this.permute(rbt, rws, iws, rtws, itws, N); 2569 2570 for (var s = 1; s < N; s <<= 1) { 2571 var l = s << 1; 2572 2573 var rtwdf = Math.cos(2 * Math.PI / l); 2574 var itwdf = Math.sin(2 * Math.PI / l); 2575 2576 for (var p = 0; p < N; p += l) { 2577 var rtwdf_ = rtwdf; 2578 var itwdf_ = itwdf; 2579 2580 for (var j = 0; j < s; j++) { 2581 var re = rtws[p + j]; 2582 var ie = itws[p + j]; 2583 2584 var ro = rtws[p + j + s]; 2585 var io = itws[p + j + s]; 2586 2587 var rx = rtwdf_ * ro - itwdf_ * io; 2588 2589 io = rtwdf_ * io + itwdf_ * ro; 2590 ro = rx; 2591 2592 rtws[p + j] = re + ro; 2593 itws[p + j] = ie + io; 2594 2595 rtws[p + j + s] = re - ro; 2596 itws[p + j + s] = ie - io; 2597 2598 /* jshint maxdepth : false */ 2599 if (j !== l) { 2600 rx = rtwdf * rtwdf_ - itwdf * itwdf_; 2601 2602 itwdf_ = rtwdf * itwdf_ + itwdf * rtwdf_; 2603 rtwdf_ = rx; 2604 } 2605 } 2606 } 2607 } 2608 }; 2609 2610 FFTM.prototype.guessLen13b = function guessLen13b (n, m) { 2611 var N = Math.max(m, n) | 1; 2612 var odd = N & 1; 2613 var i = 0; 2614 for (N = N / 2 | 0; N; N = N >>> 1) { 2615 i++; 2616 } 2617 2618 return 1 << i + 1 + odd; 2619 }; 2620 2621 FFTM.prototype.conjugate = function conjugate (rws, iws, N) { 2622 if (N <= 1) return; 2623 2624 for (var i = 0; i < N / 2; i++) { 2625 var t = rws[i]; 2626 2627 rws[i] = rws[N - i - 1]; 2628 rws[N - i - 1] = t; 2629 2630 t = iws[i]; 2631 2632 iws[i] = -iws[N - i - 1]; 2633 iws[N - i - 1] = -t; 2634 } 2635 }; 2636 2637 FFTM.prototype.normalize13b = function normalize13b (ws, N) { 2638 var carry = 0; 2639 for (var i = 0; i < N / 2; i++) { 2640 var w = Math.round(ws[2 * i + 1] / N) * 0x2000 + 2641 Math.round(ws[2 * i] / N) + 2642 carry; 2643 2644 ws[i] = w & 0x3ffffff; 2645 2646 if (w < 0x4000000) { 2647 carry = 0; 2648 } else { 2649 carry = w / 0x4000000 | 0; 2650 } 2651 } 2652 2653 return ws; 2654 }; 2655 2656 FFTM.prototype.convert13b = function convert13b (ws, len, rws, N) { 2657 var carry = 0; 2658 for (var i = 0; i < len; i++) { 2659 carry = carry + (ws[i] | 0); 2660 2661 rws[2 * i] = carry & 0x1fff; carry = carry >>> 13; 2662 rws[2 * i + 1] = carry & 0x1fff; carry = carry >>> 13; 2663 } 2664 2665 // Pad with zeroes 2666 for (i = 2 * len; i < N; ++i) { 2667 rws[i] = 0; 2668 } 2669 2670 assert(carry === 0); 2671 assert((carry & ~0x1fff) === 0); 2672 }; 2673 2674 FFTM.prototype.stub = function stub (N) { 2675 var ph = new Array(N); 2676 for (var i = 0; i < N; i++) { 2677 ph[i] = 0; 2678 } 2679 2680 return ph; 2681 }; 2682 2683 FFTM.prototype.mulp = function mulp (x, y, out) { 2684 var N = 2 * this.guessLen13b(x.length, y.length); 2685 2686 var rbt = this.makeRBT(N); 2687 2688 var _ = this.stub(N); 2689 2690 var rws = new Array(N); 2691 var rwst = new Array(N); 2692 var iwst = new Array(N); 2693 2694 var nrws = new Array(N); 2695 var nrwst = new Array(N); 2696 var niwst = new Array(N); 2697 2698 var rmws = out.words; 2699 rmws.length = N; 2700 2701 this.convert13b(x.words, x.length, rws, N); 2702 this.convert13b(y.words, y.length, nrws, N); 2703 2704 this.transform(rws, _, rwst, iwst, N, rbt); 2705 this.transform(nrws, _, nrwst, niwst, N, rbt); 2706 2707 for (var i = 0; i < N; i++) { 2708 var rx = rwst[i] * nrwst[i] - iwst[i] * niwst[i]; 2709 iwst[i] = rwst[i] * niwst[i] + iwst[i] * nrwst[i]; 2710 rwst[i] = rx; 2711 } 2712 2713 this.conjugate(rwst, iwst, N); 2714 this.transform(rwst, iwst, rmws, _, N, rbt); 2715 this.conjugate(rmws, _, N); 2716 this.normalize13b(rmws, N); 2717 2718 out.negative = x.negative ^ y.negative; 2719 out.length = x.length + y.length; 2720 return out.strip(); 2721 }; 2722 2723 // Multiply `this` by `num` 2724 BN.prototype.mul = function mul (num) { 2725 var out = new BN(null); 2726 out.words = new Array(this.length + num.length); 2727 return this.mulTo(num, out); 2728 }; 2729 2730 // Multiply employing FFT 2731 BN.prototype.mulf = function mulf (num) { 2732 var out = new BN(null); 2733 out.words = new Array(this.length + num.length); 2734 return jumboMulTo(this, num, out); 2735 }; 2736 2737 // In-place Multiplication 2738 BN.prototype.imul = function imul (num) { 2739 return this.clone().mulTo(num, this); 2740 }; 2741 2742 BN.prototype.imuln = function imuln (num) { 2743 assert(typeof num === 'number'); 2744 assert(num < 0x4000000); 2745 2746 // Carry 2747 var carry = 0; 2748 for (var i = 0; i < this.length; i++) { 2749 var w = (this.words[i] | 0) * num; 2750 var lo = (w & 0x3ffffff) + (carry & 0x3ffffff); 2751 carry >>= 26; 2752 carry += (w / 0x4000000) | 0; 2753 // NOTE: lo is 27bit maximum 2754 carry += lo >>> 26; 2755 this.words[i] = lo & 0x3ffffff; 2756 } 2757 2758 if (carry !== 0) { 2759 this.words[i] = carry; 2760 this.length++; 2761 } 2762 2763 return this; 2764 }; 2765 2766 BN.prototype.muln = function muln (num) { 2767 return this.clone().imuln(num); 2768 }; 2769 2770 // `this` * `this` 2771 BN.prototype.sqr = function sqr () { 2772 return this.mul(this); 2773 }; 2774 2775 // `this` * `this` in-place 2776 BN.prototype.isqr = function isqr () { 2777 return this.imul(this.clone()); 2778 }; 2779 2780 // Math.pow(`this`, `num`) 2781 BN.prototype.pow = function pow (num) { 2782 var w = toBitArray(num); 2783 if (w.length === 0) return new BN(1); 2784 2785 // Skip leading zeroes 2786 var res = this; 2787 for (var i = 0; i < w.length; i++, res = res.sqr()) { 2788 if (w[i] !== 0) break; 2789 } 2790 2791 if (++i < w.length) { 2792 for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) { 2793 if (w[i] === 0) continue; 2794 2795 res = res.mul(q); 2796 } 2797 } 2798 2799 return res; 2800 }; 2801 2802 // Shift-left in-place 2803 BN.prototype.iushln = function iushln (bits) { 2804 assert(typeof bits === 'number' && bits >= 0); 2805 var r = bits % 26; 2806 var s = (bits - r) / 26; 2807 var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r); 2808 var i; 2809 2810 if (r !== 0) { 2811 var carry = 0; 2812 2813 for (i = 0; i < this.length; i++) { 2814 var newCarry = this.words[i] & carryMask; 2815 var c = ((this.words[i] | 0) - newCarry) << r; 2816 this.words[i] = c | carry; 2817 carry = newCarry >>> (26 - r); 2818 } 2819 2820 if (carry) { 2821 this.words[i] = carry; 2822 this.length++; 2823 } 2824 } 2825 2826 if (s !== 0) { 2827 for (i = this.length - 1; i >= 0; i--) { 2828 this.words[i + s] = this.words[i]; 2829 } 2830 2831 for (i = 0; i < s; i++) { 2832 this.words[i] = 0; 2833 } 2834 2835 this.length += s; 2836 } 2837 2838 return this.strip(); 2839 }; 2840 2841 BN.prototype.ishln = function ishln (bits) { 2842 // TODO(indutny): implement me 2843 assert(this.negative === 0); 2844 return this.iushln(bits); 2845 }; 2846 2847 // Shift-right in-place 2848 // NOTE: `hint` is a lowest bit before trailing zeroes 2849 // NOTE: if `extended` is present - it will be filled with destroyed bits 2850 BN.prototype.iushrn = function iushrn (bits, hint, extended) { 2851 assert(typeof bits === 'number' && bits >= 0); 2852 var h; 2853 if (hint) { 2854 h = (hint - (hint % 26)) / 26; 2855 } else { 2856 h = 0; 2857 } 2858 2859 var r = bits % 26; 2860 var s = Math.min((bits - r) / 26, this.length); 2861 var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); 2862 var maskedWords = extended; 2863 2864 h -= s; 2865 h = Math.max(0, h); 2866 2867 // Extended mode, copy masked part 2868 if (maskedWords) { 2869 for (var i = 0; i < s; i++) { 2870 maskedWords.words[i] = this.words[i]; 2871 } 2872 maskedWords.length = s; 2873 } 2874 2875 if (s === 0) { 2876 // No-op, we should not move anything at all 2877 } else if (this.length > s) { 2878 this.length -= s; 2879 for (i = 0; i < this.length; i++) { 2880 this.words[i] = this.words[i + s]; 2881 } 2882 } else { 2883 this.words[0] = 0; 2884 this.length = 1; 2885 } 2886 2887 var carry = 0; 2888 for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) { 2889 var word = this.words[i] | 0; 2890 this.words[i] = (carry << (26 - r)) | (word >>> r); 2891 carry = word & mask; 2892 } 2893 2894 // Push carried bits as a mask 2895 if (maskedWords && carry !== 0) { 2896 maskedWords.words[maskedWords.length++] = carry; 2897 } 2898 2899 if (this.length === 0) { 2900 this.words[0] = 0; 2901 this.length = 1; 2902 } 2903 2904 return this.strip(); 2905 }; 2906 2907 BN.prototype.ishrn = function ishrn (bits, hint, extended) { 2908 // TODO(indutny): implement me 2909 assert(this.negative === 0); 2910 return this.iushrn(bits, hint, extended); 2911 }; 2912 2913 // Shift-left 2914 BN.prototype.shln = function shln (bits) { 2915 return this.clone().ishln(bits); 2916 }; 2917 2918 BN.prototype.ushln = function ushln (bits) { 2919 return this.clone().iushln(bits); 2920 }; 2921 2922 // Shift-right 2923 BN.prototype.shrn = function shrn (bits) { 2924 return this.clone().ishrn(bits); 2925 }; 2926 2927 BN.prototype.ushrn = function ushrn (bits) { 2928 return this.clone().iushrn(bits); 2929 }; 2930 2931 // Test if n bit is set 2932 BN.prototype.testn = function testn (bit) { 2933 assert(typeof bit === 'number' && bit >= 0); 2934 var r = bit % 26; 2935 var s = (bit - r) / 26; 2936 var q = 1 << r; 2937 2938 // Fast case: bit is much higher than all existing words 2939 if (this.length <= s) return false; 2940 2941 // Check bit and return 2942 var w = this.words[s]; 2943 2944 return !!(w & q); 2945 }; 2946 2947 // Return only lowers bits of number (in-place) 2948 BN.prototype.imaskn = function imaskn (bits) { 2949 assert(typeof bits === 'number' && bits >= 0); 2950 var r = bits % 26; 2951 var s = (bits - r) / 26; 2952 2953 assert(this.negative === 0, 'imaskn works only with positive numbers'); 2954 2955 if (this.length <= s) { 2956 return this; 2957 } 2958 2959 if (r !== 0) { 2960 s++; 2961 } 2962 this.length = Math.min(s, this.length); 2963 2964 if (r !== 0) { 2965 var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); 2966 this.words[this.length - 1] &= mask; 2967 } 2968 2969 return this.strip(); 2970 }; 2971 2972 // Return only lowers bits of number 2973 BN.prototype.maskn = function maskn (bits) { 2974 return this.clone().imaskn(bits); 2975 }; 2976 2977 // Add plain number `num` to `this` 2978 BN.prototype.iaddn = function iaddn (num) { 2979 assert(typeof num === 'number'); 2980 assert(num < 0x4000000); 2981 if (num < 0) return this.isubn(-num); 2982 2983 // Possible sign change 2984 if (this.negative !== 0) { 2985 if (this.length === 1 && (this.words[0] | 0) < num) { 2986 this.words[0] = num - (this.words[0] | 0); 2987 this.negative = 0; 2988 return this; 2989 } 2990 2991 this.negative = 0; 2992 this.isubn(num); 2993 this.negative = 1; 2994 return this; 2995 } 2996 2997 // Add without checks 2998 return this._iaddn(num); 2999 }; 3000 3001 BN.prototype._iaddn = function _iaddn (num) { 3002 this.words[0] += num; 3003 3004 // Carry 3005 for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) { 3006 this.words[i] -= 0x4000000; 3007 if (i === this.length - 1) { 3008 this.words[i + 1] = 1; 3009 } else { 3010 this.words[i + 1]++; 3011 } 3012 } 3013 this.length = Math.max(this.length, i + 1); 3014 3015 return this; 3016 }; 3017 3018 // Subtract plain number `num` from `this` 3019 BN.prototype.isubn = function isubn (num) { 3020 assert(typeof num === 'number'); 3021 assert(num < 0x4000000); 3022 if (num < 0) return this.iaddn(-num); 3023 3024 if (this.negative !== 0) { 3025 this.negative = 0; 3026 this.iaddn(num); 3027 this.negative = 1; 3028 return this; 3029 } 3030 3031 this.words[0] -= num; 3032 3033 if (this.length === 1 && this.words[0] < 0) { 3034 this.words[0] = -this.words[0]; 3035 this.negative = 1; 3036 } else { 3037 // Carry 3038 for (var i = 0; i < this.length && this.words[i] < 0; i++) { 3039 this.words[i] += 0x4000000; 3040 this.words[i + 1] -= 1; 3041 } 3042 } 3043 3044 return this.strip(); 3045 }; 3046 3047 BN.prototype.addn = function addn (num) { 3048 return this.clone().iaddn(num); 3049 }; 3050 3051 BN.prototype.subn = function subn (num) { 3052 return this.clone().isubn(num); 3053 }; 3054 3055 BN.prototype.iabs = function iabs () { 3056 this.negative = 0; 3057 3058 return this; 3059 }; 3060 3061 BN.prototype.abs = function abs () { 3062 return this.clone().iabs(); 3063 }; 3064 3065 BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) { 3066 var len = num.length + shift; 3067 var i; 3068 3069 this._expand(len); 3070 3071 var w; 3072 var carry = 0; 3073 for (i = 0; i < num.length; i++) { 3074 w = (this.words[i + shift] | 0) + carry; 3075 var right = (num.words[i] | 0) * mul; 3076 w -= right & 0x3ffffff; 3077 carry = (w >> 26) - ((right / 0x4000000) | 0); 3078 this.words[i + shift] = w & 0x3ffffff; 3079 } 3080 for (; i < this.length - shift; i++) { 3081 w = (this.words[i + shift] | 0) + carry; 3082 carry = w >> 26; 3083 this.words[i + shift] = w & 0x3ffffff; 3084 } 3085 3086 if (carry === 0) return this.strip(); 3087 3088 // Subtraction overflow 3089 assert(carry === -1); 3090 carry = 0; 3091 for (i = 0; i < this.length; i++) { 3092 w = -(this.words[i] | 0) + carry; 3093 carry = w >> 26; 3094 this.words[i] = w & 0x3ffffff; 3095 } 3096 this.negative = 1; 3097 3098 return this.strip(); 3099 }; 3100 3101 BN.prototype._wordDiv = function _wordDiv (num, mode) { 3102 var shift = this.length - num.length; 3103 3104 var a = this.clone(); 3105 var b = num; 3106 3107 // Normalize 3108 var bhi = b.words[b.length - 1] | 0; 3109 var bhiBits = this._countBits(bhi); 3110 shift = 26 - bhiBits; 3111 if (shift !== 0) { 3112 b = b.ushln(shift); 3113 a.iushln(shift); 3114 bhi = b.words[b.length - 1] | 0; 3115 } 3116 3117 // Initialize quotient 3118 var m = a.length - b.length; 3119 var q; 3120 3121 if (mode !== 'mod') { 3122 q = new BN(null); 3123 q.length = m + 1; 3124 q.words = new Array(q.length); 3125 for (var i = 0; i < q.length; i++) { 3126 q.words[i] = 0; 3127 } 3128 } 3129 3130 var diff = a.clone()._ishlnsubmul(b, 1, m); 3131 if (diff.negative === 0) { 3132 a = diff; 3133 if (q) { 3134 q.words[m] = 1; 3135 } 3136 } 3137 3138 for (var j = m - 1; j >= 0; j--) { 3139 var qj = (a.words[b.length + j] | 0) * 0x4000000 + 3140 (a.words[b.length + j - 1] | 0); 3141 3142 // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max 3143 // (0x7ffffff) 3144 qj = Math.min((qj / bhi) | 0, 0x3ffffff); 3145 3146 a._ishlnsubmul(b, qj, j); 3147 while (a.negative !== 0) { 3148 qj--; 3149 a.negative = 0; 3150 a._ishlnsubmul(b, 1, j); 3151 if (!a.isZero()) { 3152 a.negative ^= 1; 3153 } 3154 } 3155 if (q) { 3156 q.words[j] = qj; 3157 } 3158 } 3159 if (q) { 3160 q.strip(); 3161 } 3162 a.strip(); 3163 3164 // Denormalize 3165 if (mode !== 'div' && shift !== 0) { 3166 a.iushrn(shift); 3167 } 3168 3169 return { 3170 div: q || null, 3171 mod: a 3172 }; 3173 }; 3174 3175 // NOTE: 1) `mode` can be set to `mod` to request mod only, 3176 // to `div` to request div only, or be absent to 3177 // request both div & mod 3178 // 2) `positive` is true if unsigned mod is requested 3179 BN.prototype.divmod = function divmod (num, mode, positive) { 3180 assert(!num.isZero()); 3181 3182 if (this.isZero()) { 3183 return { 3184 div: new BN(0), 3185 mod: new BN(0) 3186 }; 3187 } 3188 3189 var div, mod, res; 3190 if (this.negative !== 0 && num.negative === 0) { 3191 res = this.neg().divmod(num, mode); 3192 3193 if (mode !== 'mod') { 3194 div = res.div.neg(); 3195 } 3196 3197 if (mode !== 'div') { 3198 mod = res.mod.neg(); 3199 if (positive && mod.negative !== 0) { 3200 mod.iadd(num); 3201 } 3202 } 3203 3204 return { 3205 div: div, 3206 mod: mod 3207 }; 3208 } 3209 3210 if (this.negative === 0 && num.negative !== 0) { 3211 res = this.divmod(num.neg(), mode); 3212 3213 if (mode !== 'mod') { 3214 div = res.div.neg(); 3215 } 3216 3217 return { 3218 div: div, 3219 mod: res.mod 3220 }; 3221 } 3222 3223 if ((this.negative & num.negative) !== 0) { 3224 res = this.neg().divmod(num.neg(), mode); 3225 3226 if (mode !== 'div') { 3227 mod = res.mod.neg(); 3228 if (positive && mod.negative !== 0) { 3229 mod.isub(num); 3230 } 3231 } 3232 3233 return { 3234 div: res.div, 3235 mod: mod 3236 }; 3237 } 3238 3239 // Both numbers are positive at this point 3240 3241 // Strip both numbers to approximate shift value 3242 if (num.length > this.length || this.cmp(num) < 0) { 3243 return { 3244 div: new BN(0), 3245 mod: this 3246 }; 3247 } 3248 3249 // Very short reduction 3250 if (num.length === 1) { 3251 if (mode === 'div') { 3252 return { 3253 div: this.divn(num.words[0]), 3254 mod: null 3255 }; 3256 } 3257 3258 if (mode === 'mod') { 3259 return { 3260 div: null, 3261 mod: new BN(this.modn(num.words[0])) 3262 }; 3263 } 3264 3265 return { 3266 div: this.divn(num.words[0]), 3267 mod: new BN(this.modn(num.words[0])) 3268 }; 3269 } 3270 3271 return this._wordDiv(num, mode); 3272 }; 3273 3274 // Find `this` / `num` 3275 BN.prototype.div = function div (num) { 3276 return this.divmod(num, 'div', false).div; 3277 }; 3278 3279 // Find `this` % `num` 3280 BN.prototype.mod = function mod (num) { 3281 return this.divmod(num, 'mod', false).mod; 3282 }; 3283 3284 BN.prototype.umod = function umod (num) { 3285 return this.divmod(num, 'mod', true).mod; 3286 }; 3287 3288 // Find Round(`this` / `num`) 3289 BN.prototype.divRound = function divRound (num) { 3290 var dm = this.divmod(num); 3291 3292 // Fast case - exact division 3293 if (dm.mod.isZero()) return dm.div; 3294 3295 var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod; 3296 3297 var half = num.ushrn(1); 3298 var r2 = num.andln(1); 3299 var cmp = mod.cmp(half); 3300 3301 // Round down 3302 if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div; 3303 3304 // Round up 3305 return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1); 3306 }; 3307 3308 BN.prototype.modn = function modn (num) { 3309 assert(num <= 0x3ffffff); 3310 var p = (1 << 26) % num; 3311 3312 var acc = 0; 3313 for (var i = this.length - 1; i >= 0; i--) { 3314 acc = (p * acc + (this.words[i] | 0)) % num; 3315 } 3316 3317 return acc; 3318 }; 3319 3320 // In-place division by number 3321 BN.prototype.idivn = function idivn (num) { 3322 assert(num <= 0x3ffffff); 3323 3324 var carry = 0; 3325 for (var i = this.length - 1; i >= 0; i--) { 3326 var w = (this.words[i] | 0) + carry * 0x4000000; 3327 this.words[i] = (w / num) | 0; 3328 carry = w % num; 3329 } 3330 3331 return this.strip(); 3332 }; 3333 3334 BN.prototype.divn = function divn (num) { 3335 return this.clone().idivn(num); 3336 }; 3337 3338 BN.prototype.egcd = function egcd (p) { 3339 assert(p.negative === 0); 3340 assert(!p.isZero()); 3341 3342 var x = this; 3343 var y = p.clone(); 3344 3345 if (x.negative !== 0) { 3346 x = x.umod(p); 3347 } else { 3348 x = x.clone(); 3349 } 3350 3351 // A * x + B * y = x 3352 var A = new BN(1); 3353 var B = new BN(0); 3354 3355 // C * x + D * y = y 3356 var C = new BN(0); 3357 var D = new BN(1); 3358 3359 var g = 0; 3360 3361 while (x.isEven() && y.isEven()) { 3362 x.iushrn(1); 3363 y.iushrn(1); 3364 ++g; 3365 } 3366 3367 var yp = y.clone(); 3368 var xp = x.clone(); 3369 3370 while (!x.isZero()) { 3371 for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1); 3372 if (i > 0) { 3373 x.iushrn(i); 3374 while (i-- > 0) { 3375 if (A.isOdd() || B.isOdd()) { 3376 A.iadd(yp); 3377 B.isub(xp); 3378 } 3379 3380 A.iushrn(1); 3381 B.iushrn(1); 3382 } 3383 } 3384 3385 for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); 3386 if (j > 0) { 3387 y.iushrn(j); 3388 while (j-- > 0) { 3389 if (C.isOdd() || D.isOdd()) { 3390 C.iadd(yp); 3391 D.isub(xp); 3392 } 3393 3394 C.iushrn(1); 3395 D.iushrn(1); 3396 } 3397 } 3398 3399 if (x.cmp(y) >= 0) { 3400 x.isub(y); 3401 A.isub(C); 3402 B.isub(D); 3403 } else { 3404 y.isub(x); 3405 C.isub(A); 3406 D.isub(B); 3407 } 3408 } 3409 3410 return { 3411 a: C, 3412 b: D, 3413 gcd: y.iushln(g) 3414 }; 3415 }; 3416 3417 // This is reduced incarnation of the binary EEA 3418 // above, designated to invert members of the 3419 // _prime_ fields F(p) at a maximal speed 3420 BN.prototype._invmp = function _invmp (p) { 3421 assert(p.negative === 0); 3422 assert(!p.isZero()); 3423 3424 var a = this; 3425 var b = p.clone(); 3426 3427 if (a.negative !== 0) { 3428 a = a.umod(p); 3429 } else { 3430 a = a.clone(); 3431 } 3432 3433 var x1 = new BN(1); 3434 var x2 = new BN(0); 3435 3436 var delta = b.clone(); 3437 3438 while (a.cmpn(1) > 0 && b.cmpn(1) > 0) { 3439 for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1); 3440 if (i > 0) { 3441 a.iushrn(i); 3442 while (i-- > 0) { 3443 if (x1.isOdd()) { 3444 x1.iadd(delta); 3445 } 3446 3447 x1.iushrn(1); 3448 } 3449 } 3450 3451 for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); 3452 if (j > 0) { 3453 b.iushrn(j); 3454 while (j-- > 0) { 3455 if (x2.isOdd()) { 3456 x2.iadd(delta); 3457 } 3458 3459 x2.iushrn(1); 3460 } 3461 } 3462 3463 if (a.cmp(b) >= 0) { 3464 a.isub(b); 3465 x1.isub(x2); 3466 } else { 3467 b.isub(a); 3468 x2.isub(x1); 3469 } 3470 } 3471 3472 var res; 3473 if (a.cmpn(1) === 0) { 3474 res = x1; 3475 } else { 3476 res = x2; 3477 } 3478 3479 if (res.cmpn(0) < 0) { 3480 res.iadd(p); 3481 } 3482 3483 return res; 3484 }; 3485 3486 BN.prototype.gcd = function gcd (num) { 3487 if (this.isZero()) return num.abs(); 3488 if (num.isZero()) return this.abs(); 3489 3490 var a = this.clone(); 3491 var b = num.clone(); 3492 a.negative = 0; 3493 b.negative = 0; 3494 3495 // Remove common factor of two 3496 for (var shift = 0; a.isEven() && b.isEven(); shift++) { 3497 a.iushrn(1); 3498 b.iushrn(1); 3499 } 3500 3501 do { 3502 while (a.isEven()) { 3503 a.iushrn(1); 3504 } 3505 while (b.isEven()) { 3506 b.iushrn(1); 3507 } 3508 3509 var r = a.cmp(b); 3510 if (r < 0) { 3511 // Swap `a` and `b` to make `a` always bigger than `b` 3512 var t = a; 3513 a = b; 3514 b = t; 3515 } else if (r === 0 || b.cmpn(1) === 0) { 3516 break; 3517 } 3518 3519 a.isub(b); 3520 } while (true); 3521 3522 return b.iushln(shift); 3523 }; 3524 3525 // Invert number in the field F(num) 3526 BN.prototype.invm = function invm (num) { 3527 return this.egcd(num).a.umod(num); 3528 }; 3529 3530 BN.prototype.isEven = function isEven () { 3531 return (this.words[0] & 1) === 0; 3532 }; 3533 3534 BN.prototype.isOdd = function isOdd () { 3535 return (this.words[0] & 1) === 1; 3536 }; 3537 3538 // And first word and num 3539 BN.prototype.andln = function andln (num) { 3540 return this.words[0] & num; 3541 }; 3542 3543 // Increment at the bit position in-line 3544 BN.prototype.bincn = function bincn (bit) { 3545 assert(typeof bit === 'number'); 3546 var r = bit % 26; 3547 var s = (bit - r) / 26; 3548 var q = 1 << r; 3549 3550 // Fast case: bit is much higher than all existing words 3551 if (this.length <= s) { 3552 this._expand(s + 1); 3553 this.words[s] |= q; 3554 return this; 3555 } 3556 3557 // Add bit and propagate, if needed 3558 var carry = q; 3559 for (var i = s; carry !== 0 && i < this.length; i++) { 3560 var w = this.words[i] | 0; 3561 w += carry; 3562 carry = w >>> 26; 3563 w &= 0x3ffffff; 3564 this.words[i] = w; 3565 } 3566 if (carry !== 0) { 3567 this.words[i] = carry; 3568 this.length++; 3569 } 3570 return this; 3571 }; 3572 3573 BN.prototype.isZero = function isZero () { 3574 return this.length === 1 && this.words[0] === 0; 3575 }; 3576 3577 BN.prototype.cmpn = function cmpn (num) { 3578 var negative = num < 0; 3579 3580 if (this.negative !== 0 && !negative) return -1; 3581 if (this.negative === 0 && negative) return 1; 3582 3583 this.strip(); 3584 3585 var res; 3586 if (this.length > 1) { 3587 res = 1; 3588 } else { 3589 if (negative) { 3590 num = -num; 3591 } 3592 3593 assert(num <= 0x3ffffff, 'Number is too big'); 3594 3595 var w = this.words[0] | 0; 3596 res = w === num ? 0 : w < num ? -1 : 1; 3597 } 3598 if (this.negative !== 0) return -res | 0; 3599 return res; 3600 }; 3601 3602 // Compare two numbers and return: 3603 // 1 - if `this` > `num` 3604 // 0 - if `this` == `num` 3605 // -1 - if `this` < `num` 3606 BN.prototype.cmp = function cmp (num) { 3607 if (this.negative !== 0 && num.negative === 0) return -1; 3608 if (this.negative === 0 && num.negative !== 0) return 1; 3609 3610 var res = this.ucmp(num); 3611 if (this.negative !== 0) return -res | 0; 3612 return res; 3613 }; 3614 3615 // Unsigned comparison 3616 BN.prototype.ucmp = function ucmp (num) { 3617 // At this point both numbers have the same sign 3618 if (this.length > num.length) return 1; 3619 if (this.length < num.length) return -1; 3620 3621 var res = 0; 3622 for (var i = this.length - 1; i >= 0; i--) { 3623 var a = this.words[i] | 0; 3624 var b = num.words[i] | 0; 3625 3626 if (a === b) continue; 3627 if (a < b) { 3628 res = -1; 3629 } else if (a > b) { 3630 res = 1; 3631 } 3632 break; 3633 } 3634 return res; 3635 }; 3636 3637 BN.prototype.gtn = function gtn (num) { 3638 return this.cmpn(num) === 1; 3639 }; 3640 3641 BN.prototype.gt = function gt (num) { 3642 return this.cmp(num) === 1; 3643 }; 3644 3645 BN.prototype.gten = function gten (num) { 3646 return this.cmpn(num) >= 0; 3647 }; 3648 3649 BN.prototype.gte = function gte (num) { 3650 return this.cmp(num) >= 0; 3651 }; 3652 3653 BN.prototype.ltn = function ltn (num) { 3654 return this.cmpn(num) === -1; 3655 }; 3656 3657 BN.prototype.lt = function lt (num) { 3658 return this.cmp(num) === -1; 3659 }; 3660 3661 BN.prototype.lten = function lten (num) { 3662 return this.cmpn(num) <= 0; 3663 }; 3664 3665 BN.prototype.lte = function lte (num) { 3666 return this.cmp(num) <= 0; 3667 }; 3668 3669 BN.prototype.eqn = function eqn (num) { 3670 return this.cmpn(num) === 0; 3671 }; 3672 3673 BN.prototype.eq = function eq (num) { 3674 return this.cmp(num) === 0; 3675 }; 3676 3677 // 3678 // A reduce context, could be using montgomery or something better, depending 3679 // on the `m` itself. 3680 // 3681 BN.red = function red (num) { 3682 return new Red(num); 3683 }; 3684 3685 BN.prototype.toRed = function toRed (ctx) { 3686 assert(!this.red, 'Already a number in reduction context'); 3687 assert(this.negative === 0, 'red works only with positives'); 3688 return ctx.convertTo(this)._forceRed(ctx); 3689 }; 3690 3691 BN.prototype.fromRed = function fromRed () { 3692 assert(this.red, 'fromRed works only with numbers in reduction context'); 3693 return this.red.convertFrom(this); 3694 }; 3695 3696 BN.prototype._forceRed = function _forceRed (ctx) { 3697 this.red = ctx; 3698 return this; 3699 }; 3700 3701 BN.prototype.forceRed = function forceRed (ctx) { 3702 assert(!this.red, 'Already a number in reduction context'); 3703 return this._forceRed(ctx); 3704 }; 3705 3706 BN.prototype.redAdd = function redAdd (num) { 3707 assert(this.red, 'redAdd works only with red numbers'); 3708 return this.red.add(this, num); 3709 }; 3710 3711 BN.prototype.redIAdd = function redIAdd (num) { 3712 assert(this.red, 'redIAdd works only with red numbers'); 3713 return this.red.iadd(this, num); 3714 }; 3715 3716 BN.prototype.redSub = function redSub (num) { 3717 assert(this.red, 'redSub works only with red numbers'); 3718 return this.red.sub(this, num); 3719 }; 3720 3721 BN.prototype.redISub = function redISub (num) { 3722 assert(this.red, 'redISub works only with red numbers'); 3723 return this.red.isub(this, num); 3724 }; 3725 3726 BN.prototype.redShl = function redShl (num) { 3727 assert(this.red, 'redShl works only with red numbers'); 3728 return this.red.shl(this, num); 3729 }; 3730 3731 BN.prototype.redMul = function redMul (num) { 3732 assert(this.red, 'redMul works only with red numbers'); 3733 this.red._verify2(this, num); 3734 return this.red.mul(this, num); 3735 }; 3736 3737 BN.prototype.redIMul = function redIMul (num) { 3738 assert(this.red, 'redMul works only with red numbers'); 3739 this.red._verify2(this, num); 3740 return this.red.imul(this, num); 3741 }; 3742 3743 BN.prototype.redSqr = function redSqr () { 3744 assert(this.red, 'redSqr works only with red numbers'); 3745 this.red._verify1(this); 3746 return this.red.sqr(this); 3747 }; 3748 3749 BN.prototype.redISqr = function redISqr () { 3750 assert(this.red, 'redISqr works only with red numbers'); 3751 this.red._verify1(this); 3752 return this.red.isqr(this); 3753 }; 3754 3755 // Square root over p 3756 BN.prototype.redSqrt = function redSqrt () { 3757 assert(this.red, 'redSqrt works only with red numbers'); 3758 this.red._verify1(this); 3759 return this.red.sqrt(this); 3760 }; 3761
vendor: 12,018 bytes, lines 3762-4279
3762 BN.prototype.redInvm = function redInvm () { 3763 assert(this.red, 'redInvm works only with red numbers'); 3764 this.red._verify1(this); 3765 return this.red.invm(this); 3766 }; 3767 3768 // Return negative clone of `this` % `red modulo` 3769 BN.prototype.redNeg = function redNeg () { 3770 assert(this.red, 'redNeg works only with red numbers'); 3771 this.red._verify1(this); 3772 return this.red.neg(this); 3773 }; 3774 3775 BN.prototype.redPow = function redPow (num) { 3776 assert(this.red && !num.red, 'redPow(normalNum)'); 3777 this.red._verify1(this); 3778 return this.red.pow(this, num); 3779 }; 3780 3781 // Prime numbers with efficient reduction 3782 var primes = { 3783 k256: null, 3784 p224: null, 3785 p192: null, 3786 p25519: null 3787 }; 3788 3789 // Pseudo-Mersenne prime 3790 function MPrime (name, p) { 3791 // P = 2 ^ N - K 3792 this.name = name; 3793 this.p = new BN(p, 16); 3794 this.n = this.p.bitLength(); 3795 this.k = new BN(1).iushln(this.n).isub(this.p); 3796 3797 this.tmp = this._tmp(); 3798 } 3799 3800 MPrime.prototype._tmp = function _tmp () { 3801 var tmp = new BN(null); 3802 tmp.words = new Array(Math.ceil(this.n / 13)); 3803 return tmp; 3804 }; 3805 3806 MPrime.prototype.ireduce = function ireduce (num) { 3807 // Assumes that `num` is less than `P^2` 3808 // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P) 3809 var r = num; 3810 var rlen; 3811 3812 do { 3813 this.split(r, this.tmp); 3814 r = this.imulK(r); 3815 r = r.iadd(this.tmp); 3816 rlen = r.bitLength(); 3817 } while (rlen > this.n); 3818 3819 var cmp = rlen < this.n ? -1 : r.ucmp(this.p); 3820 if (cmp === 0) { 3821 r.words[0] = 0; 3822 r.length = 1; 3823 } else if (cmp > 0) { 3824 r.isub(this.p); 3825 } else { 3826 r.strip(); 3827 } 3828 3829 return r; 3830 }; 3831 3832 MPrime.prototype.split = function split (input, out) { 3833 input.iushrn(this.n, 0, out); 3834 }; 3835 3836 MPrime.prototype.imulK = function imulK (num) { 3837 return num.imul(this.k); 3838 }; 3839 3840 function K256 () { 3841 MPrime.call( 3842 this, 3843 'k256', 3844 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f'); 3845 } 3846 inherits(K256, MPrime); 3847 3848 K256.prototype.split = function split (input, output) { 3849 // 256 = 9 * 26 + 22 3850 var mask = 0x3fffff; 3851 3852 var outLen = Math.min(input.length, 9); 3853 for (var i = 0; i < outLen; i++) { 3854 output.words[i] = input.words[i]; 3855 } 3856 output.length = outLen; 3857 3858 if (input.length <= 9) { 3859 input.words[0] = 0; 3860 input.length = 1; 3861 return; 3862 } 3863 3864 // Shift by 9 limbs 3865 var prev = input.words[9]; 3866 output.words[output.length++] = prev & mask; 3867 3868 for (i = 10; i < input.length; i++) { 3869 var next = input.words[i] | 0; 3870 input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22); 3871 prev = next; 3872 } 3873 prev >>>= 22; 3874 input.words[i - 10] = prev; 3875 if (prev === 0 && input.length > 10) { 3876 input.length -= 10; 3877 } else { 3878 input.length -= 9; 3879 } 3880 }; 3881 3882 K256.prototype.imulK = function imulK (num) { 3883 // K = 0x1000003d1 = [ 0x40, 0x3d1 ] 3884 num.words[num.length] = 0; 3885 num.words[num.length + 1] = 0; 3886 num.length += 2; 3887 3888 // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390 3889 var lo = 0; 3890 for (var i = 0; i < num.length; i++) { 3891 var w = num.words[i] | 0; 3892 lo += w * 0x3d1; 3893 num.words[i] = lo & 0x3ffffff; 3894 lo = w * 0x40 + ((lo / 0x4000000) | 0); 3895 } 3896 3897 // Fast length reduction 3898 if (num.words[num.length - 1] === 0) { 3899 num.length--; 3900 if (num.words[num.length - 1] === 0) { 3901 num.length--; 3902 } 3903 } 3904 return num; 3905 }; 3906 3907 function P224 () { 3908 MPrime.call( 3909 this, 3910 'p224', 3911 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001'); 3912 } 3913 inherits(P224, MPrime); 3914 3915 function P192 () { 3916 MPrime.call( 3917 this, 3918 'p192', 3919 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff'); 3920 } 3921 inherits(P192, MPrime); 3922 3923 function P25519 () { 3924 // 2 ^ 255 - 19 3925 MPrime.call( 3926 this, 3927 '25519', 3928 '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed'); 3929 } 3930 inherits(P25519, MPrime); 3931 3932 P25519.prototype.imulK = function imulK (num) { 3933 // K = 0x13 3934 var carry = 0; 3935 for (var i = 0; i < num.length; i++) { 3936 var hi = (num.words[i] | 0) * 0x13 + carry; 3937 var lo = hi & 0x3ffffff; 3938 hi >>>= 26; 3939 3940 num.words[i] = lo; 3941 carry = hi; 3942 } 3943 if (carry !== 0) { 3944 num.words[num.length++] = carry; 3945 } 3946 return num; 3947 }; 3948 3949 // Exported mostly for testing purposes, use plain name instead 3950 BN._prime = function prime (name) { 3951 // Cached version of prime 3952 if (primes[name]) return primes[name]; 3953 3954 var prime; 3955 if (name === 'k256') { 3956 prime = new K256(); 3957 } else if (name === 'p224') { 3958 prime = new P224(); 3959 } else if (name === 'p192') { 3960 prime = new P192(); 3961 } else if (name === 'p25519') { 3962 prime = new P25519(); 3963 } else { 3964 throw new Error('Unknown prime ' + name); 3965 } 3966 primes[name] = prime; 3967 3968 return prime; 3969 }; 3970 3971 // 3972 // Base reduction engine 3973 // 3974 function Red (m) { 3975 if (typeof m === 'string') { 3976 var prime = BN._prime(m); 3977 this.m = prime.p; 3978 this.prime = prime; 3979 } else { 3980 assert(m.gtn(1), 'modulus must be greater than 1'); 3981 this.m = m; 3982 this.prime = null; 3983 } 3984 } 3985 3986 Red.prototype._verify1 = function _verify1 (a) { 3987 assert(a.negative === 0, 'red works only with positives'); 3988 assert(a.red, 'red works only with red numbers'); 3989 }; 3990 3991 Red.prototype._verify2 = function _verify2 (a, b) { 3992 assert((a.negative | b.negative) === 0, 'red works only with positives'); 3993 assert(a.red && a.red === b.red, 3994 'red works only with red numbers'); 3995 }; 3996 3997 Red.prototype.imod = function imod (a) { 3998 if (this.prime) return this.prime.ireduce(a)._forceRed(this); 3999 return a.umod(this.m)._forceRed(this); 4000 }; 4001 4002 Red.prototype.neg = function neg (a) { 4003 if (a.isZero()) { 4004 return a.clone(); 4005 } 4006 4007 return this.m.sub(a)._forceRed(this); 4008 }; 4009 4010 Red.prototype.add = function add (a, b) { 4011 this._verify2(a, b); 4012 4013 var res = a.add(b); 4014 if (res.cmp(this.m) >= 0) { 4015 res.isub(this.m); 4016 } 4017 return res._forceRed(this); 4018 }; 4019 4020 Red.prototype.iadd = function iadd (a, b) { 4021 this._verify2(a, b); 4022 4023 var res = a.iadd(b); 4024 if (res.cmp(this.m) >= 0) { 4025 res.isub(this.m); 4026 } 4027 return res; 4028 }; 4029 4030 Red.prototype.sub = function sub (a, b) { 4031 this._verify2(a, b); 4032 4033 var res = a.sub(b); 4034 if (res.cmpn(0) < 0) { 4035 res.iadd(this.m); 4036 } 4037 return res._forceRed(this); 4038 }; 4039 4040 Red.prototype.isub = function isub (a, b) { 4041 this._verify2(a, b); 4042 4043 var res = a.isub(b); 4044 if (res.cmpn(0) < 0) { 4045 res.iadd(this.m); 4046 } 4047 return res; 4048 }; 4049 4050 Red.prototype.shl = function shl (a, num) { 4051 this._verify1(a); 4052 return this.imod(a.ushln(num)); 4053 }; 4054 4055 Red.prototype.imul = function imul (a, b) { 4056 this._verify2(a, b); 4057 return this.imod(a.imul(b)); 4058 }; 4059 4060 Red.prototype.mul = function mul (a, b) { 4061 this._verify2(a, b); 4062 return this.imod(a.mul(b)); 4063 }; 4064 4065 Red.prototype.isqr = function isqr (a) { 4066 return this.imul(a, a.clone()); 4067 }; 4068 4069 Red.prototype.sqr = function sqr (a) { 4070 return this.mul(a, a); 4071 }; 4072 4073 Red.prototype.sqrt = function sqrt (a) { 4074 if (a.isZero()) return a.clone(); 4075 4076 var mod3 = this.m.andln(3); 4077 assert(mod3 % 2 === 1); 4078 4079 // Fast case 4080 if (mod3 === 3) { 4081 var pow = this.m.add(new BN(1)).iushrn(2); 4082 return this.pow(a, pow); 4083 } 4084 4085 // Tonelli-Shanks algorithm (Totally unoptimized and slow) 4086 // 4087 // Find Q and S, that Q * 2 ^ S = (P - 1) 4088 var q = this.m.subn(1); 4089 var s = 0; 4090 while (!q.isZero() && q.andln(1) === 0) { 4091 s++; 4092 q.iushrn(1); 4093 } 4094 assert(!q.isZero()); 4095 4096 var one = new BN(1).toRed(this); 4097 var nOne = one.redNeg(); 4098 4099 // Find quadratic non-residue 4100 // NOTE: Max is such because of generalized Riemann hypothesis. 4101 var lpow = this.m.subn(1).iushrn(1); 4102 var z = this.m.bitLength(); 4103 z = new BN(2 * z * z).toRed(this); 4104 4105 while (this.pow(z, lpow).cmp(nOne) !== 0) { 4106 z.redIAdd(nOne); 4107 } 4108 4109 var c = this.pow(z, q); 4110 var r = this.pow(a, q.addn(1).iushrn(1)); 4111 var t = this.pow(a, q); 4112 var m = s; 4113 while (t.cmp(one) !== 0) { 4114 var tmp = t; 4115 for (var i = 0; tmp.cmp(one) !== 0; i++) { 4116 tmp = tmp.redSqr(); 4117 } 4118 assert(i < m); 4119 var b = this.pow(c, new BN(1).iushln(m - i - 1)); 4120 4121 r = r.redMul(b); 4122 c = b.redSqr(); 4123 t = t.redMul(c); 4124 m = i; 4125 } 4126 4127 return r; 4128 }; 4129 4130 Red.prototype.invm = function invm (a) { 4131 var inv = a._invmp(this.m); 4132 if (inv.negative !== 0) { 4133 inv.negative = 0; 4134 return this.imod(inv).redNeg(); 4135 } else { 4136 return this.imod(inv); 4137 } 4138 }; 4139 4140 Red.prototype.pow = function pow (a, num) { 4141 if (num.isZero()) return new BN(1).toRed(this); 4142 if (num.cmpn(1) === 0) return a.clone(); 4143 4144 var windowSize = 4; 4145 var wnd = new Array(1 << windowSize); 4146 wnd[0] = new BN(1).toRed(this); 4147 wnd[1] = a; 4148 for (var i = 2; i < wnd.length; i++) { 4149 wnd[i] = this.mul(wnd[i - 1], a); 4150 } 4151 4152 var res = wnd[0]; 4153 var current = 0; 4154 var currentLen = 0; 4155 var start = num.bitLength() % 26; 4156 if (start === 0) { 4157 start = 26; 4158 } 4159 4160 for (i = num.length - 1; i >= 0; i--) { 4161 var word = num.words[i]; 4162 for (var j = start - 1; j >= 0; j--) { 4163 var bit = (word >> j) & 1; 4164 if (res !== wnd[0]) { 4165 res = this.sqr(res); 4166 } 4167 4168 if (bit === 0 && current === 0) { 4169 currentLen = 0; 4170 continue; 4171 } 4172 4173 current <<= 1; 4174 current |= bit; 4175 currentLen++; 4176 if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue; 4177 4178 res = this.mul(res, wnd[current]); 4179 currentLen = 0; 4180 current = 0; 4181 } 4182 start = 26; 4183 } 4184 4185 return res; 4186 }; 4187 4188 Red.prototype.convertTo = function convertTo (num) { 4189 var r = num.umod(this.m); 4190 4191 return r === num ? r.clone() : r; 4192 }; 4193 4194 Red.prototype.convertFrom = function convertFrom (num) { 4195 var res = num.clone(); 4196 res.red = null; 4197 return res; 4198 }; 4199 4200 // 4201 // Montgomery method engine 4202 // 4203 4204 BN.mont = function mont (num) { 4205 return new Mont(num); 4206 }; 4207 4208 function Mont (m) { 4209 Red.call(this, m); 4210 4211 this.shift = this.m.bitLength(); 4212 if (this.shift % 26 !== 0) { 4213 this.shift += 26 - (this.shift % 26); 4214 } 4215 4216 this.r = new BN(1).iushln(this.shift); 4217 this.r2 = this.imod(this.r.sqr()); 4218 this.rinv = this.r._invmp(this.m); 4219 4220 this.minv = this.rinv.mul(this.r).isubn(1).div(this.m); 4221 this.minv = this.minv.umod(this.r); 4222 this.minv = this.r.sub(this.minv); 4223 } 4224 inherits(Mont, Red); 4225 4226 Mont.prototype.convertTo = function convertTo (num) { 4227 return this.imod(num.ushln(this.shift)); 4228 }; 4229 4230 Mont.prototype.convertFrom = function convertFrom (num) { 4231 var r = this.imod(num.mul(this.rinv)); 4232 r.red = null; 4233 return r; 4234 }; 4235 4236 Mont.prototype.imul = function imul (a, b) { 4237 if (a.isZero() || b.isZero()) { 4238 a.words[0] = 0; 4239 a.length = 1; 4240 return a; 4241 } 4242 4243 var t = a.imul(b); 4244 var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); 4245 var u = t.isub(c).iushrn(this.shift); 4246 var res = u; 4247 4248 if (u.cmp(this.m) >= 0) { 4249 res = u.isub(this.m); 4250 } else if (u.cmpn(0) < 0) { 4251 res = u.iadd(this.m); 4252 } 4253 4254 return res._forceRed(this); 4255 }; 4256 4257 Mont.prototype.mul = function mul (a, b) { 4258 if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this); 4259 4260 var t = a.mul(b); 4261 var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); 4262 var u = t.isub(c).iushrn(this.shift); 4263 var res = u; 4264 if (u.cmp(this.m) >= 0) { 4265 res = u.isub(this.m); 4266 } else if (u.cmpn(0) < 0) { 4267 res = u.iadd(this.m); 4268 } 4269 4270 return res._forceRed(this); 4271 }; 4272 4273 Mont.prototype.invm = function invm (a) { 4274 // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R 4275 var res = this.imod(a._invmp(this.m).mul(this.r2)); 4276 return res._forceRed(this); 4277 }; 4278})(typeof module === 'undefined' || module, this); 4279
4280},{"buffer":81}],24:[function(require,module,exports){ 4281'use strict' 4282 4283module.exports = findBounds 4284 4285function findBounds(points) { 4286 var n = points.length 4287 if(n === 0) { 4288 return [[], []] 4289 } 4290 var d = points[0].length 4291 var lo = points[0].slice() 4292 var hi = points[0].slice() 4293 for(var i=1; i<n; ++i) { 4294 var p = points[i] 4295 for(var j=0; j<d; ++j) { 4296 var x = p[j] 4297 lo[j] = Math.min(lo[j], x) 4298 hi[j] = Math.max(hi[j], x) 4299 } 4300 } 4301 return [lo, hi] 4302} 4303},{}],25:[function(require,module,exports){ 4304'use strict' 4305 4306module.exports = boxIntersectWrapper 4307 4308var pool = require('typedarray-pool') 4309var sweep = require('./lib/sweep') 4310var boxIntersectIter = require('./lib/intersect') 4311 4312function boxEmpty(d, box) { 4313 for(var j=0; j<d; ++j) { 4314 if(!(box[j] <= box[j+d])) { 4315 return true 4316 } 4317 } 4318 return false 4319} 4320 4321//Unpack boxes into a flat typed array, remove empty boxes 4322function convertBoxes(boxes, d, data, ids) { 4323 var ptr = 0 4324 var count = 0 4325 for(var i=0, n=boxes.length; i<n; ++i) { 4326 var b = boxes[i] 4327 if(boxEmpty(d, b)) { 4328 continue 4329 } 4330 for(var j=0; j<2*d; ++j) { 4331 data[ptr++] = b[j] 4332 } 4333 ids[count++] = i 4334 } 4335 return count 4336} 4337 4338//Perform type conversions, check bounds 4339function boxIntersect(red, blue, visit, full) { 4340 var n = red.length 4341 var m = blue.length 4342 4343 //If either array is empty, then we can skip this whole thing 4344 if(n <= 0 || m <= 0) { 4345 return 4346 } 4347 4348 //Compute dimension, if it is 0 then we skip 4349 var d = (red[0].length)>>>1 4350 if(d <= 0) { 4351 return 4352 } 4353 4354 var retval 4355 4356 //Convert red boxes 4357 var redList = pool.mallocDouble(2*d*n) 4358 var redIds = pool.mallocInt32(n) 4359 n = convertBoxes(red, d, redList, redIds) 4360 4361 if(n > 0) { 4362 if(d === 1 && full) { 4363 //Special case: 1d complete 4364 sweep.init(n) 4365 retval = sweep.sweepComplete( 4366 d, visit, 4367 0, n, redList, redIds, 4368 0, n, redList, redIds) 4369 } else { 4370 4371 //Convert blue boxes 4372 var blueList = pool.mallocDouble(2*d*m) 4373 var blueIds = pool.mallocInt32(m) 4374 m = convertBoxes(blue, d, blueList, blueIds) 4375 4376 if(m > 0) { 4377 sweep.init(n+m) 4378 4379 if(d === 1) { 4380 //Special case: 1d bipartite 4381 retval = sweep.sweepBipartite( 4382 d, visit, 4383 0, n, redList, redIds, 4384 0, m, blueList, blueIds) 4385 } else { 4386 //General case: d>1 4387 retval = boxIntersectIter( 4388 d, visit, full, 4389 n, redList, redIds, 4390 m, blueList, blueIds) 4391 } 4392 4393 pool.free(blueList) 4394 pool.free(blueIds) 4395 } 4396 } 4397 4398 pool.free(redList) 4399 pool.free(redIds) 4400 } 4401 4402 return retval 4403} 4404 4405 4406var RESULT 4407 4408function appendItem(i,j) { 4409 RESULT.push([i,j]) 4410} 4411 4412function intersectFullArray(x) { 4413 RESULT = [] 4414 boxIntersect(x, x, appendItem, true) 4415 return RESULT 4416} 4417 4418function intersectBipartiteArray(x, y) { 4419 RESULT = [] 4420 boxIntersect(x, y, appendItem, false) 4421 return RESULT 4422} 4423 4424//User-friendly wrapper, handle full input and no-visitor cases 4425function boxIntersectWrapper(arg0, arg1, arg2) { 4426 var result 4427 switch(arguments.length) { 4428 case 1: 4429 return intersectFullArray(arg0) 4430 case 2: 4431 if(typeof arg1 === 'function') { 4432 return boxIntersect(arg0, arg0, arg1, true) 4433 } else { 4434 return intersectBipartiteArray(arg0, arg1) 4435 } 4436 case 3: 4437 return boxIntersect(arg0, arg1, arg2, false) 4438 default: 4439 throw new Error('box-intersect: Invalid arguments') 4440 } 4441} 4442},{"./lib/intersect":27,"./lib/sweep":31,"typedarray-pool":74}],26:[function(require,module,exports){ 4443'use strict' 4444 4445var DIMENSION = 'd' 4446var AXIS = 'ax' 4447var VISIT = 'vv' 4448var FLIP = 'fp' 4449 4450var ELEM_SIZE = 'es' 4451 4452var RED_START = 'rs' 4453var RED_END = 're' 4454var RED_BOXES = 'rb' 4455var RED_INDEX = 'ri' 4456var RED_PTR = 'rp' 4457 4458var BLUE_START = 'bs' 4459var BLUE_END = 'be' 4460var BLUE_BOXES = 'bb' 4461var BLUE_INDEX = 'bi' 4462var BLUE_PTR = 'bp' 4463 4464var RETVAL = 'rv' 4465 4466var INNER_LABEL = 'Q' 4467 4468var ARGS = [ 4469 DIMENSION, 4470 AXIS, 4471 VISIT, 4472 RED_START, 4473 RED_END, 4474 RED_BOXES, 4475 RED_INDEX, 4476 BLUE_START, 4477 BLUE_END, 4478 BLUE_BOXES, 4479 BLUE_INDEX 4480] 4481 4482function generateBruteForce(redMajor, flip, full) { 4483 var funcName = 'bruteForce' + 4484 (redMajor ? 'Red' : 'Blue') + 4485 (flip ? 'Flip' : '') + 4486 (full ? 'Full' : '') 4487 4488 var code = ['function ', funcName, '(', ARGS.join(), '){', 4489 'var ', ELEM_SIZE, '=2*', DIMENSION, ';'] 4490 4491 var redLoop = 4492 'for(var i=' + RED_START + ',' + RED_PTR + '=' + ELEM_SIZE + '*' + RED_START + ';' + 4493 'i<' + RED_END +';' + 4494 '++i,' + RED_PTR + '+=' + ELEM_SIZE + '){' + 4495 'var x0=' + RED_BOXES + '[' + AXIS + '+' + RED_PTR + '],' + 4496 'x1=' + RED_BOXES + '[' + AXIS + '+' + RED_PTR + '+' + DIMENSION + '],' + 4497 'xi=' + RED_INDEX + '[i];' 4498 4499 var blueLoop = 4500 'for(var j=' + BLUE_START + ',' + BLUE_PTR + '=' + ELEM_SIZE + '*' + BLUE_START + ';' + 4501 'j<' + BLUE_END + ';' + 4502 '++j,' + BLUE_PTR + '+=' + ELEM_SIZE + '){' + 4503 'var y0=' + BLUE_BOXES + '[' + AXIS + '+' + BLUE_PTR + '],' + 4504 (full ? 'y1=' + BLUE_BOXES + '[' + AXIS + '+' + BLUE_PTR + '+' + DIMENSION + '],' : '') + 4505 'yi=' + BLUE_INDEX + '[j];' 4506 4507 if(redMajor) { 4508 code.push(redLoop, INNER_LABEL, ':', blueLoop) 4509 } else { 4510 code.push(blueLoop, INNER_LABEL, ':', redLoop) 4511 } 4512 4513 if(full) { 4514 code.push('if(y1<x0||x1<y0)continue;') 4515 } else if(flip) { 4516 code.push('if(y0<=x0||x1<y0)continue;') 4517 } else { 4518 code.push('if(y0<x0||x1<y0)continue;') 4519 } 4520 4521 code.push('for(var k='+AXIS+'+1;k<'+DIMENSION+';++k){'+ 4522 'var r0='+RED_BOXES+'[k+'+RED_PTR+'],'+ 4523 'r1='+RED_BOXES+'[k+'+DIMENSION+'+'+RED_PTR+'],'+ 4524 'b0='+BLUE_BOXES+'[k+'+BLUE_PTR+'],'+ 4525 'b1='+BLUE_BOXES+'[k+'+DIMENSION+'+'+BLUE_PTR+'];'+ 4526 'if(r1<b0||b1<r0)continue ' + INNER_LABEL + ';}' + 4527 'var ' + RETVAL + '=' + VISIT + '(') 4528 4529 if(flip) { 4530 code.push('yi,xi') 4531 } else { 4532 code.push('xi,yi') 4533 } 4534 4535 code.push(');if(' + RETVAL + '!==void 0)return ' + RETVAL + ';}}}') 4536 4537 return { 4538 name: funcName, 4539 code: code.join('') 4540 } 4541} 4542 4543function bruteForcePlanner(full) { 4544 var funcName = 'bruteForce' + (full ? 'Full' : 'Partial') 4545 var prefix = [] 4546 var fargs = ARGS.slice() 4547 if(!full) { 4548 fargs.splice(3, 0, FLIP) 4549 } 4550 4551 var code = ['function ' + funcName + '(' + fargs.join() + '){'] 4552 4553 function invoke(redMajor, flip) { 4554 var res = generateBruteForce(redMajor, flip, full) 4555 prefix.push(res.code) 4556 code.push('return ' + res.name + '(' + ARGS.join() + ');') 4557 } 4558 4559 code.push('if(' + RED_END + '-' + RED_START + '>' + 4560 BLUE_END + '-' + BLUE_START + '){') 4561 4562 if(full) { 4563 invoke(true, false) 4564 code.push('}else{') 4565 invoke(false, false) 4566 } else { 4567 code.push('if(' + FLIP + '){') 4568 invoke(true, true) 4569 code.push('}else{') 4570 invoke(true, false) 4571 code.push('}}else{if(' + FLIP + '){') 4572 invoke(false, true) 4573 code.push('}else{') 4574 invoke(false, false) 4575 code.push('}') 4576 } 4577 code.push('}}return ' + funcName) 4578 4579 var codeStr = prefix.join('') + code.join('') 4580 var proc = new Function(codeStr) 4581 return proc() 4582} 4583 4584 4585exports.partial = bruteForcePlanner(false) 4586exports.full = bruteForcePlanner(true) 4587},{}],27:[function(require,module,exports){ 4588'use strict' 4589 4590module.exports = boxIntersectIter 4591 4592var pool = require('typedarray-pool') 4593var bits = require('bit-twiddle') 4594var bruteForce = require('./brute') 4595var bruteForcePartial = bruteForce.partial 4596var bruteForceFull = bruteForce.full 4597var sweep = require('./sweep') 4598var findMedian = require('./median') 4599var genPartition = require('./partition') 4600 4601//Twiddle parameters 4602var BRUTE_FORCE_CUTOFF = 128 //Cut off for brute force search 4603var SCAN_CUTOFF = (1<<22) //Cut off for two way scan 4604var SCAN_COMPLETE_CUTOFF = (1<<22) 4605 4606//Partition functions
4607var partitionInteriorContainsInterval = genPartition( 4608 '!(lo>=p0)&&!(p1>=hi)', 4609 ['p0', 'p1']) 4610 4611var partitionStartEqual = genPartition( 4612 'lo===p0', 4613 ['p0']) 4614 4615var partitionStartLessThan = genPartition( 4616 'lo<p0', 4617 ['p0']) 4618 4619var partitionEndLessThanEqual = genPartition( 4620 'hi<=p0', 4621 ['p0']) 4622 4623var partitionContainsPoint = genPartition( 4624 'lo<=p0&&p0<=hi', 4625 ['p0']) 4626 4627var partitionContainsPointProper = genPartition( 4628 'lo<p0&&p0<=hi', 4629 ['p0']) 4630 4631//Frame size for iterative loop 4632var IFRAME_SIZE = 6 4633var DFRAME_SIZE = 2 4634 4635//Data for box statck 4636var INIT_CAPACITY = 1024 4637var BOX_ISTACK = pool.mallocInt32(INIT_CAPACITY) 4638var BOX_DSTACK = pool.mallocDouble(INIT_CAPACITY) 4639 4640//Initialize iterative loop queue 4641function iterInit(d, count) { 4642 var levels = (8 * bits.log2(count+1) * (d+1))|0 4643 var maxInts = bits.nextPow2(IFRAME_SIZE*levels) 4644 if(BOX_ISTACK.length < maxInts) { 4645 pool.free(BOX_ISTACK) 4646 BOX_ISTACK = pool.mallocInt32(maxInts) 4647 } 4648 var maxDoubles = bits.nextPow2(DFRAME_SIZE*levels) 4649 if(BOX_DSTACK < maxDoubles) { 4650 pool.free(BOX_DSTACK) 4651 BOX_DSTACK = pool.mallocDouble(maxDoubles) 4652 } 4653} 4654 4655//Append item to queue 4656function iterPush(ptr, 4657 axis, 4658 redStart, redEnd, 4659 blueStart, blueEnd, 4660 state, 4661 lo, hi) { 4662 4663 var iptr = IFRAME_SIZE * ptr 4664 BOX_ISTACK[iptr] = axis 4665 BOX_ISTACK[iptr+1] = redStart 4666 BOX_ISTACK[iptr+2] = redEnd 4667 BOX_ISTACK[iptr+3] = blueStart 4668 BOX_ISTACK[iptr+4] = blueEnd 4669 BOX_ISTACK[iptr+5] = state 4670 4671 var dptr = DFRAME_SIZE * ptr 4672 BOX_DSTACK[dptr] = lo 4673 BOX_DSTACK[dptr+1] = hi 4674} 4675 4676//Special case: Intersect single point with list of intervals 4677function onePointPartial( 4678 d, axis, visit, flip, 4679 redStart, redEnd, red, redIndex, 4680 blueOffset, blue, blueId) { 4681 4682 var elemSize = 2 * d 4683 var bluePtr = blueOffset * elemSize 4684 var blueX = blue[bluePtr + axis] 4685 4686red_loop: 4687 for(var i=redStart, redPtr=redStart*elemSize; i<redEnd; ++i, redPtr+=elemSize) { 4688 var r0 = red[redPtr+axis] 4689 var r1 = red[redPtr+axis+d] 4690 if(blueX < r0 || r1 < blueX) { 4691 continue 4692 } 4693 if(flip && blueX === r0) { 4694 continue 4695 } 4696 var redId = redIndex[i] 4697 for(var j=axis+1; j<d; ++j) { 4698 var r0 = red[redPtr+j] 4699 var r1 = red[redPtr+j+d] 4700 var b0 = blue[bluePtr+j] 4701 var b1 = blue[bluePtr+j+d] 4702 if(r1 < b0 || b1 < r0) { 4703 continue red_loop 4704 } 4705 } 4706 var retval 4707 if(flip) { 4708 retval = visit(blueId, redId) 4709 } else { 4710 retval = visit(redId, blueId) 4711 } 4712 if(retval !== void 0) { 4713 return retval 4714 } 4715 } 4716} 4717 4718//Special case: Intersect one point with list of intervals 4719function onePointFull( 4720 d, axis, visit, 4721 redStart, redEnd, red, redIndex, 4722 blueOffset, blue, blueId) { 4723 4724 var elemSize = 2 * d 4725 var bluePtr = blueOffset * elemSize 4726 var blueX = blue[bluePtr + axis] 4727 4728red_loop: 4729 for(var i=redStart, redPtr=redStart*elemSize; i<redEnd; ++i, redPtr+=elemSize) { 4730 var redId = redIndex[i] 4731 if(redId === blueId) { 4732 continue 4733 } 4734 var r0 = red[redPtr+axis] 4735 var r1 = red[redPtr+axis+d] 4736 if(blueX < r0 || r1 < blueX) { 4737 continue 4738 } 4739 for(var j=axis+1; j<d; ++j) { 4740 var r0 = red[redPtr+j] 4741 var r1 = red[redPtr+j+d] 4742 var b0 = blue[bluePtr+j] 4743 var b1 = blue[bluePtr+j+d] 4744 if(r1 < b0 || b1 < r0) { 4745 continue red_loop 4746 } 4747 } 4748 var retval = visit(redId, blueId) 4749 if(retval !== void 0) { 4750 return retval 4751 } 4752 } 4753} 4754 4755//The main box intersection routine 4756function boxIntersectIter( 4757 d, visit, initFull, 4758 xSize, xBoxes, xIndex, 4759 ySize, yBoxes, yIndex) { 4760 4761 //Reserve memory for stack 4762 iterInit(d, xSize + ySize) 4763 4764 var top = 0 4765 var elemSize = 2 * d 4766 var retval 4767 4768 iterPush(top++, 4769 0, 4770 0, xSize, 4771 0, ySize, 4772 initFull ? 16 : 0, 4773 -Infinity, Infinity) 4774 if(!initFull) { 4775 iterPush(top++, 4776 0, 4777 0, ySize, 4778 0, xSize, 4779 1, 4780 -Infinity, Infinity) 4781 } 4782 4783 while(top > 0) { 4784 top -= 1 4785 4786 var iptr = top * IFRAME_SIZE 4787 var axis = BOX_ISTACK[iptr] 4788 var redStart = BOX_ISTACK[iptr+1] 4789 var redEnd = BOX_ISTACK[iptr+2] 4790 var blueStart = BOX_ISTACK[iptr+3] 4791 var blueEnd = BOX_ISTACK[iptr+4] 4792 var state = BOX_ISTACK[iptr+5] 4793 4794 var dptr = top * DFRAME_SIZE 4795 var lo = BOX_DSTACK[dptr] 4796 var hi = BOX_DSTACK[dptr+1] 4797 4798 //Unpack state info 4799 var flip = (state & 1) 4800 var full = !!(state & 16) 4801 4802 //Unpack indices 4803 var red = xBoxes 4804 var redIndex = xIndex 4805 var blue = yBoxes 4806 var blueIndex = yIndex 4807 if(flip) { 4808 red = yBoxes 4809 redIndex = yIndex 4810 blue = xBoxes 4811 blueIndex = xIndex 4812 } 4813 4814 if(state & 2) { 4815 redEnd = partitionStartLessThan( 4816 d, axis, 4817 redStart, redEnd, red, redIndex, 4818 hi) 4819 if(redStart >= redEnd) { 4820 continue 4821 } 4822 } 4823 if(state & 4) { 4824 redStart = partitionEndLessThanEqual( 4825 d, axis, 4826 redStart, redEnd, red, redIndex, 4827 lo) 4828 if(redStart >= redEnd) { 4829 continue 4830 } 4831 } 4832 4833 var redCount = redEnd - redStart 4834 var blueCount = blueEnd - blueStart 4835 4836 if(full) { 4837 if(d * redCount * (redCount + blueCount) < SCAN_COMPLETE_CUTOFF) { 4838 retval = sweep.scanComplete( 4839 d, axis, visit, 4840 redStart, redEnd, red, redIndex, 4841 blueStart, blueEnd, blue, blueIndex) 4842 if(retval !== void 0) { 4843 return retval 4844 } 4845 continue 4846 } 4847 } else { 4848 if(d * Math.min(redCount, blueCount) < BRUTE_FORCE_CUTOFF) { 4849 //If input small, then use brute force 4850 retval = bruteForcePartial( 4851 d, axis, visit, flip, 4852 redStart, redEnd, red, redIndex, 4853 blueStart, blueEnd, blue, blueIndex) 4854 if(retval !== void 0) { 4855 return retval 4856 } 4857 continue 4858 } else if(d * redCount * blueCount < SCAN_CUTOFF) { 4859 //If input medium sized, then use sweep and prune 4860 retval = sweep.scanBipartite( 4861 d, axis, visit, flip, 4862 redStart, redEnd, red, redIndex, 4863 blueStart, blueEnd, blue, blueIndex) 4864 if(retval !== void 0) { 4865 return retval 4866 } 4867 continue 4868 } 4869 } 4870 4871 //First, find all red intervals whose interior contains (lo,hi)
4872 var red0 = partitionInteriorContainsInterval( 4873 d, axis, 4874 redStart, redEnd, red, redIndex, 4875 lo, hi) 4876 4877 //Lower dimensional case 4878 if(redStart < red0) { 4879 4880 if(d * (red0 - redStart) < BRUTE_FORCE_CUTOFF) { 4881 //Special case for small inputs: use brute force 4882 retval = bruteForceFull( 4883 d, axis+1, visit, 4884 redStart, red0, red, redIndex, 4885 blueStart, blueEnd, blue, blueIndex) 4886 if(retval !== void 0) { 4887 return retval 4888 } 4889 } else if(axis === d-2) { 4890 if(flip) { 4891 retval = sweep.sweepBipartite( 4892 d, visit, 4893 blueStart, blueEnd, blue, blueIndex, 4894 redStart, red0, red, redIndex) 4895 } else { 4896 retval = sweep.sweepBipartite( 4897 d, visit, 4898 redStart, red0, red, redIndex, 4899 blueStart, blueEnd, blue, blueIndex) 4900 } 4901 if(retval !== void 0) { 4902 return retval 4903 } 4904 } else { 4905 iterPush(top++, 4906 axis+1, 4907 redStart, red0, 4908 blueStart, blueEnd, 4909 flip, 4910 -Infinity, Infinity) 4911 iterPush(top++, 4912 axis+1, 4913 blueStart, blueEnd, 4914 redStart, red0, 4915 flip^1, 4916 -Infinity, Infinity) 4917 } 4918 } 4919 4920 //Divide and conquer phase 4921 if(red0 < redEnd) { 4922 4923 //Cut blue into 3 parts: 4924 // 4925 // Points < mid point 4926 // Points = mid point 4927 // Points > mid point 4928 // 4929 var blue0 = findMedian( 4930 d, axis, 4931 blueStart, blueEnd, blue, blueIndex) 4932 var mid = blue[elemSize * blue0 + axis] 4933 var blue1 = partitionStartEqual( 4934 d, axis, 4935 blue0, blueEnd, blue, blueIndex, 4936 mid) 4937 4938 //Right case 4939 if(blue1 < blueEnd) { 4940 iterPush(top++, 4941 axis, 4942 red0, redEnd, 4943 blue1, blueEnd, 4944 (flip|4) + (full ? 16 : 0), 4945 mid, hi) 4946 } 4947 4948 //Left case 4949 if(blueStart < blue0) { 4950 iterPush(top++, 4951 axis, 4952 red0, redEnd, 4953 blueStart, blue0, 4954 (flip|2) + (full ? 16 : 0), 4955 lo, mid) 4956 } 4957 4958 //Center case (the hard part) 4959 if(blue0 + 1 === blue1) { 4960 //Optimization: Range with exactly 1 point, use a brute force scan 4961 if(full) { 4962 retval = onePointFull( 4963 d, axis, visit, 4964 red0, redEnd, red, redIndex, 4965 blue0, blue, blueIndex[blue0]) 4966 } else { 4967 retval = onePointPartial( 4968 d, axis, visit, flip, 4969 red0, redEnd, red, redIndex, 4970 blue0, blue, blueIndex[blue0]) 4971 } 4972 if(retval !== void 0) { 4973 return retval 4974 } 4975 } else if(blue0 < blue1) { 4976 var red1 4977 if(full) { 4978 //If full intersection, need to handle special case 4979 red1 = partitionContainsPoint( 4980 d, axis, 4981 red0, redEnd, red, redIndex, 4982 mid) 4983 if(red0 < red1) { 4984 var redX = partitionStartEqual( 4985 d, axis, 4986 red0, red1, red, redIndex, 4987 mid) 4988 if(axis === d-2) { 4989 //Degenerate sweep intersection: 4990 // [red0, redX] with [blue0, blue1] 4991 if(red0 < redX) { 4992 retval = sweep.sweepComplete( 4993 d, visit, 4994 red0, redX, red, redIndex, 4995 blue0, blue1, blue, blueIndex) 4996 if(retval !== void 0) { 4997 return retval 4998 } 4999 } 5000 5001 //Normal sweep intersection: 5002 // [redX, red1] with [blue0, blue1] 5003 if(redX < red1) { 5004 retval = sweep.sweepBipartite( 5005 d, visit, 5006 redX, red1, red, redIndex, 5007 blue0, blue1, blue, blueIndex) 5008 if(retval !== void 0) { 5009 return retval 5010 } 5011 } 5012 } else { 5013 if(red0 < redX) { 5014 iterPush(top++, 5015 axis+1, 5016 red0, redX, 5017 blue0, blue1, 5018 16, 5019 -Infinity, Infinity) 5020 } 5021 if(redX < red1) { 5022 iterPush(top++, 5023 axis+1, 5024 redX, red1, 5025 blue0, blue1, 5026 0, 5027 -Infinity, Infinity) 5028 iterPush(top++, 5029 axis+1, 5030 blue0, blue1, 5031 redX, red1, 5032 1, 5033 -Infinity, Infinity) 5034 } 5035 } 5036 } 5037 } else { 5038 if(flip) { 5039 red1 = partitionContainsPointProper( 5040 d, axis, 5041 red0, redEnd, red, redIndex, 5042 mid) 5043 } else { 5044 red1 = partitionContainsPoint( 5045 d, axis, 5046 red0, redEnd, red, redIndex, 5047 mid) 5048 } 5049 if(red0 < red1) { 5050 if(axis === d-2) { 5051 if(flip) { 5052 retval = sweep.sweepBipartite( 5053 d, visit, 5054 blue0, blue1, blue, blueIndex, 5055 red0, red1, red, redIndex) 5056 } else { 5057 retval = sweep.sweepBipartite( 5058 d, visit, 5059 red0, red1, red, redIndex, 5060 blue0, blue1, blue, blueIndex) 5061 } 5062 } else { 5063 iterPush(top++, 5064 axis+1, 5065 red0, red1, 5066 blue0, blue1, 5067 flip, 5068 -Infinity, Infinity) 5069 iterPush(top++, 5070 axis+1, 5071 blue0, blue1, 5072 red0, red1, 5073 flip^1, 5074 -Infinity, Infinity) 5075 } 5076 } 5077 } 5078 } 5079 } 5080 } 5081} 5082},{"./brute":26,"./median":28,"./partition":29,"./sweep":31,"bit-twiddle":22,"typedarray-pool":74}],28:[function(require,module,exports){ 5083'use strict' 5084 5085module.exports = findMedian 5086 5087var genPartition = require('./partition') 5088 5089var partitionStartLessThan = genPartition('lo<p0', ['p0']) 5090 5091var PARTITION_THRESHOLD = 8 //Cut off for using insertion sort in findMedian 5092 5093//Base case for median finding: Use insertion sort 5094function insertionSort(d, axis, start, end, boxes, ids) { 5095 var elemSize = 2 * d 5096 var boxPtr = elemSize * (start+1) + axis 5097 for(var i=start+1; i<end; ++i, boxPtr+=elemSize) { 5098 var x = boxes[boxPtr] 5099 for(var j=i, ptr=elemSize*(i-1); 5100 j>start && boxes[ptr+axis] > x; 5101 --j, ptr-=elemSize) { 5102 //Swap 5103 var aPtr = ptr 5104 var bPtr = ptr+elemSize 5105 for(var k=0; k<elemSize; ++k, ++aPtr, ++bPtr) { 5106 var y = boxes[aPtr] 5107 boxes[aPtr] = boxes[bPtr] 5108 boxes[bPtr] = y 5109 } 5110 var tmp = ids[j] 5111 ids[j] = ids[j-1] 5112 ids[j-1] = tmp 5113 } 5114 } 5115} 5116 5117//Find median using quick select algorithm 5118// takes O(n) time with high probability 5119function findMedian(d, axis, start, end, boxes, ids) { 5120 if(end <= start+1) { 5121 return start 5122 } 5123 5124 var lo = start 5125 var hi = end 5126 var mid = ((end + start) >>> 1) 5127 var elemSize = 2*d 5128 var pivot = mid 5129 var value = boxes[elemSize*mid+axis] 5130 5131 while(lo < hi) { 5132 if(hi - lo < PARTITION_THRESHOLD) { 5133 insertionSort(d, axis, lo, hi, boxes, ids) 5134 value = boxes[elemSize*mid+axis] 5135 break 5136 } 5137 5138 //Select pivot using median-of-3 5139 var count = hi - lo 5140 var pivot0 = (Math.random()*count+lo)|0 5141 var value0 = boxes[elemSize*pivot0 + axis] 5142 var pivot1 = (Math.random()*count+lo)|0 5143 var value1 = boxes[elemSize*pivot1 + axis] 5144 var pivot2 = (Math.random()*count+lo)|0 5145 var value2 = boxes[elemSize*pivot2 + axis] 5146 if(value0 <= value1) { 5147 if(value2 >= value1) { 5148 pivot = pivot1 5149 value = value1 5150 } else if(value0 >= value2) { 5151 pivot = pivot0 5152 value = value0 5153 } else { 5154 pivot = pivot2 5155 value = value2 5156 } 5157 } else { 5158 if(value1 >= value2) { 5159 pivot = pivot1 5160 value = value1 5161 } else if(value2 >= value0) { 5162 pivot = pivot0 5163 value = value0 5164 } else { 5165 pivot = pivot2 5166 value = value2 5167 } 5168 } 5169 5170 //Swap pivot to end of array 5171 var aPtr = elemSize * (hi-1) 5172 var bPtr = elemSize * pivot 5173 for(var i=0; i<elemSize; ++i, ++aPtr, ++bPtr) { 5174 var x = boxes[aPtr] 5175 boxes[aPtr] = boxes[bPtr] 5176 boxes[bPtr] = x 5177 } 5178 var y = ids[hi-1] 5179 ids[hi-1] = ids[pivot] 5180 ids[pivot] = y 5181 5182 //Partition using pivot 5183 pivot = partitionStartLessThan( 5184 d, axis, 5185 lo, hi-1, boxes, ids, 5186 value) 5187 5188 //Swap pivot back 5189 var aPtr = elemSize * (hi-1) 5190 var bPtr = elemSize * pivot 5191 for(var i=0; i<elemSize; ++i, ++aPtr, ++bPtr) { 5192 var x = boxes[aPtr] 5193 boxes[aPtr] = boxes[bPtr] 5194 boxes[bPtr] = x 5195 } 5196 var y = ids[hi-1] 5197 ids[hi-1] = ids[pivot] 5198 ids[pivot] = y 5199 5200 //Swap pivot to last pivot 5201 if(mid < pivot) { 5202 hi = pivot-1 5203 while(lo < hi && 5204 boxes[elemSize*(hi-1)+axis] === value) { 5205 hi -= 1 5206 } 5207 hi += 1 5208 } else if(pivot < mid) { 5209 lo = pivot + 1 5210 while(lo < hi && 5211 boxes[elemSize*lo+axis] === value) { 5212 lo += 1 5213 } 5214 } else { 5215 break 5216 } 5217 } 5218 5219 //Make sure pivot is at start 5220 return partitionStartLessThan( 5221 d, axis, 5222 start, mid, boxes, ids, 5223 boxes[elemSize*mid+axis]) 5224} 5225},{"./partition":29}],29:[function(require,module,exports){ 5226'use strict' 5227 5228module.exports = genPartition 5229 5230var code = 'for(var j=2*a,k=j*c,l=k,m=c,n=b,o=a+b,p=c;d>p;++p,k+=j){var _;if($)if(m===p)m+=1,l+=j;else{for(var s=0;j>s;++s){var t=e[k+s];e[k+s]=e[l],e[l++]=t}var u=f[p];f[p]=f[m],f[m++]=u}}return m' 5231
5232function genPartition(predicate, args) { 5233 var fargs ='abcdef'.split('').concat(args) 5234 var reads = [] 5235 if(predicate.indexOf('lo') >= 0) { 5236 reads.push('lo=e[k+n]') 5237 } 5238 if(predicate.indexOf('hi') >= 0) { 5239 reads.push('hi=e[k+o]') 5240 } 5241 fargs.push( 5242 code.replace('_', reads.join()) 5243 .replace('$', predicate)) 5244 return Function.apply(void 0, fargs) 5245} 5246},{}],30:[function(require,module,exports){ 5247'use strict'; 5248 5249//This code is extracted from ndarray-sort 5250//It is inlined here as a temporary workaround 5251 5252module.exports = wrapper; 5253 5254var INSERT_SORT_CUTOFF = 32 5255 5256function wrapper(data, n0) { 5257 if (n0 <= 4*INSERT_SORT_CUTOFF) { 5258 insertionSort(0, n0 - 1, data); 5259 } else { 5260 quickSort(0, n0 - 1, data); 5261 } 5262} 5263 5264function insertionSort(left, right, data) { 5265 var ptr = 2*(left+1) 5266 for(var i=left+1; i<=right; ++i) { 5267 var a = data[ptr++] 5268 var b = data[ptr++] 5269 var j = i 5270 var jptr = ptr-2 5271 while(j-- > left) { 5272 var x = data[jptr-2] 5273 var y = data[jptr-1] 5274 if(x < a) { 5275 break 5276 } else if(x === a && y < b) { 5277 break 5278 } 5279 data[jptr] = x 5280 data[jptr+1] = y 5281 jptr -= 2 5282 } 5283 data[jptr] = a 5284 data[jptr+1] = b 5285 } 5286} 5287 5288function swap(i, j, data) { 5289 i *= 2 5290 j *= 2 5291 var x = data[i] 5292 var y = data[i+1] 5293 data[i] = data[j] 5294 data[i+1] = data[j+1] 5295 data[j] = x 5296 data[j+1] = y 5297} 5298 5299function move(i, j, data) { 5300 i *= 2 5301 j *= 2 5302 data[i] = data[j] 5303 data[i+1] = data[j+1] 5304} 5305 5306function rotate(i, j, k, data) { 5307 i *= 2 5308 j *= 2 5309 k *= 2 5310 var x = data[i] 5311 var y = data[i+1] 5312 data[i] = data[j] 5313 data[i+1] = data[j+1] 5314 data[j] = data[k] 5315 data[j+1] = data[k+1] 5316 data[k] = x 5317 data[k+1] = y 5318} 5319 5320function shufflePivot(i, j, px, py, data) { 5321 i *= 2 5322 j *= 2 5323 data[i] = data[j] 5324 data[j] = px 5325 data[i+1] = data[j+1] 5326 data[j+1] = py 5327} 5328 5329function compare(i, j, data) { 5330 i *= 2 5331 j *= 2 5332 var x = data[i], 5333 y = data[j] 5334 if(x < y) { 5335 return false 5336 } else if(x === y) { 5337 return data[i+1] > data[j+1] 5338 } 5339 return true 5340} 5341 5342function comparePivot(i, y, b, data) { 5343 i *= 2 5344 var x = data[i] 5345 if(x < y) { 5346 return true 5347 } else if(x === y) { 5348 return data[i+1] < b 5349 } 5350 return false 5351} 5352 5353function quickSort(left, right, data) { 5354 var sixth = (right - left + 1) / 6 | 0, 5355 index1 = left + sixth, 5356 index5 = right - sixth, 5357 index3 = left + right >> 1, 5358 index2 = index3 - sixth, 5359 index4 = index3 + sixth, 5360 el1 = index1, 5361 el2 = index2, 5362 el3 = index3, 5363 el4 = index4, 5364 el5 = index5, 5365 less = left + 1, 5366 great = right - 1, 5367 tmp = 0 5368 if(compare(el1, el2, data)) { 5369 tmp = el1 5370 el1 = el2 5371 el2 = tmp 5372 } 5373 if(compare(el4, el5, data)) { 5374 tmp = el4 5375 el4 = el5 5376 el5 = tmp 5377 } 5378 if(compare(el1, el3, data)) { 5379 tmp = el1 5380 el1 = el3 5381 el3 = tmp 5382 } 5383 if(compare(el2, el3, data)) { 5384 tmp = el2 5385 el2 = el3 5386 el3 = tmp 5387 } 5388 if(compare(el1, el4, data)) { 5389 tmp = el1 5390 el1 = el4 5391 el4 = tmp 5392 } 5393 if(compare(el3, el4, data)) { 5394 tmp = el3 5395 el3 = el4 5396 el4 = tmp 5397 } 5398 if(compare(el2, el5, data)) { 5399 tmp = el2 5400 el2 = el5 5401 el5 = tmp 5402 } 5403 if(compare(el2, el3, data)) { 5404 tmp = el2 5405 el2 = el3 5406 el3 = tmp 5407 } 5408 if(compare(el4, el5, data)) { 5409 tmp = el4 5410 el4 = el5 5411 el5 = tmp 5412 } 5413 5414 var pivot1X = data[2*el2] 5415 var pivot1Y = data[2*el2+1] 5416 var pivot2X = data[2*el4] 5417 var pivot2Y = data[2*el4+1] 5418 5419 var ptr0 = 2 * el1; 5420 var ptr2 = 2 * el3; 5421 var ptr4 = 2 * el5; 5422 var ptr5 = 2 * index1; 5423 var ptr6 = 2 * index3; 5424 var ptr7 = 2 * index5; 5425 for (var i1 = 0; i1 < 2; ++i1) { 5426 var x = data[ptr0+i1]; 5427 var y = data[ptr2+i1]; 5428 var z = data[ptr4+i1]; 5429 data[ptr5+i1] = x; 5430 data[ptr6+i1] = y; 5431 data[ptr7+i1] = z; 5432 } 5433 5434 move(index2, left, data) 5435 move(index4, right, data) 5436 for (var k = less; k <= great; ++k) { 5437 if (comparePivot(k, pivot1X, pivot1Y, data)) { 5438 if (k !== less) { 5439 swap(k, less, data) 5440 } 5441 ++less; 5442 } else { 5443 if (!comparePivot(k, pivot2X, pivot2Y, data)) { 5444 while (true) { 5445 if (!comparePivot(great, pivot2X, pivot2Y, data)) { 5446 if (--great < k) { 5447 break; 5448 } 5449 continue; 5450 } else { 5451 if (comparePivot(great, pivot1X, pivot1Y, data)) { 5452 rotate(k, less, great, data) 5453 ++less; 5454 --great; 5455 } else { 5456 swap(k, great, data) 5457 --great; 5458 } 5459 break; 5460 } 5461 } 5462 } 5463 } 5464 } 5465 shufflePivot(left, less-1, pivot1X, pivot1Y, data) 5466 shufflePivot(right, great+1, pivot2X, pivot2Y, data) 5467 if (less - 2 - left <= INSERT_SORT_CUTOFF) { 5468 insertionSort(left, less - 2, data); 5469 } else { 5470 quickSort(left, less - 2, data); 5471 } 5472 if (right - (great + 2) <= INSERT_SORT_CUTOFF) { 5473 insertionSort(great + 2, right, data); 5474 } else { 5475 quickSort(great + 2, right, data); 5476 } 5477 if (great - less <= INSERT_SORT_CUTOFF) { 5478 insertionSort(less, great, data); 5479 } else { 5480 quickSort(less, great, data); 5481 } 5482} 5483},{}],31:[function(require,module,exports){ 5484'use strict' 5485 5486module.exports = { 5487 init: sqInit, 5488 sweepBipartite: sweepBipartite, 5489 sweepComplete: sweepComplete, 5490 scanBipartite: scanBipartite, 5491 scanComplete: scanComplete 5492} 5493 5494var pool = require('typedarray-pool') 5495var bits = require('bit-twiddle') 5496var isort = require('./sort') 5497 5498//Flag for blue 5499var BLUE_FLAG = (1<<28) 5500 5501//1D sweep event queue stuff (use pool to save space) 5502var INIT_CAPACITY = 1024 5503var RED_SWEEP_QUEUE = pool.mallocInt32(INIT_CAPACITY) 5504var RED_SWEEP_INDEX = pool.mallocInt32(INIT_CAPACITY) 5505var BLUE_SWEEP_QUEUE = pool.mallocInt32(INIT_CAPACITY) 5506var BLUE_SWEEP_INDEX = pool.mallocInt32(INIT_CAPACITY) 5507var COMMON_SWEEP_QUEUE = pool.mallocInt32(INIT_CAPACITY) 5508var COMMON_SWEEP_INDEX = pool.mallocInt32(INIT_CAPACITY) 5509var SWEEP_EVENTS = pool.mallocDouble(INIT_CAPACITY * 8) 5510 5511//Reserves memory for the 1D sweep data structures 5512function sqInit(count) { 5513 var rcount = bits.nextPow2(count)
5514 if(RED_SWEEP_QUEUE.length < rcount) { 5515 pool.free(RED_SWEEP_QUEUE) 5516 RED_SWEEP_QUEUE = pool.mallocInt32(rcount) 5517 } 5518 if(RED_SWEEP_INDEX.length < rcount) { 5519 pool.free(RED_SWEEP_INDEX) 5520 RED_SWEEP_INDEX = pool.mallocInt32(rcount) 5521 } 5522 if(BLUE_SWEEP_QUEUE.length < rcount) { 5523 pool.free(BLUE_SWEEP_QUEUE) 5524 BLUE_SWEEP_QUEUE = pool.mallocInt32(rcount) 5525 } 5526 if(BLUE_SWEEP_INDEX.length < rcount) { 5527 pool.free(BLUE_SWEEP_INDEX) 5528 BLUE_SWEEP_INDEX = pool.mallocInt32(rcount) 5529 } 5530 if(COMMON_SWEEP_QUEUE.length < rcount) { 5531 pool.free(COMMON_SWEEP_QUEUE) 5532 COMMON_SWEEP_QUEUE = pool.mallocInt32(rcount) 5533 } 5534 if(COMMON_SWEEP_INDEX.length < rcount) { 5535 pool.free(COMMON_SWEEP_INDEX) 5536 COMMON_SWEEP_INDEX = pool.mallocInt32(rcount) 5537 } 5538 var eventLength = 8 * rcount 5539 if(SWEEP_EVENTS.length < eventLength) { 5540 pool.free(SWEEP_EVENTS) 5541 SWEEP_EVENTS = pool.mallocDouble(eventLength) 5542 } 5543} 5544 5545//Remove an item from the active queue in O(1) 5546function sqPop(queue, index, count, item) { 5547 var idx = index[item] 5548 var top = queue[count-1] 5549 queue[idx] = top 5550 index[top] = idx 5551} 5552 5553//Insert an item into the active queue in O(1) 5554function sqPush(queue, index, count, item) { 5555 queue[count] = item 5556 index[item] = count 5557} 5558 5559//Recursion base case: use 1D sweep algorithm 5560function sweepBipartite( 5561 d, visit, 5562 redStart, redEnd, red, redIndex, 5563 blueStart, blueEnd, blue, blueIndex) { 5564 5565 //store events as pairs [coordinate, idx] 5566 // 5567 // red create: -(idx+1) 5568 // red destroy: idx 5569 // blue create: -(idx+BLUE_FLAG) 5570 // blue destroy: idx+BLUE_FLAG 5571 // 5572 var ptr = 0 5573 var elemSize = 2*d 5574 var istart = d-1 5575 var iend = elemSize-1 5576 5577 for(var i=redStart; i<redEnd; ++i) { 5578 var idx = redIndex[i] 5579 var redOffset = elemSize*i 5580 SWEEP_EVENTS[ptr++] = red[redOffset+istart] 5581 SWEEP_EVENTS[ptr++] = -(idx+1) 5582 SWEEP_EVENTS[ptr++] = red[redOffset+iend] 5583 SWEEP_EVENTS[ptr++] = idx 5584 } 5585 5586 for(var i=blueStart; i<blueEnd; ++i) { 5587 var idx = blueIndex[i]+BLUE_FLAG 5588 var blueOffset = elemSize*i 5589 SWEEP_EVENTS[ptr++] = blue[blueOffset+istart] 5590 SWEEP_EVENTS[ptr++] = -idx 5591 SWEEP_EVENTS[ptr++] = blue[blueOffset+iend] 5592 SWEEP_EVENTS[ptr++] = idx 5593 } 5594 5595 //process events from left->right 5596 var n = ptr >>> 1 5597 isort(SWEEP_EVENTS, n) 5598 5599 var redActive = 0 5600 var blueActive = 0 5601 for(var i=0; i<n; ++i) { 5602 var e = SWEEP_EVENTS[2*i+1]|0 5603 if(e >= BLUE_FLAG) { 5604 //blue destroy event 5605 e = (e-BLUE_FLAG)|0 5606 sqPop(BLUE_SWEEP_QUEUE, BLUE_SWEEP_INDEX, blueActive--, e) 5607 } else if(e >= 0) { 5608 //red destroy event 5609 sqPop(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive--, e) 5610 } else if(e <= -BLUE_FLAG) { 5611 //blue create event 5612 e = (-e-BLUE_FLAG)|0 5613 for(var j=0; j<redActive; ++j) { 5614 var retval = visit(RED_SWEEP_QUEUE[j], e) 5615 if(retval !== void 0) { 5616 return retval 5617 } 5618 } 5619 sqPush(BLUE_SWEEP_QUEUE, BLUE_SWEEP_INDEX, blueActive++, e) 5620 } else { 5621 //red create event 5622 e = (-e-1)|0 5623 for(var j=0; j<blueActive; ++j) { 5624 var retval = visit(e, BLUE_SWEEP_QUEUE[j]) 5625 if(retval !== void 0) { 5626 return retval 5627 } 5628 } 5629 sqPush(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive++, e) 5630 } 5631 } 5632} 5633 5634//Complete sweep 5635function sweepComplete(d, visit, 5636 redStart, redEnd, red, redIndex, 5637 blueStart, blueEnd, blue, blueIndex) { 5638 5639 var ptr = 0 5640 var elemSize = 2*d 5641 var istart = d-1 5642 var iend = elemSize-1 5643 5644 for(var i=redStart; i<redEnd; ++i) { 5645 var idx = (redIndex[i]+1)<<1 5646 var redOffset = elemSize*i 5647 SWEEP_EVENTS[ptr++] = red[redOffset+istart] 5648 SWEEP_EVENTS[ptr++] = -idx 5649 SWEEP_EVENTS[ptr++] = red[redOffset+iend] 5650 SWEEP_EVENTS[ptr++] = idx 5651 } 5652 5653 for(var i=blueStart; i<blueEnd; ++i) { 5654 var idx = (blueIndex[i]+1)<<1 5655 var blueOffset = elemSize*i 5656 SWEEP_EVENTS[ptr++] = blue[blueOffset+istart] 5657 SWEEP_EVENTS[ptr++] = (-idx)|1 5658 SWEEP_EVENTS[ptr++] = blue[blueOffset+iend] 5659 SWEEP_EVENTS[ptr++] = idx|1 5660 } 5661 5662 //process events from left->right 5663 var n = ptr >>> 1 5664 isort(SWEEP_EVENTS, n) 5665 5666 var redActive = 0 5667 var blueActive = 0 5668 var commonActive = 0 5669 for(var i=0; i<n; ++i) { 5670 var e = SWEEP_EVENTS[2*i+1]|0 5671 var color = e&1 5672 if(i < n-1 && (e>>1) === (SWEEP_EVENTS[2*i+3]>>1)) { 5673 color = 2 5674 i += 1 5675 } 5676
5677 if(e < 0) { 5678 //Create event 5679 var id = -(e>>1) - 1 5680 5681 //Intersect with common 5682 for(var j=0; j<commonActive; ++j) { 5683 var retval = visit(COMMON_SWEEP_QUEUE[j], id) 5684 if(retval !== void 0) { 5685 return retval 5686 } 5687 } 5688 5689 if(color !== 0) { 5690 //Intersect with red 5691 for(var j=0; j<redActive; ++j) { 5692 var retval = visit(RED_SWEEP_QUEUE[j], id) 5693 if(retval !== void 0) { 5694 return retval 5695 } 5696 } 5697 } 5698 5699 if(color !== 1) { 5700 //Intersect with blue 5701 for(var j=0; j<blueActive; ++j) { 5702 var retval = visit(BLUE_SWEEP_QUEUE[j], id) 5703 if(retval !== void 0) { 5704 return retval 5705 } 5706 } 5707 } 5708 5709 if(color === 0) { 5710 //Red 5711 sqPush(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive++, id) 5712 } else if(color === 1) { 5713 //Blue 5714 sqPush(BLUE_SWEEP_QUEUE, BLUE_SWEEP_INDEX, blueActive++, id) 5715 } else if(color === 2) { 5716 //Both 5717 sqPush(COMMON_SWEEP_QUEUE, COMMON_SWEEP_INDEX, commonActive++, id) 5718 } 5719 } else { 5720 //Destroy event 5721 var id = (e>>1) - 1 5722 if(color === 0) { 5723 //Red 5724 sqPop(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive--, id) 5725 } else if(color === 1) { 5726 //Blue 5727 sqPop(BLUE_SWEEP_QUEUE, BLUE_SWEEP_INDEX, blueActive--, id) 5728 } else if(color === 2) { 5729 //Both 5730 sqPop(COMMON_SWEEP_QUEUE, COMMON_SWEEP_INDEX, commonActive--, id) 5731 } 5732 } 5733 } 5734} 5735 5736//Sweep and prune/scanline algorithm: 5737// Scan along axis, detect intersections 5738// Brute force all boxes along axis 5739function scanBipartite( 5740 d, axis, visit, flip, 5741 redStart, redEnd, red, redIndex, 5742 blueStart, blueEnd, blue, blueIndex) { 5743 5744 var ptr = 0 5745 var elemSize = 2*d 5746 var istart = axis 5747 var iend = axis+d 5748 5749 var redShift = 1 5750 var blueShift = 1 5751 if(flip) { 5752 blueShift = BLUE_FLAG 5753 } else { 5754 redShift = BLUE_FLAG 5755 } 5756 5757 for(var i=redStart; i<redEnd; ++i) { 5758 var idx = i + redShift 5759 var redOffset = elemSize*i 5760 SWEEP_EVENTS[ptr++] = red[redOffset+istart] 5761 SWEEP_EVENTS[ptr++] = -idx 5762 SWEEP_EVENTS[ptr++] = red[redOffset+iend] 5763 SWEEP_EVENTS[ptr++] = idx 5764 } 5765 for(var i=blueStart; i<blueEnd; ++i) { 5766 var idx = i + blueShift 5767 var blueOffset = elemSize*i 5768 SWEEP_EVENTS[ptr++] = blue[blueOffset+istart] 5769 SWEEP_EVENTS[ptr++] = -idx 5770 } 5771 5772 //process events from left->right 5773 var n = ptr >>> 1 5774 isort(SWEEP_EVENTS, n) 5775 5776 var redActive = 0 5777 for(var i=0; i<n; ++i) { 5778 var e = SWEEP_EVENTS[2*i+1]|0 5779 if(e < 0) { 5780 var idx = -e 5781 var isRed = false 5782 if(idx >= BLUE_FLAG) { 5783 isRed = !flip 5784 idx -= BLUE_FLAG 5785 } else { 5786 isRed = !!flip 5787 idx -= 1 5788 } 5789 if(isRed) { 5790 sqPush(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive++, idx) 5791 } else { 5792 var blueId = blueIndex[idx] 5793 var bluePtr = elemSize * idx 5794 5795 var b0 = blue[bluePtr+axis+1] 5796 var b1 = blue[bluePtr+axis+1+d] 5797 5798red_loop: 5799 for(var j=0; j<redActive; ++j) { 5800 var oidx = RED_SWEEP_QUEUE[j] 5801 var redPtr = elemSize * oidx 5802 5803 if(b1 < red[redPtr+axis+1] || 5804 red[redPtr+axis+1+d] < b0) { 5805 continue 5806 } 5807 5808 for(var k=axis+2; k<d; ++k) { 5809 if(blue[bluePtr + k + d] < red[redPtr + k] || 5810 red[redPtr + k + d] < blue[bluePtr + k]) { 5811 continue red_loop 5812 } 5813 } 5814 5815 var redId = redIndex[oidx] 5816 var retval 5817 if(flip) { 5818 retval = visit(blueId, redId) 5819 } else { 5820 retval = visit(redId, blueId) 5821 } 5822 if(retval !== void 0) { 5823 return retval 5824 } 5825 } 5826 } 5827 } else { 5828 sqPop(RED_SWEEP_QUEUE, RED_SWEEP_INDEX, redActive--, e - redShift) 5829 } 5830 } 5831} 5832 5833function scanComplete( 5834 d, axis, visit, 5835 redStart, redEnd, red, redIndex, 5836 blueStart, blueEnd, blue, blueIndex) { 5837 5838 var ptr = 0 5839 var elemSize = 2*d 5840 var istart = axis 5841 var iend = axis+d 5842 5843 for(var i=redStart; i<redEnd; ++i) { 5844 var idx = i + BLUE_FLAG 5845 var redOffset = elemSize*i 5846 SWEEP_EVENTS[ptr++] = red[redOffset+istart] 5847 SWEEP_EVENTS[ptr++] = -idx 5848 SWEEP_EVENTS[ptr++] = red[redOffset+iend] 5849 SWEEP_EVENTS[ptr++] = idx 5850 } 5851 for(var i=blueStart; i<blueEnd; ++i) { 5852 var idx = i + 1 5853 var blueOffset = elemSize*i 5854 SWEEP_EVENTS[ptr++] = blue[blueOffset+istart] 5855 SWEEP_EVENTS[ptr++] = -idx 5856 } 5857 5858 //process events from left->right 5859 var n = ptr >>> 1 5860 isort(SWEEP_EVENTS, n) 5861 5862 var redActive = 0 5863 for(var i=0; i<n; ++i) { 5864 var e = SWEEP_EVENTS[2*i+1]|0
5865 if(e < 0) { 5866 var idx = -e 5867 if(idx >= BLUE_FLAG) { 5868 RED_SWEEP_QUEUE[redActive++] = idx - BLUE_FLAG 5869 } else { 5870 idx -= 1 5871 var blueId = blueIndex[idx] 5872 var bluePtr = elemSize * idx 5873 5874 var b0 = blue[bluePtr+axis+1] 5875 var b1 = blue[bluePtr+axis+1+d] 5876 5877red_loop: 5878 for(var j=0; j<redActive; ++j) { 5879 var oidx = RED_SWEEP_QUEUE[j] 5880 var redId = redIndex[oidx] 5881 5882 if(redId === blueId) { 5883 break 5884 } 5885 5886 var redPtr = elemSize * oidx 5887 if(b1 < red[redPtr+axis+1] || 5888 red[redPtr+axis+1+d] < b0) { 5889 continue 5890 } 5891 for(var k=axis+2; k<d; ++k) { 5892 if(blue[bluePtr + k + d] < red[redPtr + k] || 5893 red[redPtr + k + d] < blue[bluePtr + k]) { 5894 continue red_loop 5895 } 5896 } 5897 5898 var retval = visit(redId, blueId) 5899 if(retval !== void 0) { 5900 return retval 5901 } 5902 } 5903 } 5904 } else { 5905 var idx = e - BLUE_FLAG 5906 for(var j=redActive-1; j>=0; --j) { 5907 if(RED_SWEEP_QUEUE[j] === idx) { 5908 for(var k=j+1; k<redActive; ++k) { 5909 RED_SWEEP_QUEUE[k-1] = RED_SWEEP_QUEUE[k] 5910 } 5911 break 5912 } 5913 } 5914 --redActive 5915 } 5916 } 5917} 5918},{"./sort":30,"bit-twiddle":22,"typedarray-pool":74}],32:[function(require,module,exports){ 5919'use strict' 5920 5921var monotoneTriangulate = require('./lib/monotone') 5922var makeIndex = require('./lib/triangulation') 5923var delaunayFlip = require('./lib/delaunay') 5924var filterTriangulation = require('./lib/filter') 5925 5926module.exports = cdt2d 5927 5928function canonicalizeEdge(e) { 5929 return [Math.min(e[0], e[1]), Math.max(e[0], e[1])] 5930} 5931 5932function compareEdge(a, b) { 5933 return a[0]-b[0] || a[1]-b[1] 5934} 5935 5936function canonicalizeEdges(edges) { 5937 return edges.map(canonicalizeEdge).sort(compareEdge) 5938} 5939 5940function getDefault(options, property, dflt) { 5941 if(property in options) { 5942 return options[property] 5943 } 5944 return dflt 5945} 5946 5947function cdt2d(points, edges, options) { 5948 5949 if(!Array.isArray(edges)) { 5950 options = edges || {} 5951 edges = [] 5952 } else { 5953 options = options || {} 5954 edges = edges || [] 5955 } 5956 5957 //Parse out options 5958 var delaunay = !!getDefault(options, 'delaunay', true) 5959 var interior = !!getDefault(options, 'interior', true) 5960 var exterior = !!getDefault(options, 'exterior', true) 5961 var infinity = !!getDefault(options, 'infinity', false) 5962 5963 //Handle trivial case 5964 if((!interior && !exterior) || points.length === 0) { 5965 return [] 5966 } 5967 5968 //Construct initial triangulation 5969 var cells = monotoneTriangulate(points, edges) 5970 5971 //If delaunay refinement needed, then improve quality by edge flipping 5972 if(delaunay || interior !== exterior || infinity) { 5973 5974 //Index all of the cells to support fast neighborhood queries 5975 var triangulation = makeIndex(points.length, canonicalizeEdges(edges)) 5976 for(var i=0; i<cells.length; ++i) { 5977 var f = cells[i] 5978 triangulation.addTriangle(f[0], f[1], f[2]) 5979 } 5980 5981 //Run edge flipping 5982 if(delaunay) { 5983 delaunayFlip(points, triangulation) 5984 } 5985 5986 //Filter points 5987 if(!exterior) { 5988 return filterTriangulation(triangulation, -1) 5989 } else if(!interior) { 5990 return filterTriangulation(triangulation, 1, infinity) 5991 } else if(infinity) { 5992 return filterTriangulation(triangulation, 0, infinity) 5993 } else { 5994 return triangulation.cells() 5995 } 5996 5997 } else { 5998 return cells 5999 } 6000} 6001 6002},{"./lib/delaunay":33,"./lib/filter":34,"./lib/monotone":35,"./lib/triangulation":36}],33:[function(require,module,exports){ 6003'use strict' 6004 6005var inCircle = require('robust-in-sphere')[4] 6006var bsearch = require('binary-search-bounds') 6007 6008module.exports = delaunayRefine 6009 6010function testFlip(points, triangulation, stack, a, b, x) { 6011 var y = triangulation.opposite(a, b) 6012 6013 //Test boundary edge 6014 if(y < 0) { 6015 return 6016 } 6017 6018 //Swap edge if order flipped 6019 if(b < a) { 6020 var tmp = a 6021 a = b 6022 b = tmp 6023 tmp = x 6024 x = y 6025 y = tmp 6026 } 6027 6028 //Test if edge is constrained 6029 if(triangulation.isConstraint(a, b)) { 6030 return 6031 } 6032 6033 //Test if edge is delaunay 6034 if(inCircle(points[a], points[b], points[x], points[y]) < 0) { 6035 stack.push(a, b) 6036 } 6037} 6038 6039//Assume edges are sorted lexicographically 6040function delaunayRefine(points, triangulation) { 6041 var stack = [] 6042 6043 var numPoints = points.length 6044 var stars = triangulation.stars 6045 for(var a=0; a<numPoints; ++a) { 6046 var star = stars[a] 6047 for(var j=1; j<star.length; j+=2) { 6048 var b = star[j] 6049 6050 //If order is not consistent, then skip edge 6051 if(b < a) { 6052 continue 6053 } 6054 6055 //Check if edge is constrained 6056 if(triangulation.isConstraint(a, b)) { 6057 continue 6058 } 6059 6060 //Find opposite edge 6061 var x = star[j-1], y = -1 6062 for(var k=1; k<star.length; k+=2) { 6063 if(star[k-1] === b) { 6064 y = star[k] 6065 break 6066 } 6067 } 6068 6069 //If this is a boundary edge, don't flip it 6070 if(y < 0) { 6071 continue 6072 } 6073 6074 //If edge is in circle, flip it 6075 if(inCircle(points[a], points[b], points[x], points[y]) < 0) { 6076 stack.push(a, b) 6077 } 6078 } 6079 } 6080 6081 while(stack.length > 0) { 6082 var b = stack.pop() 6083 var a = stack.pop() 6084 6085 //Find opposite pairs 6086 var x = -1, y = -1 6087 var star = stars[a] 6088 for(var i=1; i<star.length; i+=2) { 6089 var s = star[i-1] 6090 var t = star[i] 6091 if(s === b) { 6092 y = t 6093 } else if(t === b) { 6094 x = s 6095 } 6096 } 6097 6098 //If x/y are both valid then skip edge 6099 if(x < 0 || y < 0) { 6100 continue 6101 } 6102 6103 //If edge is now delaunay, then don't flip it 6104 if(inCircle(points[a], points[b], points[x], points[y]) >= 0) { 6105 continue 6106 } 6107 6108 //Flip the edge 6109 triangulation.flip(a, b) 6110 6111 //Test flipping neighboring edges 6112 testFlip(points, triangulation, stack, x, a, y) 6113 testFlip(points, triangulation, stack, a, y, x) 6114 testFlip(points, triangulation, stack, y, b, x) 6115 testFlip(points, triangulation, stack, b, x, y) 6116 } 6117} 6118 6119}
6119,{"binary-search-bounds":21,"robust-in-sphere":59}],34:[function(require,module,exports){ 6120'use strict' 6121 6122var bsearch = require('binary-search-bounds') 6123 6124module.exports = classifyFaces 6125 6126function FaceIndex(cells, neighbor, constraint, flags, active, next, boundary) { 6127 this.cells = cells 6128 this.neighbor = neighbor 6129 this.flags = flags 6130 this.constraint = constraint 6131 this.active = active 6132 this.next = next 6133 this.boundary = boundary 6134} 6135 6136var proto = FaceIndex.prototype 6137 6138function compareCell(a, b) { 6139 return a[0] - b[0] || 6140 a[1] - b[1] || 6141 a[2] - b[2] 6142} 6143 6144proto.locate = (function() { 6145 var key = [0,0,0] 6146 return function(a, b, c) { 6147 var x = a, y = b, z = c 6148 if(b < c) { 6149 if(b < a) { 6150 x = b 6151 y = c 6152 z = a 6153 } 6154 } else if(c < a) { 6155 x = c 6156 y = a 6157 z = b 6158 } 6159 if(x < 0) { 6160 return -1 6161 } 6162 key[0] = x 6163 key[1] = y 6164 key[2] = z 6165 return bsearch.eq(this.cells, key, compareCell) 6166 } 6167})() 6168 6169function indexCells(triangulation, infinity) { 6170 //First get cells and canonicalize 6171 var cells = triangulation.cells() 6172 var nc = cells.length 6173 for(var i=0; i<nc; ++i) { 6174 var c = cells[i] 6175 var x = c[0], y = c[1], z = c[2] 6176 if(y < z) { 6177 if(y < x) { 6178 c[0] = y 6179 c[1] = z 6180 c[2] = x 6181 } 6182 } else if(z < x) { 6183 c[0] = z 6184 c[1] = x 6185 c[2] = y 6186 } 6187 } 6188 cells.sort(compareCell) 6189 6190 //Initialize flag array 6191 var flags = new Array(nc) 6192 for(var i=0; i<flags.length; ++i) { 6193 flags[i] = 0 6194 } 6195 6196 //Build neighbor index, initialize queues 6197 var active = [] 6198 var next = [] 6199 var neighbor = new Array(3*nc) 6200 var constraint = new Array(3*nc) 6201 var boundary = null 6202 if(infinity) { 6203 boundary = [] 6204 } 6205 var index = new FaceIndex( 6206 cells, 6207 neighbor, 6208 constraint, 6209 flags, 6210 active, 6211 next, 6212 boundary) 6213 for(var i=0; i<nc; ++i) { 6214 var c = cells[i] 6215 for(var j=0; j<3; ++j) { 6216 var x = c[j], y = c[(j+1)%3] 6217 var a = neighbor[3*i+j] = index.locate(y, x, triangulation.opposite(y, x)) 6218 var b = constraint[3*i+j] = triangulation.isConstraint(x, y) 6219 if(a < 0) { 6220 if(b) { 6221 next.push(i) 6222 } else { 6223 active.push(i) 6224 flags[i] = 1 6225 } 6226 if(infinity) { 6227 boundary.push([y, x, -1]) 6228 } 6229 } 6230 } 6231 } 6232 return index 6233} 6234 6235function filterCells(cells, flags, target) { 6236 var ptr = 0 6237 for(var i=0; i<cells.length; ++i) { 6238 if(flags[i] === target) { 6239 cells[ptr++] = cells[i] 6240 } 6241 } 6242 cells.length = ptr 6243 return cells 6244} 6245 6246function classifyFaces(triangulation, target, infinity) { 6247 var index = indexCells(triangulation, infinity) 6248 6249 if(target === 0) { 6250 if(infinity) { 6251 return index.cells.concat(index.boundary) 6252 } else { 6253 return index.cells 6254 } 6255 } 6256 6257 var side = 1 6258 var active = index.active 6259 var next = index.next 6260 var flags = index.flags 6261 var cells = index.cells 6262 var constraint = index.constraint 6263 var neighbor = index.neighbor 6264 6265 while(active.length > 0 || next.length > 0) { 6266 while(active.length > 0) { 6267 var t = active.pop() 6268 if(flags[t] === -side) { 6269 continue 6270 } 6271 flags[t] = side 6272 var c = cells[t] 6273 for(var j=0; j<3; ++j) { 6274 var f = neighbor[3*t+j] 6275 if(f >= 0 && flags[f] === 0) { 6276 if(constraint[3*t+j]) { 6277 next.push(f) 6278 } else { 6279 active.push(f) 6280 flags[f] = side 6281 } 6282 } 6283 } 6284 } 6285 6286 //Swap arrays and loop 6287 var tmp = next 6288 next = active 6289 active = tmp 6290 next.length = 0 6291 side = -side 6292 } 6293 6294 var result = filterCells(cells, flags, target) 6295 if(infinity) { 6296 return result.concat(index.boundary) 6297 } 6298 return result 6299} 6300 6301},{"binary-search-bounds":21}],35:[function(require,module,exports){ 6302'use strict' 6303 6304var bsearch = require('binary-search-bounds') 6305var orient = require('robust-orientation')[3] 6306 6307var EVENT_POINT = 0 6308var EVENT_END = 1 6309var EVENT_START = 2 6310 6311module.exports = monotoneTriangulate 6312 6313//A partial convex hull fragment, made of two unimonotone polygons 6314function PartialHull(a, b, idx, lowerIds, upperIds) { 6315 this.a = a 6316 this.b = b 6317 this.idx = idx 6318 this.lowerIds = lowerIds 6319 this.upperIds = upperIds 6320} 6321 6322//An event in the sweep line procedure 6323function Event(a, b, type, idx) { 6324 this.a = a 6325 this.b = b 6326 this.type = type 6327 this.idx = idx 6328} 6329 6330//This is used to compare events for the sweep line procedure
6331// Points are: 6332// 1. sorted lexicographically 6333// 2. sorted by type (point < end < start) 6334// 3. segments sorted by winding order 6335// 4. sorted by index 6336function compareEvent(a, b) { 6337 var d = 6338 (a.a[0] - b.a[0]) || 6339 (a.a[1] - b.a[1]) || 6340 (a.type - b.type) 6341 if(d) { return d } 6342 if(a.type !== EVENT_POINT) { 6343 d = orient(a.a, a.b, b.b) 6344 if(d) { return d } 6345 } 6346 return a.idx - b.idx 6347} 6348 6349function testPoint(hull, p) { 6350 return orient(hull.a, hull.b, p) 6351} 6352 6353function addPoint(cells, hulls, points, p, idx) { 6354 var lo = bsearch.lt(hulls, p, testPoint) 6355 var hi = bsearch.gt(hulls, p, testPoint) 6356 for(var i=lo; i<hi; ++i) { 6357 var hull = hulls[i] 6358 6359 //Insert p into lower hull 6360 var lowerIds = hull.lowerIds 6361 var m = lowerIds.length 6362 while(m > 1 && orient( 6363 points[lowerIds[m-2]], 6364 points[lowerIds[m-1]], 6365 p) > 0) { 6366 cells.push( 6367 [lowerIds[m-1], 6368 lowerIds[m-2], 6369 idx]) 6370 m -= 1 6371 } 6372 lowerIds.length = m 6373 lowerIds.push(idx) 6374 6375 //Insert p into upper hull 6376 var upperIds = hull.upperIds 6377 var m = upperIds.length 6378 while(m > 1 && orient( 6379 points[upperIds[m-2]], 6380 points[upperIds[m-1]], 6381 p) < 0) { 6382 cells.push( 6383 [upperIds[m-2], 6384 upperIds[m-1], 6385 idx]) 6386 m -= 1 6387 } 6388 upperIds.length = m 6389 upperIds.push(idx) 6390 } 6391} 6392 6393function findSplit(hull, edge) { 6394 var d 6395 if(hull.a[0] < edge.a[0]) { 6396 d = orient(hull.a, hull.b, edge.a) 6397 } else { 6398 d = orient(edge.b, edge.a, hull.a) 6399 } 6400 if(d) { return d } 6401 if(edge.b[0] < hull.b[0]) { 6402 d = orient(hull.a, hull.b, edge.b) 6403 } else { 6404 d = orient(edge.b, edge.a, hull.b) 6405 } 6406 return d || hull.idx - edge.idx 6407} 6408 6409function splitHulls(hulls, points, event) { 6410 var splitIdx = bsearch.le(hulls, event, findSplit) 6411 var hull = hulls[splitIdx] 6412 var upperIds = hull.upperIds 6413 var x = upperIds[upperIds.length-1] 6414 hull.upperIds = [x] 6415 hulls.splice(splitIdx+1, 0, 6416 new PartialHull(event.a, event.b, event.idx, [x], upperIds)) 6417} 6418 6419 6420function mergeHulls(hulls, points, event) { 6421 //Swap pointers for merge search 6422 var tmp = event.a 6423 event.a = event.b 6424 event.b = tmp 6425 var mergeIdx = bsearch.eq(hulls, event, findSplit) 6426 var upper = hulls[mergeIdx] 6427 var lower = hulls[mergeIdx-1] 6428 lower.upperIds = upper.upperIds 6429 hulls.splice(mergeIdx, 1) 6430} 6431 6432 6433function monotoneTriangulate(points, edges) { 6434 6435 var numPoints = points.length 6436 var numEdges = edges.length 6437 6438 var events = [] 6439 6440 //Create point events 6441 for(var i=0; i<numPoints; ++i) { 6442 events.push(new Event( 6443 points[i], 6444 null, 6445 EVENT_POINT, 6446 i)) 6447 } 6448 6449 //Create edge events 6450 for(var i=0; i<numEdges; ++i) { 6451 var e = edges[i] 6452 var a = points[e[0]] 6453 var b = points[e[1]] 6454 if(a[0] < b[0]) { 6455 events.push( 6456 new Event(a, b, EVENT_START, i), 6457 new Event(b, a, EVENT_END, i)) 6458 } else if(a[0] > b[0]) { 6459 events.push( 6460 new Event(b, a, EVENT_START, i), 6461 new Event(a, b, EVENT_END, i)) 6462 } 6463 } 6464 6465 //Sort events 6466 events.sort(compareEvent) 6467 6468 //Initialize hull 6469 var minX = events[0].a[0] - (1 + Math.abs(events[0].a[0])) * Math.pow(2, -52) 6470 var hull = [ new PartialHull([minX, 1], [minX, 0], -1, [], [], [], []) ] 6471 6472 //Process events in order 6473 var cells = [] 6474 for(var i=0, numEvents=events.length; i<numEvents; ++i) { 6475 var event = events[i] 6476 var type = event.type 6477 if(type === EVENT_POINT) { 6478 addPoint(cells, hull, points, event.a, event.idx) 6479 } else if(type === EVENT_START) { 6480 splitHulls(hull, points, event) 6481 } else { 6482 mergeHulls(hull, points, event) 6483 } 6484 } 6485 6486 //Return triangulation 6487 return cells 6488} 6489 6490},{"binary-search-bounds":21,"robust-orientation":60}],36:[function(require,module,exports){ 6491'use strict' 6492 6493var bsearch = require('binary-search-bounds') 6494 6495module.exports = createTriangulation 6496 6497function Triangulation(stars, edges) { 6498 this.stars = stars 6499 this.edges = edges 6500} 6501 6502var proto = Triangulation.prototype 6503 6504function removePair(list, j, k) { 6505 for(var i=1, n=list.length; i<n; i+=2) { 6506 if(list[i-1] === j && list[i] === k) { 6507 list[i-1] = list[n-2] 6508 list[i] = list[n-1] 6509 list.length = n - 2 6510 return 6511 } 6512 } 6513} 6514 6515proto.isConstraint = (function() { 6516 var e = [0,0] 6517 function compareLex(a, b) { 6518 return a[0] - b[0] || a[1] - b[1] 6519 } 6520 return function(i, j) { 6521 e[0] = Math.min(i,j) 6522 e[1] = Math.max(i,j) 6523 return bsearch.eq(this.edges, e, compareLex) >= 0 6524 } 6525})() 6526 6527proto.removeTriangle = function(i, j, k) { 6528 var stars = this.stars 6529 removePair(stars[i], j, k) 6530 removePair(stars[j], k, i) 6531 removePair(stars[k], i, j) 6532} 6533 6534proto.addTriangle = function(i, j, k) { 6535 var stars = this.stars 6536 stars[i].push(j, k) 6537 stars[j].push(k, i) 6538 stars[k].push(i, j) 6539} 6540 6541proto.opposite = function(j, i) { 6542 var list = this.stars[i] 6543 for(var k=1, n=list.length; k<n; k+=2) { 6544 if(list[k] === j) { 6545 return list[k-1] 6546 } 6547 } 6548 return -1 6549} 6550 6551proto.flip = function(i, j) { 6552 var a = this.opposite(i, j) 6553 var b = this.opposite(j, i) 6554 this.removeTriangle(i, j, a) 6555 this.removeTriangle(j, i, b) 6556 this.addTriangle(i, b, a) 6557 this.addTriangle(j, a, b) 6558} 6559 6560proto.edges = function() { 6561 var stars = this.stars 6562 var result = [] 6563 for(var i=0, n=stars.length; i<n; ++i) { 6564 var list = stars[i] 6565 for(var j=0, m=list.length; j<m; j+=2) { 6566 result.push([list[j], list[j+1]]) 6567 } 6568 } 6569 return result 6570} 6571 6572proto.cells = function() { 6573 var stars = this.stars 6574 var result = [] 6575 for(var i=0, n=stars.length; i<n; ++i) { 6576 var list = stars[i] 6577 for(var j=0, m=list.length; j<m; j+=2) { 6578 var s = list[j] 6579 var t = list[j+1] 6580 if(i < Math.min(s, t)) { 6581 result.push([i, s, t]) 6582 } 6583 } 6584 } 6585 return result 6586} 6587
6588function createTriangulation(numVerts, edges) { 6589 var stars = new Array(numVerts) 6590 for(var i=0; i<numVerts; ++i) { 6591 stars[i] = [] 6592 } 6593 return new Triangulation(stars, edges) 6594} 6595 6596},{"binary-search-bounds":21}],37:[function(require,module,exports){ 6597'use strict' 6598 6599module.exports = cleanPSLG 6600 6601var UnionFind = require('union-find') 6602var boxIntersect = require('box-intersect') 6603var segseg = require('robust-segment-intersect') 6604var rat = require('big-rat') 6605var ratCmp = require('big-rat/cmp') 6606var ratToFloat = require('big-rat/to-float') 6607var ratVec = require('rat-vec') 6608var nextafter = require('nextafter') 6609 6610var solveIntersection = require('./lib/rat-seg-intersect') 6611 6612// Bounds on a rational number when rounded to a float 6613function boundRat (r) { 6614 var f = ratToFloat(r) 6615 return [ 6616 nextafter(f, -Infinity), 6617 nextafter(f, Infinity) 6618 ] 6619} 6620 6621// Convert a list of edges in a pslg to bounding boxes 6622function boundEdges (points, edges) { 6623 var bounds = new Array(edges.length) 6624 for (var i = 0; i < edges.length; ++i) { 6625 var e = edges[i] 6626 var a = points[e[0]] 6627 var b = points[e[1]] 6628 bounds[i] = [ 6629 nextafter(Math.min(a[0], b[0]), -Infinity), 6630 nextafter(Math.min(a[1], b[1]), -Infinity), 6631 nextafter(Math.max(a[0], b[0]), Infinity), 6632 nextafter(Math.max(a[1], b[1]), Infinity) 6633 ] 6634 } 6635 return bounds 6636} 6637 6638// Convert a list of points into bounding boxes by duplicating coords 6639function boundPoints (points) { 6640 var bounds = new Array(points.length) 6641 for (var i = 0; i < points.length; ++i) { 6642 var p = points[i] 6643 bounds[i] = [ 6644 nextafter(p[0], -Infinity), 6645 nextafter(p[1], -Infinity), 6646 nextafter(p[0], Infinity), 6647 nextafter(p[1], Infinity) 6648 ] 6649 } 6650 return bounds 6651} 6652 6653// Find all pairs of crossing edges in a pslg (given edge bounds) 6654function getCrossings (points, edges, edgeBounds) { 6655 var result = [] 6656 boxIntersect(edgeBounds, function (i, j) { 6657 var e = edges[i] 6658 var f = edges[j] 6659 if (e[0] === f[0] || e[0] === f[1] || 6660 e[1] === f[0] || e[1] === f[1]) { 6661 return 6662 } 6663 var a = points[e[0]] 6664 var b = points[e[1]] 6665 var c = points[f[0]] 6666 var d = points[f[1]] 6667 if (segseg(a, b, c, d)) { 6668 result.push([i, j]) 6669 } 6670 }) 6671 return result 6672} 6673 6674// Find all pairs of crossing vertices in a pslg (given edge/vert bounds) 6675function getTJunctions (points, edges, edgeBounds, vertBounds) { 6676 var result = [] 6677 boxIntersect(edgeBounds, vertBounds, function (i, v) { 6678 var e = edges[i] 6679 if (e[0] === v || e[1] === v) { 6680 return 6681 } 6682 var p = points[v] 6683 var a = points[e[0]] 6684 var b = points[e[1]] 6685 if (segseg(a, b, p, p)) { 6686 result.push([i, v]) 6687 } 6688 }) 6689 return result 6690} 6691 6692// Cut edges along crossings/tjunctions 6693function cutEdges (floatPoints, edges, crossings, junctions, useColor) { 6694 var i, e 6695 6696 // Convert crossings into tjunctions by constructing rational points 6697 var ratPoints = floatPoints.map(function(p) { 6698 return [ 6699 rat(p[0]), 6700 rat(p[1]) 6701 ] 6702 }) 6703 for (i = 0; i < crossings.length; ++i) { 6704 var crossing = crossings[i] 6705 e = crossing[0] 6706 var f = crossing[1] 6707 var ee = edges[e] 6708 var ef = edges[f] 6709 var x = solveIntersection( 6710 ratVec(floatPoints[ee[0]]), 6711 ratVec(floatPoints[ee[1]]), 6712 ratVec(floatPoints[ef[0]]), 6713 ratVec(floatPoints[ef[1]])) 6714 if (!x) { 6715 // Segments are parallel, should already be handled by t-junctions 6716 continue 6717 } 6718 var idx = floatPoints.length 6719 floatPoints.push([ratToFloat(x[0]), ratToFloat(x[1])]) 6720 ratPoints.push(x) 6721 junctions.push([e, idx], [f, idx]) 6722 } 6723 6724 // Sort tjunctions 6725 junctions.sort(function (a, b) { 6726 if (a[0] !== b[0]) { 6727 return a[0] - b[0] 6728 } 6729 var u = ratPoints[a[1]] 6730 var v = ratPoints[b[1]] 6731 return ratCmp(u[0], v[0]) || ratCmp(u[1], v[1]) 6732 }) 6733 6734 // Split edges along junctions 6735 for (i = junctions.length - 1; i >= 0; --i) { 6736 var junction = junctions[i] 6737 e = junction[0] 6738 6739 var edge = edges[e] 6740 var s = edge[0] 6741 var t = edge[1] 6742 6743 // Check if edge is not lexicographically sorted 6744 var a = floatPoints[s] 6745 var b = floatPoints[t] 6746 if (((a[0] - b[0]) || (a[1] - b[1])) < 0) { 6747 var tmp = s 6748 s = t 6749 t = tmp 6750 } 6751 6752 // Split leading edge 6753 edge[0] = s 6754 var last = edge[1] = junction[1] 6755 6756 // If we are grouping edges by color, remember to track data 6757 var color 6758 if (useColor) { 6759 color = edge[2] 6760 } 6761 6762 // Split other edges 6763 while (i > 0 && junctions[i - 1][0] === e) { 6764 var junction = junctions[--i] 6765 var next = junction[1] 6766 if (useColor) { 6767 edges.push([last, next, color]) 6768 } else { 6769 edges.push([last, next]) 6770 } 6771 last = next 6772 } 6773
6774 // Add final edge 6775 if (useColor) { 6776 edges.push([last, t, color]) 6777 } else { 6778 edges.push([last, t]) 6779 } 6780 } 6781 6782 // Return constructed rational points 6783 return ratPoints 6784} 6785 6786// Merge overlapping points 6787function dedupPoints (floatPoints, ratPoints, floatBounds) { 6788 var numPoints = ratPoints.length 6789 var uf = new UnionFind(numPoints) 6790 6791 // Compute rational bounds 6792 var bounds = [] 6793 for (var i = 0; i < ratPoints.length; ++i) { 6794 var p = ratPoints[i] 6795 var xb = boundRat(p[0]) 6796 var yb = boundRat(p[1]) 6797 bounds.push([ 6798 nextafter(xb[0], -Infinity), 6799 nextafter(yb[0], -Infinity), 6800 nextafter(xb[1], Infinity), 6801 nextafter(yb[1], Infinity) 6802 ]) 6803 } 6804 6805 // Link all points with over lapping boxes 6806 boxIntersect(bounds, function (i, j) { 6807 uf.link(i, j) 6808 }) 6809 6810 // Do 1 pass over points to combine points in label sets 6811 var noDupes = true 6812 var labels = new Array(numPoints) 6813 for (var i = 0; i < numPoints; ++i) { 6814 var j = uf.find(i) 6815 if (j !== i) { 6816 // Clear no-dupes flag, zero out label 6817 noDupes = false 6818 // Make each point the top-left point from its cell 6819 floatPoints[j] = [ 6820 Math.min(floatPoints[i][0], floatPoints[j][0]), 6821 Math.min(floatPoints[i][1], floatPoints[j][1]) 6822 ] 6823 } 6824 } 6825 6826 // If no duplicates, return null to signal termination 6827 if (noDupes) { 6828 return null 6829 } 6830 6831 var ptr = 0 6832 for (var i = 0; i < numPoints; ++i) { 6833 var j = uf.find(i) 6834 if (j === i) { 6835 labels[i] = ptr 6836 floatPoints[ptr++] = floatPoints[i] 6837 } else { 6838 labels[i] = -1 6839 } 6840 } 6841 6842 floatPoints.length = ptr 6843 6844 // Do a second pass to fix up missing labels 6845 for (var i = 0; i < numPoints; ++i) { 6846 if (labels[i] < 0) { 6847 labels[i] = labels[uf.find(i)] 6848 } 6849 } 6850 6851 // Return resulting union-find data structure 6852 return labels 6853} 6854 6855function compareLex2 (a, b) { return (a[0] - b[0]) || (a[1] - b[1]) } 6856function compareLex3 (a, b) { 6857 var d = (a[0] - b[0]) || (a[1] - b[1]) 6858 if (d) { 6859 return d 6860 } 6861 if (a[2] < b[2]) { 6862 return -1 6863 } else if (a[2] > b[2]) { 6864 return 1 6865 } 6866 return 0 6867} 6868 6869// Remove duplicate edge labels 6870function dedupEdges (edges, labels, useColor) { 6871 if (edges.length === 0) { 6872 return 6873 } 6874 if (labels) { 6875 for (var i = 0; i < edges.length; ++i) { 6876 var e = edges[i] 6877 var a = labels[e[0]] 6878 var b = labels[e[1]] 6879 e[0] = Math.min(a, b) 6880 e[1] = Math.max(a, b) 6881 } 6882 } else { 6883 for (var i = 0; i < edges.length; ++i) { 6884 var e = edges[i] 6885 var a = e[0] 6886 var b = e[1] 6887 e[0] = Math.min(a, b) 6888 e[1] = Math.max(a, b) 6889 } 6890 } 6891 if (useColor) { 6892 edges.sort(compareLex3) 6893 } else { 6894 edges.sort(compareLex2) 6895 } 6896 var ptr = 1 6897 for (var i = 1; i < edges.length; ++i) { 6898 var prev = edges[i - 1] 6899 var next = edges[i] 6900 if (next[0] === prev[0] && next[1] === prev[1] && 6901 (!useColor || next[2] === prev[2])) { 6902 continue 6903 } 6904 edges[ptr++] = next 6905 } 6906 edges.length = ptr 6907} 6908 6909function preRound (points, edges, useColor) { 6910 var labels = dedupPoints(points, [], boundPoints(points)) 6911 dedupEdges(edges, labels, useColor) 6912 return !!labels 6913} 6914 6915// Repeat until convergence 6916function snapRound (points, edges, useColor) { 6917 // 1. find edge crossings 6918 var edgeBounds = boundEdges(points, edges) 6919 var crossings = getCrossings(points, edges, edgeBounds) 6920 6921 // 2. find t-junctions 6922 var vertBounds = boundPoints(points) 6923 var tjunctions = getTJunctions(points, edges, edgeBounds, vertBounds) 6924 6925 // 3. cut edges, construct rational points 6926 var ratPoints = cutEdges(points, edges, crossings, tjunctions, useColor) 6927 6928 // 4. dedupe verts 6929 var labels = dedupPoints(points, ratPoints, vertBounds) 6930 6931 // 5. dedupe edges 6932 dedupEdges(edges, labels, useColor) 6933 6934 // 6. check termination 6935 if (!labels) { 6936 return (crossings.length > 0 || tjunctions.length > 0) 6937 } 6938 6939 // More iterations necessary 6940 return true 6941} 6942 6943// Main loop, runs PSLG clean up until completion 6944function cleanPSLG (points, edges, colors) { 6945 // If using colors, augment edges with color data 6946 var prevEdges 6947 if (colors) { 6948 prevEdges = edges 6949 var augEdges = new Array(edges.length) 6950 for (var i = 0; i < edges.length; ++i) { 6951 var e = edges[i] 6952 augEdges[i] = [e[0], e[1], colors[i]] 6953 }
6954 edges = augEdges 6955 } 6956 6957 // First round: remove duplicate edges and points 6958 var modified = preRound(points, edges, !!colors) 6959 6960 // Run snap rounding until convergence 6961 while (snapRound(points, edges, !!colors)) { 6962 modified = true 6963 } 6964 6965 // Strip color tags 6966 if (!!colors && modified) { 6967 prevEdges.length = 0 6968 colors.length = 0 6969 for (var i = 0; i < edges.length; ++i) { 6970 var e = edges[i] 6971 prevEdges.push([e[0], e[1]]) 6972 colors.push(e[2]) 6973 } 6974 } 6975 6976 return modified 6977} 6978 6979},{"./lib/rat-seg-intersect":38,"big-rat":8,"big-rat/cmp":6,"big-rat/to-float":20,"box-intersect":25,"nextafter":47,"rat-vec":56,"robust-segment-intersect":62,"union-find":75}],38:[function(require,module,exports){ 6980'use strict' 6981 6982module.exports = solveIntersection 6983 6984var ratMul = require('big-rat/mul') 6985var ratDiv = require('big-rat/div') 6986var ratSub = require('big-rat/sub') 6987var ratSign = require('big-rat/sign') 6988var rvSub = require('rat-vec/sub') 6989var rvAdd = require('rat-vec/add') 6990var rvMuls = require('rat-vec/muls') 6991 6992function ratPerp (a, b) { 6993 return ratSub(ratMul(a[0], b[1]), ratMul(a[1], b[0])) 6994} 6995 6996// Solve for intersection
6997// x = a + t (b-a) 6998// (x - c) ^ (d-c) = 0 6999// (t * (b-a) + (a-c) ) ^ (d-c) = 0 7000// t * (b-a)^(d-c) = (d-c)^(a-c) 7001// t = (d-c)^(a-c) / (b-a)^(d-c) 7002 7003function solveIntersection (a, b, c, d) { 7004 var ba = rvSub(b, a) 7005 var dc = rvSub(d, c) 7006 7007 var baXdc = ratPerp(ba, dc) 7008 7009 if (ratSign(baXdc) === 0) { 7010 return null 7011 } 7012 7013 var ac = rvSub(a, c) 7014 var dcXac = ratPerp(dc, ac) 7015 7016 var t = ratDiv(dcXac, baXdc) 7017 var s = rvMuls(ba, t) 7018 var r = rvAdd(a, s) 7019 7020 return r 7021} 7022 7023},{"big-rat/div":7,"big-rat/mul":17,"big-rat/sign":18,"big-rat/sub":19,"rat-vec/add":55,"rat-vec/muls":57,"rat-vec/sub":58}],39:[function(require,module,exports){ 7024(function (Buffer){ 7025var hasTypedArrays = false 7026if(typeof Float64Array !== "undefined") { 7027 var DOUBLE_VIEW = new Float64Array(1) 7028 , UINT_VIEW = new Uint32Array(DOUBLE_VIEW.buffer) 7029 DOUBLE_VIEW[0] = 1.0 7030 hasTypedArrays = true 7031 if(UINT_VIEW[1] === 0x3ff00000) { 7032 //Use little endian 7033 module.exports = function doubleBitsLE(n) { 7034 DOUBLE_VIEW[0] = n 7035 return [ UINT_VIEW[0], UINT_VIEW[1] ] 7036 } 7037 function toDoubleLE(lo, hi) { 7038 UINT_VIEW[0] = lo 7039 UINT_VIEW[1] = hi 7040 return DOUBLE_VIEW[0] 7041 } 7042 module.exports.pack = toDoubleLE 7043 function lowUintLE(n) { 7044 DOUBLE_VIEW[0] = n 7045 return UINT_VIEW[0] 7046 } 7047 module.exports.lo = lowUintLE 7048 function highUintLE(n) { 7049 DOUBLE_VIEW[0] = n 7050 return UINT_VIEW[1] 7051 } 7052 module.exports.hi = highUintLE 7053 } else if(UINT_VIEW[0] === 0x3ff00000) { 7054 //Use big endian 7055 module.exports = function doubleBitsBE(n) { 7056 DOUBLE_VIEW[0] = n 7057 return [ UINT_VIEW[1], UINT_VIEW[0] ] 7058 } 7059 function toDoubleBE(lo, hi) { 7060 UINT_VIEW[1] = lo 7061 UINT_VIEW[0] = hi 7062 return DOUBLE_VIEW[0] 7063 } 7064 module.exports.pack = toDoubleBE 7065 function lowUintBE(n) { 7066 DOUBLE_VIEW[0] = n 7067 return UINT_VIEW[1] 7068 } 7069 module.exports.lo = lowUintBE 7070 function highUintBE(n) { 7071 DOUBLE_VIEW[0] = n 7072 return UINT_VIEW[0] 7073 } 7074 module.exports.hi = highUintBE 7075 } else { 7076 hasTypedArrays = false 7077 } 7078} 7079if(!hasTypedArrays) { 7080 var buffer = new Buffer(8) 7081 module.exports = function doubleBits(n) { 7082 buffer.writeDoubleLE(n, 0, true) 7083 return [ buffer.readUInt32LE(0, true), buffer.readUInt32LE(4, true) ] 7084 } 7085 function toDouble(lo, hi) { 7086 buffer.writeUInt32LE(lo, 0, true) 7087 buffer.writeUInt32LE(hi, 4, true) 7088 return buffer.readDoubleLE(0, true) 7089 } 7090 module.exports.pack = toDouble 7091 function lowUint(n) { 7092 buffer.writeDoubleLE(n, 0, true) 7093 return buffer.readUInt32LE(0, true) 7094 } 7095 module.exports.lo = lowUint 7096 function highUint(n) { 7097 buffer.writeDoubleLE(n, 0, true) 7098 return buffer.readUInt32LE(4, true) 7099 } 7100 module.exports.hi = highUint 7101} 7102 7103module.exports.sign = function(n) { 7104 return module.exports.hi(n) >>> 31 7105} 7106 7107module.exports.exponent = function(n) { 7108 var b = module.exports.hi(n) 7109 return ((b<<1) >>> 21) - 1023 7110} 7111 7112module.exports.fraction = function(n) { 7113 var lo = module.exports.lo(n) 7114 var hi = module.exports.hi(n) 7115 var b = hi & ((1<<20) - 1) 7116 if(hi & 0x7ff00000) { 7117 b += (1<<20) 7118 } 7119 return [lo, b] 7120} 7121 7122module.exports.denormalized = function(n) { 7123 var hi = module.exports.hi(n) 7124 return !(hi & 0x7ff00000) 7125} 7126}).call(this,require("buffer").Buffer) 7127},{"buffer":82}],40:[function(require,module,exports){ 7128"use strict" 7129 7130function dupe_array(count, value, i) { 7131 var c = count[i]|0 7132 if(c <= 0) { 7133 return [] 7134 } 7135 var result = new Array(c), j 7136 if(i === count.length-1) { 7137 for(j=0; j<c; ++j) { 7138 result[j] = value 7139 } 7140 } else { 7141 for(j=0; j<c; ++j) { 7142 result[j] = dupe_array(count, value, i+1) 7143 } 7144 } 7145 return result 7146} 7147 7148function dupe_number(count, value) { 7149 var result, i 7150 result = new Array(count) 7151 for(i=0; i<count; ++i) { 7152 result[i] = value 7153 } 7154 return result 7155} 7156 7157function dupe(count, value) { 7158 if(typeof value === "undefined") { 7159 value = 0 7160 } 7161 switch(typeof count) { 7162 case "number": 7163 if(count > 0) { 7164 return dupe_number(count|0, value) 7165 } 7166 break 7167 case "object": 7168 if(typeof (count.length) === "number") { 7169 return dupe_array(count, value, 0) 7170 } 7171 break 7172 } 7173 return [] 7174} 7175 7176module.exports = dupe 7177},{}],41:[function(require,module,exports){ 7178var parseXml = require('xml-parse-from-string') 7179 7180function extractSvgPath (svgDoc) { 7181 // concat all the <path> elements to form an SVG path string 7182 if (typeof svgDoc === 'string') { 7183 svgDoc = parseXml(svgDoc) 7184 } 7185 if (!svgDoc || typeof svgDoc.getElementsByTagName !== 'function') { 7186 throw new Error('could not get an XML document from the specified SVG contents') 7187 } 7188 7189 var paths = Array.prototype.slice.call(svgDoc.getElementsByTagName('path')) 7190 return paths.reduce(function (prev, path) { 7191 var d = path.getAttribute('d') || '' 7192 return prev + ' ' + d.replace(/\s+/g, ' ').trim() 7193 }, '').trim() 7194} 7195 7196module.exports = function () { 7197 throw new Error('use extract-svg-path/transform to inline SVG contents into your bundle') 7198} 7199 7200module.exports.parse = extractSvgPath 7201 7202//deprecated 7203module.exports.fromString = extractSvgPath 7204 7205},{"xml-parse-from-string":79}],42:[function(require,module,exports){ 7206'use strict'; 7207 7208var isCallable = require('is-callable'); 7209 7210var toStr = Object.prototype.toString; 7211var hasOwnProperty = Object.prototype.hasOwnProperty; 7212
7213var forEachArray = function forEachArray(array, iterator, receiver) { 7214 for (var i = 0, len = array.length; i < len; i++) { 7215 if (hasOwnProperty.call(array, i)) { 7216 if (receiver == null) { 7217 iterator(array[i], i, array); 7218 } else { 7219 iterator.call(receiver, array[i], i, array); 7220 } 7221 } 7222 } 7223}; 7224 7225var forEachString = function forEachString(string, iterator, receiver) { 7226 for (var i = 0, len = string.length; i < len; i++) { 7227 // no such thing as a sparse string. 7228 if (receiver == null) { 7229 iterator(string.charAt(i), i, string); 7230 } else { 7231 iterator.call(receiver, string.charAt(i), i, string); 7232 } 7233 } 7234}; 7235 7236var forEachObject = function forEachObject(object, iterator, receiver) { 7237 for (var k in object) { 7238 if (hasOwnProperty.call(object, k)) { 7239 if (receiver == null) { 7240 iterator(object[k], k, object); 7241 } else { 7242 iterator.call(receiver, object[k], k, object); 7243 } 7244 } 7245 } 7246}; 7247 7248var forEach = function forEach(list, iterator, thisArg) { 7249 if (!isCallable(iterator)) { 7250 throw new TypeError('iterator must be a function'); 7251 } 7252 7253 var receiver; 7254 if (arguments.length >= 3) { 7255 receiver = thisArg; 7256 } 7257 7258 if (toStr.call(list) === '[object Array]') { 7259 forEachArray(list, iterator, receiver); 7260 } else if (typeof list === 'string') { 7261 forEachString(list, iterator, receiver); 7262 } else { 7263 forEachObject(list, iterator, receiver); 7264 } 7265}; 7266 7267module.exports = forEach; 7268 7269},{"is-callable":45}],43:[function(require,module,exports){ 7270(function (global){ 7271var win; 7272 7273if (typeof window !== "undefined") { 7274 win = window; 7275} else if (typeof global !== "undefined") { 7276 win = global; 7277} else if (typeof self !== "undefined"){ 7278 win = self; 7279} else { 7280 win = {}; 7281} 7282 7283module.exports = win; 7284 7285}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) 7286},{}],44:[function(require,module,exports){ 7287if (typeof Object.create === 'function') { 7288 // implementation from standard node.js 'util' module 7289 module.exports = function inherits(ctor, superCtor) { 7290 ctor.super_ = superCtor 7291 ctor.prototype = Object.create(superCtor.prototype, { 7292 constructor: { 7293 value: ctor, 7294 enumerable: false, 7295 writable: true, 7296 configurable: true 7297 } 7298 }); 7299 }; 7300} else { 7301 // old school shim for old browsers 7302 module.exports = function inherits(ctor, superCtor) { 7303 ctor.super_ = superCtor 7304 var TempCtor = function () {} 7305 TempCtor.prototype = superCtor.prototype 7306 ctor.prototype = new TempCtor() 7307 ctor.prototype.constructor = ctor 7308 } 7309} 7310 7311},{}],45:[function(require,module,exports){ 7312'use strict'; 7313 7314var fnToStr = Function.prototype.toString; 7315 7316var constructorRegex = /^\s*class\b/; 7317var isES6ClassFn = function isES6ClassFunction(value) { 7318 try { 7319 var fnStr = fnToStr.call(value); 7320 return constructorRegex.test(fnStr); 7321 } catch (e) { 7322 return false; // not a function 7323 } 7324}; 7325 7326var tryFunctionObject = function tryFunctionToStr(value) { 7327 try { 7328 if (isES6ClassFn(value)) { return false; } 7329 fnToStr.call(value); 7330 return true; 7331 } catch (e) { 7332 return false; 7333 } 7334}; 7335var toStr = Object.prototype.toString; 7336var fnClass = '[object Function]'; 7337var genClass = '[object GeneratorFunction]'; 7338var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'; 7339 7340module.exports = function isCallable(value) { 7341 if (!value) { return false; } 7342 if (typeof value !== 'function' && typeof value !== 'object') { return false; } 7343 if (typeof value === 'function' && !value.prototype) { return true; } 7344 if (hasToStringTag) { return tryFunctionObject(value); } 7345 if (isES6ClassFn(value)) { return false; } 7346 var strClass = toStr.call(value); 7347 return strClass === fnClass || strClass === genClass; 7348}; 7349 7350},{}],46:[function(require,module,exports){ 7351var xhr = require('xhr'); 7352 7353module.exports = function (opts, cb) { 7354 if (typeof opts === 'string') opts = { uri: opts }; 7355 7356 xhr(opts, function (err, res, body) { 7357 if (err) return cb(err); 7358 if (!/^2/.test(res.statusCode)) { 7359 return cb(new Error('http status code: ' + res.statusCode)); 7360 } 7361 var div = document.createElement('div'); 7362 div.innerHTML = body; 7363 var svg = div.querySelector('svg'); 7364 if (!svg) return cb(new Error('svg not present in resource')); 7365 cb(null, svg); 7366 }); 7367}; 7368 7369},{"xhr":78}],47:[function(require,module,exports){ 7370"use strict" 7371 7372var doubleBits = require("double-bits") 7373 7374var SMALLEST_DENORM = Math.pow(2, -1074) 7375var UINT_MAX = (-1)>>>0 7376 7377module.exports = nextafter 7378 7379function nextafter(x, y) { 7380 if(isNaN(x) || isNaN(y)) { 7381 return NaN 7382 } 7383 if(x === y) { 7384 return x 7385 } 7386 if(x === 0) { 7387 if(y < 0) { 7388 return -SMALLEST_DENORM 7389 } else { 7390 return SMALLEST_DENORM 7391 } 7392 } 7393 var hi = doubleBits.hi(x) 7394 var lo = doubleBits.lo(x) 7395 if((y > x) === (x > 0)) { 7396 if(lo === UINT_MAX) { 7397 hi += 1 7398 lo = 0 7399 } else { 7400 lo += 1 7401 } 7402 } else { 7403 if(lo === 0) { 7404 lo = UINT_MAX 7405 hi -= 1 7406 } else { 7407 lo -= 1 7408 } 7409 } 7410 return doubleBits.pack(lo, hi) 7411} 7412},{"double-bits":39}],48:[function(require,module,exports){
7413var getBounds = require('bound-points') 7414var unlerp = require('unlerp') 7415 7416module.exports = normalizePathScale 7417function normalizePathScale (positions, bounds) { 7418 if (!Array.isArray(positions)) { 7419 throw new TypeError('must specify positions as first argument') 7420 } 7421 if (!Array.isArray(bounds)) { 7422 bounds = getBounds(positions) 7423 } 7424 7425 var min = bounds[0] 7426 var max = bounds[1] 7427 7428 var width = max[0] - min[0] 7429 var height = max[1] - min[1] 7430 7431 var aspectX = width > height ? 1 : (height / width) 7432 var aspectY = width > height ? (width / height) : 1 7433 7434 if (max[0] - min[0] === 0 || max[1] - min[1] === 0) { 7435 return positions // div by zero; leave positions unchanged 7436 } 7437 7438 for (var i = 0; i < positions.length; i++) { 7439 var pos = positions[i] 7440 pos[0] = (unlerp(min[0], max[0], pos[0]) * 2 - 1) / aspectX 7441 pos[1] = (unlerp(min[1], max[1], pos[1]) * 2 - 1) / aspectY 7442 } 7443 return positions 7444} 7445},{"bound-points":24,"unlerp":76}],49:[function(require,module,exports){ 7446 7447var PI = Math.PI 7448var _120 = radians(120) 7449 7450module.exports = normalize 7451 7452/** 7453 * describe `path` in terms of cubic bézier 7454 * curves and move commands 7455 * 7456 * @param {Array} path 7457 * @return {Array} 7458 */ 7459 7460function normalize(path){ 7461 // init state 7462 var prev 7463 var result = [] 7464 var bezierX = 0 7465 var bezierY = 0 7466 var startX = 0 7467 var startY = 0 7468 var quadX = null 7469 var quadY = null 7470 var x = 0 7471 var y = 0 7472 7473 for (var i = 0, len = path.length; i < len; i++) { 7474 var seg = path[i] 7475 var command = seg[0] 7476 switch (command) { 7477 case 'M': 7478 startX = seg[1] 7479 startY = seg[2] 7480 break 7481 case 'A': 7482 seg = arc(x, y,seg[1],seg[2],radians(seg[3]),seg[4],seg[5],seg[6],seg[7]) 7483 // split multi part 7484 seg.unshift('C') 7485 if (seg.length > 7) { 7486 result.push(seg.splice(0, 7)) 7487 seg.unshift('C') 7488 } 7489 break 7490 case 'S': 7491 // default control point 7492 var cx = x 7493 var cy = y 7494 if (prev == 'C' || prev == 'S') { 7495 cx += cx - bezierX // reflect the previous command's control 7496 cy += cy - bezierY // point relative to the current point 7497 } 7498 seg = ['C', cx, cy, seg[1], seg[2], seg[3], seg[4]] 7499 break 7500 case 'T': 7501 if (prev == 'Q' || prev == 'T') { 7502 quadX = x * 2 - quadX // as with 'S' reflect previous control point 7503 quadY = y * 2 - quadY 7504 } else { 7505 quadX = x 7506 quadY = y 7507 } 7508 seg = quadratic(x, y, quadX, quadY, seg[1], seg[2]) 7509 break 7510 case 'Q': 7511 quadX = seg[1] 7512 quadY = seg[2] 7513 seg = quadratic(x, y, seg[1], seg[2], seg[3], seg[4]) 7514 break 7515 case 'L': 7516 seg = line(x, y, seg[1], seg[2]) 7517 break 7518 case 'H': 7519 seg = line(x, y, seg[1], y) 7520 break 7521 case 'V': 7522 seg = line(x, y, x, seg[1]) 7523 break 7524 case 'Z': 7525 seg = line(x, y, startX, startY) 7526 break 7527 } 7528 7529 // update state 7530 prev = command 7531 x = seg[seg.length - 2] 7532 y = seg[seg.length - 1] 7533 if (seg.length > 4) { 7534 bezierX = seg[seg.length - 4] 7535 bezierY = seg[seg.length - 3] 7536 } else { 7537 bezierX = x 7538 bezierY = y 7539 } 7540 result.push(seg) 7541 } 7542 7543 return result 7544} 7545 7546function line(x1, y1, x2, y2){ 7547 return ['C', x1, y1, x2, y2, x2, y2] 7548} 7549 7550function quadratic(x1, y1, cx, cy, x2, y2){ 7551 return [ 7552 'C', 7553 x1/3 + (2/3) * cx, 7554 y1/3 + (2/3) * cy, 7555 x2/3 + (2/3) * cx, 7556 y2/3 + (2/3) * cy, 7557 x2, 7558 y2 7559 ] 7560} 7561 7562// This function is ripped from 7563// github.com/DmitryBaranovskiy/raphael/blob/4d97d4/raphael.js#L2216-L2304 7564// which references w3.org/TR/SVG11/implnote.html#ArcImplementationNotes 7565// TODO: make it human readable 7566 7567function arc(x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) { 7568 if (!recursive) { 7569 var xy = rotate(x1, y1, -angle) 7570 x1 = xy.x 7571 y1 = xy.y 7572 xy = rotate(x2, y2, -angle) 7573 x2 = xy.x 7574 y2 = xy.y 7575 var x = (x1 - x2) / 2 7576 var y = (y1 - y2) / 2 7577 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry) 7578 if (h > 1) { 7579 h = Math.sqrt(h) 7580 rx = h * rx 7581 ry = h * ry 7582 } 7583 var rx2 = rx * rx 7584 var ry2 = ry * ry 7585 var k = (large_arc_flag == sweep_flag ? -1 : 1) 7586 * Math.sqrt(Math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))) 7587 if (k == Infinity) k = 1 // neutralize 7588 var cx = k * rx * y / ry + (x1 + x2) / 2 7589 var cy = k * -ry * x / rx + (y1 + y2) / 2 7590 var f1 = Math.asin(((y1 - cy) / ry).toFixed(9)) 7591 var f2 = Math.asin(((y2 - cy) / ry).toFixed(9)) 7592 7593 f1 = x1 < cx ? PI - f1 : f1 7594 f2 = x2 < cx ? PI - f2 : f2 7595 if (f1 < 0) f1 = PI * 2 + f1 7596 if (f2 < 0) f2 = PI * 2 + f2 7597 if (sweep_flag && f1 > f2) f1 = f1 - PI * 2 7598 if (!sweep_flag && f2 > f1) f2 = f2 - PI * 2 7599 } else { 7600 f1 = recursive[0] 7601 f2 = recursive[1] 7602 cx = recursive[2] 7603 cy = recursive[3] 7604 } 7605 // greater than 120 degrees requires multiple segments 7606 if (Math.abs(f2 - f1) > _120) { 7607 var f2old = f2 7608 var x2old = x2 7609 var y2old = y2 7610 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1) 7611 x2 = cx + rx * Math.cos(f2) 7612 y2 = cy + ry * Math.sin(f2) 7613 var res = arc(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]) 7614 } 7615 var t = Math.tan((f2 - f1) / 4) 7616 var hx = 4 / 3 * rx * t 7617 var hy = 4 / 3 * ry * t 7618 var curve = [ 7619 2 * x1 - (x1 + hx * Math.sin(f1)), 7620 2 * y1 - (y1 - hy * Math.cos(f1)), 7621 x2 + hx * Math.sin(f2), 7622 y2 - hy * Math.cos(f2), 7623 x2, 7624 y2 7625 ] 7626 if (recursive) return curve 7627 if (res) curve = curve.concat(res) 7628 for (var i = 0; i < curve.length;) { 7629 var rot = rotate(curve[i], curve[i+1], angle) 7630 curve[i++] = rot.x 7631 curve[i++] = rot.y 7632 } 7633 return curve 7634} 7635 7636function rotate(x, y, rad){ 7637 return { 7638 x: x * Math.cos(rad) - y * Math.sin(rad), 7639 y: x * Math.sin(rad) + y * Math.cos(rad) 7640 } 7641} 7642 7643function radians(degress){ 7644 return degress * (PI / 180) 7645} 7646 7647},{}],50:[function(require,module,exports){ 7648/* 7649object-assign 7650(c) Sindre Sorhus 7651@license MIT
7652*/ 7653 7654'use strict'; 7655/* eslint-disable no-unused-vars */ 7656var getOwnPropertySymbols = Object.getOwnPropertySymbols; 7657var hasOwnProperty = Object.prototype.hasOwnProperty; 7658var propIsEnumerable = Object.prototype.propertyIsEnumerable; 7659 7660function toObject(val) { 7661 if (val === null || val === undefined) { 7662 throw new TypeError('Object.assign cannot be called with null or undefined'); 7663 } 7664 7665 return Object(val); 7666} 7667 7668function shouldUseNative() { 7669 try { 7670 if (!Object.assign) { 7671 return false; 7672 } 7673 7674 // Detect buggy property enumeration order in older V8 versions. 7675 7676 // https://bugs.chromium.org/p/v8/issues/detail?id=4118 7677 var test1 = new String('abc'); // eslint-disable-line no-new-wrappers 7678 test1[5] = 'de'; 7679 if (Object.getOwnPropertyNames(test1)[0] === '5') { 7680 return false; 7681 } 7682 7683 // https://bugs.chromium.org/p/v8/issues/detail?id=3056 7684 var test2 = {}; 7685 for (var i = 0; i < 10; i++) { 7686 test2['_' + String.fromCharCode(i)] = i; 7687 } 7688 var order2 = Object.getOwnPropertyNames(test2).map(function (n) { 7689 return test2[n]; 7690 }); 7691 if (order2.join('') !== '0123456789') { 7692 return false; 7693 } 7694 7695 // https://bugs.chromium.org/p/v8/issues/detail?id=3056 7696 var test3 = {}; 7697 'abcdefghijklmnopqrst'.split('').forEach(function (letter) { 7698 test3[letter] = letter; 7699 }); 7700 if (Object.keys(Object.assign({}, test3)).join('') !== 7701 'abcdefghijklmnopqrst') { 7702 return false; 7703 } 7704 7705 return true; 7706 } catch (err) { 7707 // We don't expect any of the above to throw, but better to be safe. 7708 return false; 7709 } 7710} 7711 7712module.exports = shouldUseNative() ? Object.assign : function (target, source) { 7713 var from; 7714 var to = toObject(target); 7715 var symbols; 7716 7717 for (var s = 1; s < arguments.length; s++) { 7718 from = Object(arguments[s]); 7719 7720 for (var key in from) { 7721 if (hasOwnProperty.call(from, key)) { 7722 to[key] = from[key]; 7723 } 7724 } 7725 7726 if (getOwnPropertySymbols) { 7727 symbols = getOwnPropertySymbols(from); 7728 for (var i = 0; i < symbols.length; i++) { 7729 if (propIsEnumerable.call(from, symbols[i])) { 7730 to[symbols[i]] = from[symbols[i]]; 7731 } 7732 } 7733 } 7734 } 7735 7736 return to; 7737}; 7738 7739},{}],51:[function(require,module,exports){ 7740module.exports = once 7741 7742once.proto = once(function () { 7743 Object.defineProperty(Function.prototype, 'once', { 7744 value: function () { 7745 return once(this) 7746 }, 7747 configurable: true 7748 }) 7749}) 7750 7751function once (fn) { 7752 var called = false 7753 return function () { 7754 if (called) return 7755 called = true 7756 return fn.apply(this, arguments) 7757 } 7758} 7759 7760},{}],52:[function(require,module,exports){ 7761var trim = require('trim') 7762 , forEach = require('for-each') 7763 , isArray = function(arg) { 7764 return Object.prototype.toString.call(arg) === '[object Array]'; 7765 } 7766 7767module.exports = function (headers) { 7768 if (!headers) 7769 return {} 7770 7771 var result = {} 7772 7773 forEach( 7774 trim(headers).split('\n') 7775 , function (row) { 7776 var index = row.indexOf(':') 7777 , key = trim(row.slice(0, index)).toLowerCase() 7778 , value = trim(row.slice(index + 1)) 7779 7780 if (typeof(result[key]) === 'undefined') { 7781 result[key] = value 7782 } else if (isArray(result[key])) { 7783 result[key].push(value) 7784 } else { 7785 result[key] = [ result[key], value ] 7786 } 7787 } 7788 ) 7789 7790 return result 7791} 7792},{"for-each":42,"trim":71}],53:[function(require,module,exports){ 7793 7794module.exports = parse 7795 7796/** 7797 * expected argument lengths 7798 * @type {Object} 7799 */ 7800 7801var length = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0} 7802 7803/** 7804 * segment pattern 7805 * @type {RegExp} 7806 */ 7807 7808var segment = /([astvzqmhlc])([^astvzqmhlc]*)/ig 7809 7810/** 7811 * parse an svg path data string. Generates an Array 7812 * of commands where each command is an Array of the 7813 * form `[command, arg1, arg2, ...]` 7814 * 7815 * @param {String} path 7816 * @return {Array} 7817 */ 7818 7819function parse(path) { 7820 var data = [] 7821 path.replace(segment, function(_, command, args){ 7822 var type = command.toLowerCase() 7823 args = parseValues(args) 7824 7825 // overloaded moveTo 7826 if (type == 'm' && args.length > 2) { 7827 data.push([command].concat(args.splice(0, 2))) 7828 type = 'l' 7829 command = command == 'm' ? 'l' : 'L' 7830 } 7831 7832 while (true) { 7833 if (args.length == length[type]) { 7834 args.unshift(command) 7835 return data.push(args) 7836 }
7837 if (args.length < length[type]) throw new Error('malformed path data') 7838 data.push([command].concat(args.splice(0, length[type]))) 7839 } 7840 }) 7841 return data 7842} 7843 7844var number = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/ig 7845 7846function parseValues(args) { 7847 var numbers = args.match(number) 7848 return numbers ? numbers.map(Number) : [] 7849} 7850 7851},{}],54:[function(require,module,exports){ 7852'use strict'; 7853module.exports = function (min, max) { 7854 if (max === undefined) { 7855 max = min; 7856 min = 0; 7857 } 7858 7859 if (typeof min !== 'number' || typeof max !== 'number') { 7860 throw new TypeError('Expected all arguments to be numbers'); 7861 } 7862 7863 return Math.random() * (max - min) + min; 7864}; 7865 7866},{}],55:[function(require,module,exports){ 7867'use strict' 7868 7869var bnadd = require('big-rat/add') 7870 7871module.exports = add 7872 7873function add (a, b) { 7874 var n = a.length 7875 var r = new Array(n) 7876 for (var i=0; i<n; ++i) { 7877 r[i] = bnadd(a[i], b[i]) 7878 } 7879 return r 7880} 7881 7882},{"big-rat/add":5}],56:[function(require,module,exports){ 7883'use strict' 7884 7885module.exports = float2rat 7886 7887var rat = require('big-rat') 7888 7889function float2rat(v) { 7890 var result = new Array(v.length) 7891 for(var i=0; i<v.length; ++i) { 7892 result[i] = rat(v[i]) 7893 } 7894 return result 7895} 7896 7897},{"big-rat":8}],57:[function(require,module,exports){ 7898'use strict' 7899 7900var rat = require('big-rat') 7901var mul = require('big-rat/mul') 7902 7903module.exports = muls 7904 7905function muls(a, x) { 7906 var s = rat(x) 7907 var n = a.length 7908 var r = new Array(n) 7909 for(var i=0; i<n; ++i) { 7910 r[i] = mul(a[i], s) 7911 } 7912 return r 7913} 7914 7915},{"big-rat":8,"big-rat/mul":17}],58:[function(require,module,exports){ 7916'use strict' 7917 7918var bnsub = require('big-rat/sub') 7919 7920module.exports = sub 7921 7922function sub(a, b) { 7923 var n = a.length 7924 var r = new Array(n) 7925 for(var i=0; i<n; ++i) { 7926 r[i] = bnsub(a[i], b[i]) 7927 } 7928 return r 7929} 7930 7931},{"big-rat/sub":19}],59:[function(require,module,exports){ 7932"use strict" 7933 7934var twoProduct = require("two-product") 7935var robustSum = require("robust-sum") 7936var robustDiff = require("robust-subtract") 7937var robustScale = require("robust-scale") 7938 7939var NUM_EXPAND = 6 7940 7941function cofactor(m, c) { 7942 var result = new Array(m.length-1) 7943 for(var i=1; i<m.length; ++i) { 7944 var r = result[i-1] = new Array(m.length-1) 7945 for(var j=0,k=0; j<m.length; ++j) { 7946 if(j === c) { 7947 continue 7948 } 7949 r[k++] = m[i][j] 7950 } 7951 } 7952 return result 7953} 7954 7955function matrix(n) { 7956 var result = new Array(n) 7957 for(var i=0; i<n; ++i) { 7958 result[i] = new Array(n) 7959 for(var j=0; j<n; ++j) { 7960 result[i][j] = ["m", j, "[", (n-i-2), "]"].join("") 7961 } 7962 } 7963 return result 7964} 7965 7966function generateSum(expr) { 7967 if(expr.length === 1) { 7968 return expr[0] 7969 } else if(expr.length === 2) { 7970 return ["sum(", expr[0], ",", expr[1], ")"].join("") 7971 } else { 7972 var m = expr.length>>1 7973 return ["sum(", generateSum(expr.slice(0, m)), ",", generateSum(expr.slice(m)), ")"].join("") 7974 } 7975} 7976 7977function makeProduct(a, b) { 7978 if(a.charAt(0) === "m") { 7979 if(b.charAt(0) === "w") { 7980 var toks = a.split("[") 7981 return ["w", b.substr(1), "m", toks[0].substr(1)].join("") 7982 } else { 7983 return ["prod(", a, ",", b, ")"].join("") 7984 } 7985 } else { 7986 return makeProduct(b, a) 7987 } 7988} 7989 7990function sign(s) { 7991 if(s & 1 !== 0) { 7992 return "-" 7993 } 7994 return "" 7995} 7996 7997function determinant(m) { 7998 if(m.length === 2) { 7999 return [["diff(", makeProduct(m[0][0], m[1][1]), ",", makeProduct(m[1][0], m[0][1]), ")"].join("")] 8000 } else { 8001 var expr = [] 8002 for(var i=0; i<m.length; ++i) { 8003 expr.push(["scale(", generateSum(determinant(cofactor(m, i))), ",", sign(i), m[0][i], ")"].join("")) 8004 } 8005 return expr 8006 } 8007} 8008 8009function makeSquare(d, n) { 8010 var terms = [] 8011 for(var i=0; i<n-2; ++i) { 8012 terms.push(["prod(m", d, "[", i, "],m", d, "[", i, "])"].join("")) 8013 } 8014 return generateSum(terms) 8015} 8016 8017function orientation(n) { 8018 var pos = [] 8019 var neg = [] 8020 var m = matrix(n) 8021 for(var i=0; i<n; ++i) { 8022 m[0][i] = "1" 8023 m[n-1][i] = "w"+i 8024 } 8025 for(var i=0; i<n; ++i) { 8026 if((i&1)===0) { 8027 pos.push.apply(pos,determinant(cofactor(m, i))) 8028 } else { 8029 neg.push.apply(neg,determinant(cofactor(m, i))) 8030 } 8031 } 8032 var posExpr = generateSum(pos) 8033 var negExpr = generateSum(neg) 8034 var funcName = "exactInSphere" + n 8035 var funcArgs = [] 8036 for(var i=0; i<n; ++i) { 8037 funcArgs.push("m" + i) 8038 } 8039 var code = ["function ", funcName, "(", funcArgs.join(), "){"] 8040 for(var i=0; i<n; ++i) { 8041 code.push("var w",i,"=",makeSquare(i,n),";") 8042 for(var j=0; j<n; ++j) { 8043 if(j !== i) { 8044 code.push("var w",i,"m",j,"=scale(w",i,",m",j,"[0]);") 8045 } 8046 } 8047 } 8048 code.push("var p=", posExpr, ",n=", negExpr, ",d=diff(p,n);return d[d.length-1];}return ", funcName) 8049 var proc = new Function("sum", "diff", "prod", "scale", code.join("")) 8050 return proc(robustSum, robustDiff, twoProduct, robustScale) 8051} 8052 8053function inSphere0() { return 0 } 8054function inSphere1() { return 0 } 8055function inSphere2() { return 0 } 8056 8057var CACHED = [ 8058 inSphere0, 8059 inSphere1, 8060 inSphere2 8061] 8062 8063function slowInSphere(args) { 8064 var proc = CACHED[args.length] 8065 if(!proc) { 8066 proc = CACHED[args.length] = orientation(args.length) 8067 } 8068 return proc.apply(undefined, args) 8069} 8070 8071function generateInSphereTest() { 8072 while(CACHED.length <= NUM_EXPAND) { 8073 CACHED.push(orientation(CACHED.length)) 8074 } 8075 var args = [] 8076 var procArgs = ["slow"] 8077 for(var i=0; i<=NUM_EXPAND; ++i) { 8078 args.push("a" + i) 8079 procArgs.push("o" + i) 8080 } 8081 var code = [ 8082 "function testInSphere(", args.join(), "){switch(arguments.length){case 0:case 1:return 0;" 8083 ] 8084 for(var i=2; i<=NUM_EXPAND; ++i) { 8085 code.push("case ", i, ":return o", i, "(", args.slice(0, i).join(), ");") 8086 } 8087 code.push("}var s=new Array(arguments.length);for(var i=0;i<arguments.length;++i){s[i]=arguments[i]};return slow(s);}return testInSphere") 8088 procArgs.push(code.join("")) 8089 8090 var proc = Function.apply(undefined, procArgs) 8091 8092 module.exports = proc.apply(undefined, [slowInSphere].concat(CACHED)) 8093 for(var i=0; i<=NUM_EXPAND; ++i) { 8094 module.exports[i] = CACHED[i] 8095 } 8096} 8097 8098generateInSphereTest() 8099},{"robust-scale":61,"robust-subtract":63,"robust-sum":64,"two-product":72}],60:[function(require,module,exports){ 8100"use strict" 8101 8102var twoProduct = require("two-product") 8103var robustSum = require("robust-sum") 8104var robustScale = require("robust-scale") 8105var robustSubtract = require("robust-subtract") 8106 8107var NUM_EXPAND = 5 8108 8109var EPSILON = 1.1102230246251565e-16 8110var ERRBOUND3 = (3.0 + 16.0 * EPSILON) * EPSILON 8111var ERRBOUND4 = (7.0 + 56.0 * EPSILON) * EPSILON 8112 8113function cofactor(m, c) { 8114 var result = new Array(m.length-1) 8115 for(var i=1; i<m.length; ++i) { 8116 var r = result[i-1] = new Array(m.length-1) 8117 for(var j=0,k=0; j<m.length; ++j) { 8118 if(j === c) { 8119 continue 8120 } 8121 r[k++] = m[i][j] 8122 } 8123 } 8124 return result 8125} 8126 8127function matrix(n) { 8128 var result = new Array(n) 8129 for(var i=0; i<n; ++i) { 8130 result[i] = new Array(n) 8131 for(var j=0; j<n; ++j) { 8132 result[i][j] = ["m", j, "[", (n-i-1), "]"].join("") 8133 } 8134 } 8135 return result 8136} 8137 8138function sign(n) { 8139 if(n & 1) { 8140 return "-" 8141 } 8142 return "" 8143} 8144 8145function generateSum(expr) { 8146 if(expr.length === 1) { 8147 return expr[0] 8148 } else if(expr.length === 2) { 8149 return ["sum(", expr[0], ",", expr[1], ")"].join("") 8150 } else { 8151 var m = expr.length>>1 8152 return ["sum(", generateSum(expr.slice(0, m)), ",", generateSum(expr.slice(m)), ")"].join("") 8153 } 8154} 8155 8156function determinant(m) { 8157 if(m.length === 2) { 8158 return [["sum(prod(", m[0][0], ",", m[1][1], "),prod(-", m[0][1], ",", m[1][0], "))"].join("")] 8159 } else { 8160 var expr = [] 8161 for(var i=0; i<m.length; ++i) { 8162 expr.push(["scale(", generateSum(determinant(cofactor(m, i))), ",", sign(i), m[0][i], ")"].join("")) 8163 } 8164 return expr 8165 } 8166} 8167 8168function orientation(n) { 8169 var pos = [] 8170 var neg = [] 8171 var m = matrix(n) 8172 var args = [] 8173 for(var i=0; i<n; ++i) { 8174 if((i&1)===0) {
8175 pos.push.apply(pos, determinant(cofactor(m, i))) 8176 } else { 8177 neg.push.apply(neg, determinant(cofactor(m, i))) 8178 } 8179 args.push("m" + i) 8180 } 8181 var posExpr = generateSum(pos) 8182 var negExpr = generateSum(neg) 8183 var funcName = "orientation" + n + "Exact" 8184 var code = ["function ", funcName, "(", args.join(), "){var p=", posExpr, ",n=", negExpr, ",d=sub(p,n);\ 8185return d[d.length-1];};return ", funcName].join("") 8186 var proc = new Function("sum", "prod", "scale", "sub", code) 8187 return proc(robustSum, twoProduct, robustScale, robustSubtract) 8188} 8189 8190var orientation3Exact = orientation(3) 8191var orientation4Exact = orientation(4) 8192 8193var CACHED = [ 8194 function orientation0() { return 0 }, 8195 function orientation1() { return 0 }, 8196 function orientation2(a, b) { 8197 return b[0] - a[0] 8198 }, 8199 function orientation3(a, b, c) { 8200 var l = (a[1] - c[1]) * (b[0] - c[0]) 8201 var r = (a[0] - c[0]) * (b[1] - c[1]) 8202 var det = l - r 8203 var s 8204 if(l > 0) { 8205 if(r <= 0) { 8206 return det 8207 } else { 8208 s = l + r 8209 } 8210 } else if(l < 0) { 8211 if(r >= 0) { 8212 return det 8213 } else { 8214 s = -(l + r) 8215 } 8216 } else { 8217 return det 8218 } 8219 var tol = ERRBOUND3 * s 8220 if(det >= tol || det <= -tol) { 8221 return det 8222 } 8223 return orientation3Exact(a, b, c) 8224 }, 8225 function orientation4(a,b,c,d) { 8226 var adx = a[0] - d[0] 8227 var bdx = b[0] - d[0] 8228 var cdx = c[0] - d[0] 8229 var ady = a[1] - d[1] 8230 var bdy = b[1] - d[1] 8231 var cdy = c[1] - d[1] 8232 var adz = a[2] - d[2] 8233 var bdz = b[2] - d[2] 8234 var cdz = c[2] - d[2] 8235 var bdxcdy = bdx * cdy 8236 var cdxbdy = cdx * bdy
8237 var cdxady = cdx * ady 8238 var adxcdy = adx * cdy 8239 var adxbdy = adx * bdy 8240 var bdxady = bdx * ady 8241 var det = adz * (bdxcdy - cdxbdy) 8242 + bdz * (cdxady - adxcdy) 8243 + cdz * (adxbdy - bdxady) 8244 var permanent = (Math.abs(bdxcdy) + Math.abs(cdxbdy)) * Math.abs(adz) 8245 + (Math.abs(cdxady) + Math.abs(adxcdy)) * Math.abs(bdz) 8246 + (Math.abs(adxbdy) + Math.abs(bdxady)) * Math.abs(cdz) 8247 var tol = ERRBOUND4 * permanent 8248 if ((det > tol) || (-det > tol)) { 8249 return det 8250 } 8251 return orientation4Exact(a,b,c,d) 8252 } 8253] 8254 8255function slowOrient(args) { 8256 var proc = CACHED[args.length] 8257 if(!proc) { 8258 proc = CACHED[args.length] = orientation(args.length) 8259 } 8260 return proc.apply(undefined, args) 8261} 8262 8263function generateOrientationProc() { 8264 while(CACHED.length <= NUM_EXPAND) { 8265 CACHED.push(orientation(CACHED.length)) 8266 } 8267 var args = [] 8268 var procArgs = ["slow"] 8269 for(var i=0; i<=NUM_EXPAND; ++i) { 8270 args.push("a" + i) 8271 procArgs.push("o" + i) 8272 } 8273 var code = [ 8274 "function getOrientation(", args.join(), "){switch(arguments.length){case 0:case 1:return 0;" 8275 ] 8276 for(var i=2; i<=NUM_EXPAND; ++i) { 8277 code.push("case ", i, ":return o", i, "(", args.slice(0, i).join(), ");") 8278 } 8279 code.push("}var s=new Array(arguments.length);for(var i=0;i<arguments.length;++i){s[i]=arguments[i]};return slow(s);}return getOrientation") 8280 procArgs.push(code.join("")) 8281 8282 var proc = Function.apply(undefined, procArgs) 8283 module.exports = proc.apply(undefined, [slowOrient].concat(CACHED)) 8284 for(var i=0; i<=NUM_EXPAND; ++i) { 8285 module.exports[i] = CACHED[i] 8286 } 8287} 8288 8289generateOrientationProc() 8290},{"robust-scale":61,"robust-subtract":63,"robust-sum":64,"two-product":72}],61:[function(require,module,exports){ 8291"use strict" 8292 8293var twoProduct = require("two-product") 8294var twoSum = require("two-sum") 8295 8296module.exports = scaleLinearExpansion 8297 8298function scaleLinearExpansion(e, scale) { 8299 var n = e.length 8300 if(n === 1) { 8301 var ts = twoProduct(e[0], scale) 8302 if(ts[0]) { 8303 return ts 8304 } 8305 return [ ts[1] ] 8306 } 8307 var g = new Array(2 * n) 8308 var q = [0.1, 0.1] 8309 var t = [0.1, 0.1] 8310 var count = 0 8311 twoProduct(e[0], scale, q) 8312 if(q[0]) { 8313 g[count++] = q[0] 8314 } 8315 for(var i=1; i<n; ++i) { 8316 twoProduct(e[i], scale, t) 8317 var pq = q[1] 8318 twoSum(pq, t[0], q) 8319 if(q[0]) { 8320 g[count++] = q[0] 8321 } 8322 var a = t[1] 8323 var b = q[1] 8324 var x = a + b 8325 var bv = x - a 8326 var y = b - bv 8327 q[1] = x 8328 if(y) { 8329 g[count++] = y 8330 } 8331 } 8332 if(q[1]) { 8333 g[count++] = q[1] 8334 } 8335 if(count === 0) { 8336 g[count++] = 0.0 8337 } 8338 g.length = count 8339 return g 8340} 8341},{"two-product":72,"two-sum":73}],62:[function(require,module,exports){ 8342"use strict" 8343 8344module.exports = segmentsIntersect 8345 8346var orient = require("robust-orientation")[3] 8347 8348function checkCollinear(a0, a1, b0, b1) { 8349 8350 for(var d=0; d<2;
8350 ++d) { 8351 var x0 = a0[d] 8352 var y0 = a1[d] 8353 var l0 = Math.min(x0, y0) 8354 var h0 = Math.max(x0, y0) 8355 8356 var x1 = b0[d] 8357 var y1 = b1[d] 8358 var l1 = Math.min(x1, y1) 8359 var h1 = Math.max(x1, y1) 8360 8361 if(h1 < l0 || h0 < l1) { 8362 return false 8363 } 8364 } 8365 8366 return true 8367} 8368 8369function segmentsIntersect(a0, a1, b0, b1) { 8370 var x0 = orient(a0, b0, b1) 8371 var y0 = orient(a1, b0, b1) 8372 if((x0 > 0 && y0 > 0) || (x0 < 0 && y0 < 0)) { 8373 return false 8374 } 8375 8376 var x1 = orient(b0, a0, a1) 8377 var y1 = orient(b1, a0, a1) 8378 if((x1 > 0 && y1 > 0) || (x1 < 0 && y1 < 0)) { 8379 return false 8380 } 8381 8382 //Check for degenerate collinear case 8383 if(x0 === 0 && y0 === 0 && x1 === 0 && y1 === 0) { 8384 return checkCollinear(a0, a1, b0, b1) 8385 } 8386 8387 return true 8388} 8389},{"robust-orientation":60}],63:[function(require,module,exports){ 8390"use strict" 8391 8392module.exports = robustSubtract 8393 8394//Easy case: Add two scalars 8395function scalarScalar(a, b) { 8396 var x = a + b 8397 var bv = x - a 8398 var av = x - bv 8399 var br = b - bv 8400 var ar = a - av 8401 var y = ar + br 8402 if(y) { 8403 return [y, x] 8404 } 8405 return [x] 8406} 8407 8408function robustSubtract(e, f) { 8409 var ne = e.length|0 8410 var nf = f.length|0 8411 if(ne === 1 && nf === 1) { 8412 return scalarScalar(e[0], -f[0]) 8413 } 8414 var n = ne + nf 8415 var g = new Array(n) 8416 var count = 0 8417 var eptr = 0 8418 var fptr = 0 8419 var abs = Math.abs 8420 var ei = e[eptr] 8421 var ea = abs(ei) 8422 var fi = -f[fptr] 8423 var fa = abs(fi) 8424 var a, b 8425 if(ea < fa) { 8426 b = ei 8427 eptr += 1 8428 if(eptr < ne) { 8429 ei = e[eptr] 8430 ea = abs(ei) 8431 } 8432 } else { 8433 b = fi 8434 fptr += 1 8435 if(fptr < nf) { 8436 fi = -f[fptr] 8437 fa = abs(fi) 8438 } 8439 } 8440 if((eptr < ne && ea < fa) || (fptr >= nf)) { 8441 a = ei 8442 eptr += 1 8443 if(eptr < ne) { 8444 ei = e[eptr] 8445 ea = abs(ei) 8446 } 8447 } else { 8448 a = fi 8449 fptr += 1 8450 if(fptr < nf) { 8451 fi = -f[fptr] 8452 fa = abs(fi) 8453 } 8454 } 8455 var x = a + b 8456 var bv = x - a 8457 var y = b - bv 8458 var q0 = y 8459 var q1 = x 8460 var _x, _bv, _av, _br, _ar 8461 while(eptr < ne && fptr < nf) { 8462 if(ea < fa) { 8463 a = ei 8464 eptr += 1 8465 if(eptr < ne) { 8466 ei = e[eptr] 8467 ea = abs(ei) 8468 } 8469 } else { 8470 a = fi 8471 fptr += 1 8472 if(fptr < nf) { 8473 fi = -f[fptr] 8474 fa = abs(fi) 8475 } 8476 } 8477 b = q0 8478 x = a + b 8479 bv = x - a 8480 y = b - bv 8481 if(y) { 8482 g[count++] = y 8483 } 8484 _x = q1 + x 8485 _bv = _x - q1 8486 _av = _x - _bv 8487 _br = x - _bv 8488 _ar = q1 - _av 8489 q0 = _ar + _br 8490 q1 = _x 8491 } 8492 while(eptr < ne) { 8493 a = ei 8494 b = q0 8495 x = a + b 8496 bv = x - a 8497 y = b - bv 8498 if(y) { 8499 g[count++] = y 8500 } 8501 _x = q1 + x 8502 _bv = _x - q1 8503 _av = _x - _bv 8504 _br = x - _bv 8505 _ar = q1 - _av 8506 q0 = _ar + _br 8507 q1 = _x 8508 eptr += 1 8509 if(eptr < ne) { 8510 ei = e[eptr] 8511 } 8512 } 8513 while(fptr < nf) { 8514 a = fi 8515 b = q0 8516 x = a + b 8517 bv = x - a 8518 y = b - bv 8519 if(y) { 8520 g[count++] = y 8521 } 8522 _x = q1 + x 8523 _bv = _x - q1 8524 _av = _x - _bv 8525 _br = x - _bv 8526 _ar = q1 - _av 8527 q0 = _ar + _br 8528 q1 = _x 8529 fptr += 1 8530 if(fptr < nf) { 8531 fi = -f[fptr] 8532 } 8533 } 8534 if(q0) { 8535 g[count++] = q0 8536 } 8537 if(q1) { 8538 g[count++] = q1 8539 } 8540 if(!count) { 8541 g[count++] = 0.0 8542 } 8543 g.length = count 8544 return g 8545} 8546},{}],64:[function(require,module,exports){ 8547"use strict" 8548 8549module.exports = linearExpansionSum 8550 8551//Easy case: Add two scalars 8552function scalarScalar(a, b) { 8553 var x = a + b 8554 var bv = x - a 8555 var av = x - bv 8556 var br = b - bv 8557 var ar = a - av 8558 var y = ar + br 8559 if(y) { 8560 return [y, x] 8561 } 8562 return [x] 8563} 8564 8565function linearExpansionSum(e, f) { 8566 var ne = e.length|0 8567 var nf = f.length|0 8568 if(ne === 1 && nf === 1) { 8569 return scalarScalar(e[0], f[0]) 8570 } 8571 var n = ne + nf 8572 var g = new Array(n) 8573 var count = 0 8574 var eptr = 0 8575 var fptr = 0 8576 var abs = Math.abs 8577 var ei = e[eptr] 8578 var ea = abs(ei) 8579 var fi = f[fptr] 8580 var fa = abs(fi) 8581 var a, b 8582 if(ea < fa) { 8583 b = ei 8584 eptr += 1 8585 if(eptr < ne) { 8586 ei = e[eptr] 8587 ea = abs(ei) 8588 } 8589 } else { 8590 b = fi 8591 fptr += 1 8592 if(fptr < nf) { 8593 fi = f[fptr] 8594 fa = abs(fi) 8595 } 8596 } 8597 if((eptr < ne && ea < fa) || (fptr >= nf)) { 8598 a = ei 8599 eptr += 1 8600 if(eptr < ne) { 8601 ei = e[eptr] 8602 ea = abs(ei) 8603 } 8604 } else { 8605 a = fi 8606 fptr += 1 8607 if(fptr < nf) { 8608 fi = f[fptr] 8609 fa = abs(fi) 8610 } 8611 } 8612 var x = a + b 8613 var bv = x - a 8614 var y = b - bv 8615 var q0 = y 8616 var q1 = x 8617 var _x, _bv, _av, _br, _ar 8618 while(eptr < ne && fptr < nf) { 8619 if(ea < fa) { 8620 a = ei 8621 eptr += 1 8622 if(eptr < ne) { 8623 ei = e[eptr] 8624 ea = abs(ei) 8625 } 8626 } else { 8627 a = fi 8628 fptr += 1 8629 if(fptr < nf) { 8630 fi = f[fptr] 8631 fa = abs(fi) 8632 } 8633 } 8634 b = q0 8635 x = a + b 8636 bv = x - a 8637 y = b - bv 8638 if(y) { 8639 g[count++] = y 8640 } 8641 _x = q1 + x 8642 _bv = _x - q1 8643 _av = _x - _bv 8644 _br = x - _bv 8645 _ar = q1 - _av 8646 q0 = _ar + _br 8647 q1 = _x 8648 } 8649 while(eptr < ne) { 8650 a = ei 8651 b = q0 8652 x = a + b 8653 bv = x - a 8654 y = b - bv 8655 if(y) { 8656 g[count++] = y 8657 } 8658 _x = q1 + x 8659 _bv = _x - q1 8660 _av = _x - _bv 8661 _br = x - _bv 8662 _ar = q1 - _av 8663 q0 = _ar + _br 8664 q1 = _x 8665 eptr += 1 8666 if(eptr < ne) { 8667 ei = e[eptr] 8668 } 8669 } 8670 while(fptr < nf) { 8671 a = fi 8672 b = q0 8673 x = a + b 8674 bv = x - a 8675 y = b - bv 8676 if(y) { 8677 g[count++] = y 8678 } 8679 _x = q1 + x 8680 _bv = _x - q1 8681 _av = _x - _bv 8682 _br = x - _bv 8683 _ar = q1 - _av 8684 q0 = _ar + _br 8685 q1 = _x 8686 fptr += 1 8687 if(fptr < nf) { 8688 fi = f[fptr] 8689 } 8690 } 8691 if(q0) { 8692 g[count++] = q0 8693 } 8694 if(q1) { 8695 g[count++] = q1 8696 } 8697 if(!count) { 8698 g[count++] = 0.0 8699 } 8700 g.length = count 8701 return g 8702} 8703},{}],65:[function(require,module,exports){ 8704// square distance from a point to a segment 8705function getSqSegDist(p, p1, p2) { 8706 var x = p1[0], 8707 y = p1[1], 8708 dx = p2[0] - x, 8709 dy = p2[1] - y; 8710 8711 if (dx !== 0 || dy !== 0) { 8712 8713 var t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy); 8714 8715 if (t > 1) { 8716 x = p2[0]; 8717 y = p2[1]; 8718 8719 } else if (t > 0) { 8720 x += dx * t; 8721 y += dy * t; 8722 } 8723 } 8724 8725 dx = p[0] - x; 8726 dy = p[1] - y; 8727 8728 return dx * dx + dy * dy; 8729} 8730 8731function simplifyDPStep(points, first, last, sqTolerance, simplified) { 8732 var maxSqDist = sqTolerance, 8733 index; 8734 8735 for (var i = first + 1; i < last; i++) { 8736 var sqDist = getSqSegDist(points[i], points[first], points[last]); 8737 8738 if (sqDist > maxSqDist) { 8739 index = i; 8740 maxSqDist = sqDist; 8741 } 8742 } 8743 8744 if (maxSqDist > sqTolerance) { 8745 if (index - first > 1) simplifyDPStep(points, first, index, sqTolerance, simplified); 8746 simplified.push(points[index]); 8747 if (last - index > 1) simplifyDPStep(points, index, last, sqTolerance, simplified); 8748 } 8749} 8750 8751// simplification using Ramer-Douglas-Peucker algorithm 8752module.exports = function simplifyDouglasPeucker(points, tolerance) { 8753 if (points.length<=1) 8754 return points; 8755 tolerance = typeof tolerance === 'number' ? tolerance : 1; 8756 var sqTolerance = tolerance * tolerance; 8757 8758 var last = points.length - 1; 8759 8760 var simplified = [points[0]]; 8761 simplifyDPStep(points, 0, last, sqTolerance, simplified); 8762 simplified.push(points[last]); 8763 8764 return simplified; 8765} 8766 8767},{}],66:[function(require,module,exports){ 8768var simplifyRadialDist = require('./radial-distance') 8769var simplifyDouglasPeucker = require('./douglas-peucker') 8770 8771//simplifies using both algorithms 8772module.exports = function simplify(points, tolerance) { 8773 points = simplifyRadialDist(points, tolerance); 8774 points = simplifyDouglasPeucker(points, tolerance); 8775 return points; 8776} 8777 8778module.exports.radialDistance = simplifyRadialDist; 8779module.exports.douglasPeucker = simplifyDouglasPeucker; 8780},{"./douglas-peucker":65,"./radial-distance":67}],67:[function(require,module,exports){ 8781function getSqDist(p1, p2) { 8782 var dx = p1[0] - p2[0], 8783 dy = p1[1] - p2[1]; 8784 8785 return dx * dx + dy * dy; 8786} 8787 8788// basic distance-based simplification 8789module.exports = function simplifyRadialDist(points, tolerance) { 8790 if (points.length<=1) 8791 return points; 8792 tolerance = typeof tolerance === 'number' ? tolerance : 1; 8793 var sqTolerance = tolerance * tolerance; 8794 8795 var prevPoint = points[0], 8796 newPoints = [prevPoint], 8797 point; 8798 8799 for (var i = 1, len = points.length; i < len; i++) { 8800 point = points[i]; 8801 8802 if (getSqDist(point, prevPoint) > sqTolerance) { 8803 newPoints.push(point); 8804 prevPoint = point; 8805 } 8806 } 8807 8808 if (prevPoint !== point) newPoints.push(point); 8809 8810 return newPoints; 8811} 8812},{}],68:[function(require,module,exports){ 8813var parseSVG = require('parse-svg-path') 8814var getContours = require('svg-path-contours') 8815var cdt2d = require('cdt2d') 8816var cleanPSLG = require('clean-pslg')
8817var getBounds = require('bound-points') 8818var normalize = require('normalize-path-scale') 8819var random = require('random-float') 8820var assign = require('object-assign') 8821var simplify = require('simplify-path') 8822 8823module.exports = svgMesh3d 8824function svgMesh3d (svgPath, opt) { 8825 if (typeof svgPath !== 'string') { 8826 throw new TypeError('must provide a string as first parameter') 8827 } 8828 8829 opt = assign({ 8830 delaunay: true, 8831 clean: true, 8832 exterior: false, 8833 randomization: 0, 8834 simplify: 0, 8835 scale: 1 8836 }, opt) 8837 8838 var i 8839 // parse string as a list of operations 8840 var svg = parseSVG(svgPath) 8841 8842 // convert curves into discrete points 8843 var contours = getContours(svg, opt.scale) 8844 8845 // optionally simplify the path for faster triangulation and/or aesthetics 8846 if (opt.simplify > 0 && typeof opt.simplify === 'number') { 8847 for (i = 0; i < contours.length; i++) { 8848 contours[i] = simplify(contours[i], opt.simplify) 8849 } 8850 } 8851 8852 // prepare for triangulation 8853 var polyline = denestPolyline(contours) 8854 var positions = polyline.positions 8855 var bounds = getBounds(positions) 8856 8857 // optionally add random points for aesthetics 8858 var randomization = opt.randomization 8859 if (typeof randomization === 'number' && randomization > 0) { 8860 addRandomPoints(positions, bounds, randomization) 8861 } 8862 8863 var loops = polyline.edges 8864 var edges = [] 8865 for (i = 0; i < loops.length; ++i) { 8866 var loop = loops[i] 8867 for (var j = 0; j < loop.length; ++j) { 8868 edges.push([loop[j], loop[(j + 1) % loop.length]]) 8869 } 8870 } 8871 8872 // this updates points/edges so that they now form a valid PSLG 8873 if (opt.clean !== false) { 8874 cleanPSLG(positions, edges) 8875 } 8876 8877 // triangulate mesh 8878 var cells = cdt2d(positions, edges, opt) 8879 8880 // rescale to [-1 ... 1] 8881 if (opt.normalize !== false) { 8882 normalize(positions, bounds) 8883 } 8884 8885 // convert to 3D representation and flip on Y axis for convenience w/ OpenGL 8886 to3D(positions) 8887 8888 return { 8889 positions: positions, 8890 cells: cells 8891 } 8892} 8893 8894function to3D (positions) { 8895 for (var i = 0; i < positions.length; i++) { 8896 var xy = positions[i] 8897 xy[1] *= -1 8898 xy[2] = xy[2] || 0 8899 } 8900} 8901 8902function addRandomPoints (positions, bounds, count) { 8903 var min = bounds[0] 8904 var max = bounds[1] 8905 8906 for (var i = 0; i < count; i++) { 8907 positions.push([ // random [ x, y ] 8908 random(min[0], max[0]), 8909 random(min[1], max[1]) 8910 ]) 8911 } 8912} 8913 8914function denestPolyline (nested) { 8915 var positions = [] 8916 var edges = [] 8917 8918 for (var i = 0; i < nested.length; i++) { 8919 var path = nested[i] 8920 var loop = [] 8921 for (var j = 0; j < path.length; j++) { 8922 var pos = path[j] 8923 var idx = positions.indexOf(pos) 8924 if (idx === -1) { 8925 positions.push(pos) 8926 idx = positions.length - 1 8927 } 8928 loop.push(idx) 8929 } 8930 edges.push(loop) 8931 } 8932 return { 8933 positions: positions, 8934 edges: edges 8935 } 8936} 8937 8938},{"bound-points":24,"cdt2d":32,"clean-pslg":37,"normalize-path-scale":48,"object-assign":50,"parse-svg-path":53,"random-float":54,"simplify-path":66,"svg-path-contours":69}],69:[function(require,module,exports){ 8939var bezier = require('adaptive-bezier-curve') 8940var abs = require('abs-svg-path') 8941var norm = require('normalize-svg-path') 8942var copy = require('vec2-copy') 8943 8944function set(out, x, y) { 8945 out[0] = x 8946 out[1] = y 8947 return out 8948} 8949 8950var tmp1 = [0,0], 8951 tmp2 = [0,0], 8952 tmp3 = [0,0] 8953 8954function bezierTo(points, scale, start, seg) { 8955 bezier(start, 8956 set(tmp1, seg[1], seg[2]), 8957 set(tmp2, seg[3], seg[4]), 8958 set(tmp3, seg[5], seg[6]), scale, points) 8959} 8960 8961module.exports = function contours(svg, scale) { 8962 var paths = [] 8963 8964 var points = [] 8965 var pen = [0, 0] 8966 norm(abs(svg)).forEach(function(segment, i, self) { 8967 if (segment[0] === 'M') { 8968 copy(pen, segment.slice(1)) 8969 if (points.length>0) { 8970 paths.push(points) 8971 points = [] 8972 } 8973 } else if (segment[0] === 'C') { 8974 bezierTo(points, scale, pen, segment) 8975 set(pen, segment[5], segment[6]) 8976 } else { 8977 throw new Error('illegal type in SVG: '+segment[0]) 8978 } 8979 }) 8980 if (points.length>0) 8981 paths.push(points) 8982 return paths 8983} 8984},{"abs-svg-path":2,"adaptive-bezier-curve":4,"normalize-svg-path":49,"vec2-copy":77}],70:[function(require,module,exports){ 8985var inherits = require('inherits') 8986 8987module.exports = function(THREE) { 8988 8989 function Complex(mesh) { 8990 if (!(this instanceof Complex)) 8991 return new Complex(mesh) 8992 THREE.Geometry.call(this) 8993 this.dynamic = true 8994 8995 if (mesh) 8996 this.update(mesh) 8997 } 8998 8999 inherits(Complex, THREE.Geometry) 9000 9001 //may expose these in next version 9002 Complex.prototype._updatePositions = function(positions) { 9003 for (var i=0; i<positions.length; i++) { 9004 var pos = positions[i] 9005 if (i > this.vertices.length-1) 9006 this.vertices.push(new THREE.Vector3().fromArray(pos)) 9007 else 9008 this.vertices[i].fromArray(pos) 9009 } 9010 this.vertices.length = positions.length 9011 this.verticesNeedUpdate = true 9012 } 9013 9014 Complex.prototype._updateCells = function(cells) { 9015 for (var i=0; i<cells.length; i++) { 9016 var face = cells[i] 9017 if (i > this.faces.length-1) 9018 this.faces.push(new THREE.Face3(face[0], face[1], face[2])) 9019 else { 9020 var tf = this.faces[i] 9021 tf.a = face[0] 9022 tf.b = face[1] 9023 tf.c = face[2] 9024 } 9025 } 9026 9027 this.faces.length = cells.length 9028 this.elementsNeedUpdate = true 9029 } 9030 9031 Complex.prototype.update = function(mesh) { 9032 this._updatePositions(mesh.positions) 9033 this._updateCells(mesh.cells) 9034 } 9035 9036 return Complex 9037} 9038},{"inherits":44}],71:[function(require,module,exports){ 9039 9040exports = module.exports = trim; 9041 9042function trim(str){ 9043 return str.replace(/^\s*|\s*$/g, ''); 9044} 9045 9046exports.left = function(str){ 9047 return str.replace(/^\s*/, ''); 9048}; 9049 9050exports.right = function(str){ 9051 return str.replace(/\s*$/, ''); 9052}; 9053 9054},{}],72:[function(require,module,exports){ 9055"use strict" 9056 9057module.exports = twoProduct 9058 9059var SPLITTER = +(Math.pow(2, 27) + 1.0) 9060 9061function twoProduct(a, b, result) { 9062 var x = a * b 9063 9064 var c = SPLITTER * a 9065 var abig = c - a 9066 var ahi = c - abig 9067 var alo = a - ahi 9068 9069 var d = SPLITTER * b 9070 var bbig = d - b 9071 var bhi = d - bbig 9072 var blo = b - bhi 9073 9074 var err1 = x - (ahi * bhi) 9075 var err2 = err1 - (alo * bhi) 9076 var err3 = err2 - (ahi * blo) 9077 9078 var y = alo * blo - err3 9079 9080 if(result) { 9081 result[0] = y 9082 result[1] = x 9083 return result 9084 } 9085 9086 return [ y, x ] 9087} 9088},{}],73:[function(require,module,exports){ 9089"use strict" 9090 9091module.exports = fastTwoSum 9092 9093function fastTwoSum(a, b, result) { 9094 var x = a + b 9095 var bv = x - a 9096 var av = x - bv 9097 var br = b - bv 9098 var ar = a - av 9099 if(result) { 9100 result[0] = ar + br 9101 result[1] = x 9102 return result 9103 } 9104 return [ar+br, x] 9105} 9106},{}],74:[function(require,module,exports){ 9107(function (global,Buffer){ 9108'use strict' 9109 9110var bits = require('bit-twiddle') 9111var dup = require('dup') 9112 9113//Legacy pool support 9114if(!global.__TYPEDARRAY_POOL) { 9115 global.__TYPEDARRAY_POOL = {
9116 UINT8 : dup([32, 0]) 9117 , UINT16 : dup([32, 0]) 9118 , UINT32 : dup([32, 0]) 9119 , INT8 : dup([32, 0]) 9120 , INT16 : dup([32, 0]) 9121 , INT32 : dup([32, 0]) 9122 , FLOAT : dup([32, 0]) 9123 , DOUBLE : dup([32, 0]) 9124 , DATA : dup([32, 0]) 9125 , UINT8C : dup([32, 0]) 9126 , BUFFER : dup([32, 0]) 9127 } 9128} 9129 9130var hasUint8C = (typeof Uint8ClampedArray) !== 'undefined' 9131var POOL = global.__TYPEDARRAY_POOL 9132 9133//Upgrade pool 9134if(!POOL.UINT8C) { 9135 POOL.UINT8C = dup([32, 0]) 9136} 9137if(!POOL.BUFFER) { 9138 POOL.BUFFER = dup([32, 0]) 9139} 9140 9141//New technique: Only allocate from ArrayBufferView and Buffer 9142var DATA = POOL.DATA 9143 , BUFFER = POOL.BUFFER 9144 9145exports.free = function free(array) { 9146 if(Buffer.isBuffer(array)) { 9147 BUFFER[bits.log2(array.length)].push(array) 9148 } else { 9149 if(Object.prototype.toString.call(array) !== '[object ArrayBuffer]') { 9150 array = array.buffer 9151 } 9152 if(!array) { 9153 return 9154 } 9155 var n = array.length || array.byteLength 9156 var log_n = bits.log2(n)|0 9157 DATA[log_n].push(array) 9158 } 9159} 9160 9161function freeArrayBuffer(buffer) { 9162 if(!buffer) { 9163 return 9164 } 9165 var n = buffer.length || buffer.byteLength 9166 var log_n = bits.log2(n) 9167 DATA[log_n].push(buffer) 9168} 9169 9170function freeTypedArray(array) { 9171 freeArrayBuffer(array.buffer) 9172} 9173 9174exports.freeUint8 = 9175exports.freeUint16 = 9176exports.freeUint32 = 9177exports.freeInt8 = 9178exports.freeInt16 = 9179exports.freeInt32 = 9180exports.freeFloat32 = 9181exports.freeFloat = 9182exports.freeFloat64 = 9183exports.freeDouble = 9184exports.freeUint8Clamped = 9185exports.freeDataView = freeTypedArray 9186 9187exports.freeArrayBuffer = freeArrayBuffer 9188 9189exports.freeBuffer = function freeBuffer(array) { 9190 BUFFER[bits.log2(array.length)].push(array) 9191} 9192 9193exports.malloc = function malloc(n, dtype) { 9194 if(dtype === undefined || dtype === 'arraybuffer') { 9195 return mallocArrayBuffer(n) 9196 } else { 9197 switch(dtype) { 9198 case 'uint8': 9199 return mallocUint8(n) 9200 case 'uint16': 9201 return mallocUint16(n) 9202 case 'uint32': 9203 return mallocUint32(n) 9204 case 'int8': 9205 return mallocInt8(n) 9206 case 'int16': 9207 return mallocInt16(n) 9208 case 'int32': 9209 return mallocInt32(n) 9210 case 'float': 9211 case 'float32': 9212 return mallocFloat(n) 9213 case 'double': 9214 case 'float64': 9215 return mallocDouble(n) 9216 case 'uint8_clamped': 9217 return mallocUint8Clamped(n) 9218 case 'buffer': 9219 return mallocBuffer(n) 9220 case 'data': 9221 case 'dataview': 9222 return mallocDataView(n) 9223 9224 default: 9225 return null 9226 } 9227 } 9228 return null 9229} 9230 9231function mallocArrayBuffer(n) { 9232 var n = bits.nextPow2(n) 9233 var log_n = bits.log2(n) 9234 var d = DATA[log_n] 9235 if(d.length > 0) { 9236 return d.pop() 9237 } 9238 return new ArrayBuffer(n) 9239} 9240exports.mallocArrayBuffer = mallocArrayBuffer 9241 9242function mallocUint8(n) { 9243 return new Uint8Array(mallocArrayBuffer(n), 0, n) 9244} 9245exports.mallocUint8 = mallocUint8 9246 9247function mallocUint16(n) { 9248 return new Uint16Array(mallocArrayBuffer(2*n), 0, n) 9249} 9250exports.mallocUint16 = mallocUint16 9251 9252function mallocUint32(n) { 9253 return new Uint32Array(mallocArrayBuffer(4*n), 0, n) 9254} 9255exports.mallocUint32 = mallocUint32 9256 9257function mallocInt8(n) { 9258 return new Int8Array(mallocArrayBuffer(n), 0, n) 9259} 9260exports.mallocInt8 = mallocInt8 9261 9262function mallocInt16(n) { 9263 return new Int16Array(mallocArrayBuffer(2*n), 0, n) 9264} 9265exports.mallocInt16 = mallocInt16 9266 9267function mallocInt32(n) { 9268 return new Int32Array(mallocArrayBuffer(4*n), 0, n) 9269} 9270exports.mallocInt32 = mallocInt32 9271 9272function mallocFloat(n) { 9273 return new Float32Array(mallocArrayBuffer(4*n), 0, n) 9274} 9275exports.mallocFloat32 = exports.mallocFloat = mallocFloat 9276 9277function mallocDouble(n) { 9278 return new Float64Array(mallocArrayBuffer(8*n), 0, n) 9279} 9280exports.mallocFloat64 = exports.mallocDouble = mallocDouble 9281 9282function mallocUint8Clamped(n) { 9283 if(hasUint8C) { 9284 return new Uint8ClampedArray(mallocArrayBuffer(n), 0, n) 9285 } else { 9286 return mallocUint8(n) 9287 } 9288} 9289exports.mallocUint8Clamped = mallocUint8Clamped 9290 9291function mallocDataView(n) { 9292 return new DataView(mallocArrayBuffer(n), 0, n) 9293} 9294exports.mallocDataView = mallocDataView 9295 9296function mallocBuffer(n) { 9297 n = bits.nextPow2(n) 9298 var log_n = bits.log2(n) 9299 var cache = BUFFER[log_n] 9300 if(cache.length > 0) { 9301 return cache.pop() 9302 } 9303 return new Buffer(n) 9304} 9305exports.mallocBuffer = mallocBuffer 9306 9307exports.clearCache = function clearCache() { 9308 for(var i=0; i<32; ++i) { 9309 POOL.UINT8[i].length = 0 9310 POOL.UINT16[i].length = 0 9311 POOL.UINT32[i].length = 0 9312 POOL.INT8[i].length = 0 9313 POOL.INT16[i].length = 0 9314 POOL.INT32[i].length = 0 9315 POOL.FLOAT[i].length = 0 9316 POOL.DOUBLE[i].length = 0 9317 POOL.UINT8C[i].length = 0 9318 DATA[i].length = 0 9319 BUFFER[i].length = 0 9320 } 9321} 9322}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("buffer").Buffer) 9323},{"bit-twiddle":22,"buffer":82,"dup":40}],75:[function(require,module,exports){ 9324"use strict"; "use restrict"; 9325 9326module.exports = UnionFind; 9327 9328function UnionFind(count) { 9329 this.roots = new Array(count); 9330 this.ranks = new Array(count); 9331 9332 for(var i=0; i<count; ++i) { 9333 this.roots[i] = i; 9334 this.ranks[i] = 0; 9335 } 9336} 9337 9338var proto = UnionFind.prototype 9339 9340Object.defineProperty(proto, "length", { 9341 "get": function() { 9342 return this.roots.length 9343 } 9344}) 9345 9346proto.makeSet = function() { 9347 var n = this.roots.length; 9348 this.roots.push(n); 9349 this.ranks.push(0); 9350 return n; 9351} 9352 9353proto.find = function(x) { 9354 var x0 = x 9355 var roots = this.roots; 9356 while(roots[x] !== x) { 9357 x = roots[x] 9358 } 9359 while(roots[x0] !== x) { 9360 var y = roots[x0] 9361 roots[x0] = x 9362 x0 = y 9363 } 9364 return x; 9365} 9366 9367proto.link = function(x, y) { 9368 var xr = this.find(x) 9369 , yr = this.find(y); 9370 if(xr === yr) { 9371 return; 9372 } 9373 var ranks = this.ranks 9374 , roots = this.roots 9375 , xd = ranks[xr] 9376 , yd = ranks[yr]; 9377 if(xd < yd) { 9378 roots[xr] = yr; 9379 } else if(yd < xd) {
9380 roots[yr] = xr; 9381 } else { 9382 roots[yr] = xr; 9383 ++ranks[xr]; 9384 } 9385} 9386},{}],76:[function(require,module,exports){ 9387module.exports = function range(min, max, value) { 9388 return (value - min) / (max - min) 9389} 9390},{}],77:[function(require,module,exports){ 9391module.exports = function vec2Copy(out, a) { 9392 out[0] = a[0] 9393 out[1] = a[1] 9394 return out 9395} 9396},{}],78:[function(require,module,exports){ 9397var window = require("global/window") 9398var once = require("once") 9399var parseHeaders = require('parse-headers') 9400 9401var messages = { 9402 "0": "Internal XMLHttpRequest Error", 9403 "4": "4xx Client Error", 9404 "5": "5xx Server Error" 9405} 9406 9407var XHR = window.XMLHttpRequest || noop 9408var XDR = "withCredentials" in (new XHR()) ? XHR : window.XDomainRequest 9409 9410module.exports = createXHR 9411 9412function createXHR(options, callback) { 9413 if (typeof options === "string") { 9414 options = { uri: options } 9415 } 9416 9417 options = options || {} 9418 callback = once(callback) 9419 9420 var xhr = options.xhr || null 9421 9422 if (!xhr) { 9423 if (options.cors || options.useXDR) { 9424 xhr = new XDR() 9425 }else{ 9426 xhr = new XHR() 9427 } 9428 } 9429 9430 var uri = xhr.url = options.uri || options.url 9431 var method = xhr.method = options.method || "GET" 9432 var body = options.body || options.data 9433 var headers = xhr.headers = options.headers || {} 9434 var sync = !!options.sync 9435 var isJson = false 9436 var key 9437 var load = options.response ? loadResponse : loadXhr 9438 9439 if ("json" in options) { 9440 isJson = true 9441 headers["Accept"] = "application/json" 9442 if (method !== "GET" && method !== "HEAD") { 9443 headers["Content-Type"] = "application/json" 9444 body = JSON.stringify(options.json) 9445 } 9446 } 9447 9448 xhr.onreadystatechange = readystatechange 9449 xhr.onload = load 9450 xhr.onerror = error 9451 // IE9 must have onprogress be set to a unique function. 9452 xhr.onprogress = function () { 9453 // IE must die 9454 } 9455 // hate IE 9456 xhr.ontimeout = noop 9457 xhr.open(method, uri, !sync) 9458 //backward compatibility 9459 if (options.withCredentials || (options.cors && options.withCredentials !== false)) { 9460 xhr.withCredentials = true 9461 } 9462 9463 // Cannot set timeout with sync request 9464 if (!sync) { 9465 xhr.timeout = "timeout" in options ? options.timeout : 5000 9466 } 9467 9468 if (xhr.setRequestHeader) { 9469 for(key in headers){ 9470 if(headers.hasOwnProperty(key)){ 9471 xhr.setRequestHeader(key, headers[key]) 9472 } 9473 } 9474 } else if (options.headers) { 9475 throw new Error("Headers cannot be set on an XDomainRequest object") 9476 } 9477 9478 if ("responseType" in options) { 9479 xhr.responseType = options.responseType 9480 } 9481 9482 if ("beforeSend" in options && 9483 typeof options.beforeSend === "function" 9484 ) { 9485 options.beforeSend(xhr) 9486 } 9487 9488 xhr.send(body) 9489 9490 return xhr 9491 9492 function readystatechange() { 9493 if (xhr.readyState === 4) { 9494 load() 9495 } 9496 } 9497 9498 function getBody() { 9499 // Chrome with requestType=blob throws errors arround when even testing access to responseText 9500 var body = null 9501 9502 if (xhr.response) { 9503 body = xhr.response 9504 } else if (xhr.responseType === 'text' || !xhr.responseType) { 9505 body = xhr.responseText || xhr.responseXML 9506 } 9507 9508 if (isJson) { 9509 try { 9510 body = JSON.parse(body) 9511 } catch (e) {} 9512 } 9513 9514 return body 9515 } 9516 9517 function getStatusCode() { 9518 return xhr.status === 1223 ? 204 : xhr.status 9519 } 9520 9521 // if we're getting a none-ok statusCode, build & return an error 9522 function errorFromStatusCode(status, body) { 9523 var error = null 9524 if (status === 0 || (status >= 400 && status < 600)) { 9525 var message = (typeof body === "string" ? body : false) || 9526 messages[String(status).charAt(0)] 9527 error = new Error(message) 9528 error.statusCode = status 9529 } 9530 9531 return error 9532 } 9533 9534 // will load the data & process the response in a special response object 9535 function loadResponse() { 9536 var status = getStatusCode() 9537 var body = getBody() 9538 var error = errorFromStatusCode(status, body) 9539 var response = { 9540 body: body, 9541 statusCode: status, 9542 statusText: xhr.statusText, 9543 raw: xhr 9544 } 9545 if(xhr.getAllResponseHeaders){ //remember xhr can in fact be XDR for CORS in IE 9546 response.headers = parseHeaders(xhr.getAllResponseHeaders()) 9547 } else { 9548 response.headers = {} 9549 } 9550 9551 callback(error, response, response.body) 9552 } 9553 9554 // will load the data and add some response properties to the source xhr 9555 // and then respond with that 9556 function loadXhr() { 9557 var status = getStatusCode() 9558 var error = errorFromStatusCode(status) 9559 9560 xhr.status = xhr.statusCode = status 9561 xhr.body = getBody() 9562 xhr.headers = parseHeaders(xhr.getAllResponseHeaders()) 9563 9564 callback(error, xhr, xhr.body) 9565 } 9566 9567 function error(evt) { 9568 callback(evt, xhr) 9569 } 9570} 9571 9572 9573function noop() {} 9574 9575}
9575,{"global/window":43,"once":51,"parse-headers":52}],79:[function(require,module,exports){ 9576module.exports = (function xmlparser() { 9577 //common browsers 9578 if (typeof self.DOMParser !== 'undefined') { 9579 return function(str) { 9580 var parser = new self.DOMParser() 9581 return parser.parseFromString(str, 'application/xml') 9582 } 9583 } 9584 9585 //IE8 fallback 9586 if (typeof self.ActiveXObject !== 'undefined' 9587 && new self.ActiveXObject('Microsoft.XMLDOM')) { 9588 return function(str) { 9589 var xmlDoc = new self.ActiveXObject("Microsoft.XMLDOM") 9590 xmlDoc.async = "false" 9591 xmlDoc.loadXML(str) 9592 return xmlDoc 9593 } 9594 } 9595 9596 //last resort fallback 9597 return function(str) { 9598 var div = document.createElement('div') 9599 div.innerHTML = str 9600 return div 9601 } 9602})() 9603 9604},{}],80:[function(require,module,exports){ 9605'use strict' 9606 9607exports.byteLength = byteLength 9608exports.toByteArray = toByteArray 9609exports.fromByteArray = fromByteArray 9610 9611var lookup = [] 9612var revLookup = [] 9613var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array 9614 9615var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' 9616for (var i = 0, len = code.length; i < len; ++i) { 9617 lookup[i] = code[i] 9618 revLookup[code.charCodeAt(i)] = i 9619} 9620 9621// Support decoding URL-safe base64 strings, as Node.js does. 9622// See: https://en.wikipedia.org/wiki/Base64#URL_applications 9623revLookup['-'.charCodeAt(0)] = 62 9624revLookup['_'.charCodeAt(0)] = 63 9625 9626function placeHoldersCount (b64) { 9627 var len = b64.length 9628 if (len % 4 > 0) { 9629 throw new Error('Invalid string. Length must be a multiple of 4') 9630 } 9631 9632 // the number of equal signs (place holders) 9633 // if there are two placeholders, than the two characters before it 9634 // represent one byte 9635 // if there is only one, then the three characters before it represent 2 bytes 9636 // this is just a cheap hack to not do indexOf twice 9637 return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 9638} 9639 9640function byteLength (b64) { 9641 // base64 is 4/3 + up to two characters of the original data 9642 return (b64.length * 3 / 4) - placeHoldersCount(b64) 9643} 9644 9645function toByteArray (b64) { 9646 var i, l, tmp, placeHolders, arr 9647 var len = b64.length 9648 placeHolders = placeHoldersCount(b64) 9649 9650 arr = new Arr((len * 3 / 4) - placeHolders) 9651 9652 // if there are placeholders, only get up to the last complete 4 chars 9653 l = placeHolders > 0 ? len - 4 : len 9654 9655 var L = 0 9656 9657 for (i = 0; i < l; i += 4) { 9658 tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] 9659 arr[L++] = (tmp >> 16) & 0xFF 9660 arr[L++] = (tmp >> 8) & 0xFF 9661 arr[L++] = tmp & 0xFF 9662 } 9663 9664 if (placeHolders === 2) { 9665 tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4) 9666 arr[L++] = tmp & 0xFF 9667 } else if (placeHolders === 1) { 9668 tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2) 9669 arr[L++] = (tmp >> 8) & 0xFF 9670 arr[L++] = tmp & 0xFF 9671 } 9672 9673 return arr 9674} 9675 9676function tripletToBase64 (num) { 9677 return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] 9678} 9679 9680function encodeChunk (uint8, start, end) { 9681 var tmp 9682 var output = [] 9683 for (var i = start; i < end; i += 3) { 9684 tmp = ((uint8[i] << 16) & 0xFF0000) + ((uint8[i + 1] << 8) & 0xFF00) + (uint8[i + 2] & 0xFF) 9685 output.push(tripletToBase64(tmp)) 9686 } 9687 return output.join('') 9688} 9689 9690function fromByteArray (uint8) { 9691 var tmp 9692 var len = uint8.length 9693 var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes 9694 var output = '' 9695 var parts = [] 9696 var maxChunkLength = 16383 // must be multiple of 3 9697 9698 // go through the array every three bytes, we'll deal with trailing stuff later 9699 for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { 9700 parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) 9701 } 9702 9703 // pad the end with zeros, but make sure to not forget the extra bytes 9704 if (extraBytes === 1) { 9705 tmp = uint8[len - 1] 9706 output += lookup[tmp >> 2] 9707 output += lookup[(tmp << 4) & 0x3F] 9708 output += '==' 9709 } else if (extraBytes === 2) { 9710 tmp = (uint8[len - 2] << 8) + (uint8[len - 1]) 9711 output += lookup[tmp >> 10] 9712 output += lookup[(tmp >> 4) & 0x3F] 9713 output += lookup[(tmp << 2) & 0x3F] 9714 output += '=' 9715 } 9716 9717 parts.push(output) 9718 9719 return parts.join('') 9720} 9721 9722},{}],81:[function(require,module,exports){ 9723 9724},{}],82:[function(require,module,exports){ 9725/*! 9726 * The buffer module from node.js, for the browser. 9727 * 9728 * @author Feross Aboukhadijeh <https://feross.org> 9729 * @license MIT 9730 */ 9731/* eslint-disable no-proto */ 9732 9733'use strict' 9734 9735var base64 = require('base64-js') 9736var ieee754 = require('ieee754') 9737 9738exports.Buffer = Buffer 9739exports.SlowBuffer = SlowBuffer 9740exports.INSPECT_MAX_BYTES = 50 9741 9742var K_MAX_LENGTH = 0x7fffffff
vendor: 5,710 bytes, lines 9743-9941
9743exports.kMaxLength = K_MAX_LENGTH 9744 9745/** 9746 * If `Buffer.TYPED_ARRAY_SUPPORT`: 9747 * === true Use Uint8Array implementation (fastest) 9748 * === false Print warning and recommend using `buffer` v4.x which has an Object 9749 * implementation (most compatible, even IE6) 9750 * 9751 * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, 9752 * Opera 11.6+, iOS 4.2+. 9753 * 9754 * We report that the browser does not support typed arrays if the are not subclassable 9755 * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array` 9756 * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support 9757 * for __proto__ and has a buggy typed array implementation. 9758 */ 9759Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport() 9760 9761if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && 9762 typeof console.error === 'function') { 9763 console.error( 9764 'This browser lacks typed array (Uint8Array) support which is required by ' + 9765 '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.' 9766 ) 9767} 9768 9769function typedArraySupport () { 9770 // Can typed array instances can be augmented? 9771 try { 9772 var arr = new Uint8Array(1) 9773 arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }} 9774 return arr.foo() === 42 9775 } catch (e) { 9776 return false 9777 } 9778} 9779 9780Object.defineProperty(Buffer.prototype, 'parent', { 9781 get: function () { 9782 if (!(this instanceof Buffer)) { 9783 return undefined 9784 } 9785 return this.buffer 9786 } 9787}) 9788 9789Object.defineProperty(Buffer.prototype, 'offset', { 9790 get: function () { 9791 if (!(this instanceof Buffer)) { 9792 return undefined 9793 } 9794 return this.byteOffset 9795 } 9796}) 9797 9798function createBuffer (length) { 9799 if (length > K_MAX_LENGTH) { 9800 throw new RangeError('Invalid typed array length') 9801 } 9802 // Return an augmented `Uint8Array` instance 9803 var buf = new Uint8Array(length) 9804 buf.__proto__ = Buffer.prototype 9805 return buf 9806} 9807 9808/** 9809 * The Buffer constructor returns instances of `Uint8Array` that have their 9810 * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of 9811 * `Uint8Array`, so the returned instances will have all the node `Buffer` methods 9812 * and the `Uint8Array` methods. Square bracket notation works as expected -- it 9813 * returns a single octet. 9814 * 9815 * The `Uint8Array` prototype remains unmodified. 9816 */ 9817 9818function Buffer (arg, encodingOrOffset, length) { 9819 // Common case. 9820 if (typeof arg === 'number') { 9821 if (typeof encodingOrOffset === 'string') { 9822 throw new Error( 9823 'If encoding is specified then the first argument must be a string' 9824 ) 9825 } 9826 return allocUnsafe(arg) 9827 } 9828 return from(arg, encodingOrOffset, length) 9829} 9830 9831// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 9832if (typeof Symbol !== 'undefined' && Symbol.species && 9833 Buffer[Symbol.species] === Buffer) { 9834 Object.defineProperty(Buffer, Symbol.species, { 9835 value: null, 9836 configurable: true, 9837 enumerable: false, 9838 writable: false 9839 }) 9840} 9841 9842Buffer.poolSize = 8192 // not used by this implementation 9843 9844function from (value, encodingOrOffset, length) { 9845 if (typeof value === 'number') { 9846 throw new TypeError('"value" argument must not be a number') 9847 } 9848 9849 if (isArrayBuffer(value) || (value && isArrayBuffer(value.buffer))) { 9850 return fromArrayBuffer(value, encodingOrOffset, length) 9851 } 9852 9853 if (typeof value === 'string') { 9854 return fromString(value, encodingOrOffset) 9855 } 9856 9857 return fromObject(value) 9858} 9859 9860/** 9861 * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError 9862 * if value is a number. 9863 * Buffer.from(str[, encoding]) 9864 * Buffer.from(array) 9865 * Buffer.from(buffer) 9866 * Buffer.from(arrayBuffer[, byteOffset[, length]]) 9867 **/ 9868Buffer.from = function (value, encodingOrOffset, length) { 9869 return from(value, encodingOrOffset, length) 9870} 9871 9872// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: 9873// https://github.com/feross/buffer/pull/148 9874Buffer.prototype.__proto__ = Uint8Array.prototype 9875Buffer.__proto__ = Uint8Array 9876 9877function assertSize (size) { 9878 if (typeof size !== 'number') { 9879 throw new TypeError('"size" argument must be of type number') 9880 } else if (size < 0) { 9881 throw new RangeError('"size" argument must not be negative') 9882 } 9883} 9884 9885function alloc (size, fill, encoding) { 9886 assertSize(size) 9887 if (size <= 0) { 9888 return createBuffer(size) 9889 } 9890 if (fill !== undefined) { 9891 // Only pay attention to encoding if it's a string. This 9892 // prevents accidentally sending in a number that would 9893 // be interpretted as a start offset. 9894 return typeof encoding === 'string' 9895 ? createBuffer(size).fill(fill, encoding) 9896 : createBuffer(size).fill(fill) 9897 } 9898 return createBuffer(size) 9899} 9900 9901/** 9902 * Creates a new filled Buffer instance. 9903 * alloc(size[, fill[, encoding]]) 9904 **/ 9905Buffer.alloc = function (size, fill, encoding) { 9906 return alloc(size, fill, encoding) 9907} 9908 9909function allocUnsafe (size) { 9910 assertSize(size) 9911 return createBuffer(size < 0 ? 0 : checked(size) | 0) 9912} 9913 9914/** 9915 * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. 9916 * */ 9917Buffer.allocUnsafe = function (size) { 9918 return allocUnsafe(size) 9919} 9920/** 9921 * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. 9922 */ 9923Buffer.allocUnsafeSlow = function (size) { 9924 return allocUnsafe(size) 9925} 9926 9927function fromString (string, encoding) { 9928 if (typeof encoding !== 'string' || encoding === '') { 9929 encoding = 'utf8' 9930 } 9931 9932 if (!Buffer.isEncoding(encoding)) { 9933 throw new TypeError('Unknown encoding: ' + encoding) 9934 } 9935 9936 var length = byteLength(string, encoding) | 0 9937 var buf = createBuffer(length) 9938 9939 var actual = buf.write(string, encoding) 9940 9941 if (actual !== length) {
vendor: 5,099 bytes, lines 9942-10156
9942 // Writing a hex string, for example, that contains invalid characters will 9943 // cause everything after the first invalid character to be ignored. (e.g. 9944 // 'abxxcd' will be treated as 'ab') 9945 buf = buf.slice(0, actual) 9946 } 9947 9948 return buf 9949} 9950 9951function fromArrayLike (array) { 9952 var length = array.length < 0 ? 0 : checked(array.length) | 0 9953 var buf = createBuffer(length) 9954 for (var i = 0; i < length; i += 1) { 9955 buf[i] = array[i] & 255 9956 } 9957 return buf 9958} 9959 9960function fromArrayBuffer (array, byteOffset, length) { 9961 if (byteOffset < 0 || array.byteLength < byteOffset) { 9962 throw new RangeError('"offset" is outside of buffer bounds') 9963 } 9964 9965 if (array.byteLength < byteOffset + (length || 0)) { 9966 throw new RangeError('"length" is outside of buffer bounds') 9967 } 9968 9969 var buf 9970 if (byteOffset === undefined && length === undefined) { 9971 buf = new Uint8Array(array) 9972 } else if (length === undefined) { 9973 buf = new Uint8Array(array, byteOffset) 9974 } else { 9975 buf = new Uint8Array(array, byteOffset, length) 9976 } 9977 9978 // Return an augmented `Uint8Array` instance 9979 buf.__proto__ = Buffer.prototype 9980 return buf 9981} 9982 9983function fromObject (obj) { 9984 if (Buffer.isBuffer(obj)) { 9985 var len = checked(obj.length) | 0 9986 var buf = createBuffer(len) 9987 9988 if (buf.length === 0) { 9989 return buf 9990 } 9991 9992 obj.copy(buf, 0, 0, len) 9993 return buf 9994 } 9995 9996 if (obj) { 9997 if (ArrayBuffer.isView(obj) || 'length' in obj) { 9998 if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) { 9999 return createBuffer(0) 10000 } 10001 return fromArrayLike(obj) 10002 } 10003 10004 if (obj.type === 'Buffer' && Array.isArray(obj.data)) { 10005 return fromArrayLike(obj.data) 10006 } 10007 } 10008 10009 throw new TypeError('The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object.') 10010} 10011 10012function checked (length) { 10013 // Note: cannot use `length < K_MAX_LENGTH` here because that fails when 10014 // length is NaN (which is otherwise coerced to zero.) 10015 if (length >= K_MAX_LENGTH) { 10016 throw new RangeError('Attempt to allocate Buffer larger than maximum ' + 10017 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes') 10018 } 10019 return length | 0 10020} 10021 10022function SlowBuffer (length) { 10023 if (+length != length) { // eslint-disable-line eqeqeq 10024 length = 0 10025 } 10026 return Buffer.alloc(+length) 10027} 10028 10029Buffer.isBuffer = function isBuffer (b) { 10030 return b != null && b._isBuffer === true 10031} 10032 10033Buffer.compare = function compare (a, b) { 10034 if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { 10035 throw new TypeError('Arguments must be Buffers') 10036 } 10037 10038 if (a === b) return 0 10039 10040 var x = a.length 10041 var y = b.length 10042 10043 for (var i = 0, len = Math.min(x, y); i < len; ++i) { 10044 if (a[i] !== b[i]) { 10045 x = a[i] 10046 y = b[i] 10047 break 10048 } 10049 } 10050 10051 if (x < y) return -1 10052 if (y < x) return 1 10053 return 0 10054} 10055 10056Buffer.isEncoding = function isEncoding (encoding) { 10057 switch (String(encoding).toLowerCase()) { 10058 case 'hex': 10059 case 'utf8': 10060 case 'utf-8': 10061 case 'ascii': 10062 case 'latin1': 10063 case 'binary': 10064 case 'base64': 10065 case 'ucs2': 10066 case 'ucs-2': 10067 case 'utf16le': 10068 case 'utf-16le': 10069 return true 10070 default: 10071 return false 10072 } 10073} 10074 10075Buffer.concat = function concat (list, length) { 10076 if (!Array.isArray(list)) { 10077 throw new TypeError('"list" argument must be an Array of Buffers') 10078 } 10079 10080 if (list.length === 0) { 10081 return Buffer.alloc(0) 10082 } 10083 10084 var i 10085 if (length === undefined) { 10086 length = 0 10087 for (i = 0; i < list.length; ++i) { 10088 length += list[i].length 10089 } 10090 } 10091 10092 var buffer = Buffer.allocUnsafe(length) 10093 var pos = 0 10094 for (i = 0; i < list.length; ++i) { 10095 var buf = list[i] 10096 if (ArrayBuffer.isView(buf)) { 10097 buf = Buffer.from(buf) 10098 } 10099 if (!Buffer.isBuffer(buf)) { 10100 throw new TypeError('"list" argument must be an Array of Buffers') 10101 } 10102 buf.copy(buffer, pos) 10103 pos += buf.length 10104 } 10105 return buffer 10106} 10107 10108function byteLength (string, encoding) { 10109 if (Buffer.isBuffer(string)) { 10110 return string.length 10111 } 10112 if (ArrayBuffer.isView(string) || isArrayBuffer(string)) { 10113 return string.byteLength 10114 } 10115 if (typeof string !== 'string') { 10116 string = '' + string 10117 } 10118 10119 var len = string.length 10120 if (len === 0) return 0 10121 10122 // Use a for loop to avoid recursion 10123 var loweredCase = false 10124 for (;;) { 10125 switch (encoding) { 10126 case 'ascii': 10127 case 'latin1': 10128 case 'binary': 10129 return len 10130 case 'utf8': 10131 case 'utf-8': 10132 case undefined: 10133 return utf8ToBytes(string).length 10134 case 'ucs2': 10135 case 'ucs-2': 10136 case 'utf16le': 10137 case 'utf-16le': 10138 return len * 2 10139 case 'hex': 10140 return len >>> 1 10141 case 'base64': 10142 return base64ToBytes(string).length 10143 default: 10144 if (loweredCase) return utf8ToBytes(string).length // assume utf8 10145 encoding = ('' + encoding).toLowerCase() 10146 loweredCase = true 10147 } 10148 } 10149} 10150Buffer.byteLength = byteLength 10151 10152function slowToString (encoding, start, end) { 10153 var loweredCase = false 10154 10155 // No need to verify that "this.length <= MAX_UINT32" since it's a read-only 10156 // property of a typed array.
vendor: 6,456 bytes, lines 10157-10402
10157 10158 // This behaves neither like String nor Uint8Array in that we set start/end 10159 // to their upper/lower bounds if the value passed is out of range. 10160 // undefined is handled specially as per ECMA-262 6th Edition, 10161 // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. 10162 if (start === undefined || start < 0) { 10163 start = 0 10164 } 10165 // Return early if start > this.length. Done here to prevent potential uint32 10166 // coercion fail below. 10167 if (start > this.length) { 10168 return '' 10169 } 10170 10171 if (end === undefined || end > this.length) { 10172 end = this.length 10173 } 10174 10175 if (end <= 0) { 10176 return '' 10177 } 10178 10179 // Force coersion to uint32. This will also coerce falsey/NaN values to 0. 10180 end >>>= 0 10181 start >>>= 0 10182 10183 if (end <= start) { 10184 return '' 10185 } 10186 10187 if (!encoding) encoding = 'utf8' 10188 10189 while (true) { 10190 switch (encoding) { 10191 case 'hex': 10192 return hexSlice(this, start, end) 10193 10194 case 'utf8': 10195 case 'utf-8': 10196 return utf8Slice(this, start, end) 10197 10198 case 'ascii': 10199 return asciiSlice(this, start, end) 10200 10201 case 'latin1': 10202 case 'binary': 10203 return latin1Slice(this, start, end) 10204 10205 case 'base64': 10206 return base64Slice(this, start, end) 10207 10208 case 'ucs2': 10209 case 'ucs-2': 10210 case 'utf16le': 10211 case 'utf-16le': 10212 return utf16leSlice(this, start, end) 10213 10214 default: 10215 if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) 10216 encoding = (encoding + '').toLowerCase() 10217 loweredCase = true 10218 } 10219 } 10220} 10221 10222// This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package) 10223// to detect a Buffer instance. It's not possible to use `instanceof Buffer` 10224// reliably in a browserify context because there could be multiple different 10225// copies of the 'buffer' package in use. This method works even for Buffer 10226// instances that were created from another copy of the `buffer` package. 10227// See: https://github.com/feross/buffer/issues/154 10228Buffer.prototype._isBuffer = true 10229 10230function swap (b, n, m) { 10231 var i = b[n] 10232 b[n] = b[m] 10233 b[m] = i 10234} 10235 10236Buffer.prototype.swap16 = function swap16 () { 10237 var len = this.length 10238 if (len % 2 !== 0) { 10239 throw new RangeError('Buffer size must be a multiple of 16-bits') 10240 } 10241 for (var i = 0; i < len; i += 2) { 10242 swap(this, i, i + 1) 10243 } 10244 return this 10245} 10246 10247Buffer.prototype.swap32 = function swap32 () { 10248 var len = this.length 10249 if (len % 4 !== 0) { 10250 throw new RangeError('Buffer size must be a multiple of 32-bits') 10251 } 10252 for (var i = 0; i < len; i += 4) { 10253 swap(this, i, i + 3) 10254 swap(this, i + 1, i + 2) 10255 } 10256 return this 10257} 10258 10259Buffer.prototype.swap64 = function swap64 () { 10260 var len = this.length 10261 if (len % 8 !== 0) { 10262 throw new RangeError('Buffer size must be a multiple of 64-bits') 10263 } 10264 for (var i = 0; i < len; i += 8) { 10265 swap(this, i, i + 7) 10266 swap(this, i + 1, i + 6) 10267 swap(this, i + 2, i + 5) 10268 swap(this, i + 3, i + 4) 10269 } 10270 return this 10271} 10272 10273Buffer.prototype.toString = function toString () { 10274 var length = this.length 10275 if (length === 0) return '' 10276 if (arguments.length === 0) return utf8Slice(this, 0, length) 10277 return slowToString.apply(this, arguments) 10278} 10279 10280Buffer.prototype.toLocaleString = Buffer.prototype.toString 10281 10282Buffer.prototype.equals = function equals (b) { 10283 if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') 10284 if (this === b) return true 10285 return Buffer.compare(this, b) === 0 10286} 10287 10288Buffer.prototype.inspect = function inspect () { 10289 var str = '' 10290 var max = exports.INSPECT_MAX_BYTES 10291 if (this.length > 0) { 10292 str = this.toString('hex', 0, max).match(/.{2}/g).join(' ') 10293 if (this.length > max) str += ' ... ' 10294 } 10295 return '<Buffer ' + str + '>' 10296} 10297 10298Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { 10299 if (!Buffer.isBuffer(target)) { 10300 throw new TypeError('Argument must be a Buffer') 10301 } 10302 10303 if (start === undefined) { 10304 start = 0 10305 } 10306 if (end === undefined) { 10307 end = target ? target.length : 0 10308 } 10309 if (thisStart === undefined) { 10310 thisStart = 0 10311 } 10312 if (thisEnd === undefined) { 10313 thisEnd = this.length 10314 } 10315 10316 if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { 10317 throw new RangeError('out of range index') 10318 } 10319 10320 if (thisStart >= thisEnd && start >= end) { 10321 return 0 10322 } 10323 if (thisStart >= thisEnd) { 10324 return -1 10325 } 10326 if (start >= end) { 10327 return 1 10328 } 10329 10330 start >>>= 0 10331 end >>>= 0 10332 thisStart >>>= 0 10333 thisEnd >>>= 0 10334 10335 if (this === target) return 0 10336 10337 var x = thisEnd - thisStart 10338 var y = end - start 10339 var len = Math.min(x, y) 10340 10341 var thisCopy = this.slice(thisStart, thisEnd) 10342 var targetCopy = target.slice(start, end) 10343 10344 for (var i = 0; i < len; ++i) { 10345 if (thisCopy[i] !== targetCopy[i]) { 10346 x = thisCopy[i] 10347 y = targetCopy[i] 10348 break 10349 } 10350 } 10351 10352 if (x < y) return -1 10353 if (y < x) return 1 10354 return 0 10355} 10356 10357// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`, 10358// OR the last index of `val` in `buffer` at offset <= `byteOffset`. 10359// 10360// Arguments: 10361// - buffer - a Buffer to search 10362// - val - a string, Buffer, or number 10363// - byteOffset - an index into `buffer`; will be clamped to an int32 10364// - encoding - an optional encoding, relevant is val is a string 10365// - dir - true for indexOf, false for lastIndexOf 10366function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { 10367 // Empty buffer means no match 10368 if (buffer.length === 0) return -1 10369 10370 // Normalize byteOffset 10371 if (typeof byteOffset === 'string') { 10372 encoding = byteOffset 10373 byteOffset = 0 10374 } else if (byteOffset > 0x7fffffff) { 10375 byteOffset = 0x7fffffff 10376 } else if (byteOffset < -0x80000000) { 10377 byteOffset = -0x80000000 10378 } 10379 byteOffset = +byteOffset // Coerce to Number. 10380 if (numberIsNaN(byteOffset)) { 10381 // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer 10382 byteOffset = dir ? 0 : (buffer.length - 1) 10383 } 10384 10385 // Normalize byteOffset: negative offsets start from the end of the buffer 10386 if (byteOffset < 0) byteOffset = buffer.length + byteOffset 10387 if (byteOffset >= buffer.length) { 10388 if (dir) return -1 10389 else byteOffset = buffer.length - 1 10390 } else if (byteOffset < 0) { 10391 if (dir) byteOffset = 0 10392 else return -1 10393 } 10394 10395 // Normalize val 10396 if (typeof val === 'string') { 10397 val = Buffer.from(val, encoding) 10398 } 10399 10400 // Finally, search either indexOf (if dir is true) or lastIndexOf 10401 if (Buffer.isBuffer(val)) { 10402 // Special case: looking for empty string/buffer always fail
vendor: 6,171 bytes, lines 10402-10631
10402s 10403 if (val.length === 0) { 10404 return -1 10405 } 10406 return arrayIndexOf(buffer, val, byteOffset, encoding, dir) 10407 } else if (typeof val === 'number') { 10408 val = val & 0xFF // Search for a byte value [0-255] 10409 if (typeof Uint8Array.prototype.indexOf === 'function') { 10410 if (dir) { 10411 return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset) 10412 } else { 10413 return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset) 10414 } 10415 } 10416 return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir) 10417 } 10418 10419 throw new TypeError('val must be string, number or Buffer') 10420} 10421 10422function arrayIndexOf (arr, val, byteOffset, encoding, dir) { 10423 var indexSize = 1 10424 var arrLength = arr.length 10425 var valLength = val.length 10426 10427 if (encoding !== undefined) { 10428 encoding = String(encoding).toLowerCase() 10429 if (encoding === 'ucs2' || encoding === 'ucs-2' || 10430 encoding === 'utf16le' || encoding === 'utf-16le') { 10431 if (arr.length < 2 || val.length < 2) { 10432 return -1 10433 } 10434 indexSize = 2 10435 arrLength /= 2 10436 valLength /= 2 10437 byteOffset /= 2 10438 } 10439 } 10440 10441 function read (buf, i) { 10442 if (indexSize === 1) { 10443 return buf[i] 10444 } else { 10445 return buf.readUInt16BE(i * indexSize) 10446 } 10447 } 10448 10449 var i 10450 if (dir) { 10451 var foundIndex = -1 10452 for (i = byteOffset; i < arrLength; i++) { 10453 if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { 10454 if (foundIndex === -1) foundIndex = i 10455 if (i - foundIndex + 1 === valLength) return foundIndex * indexSize 10456 } else { 10457 if (foundIndex !== -1) i -= i - foundIndex 10458 foundIndex = -1 10459 } 10460 } 10461 } else { 10462 if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength 10463 for (i = byteOffset; i >= 0; i--) { 10464 var found = true 10465 for (var j = 0; j < valLength; j++) { 10466 if (read(arr, i + j) !== read(val, j)) { 10467 found = false 10468 break 10469 } 10470 } 10471 if (found) return i 10472 } 10473 } 10474 10475 return -1 10476} 10477 10478Buffer.prototype.includes = function includes (val, byteOffset, encoding) { 10479 return this.indexOf(val, byteOffset, encoding) !== -1 10480} 10481 10482Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { 10483 return bidirectionalIndexOf(this, val, byteOffset, encoding, true) 10484} 10485 10486Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) { 10487 return bidirectionalIndexOf(this, val, byteOffset, encoding, false) 10488} 10489 10490function hexWrite (buf, string, offset, length) { 10491 offset = Number(offset) || 0 10492 var remaining = buf.length - offset 10493 if (!length) { 10494 length = remaining 10495 } else { 10496 length = Number(length) 10497 if (length > remaining) { 10498 length = remaining 10499 } 10500 } 10501 10502 var strLen = string.length 10503 10504 if (length > strLen / 2) { 10505 length = strLen / 2 10506 } 10507 for (var i = 0; i < length; ++i) { 10508 var parsed = parseInt(string.substr(i * 2, 2), 16) 10509 if (numberIsNaN(parsed)) return i 10510 buf[offset + i] = parsed 10511 } 10512 return i 10513} 10514 10515function utf8Write (buf, string, offset, length) { 10516 return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) 10517} 10518 10519function asciiWrite (buf, string, offset, length) { 10520 return blitBuffer(asciiToBytes(string), buf, offset, length) 10521} 10522 10523function latin1Write (buf, string, offset, length) { 10524 return asciiWrite(buf, string, offset, length) 10525} 10526 10527function base64Write (buf, string, offset, length) { 10528 return blitBuffer(base64ToBytes(string), buf, offset, length) 10529} 10530 10531function ucs2Write (buf, string, offset, length) { 10532 return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) 10533} 10534 10535Buffer.prototype.write = function write (string, offset, length, encoding) { 10536 // Buffer#write(string) 10537 if (offset === undefined) { 10538 encoding = 'utf8' 10539 length = this.length 10540 offset = 0 10541 // Buffer#write(string, encoding) 10542 } else if (length === undefined && typeof offset === 'string') { 10543 encoding = offset 10544 length = this.length 10545 offset = 0 10546 // Buffer#write(string, offset[, length][, encoding]) 10547 } else if (isFinite(offset)) { 10548 offset = offset >>> 0 10549 if (isFinite(length)) { 10550 length = length >>> 0 10551 if (encoding === undefined) encoding = 'utf8' 10552 } else { 10553 encoding = length 10554 length = undefined 10555 } 10556 } else { 10557 throw new Error( 10558 'Buffer.write(string, encoding, offset[, length]) is no longer supported' 10559 ) 10560 } 10561 10562 var remaining = this.length - offset 10563 if (length === undefined || length > remaining) length = remaining 10564 10565 if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { 10566 throw new RangeError('Attempt to write outside buffer bounds') 10567 } 10568 10569 if (!encoding) encoding = 'utf8' 10570 10571 var loweredCase = false 10572 for (;;) { 10573 switch (encoding) { 10574 case 'hex': 10575 return hexWrite(this, string, offset, length) 10576 10577 case 'utf8': 10578 case 'utf-8': 10579 return utf8Write(this, string, offset, length) 10580 10581 case 'ascii': 10582 return asciiWrite(this, string, offset, length) 10583 10584 case 'latin1': 10585 case 'binary': 10586 return latin1Write(this, string, offset, length) 10587 10588 case 'base64': 10589 // Warning: maxLength not taken into account in base64Write 10590 return base64Write(this, string, offset, length) 10591 10592 case 'ucs2': 10593 case 'ucs-2': 10594 case 'utf16le': 10595 case 'utf-16le': 10596 return ucs2Write(this, string, offset, length) 10597 10598 default: 10599 if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) 10600 encoding = ('' + encoding).toLowerCase() 10601 loweredCase = true 10602 } 10603 } 10604} 10605 10606Buffer.prototype.toJSON = function toJSON () { 10607 return { 10608 type: 'Buffer', 10609 data: Array.prototype.slice.call(this._arr || this, 0) 10610 } 10611} 10612 10613function base64Slice (buf, start, end) { 10614 if (start === 0 && end === buf.length) { 10615 return base64.fromByteArray(buf) 10616 } else { 10617 return base64.fromByteArray(buf.slice(start, end)) 10618 } 10619} 10620 10621function utf8Slice (buf, start, end) { 10622 end = Math.min(buf.length, end) 10623 var res = [] 10624 10625 var i = start 10626 while (i < end) { 10627 var firstByte = buf[i] 10628 var codePoint = null 10629 var bytesPerSequence = (firstByte > 0xEF) ? 4 10630 : (firstByte > 0xDF) ? 3 10631 : (firstByte > 0xBF) ? 2
vendor: 19,139 bytes, lines 10632-11295
10632 : 1 10633 10634 if (i + bytesPerSequence <= end) { 10635 var secondByte, thirdByte, fourthByte, tempCodePoint 10636 10637 switch (bytesPerSequence) { 10638 case 1: 10639 if (firstByte < 0x80) { 10640 codePoint = firstByte 10641 } 10642 break 10643 case 2: 10644 secondByte = buf[i + 1] 10645 if ((secondByte & 0xC0) === 0x80) { 10646 tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) 10647 if (tempCodePoint > 0x7F) { 10648 codePoint = tempCodePoint 10649 } 10650 } 10651 break 10652 case 3: 10653 secondByte = buf[i + 1] 10654 thirdByte = buf[i + 2] 10655 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { 10656 tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) 10657 if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { 10658 codePoint = tempCodePoint 10659 } 10660 } 10661 break 10662 case 4: 10663 secondByte = buf[i + 1] 10664 thirdByte = buf[i + 2] 10665 fourthByte = buf[i + 3] 10666 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { 10667 tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) 10668 if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { 10669 codePoint = tempCodePoint 10670 } 10671 } 10672 } 10673 } 10674 10675 if (codePoint === null) { 10676 // we did not generate a valid codePoint so insert a 10677 // replacement char (U+FFFD) and advance only 1 byte 10678 codePoint = 0xFFFD 10679 bytesPerSequence = 1 10680 } else if (codePoint > 0xFFFF) { 10681 // encode to utf16 (surrogate pair dance) 10682 codePoint -= 0x10000 10683 res.push(codePoint >>> 10 & 0x3FF | 0xD800) 10684 codePoint = 0xDC00 | codePoint & 0x3FF 10685 } 10686 10687 res.push(codePoint) 10688 i += bytesPerSequence 10689 } 10690 10691 return decodeCodePointsArray(res) 10692} 10693 10694// Based on http://stackoverflow.com/a/22747272/680742, the browser with 10695// the lowest limit is Chrome, with 0x10000 args. 10696// We go 1 magnitude less, for safety 10697var MAX_ARGUMENTS_LENGTH = 0x1000 10698 10699function decodeCodePointsArray (codePoints) { 10700 var len = codePoints.length 10701 if (len <= MAX_ARGUMENTS_LENGTH) { 10702 return String.fromCharCode.apply(String, codePoints) // avoid extra slice() 10703 } 10704 10705 // Decode in chunks to avoid "call stack size exceeded". 10706 var res = '' 10707 var i = 0 10708 while (i < len) { 10709 res += String.fromCharCode.apply( 10710 String, 10711 codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) 10712 ) 10713 } 10714 return res 10715} 10716 10717function asciiSlice (buf, start, end) { 10718 var ret = '' 10719 end = Math.min(buf.length, end) 10720 10721 for (var i = start; i < end; ++i) { 10722 ret += String.fromCharCode(buf[i] & 0x7F) 10723 } 10724 return ret 10725} 10726 10727function latin1Slice (buf, start, end) { 10728 var ret = '' 10729 end = Math.min(buf.length, end) 10730 10731 for (var i = start; i < end; ++i) { 10732 ret += String.fromCharCode(buf[i]) 10733 } 10734 return ret 10735} 10736 10737function hexSlice (buf, start, end) { 10738 var len = buf.length 10739 10740 if (!start || start < 0) start = 0 10741 if (!end || end < 0 || end > len) end = len 10742 10743 var out = '' 10744 for (var i = start; i < end; ++i) { 10745 out += toHex(buf[i]) 10746 } 10747 return out 10748} 10749 10750function utf16leSlice (buf, start, end) { 10751 var bytes = buf.slice(start, end) 10752 var res = '' 10753 for (var i = 0; i < bytes.length; i += 2) { 10754 res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)) 10755 } 10756 return res 10757} 10758 10759Buffer.prototype.slice = function slice (start, end) { 10760 var len = this.length 10761 start = ~~start 10762 end = end === undefined ? len : ~~end 10763 10764 if (start < 0) { 10765 start += len 10766 if (start < 0) start = 0 10767 } else if (start > len) { 10768 start = len 10769 } 10770 10771 if (end < 0) { 10772 end += len 10773 if (end < 0) end = 0 10774 } else if (end > len) { 10775 end = len 10776 } 10777 10778 if (end < start) end = start 10779 10780 var newBuf = this.subarray(start, end) 10781 // Return an augmented `Uint8Array` instance 10782 newBuf.__proto__ = Buffer.prototype 10783 return newBuf 10784} 10785 10786/* 10787 * Need to make sure that buffer isn't trying to write out of bounds. 10788 */ 10789function checkOffset (offset, ext, length) { 10790 if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') 10791 if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') 10792} 10793 10794Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { 10795 offset = offset >>> 0 10796 byteLength = byteLength >>> 0 10797 if (!noAssert) checkOffset(offset, byteLength, this.length) 10798 10799 var val = this[offset] 10800 var mul = 1 10801 var i = 0 10802 while (++i < byteLength && (mul *= 0x100)) { 10803 val += this[offset + i] * mul 10804 } 10805 10806 return val 10807} 10808 10809Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { 10810 offset = offset >>> 0 10811 byteLength = byteLength >>> 0 10812 if (!noAssert) { 10813 checkOffset(offset, byteLength, this.length) 10814 } 10815 10816 var val = this[offset + --byteLength] 10817 var mul = 1 10818 while (byteLength > 0 && (mul *= 0x100)) { 10819 val += this[offset + --byteLength] * mul 10820 } 10821 10822 return val 10823} 10824 10825Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { 10826 offset = offset >>> 0 10827 if (!noAssert) checkOffset(offset, 1, this.length) 10828 return this[offset] 10829} 10830 10831Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { 10832 offset = offset >>> 0 10833 if (!noAssert) checkOffset(offset, 2, this.length) 10834 return this[offset] | (this[offset + 1] << 8) 10835} 10836 10837Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { 10838 offset = offset >>> 0 10839 if (!noAssert) checkOffset(offset, 2, this.length) 10840 return (this[offset] << 8) | this[offset + 1] 10841} 10842 10843Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { 10844 offset = offset >>> 0 10845 if (!noAssert) checkOffset(offset, 4, this.length) 10846 10847 return ((this[offset]) | 10848 (this[offset + 1] << 8) | 10849 (this[offset + 2] << 16)) + 10850 (this[offset + 3] * 0x1000000) 10851} 10852 10853Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { 10854 offset = offset >>> 0 10855 if (!noAssert) checkOffset(offset, 4, this.length) 10856 10857 return (this[offset] * 0x1000000) + 10858 ((this[offset + 1] << 16) | 10859 (this[offset + 2] << 8) | 10860 this[offset + 3]) 10861} 10862 10863Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { 10864 offset = offset >>> 0 10865 byteLength = byteLength >>> 0 10866 if (!noAssert) checkOffset(offset, byteLength, this.length) 10867 10868 var val = this[offset] 10869 var mul = 1 10870 var i = 0 10871 while (++i < byteLength && (mul *= 0x100)) { 10872 val += this[offset + i] * mul 10873 } 10874 mul *= 0x80 10875 10876 if (val >= mul) val -= Math.pow(2, 8 * byteLength) 10877 10878 return val 10879} 10880 10881Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { 10882 offset = offset >>> 0 10883 byteLength = byteLength >>> 0 10884 if (!noAssert) checkOffset(offset, byteLength, this.length) 10885 10886 var i = byteLength 10887 var mul = 1 10888 var val = this[offset + --i] 10889 while (i > 0 && (mul *= 0x100)) { 10890 val += this[offset + --i] * mul 10891 } 10892 mul *= 0x80 10893 10894 if (val >= mul) val -= Math.pow(2, 8 * byteLength) 10895 10896 return val 10897} 10898 10899Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { 10900 offset = offset >>> 0 10901 if (!noAssert) checkOffset(offset, 1, this.length) 10902 if (!(this[offset] & 0x80)) return (this[offset]) 10903 return ((0xff - this[offset] + 1) * -1) 10904} 10905 10906Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { 10907 offset = offset >>> 0 10908 if (!noAssert) checkOffset(offset, 2, this.length) 10909 var val = this[offset] | (this[offset + 1] << 8) 10910 return (val & 0x8000) ? val | 0xFFFF0000 : val 10911} 10912 10913Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { 10914 offset = offset >>> 0 10915 if (!noAssert) checkOffset(offset, 2, this.length) 10916 var val = this[offset + 1] | (this[offset] << 8) 10917 return (val & 0x8000) ? val | 0xFFFF0000 : val 10918} 10919 10920Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { 10921 offset = offset >>> 0 10922 if (!noAssert) checkOffset(offset, 4, this.length) 10923 10924 return (this[offset]) | 10925 (this[offset + 1] << 8) | 10926 (this[offset + 2] << 16) | 10927 (this[offset + 3] << 24) 10928} 10929 10930Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { 10931 offset = offset >>> 0 10932 if (!noAssert) checkOffset(offset, 4, this.length) 10933 10934 return (this[offset] << 24) | 10935 (this[offset + 1] << 16) | 10936 (this[offset + 2] << 8) | 10937 (this[offset + 3]) 10938} 10939 10940Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { 10941 offset = offset >>> 0 10942 if (!noAssert) checkOffset(offset, 4, this.length) 10943 return ieee754.read(this, offset, true, 23, 4) 10944} 10945 10946Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { 10947 offset = offset >>> 0 10948 if (!noAssert) checkOffset(offset, 4, this.length) 10949 return ieee754.read(this, offset, false, 23, 4) 10950} 10951 10952Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { 10953 offset = offset >>> 0 10954 if (!noAssert) checkOffset(offset, 8, this.length) 10955 return ieee754.read(this, offset, true, 52, 8) 10956} 10957 10958Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { 10959 offset = offset >>> 0 10960 if (!noAssert) checkOffset(offset, 8, this.length) 10961 return ieee754.read(this, offset, false, 52, 8) 10962} 10963 10964function checkInt (buf, value, offset, ext, max, min) { 10965 if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') 10966 if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') 10967 if (offset + ext > buf.length) throw new RangeError('Index out of range') 10968} 10969 10970Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { 10971 value = +value 10972 offset = offset >>> 0 10973 byteLength = byteLength >>> 0 10974 if (!noAssert) { 10975 var maxBytes = Math.pow(2, 8 * byteLength) - 1 10976 checkInt(this, value, offset, byteLength, maxBytes, 0) 10977 } 10978 10979 var mul = 1 10980 var i = 0 10981 this[offset] = value & 0xFF 10982 while (++i < byteLength && (mul *= 0x100)) { 10983 this[offset + i] = (value / mul) & 0xFF 10984 } 10985 10986 return offset + byteLength 10987} 10988 10989Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { 10990 value = +value 10991 offset = offset >>> 0 10992 byteLength = byteLength >>> 0 10993 if (!noAssert) { 10994 var maxBytes = Math.pow(2, 8 * byteLength) - 1 10995 checkInt(this, value, offset, byteLength, maxBytes, 0) 10996 } 10997 10998 var i = byteLength - 1 10999 var mul = 1 11000 this[offset + i] = value & 0xFF 11001 while (--i >= 0 && (mul *= 0x100)) { 11002 this[offset + i] = (value / mul) & 0xFF 11003 } 11004 11005 return offset + byteLength 11006} 11007 11008Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { 11009 value = +value 11010 offset = offset >>> 0 11011 if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) 11012 this[offset] = (value & 0xff) 11013 return offset + 1 11014} 11015 11016Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { 11017 value = +value 11018 offset = offset >>> 0 11019 if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) 11020 this[offset] = (value & 0xff) 11021 this[offset + 1] = (value >>> 8) 11022 return offset + 2 11023} 11024 11025Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { 11026 value = +value 11027 offset = offset >>> 0 11028 if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) 11029 this[offset] = (value >>> 8) 11030 this[offset + 1] = (value & 0xff) 11031 return offset + 2 11032} 11033 11034Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { 11035 value = +value 11036 offset = offset >>> 0 11037 if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) 11038 this[offset + 3] = (value >>> 24) 11039 this[offset + 2] = (value >>> 16) 11040 this[offset + 1] = (value >>> 8) 11041 this[offset] = (value & 0xff) 11042 return offset + 4 11043} 11044 11045Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { 11046 value = +value 11047 offset = offset >>> 0 11048 if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) 11049 this[offset] = (value >>> 24) 11050 this[offset + 1] = (value >>> 16) 11051 this[offset + 2] = (value >>> 8) 11052 this[offset + 3] = (value & 0xff) 11053 return offset + 4 11054} 11055 11056Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { 11057 value = +value 11058 offset = offset >>> 0 11059 if (!noAssert) { 11060 var limit = Math.pow(2, (8 * byteLength) - 1) 11061 11062 checkInt(this, value, offset, byteLength, limit - 1, -limit) 11063 } 11064 11065 var i = 0 11066 var mul = 1 11067 var sub = 0 11068 this[offset] = value & 0xFF 11069 while (++i < byteLength && (mul *= 0x100)) { 11070 if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { 11071 sub = 1 11072 } 11073 this[offset + i] = ((value / mul) >> 0) - sub & 0xFF 11074 } 11075 11076 return offset + byteLength 11077} 11078 11079Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { 11080 value = +value 11081 offset = offset >>> 0 11082 if (!noAssert) { 11083 var limit = Math.pow(2, (8 * byteLength) - 1) 11084 11085 checkInt(this, value, offset, byteLength, limit - 1, -limit) 11086 } 11087 11088 var i = byteLength - 1 11089 var mul = 1 11090 var sub = 0 11091 this[offset + i] = value & 0xFF 11092 while (--i >= 0 && (mul *= 0x100)) { 11093 if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { 11094 sub = 1 11095 } 11096 this[offset + i] = ((value / mul) >> 0) - sub & 0xFF 11097 } 11098 11099 return offset + byteLength 11100} 11101 11102Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { 11103 value = +value 11104 offset = offset >>> 0 11105 if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) 11106 if (value < 0) value = 0xff + value + 1 11107 this[offset] = (value & 0xff) 11108 return offset + 1 11109} 11110 11111Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { 11112 value = +value 11113 offset = offset >>> 0 11114 if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) 11115 this[offset] = (value & 0xff) 11116 this[offset + 1] = (value >>> 8) 11117 return offset + 2 11118} 11119 11120Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { 11121 value = +value 11122 offset = offset >>> 0 11123 if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) 11124 this[offset] = (value >>> 8) 11125 this[offset + 1] = (value & 0xff) 11126 return offset + 2 11127} 11128 11129Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { 11130 value = +value 11131 offset = offset >>> 0 11132 if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) 11133 this[offset] = (value & 0xff) 11134 this[offset + 1] = (value >>> 8) 11135 this[offset + 2] = (value >>> 16) 11136 this[offset + 3] = (value >>> 24) 11137 return offset + 4 11138} 11139 11140Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { 11141 value = +value 11142 offset = offset >>> 0 11143 if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) 11144 if (value < 0) value = 0xffffffff + value + 1 11145 this[offset] = (value >>> 24) 11146 this[offset + 1] = (value >>> 16) 11147 this[offset + 2] = (value >>> 8) 11148 this[offset + 3] = (value & 0xff) 11149 return offset + 4 11150} 11151 11152function checkIEEE754 (buf, value, offset, ext, max, min) { 11153 if (offset + ext > buf.length) throw new RangeError('Index out of range') 11154 if (offset < 0) throw new RangeError('Index out of range') 11155} 11156 11157function writeFloat (buf, value, offset, littleEndian, noAssert) { 11158 value = +value 11159 offset = offset >>> 0 11160 if (!noAssert) { 11161 checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) 11162 } 11163 ieee754.write(buf, value, offset, littleEndian, 23, 4) 11164 return offset + 4 11165} 11166 11167Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { 11168 return writeFloat(this, value, offset, true, noAssert) 11169} 11170 11171Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { 11172 return writeFloat(this, value, offset, false, noAssert) 11173} 11174 11175function writeDouble (buf, value, offset, littleEndian, noAssert) { 11176 value = +value 11177 offset = offset >>> 0 11178 if (!noAssert) { 11179 checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) 11180 } 11181 ieee754.write(buf, value, offset, littleEndian, 52, 8) 11182 return offset + 8 11183} 11184 11185Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { 11186 return writeDouble(this, value, offset, true, noAssert) 11187} 11188 11189Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { 11190 return writeDouble(this, value, offset, false, noAssert) 11191} 11192 11193// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) 11194Buffer.prototype.copy = function copy (target, targetStart, start, end) { 11195 if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer') 11196 if (!start) start = 0 11197 if (!end && end !== 0) end = this.length 11198 if (targetStart >= target.length) targetStart = target.length 11199 if (!targetStart) targetStart = 0 11200 if (end > 0 && end < start) end = start 11201 11202 // Copy 0 bytes; we're done 11203 if (end === start) return 0 11204 if (target.length === 0 || this.length === 0) return 0 11205 11206 // Fatal error conditions 11207 if (targetStart < 0) { 11208 throw new RangeError('targetStart out of bounds') 11209 } 11210 if (start < 0 || start >= this.length) throw new RangeError('Index out of range') 11211 if (end < 0) throw new RangeError('sourceEnd out of bounds') 11212 11213 // Are we oob? 11214 if (end > this.length) end = this.length 11215 if (target.length - targetStart < end - start) { 11216 end = target.length - targetStart + start 11217 } 11218 11219 var len = end - start 11220 11221 if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') { 11222 // Use built-in when available, missing from IE11 11223 this.copyWithin(targetStart, start, end) 11224 } else if (this === target && start < targetStart && targetStart < end) { 11225 // descending copy from end 11226 for (var i = len - 1; i >= 0; --i) { 11227 target[i + targetStart] = this[i + start] 11228 } 11229 } else { 11230 Uint8Array.prototype.set.call( 11231 target, 11232 this.subarray(start, end), 11233 targetStart 11234 ) 11235 } 11236 11237 return len 11238} 11239 11240// Usage: 11241// buffer.fill(number[, offset[, end]]) 11242// buffer.fill(buffer[, offset[, end]]) 11243// buffer.fill(string[, offset[, end]][, encoding]) 11244Buffer.prototype.fill = function fill (val, start, end, encoding) { 11245 // Handle string cases: 11246 if (typeof val === 'string') { 11247 if (typeof start === 'string') { 11248 encoding = start 11249 start = 0 11250 end = this.length 11251 } else if (typeof end === 'string') { 11252 encoding = end 11253 end = this.length 11254 } 11255 if (encoding !== undefined && typeof encoding !== 'string') { 11256 throw new TypeError('encoding must be a string') 11257 } 11258 if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { 11259 throw new TypeError('Unknown encoding: ' + encoding) 11260 } 11261 if (val.length === 1) { 11262 var code = val.charCodeAt(0) 11263 if ((encoding === 'utf8' && code < 128) || 11264 encoding === 'latin1') { 11265 // Fast path: If `val` fits into a single byte, use that numeric value. 11266 val = code 11267 } 11268 } 11269 } else if (typeof val === 'number') { 11270 val = val & 255 11271 } 11272 11273 // Invalid ranges are not set to a default, so can range check early. 11274 if (start < 0 || this.length < start || this.length < end) { 11275 throw new RangeError('Out of range index') 11276 } 11277 11278 if (end <= start) { 11279 return this 11280 } 11281 11282 start = start >>> 0 11283 end = end === undefined ? this.length : end >>> 0 11284 11285 if (!val) val = 0 11286 11287 var i 11288 if (typeof val === 'number') { 11289 for (i = start; i < end; ++i) { 11290 this[i] = val 11291 } 11292 } else { 11293 var bytes = Buffer.isBuffer(val) 11294 ? val 11295 :
11295new Buffer(val, encoding) 11296 var len = bytes.length 11297 if (len === 0) { 11298 throw new TypeError('The value "' + val +
vendor: 4,203 bytes, lines 11299-11462
11299 '" is invalid for argument "value"') 11300 } 11301 for (i = 0; i < end - start; ++i) { 11302 this[i + start] = bytes[i % len] 11303 } 11304 } 11305 11306 return this 11307} 11308 11309// HELPER FUNCTIONS 11310// ================ 11311 11312var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g 11313 11314function base64clean (str) { 11315 // Node takes equal signs as end of the Base64 encoding 11316 str = str.split('=')[0] 11317 // Node strips out invalid characters like \n and \t from the string, base64-js does not 11318 str = str.trim().replace(INVALID_BASE64_RE, '') 11319 // Node converts strings with length < 2 to '' 11320 if (str.length < 2) return '' 11321 // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not 11322 while (str.length % 4 !== 0) { 11323 str = str + '=' 11324 } 11325 return str 11326} 11327 11328function toHex (n) { 11329 if (n < 16) return '0' + n.toString(16) 11330 return n.toString(16) 11331} 11332 11333function utf8ToBytes (string, units) { 11334 units = units || Infinity 11335 var codePoint 11336 var length = string.length 11337 var leadSurrogate = null 11338 var bytes = [] 11339 11340 for (var i = 0; i < length; ++i) { 11341 codePoint = string.charCodeAt(i) 11342 11343 // is surrogate component 11344 if (codePoint > 0xD7FF && codePoint < 0xE000) { 11345 // last char was a lead 11346 if (!leadSurrogate) { 11347 // no lead yet 11348 if (codePoint > 0xDBFF) { 11349 // unexpected trail 11350 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 11351 continue 11352 } else if (i + 1 === length) { 11353 // unpaired lead 11354 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 11355 continue 11356 } 11357 11358 // valid lead 11359 leadSurrogate = codePoint 11360 11361 continue 11362 } 11363 11364 // 2 leads in a row 11365 if (codePoint < 0xDC00) { 11366 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 11367 leadSurrogate = codePoint 11368 continue 11369 } 11370 11371 // valid surrogate pair 11372 codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 11373 } else if (leadSurrogate) { 11374 // valid bmp char, but last char was a lead 11375 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 11376 } 11377 11378 leadSurrogate = null 11379 11380 // encode utf8 11381 if (codePoint < 0x80) { 11382 if ((units -= 1) < 0) break 11383 bytes.push(codePoint) 11384 } else if (codePoint < 0x800) { 11385 if ((units -= 2) < 0) break 11386 bytes.push( 11387 codePoint >> 0x6 | 0xC0, 11388 codePoint & 0x3F | 0x80 11389 ) 11390 } else if (codePoint < 0x10000) { 11391 if ((units -= 3) < 0) break 11392 bytes.push( 11393 codePoint >> 0xC | 0xE0, 11394 codePoint >> 0x6 & 0x3F | 0x80, 11395 codePoint & 0x3F | 0x80 11396 ) 11397 } else if (codePoint < 0x110000) { 11398 if ((units -= 4) < 0) break 11399 bytes.push( 11400 codePoint >> 0x12 | 0xF0, 11401 codePoint >> 0xC & 0x3F | 0x80, 11402 codePoint >> 0x6 & 0x3F | 0x80, 11403 codePoint & 0x3F | 0x80 11404 ) 11405 } else { 11406 throw new Error('Invalid code point') 11407 } 11408 } 11409 11410 return bytes 11411} 11412 11413function asciiToBytes (str) { 11414 var byteArray = [] 11415 for (var i = 0; i < str.length; ++i) { 11416 // Node's code seems to be doing this and not & 0x7F.. 11417 byteArray.push(str.charCodeAt(i) & 0xFF) 11418 } 11419 return byteArray 11420} 11421 11422function utf16leToBytes (str, units) { 11423 var c, hi, lo 11424 var byteArray = [] 11425 for (var i = 0; i < str.length; ++i) { 11426 if ((units -= 2) < 0) break 11427 11428 c = str.charCodeAt(i) 11429 hi = c >> 8 11430 lo = c % 256 11431 byteArray.push(lo) 11432 byteArray.push(hi) 11433 } 11434 11435 return byteArray 11436} 11437 11438function base64ToBytes (str) { 11439 return base64.toByteArray(base64clean(str)) 11440} 11441 11442function blitBuffer (src, dst, offset, length) { 11443 for (var i = 0; i < length; ++i) { 11444 if ((i + offset >= dst.length) || (i >= src.length)) break 11445 dst[i + offset] = src[i] 11446 } 11447 return i 11448} 11449 11450// ArrayBuffers from another context (i.e. an iframe) do not pass the `instanceof` check 11451// but they should be treated as valid. See: https://github.com/feross/buffer/issues/166 11452function isArrayBuffer (obj) { 11453 return obj instanceof ArrayBuffer || 11454 (obj != null && obj.constructor != null && obj.constructor.name === 'ArrayBuffer' && 11455 typeof obj.byteLength === 'number') 11456} 11457 11458function numberIsNaN (obj) { 11459 return obj !== obj // eslint-disable-line no-self-compare 11460} 11461 11462},{"base64-js":80,"ieee754":83}],83:[function(require,module,exports){
vendor: 2,071 bytes, lines 11463-11548
11463exports.read = function (buffer, offset, isLE, mLen, nBytes) { 11464 var e, m 11465 var eLen = nBytes * 8 - mLen - 1 11466 var eMax = (1 << eLen) - 1 11467 var eBias = eMax >> 1 11468 var nBits = -7 11469 var i = isLE ? (nBytes - 1) : 0 11470 var d = isLE ? -1 : 1 11471 var s = buffer[offset + i] 11472 11473 i += d 11474 11475 e = s & ((1 << (-nBits)) - 1) 11476 s >>= (-nBits) 11477 nBits += eLen 11478 for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {} 11479 11480 m = e & ((1 << (-nBits)) - 1) 11481 e >>= (-nBits) 11482 nBits += mLen 11483 for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {} 11484 11485 if (e === 0) { 11486 e = 1 - eBias 11487 } else if (e === eMax) { 11488 return m ? NaN : ((s ? -1 : 1) * Infinity) 11489 } else { 11490 m = m + Math.pow(2, mLen) 11491 e = e - eBias 11492 } 11493 return (s ? -1 : 1) * m * Math.pow(2, e - mLen) 11494} 11495 11496exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { 11497 var e, m, c 11498 var eLen = nBytes * 8 - mLen - 1 11499 var eMax = (1 << eLen) - 1 11500 var eBias = eMax >> 1 11501 var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) 11502 var i = isLE ? 0 : (nBytes - 1) 11503 var d = isLE ? 1 : -1 11504 var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 11505 11506 value = Math.abs(value) 11507 11508 if (isNaN(value) || value === Infinity) { 11509 m = isNaN(value) ? 1 : 0 11510 e = eMax 11511 } else { 11512 e = Math.floor(Math.log(value) / Math.LN2) 11513 if (value * (c = Math.pow(2, -e)) < 1) { 11514 e-- 11515 c *= 2 11516 } 11517 if (e + eBias >= 1) { 11518 value += rt / c 11519 } else { 11520 value += rt * Math.pow(2, 1 - eBias) 11521 } 11522 if (value * c >= 2) { 11523 e++ 11524 c /= 2 11525 } 11526 11527 if (e + eBias >= eMax) { 11528 m = 0 11529 e = eMax 11530 } else if (e + eBias >= 1) { 11531 m = (value * c - 1) * Math.pow(2, mLen) 11532 e = e + eBias 11533 } else { 11534 m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) 11535 e = 0 11536 } 11537 } 11538 11539 for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} 11540 11541 e = (e << mLen) | m 11542 eLen += mLen 11543 for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} 11544 11545 buffer[offset + i - d] |= s * 128 11546} 11547 11548},{}]},{},[1]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.