summaryrefslogtreecommitdiff
path: root/tests/examplefiles/CPDictionary.j
blob: 50243f1dfd3b6af97be003fc6e12ad10259da4fc (plain)
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
/*
 * CPDictionary.j
 * Foundation
 *
 * Created by Francisco Tolmasky.
 * Copyright 2008, 280 North, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

//@import "CPRange.j"
@import "CPObject.j"
@import "CPEnumerator.j"
@import "CPException.j"

/* @ignore */
@implementation _CPDictionaryValueEnumerator : CPEnumerator
{
    CPEnumerator    _keyEnumerator;
    CPDictionary    _dictionary;
}

- (id)initWithDictionary:(CPDictionary)aDictionary
{
    self = [super init];
    
    if (self)
    {
        _keyEnumerator = [aDictionary keyEnumerator];
        _dictionary = aDictionary;
    }
    
    return self;
}

- (id)nextObject
{
    var key = [_keyEnumerator nextObject];
    
    if (!key)
        return nil;

    return [_dictionary objectForKey:key];
}

@end

/*! 
    @class CPDictionary
    @ingroup foundation
    @brief A mutable key-value pair collection.

    A dictionary is the standard way of passing around key-value pairs in
    the Cappuccino framework. It is similar to the
    <a href="http://java.sun.com/javase/6/docs/api/index.html">Java map interface</a>,
    except all keys are CPStrings and values can be any
    Cappuccino or JavaScript object.

    If you are familiar with dictionaries in Cocoa, you'll notice that
    there is no CPMutableDictionary class. The regular CPDictionary
    has \c -setObject:forKey: and \c -removeObjectForKey: methods.
    In Cappuccino there is no distinction between immutable and mutable classes.
    They are all mutable.
*/
@implementation CPDictionary : CPObject
{
}

/*
    @ignore
*/
+ (id)alloc
{
    return new objj_dictionary();
}

/*!
    Returns a new empty CPDictionary.
*/
+ (id)dictionary
{
    return [[self alloc] init];
}

/*!
    Returns a new dictionary, initialized with the contents of \c aDictionary.
    @param aDictionary the dictionary to copy key-value pairs from
    @return the new CPDictionary
*/
+ (id)dictionaryWithDictionary:(CPDictionary)aDictionary
{
    return [[self alloc] initWithDictionary:aDictionary];
}

/*!
    Creates a new dictionary with single key-value pair.
    @param anObject the object for the paring
    @param aKey the key for the pairing
    @return the new CPDictionary
*/
+ (id)dictionaryWithObject:(id)anObject forKey:(id)aKey
{
    return [[self alloc] initWithObjects:[anObject] forKeys:[aKey]];
}

/*!
    Creates a dictionary with multiple key-value pairs.
    @param objects the objects to place in the dictionary
    @param keys the keys for each of the objects
    @throws CPInvalidArgumentException if the number of objects and keys is different
    @return the new CPDictionary
*/
+ (id)dictionaryWithObjects:(CPArray)objects forKeys:(CPArray)keys
{
    return [[self alloc] initWithObjects:objects forKeys:keys];
}

/*!
    Creates a dictionary with multiple key-value pairs.
    @param JavaScript object
    @return the new CPDictionary
*/
+ (id)dictionaryWithJSObject:(JSObject)object
{
    return [self dictionaryWithJSObject:object recursively:NO];
}

/*!
    Creates a dictionary with multiple key-value pairs, recursively.
    @param JavaScript object
    @return the new CPDictionary
*/
+ (id)dictionaryWithJSObject:(JSObject)object recursively:(BOOL)recursively
{
    var dictionary = [[self alloc] init];
        
    for (var key in object)
    {
        var value = object[key];
    
        if (recursively && value.constructor === Object)
            value = [CPDictionary dictionaryWithJSObject:value recursively:YES];
    
        [dictionary setObject:value forKey:key];
    }
    
    return dictionary;
}

/*!
    Creates and returns a dictionary constructed by a given pairs of keys and values.
    @param firstObject first object value
    @param ... key for the first object and ongoing value-key pairs for more objects.
    @throws CPInvalidArgumentException if the number of objects and keys is different
    @return the new CPDictionary
    
    Assuming that there's no object retaining in Cappuccino, you can create
    dictionaries same way as with alloc and initWithObjectsAndKeys:
    var dict = [CPDictionary dictionaryWithObjectsAndKeys:
    @"value1", @"key1",
    @"value2", @"key2"];
    
    Note, that there's no final nil like in Objective-C/Cocoa.
    
    @see [CPDictionary initWithObjectsAndKeys:]
*/
+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ...
{
    arguments[0] = [self alloc];
    arguments[1] = @selector(initWithObjectsAndKeys:);
    
    return objj_msgSend.apply(this, arguments);
}

