00001
00002 @import "CPSet.j"
00003
00007 @implementation CPCountedSet : CPMutableSet
00008 {
00009 Object _counts;
00010 }
00011
00012 - (void)addObject:(id)anObject
00013 {
00014 if (!_counts)
00015 _counts = {};
00016
00017 [super addObject:anObject];
00018
00019 var hash = [anObject hash];
00020
00021 if (_counts[hash] === undefined)
00022 _counts[hash] = 1;
00023 else
00024 ++_counts[hash];
00025 }
00026
00027 - (void)removeObject:(id)anObject
00028 {
00029 if (!_counts)
00030 return;
00031
00032 var hash = [anObject hash];
00033
00034 if (_counts[hash] === undefined)
00035 return;
00036
00037 else
00038 {
00039 --_counts[hash];
00040
00041 if (_counts[hash] === 0)
00042 {
00043 delete _counts[hash];
00044 [super removeObject:anObject];
00045 }
00046 }
00047 }
00048
00049 - (void)removeAllObjects
00050 {
00051 [super removeAllObjects];
00052 _counts = {};
00053 }
00054
00055
00056
00057
00058
00059 - (unsigned)countForObject:(id)anObject
00060 {
00061 if (!_counts)
00062 _counts = {};
00063
00064 var hash = [anObject hash];
00065
00066 if (_counts[hash] === undefined)
00067 return 0;
00068
00069 return _counts[hash];
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 @end