API  0.9.7
 All Classes Files Functions Variables Macros Groups Pages
CPCountedSet.j
Go to the documentation of this file.
1 /*
2  * CPCountedSet.j
3  * Foundation
4  *
5  * Created by .
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
30 @implementation CPCountedSet : _CPConcreteMutableSet
31 {
32  Object _counts;
33 }
34 
35 - (void)addObject:(id)anObject
36 {
37  if (!_counts)
38  _counts = {};
39 
40  [super addObject:anObject];
41 
42  var UID = [anObject UID];
43 
44  if (_counts[UID] === undefined)
45  _counts[UID] = 1;
46  else
47  ++_counts[UID];
48 }
49 
50 - (void)removeObject:(id)anObject
51 {
52  if (!_counts)
53  return;
54 
55  var UID = [anObject UID];
56 
57  if (_counts[UID] === undefined)
58  return;
59 
60  else
61  {
62  --_counts[UID];
63 
64  if (_counts[UID] === 0)
65  {
66  delete _counts[UID];
67  [super removeObject:anObject];
68  }
69  }
70 }
71 
72 - (void)removeAllObjects
73 {
74  [super removeAllObjects];
75  _counts = {};
76 }
77 
78 /*
79  Returns the number of times anObject appears in the receiver.
80  @param anObject The object to check the count for.
81 */
82 - (unsigned)countForObject:(id)anObject
83 {
84  if (!_counts)
85  _counts = {};
86 
87  var UID = [anObject UID];
88 
89  if (_counts[UID] === undefined)
90  return 0;
91 
92  return _counts[UID];
93 }
94 
95 
96 /*
97 
98 Eventually we should see what these are supposed to do, and then do that.
99 
100 - (void)intersectSet:(CPSet)set
101 
102 - (void)minusSet:(CPSet)set
103 
104 - (void)unionSet:(CPSet)set
105 
106 */
107 
108 @end