/*!
    Initializes the dictionary with the contents of another dictionary.
    @param aDictionary the dictionary to copy key-value pairs from
    @return the initialized dictionary
*/
- (id)initWithDictionary:(CPDictionary)aDictionary
{
    var key = "",
        dictionary = [[CPDictionary alloc] init];
    
    for (key in aDictionary._buckets)
        [dictionary setObject:[aDictionary objectForKey:key] forKey:key];
        
    return dictionary;
}

/*!
    Initializes the dictionary from the arrays of keys and objects.
    @param objects the objects to put in the dictionary
    @param keyArray the keys for the objects to put in the dictionary
    @throws CPInvalidArgumentException if the number of objects and keys is different
    @return the initialized dictionary
*/
- (id)initWithObjects:(CPArray)objects forKeys:(CPArray)keyArray
{
    self = [super init];

    if ([objects count] != [keyArray count])
        [CPException raise:CPInvalidArgumentException reason:"Counts are different.("+[objects count]+"!="+[keyArray count]+")"];

    if (self)
    {
        var i = [keyArray count];
        
        while (i--)
            [self setObject:objects[i] forKey:keyArray[i]];
    }
    
    return self;
}

/*!
    Creates and returns a dictionary constructed by a given pairs of keys and values.
    @param firstObject first object value
    @param ... key for the first object and ongoing value-key pairs for more objects.
    @throws CPInvalidArgumentException if the number of objects and keys is different
    @return the new CPDictionary
    
    You can create dictionaries this way:
    var dict = [[CPDictionary alloc] initWithObjectsAndKeys:
    @"value1", @"key1",
    @"value2", @"key2"];
    
    Note, that there's no final nil like in Objective-C/Cocoa.
*/
- (id)initWithObjectsAndKeys:(id)firstObject, ...
{
    var argCount = arguments.length;
    
    if (argCount % 2 !== 0)
        [CPException raise:CPInvalidArgumentException reason:"Key-value count is mismatched. (" + argCount + " arguments passed)"];

    self = [super init];
    
    if (self)
    {
        // The arguments array contains self and _cmd, so the first object is at position 2.
        var index = 2;
        
        for(; index < argCount; index += 2)
        {
            var value = arguments[index];

            if (value === nil)
                break;

            [self setObject:value forKey:arguments[index + 1]];
        }
    }

    return self;
}

/*!
    return a copy of the receiver (does not deep copy the objects contained in the dictionary).
*/
- (CPDictionary)copy
{
    return [CPDictionary dictionaryWithDictionary:self];
}

/*!
    Returns the number of entries in the dictionary
*/
- (int)count
{
    return count;
}

/*!
    Returns an array of keys for all the entries in the dictionary.
*/
- (CPArray)allKeys
{
    return _keys;
}

/*!
    Returns an array of values for all the entries in the dictionary.
*/
- (CPArray)allValues
{
    var index = _keys.length,
        values = [];
        
    while (index--)
        values.push(dictionary_getValue(self, [_keys[index]]));

    return values;
}

/*!
    Returns an enumerator that enumerates over all the dictionary's keys.
*/
- (CPEnumerator)keyEnumerator
{
    return [_keys objectEnumerator];
}

/*!
    Returns an enumerator that enumerates over all the dictionary's values.
*/
- (CPEnumerator)objectEnumerator
{
    return [[_CPDictionaryValueEnumerator alloc] initWithDictionary:self];
}

/*!
    Compare the receiver to this dictionary, and return whether or not they are equal. 
*/
- (BOOL)isEqualToDictionary:(CPDictionary)aDictionary
{
    if (count !== [aDictionary count])
        return NO;

    var index = count;
    while (index--)
    {
        var currentKey = _keys[index],
            lhsObject = _buckets[currentKey],
            rhsObject = aDictionary._buckets[currentKey];

        if (lhsObject === rhsObject)
            continue;
            
        if (lhsObject.isa && rhsObject.isa && [lhsObject respondsToSelector:@selector(isEqual:)] && [lhsObject isEqual:rhsObject])
            continue;
        
        return NO;
    }

    return YES;
}

