API  1.0.0
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 
29 @implementation CPCountedSet : _CPConcreteMutableSet
30 {
31  Object _counts;
32 }
33 
34 - (void)addObject:(id)anObject
35 {
36  if (!_counts)
37  _counts = {};
38 
39  [super addObject:anObject];
40 
41  var UID = [anObject UID];
42 
43  if (_counts[UID] === undefined)
44  _counts[UID] = 1;
45  else
46  ++_counts[UID];
47 }
48 
49 - (void)removeObject:(id)anObject
50 {
51  if (!_counts)
52  return;
53 
54  var UID = [anObject UID];
55 
56  if (_counts[UID] === undefined)
57  return;
58 
59  else
60  {
61  --_counts[UID];
62 
63  if (_counts[UID] === 0)
64  {
65  delete _counts[UID];
66  [super removeObject:anObject];
67  }
68  }
69 }
70 
71 - (void)removeAllObjects
72 {
73  [super removeAllObjects];
74  _counts = {};
75 }
76 
77 /*
78  Returns the number of times anObject appears in the receiver.
79  @param anObject The object to check the count for.
80 */
81 - (unsigned)countForObject:(id)anObject
82 {
83  if (!_counts)
84  _counts = {};
85 
86  var UID = [anObject UID];
87 
88  if (_counts[UID] === undefined)
89  return 0;
90 
91  return _counts[UID];
92 }
93 
94 
95 /*
96 
97 Eventually we should see what these are supposed to do, and then do that.
98 
99 - (void)intersectSet:(CPSet)set
100 
101 - (void)minusSet:(CPSet)set
102 
103 - (void)unionSet:(CPSet)set
104 
105 */
106 
107 @end
An mutable collection which may contain a specific object numerous times.
Definition: CPCountedSet.h:2