2
<!DOCTYPE gsdoc PUBLIC "-//GNUstep//DTD gsdoc 0.6.6//EN" "http://www.gnustep.org/gsdoc-0_6_6.xml">
3
<gsdoc base="NSCountedSet" prev="NSConnection" next="NSDate" up="Base">
5
<title>NSCountedSet</title>
6
<author name="Richard Frith-Macdonald">
7
<email address="rfm@gnu.org"/>
8
<url url="http://www.gnustep.org/developers/whoiswho.html"/>
10
<version>$Revision: 1.11 $</version>
11
<date>$Date: 2001/03/03 09:58:53 $</date>
15
<heading>NSCountedSet</heading>
16
<class name="NSCountedSet" super="NSMutableSet">
17
<declared>Foundation/NSSet.h</declared>
18
<conform>NSCoding</conform>
21
The NSCountedSet class is used to maintain a set of objects where
22
the number of times each object has been added (wiithout a
23
corresponding removal) is kept track of.
26
In GNUstep, extra methods are provided to make use of a counted
27
set for <em>uniquing</em> objects easier.
32
<arg type="id">anObject</arg>
34
Adds an object to the set. If the set already contains an object
35
equal to the specified object (as determined by the [-isEqual:]
36
method) then the count for that object is incremented rather
37
than the new object being added.
40
<method type="NSArray*">
43
Returns an array containing all the objects stored in the set.
46
<method type="unsigned int">
49
Returns the number of objects stored in the set.
52
<method type="unsigned int">
53
<sel>countForObject:</sel>
54
<arg type="id">anObject</arg>
56
Returns the number of times that an object that is equal to the
57
specified object (as determined byt the [-isEqual:] method) has
58
been added to the set and not removed from it.
62
<sel>initWithArray:</sel>
63
<arg type="NSArray*">anArray</arg>
65
Initialises a newly allocated set by adding all the objects
66
in the supplied array to the set. Each object is added to
67
the set as many times as it occurs in the array.
71
<sel>initWithCapacity:</sel>
72
<arg type="unsigned int">numItems</arg>
74
Initialises a newly allocated set to contain no objects but
75
to have space available to hold the specified number of items.
79
<sel>initWithSet:</sel>
80
<arg type="NSSet*">aSet</arg>
82
Initialises a newly allocated set by adding all the objects
86
<method type="NSEnumerator*">
87
<sel>objectEnumerator</sel>
89
Returns an NSEnumerator object able to step through all the
95
<arg type="int">count</arg>
98
This method removes from the set all objects whose count is
99
less than or equal to the specified value.
102
This is useful where a counted set is used for uniquing objects.
103
The set can be periodically purged of objects that have only
104
been added once - and are therefore simply wasting space.
107
<standards><NotOpenStep/><NotMacOS-X/></standards>
110
<sel>removeObject:</sel>
111
<arg type="id">anObject</arg>
113
Decrements the count of the number of times that the specified
114
object (or an object qequal to it as determined by the
115
[-isEqual:] method) has been added to the set. If the count
116
becomes zero, the object is removed from the set.
121
<arg type="id">anObject</arg>
124
If the supplied object (or one equal to it as determined by
125
the [-isEqual:] method) is already present in the set, the
126
count for that object is incremented, the supplied object
127
is released, and the object in the set is retained and returned.
128
Otherwise, the supplied object is added to the set and returned.
131
This method is useful for uniquing objects - the init method of
132
a class need simply end with -
134
return [myUniquingSet unique: self];
138
<standards><NotOpenStep/><NotMacOS-X/></standards>
143
<heading>NSCountedSet related functions</heading>
145
GNUstep provides some functions that may be using to maintain a
146
global NSCountedSet object for usin in uniquing objects. In a
147
multi-threaded application, accesses to this global set are
148
automatically protected by locks.
150
<function name="GSUniquing" type="void">
151
<arg type="BOOL">flag</arg>
153
This function sets the state of a flag that determines the
154
behavior of the GSUnique() function. If the flag is on,
155
uniquing is performed, if it is off the function has no effect.
156
The default is for uniquing to be turned off.
158
<standards><GNUstep/><NotOpenStep/><NotMacOS-X/></standards>
160
<function name="GSUnique" type="id">
161
<arg type="id">anObject</arg>
163
This function <em>uniques</em> the supplied argument, returning
164
the result. It works by using the [-unique:] method of a global
165
NSCountedSet object. It handles locking as necessary.
166
If uniquing is turned off, it simply returns its argument.
168
<standards><GNUstep/><NotOpenStep/><NotMacOS-X/></standards>
170
<function name="GSUPurge" type="void">
171
<arg type="unsigned int">count</arg>
173
This function purges the global NSCountedSet object used for
174
uniquing. It handles locking as necessary. It can be used to
175
purge the set even when uniquing is turned off.
177
<standards><GNUstep/><NotOpenStep/><NotMacOS-X/></standards>
179
<function name="GSUSet" type="id">
180
<arg type="id">anObject</arg>
181
<arg type="unsigned int">count</arg>
183
This function sets the count for the specified object. If the
184
count for the object is set to zero then the object is removed
185
from the global uniquing set. The object is added to the set
186
if necessary. The object returned is the one stored in the set.
187
The function handles locking as necessary. It can be used to
188
alter the set even when uniquing is turned off.
190
<standards><GNUstep/><NotOpenStep/><NotMacOS-X/></standards>