/*
    Instance.isEqualToDictionary(aDictionary)
    {
        if(this.count()!=aDictionary.count()) return NO;
        
        var i= this._keys.count();
        while(i--) if(this.objectForKey(this._keys[i])!=aDictionary.objectForKey(this._keys[i])) return NO;
        
        return YES;
    }
    
    Instance.allKeys()
    {
        return this._keys;
    }
    
    Instance.allKeysForObject(anObject)
    {
        var i= 0,
            keys= CPArray.array(),
            count= this.count();
        
        while((i= this._objects.indexOfObjectInRage(0, count-i))!=CPNotFound) keys.addObject(this._keys[i]);
        
        return keys;
    }
    
    Instance.allValues()
    {
        return this._objects;
    }
    
    Instance.keyEnumerator()
    {
        return this._keys.objectEnumerator();
    }
    
    Instance.keysSortedByValueUsingSelector(aSelector)
    {
        var dictionary= this,
            objectSelector= function(rhs)
            {
                return aSelector.apply(dictionary.objectForKey(this), [dictionary.objectForKey(rhs)]);
            };
        
        return this._keys.sortedArrayUsingSelector(objectSelector);
    }
    
    Instance.objectEnumerator()
    {
        return this._objects.objectEnumerator();
    }
*/
/*!
    Returns the object for the entry with key \c aKey.
    @param aKey the key for the object's entry
    @return the object for the entry
*/
- (id)objectForKey:(CPString)aKey
{
    var object = _buckets[aKey];
    
    return (object === undefined) ? nil : object;
}
/*
    Instance.objectsForKeys(keys, aNotFoundMarker)
    {
        var i= keys.length,
            objects= CPArray.array();
        
        while(i--)
        {
            var object= this.objectForKey(keys[i]);
            objects.addObject(object==nil?aNotFoundMarker:object);
        }
        
        return objects;
    }
    
    Instance.valueForKey(aKey)
    {
        if(aKey.length && aKey[0]=="@") return this.objectForKey(aKey.substr(1));
        
        return base.valueForKey(aKey);
    }
    
    //
    
    Instance.addEntriesFromDictionary(aDictionary)
    {
        var key,
            keyEnumerator= aDictionary.keyEnumerator();

        while(key= keyEnumerator.nextObject()) this.setObjectForKey(aDictionary.objectForKey(key), key);
    }
*/
/*!
    Removes all the entries from the dictionary.
*/
- (void)removeAllObjects
{
    _keys = [];
    count = 0;
    _buckets = {};
}

/*!
    Removes the entry for the specified key.
    @param aKey the key of the entry to be removed
*/
- (void)removeObjectForKey:(id)aKey
{
    dictionary_removeValue(self, aKey);
}

/*!
    Removes each entry in allKeys from the receiver.
    @param allKeys an array of keys that will be removed from the dictionary
*/
- (void)removeObjectsForKeys:(CPArray)allKeys
{
    var index = allKeys.length;

    while (index--)
        dictionary_removeValue(self, allKeys[index]);
}

/*
    Instance.removeObjectForKey(aKey)
    {
        var entry= this._dictionary[aKey];
        
        if(entry)
        {
            var range= CPMakeRange(entry.index, 1);
            
            this._keys.removeObjectsInRange(range);
            this._objects.removeObjectsInRange(range);
        
            delete this._dictionary[aKey];
        }
    }
    
    Instance.setDictionary(aDictionary)
    {
        this._keys= CPArray.arrayWithArray(aDictionary.allKeys());
        this._objects= CPArray.arrayWithArray(aDictionary.allValues());
        
        this._dictionary= { };
        
        var i= this._keys.count();
        while(i--) this._dictionary[this._keys[i]]= { object: this._objects[i], index: i };
    }
*/
/*!
    Adds an entry into the dictionary.
    @param anObject the object for the entry
    @param aKey the entry's key
*/
- (void)setObject:(id)anObject forKey:(id)aKey
{
    dictionary_setValue(self, aKey, anObject);
}
/*
    Instance.setValueForKey(aValue, aKey)
    {
        if(!aValue) this.removeObjectForKey(aKey);
        else this.setObjectForKey(aValue, aKey);
    }
    
    Instance.copy()
    {
        return CPDictionary.alloc().dictionaryWithDictionary(this);
    }
*/

/*!
    Take all the key/value pairs in aDictionary and apply them to this dictionary.
*/
- (void)addEntriesFromDictionary:(CPDictionary)aDictionary
{
    if (!aDictionary)
        return;
        
    var keys = [aDictionary allKeys],
        index = [keys count];
    
    while (index--)
    {
        var key = keys[index];

        [self setObject:[aDictionary objectForKey:key] forKey:key];
    }
}

/*!
    Returns a human readable description of the dictionary.
*/
- (CPString)description
{
    var description = @"CPDictionary {\n";
    
    var i = _keys.length;
    
    while (i--)
    {
        description += _keys[i] + ":";

        var object = _buckets[_keys[i]];

        if (object && object.isa)
            description += [object description];
        else
            description += object;

        description += "\n";
    }

    description += "}";

    return description;
}

@end

@implementation CPDictionary (CPCoding)

/*
    Initializes the dictionary by unarchiving the data from a coder.
    @param aCoder the coder from which the data will be unarchived.
    @return the initialized dictionary
*/
- (id)initWithCoder:(CPCoder)aCoder
{
    return [aCoder _decodeDictionaryOfObjectsForKey:@"CP.objects"];
}

/*!
    Archives the dictionary to a provided coder.
    @param aCoder the coder to which the dictionary data will be archived.
*/
- (void)encodeWithCoder:(CPCoder)aCoder
{
    [aCoder _encodeDictionaryOfObjects:self forKey:@"CP.objects"];
}

@end

/*!
    @class CPMutableDictionary
    @ingroup compatability

    This class is just an empty subclass of CPDictionary.
    CPDictionary already implements mutable methods and
    this class only exists for source compatability.
*/
@implementation CPMutableDictionary : CPDictionary

@end

objj_dictionary.prototype.isa = CPDictionary;