-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolitaire.js
2290 lines (1977 loc) · 131 KB
/
Solitaire.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
// This object will hold all exports.
var Haste = {};
/* Thunk
Creates a thunk representing the given closure.
If the non-updatable flag is undefined, the thunk is updatable.
*/
function T(f, nu) {
this.f = f;
if(nu === undefined) {
this.x = __updatable;
}
}
/* Hint to optimizer that an imported symbol is strict. */
function __strict(x) {return x}
// A tailcall.
function F(f) {
this.f = f;
}
// A partially applied function. Invariant: members are never thunks.
function PAP(f, args) {
this.f = f;
this.args = args;
this.arity = f.length - args.length;
}
// Special object used for blackholing.
var __blackhole = {};
// Used to indicate that an object is updatable.
var __updatable = {};
/* Generic apply.
Applies a function *or* a partial application object to a list of arguments.
See https://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/HaskellExecution/FunctionCalls
for more information.
*/
function A(f, args) {
while(true) {
f = E(f);
if(f instanceof F) {
f = E(B(f));
}
if(f instanceof PAP) {
// f is a partial application
if(args.length == f.arity) {
// Saturated application
return f.f.apply(null, f.args.concat(args));
} else if(args.length < f.arity) {
// Application is still unsaturated
return new PAP(f.f, f.args.concat(args));
} else {
// Application is oversaturated;
var f2 = f.f.apply(null, f.args.concat(args.slice(0, f.arity)));
args = args.slice(f.arity);
f = B(f2);
}
} else if(f instanceof Function) {
if(args.length == f.length) {
return f.apply(null, args);
} else if(args.length < f.length) {
return new PAP(f, args);
} else {
var f2 = f.apply(null, args.slice(0, f.length));
args = args.slice(f.length);
f = B(f2);
}
} else {
return f;
}
}
}
/* Eval
Evaluate the given thunk t into head normal form.
If the "thunk" we get isn't actually a thunk, just return it.
*/
function E(t) {
if(t instanceof T) {
if(t.f !== __blackhole) {
var f = t.f;
t.f = __blackhole;
if(t.x === __updatable) {
t.x = f();
} else {
return f();
}
}
return t.x;
} else {
return t;
}
}
/* Bounce
Bounce on a trampoline for as long as we get a function back.
*/
function B(f) {
while(f instanceof F) {
var fun = f.f;
f.f = __blackhole;
f = fun();
}
return f;
}
// Export Haste, A, B and E. Haste because we need to preserve exports, A, B
// and E because they're handy for Haste.Foreign.
if(!window) {
var window = {};
}
window['Haste'] = Haste;
window['A'] = A;
window['E'] = E;
window['B'] = B;
/* Throw an error.
We need to be able to use throw as an exception so we wrap it in a function.
*/
function die(err) {
throw E(err);
}
function quot(a, b) {
return (a-a%b)/b;
}
function quotRemI(a, b) {
return [0, (a-a%b)/b, a%b];
}
// 32 bit integer multiplication, with correct overflow behavior
// note that |0 or >>>0 needs to be applied to the result, for int and word
// respectively.
if(Math.imul) {
var imul = Math.imul;
} else {
var imul = function(a, b) {
// ignore high a * high a as the result will always be truncated
var lows = (a & 0xffff) * (b & 0xffff); // low a * low b
var aB = (a & 0xffff) * (b & 0xffff0000); // low a * high b
var bA = (a & 0xffff0000) * (b & 0xffff); // low b * high a
return lows + aB + bA; // sum will not exceed 52 bits, so it's safe
}
}
function addC(a, b) {
var x = a+b;
return [0, x & 0xffffffff, x > 0x7fffffff];
}
function subC(a, b) {
var x = a-b;
return [0, x & 0xffffffff, x < -2147483648];
}
function sinh (arg) {
return (Math.exp(arg) - Math.exp(-arg)) / 2;
}
function tanh (arg) {
return (Math.exp(arg) - Math.exp(-arg)) / (Math.exp(arg) + Math.exp(-arg));
}
function cosh (arg) {
return (Math.exp(arg) + Math.exp(-arg)) / 2;
}
function isFloatFinite(x) {
return isFinite(x);
}
function isDoubleFinite(x) {
return isFinite(x);
}
function err(str) {
die(toJSStr(str));
}
/* unpackCString#
NOTE: update constructor tags if the code generator starts munging them.
*/
function unCStr(str) {return unAppCStr(str, [0]);}
function unFoldrCStr(str, f, z) {
var acc = z;
for(var i = str.length-1; i >= 0; --i) {
acc = B(A(f, [str.charCodeAt(i), acc]));
}
return acc;
}
function unAppCStr(str, chrs) {
var i = arguments[2] ? arguments[2] : 0;
if(i >= str.length) {
return E(chrs);
} else {
return [1,str.charCodeAt(i),new T(function() {
return unAppCStr(str,chrs,i+1);
})];
}
}
function charCodeAt(str, i) {return str.charCodeAt(i);}
function fromJSStr(str) {
return unCStr(E(str));
}
function toJSStr(hsstr) {
var s = '';
for(var str = E(hsstr); str[0] == 1; str = E(str[2])) {
s += String.fromCharCode(E(str[1]));
}
return s;
}
// newMutVar
function nMV(val) {
return ({x: val});
}
// readMutVar
function rMV(mv) {
return mv.x;
}
// writeMutVar
function wMV(mv, val) {
mv.x = val;
}
// atomicModifyMutVar
function mMV(mv, f) {
var x = B(A(f, [mv.x]));
mv.x = x[1];
return x[2];
}
function localeEncoding() {
var le = newByteArr(5);
le['v']['i8'][0] = 'U'.charCodeAt(0);
le['v']['i8'][1] = 'T'.charCodeAt(0);
le['v']['i8'][2] = 'F'.charCodeAt(0);
le['v']['i8'][3] = '-'.charCodeAt(0);
le['v']['i8'][4] = '8'.charCodeAt(0);
return le;
}
var isDoubleNaN = isNaN;
var isFloatNaN = isNaN;
function isDoubleInfinite(d) {
return (d === Infinity);
}
var isFloatInfinite = isDoubleInfinite;
function isDoubleNegativeZero(x) {
return (x===0 && (1/x)===-Infinity);
}
var isFloatNegativeZero = isDoubleNegativeZero;
function strEq(a, b) {
return a == b;
}
function strOrd(a, b) {
if(a < b) {
return 0;
} else if(a == b) {
return 1;
}
return 2;
}
function jsCatch(act, handler) {
try {
return B(A(act,[0]));
} catch(e) {
return B(A(handler,[e, 0]));
}
}
/* Haste represents constructors internally using 1 for the first constructor,
2 for the second, etc.
However, dataToTag should use 0, 1, 2, etc. Also, booleans might be unboxed.
*/
function dataToTag(x) {
if(x instanceof Array) {
return x[0];
} else {
return x;
}
}
function __word_encodeDouble(d, e) {
return d * Math.pow(2,e);
}
var __word_encodeFloat = __word_encodeDouble;
var jsRound = Math.round;
var jsTrunc = Math.trunc ? Math.trunc : function(x) {
return x < 0 ? Math.ceil(x) : Math.floor(x);
};
function jsRoundW(n) {
return Math.abs(jsTrunc(n));
}
var realWorld = undefined;
if(typeof _ == 'undefined') {
var _ = undefined;
}
function popCnt(i) {
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}
// Scratch space for byte arrays.
var rts_scratchBuf = new ArrayBuffer(8);
var rts_scratchW32 = new Uint32Array(rts_scratchBuf);
var rts_scratchFloat = new Float32Array(rts_scratchBuf);
var rts_scratchDouble = new Float64Array(rts_scratchBuf);
function decodeFloat(x) {
rts_scratchFloat[0] = x;
var sign = x < 0 ? -1 : 1;
var exp = ((rts_scratchW32[0] >> 23) & 0xff) - 150;
var man = rts_scratchW32[0] & 0x7fffff;
if(exp === 0) {
++exp;
} else {
man |= (1 << 23);
}
return [0, sign*man, exp];
}
function decodeDouble(x) {
rts_scratchDouble[0] = x;
var sign = x < 0 ? -1 : 1;
var manHigh = rts_scratchW32[1] & 0xfffff;
var manLow = rts_scratchW32[0];
var exp = ((rts_scratchW32[1] >> 20) & 0x7ff) - 1075;
if(exp === 0) {
++exp;
} else {
manHigh |= (1 << 20);
}
return [0, sign, manHigh, manLow, exp];
}
function jsAlert(val) {
if(typeof alert != 'undefined') {
alert(val);
} else {
print(val);
}
}
function jsLog(val) {
console.log(val);
}
function jsPrompt(str) {
var val;
if(typeof prompt != 'undefined') {
val = prompt(str);
} else {
print(str);
val = readline();
}
return val == undefined ? '' : val.toString();
}
function jsEval(str) {
var x = eval(str);
return x == undefined ? '' : x.toString();
}
function isNull(obj) {
return obj === null;
}
function jsRead(str) {
return Number(str);
}
function jsShowI(val) {return val.toString();}
function jsShow(val) {
var ret = val.toString();
return val == Math.round(val) ? ret + '.0' : ret;
}
window['jsGetMouseCoords'] = function jsGetMouseCoords(e) {
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
return [posx - (e.currentTarget.offsetLeft || 0),
posy - (e.currentTarget.offsetTop || 0)];
}
function jsGet(elem, prop) {
return elem[prop].toString();
}
function jsSet(elem, prop, val) {
elem[prop] = val;
}
function jsGetAttr(elem, prop) {
if(elem.hasAttribute(prop)) {
return elem.getAttribute(prop).toString();
} else {
return "";
}
}
function jsSetAttr(elem, prop, val) {
elem.setAttribute(prop, val);
}
function jsGetStyle(elem, prop) {
return elem.style[prop].toString();
}
function jsSetStyle(elem, prop, val) {
elem.style[prop] = val;
}
function jsKillChild(child, parent) {
parent.removeChild(child);
}
function jsClearChildren(elem) {
while(elem.hasChildNodes()){
elem.removeChild(elem.lastChild);
}
}
function jsFind(elem) {
var e = document.getElementById(elem)
if(e) {
return [1,e];
}
return [0];
}
function jsElemsByClassName(cls) {
var es = document.getElementsByClassName(cls);
var els = [0];
for (var i = es.length-1; i >= 0; --i) {
els = [1, es[i], els];
}
return els;
}
function jsQuerySelectorAll(elem, query) {
var els = [0], nl;
if (!elem || typeof elem.querySelectorAll !== 'function') {
return els;
}
nl = elem.querySelectorAll(query);
for (var i = nl.length-1; i >= 0; --i) {
els = [1, nl[i], els];
}
return els;
}
function jsCreateElem(tag) {
return document.createElement(tag);
}
function jsCreateTextNode(str) {
return document.createTextNode(str);
}
function jsGetChildBefore(elem) {
elem = elem.previousSibling;
while(elem) {
if(typeof elem.tagName != 'undefined') {
return [1,elem];
}
elem = elem.previousSibling;
}
return [0];
}
function jsGetLastChild(elem) {
var len = elem.childNodes.length;
for(var i = len-1; i >= 0; --i) {
if(typeof elem.childNodes[i].tagName != 'undefined') {
return [1,elem.childNodes[i]];
}
}
return [0];
}
function jsGetFirstChild(elem) {
var len = elem.childNodes.length;
for(var i = 0; i < len; i++) {
if(typeof elem.childNodes[i].tagName != 'undefined') {
return [1,elem.childNodes[i]];
}
}
return [0];
}
function jsGetChildren(elem) {
var children = [0];
var len = elem.childNodes.length;
for(var i = len-1; i >= 0; --i) {
if(typeof elem.childNodes[i].tagName != 'undefined') {
children = [1, elem.childNodes[i], children];
}
}
return children;
}
function jsSetChildren(elem, children) {
children = E(children);
jsClearChildren(elem, 0);
while(children[0] === 1) {
elem.appendChild(E(children[1]));
children = E(children[2]);
}
}
function jsAppendChild(child, container) {
container.appendChild(child);
}
function jsAddChildBefore(child, container, after) {
container.insertBefore(child, after);
}
var jsRand = Math.random;
// Concatenate a Haskell list of JS strings
function jsCat(strs, sep) {
var arr = [];
strs = E(strs);
while(strs[0]) {
strs = E(strs);
arr.push(E(strs[1]));
strs = E(strs[2]);
}
return arr.join(sep);
}
// JSON stringify a string
function jsStringify(str) {
return JSON.stringify(str);
}
// Parse a JSON message into a Haste.JSON.JSON value.
// As this pokes around inside Haskell values, it'll need to be updated if:
// * Haste.JSON.JSON changes;
// * E() starts to choke on non-thunks;
// * data constructor code generation changes; or
// * Just and Nothing change tags.
function jsParseJSON(str) {
try {
var js = JSON.parse(str);
var hs = toHS(js);
} catch(_) {
return [0];
}
return [1,hs];
}
function toHS(obj) {
switch(typeof obj) {
case 'number':
return [0, jsRead(obj)];
case 'string':
return [1, obj];
case 'boolean':
return [2, obj]; // Booleans are special wrt constructor tags!
case 'object':
if(obj instanceof Array) {
return [3, arr2lst_json(obj, 0)];
} else if (obj == null) {
return [5];
} else {
// Object type but not array - it's a dictionary.
// The RFC doesn't say anything about the ordering of keys, but
// considering that lots of people rely on keys being "in order" as
// defined by "the same way someone put them in at the other end,"
// it's probably a good idea to put some cycles into meeting their
// misguided expectations.
var ks = [];
for(var k in obj) {
ks.unshift(k);
}
var xs = [0];
for(var i = 0; i < ks.length; i++) {
xs = [1, [0, ks[i], toHS(obj[ks[i]])], xs];
}
return [4, xs];
}
}
}
function arr2lst_json(arr, elem) {
if(elem >= arr.length) {
return [0];
}
return [1, toHS(arr[elem]), new T(function() {return arr2lst_json(arr,elem+1);}),true]
}
function ajaxReq(method, url, async, postdata, cb) {
var xhr = new XMLHttpRequest();
xhr.open(method, url, async);
if(method == "POST") {
xhr.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
}
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
B(A(cb,[[1,xhr.responseText],0]));
} else {
B(A(cb,[[0],0])); // Nothing
}
}
}
xhr.send(postdata);
}
/* gettimeofday(2) */
function gettimeofday(tv, _tz) {
var t = new Date().getTime();
writeOffAddr("i32", 4, tv, 0, (t/1000)|0);
writeOffAddr("i32", 4, tv, 1, ((t%1000)*1000)|0);
return 0;
}
/* Utility functions for working with JSStrings. */
var _jss_singleton = String.fromCharCode;
function _jss_cons(c,s) {return String.fromCharCode(c)+s;}
function _jss_snoc(s,c) {return s+String.fromCharCode(c);}
function _jss_append(a,b) {return a+b;}
function _jss_len(s) {return s.length;}
function _jss_index(s,i) {return s.charCodeAt(i);}
function _jss_drop(s,i) {return s.substr(i);}
function _jss_substr(s,a,b) {return s.substr(a,b);}
function _jss_take(n,s) {return s.substr(0,n);}
// TODO: incorrect for some unusual characters.
function _jss_rev(s) {return s.split("").reverse().join("");}
function _jss_map(f,s) {
f = E(f);
var s2 = '';
for(var i in s) {
s2 += String.fromCharCode(E(f(s.charCodeAt(i))));
}
return s2;
}
function _jss_foldl(f,x,s) {
f = E(f);
for(var i in s) {
x = A(f,[x,s.charCodeAt(i)]);
}
return x;
}
function _jss_re_match(s,re) {return s.search(re)>=0;}
function _jss_re_compile(re,fs) {return new RegExp(re,fs);}
function _jss_re_replace(s,re,rep) {return s.replace(re,rep);}
function _jss_re_find(re,s) {
var a = s.match(re);
return a ? mklst(a) : [0];
}
function mklst(arr) {
var l = [0], len = arr.length-1;
for(var i = 0; i <= len; ++i) {
l = [1,arr[len-i],l];
}
return l;
}
// Create a little endian ArrayBuffer representation of something.
function toABHost(v, n, x) {
var a = new ArrayBuffer(n);
new window[v](a)[0] = x;
return a;
}
function toABSwap(v, n, x) {
var a = new ArrayBuffer(n);
new window[v](a)[0] = x;
var bs = new Uint8Array(a);
for(var i = 0, j = n-1; i < j; ++i, --j) {
var tmp = bs[i];
bs[i] = bs[j];
bs[j] = tmp;
}
return a;
}
window['toABle'] = toABHost;
window['toABbe'] = toABSwap;
// Swap byte order if host is not little endian.
var buffer = new ArrayBuffer(2);
new DataView(buffer).setInt16(0, 256, true);
if(new Int16Array(buffer)[0] !== 256) {
window['toABle'] = toABSwap;
window['toABbe'] = toABHost;
}
// MVar implementation.
// Since Haste isn't concurrent, takeMVar and putMVar don't block on empty
// and full MVars respectively, but terminate the program since they would
// otherwise be blocking forever.
function newMVar() {
return ({empty: true});
}
function tryTakeMVar(mv) {
if(mv.empty) {
return [0, 0, undefined];
} else {
var val = mv.x;
mv.empty = true;
mv.x = null;
return [0, 1, val];
}
}
function takeMVar(mv) {
if(mv.empty) {
// TODO: real BlockedOnDeadMVar exception, perhaps?
err("Attempted to take empty MVar!");
}
var val = mv.x;
mv.empty = true;
mv.x = null;
return val;
}
function putMVar(mv, val) {
if(!mv.empty) {
// TODO: real BlockedOnDeadMVar exception, perhaps?
err("Attempted to put full MVar!");
}
mv.empty = false;
mv.x = val;
}
function tryPutMVar(mv, val) {
if(!mv.empty) {
return 0;
} else {
mv.empty = false;
mv.x = val;
return 1;
}
}
function sameMVar(a, b) {
return (a == b);
}
function isEmptyMVar(mv) {
return mv.empty ? 1 : 0;
}
// Implementation of stable names.
// Unlike native GHC, the garbage collector isn't going to move data around
// in a way that we can detect, so each object could serve as its own stable
// name if it weren't for the fact we can't turn a JS reference into an
// integer.
// So instead, each object has a unique integer attached to it, which serves
// as its stable name.
var __next_stable_name = 1;
var __stable_table;
function makeStableName(x) {
if(x instanceof Object) {
if(!x.stableName) {
x.stableName = __next_stable_name;
__next_stable_name += 1;
}
return {type: 'obj', name: x.stableName};
} else {
return {type: 'prim', name: Number(x)};
}
}
function eqStableName(x, y) {
return (x.type == y.type && x.name == y.name) ? 1 : 0;
}
var Integer = function(bits, sign) {
this.bits_ = [];
this.sign_ = sign;
var top = true;
for (var i = bits.length - 1; i >= 0; i--) {
var val = bits[i] | 0;
if (!top || val != sign) {
this.bits_[i] = val;
top = false;
}
}
};
Integer.IntCache_ = {};
var I_fromInt = function(value) {
if (-128 <= value && value < 128) {
var cachedObj = Integer.IntCache_[value];
if (cachedObj) {
return cachedObj;
}
}
var obj = new Integer([value | 0], value < 0 ? -1 : 0);
if (-128 <= value && value < 128) {
Integer.IntCache_[value] = obj;
}
return obj;
};
var I_fromNumber = function(value) {
if (isNaN(value) || !isFinite(value)) {
return Integer.ZERO;
} else if (value < 0) {
return I_negate(I_fromNumber(-value));
} else {
var bits = [];
var pow = 1;
for (var i = 0; value >= pow; i++) {
bits[i] = (value / pow) | 0;
pow *= Integer.TWO_PWR_32_DBL_;
}
return new Integer(bits, 0);
}
};
var I_fromBits = function(bits) {
var high = bits[bits.length - 1];
return new Integer(bits, high & (1 << 31) ? -1 : 0);
};
var I_fromString = function(str, opt_radix) {
if (str.length == 0) {
throw Error('number format error: empty string');
}
var radix = opt_radix || 10;
if (radix < 2 || 36 < radix) {
throw Error('radix out of range: ' + radix);
}
if (str.charAt(0) == '-') {
return I_negate(I_fromString(str.substring(1), radix));
} else if (str.indexOf('-') >= 0) {
throw Error('number format error: interior "-" character');
}
var radixToPower = I_fromNumber(Math.pow(radix, 8));
var result = Integer.ZERO;
for (var i = 0; i < str.length; i += 8) {
var size = Math.min(8, str.length - i);
var value = parseInt(str.substring(i, i + size), radix);
if (size < 8) {
var power = I_fromNumber(Math.pow(radix, size));
result = I_add(I_mul(result, power), I_fromNumber(value));
} else {
result = I_mul(result, radixToPower);
result = I_add(result, I_fromNumber(value));
}
}
return result;
};
Integer.TWO_PWR_32_DBL_ = (1 << 16) * (1 << 16);
Integer.ZERO = I_fromInt(0);
Integer.ONE = I_fromInt(1);
Integer.TWO_PWR_24_ = I_fromInt(1 << 24);
var I_toInt = function(self) {
return self.bits_.length > 0 ? self.bits_[0] : self.sign_;
};
var I_toWord = function(self) {
return I_toInt(self) >>> 0;
};
var I_toNumber = function(self) {
if (isNegative(self)) {
return -I_toNumber(I_negate(self));
} else {
var val = 0;
var pow = 1;
for (var i = 0; i < self.bits_.length; i++) {
val += I_getBitsUnsigned(self, i) * pow;
pow *= Integer.TWO_PWR_32_DBL_;
}
return val;
}
};
var I_getBits = function(self, index) {
if (index < 0) {
return 0;
} else if (index < self.bits_.length) {
return self.bits_[index];
} else {
return self.sign_;
}
};
var I_getBitsUnsigned = function(self, index) {
var val = I_getBits(self, index);
return val >= 0 ? val : Integer.TWO_PWR_32_DBL_ + val;
};
var getSign = function(self) {
return self.sign_;
};
var isZero = function(self) {
if (self.sign_ != 0) {
return false;
}
for (var i = 0; i < self.bits_.length; i++) {
if (self.bits_[i] != 0) {
return false;
}
}
return true;
};
var isNegative = function(self) {
return self.sign_ == -1;
};
var isOdd = function(self) {
return (self.bits_.length == 0) && (self.sign_ == -1) ||
(self.bits_.length > 0) && ((self.bits_[0] & 1) != 0);
};
var I_equals = function(self, other) {
if (self.sign_ != other.sign_) {
return false;
}
var len = Math.max(self.bits_.length, other.bits_.length);
for (var i = 0; i < len; i++) {
if (I_getBits(self, i) != I_getBits(other, i)) {
return false;
}
}
return true;
};
var I_notEquals = function(self, other) {
return !I_equals(self, other);
};
var I_greaterThan = function(self, other) {
return I_compare(self, other) > 0;
};
var I_greaterThanOrEqual = function(self, other) {