]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSSet.cpp
3aeb910371ebef59a34c7f09d4b35483b434a233
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* IOSet.m created by rsulack on Thu 11-Jun-1998 */
30 #include <libkern/c++/OSDictionary.h>
31 #include <libkern/c++/OSArray.h>
32 #include <libkern/c++/OSSerialize.h>
33 #include <libkern/c++/OSSet.h>
35 #define super OSCollection
37 OSDefineMetaClassAndStructors(OSSet
, OSCollection
)
38 OSMetaClassDefineReservedUnused(OSSet
, 0);
39 OSMetaClassDefineReservedUnused(OSSet
, 1);
40 OSMetaClassDefineReservedUnused(OSSet
, 2);
41 OSMetaClassDefineReservedUnused(OSSet
, 3);
42 OSMetaClassDefineReservedUnused(OSSet
, 4);
43 OSMetaClassDefineReservedUnused(OSSet
, 5);
44 OSMetaClassDefineReservedUnused(OSSet
, 6);
45 OSMetaClassDefineReservedUnused(OSSet
, 7);
47 #define EXT_CAST(obj) \
48 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
50 bool OSSet::initWithCapacity(unsigned int inCapacity
)
55 members
= OSArray::withCapacity(inCapacity
);
62 bool OSSet::initWithObjects(const OSObject
*inObjects
[],
64 unsigned int inCapacity
)
66 unsigned int capacity
= inCount
;
69 if ( inCount
> inCapacity
)
72 capacity
= inCapacity
;
75 if (!inObjects
|| !initWithCapacity(capacity
))
78 for ( unsigned int i
= 0; i
< inCount
; i
++ ) {
79 if (members
->getCount() < inCapacity
)
80 setObject(inObjects
[i
]);
88 bool OSSet::initWithArray(const OSArray
*inArray
,
89 unsigned int inCapacity
)
94 return initWithObjects((const OSObject
**) inArray
->array
,
95 inArray
->count
, inCapacity
);
98 bool OSSet::initWithSet(const OSSet
*inSet
,
99 unsigned int inCapacity
)
101 return initWithArray(inSet
->members
, inCapacity
);
104 OSSet
*OSSet::withCapacity(unsigned int capacity
)
106 OSSet
*me
= new OSSet
;
108 if (me
&& !me
->initWithCapacity(capacity
)) {
116 OSSet
*OSSet::withObjects(const OSObject
*objects
[],
118 unsigned int capacity
)
120 OSSet
*me
= new OSSet
;
122 if (me
&& !me
->initWithObjects(objects
, count
, capacity
)) {
130 OSSet
*OSSet::withArray(const OSArray
*array
,
131 unsigned int capacity
)
133 OSSet
*me
= new OSSet
;
135 if (me
&& !me
->initWithArray(array
, capacity
)) {
143 OSSet
*OSSet::withSet(const OSSet
*set
,
144 unsigned int capacity
)
146 OSSet
*me
= new OSSet
;
148 if (me
&& !me
->initWithSet(set
, capacity
)) {
158 (void) members
->super::setOptions(0, kImmutable
);
165 unsigned int OSSet::getCount() const
167 return members
->count
;
170 unsigned int OSSet::getCapacity() const
172 return members
->capacity
;
175 unsigned int OSSet::getCapacityIncrement() const
177 return members
->capacityIncrement
;
180 unsigned int OSSet::setCapacityIncrement(unsigned int increment
)
182 return members
->setCapacityIncrement(increment
);
185 unsigned int OSSet::ensureCapacity(unsigned int newCapacity
)
187 return members
->ensureCapacity(newCapacity
);
190 void OSSet::flushCollection()
193 members
->flushCollection();
196 bool OSSet::setObject(const OSMetaClassBase
*anObject
)
198 if (containsObject(anObject
))
202 return members
->setObject(anObject
);
206 bool OSSet::merge(const OSArray
*array
)
208 const OSMetaClassBase
*anObject
;
211 for (int i
= 0; (anObject
= array
->getObject(i
)); i
++)
212 if (setObject(anObject
))
218 bool OSSet::merge(const OSSet
*set
)
220 return merge(set
->members
);
223 void OSSet::removeObject(const OSMetaClassBase
*anObject
)
225 const OSMetaClassBase
*probeObject
;
227 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
228 if (probeObject
== anObject
) {
230 members
->removeObject(i
);
236 bool OSSet::containsObject(const OSMetaClassBase
*anObject
) const
238 return anObject
&& member(anObject
);
241 bool OSSet::member(const OSMetaClassBase
*anObject
) const
243 OSMetaClassBase
*probeObject
;
245 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
246 if (probeObject
== anObject
)
252 OSObject
*OSSet::getAnyObject() const
254 return members
->getObject(0);
257 bool OSSet::isEqualTo(const OSSet
*aSet
) const
261 const OSMetaClassBase
*obj1
;
262 const OSMetaClassBase
*obj2
;
267 count
= members
->count
;
268 if ( count
!= aSet
->getCount() )
271 for ( i
= 0; i
< count
; i
++ ) {
272 obj1
= aSet
->members
->getObject(i
);
273 obj2
= members
->getObject(i
);
274 if ( !obj1
|| !obj2
)
277 if ( !obj1
->isEqualTo(obj2
) )
284 bool OSSet::isEqualTo(const OSMetaClassBase
*anObject
) const
288 otherSet
= OSDynamicCast(OSSet
, anObject
);
290 return isEqualTo(otherSet
);
295 unsigned int OSSet::iteratorSize() const
297 return sizeof(unsigned int);
300 bool OSSet::initIterator(void *inIterator
) const
302 unsigned int *iteratorP
= (unsigned int *) inIterator
;
308 bool OSSet::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
310 unsigned int *iteratorP
= (unsigned int *) inIterator
;
311 unsigned int index
= (*iteratorP
)++;
313 if (index
< members
->count
)
314 *ret
= members
->getObject(index
);
321 bool OSSet::serialize(OSSerialize
*s
) const
323 const OSMetaClassBase
*o
;
325 if (s
->previouslySerialized(this)) return true;
327 if (!s
->addXMLStartTag(this, "set")) return false;
329 for (int i
= 0; (o
= members
->getObject(i
)); i
++) {
330 if (!o
->serialize(s
)) return false;
333 return s
->addXMLEndTag("set");
336 unsigned OSSet::setOptions(unsigned options
, unsigned mask
, void *)
338 unsigned old
= super::setOptions(options
, mask
);
339 if ((old
^ options
) & mask
)
340 members
->setOptions(options
, mask
);
345 OSCollection
* OSSet::copyCollection(OSDictionary
*cycleDict
)
347 bool allocDict
= !cycleDict
;
348 OSCollection
*ret
= 0;
352 cycleDict
= OSDictionary::withCapacity(16);
359 ret
= super::copyCollection(cycleDict
);
361 continue; // Found it
363 newSet
= OSSet::withCapacity(members
->capacity
);
365 continue; // Couldn't create new set abort
367 // Insert object into cycle Dictionary
368 cycleDict
->setObject((const OSSymbol
*) this, newSet
);
370 OSArray
*newMembers
= newSet
->members
;
371 newMembers
->capacityIncrement
= members
->capacityIncrement
;
373 // Now copy over the contents into the new duplicate
374 for (unsigned int i
= 0; i
< members
->count
; i
++) {
375 OSObject
*obj
= EXT_CAST(members
->array
[i
]);
376 OSCollection
*coll
= OSDynamicCast(OSCollection
, obj
);
378 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
380 obj
= newColl
; // Rely on cycleDict ref for a bit
386 newMembers
->setObject(obj
);
399 cycleDict
->release();