![]() |
API 0.9.5
|
00001 /* 00002 * CPCountedSet.j 00003 * Foundation 00004 * 00005 * Created by . 00006 * Copyright 2008, 280 North, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 00030 @implementation CPCountedSet : _CPConcreteMutableSet 00031 { 00032 Object _counts; 00033 } 00034 00035 - (void)addObject:(id)anObject 00036 { 00037 if (!_counts) 00038 _counts = {}; 00039 00040 [super addObject:anObject]; 00041 00042 var UID = [anObject UID]; 00043 00044 if (_counts[UID] === undefined) 00045 _counts[UID] = 1; 00046 else 00047 ++_counts[UID]; 00048 } 00049 00050 - (void)removeObject:(id)anObject 00051 { 00052 if (!_counts) 00053 return; 00054 00055 var UID = [anObject UID]; 00056 00057 if (_counts[UID] === undefined) 00058 return; 00059 00060 else 00061 { 00062 --_counts[UID]; 00063 00064 if (_counts[UID] === 0) 00065 { 00066 delete _counts[UID]; 00067 [super removeObject:anObject]; 00068 } 00069 } 00070 } 00071 00072 - (void)removeAllObjects 00073 { 00074 [super removeAllObjects]; 00075 _counts = {}; 00076 } 00077 00078 /* 00079 Returns the number of times anObject appears in the receiver. 00080 @param anObject The object to check the count for. 00081 */ 00082 - (unsigned)countForObject:(id)anObject 00083 { 00084 if (!_counts) 00085 _counts = {}; 00086 00087 var UID = [anObject UID]; 00088 00089 if (_counts[UID] === undefined) 00090 return 0; 00091 00092 return _counts[UID]; 00093 } 00094 00095 00096 /* 00097 00098 Eventually we should see what these are supposed to do, and then do that. 00099 00100 - (void)intersectSet:(CPSet)set 00101 00102 - (void)minusSet:(CPSet)set 00103 00104 - (void)unionSet:(CPSet)set 00105 00106 */ 00107 00108 @end