]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSSet.cpp
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* IOSet.m created by rsulack on Thu 11-Jun-1998 */
24 #include <libkern/c++/OSDictionary.h>
25 #include <libkern/c++/OSArray.h>
26 #include <libkern/c++/OSSerialize.h>
27 #include <libkern/c++/OSSet.h>
29 #define super OSCollection
31 OSDefineMetaClassAndStructors(OSSet
, OSCollection
)
32 OSMetaClassDefineReservedUnused(OSSet
, 0);
33 OSMetaClassDefineReservedUnused(OSSet
, 1);
34 OSMetaClassDefineReservedUnused(OSSet
, 2);
35 OSMetaClassDefineReservedUnused(OSSet
, 3);
36 OSMetaClassDefineReservedUnused(OSSet
, 4);
37 OSMetaClassDefineReservedUnused(OSSet
, 5);
38 OSMetaClassDefineReservedUnused(OSSet
, 6);
39 OSMetaClassDefineReservedUnused(OSSet
, 7);
41 #define EXT_CAST(obj) \
42 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
44 bool OSSet::initWithCapacity(unsigned int inCapacity
)
49 members
= OSArray::withCapacity(inCapacity
);
56 bool OSSet::initWithObjects(const OSObject
*inObjects
[],
58 unsigned int inCapacity
)
60 unsigned int capacity
= inCount
;
63 if ( inCount
> inCapacity
)
66 capacity
= inCapacity
;
69 if (!inObjects
|| !initWithCapacity(capacity
))
72 for ( unsigned int i
= 0; i
< inCount
; i
++ ) {
73 if (members
->getCount() < inCapacity
)
74 setObject(inObjects
[i
]);
82 bool OSSet::initWithArray(const OSArray
*inArray
,
83 unsigned int inCapacity
)
88 return initWithObjects((const OSObject
**) inArray
->array
,
89 inArray
->count
, inCapacity
);
92 bool OSSet::initWithSet(const OSSet
*inSet
,
93 unsigned int inCapacity
)
95 return initWithArray(inSet
->members
, inCapacity
);
98 OSSet
*OSSet::withCapacity(unsigned int capacity
)
100 OSSet
*me
= new OSSet
;
102 if (me
&& !me
->initWithCapacity(capacity
)) {
110 OSSet
*OSSet::withObjects(const OSObject
*objects
[],
112 unsigned int capacity
)
114 OSSet
*me
= new OSSet
;
116 if (me
&& !me
->initWithObjects(objects
, count
, capacity
)) {
124 OSSet
*OSSet::withArray(const OSArray
*array
,
125 unsigned int capacity
)
127 OSSet
*me
= new OSSet
;
129 if (me
&& !me
->initWithArray(array
, capacity
)) {
137 OSSet
*OSSet::withSet(const OSSet
*set
,
138 unsigned int capacity
)
140 OSSet
*me
= new OSSet
;
142 if (me
&& !me
->initWithSet(set
, capacity
)) {
152 (void) members
->super::setOptions(0, kImmutable
);
159 unsigned int OSSet::getCount() const
161 return members
->count
;
164 unsigned int OSSet::getCapacity() const
166 return members
->capacity
;
169 unsigned int OSSet::getCapacityIncrement() const
171 return members
->capacityIncrement
;
174 unsigned int OSSet::setCapacityIncrement(unsigned int increment
)
176 return members
->setCapacityIncrement(increment
);
179 unsigned int OSSet::ensureCapacity(unsigned int newCapacity
)
181 return members
->ensureCapacity(newCapacity
);
184 void OSSet::flushCollection()
187 members
->flushCollection();
190 bool OSSet::setObject(const OSMetaClassBase
*anObject
)
192 if (containsObject(anObject
))
196 return members
->setObject(anObject
);
200 bool OSSet::merge(const OSArray
*array
)
202 const OSMetaClassBase
*anObject
;
205 for (int i
= 0; (anObject
= array
->getObject(i
)); i
++)
206 if (setObject(anObject
))
212 bool OSSet::merge(const OSSet
*set
)
214 return merge(set
->members
);
217 void OSSet::removeObject(const OSMetaClassBase
*anObject
)
219 const OSMetaClassBase
*probeObject
;
221 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
222 if (probeObject
== anObject
) {
224 members
->removeObject(i
);
230 bool OSSet::containsObject(const OSMetaClassBase
*anObject
) const
232 return anObject
&& member(anObject
);
235 bool OSSet::member(const OSMetaClassBase
*anObject
) const
237 OSMetaClassBase
*probeObject
;
239 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
240 if (probeObject
== anObject
)
246 OSObject
*OSSet::getAnyObject() const
248 return members
->getObject(0);
251 bool OSSet::isEqualTo(const OSSet
*aSet
) const
255 const OSMetaClassBase
*obj1
;
256 const OSMetaClassBase
*obj2
;
261 count
= members
->count
;
262 if ( count
!= aSet
->getCount() )
265 for ( i
= 0; i
< count
; i
++ ) {
266 obj1
= aSet
->members
->getObject(i
);
267 obj2
= members
->getObject(i
);
268 if ( !obj1
|| !obj2
)
271 if ( !obj1
->isEqualTo(obj2
) )
278 bool OSSet::isEqualTo(const OSMetaClassBase
*anObject
) const
282 otherSet
= OSDynamicCast(OSSet
, anObject
);
284 return isEqualTo(otherSet
);
289 unsigned int OSSet::iteratorSize() const
291 return sizeof(unsigned int);
294 bool OSSet::initIterator(void *inIterator
) const
296 unsigned int *iteratorP
= (unsigned int *) inIterator
;
302 bool OSSet::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
304 unsigned int *iteratorP
= (unsigned int *) inIterator
;
305 unsigned int index
= (*iteratorP
)++;
307 if (index
< members
->count
)
308 *ret
= members
->getObject(index
);
315 bool OSSet::serialize(OSSerialize
*s
) const
317 const OSMetaClassBase
*o
;
319 if (s
->previouslySerialized(this)) return true;
321 if (!s
->addXMLStartTag(this, "set")) return false;
323 for (int i
= 0; (o
= members
->getObject(i
)); i
++) {
324 if (!o
->serialize(s
)) return false;
327 return s
->addXMLEndTag("set");
330 unsigned OSSet::setOptions(unsigned options
, unsigned mask
, void *)
332 unsigned old
= super::setOptions(options
, mask
);
333 if ((old
^ options
) & mask
)
334 members
->setOptions(options
, mask
);
339 OSCollection
* OSSet::copyCollection(OSDictionary
*cycleDict
)
341 bool allocDict
= !cycleDict
;
342 OSCollection
*ret
= 0;
346 cycleDict
= OSDictionary::withCapacity(16);
353 ret
= super::copyCollection(cycleDict
);
355 continue; // Found it
357 newSet
= OSSet::withCapacity(members
->capacity
);
359 continue; // Couldn't create new set abort
361 // Insert object into cycle Dictionary
362 cycleDict
->setObject((const OSSymbol
*) this, newSet
);
364 OSArray
*newMembers
= newSet
->members
;
365 newMembers
->capacityIncrement
= members
->capacityIncrement
;
367 // Now copy over the contents into the new duplicate
368 for (unsigned int i
= 0; i
< members
->count
; i
++) {
369 OSObject
*obj
= EXT_CAST(members
->array
[i
]);
370 OSCollection
*coll
= OSDynamicCast(OSCollection
, obj
);
372 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
374 obj
= newColl
; // Rely on cycleDict ref for a bit
380 newMembers
->setObject(obj
);
393 cycleDict
->release();