forked from albertsun/JavaScript-Geometry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedBlackMap.js
More file actions
1835 lines (1630 loc) · 54.2 KB
/
Copy pathRedBlackMap.js
File metadata and controls
1835 lines (1630 loc) · 54.2 KB
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
/*
Modifications to js_cols.RedBlackMap are
Copyright 2011 Dow Jones & Company, Inc. All Rights Reserved
Author: Albert Sun
WSJ.com News Graphics
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
*/
//Copyright 2010 Thomas Stjernegaard Jeppesen. All Rights Reserved.
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS-IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
/**
* @fileoverview Bootstrap for the js_cols Library.
* The code in this file, base.js, is adopted partly from googles base.js,
* and partly from structs.js.
* It has been modified to suit the needs of the js_cols Library.
* The js_cols collections should be easily integratable in projets
* using google closure library, as they generally use the same method names,
* code style and use of closures.
*
*/
/**
* Declaring a global js_cols variable
*/
js_cols = {};
/**
* Reference to the global context. In most cases this will be 'window'.
*/
js_cols.global = this;
/**
* Implements a system for the dynamic resolution of dependencies
* @param {string} rule Rule to include, in the form js_cols.part.
*/
js_cols.require = function(rule) {
if (js_cols.getObjectByName(rule)) {
return;
}
var path = js_cols.getPathFromDeps_(rule);
if (path) {
js_cols.included_[path] = true;
js_cols.writeScripts_();
} else {
var errorMessage = 'js_cols.require could not find: ' + rule;
throw Error(errorMessage);
}
};
/**
* Creates object stubs for a namespace. When present in a file, goog.provide
* also indicates that the file defines the indicated object.
* @param {string} name name of the object that this file defines.
*/
js_cols.provide = function(name) {
// Ensure that the same namespace isn't provided twice.
if (js_cols.getObjectByName(name) && !js_cols.implicitNamespaces_[name]) {
throw Error('Namespace "' + name + '" already declared.');
}
var namespace = name;
while ((namespace = namespace.substring(0, namespace.lastIndexOf('.')))) {
js_cols.implicitNamespaces_[namespace] = true;
}
js_cols.exportPath_(name);
};
js_cols.implicitNamespaces_ = {};
/**
* Path for included scripts
* @type {string}
*/
js_cols.basePath = '';
/**
* Tries to detect whether is in the context of an HTML document.
* @return {boolean} True if it looks like HTML document.
* @private
*/
js_cols.inHtmlDocument_ = function() {
var doc = js_cols.global.document;
return typeof doc != 'undefined' &&
'write' in doc; // XULDocument misses write.
};
/**
* Builds an object structure for the provided namespace path,
* ensuring that names that already exist are not overwritten. For
* example:
* "a.b.c" -> a = {};a.b={};a.b.c={};
* Used by goog.provide and goog.exportSymbol.
* @param {string} name name of the object that this file defines.
* @param {*=} opt_object the object to expose at the end of the path.
* @param {Object=} opt_objectToExportTo The object to add the path to; default
* is |js_cols.global|.
* @private
*/
js_cols.exportPath_ = function(name, opt_object, opt_objectToExportTo) {
var parts = name.split('.');
var cur = opt_objectToExportTo || js_cols.global;
// Internet Explorer exhibits strange behavior when throwing errors from
// methods externed in this manner. See the testExportSymbolExceptions in
// base_test.html for an example.
if (!(parts[0] in cur) && cur.execScript) {
cur.execScript('var ' + parts[0]);
}
// Certain browsers cannot parse code in the form for((a in b); c;);
// This pattern is produced by the JSCompiler when it collapses the
// statement above into the conditional loop below. To prevent this from
// happening, use a for-loop and reserve the init logic as below.
// Parentheses added to eliminate strict JS warning in Firefox.
for (var part; parts.length && (part = parts.shift());) {
if (!parts.length && opt_object !== undefined) {
// last part and we have an object; use it
cur[part] = opt_object;
} else if (cur[part]) {
cur = cur[part];
} else {
cur = cur[part] = {};
}
}
};
/**
* Returns an object based on its fully qualified external name. If you are
* using a compilation pass that renames property names beware that using this
* function will not find renamed properties.
*
* @param {string} name The fully qualified name.
* @param {Object=} opt_obj The object within which to look; default is
* |goog.global|.
* @return {Object} The object or, if not found, null.
*/
js_cols.getObjectByName = function(name, opt_obj) {
var parts = name.split('.');
var cur = opt_obj || js_cols.global;
for (var part; part = parts.shift(); ) {
if (cur[part]) {
cur = cur[part];
} else {
return null;
}
}
return cur;
};
js_cols.included_ = {};
/**
* This object is used to keep track of dependencies and other data that is
* used for loading scripts
* @private
* @type {Object}
*/
js_cols.dependencies_ = {
pathToNames: {}, // 1 to many
nameToPath: {}, // 1 to 1
requires: {}, // 1 to many
// used when resolving dependencies to prevent us from
// visiting the file twice
visited: {},
written: {} // used to keep track of script files we have written
};
/**
* Adds a dependency from a file to the files it requires.
* @param {string} relPath The path to the js file.
* @param {Array} provides An array of strings with the names of the objects
* this file provides.
* @param {Array} requires An array of strings with the names of the objects
* this file requires.
*/
js_cols.addDependency = function(relPath, provides, requires) {
var provide, require;
var path = relPath.replace(/\\/g, '/');
var deps = js_cols.dependencies_;
for (var i = 0; provide = provides[i]; i++) {
deps.nameToPath[provide] = path;
if (!(path in deps.pathToNames)) {
deps.pathToNames[path] = {};
}
deps.pathToNames[path][provide] = true;
}
for (var j = 0; require = requires[j]; j++) {
if (!(path in deps.requires)) {
deps.requires[path] = {};
}
deps.requires[path][require] = true;
}
};
/**
* Resolves dependencies based on the dependencies added using addDependency
* and calls importScript_ in the correct order.
* @private
*/
js_cols.writeScripts_ = function() {
// the scripts we need to write this time
var scripts = [];
var seenScript = {};
var deps = js_cols.dependencies_;
function visitNode(path) {
if (path in deps.written) {
return;
}
// we have already visited this one. We can get here if we have cyclic
// dependencies
if (path in deps.visited) {
if (!(path in seenScript)) {
seenScript[path] = true;
scripts.push(path);
}
return;
}
deps.visited[path] = true;
if (path in deps.requires) {
for (var requireName in deps.requires[path]) {
if (requireName in deps.nameToPath) {
visitNode(deps.nameToPath[requireName]);
} else if (!js_cols.getObjectByName(requireName)) {
// If the required name is defined, we assume that this
// dependency was bootstapped by other means. Otherwise,
// throw an exception.
throw Error('Undefined nameToPath for ' + requireName);
}
}
}
if (!(path in seenScript)) {
seenScript[path] = true;
scripts.push(path);
}
}
for (var path in js_cols.included_) {
if (!deps.written[path]) {
visitNode(path);
}
}
for (var i = 0; i < scripts.length; i++) {
if (scripts[i]) {
js_cols.importScript_(js_cols.basePath + scripts[i]);
} else {
throw Error('Undefined script input');
}
}
};
/**
* Imports a script if, and only if, that script hasn't already been imported.
* (Must be called at execution time)
* @param {string} src Script source.
* @private
*/
js_cols.importScript_ = function(src) {
var importScript = js_cols.writeScriptTag_;
if (!js_cols.dependencies_.written[src] && importScript(src)) {
js_cols.dependencies_.written[src] = true;
}
};
/**
* The default implementation of the import function. Writes a script tag to
* import the script.
*
* @param {string} src The script source.
* @return {boolean} True if the script was imported, false otherwise.
* @private
*/
js_cols.writeScriptTag_ = function(src) {
if (js_cols.inHtmlDocument_()) {
var doc = js_cols.global.document;
doc.write(
'<script type="text/javascript" src="' + src + '"></' + 'script>');
return true;
} else {
return false;
}
};
/**
* Looks at the dependency rules and tries to determine the script file that
* fulfills a particular rule.
* @param {string} rule In the form js_cols.Class or project.script.
* @return {?string} Url corresponding to the rule, or null.
* @private
*/
js_cols.getPathFromDeps_ = function(rule) {
if (rule in js_cols.dependencies_.nameToPath) {
return js_cols.dependencies_.nameToPath[rule];
} else {
return null;
}
};
/**
* Gets a unique ID for an object. This mutates the object so that further
* calls with the same object as a parameter returns the same value. The unique
* ID is guaranteed to be unique across the current session amongst objects that
* are passed into {@code getUid}. There is no guarantee that the ID is unique
* or consistent across sessions. It is unsafe to generate unique ID for
* function prototypes.
*
* @param {Object} obj The object to get the unique ID for.
* @return {number} The unique ID for the object.
* @public
*/
js_cols.getUid = function(obj) {
// TODO(user): Make the type stricter, do not accept null.
// In Opera window.hasOwnProperty exists but always returns false so we avoid
// using it. As a consequence the unique ID generated for BaseClass.prototype
// and SubClass.prototype will be the same.
return obj[js_cols.UID_PROPERTY_] ||
(obj[js_cols.UID_PROPERTY_] = ++js_cols.uidCounter_);
};
/**
* Removes the unique ID from an object. This is useful if the object was
* previously mutated using {@code js_cols.getUid} in which case the mutation is
* undone.
* @param {Object} obj The object to remove the unique ID field from.
* @public
*/
js_cols.removeUid = function(obj) {
// TODO(user): Make the type stricter, do not accept null.
// DOM nodes in IE are not instance of Object and throws exception
// for delete. Instead we try to use removeAttribute
if ('removeAttribute' in obj) {
obj.removeAttribute(js_cols.UID_PROPERTY_);
}
/** @preserveTry */
try {
delete obj[js_cols.UID_PROPERTY_];
} catch (ex) {
}
};
/**
* Name for unique ID property. Initialized in a way to help avoid collisions
* with other closure javascript on the same page.
* @type {string}
* @private
*/
js_cols.UID_PROPERTY_ = 'js_cols_uid_' +
Math.floor(Math.random() * 2147483648).toString(36);
/**
* Counter for UID.
* @type {number}
* @private
*/
js_cols.uidCounter_ = 0;
/**
* Returns the values of the object/map/hash.
*
* @param {Object} obj The object from which to get the values.
* @return {!Array} The values in the object/map/hash.
* @public
*/
js_cols.getValues = function(obj) {
if (js_cols.typeOf(obj) == 'array'){
return obj;
}
else if(!obj.getValues){
var res = [];
var i = 0;
for (var key in obj) {
res[i++] = obj[key];
}
}
else{
var res = obj.getValues();
}
return res;
};
/**
* Returns the keys of the object/map/hash.
*
* @param {Object} obj The object from which to get the keys.
* @return {!Array.<string>} Array of property keys.
* @public
*/
js_cols.getKeys = function(obj) {
if (obj.getKeys){
return obj.getKeys();
}
else if (js_cols.typeOf(obj) == 'array'){
var res = [];
for (var i = 0; i < obj.length; i++){
res.push(i);
}
return res;
}
else if (js_cols.typeOf(obj) == 'object'){
var res = [];
var i = 0;
for (var key in obj) {
res[i++] = key;
}
return res;
}
};
/**
* Calls a function for each element in an object/map/hash. If
* all calls return true, returns true. If any call returns false, returns
* false at this point and does not continue to check the remaining elements.
*
* @param {Object} obj The object to check.
* @param {Function} f The function to call for every element. This function
* takes 3 arguments (the element, the index and the object) and should
* return a boolean.
* @param {Object=} opt_obj This is used as the 'this' object within f.
* @return {boolean} false if any element fails the test.
* @public
*/
js_cols.every = function(obj, f, opt_obj) {
if (js_cols.typeOf(obj.every) == 'function'){
return obj.every(f, opt_obj);
}
else if (js_cols.typeOf(obj.getValues) == 'function'){
var col = obj.getValues();
for (var i =0; i< col.length; i++ ) {
if (!f.call(opt_obj, col[i], i, col)) {
return false;
}
}
return true;
}
else if (js_cols.typeOf(obj) == 'array'){
for (var i =0; i< obj.length; i++ ) {
if (!f.call(opt_obj, obj[i], i, obj)) {
return false;
}
}
return true;
}
else if (js_cols.typeOf(obj) == 'object'){
for (var key in obj) {
if (!f.call(opt_obj, obj[key], key, obj)) {
return false;
}
}
return true;
}
};
/**
* Calls a function for each element in an object/map/hash.
* @param {Object} obj The object to traverse.
* @param {Function} f The function to call for every element. This function
* takes 3 arguments (the element, the index and the object)
* @param {Object=} opt_obj This is used as the 'this' object within f.
* @public
*/
js_cols.forEach = function(obj, f, opt_obj) {
if (js_cols.typeOf(obj.forEach) == 'function'){
obj.forEach(f, opt_obj);
}
else if (js_cols.typeOf(obj.getValues) == 'function'){
var col = obj.getValues();
for (var i =0; i< col.length; i++ ) {
f.call(opt_obj, col[i], i, col)
}
}
else if (js_cols.typeOf(obj) == 'array'){
for (var i =0; i< obj.length; i++ ) {
f.call(opt_obj, obj[i], i, obj)
}
}
else if (js_cols.typeOf(obj) == 'object'){
for (var key in obj) {
f.call(opt_obj, obj[key], key, obj)
}
}
};
/**
* Returns the number of values in the collection-like object.
* @param {Object} col The collection-like object.
* @return {number} The number of values in the collection-like object.
* @public
*/
js_cols.getCount = function(col) {
if (typeof col.getCount == 'function') {
return col.getCount();
}
else if (col.length && typeof col.length == "number") {
return col.length;
}
else{
var rv = 0;
for (var key in col) {
rv++;
}
return rv;
}
};
/**
* Whether the collection contains the given value. This is O(n) and uses
* equals (==) to test the existence.
* @param {Object} col The collection-like object.
* @param {*} val The value to check for.
* @return {boolean} True if the map contains the value.
* @public
*/
js_cols.contains = function(col, val) {
if (typeof col.contains == 'function') {
return col.contains(val);
}
if (typeof col.containsValue == 'function') {
return col.containsValue(val);
}
if (js_cols.typeOf(col) == 'array') {
for (var i=0; i<col.length;i++){
if (col[i] == val) return true;
}
return false;
}
for (var key in col) {
if (col[key] == val) {
return true;
}
}
return false;
};
/**
* This is a "fixed" version of the typeof operator. It differs from the typeof
* operator in such a way that null returns 'null' and arrays return 'array'.
* @param {*} value The value to get the type of.
* @return {string} The name of the type.
* @public
*/
js_cols.typeOf = function(value) {
var s = typeof value;
if (s == 'object') {
if (value) {
// We cannot use constructor == Array or instanceof Array because
// different frames have different Array objects. In IE6, if the iframe
// where the array was created is destroyed, the array loses its
// prototype. Then dereferencing val.splice here throws an exception, so
// we can't use goog.isFunction. Calling typeof directly returns 'unknown'
// so that will work. In this case, this function will return false and
// most array functions will still work because the array is still
// array-like (supports length and []) even though it has lost its
// prototype.
// Mark Miller noticed that Object.prototype.toString
// allows access to the unforgeable [[Class]] property.
// 15.2.4.2 Object.prototype.toString ( )
// When the toString method is called, the following steps are taken:
// 1. Get the [[Class]] property of this object.
// 2. Compute a string value by concatenating the three strings
// "[object ", Result(1), and "]".
// 3. Return Result(2).
// and this behavior survives the destruction of the execution context.
if (value instanceof Array || // Works quickly in same execution context.
// If value is from a different execution context then
// !(value instanceof Object), which lets us early out in the common
// case when value is from the same context but not an array.
// The {if (value)} check above means we don't have to worry about
// undefined behavior of Object.prototype.toString on null/undefined.
//
// HACK: In order to use an Object prototype method on the arbitrary
// value, the compiler requires the value be cast to type Object,
// even though the ECMA spec explicitly allows it.
(!(value instanceof Object) &&
(Object.prototype.toString.call(
/** @type {Object} */ (value)) == '[object Array]') ||
// In IE all non value types are wrapped as objects across window
// boundaries (not iframe though) so we have to do object detection
// for this edge case
typeof value.length == 'number' &&
typeof value.splice != 'undefined' &&
typeof value.propertyIsEnumerable != 'undefined' &&
!value.propertyIsEnumerable('splice')
)) {
return 'array';
}
// HACK: There is still an array case that fails.
// function ArrayImpostor() {}
// ArrayImpostor.prototype = [];
// var impostor = new ArrayImpostor;
// this can be fixed by getting rid of the fast path
// (value instanceof Array) and solely relying on
// (value && Object.prototype.toString.vall(value) === '[object Array]')
// but that would require many more function calls and is not warranted
// unless closure code is receiving objects from untrusted sources.
// IE in cross-window calls does not correctly marshal the function type
// (it appears just as an object) so we cannot use just typeof val ==
// 'function'. However, if the object has a call property, it is a
// function.
if (!(value instanceof Object) &&
(Object.prototype.toString.call(
/** @type {Object} */ (value)) == '[object Function]' ||
typeof value.call != 'undefined' &&
typeof value.propertyIsEnumerable != 'undefined' &&
!value.propertyIsEnumerable('call'))) {
return 'function';
}
} else {
return 'null';
}
} else if (s == 'function' && typeof value.call == 'undefined') {
// In Safari typeof nodeList returns 'function', and on Firefox
// typeof behaves similarly for HTML{Applet,Embed,Object}Elements
// and RegExps. We would like to return object for those and we can
// detect an invalid function by making sure that the function
// object has a call method.
return 'object';
}
return s;
};
/**
* Inherit the prototype methods from one constructor into another.
*
* Usage:
* <pre>
* function ParentClass(a, b) { }
* ParentClass.prototype.foo = function(a) { }
*
* function ChildClass(a, b, c) {
* ParentClass.call(this, a, b);
* }
*
* js_cols.inherits(ChildClass, ParentClass);
*
* var child = new ChildClass('a', 'b', 'see');
* child.foo(); // works
* </pre>
*
* In addition, a superclass' implementation of a method can be invoked
* as follows:
*
* <pre>
* ChildClass.prototype.foo = function(a) {
* ChildClass.superClass_.foo.call(this, a);
* // other code
* };
* </pre>
*
* @param {Function} childCtor Child class.
* @param {Function} parentCtor Parent class.
*/
js_cols.inherits = function(childCtor, parentCtor) {
/** @constructor */
function tempCtor() {};
tempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new tempCtor();
childCtor.prototype.constructor = childCtor;
};
js_cols.addDependency("js_cols/ABItem.js", ['js_cols.ABItem'], []);
js_cols.addDependency("js_cols/ABTreeBag.js", ['js_cols.ABTreeBag'], ['js_cols.ABTreeSet']);
js_cols.addDependency("js_cols/ABTreeMap.js", ['js_cols.ABTreeMap'], ['js_cols.LinkedList', 'js_cols.ABItem']);
js_cols.addDependency("js_cols/ABTreeMultiMap.js", ['js_cols.ABTreeMultiMap'], ['js_cols.ABTreeMap']);
js_cols.addDependency("js_cols/ABTreeSet.js", ['js_cols.ABTreeSet'], ['js_cols.LinkedList', 'js_cols.ABItem']);
js_cols.addDependency("js_cols/HashBag.js", ['js_cols.HashBag'], ['js_cols.HashMultiMap']);
js_cols.addDependency("js_cols/HashMap.js", ['js_cols.HashMap'], []);
js_cols.addDependency("js_cols/HashMultiMap.js", ['js_cols.HashMultiMap'], []);
js_cols.addDependency("js_cols/HashSet.js", ['js_cols.HashSet'], ['js_cols.HashMap']);
js_cols.addDependency("js_cols/IntervalHeap.js", ['js_cols.IntervalHeap'], []);
js_cols.addDependency("js_cols/LinkedHashMap.js", ['js_cols.LinkedHashMap'], ['js_cols.LinkedList', 'js_cols.HashMap']);
js_cols.addDependency("js_cols/LinkedList.js", ['js_cols.LinkedList'], []);
js_cols.addDependency("js_cols/Queue.js", ['js_cols.Queue'], ['js_cols.LinkedList']);
js_cols.addDependency("js_cols/RBnode.js", ['js_cols.RBnode'], []);
js_cols.addDependency("js_cols/RedBlackBag.js", ['js_cols.RedBlackBag'], ['js_cols.RedBlackSet', 'js_cols.RBnode']);
js_cols.addDependency("js_cols/RedBlackMap.js", ['js_cols.RedBlackMap'], ['js_cols.RBnode']);
js_cols.addDependency("js_cols/RedBlackMultiMap.js", ['js_cols.RedBlackMultiMap'], ['js_cols.RedBlackMap']);
js_cols.addDependency("js_cols/RedBlackSet.js", ['js_cols.RedBlackSet'], ['js_cols.RBnode']);
js_cols.addDependency("js_cols/Stack.js", ['js_cols.Stack'], ['js_cols.LinkedList']);
//Copyright Thomas Stjernegaard Jeppesen. All Rights Reserved.
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS-IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
js_cols.provide('js_cols.RBnode');
js_cols.RBnode = function(tree){
this.tree = tree;
this.right = this.tree.sentinel;
this.left = this.tree.sentinel;
};
//Copyright Thomas Stjernegaard Jeppesen. All Rights Reserved.
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS-IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
js_cols.require('js_cols.RBnode');
js_cols.provide('js_cols.RedBlackMap');
/**
*
*
*
* js_cols.RedBlackMap provides the implementation of a Red Black Tree map datastructure. The tree
* maintains a set of values, sorted by their corresponding keys. The key/value pairs can be
* inserted and deleted efficiently in their sorted order as the tree enforces a logn
* maximum height. This implementation provides guaranteed log(n) time cost for the
* <tt>contains</tt>, <tt>insert</tt> and <tt>remove</tt>
* operations. Algorithms are adaptations of those in Thomas H. Cormen, Charles E. Leiserson,
* Ronald L. Rivest, Clifford Stein <I>Introduction to Algorithms, second edition</I>.<p>
*
* The assymptotic running time for important operations are below:
* <pre>
* Method big-O
* ----------------------------------------------------------------------------
* - clear O(1)
* - clone O(n logn)
* - contains O(logn)
* - containsAll O(m logn) m is the cardinality of the supplied collection
* - every O(n * O(f)) f is the function supplied as argument
* - filter O(n * O(f)) f is the function supplied as argument
* - forEach O(n * O(f)) f is the function supplied as argument
* - get O(logn)
* - getValues O(n)
* - insert O(logn)
* - insertAll O(m logn) m is the cardinality of the supplied collection
* - map O(n * O(f)) f is the function supplied as argument
* - remove O(logn)
* - removeAll O(m logn) m is the cardinality of the supplied collection
* - some O(n * O(f)) f is the function supplied as argument
* - contains O(n * O(f)) f is the function supplied as argument
* </pre>
*
*
* Constructs a new Red Black map
* @param {Function=} compare_func an optional compare function to compare the keys. This function should
* take two values, a and b, and return x where:
* <pre>
* x < 0 if a < b,
* x > 0 if a > b,
* x = 0 otherwise
* </pre>
* if not defined, a default compare function for <tt>numbers</tt> will be used
* @constructor
* @public
*/
js_cols.RedBlackMap = function(compare_func) {
this.RED = true;
this.BLACK = false;
this.size = 0;
this.sentinel = new js_cols.RBnode(this);
this.sentinel.color = this.BLACK;
this.root = this.sentinel; // when the tree is empty, root = sentinel
this.root.parent = this.sentinel;
/**
* Comparison function used to compare values in the tree. This function should
* take two values, a and b, and return x where:
* <pre>
* x < 0 if a < b,
* x > 0 if a > b,
* x = 0 otherwise
* </pre>
*
* @type {Function}
* @private
*/
this.compare = compare_func || this.default_compare;
};
/**
* A default compare function, if compare_func is not specified.
* @private
*/
js_cols.RedBlackMap.prototype.default_compare = function (a,b) {
if (a < b) return -1;
else if (b < a) return 1;
else return 0;
};
/**
* Clones a set and returns a new set.
* @return {!js_cols.RedBlackMap} A new map with the same key-value pairs.
*/
js_cols.RedBlackMap.prototype.clone = function() {
var rv = new js_cols.RedBlackMap(this.compare);
rv.insertAll(this);
return rv;
};
/**
* Removes all elements from this set
*
*/
js_cols.RedBlackMap.prototype.clear = function() {
this.size = 0;
this.sentinel = new js_cols.RBnode(this);
this.sentinel.color = this.BLACK;
this.root = this.sentinel; // when the tree is empty, root = sentinel
this.root.parent = this.sentinel;
};
/**
* A helper function, used for tree balancing
* @param x {js_cols.RBnode} the node to rotate about
* @private
*/
js_cols.RedBlackMap.prototype.leftRotate = function(x) {
var y = x.right;
x.right = y.left;
if (y.left != this.sentinel) y.left.parent= x;
y.parent = x.parent;
if (x.parent == this.sentinel) {
this.root = y;
}
else if(x==x.parent.left) {
x.parent.left = y;
}
else {
x.parent.right = y;
}
y.left = x;
x.parent = y;
};
/**
* A helper function, used for tree balancing
* @param x {js_cols.RBnode} the node to rotate about
* @private
*/
js_cols.RedBlackMap.prototype.rightRotate= function(x) {
var y = x.left;
x.left = y.right;
if (y.right != this.sentinel) y.right.parent= x;
y.parent = x.parent;
if (x.parent == this.sentinel) {
this.root = y;
}
else if(x==x.parent.right) {
x.parent.right = y;
}
else {
x.parent.left = y;
}
y.right = x;
x.parent = y;
};
/**
* Inserts a key/value pair into the tree
* @param {*} key the key used for ordering and location
* @param {*} element the value associated with the key
* @public
*/
js_cols.RedBlackMap.prototype.insert = function(key, value) {
if (!this.contains(key)) {
var z = new js_cols.RBnode(this);
z.key = key;
z.value = value;
var y = this.sentinel;
var x = this.root;
while (x != this.sentinel) {
y=x;
//if (z.key < x.key) x = x.left;
if (this.compare(z.key, x.key) <0) x = x.left;
else x=x.right;
}
z.parent = y;
if (y == this.sentinel) {
this.root = z;
}
//else if(z.key < y.key) {
else if(this.compare(z.key, y.key) < 0) {
y.left = z;
}
else {
y.right = z;
}
z.left = this.sentinel;
z.right = this.sentinel;
z.color = this.RED;
this.insertFixup(z);
this.size++;
}
else {
var node = this.get_(key);
node.value = value;
}
};
/**
* Helper method for insertAll
* @private
*/
js_cols.RedBlackMap.prototype.insertSwapped = function(value, key) {
this.insert(key, value);
};
/**