]>
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_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* IOSet.m created by rsulack on Thu 11-Jun-1998 */
32 #include <libkern/c++/OSDictionary.h>
33 #include <libkern/c++/OSArray.h>
34 #include <libkern/c++/OSSerialize.h>
35 #include <libkern/c++/OSSet.h>
37 #define super OSCollection
39 OSDefineMetaClassAndStructors(OSSet
, OSCollection
)
40 OSMetaClassDefineReservedUnused(OSSet
, 0);
41 OSMetaClassDefineReservedUnused(OSSet
, 1);
42 OSMetaClassDefineReservedUnused(OSSet
, 2);
43 OSMetaClassDefineReservedUnused(OSSet
, 3);
44 OSMetaClassDefineReservedUnused(OSSet
, 4);
45 OSMetaClassDefineReservedUnused(OSSet
, 5);
46 OSMetaClassDefineReservedUnused(OSSet
, 6);
47 OSMetaClassDefineReservedUnused(OSSet
, 7);
49 #define EXT_CAST(obj) \
50 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
52 bool OSSet::initWithCapacity(unsigned int inCapacity
)
57 members
= OSArray::withCapacity(inCapacity
);
64 bool OSSet::initWithObjects(const OSObject
*inObjects
[],
66 unsigned int inCapacity
)
68 unsigned int capacity
= inCount
;
71 if ( inCount
> inCapacity
)
74 capacity
= inCapacity
;
77 if (!inObjects
|| !initWithCapacity(capacity
))
80 for ( unsigned int i
= 0; i
< inCount
; i
++ ) {
81 if (members
->getCount() < inCapacity
)
82 setObject(inObjects
[i
]);
90 bool OSSet::initWithArray(const OSArray
*inArray
,
91 unsigned int inCapacity
)
96 return initWithObjects((const OSObject
**) inArray
->array
,
97 inArray
->count
, inCapacity
);
100 bool OSSet::initWithSet(const OSSet
*inSet
,
101 unsigned int inCapacity
)
103 return initWithArray(inSet
->members
, inCapacity
);
106 OSSet
*OSSet::withCapacity(unsigned int capacity
)
108 OSSet
*me
= new OSSet
;
110 if (me
&& !me
->initWithCapacity(capacity
)) {
118 OSSet
*OSSet::withObjects(const OSObject
*objects
[],
120 unsigned int capacity
)
122 OSSet
*me
= new OSSet
;
124 if (me
&& !me
->initWithObjects(objects
, count
, capacity
)) {
132 OSSet
*OSSet::withArray(const OSArray
*array
,
133 unsigned int capacity
)
135 OSSet
*me
= new OSSet
;
137 if (me
&& !me
->initWithArray(array
, capacity
)) {
145 OSSet
*OSSet::withSet(const OSSet
*set
,
146 unsigned int capacity
)
148 OSSet
*me
= new OSSet
;
150 if (me
&& !me
->initWithSet(set
, capacity
)) {
160 (void) members
->super::setOptions(0, kImmutable
);
167 unsigned int OSSet::getCount() const
169 return members
->count
;
172 unsigned int OSSet::getCapacity() const
174 return members
->capacity
;
177 unsigned int OSSet::getCapacityIncrement() const
179 return members
->capacityIncrement
;
182 unsigned int OSSet::setCapacityIncrement(unsigned int increment
)
184 return members
->setCapacityIncrement(increment
);
187 unsigned int OSSet::ensureCapacity(unsigned int newCapacity
)
189 return members
->ensureCapacity(newCapacity
);
192 void OSSet::flushCollection()
195 members
->flushCollection();
198 bool OSSet::setObject(const OSMetaClassBase
*anObject
)
200 if (containsObject(anObject
))
204 return members
->setObject(anObject
);
208 bool OSSet::merge(const OSArray
*array
)
210 const OSMetaClassBase
*anObject
;
213 for (int i
= 0; (anObject
= array
->getObject(i
)); i
++)
214 if (setObject(anObject
))
220 bool OSSet::merge(const OSSet
*set
)
222 return merge(set
->members
);
225 void OSSet::removeObject(const OSMetaClassBase
*anObject
)
227 const OSMetaClassBase
*probeObject
;
229 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
230 if (probeObject
== anObject
) {
232 members
->removeObject(i
);
238 bool OSSet::containsObject(const OSMetaClassBase
*anObject
) const
240 return anObject
&& member(anObject
);
243 bool OSSet::member(const OSMetaClassBase
*anObject
) const
245 OSMetaClassBase
*probeObject
;
247 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
248 if (probeObject
== anObject
)
254 OSObject
*OSSet::getAnyObject() const
256 return members
->getObject(0);
259 bool OSSet::isEqualTo(const OSSet
*aSet
) const
263 const OSMetaClassBase
*obj1
;
264 const OSMetaClassBase
*obj2
;
269 count
= members
->count
;
270 if ( count
!= aSet
->getCount() )
273 for ( i
= 0; i
< count
; i
++ ) {
274 obj1
= aSet
->members
->getObject(i
);
275 obj2
= members
->getObject(i
);
276 if ( !obj1
|| !obj2
)
279 if ( !obj1
->isEqualTo(obj2
) )
286 bool OSSet::isEqualTo(const OSMetaClassBase
*anObject
) const
290 otherSet
= OSDynamicCast(OSSet
, anObject
);
292 return isEqualTo(otherSet
);
297 unsigned int OSSet::iteratorSize() const
299 return sizeof(unsigned int);
302 bool OSSet::initIterator(void *inIterator
) const
304 unsigned int *iteratorP
= (unsigned int *) inIterator
;
310 bool OSSet::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
312 unsigned int *iteratorP
= (unsigned int *) inIterator
;
313 unsigned int index
= (*iteratorP
)++;
315 if (index
< members
->count
)
316 *ret
= members
->getObject(index
);
323 bool OSSet::serialize(OSSerialize
*s
) const
325 const OSMetaClassBase
*o
;
327 if (s
->previouslySerialized(this)) return true;
329 if (!s
->addXMLStartTag(this, "set")) return false;
331 for (int i
= 0; (o
= members
->getObject(i
)); i
++) {
332 if (!o
->serialize(s
)) return false;
335 return s
->addXMLEndTag("set");
338 unsigned OSSet::setOptions(unsigned options
, unsigned mask
, void *)
340 unsigned old
= super::setOptions(options
, mask
);
341 if ((old
^ options
) & mask
)
342 members
->setOptions(options
, mask
);
347 OSCollection
* OSSet::copyCollection(OSDictionary
*cycleDict
)
349 bool allocDict
= !cycleDict
;
350 OSCollection
*ret
= 0;
354 cycleDict
= OSDictionary::withCapacity(16);
361 ret
= super::copyCollection(cycleDict
);
363 continue; // Found it
365 newSet
= OSSet::withCapacity(members
->capacity
);
367 continue; // Couldn't create new set abort
369 // Insert object into cycle Dictionary
370 cycleDict
->setObject((const OSSymbol
*) this, newSet
);
372 OSArray
*newMembers
= newSet
->members
;
373 newMembers
->capacityIncrement
= members
->capacityIncrement
;
375 // Now copy over the contents into the new duplicate
376 for (unsigned int i
= 0; i
< members
->count
; i
++) {
377 OSObject
*obj
= EXT_CAST(members
->array
[i
]);
378 OSCollection
*coll
= OSDynamicCast(OSCollection
, obj
);
380 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
382 obj
= newColl
; // Rely on cycleDict ref for a bit
388 newMembers
->setObject(obj
);
401 cycleDict
->release();