-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
758 lines (665 loc) · 20.8 KB
/
map.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
/**
* @fileOverview <title>Thread-unsafe map implementations</title>
* @file $Id$
* @requires object.js
*
* @author (C) 2009-2013 <a href="mailto:js@PointedEars.de">Thomas Lahn</a>
*
* @partof PointedEars' JavaScript Extensions (JSX)
*
* JSX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JSX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JSX. If not, see <http://www.gnu.org/licenses/>.
*/
var jsx;
(function () {
"use strict";
if (typeof jsx == "undefined")
{
/**
* @namepspace
*/
jsx = {};
}
/**
* @namespace
*/
jsx.map = {
version: "$Id$",
/**
* An object that maps keys to values.
*
* A map cannot contain duplicate keys; each key can map to at most
* one value. Keys may be object references.
*
* @function
*/
Map: jsx.object.extend((
/**
* @return {jsx.map.Map}
*/
function () {
/**
* A value in the map, to distinguish it from built-in types
*
* @param value Value to be stored
* @param key Optional key for the value. Used by {@link _Bucket}.
* @private
*/
function _ValueContainer(value, key)
{
this.putValue(value, key);
}
/**
* @param value
* @return {boolean}
* <code>true</code> if <var>v</var> was created using
* {@link _ValueContainer}, otherwise <code>false</code>
*/
_ValueContainer.isInstance = function (value) {
return !!value && value.constructor === this;
};
_ValueContainer.prototype.putValue = function (value, key) {
this._value = value;
this._key = key;
};
_ValueContainer.prototype.getValue = function () {
return this._value;
};
_ValueContainer.prototype.getKey = function () {
return this._key;
};
var _getDataObject = jsx.object.getDataObject;
/**
* In order not to overwrite or shadow built-in properties, if a key is
* the name of such a property, an alias property name is used instead.
* A maximum length of the alias property name is necessary to avoid
* infinite iteration for finding an alias if the previously computed
* alias is already used as name of a built-in property.
*/
var _maxAliasLength = 255;
var _isObjectRef = jsx.object.isObject;
/**
* Returns a safe key, that is, a property name that is not yet used
* by the ECMAScript implementation.
*
* @param unsafeKey
* Potentially unsafe key, that is, a property name that may be
* already used by the ECMAScript implementation.
* @return {string}
* @throws jsx.map#KeyError if the user-defined maximum key length
* does not suffice to satisfy a safe key.
* @private
*/
function _getSafeKey (obj, unsafeKey)
{
var
safeKey = unsafeKey,
constructor = obj.constructor,
proto = (constructor ? constructor.prototype : null);
if (_isObjectRef(unsafeKey))
{
var _class = jsx.object.getClass(unsafeKey);
var constructorName = jsx.object.getFunctionName(unsafeKey.constructor);
safeKey = (_class ? "[" + _class + "]" : "")
+ (constructorName ? constructorName + "()" : "")
+ (typeof unsafeKey.nodeName != "undefined"
? unsafeKey.nodeName
: "")
+ (typeof unsafeKey.className != "undefined"
&& /\S/.test(unsafeKey.className)
? "." + unsafeKey.className.replace(/^\s+|\s+$/g, "").replace(/\s+/, ".")
: "")
+ (typeof unsafeKey.id != "undefined"
? "#" + unsafeKey.id
: "");
}
/*
* Try until an unused (not inherited and not own non-_ValueContainer) property
* was found or the maximum alias key length has been reached
*/
while (proto && (_hasOwnProperty(proto, safeKey)
|| (_hasOwnProperty(obj, safeKey)
&& !_ValueContainer.isInstance(obj[safeKey]))))
{
if (safeKey.length > _maxAliasLength)
{
jsx.throwThis("jsx.map.KeyError", unsafeKey);
}
safeKey += "_";
}
return safeKey;
}
function _Bucket()
{
this._items = _getDataObject();
}
/**
* @param value
* @return {boolean}
* <code>true</code> if <var>v</var> was created using
* {@link _Bucket}, otherwise <code>false</code>
*/
_Bucket.isInstance = function (value) {
return !!value && value.constructor === this;
};
_Bucket._nextId = 1;
_Bucket.extend(null, {
/**
* Returns the next string key for this bucket
*
* @memberOf _Bucket.prototype
* @private
* @return {string}
*/
_getNextId: function () {
return "key" + (_Bucket._nextId++) + "_";
},
/**
* Puts a value in the bucket
*
* @param oKey
* @return {_ValueContainer}
* The previous container for this key,
* <code>null</code> if none.
*/
put: function (oKey, value) {
var prevValue = null;
var items = this._items;
var sKey = this.find(oKey);
if (!sKey)
{
sKey = _getSafeKey(items, this._getNextId());
}
else
{
prevValue = items[sKey];
}
items[sKey] = new _ValueContainer(value, oKey);
return prevValue;
},
/**
* Gets a value from the bucket
*
* @param oKey
* @return {string}
* The value for the object key
*/
get: function (oKey) {
var sKey = this.find(oKey);
if (!sKey)
{
return void 0;
}
return this._items[sKey];
},
/**
* Returns the string key of an object
*
* @param oKey
* @return {string|boolean}
* The string key of <var>oKey</var> if it is in the bucket,
* <code>false</code> otherwise.
*/
find: function (oKey) {
var items = this._items;
for (var sKey in items)
{
var value = items[sKey];
if (_ValueContainer.isInstance(value) && value.getKey() == oKey)
{
return sKey;
}
}
return false;
},
/**
* Removes an object key from the bucket
*
* @param oKey
* @return {string|boolean}
* The removed value for the object key,
* or <code>false</code> if it was not in the bucket.
*/
remove: function (oKey) {
var sKey = this.find(oKey);
if (sKey)
{
var value = this._items[sKey];
delete this._items[sKey];
}
return value;
},
keys: function () {
var a = [];
var items = this._items;
for (var key in items)
{
var o = items[key];
if (_ValueContainer.isInstance(o))
{
a.push(o.getKey());
}
}
return a;
},
mappings: function () {
var a = [];
var items = this._items;
for (var p in items)
{
var o = items[p];
if (_ValueContainer.isInstance(o))
{
a.push([o.getKey(), o.getValue()]);
}
}
return a;
}
});
/* Imports */
var _hasOwnProperty = jsx.object._hasOwnProperty;
/**
* @param {jsx.map.Map} map
* The map whose mappings are to be placed in this map
* @param {Boolean} strictKeys (optional)
* If <code>true</code>, keys are stored as they are.
* Otherwise only object keys are stored as they are,
* and other keys are converted to string. The default
* is <code>false</code>.
* @constructor
*/
function _Map (map, strictKeys)
{
// var Map = arguments.callee;
/* Allows to use this as a factory */
if (!this || this.constructor != _Map)
{
return new _Map(map);
}
var _items = _getDataObject();
var _size = 0;
/**
* Gets the maximum alias property name length
* for further storage and retrieval operations.
* The default is 255.
*
* @return {boolean}
* <code>true</code> if successful,
* <code>false</code> otherwise.
* @public
*/
this.getMaxAliasLength = function () {
return _maxAliasLength;
};
/**
* Sets the maximum alias property name length
* for further storage and retrieval operations.
*
* @param {number} len
* Integer greater than 0 to define the maximum
* alias property name length. The default maximum is 255.
* @return {boolean}
* <code>true</code> if successful,
* <code>false</code> otherwise.
* @throws jsx.map#InvalidLengthError
* @public
*/
this.setMaxAliasLength = function (len) {
if (typeof len != "number")
{
len = parseInt(len, 10);
}
if (isNaN(len) || len < 1)
{
jsx.throwThis("jsx.map.InvalidLengthError");
}
_maxAliasLength = len;
return _maxAliasLength === len;
};
/**
* Returns the number of key-value mappings in this map
*
* @return {number}
* The number of key-value mappings in this map
* @public
*/
this.size = function () {
return _size;
};
/**
* Returns <code>true</code> if this map contains no key-value mappings
*
* @return {boolean}
* <code>true</code> if this map contains no key-value mappings
* @public
*/
this.isEmpty = function () {
return _size === 0;
};
/**
* Returns the value to which the specified key is mapped in this map
*
* @param key
* @param defaultValue
* @throws jsx.map#KeyError
* If there is no such key and no default value has been passed.
* @public
*/
this.get = function (key, defaultValue) {
var v = _items[_getSafeKey(_items, key)];
if (_isObjectRef(key))
{
if (_Bucket.isInstance(v))
{
v = v.get(key);
}
}
if (!v)
{
if (arguments.length > 1)
{
return defaultValue;
}
jsx.throwThis("jsx.map.KeyError", key);
}
return v.getValue();
};
/**
* Returns <code>true</code> if this map contains a mapping for the specified key
*
* @param key
* @return {boolean}
* @public
*/
this.containsKey = function (key) {
var value = _items[_getSafeKey(_items, key)];
return _ValueContainer.isInstance(value)
|| (_Bucket.isInstance(value) && !!value.find(key));
};
/**
* Associates the specified value with the specified key in this map
*
* @param key
* @param value
* @return {boolean}
* Previous value associated with specified key,
* or <code>undefined</code> if there was no mapping for <var>key</var>.
* An <code>undefined</code> return can also indicate that the Map
* previously associated <code>undefined</code> with the specified key.
* @throws jsx.map#KeyError
* @public
*/
this.set = function (key, value) {
var k = _getSafeKey(_items, key);
var v;
var prevValue = _items[k];
if (_isObjectRef(key) || (typeof key != "string" && this.strictKeys))
{
var bucket = prevValue;
if (_Bucket.isInstance(bucket))
{
if (!bucket.find(key))
{
++_size;
}
}
else
{
bucket = new _Bucket();
++_size;
}
prevValue = bucket.put(key, value);
v = bucket;
}
else
{
v = new _ValueContainer(value);
if (!prevValue)
{
++_size;
}
}
_items[k] = v;
return prevValue && prevValue.getValue();
};
/**
* Removes the mapping for the specified key from this map if present
*
* @param key
* Key whose mapping is to be removed from the map
* @return {any}
* The previous value associated with <var>key</var>,_value
* or <code>undefined</code> if there was no mapping for <var>key</var>.
* (An <code>undefined</code> return can also indicate that the map
* previously associated <code>undefined</code> with <var>key</var>.)
* @throws jsx.map#KeyError
* @public
*/
this.remove = function (key) {
var k = _getSafeKey(_items, key);
var prevValue = _items[k];
if (prevValue)
{
if (_isObjectRef(key))
{
if (_Bucket.isInstance(prevValue))
{
prevValue = prevValue.remove(key);
if (_ValueContainer.isInstance(prevValue))
{
--_size;
return prevValue.getValue();
}
return prevValue;
}
return void 0;
}
delete _items[k];
--_size;
return prevValue && prevValue.getValue();
}
return prevValue;
};
/**
* Removes all of the mappings from this map.
* The map will be empty after this call returns.
*
* @public
*/
this.clear = function () {
_items = _getDataObject();
_size = 0;
};
/**
* Returns <code>true</code> if this map maps one or more keys to the specified value.
*
* @param value
* Value whose presence in this map is to be tested
* @return {boolean}
* <code>true</code> if this map maps one or more keys
* to the specified value
* @public
*/
this.containsValue = function (value) {
for (var p in _items)
{
var o = _items[p];
if (_ValueContainer.isInstance(o) && o.getValue() === value)
{
return true;
}
if (_Bucket.isInstance(o) && o.containsValue(value))
{
return true;
}
}
return false;
};
/**
* Returns a list of the keys contained in this map
*
* @return {Array}
* @public
*/
this.keys = function () {
var a = [];
for (var p in _items)
{
var o = _items[p];
if (_ValueContainer.isInstance(o))
{
a.push(p);
}
else if (_Bucket.isInstance(o))
{
a.push.apply(a, o.keys());
}
}
return a;
};
/**
* Returns a list of the values contained in this map
*
* @return {Array}
* @public
*/
this.values = function () {
var a = [];
for (var p in _items)
{
var o = _items[p];
if (_ValueContainer.isInstance(o))
{
a.push(o.getValue());
}
else if (_Bucket.isInstance(o))
{
a.push.apply(a, o.values());
}
}
return a;
};
/**
* Returns a list of the mappings contained in this map
* as an {@link Array} of Arrays of key-value pairs.
* The inner Arrays consist of two elements each, the key
* and its associated value.
*
* @return {Array}
* @public
*/
this.mappings = function () {
var a = [];
for (var p in _items)
{
var o = _items[p];
if (_ValueContainer.isInstance(o))
{
a.push([p, o.getValue()]);
}
else if (_Bucket.isInstance(o))
{
var bucketMappings = o.mappings();
for (var i = 0, len = bucketMappings.length; i < len; ++i)
{
a.push(bucketMappings[i]);
}
}
}
return a;
};
/**
* Destructs the map so that all closures are released.
*
* @public
*/
this.destruct = function () {
_items = null;
};
this.strictKeys = !!strictKeys;
if (arguments.length > 0 && map != null)
{
this.putAll(map);
}
}
return _Map;
}()
).extend(null, {
/**
* Returns a shallow copy of this map
*
* @memberOf jsx.map.Map.prototype
* @return {jsx.map.Map}
* @public
*/
clone: function () {
return new this.constructor(this);
},
put: (function () {
var issuedWarning = false;
return function () {
if (!issuedWarning)
{
jsx.warn("Map.put(): For ECMAScript 2015+ compliant code, call set() instead");
issuedWarning = true;
}
this.set.apply(this, arguments);
};
}()),
/**
* Copies all of the mappings from the specified map to this map.
* These mappings will replace any mappings that this map had
* for any of the keys currently in the specified map.
*
* @param {jsx.map.Map} map
* @public
*/
putAll: function (map) {
if (!this.constructor.isInstance(map))
{
jsx.throwThis("jsx.InvalidArgumentError");
}
this.setMaxAliasLength(map.getMaxAliasLength());
for (var a = map.mappings(), i = a.length; i--;)
{
var o = a[i];
this.set(o[0], o[1]);
}
}
}), {
/**
* Returns <code>true</code> if the argument is a {@link jsx.map#Map Map}
*
* @memberOf jsx.map.Map
* @param {Object} o
* @return {boolean}
*/
isInstance: function (o) {
return !!o && o.constructor === this;
}
}),
KeyError: (
function (key) {
jsx.object.PropertyError.call(this);
this.message = "No such key: " + key;
}
).extend(jsx.object.PropertyError, {
name: "jsx.map.KeyError"
}),
InvalidLengthError: (
function () {
jsx.object.PropertyError.call(this, "InvalidLengthError");
}
).extend(jsx.object.PropertyError)
};
}());