/
sproutcore-datastore.js
12950 lines (10338 loc) · 394 KB
/
sproutcore-datastore.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
(function(exports) {
// ==========================================================================
// Project: SproutCore IndexSet
// Copyright: ©2011 Strobe Inc. and contributors.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
/*globals sc_assert */
var get = SC.get, set = SC.set, abs = Math.abs;
function isIndexSet(obj) {
return obj instanceof SC.IndexSet;
}
/** @private
iterates through a named range, setting hints every HINT_SIZE indexes
pointing to the nearest range start. The passed range must start on a
range boundary. It can end anywhere.
*/
function _hint(indexSet, start, length, content) {
if (content === undefined) content = indexSet._content;
var skip = SC.IndexSet.HINT_SIZE,
next = abs(content[start]), // start of next range
loc = start - (start % skip) + skip, // next hint loc
lim = start + length ; // stop
while (loc < lim) {
// make sure we are in current rnage
while ((next !== 0) && (next <= loc)) {
start = next ;
next = abs(content[start]) ;
}
// past end
if (next === 0) {
delete content[loc];
// do not change if on actual boundary
} else if (loc !== start) {
content[loc] = start ; // set hint
}
loc += skip;
}
}
/** @private
Walks a content array and copies its contents to a new array. For large
content arrays this is faster than using slice()
*/
function _sliceContent(c) {
if (c.length < 1000) return c.slice(); // use native when faster
var cur = 0, ret = [], next = c[0];
while(next !== 0) {
ret[cur] = next ;
cur = (next<0) ? (0-next) : next ;
next = c[cur];
}
ret[cur] = 0;
_hint(this, 0, cur, ret); // hints are not copied manually - add them
return ret ;
}
/**
@class
A collection of ranges. You can use an IndexSet to keep track of non-
continuous ranges of items in a parent array. IndexSet's are used for
selection, for managing invalidation ranges and other data-propogation.
Examples
---
var set = SC.IndexSet.create(ranges) ;
set.contains(index);
set.add(index, length);
set.remove(index, length);
// uses a backing SC.Array object to return each index
set.forEach(function(object) { .. })
// returns the index
set.forEachIndex(function(index) { ... });
// returns ranges
set.forEachRange(function(start, length) { .. });
Implementation Notes
---
An IndexSet stores indices on the object. A positive value great than the
index tells you the end of an occupied range. A negative values tells you
the end of an empty range. A value less than the index is a search
accelerator. It tells you the start of the nearest range.
@extends SC.Enumerable
@extends SC.MutableEnumerable
@extends SC.Copyable
@extends SC.Freezable
@since SproutCore 1.0
*/
SC.IndexSet = SC.Object.extend(SC.Enumerable, SC.MutableEnumerable, SC.Freezable, SC.Copyable,
/** @scope SC.IndexSet.prototype */ {
/**
Walk like a duck. You should use instanceof instead.
@deprecated
@type Boolean
@default YES
*/
isIndexSet: YES,
/**
Total number of indexes contained in the set
@type Number
*/
length: 0,
/**
One greater than the largest index currently stored in the set. This
is sometimes useful when determining the total range of items covering
the index set.
@type Number
*/
max: 0,
/**
The first index included in the set or -1.
@type Number
*/
min: function() {
var content = this._content,
cur = content[0];
return (cur === 0) ? -1 : (cur>0) ? 0 : abs(cur);
}.property('[]').cacheable(),
/**
When you create a new index set you can optional pass another index set
or a starting range to be added to the set.
*/
init: function(start, length) {
this._super();
// optimized method to clone an index set.
if (start && isIndexSet(start)) {
this._content = _sliceContent(start._content);
set(this, 'max', get(start, 'max'));
set(this, 'length', get(start, 'length'));
set(this, 'source', get(start, 'source'));
// otherwise just do a regular add
} else {
this._content = [0];
if (start !== undefined) this.add(start, length);
}
},
/**
Returns the first index in the set .
@type Number
*/
firstObject: function() {
return get(this, 'length')>0 ? get(this, 'min') : undefined;
}.property(),
/**
Returns the starting index of the nearest range for the specified
index.
@param {Number} index
@returns {Number} starting index
*/
rangeStartForIndex: function(index) {
var content = this._content,
max = get(this, 'max'),
ret, next, accel;
// fast cases
if (index >= max) return max ;
if (abs(content[index]) > index) return index ; // we hit a border
// use accelerator to find nearest content range
accel = index - (index % SC.IndexSet.HINT_SIZE);
ret = content[accel];
if (ret<0 || ret>index) ret = accel;
next = abs(content[ret]);
// now step forward through ranges until we find one that includes the
// index.
while (next < index) {
ret = next ;
next = abs(content[ret]);
}
return ret ;
},
/**
Returns YES if the passed index set contains the exact same indexes as
the receiver. If you pass any object other than an index set, returns NO.
@param {Object} obj another object.
@returns {Boolean}
*/
isEqual: function(obj) {
// optimize for some special cases
if (obj === this) return YES ;
if (!obj || !isIndexSet(obj) || (get(obj, 'max') !== get(this, 'max')) || (get(obj, 'length') !== get(this, 'length'))) return NO;
// ok, now we need to actually compare the ranges of the two.
var lcontent = this._content,
rcontent = obj._content,
cur = 0,
next = lcontent[cur];
do {
if (rcontent[cur] !== next) return NO ;
cur = abs(next) ;
next = lcontent[cur];
} while (cur !== 0);
return YES ;
},
/**
Returns the first index in the set before the passed index or null if
there are no previous indexes in the set.
@param {Number} index index to check
@returns {Number} index or -1
*/
indexBefore: function(index) {
if (index===0) return -1; // fast path
index--; // start with previous index
var content = this._content,
max = get(this, 'max'),
start = this.rangeStartForIndex(index);
if (!content) return null;
// loop backwards until we find a range that is in the set.
while((start===max) || (content[start]<0)) {
if (start === 0) return -1 ; // nothing before; just quit
index = start -1 ;
start = this.rangeStartForIndex(index);
}
return index;
},
/**
Returns the first index in the set after the passed index or null if
there are no additional indexes in the set.
@param {Number} index index to check
@returns {Number} index or -1
*/
indexAfter: function(index) {
var content = this._content,
max = get(this, 'max'),
start, next ;
if (!content || (index>=max)) return -1; // fast path
index++; // start with next index
// loop forwards until we find a range that is in the set.
start = this.rangeStartForIndex(index);
next = content[start];
while(next<0) {
if (next === 0) return -1 ; //nothing after; just quit
index = start = abs(next);
next = content[start];
}
return index;
},
/**
Returns YES if the index set contains the named index
@param {Number} start index or range
@param {Number} length optional range length
@returns {Boolean}
*/
contains: function(start, length) {
var content, cur, next, rstart, rnext;
// normalize input
if (length === undefined) {
if (start === null || start === undefined) return NO ;
if ('number' === typeof start) {
length = 1 ;
// if passed an index set, check each receiver range
} else if (start && isIndexSet(start)) {
if (start === this) return YES ; // optimization
content = start._content ;
cur = 0 ;
next = content[cur];
while (next !== 0) {
if ((next>0) && !this.contains(cur, next-cur)) return NO ;
cur = abs(next);
next = content[cur];
}
return YES ;
// passed just a hash range
} else {
length = start.length;
start = start.start;
}
}
rstart = this.rangeStartForIndex(start);
rnext = this._content[rstart];
return (rnext>0) && (rstart <= start) && (rnext >= (start+length));
},
/**
Returns YES if the index set contains any of the passed indexes. You
can pass a single index, a range or an index set.
@param {Number} start index, range, or IndexSet
@param {Number} length optional range length
@returns {Boolean}
*/
intersects: function(start, length) {
var content, cur, next, lim;
// normalize input
if (length === undefined) {
if ('number' === typeof start) {
length = 1 ;
// if passed an index set, check each receiver range
} else if (start && isIndexSet(start)) {
if (start === this) return YES ; // optimization
content = start._content ;
cur = 0 ;
next = content[cur];
while (next !== 0) {
if ((next>0) && this.intersects(cur, next-cur)) return YES ;
cur = abs(next);
next = content[cur];
}
return NO ;
} else {
length = start.length;
start = start.start;
}
}
cur = this.rangeStartForIndex(start);
content = this._content;
next = content[cur];
lim = start + length;
while (cur < lim) {
if (next === 0) return NO; // no match and at end!
if ((next > 0) && (next > start)) return YES ; // found a match
cur = abs(next);
next = content[cur];
}
return NO ; // no match
},
/**
Returns a new IndexSet without the passed range or indexes. This is a
convenience over simply cloning and removing. Does some optimizations.
@param {Number} start index, range, or IndexSet
@param {Number} length optional range length
@returns {SC.IndexSet} new index set
*/
without: function(start, length) {
if (start === this) return new SC.IndexSet(); // just need empty set
return this.copy().remove(start, length);
},
/**
Replace the index set's current content with the passed index set. This
is faster than clearing the index set adding the values again. It is
useful for when you want to reuse an existing index set.
@param {Number} start index, Range, or another IndexSet
@param {Number} length optional length of range.
@returns {SC.IndexSet} receiver
*/
replace: function(start, length) {
if (length === undefined) {
if ('number' === typeof start) {
length = 1 ;
} else if (start && isIndexSet(start)) {
var oldLen = get(this, 'length'), newLen = get(start, 'length');
this.enumerableContentWillChange(oldLen, newLen);
SC.beginPropertyChanges(this);
this._content = _sliceContent(start._content);
set(this, 'max', get(start, 'max'));
set(this, 'length', newLen);
set(this, 'source', get(start, 'source'));
SC.endPropertyChanges(this);
this.enumerableContentDidChange(oldLen, newLen);
return this ;
} else {
length = start.length;
start = start.start;
}
}
var oldlen = this.length;
this._content.length=1;
this._content[0] = 0;
this.length = this.max = 0 ; // reset without notifying since add()
return this.add(start, length);
},
/**
Adds the specified range of indexes to the set. You can also pass another
IndexSet to union the contents of the index set with the receiver.
@param {Number} start index, Range, or another IndexSet
@param {Number} length optional length of range.
@returns {SC.IndexSet} receiver
*/
add: function(start, length) {
if (get(this, 'isFrozen')) throw new Error(SC.FROZEN_ERROR);
var content, cur, next, notified;
// normalize IndexSet input
if (start && isIndexSet(start)) {
start.forEachRange(this.add, this);
return this ;
} else if (length === undefined) {
if (start === null || start === undefined) {
return this; // nothing to do
} else if ('number' === typeof start) {
length = 1 ;
} else {
length = start.length;
start = start.start;
}
} else if (length === null) length = 1 ;
// if no length - do nothing. - note captures when length != number
if (!(length > 0)) return this;
// special case - appending to end of set
var max = get(this, 'max'),
oldmax = max,
delta, value ;
content = this._content ;
if (start === max) {
this.enumerableContentWillChange();
notified = true;
// if adding to the end and the end is in set, merge.
if (start > 0) {
cur = this.rangeStartForIndex(start-1);
next = content[cur];
// just extend range at end
if (next > 0) {
delete content[max]; // no 0
content[cur] = max = start + length ;
start = cur ;
// previous range was not in set, just tack onto the end
} else {
content[max] = max = start + length;
}
} else {
content[start] = max = length;
}
content[max] = 0 ;
set(this, 'max', max);
set(this, 'length', get(this, 'length') + length) ;
length = max - start ;
// past end of last range, just add as a new range.
} else if (start > max) {
this.enumerableContentWillChange();
notified = true;
content[max] = 0-start; // empty!
content[start] = start+length ;
content[start+length] = 0; // set end
set(this, 'max', start + length) ;
set(this, 'length', this.length + length) ;
// affected range goes from starting range to end of content.
length = start + length - max ;
start = max ;
// otherwise, merge into existing range
} else {
// find nearest starting range. split or join that range
cur = this.rangeStartForIndex(start);
next = content[cur];
max = start + length ;
delta = 0 ;
// we are right on a boundary and we had a range or were the end, then
// go back one more.
if ((start>0) && (cur === start) && (next <= 0)) {
cur = this.rangeStartForIndex(start-1);
next = content[cur] ;
}
// previous range is not in set. splice it here
if (next < 0) {
content[cur] = 0-start ;
// if previous range extends beyond this range, splice afterwards also
if (abs(next) > max) {
content[start] = 0-max;
content[max] = next ;
} else content[start] = next;
// previous range is in set. merge the ranges
} else {
start = cur ;
if (next > max) {
// delta -= next - max ;
max = next ;
}
}
// at this point there should be clean starting point for the range.
// just walk the ranges, adding up the length delta and then removing
// the range until we find a range that passes last
cur = start;
while (cur < max) {
// get next boundary. splice if needed - if value is 0, we are at end
// just skip to last
value = content[cur];
if (value === 0) {
content[max] = 0;
next = max ;
delta += max - cur ;
if (!notified && delta>0) {
this.enumerableContentWillChange();
notified = true;
}
} else {
next = abs(value);
if (next > max) {
content[max] = value ;
next = max ;
}
// ok, cur range is entirely inside top range.
// add to delta if needed
if (value < 0) {
delta += next - cur ;
if (!notified && delta>0) {
this.enumerableContentWillChange();
notified = true;
}
}
}
delete content[cur] ; // and remove range
cur = next;
}
// cur should always === last now. if the following range is in set,
// merge in also - don't adjust delta because these aren't new indexes
if ((cur = content[max]) > 0) {
delete content[max];
max = cur ;
}
// finally set my own range.
content[start] = max ;
if (max > oldmax) set(this, 'max', max) ;
// adjust length
set(this, 'length', get(this, 'length') + delta);
// compute hint range
length = max - start ;
}
_hint(this, start, length);
if (notified) this.enumerableContentDidChange();
return this;
},
/**
Removes the specified range of indexes from the set
@param {Number} start index, Range, or IndexSet
@param {Number} length optional length of range.
@returns {SC.IndexSet} receiver
*/
remove: function(start, length) {
if (get(this, 'isFrozen')) throw new Error(SC.FROZEN_ERROR);
// normalize input
if (length === undefined) {
if (start === null || start === undefined) {
return this; // nothing to do
} else if ('number' === typeof start) {
length = 1 ;
// if passed an index set, just add each range in the index set.
} else if (isIndexSet(start)) {
start.forEachRange(this.remove, this);
return this;
} else {
length = start.length;
start = start.start;
}
}
if (!(length > 0)) return this; // handles when length != number
this.enumerableContentWillChange();
// special case - appending to end of set
var max = get(this, 'max'),
oldmax = max,
content = this._content,
cur, next, delta, value, last ;
// if we're past the end, do nothing.
if (start >= max) return this;
// find nearest starting range. split or join that range
cur = this.rangeStartForIndex(start);
next = content[cur];
last = start + length ;
delta = 0 ;
// we are right on a boundary and we had a range or were the end, then
// go back one more.
if ((start>0) && (cur === start) && (next > 0)) {
cur = this.rangeStartForIndex(start-1);
next = content[cur] ;
}
// previous range is in set. splice it here
if (next > 0) {
content[cur] = start ;
// if previous range extends beyond this range, splice afterwards also
if (next > last) {
content[start] = last;
content[last] = next ;
} else content[start] = next;
// previous range is not in set. merge the ranges
} else {
start = cur ;
next = abs(next);
if (next > last) {
last = next ;
}
}
// at this point there should be clean starting point for the range.
// just walk the ranges, adding up the length delta and then removing
// the range until we find a range that passes last
cur = start;
while (cur < last) {
// get next boundary. splice if needed - if value is 0, we are at end
// just skip to last
value = content[cur];
if (value === 0) {
content[last] = 0;
next = last ;
} else {
next = abs(value);
if (next > last) {
content[last] = value ;
next = last ;
}
// ok, cur range is entirely inside top range.
// add to delta if needed
if (value > 0) delta += next - cur ;
}
delete content[cur] ; // and remove range
cur = next;
}
// cur should always === last now. if the following range is not in set,
// merge in also - don't adjust delta because these aren't new indexes
if ((cur = content[last]) < 0) {
delete content[last];
last = abs(cur) ;
}
// set my own range - if the next item is 0, then clear it.
if (content[last] === 0) {
delete content[last];
content[start] = 0 ;
set(this, 'max', start); //max has changed
} else {
content[start] = 0-last ;
}
// adjust length
set(this, 'length', get(this, 'length') - delta);
// compute hint range
length = last - start ;
_hint(this, start, length);
this.enumerableContentDidChange();
return this;
},
/**
Clears the set
*/
clear: function() {
if (get(this, 'isFrozen')) throw new Error(SC.FROZEN_ERROR);
var oldLen = get(this, 'length');
if (oldLen>0) this.enumerableContentWillChange();
SC.beginPropertyChanges(this);
this._content.length=1;
this._content[0] = 0;
set(this, 'length', 0);
set(this, 'max', 0);
SC.endPropertyChanges(this);
if (oldLen > 0) this.enumerableContentDidChange();
},
/**
Add all the ranges in the passed array.
@param {Enumerable} objects The list of ranges you want to add
*/
addEach: function(objects) {
if (get(this, 'isFrozen')) throw new Error(SC.FROZEN_ERROR);
SC.beginPropertyChanges(this);
objects.forEach(function(idx) { this.add(idx); }, this);
SC.endPropertyChanges(this);
return this ;
},
/**
Removes all the ranges in the passed array.
@param {Object...} objects The list of objects you want to remove
*/
removeEach: function(objects) {
if (get(this, 'isFrozen')) throw new Error(SC.FROZEN_ERROR);
SC.beginPropertyChanges(this);
objects.forEach(function(idx) { this.remove(idx); }, this);
SC.endPropertyChanges(this);
return this ;
},
/**
Clones the set into a new set.
*/
copy: function() {
return new SC.IndexSet(this);
},
/** @private (nodoc) */
clone: SC.alias('copy'),
/** @private (nodoc) */
slice: SC.alias('copy'),
/**
Returns a string describing the internal range structure. Useful for
debugging.
@returns {String}
*/
inspect: function() {
var content = this._content,
len = content.length,
idx = 0,
ret = [],
item;
for(idx=0;idx<len;idx++) {
item = content[idx];
if (item !== undefined) ret.push("%@:%@".fmt(idx,item));
}
return "SC.IndexSet<%@>".fmt(ret.join(' , '));
},
/**
Invoke the callback, passing each occuppied range instead of each
index. This can be a more efficient way to iterate in some cases. The
callback should have the signature:
callback(start, length, indexSet, source) { ... }
If you pass a target as a second option, the callback will be called in
the target context.
@param {Function} callback The method to run on each iteration
@param {Object} target the object to call the callback on
@returns {SC.IndexSet} receiver
*/
forEachRange: function(callback, target) {
var content = this._content,
cur = 0,
next = content[cur],
source = this.source;
if (target === undefined) target = null ;
while (next !== 0) {
if (next > 0) callback.call(target, cur, next - cur, this, source);
cur = abs(next);
next = content[cur];
}
return this ;
},
/**
Invokes the callback for each index within the passed start/length range.
Otherwise works just like regular forEach().
@param {Number} start starting index
@param {Number} length length of range
@param {Function} callback
@param {Object} target
@returns {SC.IndexSet} receiver
*/
forEachIn: function(start, length, callback, target) {
var content = this._content,
cur = 0,
idx = 0,
lim = start + length,
source = this.source,
next = content[cur];
if (target === undefined) target = null ;
while (next !== 0) {
if (cur < start) cur = start ; // skip forward
while((cur < next) && (cur < lim)) {
callback.call(target, cur++, idx++, this, source);
}
if (cur >= lim) {
cur = next = 0 ;
} else {
cur = abs(next);
next = content[cur];
}
}
return this ;
},
/**
Total number of indexes within the specified range.
@param {Number|SC.IndexSet} start index, range object or IndexSet
@param {Number} length optional range length
@returns {Number} count of indexes
*/
lengthIn: function(start, length) {
var ret = 0 ;
// normalize input
if (length === undefined) {
if (start === null || start === undefined) {
return 0; // nothing to do
} else if ('number' === typeof start) {
length = 1 ;
// if passed an index set, just add each range in the index set.
} else if (isIndexSet(start)) {
start.forEachRange(function(start, length) {
ret += this.lengthIn(start, length);
}, this);
return ret;
} else {
length = start.length;
start = start.start;
}
}
// fast path
if (get(this, 'length') === 0) return 0;
var content = this._content,
cur = 0,
next = content[cur],
lim = start + length ;
while (cur<lim && next !== 0) {
if (next>0) {
ret += (next>lim) ? lim-cur : next-cur;
}
cur = abs(next);
next = content[cur];
}
return ret ;
},
// ..........................................................
// OBJECT API
//
/**
Optionally set the source property on an index set and then you can
iterate over the actual object values referenced by the index set. See
indexOf(), lastIndexOf(), forEachObject(), addObject() and removeObject().
*/
source: null,
/**
Returns the first index in the set that matches the passed object. You
must have a source property on the set for this to work.
@param {Object} object the object to check
@param {Number} startAt optional starting point
@returns {Number} found index or -1 if not in set
*/
indexOf: function(object, startAt) {
var source = get(this, 'source');
if (!source) throw "%@.indexOf() requires source".fmt(this);
var len = get(source, 'length'),
// start with the first index in the set
content = this._content,
cur = content[0]<0 ? abs(content[0]) : 0,
idx ;
while(cur>=0 && cur<len) {
idx = source.indexOf(object, cur);
if (idx<0) return -1 ; // not found in source
if (this.contains(idx)) return idx; // found in source and in set.
cur = idx+1;
}
return -1; // not found
},
/**
Returns the last index in the set that matches the passed object. You
must have a source property on the set for this to work.
@param {Object} object the object to check
@param {Number} startAt optional starting point
@returns {Number} found index or -1 if not in set
*/
lastIndexOf: function(object, startAt) {
var source = get(this, 'source');
if (!source) throw "%@.lastIndexOf() requires source".fmt(this);
// start with the last index in the set
var len = get(source, 'length'),
cur = get(this, 'max')-1,
idx ;
if (cur >= len) cur = len-1;
while (cur>=0) {
idx = source.lastIndexOf(object, cur);
if (idx<0) return -1 ; // not found in source
if (this.contains(idx)) return idx; // found in source and in set.
cur = idx+1;