]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSSet.cpp
06c2d9037ad61872be13b2ba585597e3f4727d09
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* IOSet.m created by rsulack on Thu 11-Jun-1998 */
25 #include <libkern/c++/OSDictionary.h>
26 #include <libkern/c++/OSArray.h>
27 #include <libkern/c++/OSSerialize.h>
28 #include <libkern/c++/OSSet.h>
30 #define super OSCollection
32 OSDefineMetaClassAndStructors(OSSet
, OSCollection
)
33 OSMetaClassDefineReservedUnused(OSSet
, 0);
34 OSMetaClassDefineReservedUnused(OSSet
, 1);
35 OSMetaClassDefineReservedUnused(OSSet
, 2);
36 OSMetaClassDefineReservedUnused(OSSet
, 3);
37 OSMetaClassDefineReservedUnused(OSSet
, 4);
38 OSMetaClassDefineReservedUnused(OSSet
, 5);
39 OSMetaClassDefineReservedUnused(OSSet
, 6);
40 OSMetaClassDefineReservedUnused(OSSet
, 7);
42 #define EXT_CAST(obj) \
43 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
45 bool OSSet::initWithCapacity(unsigned int inCapacity
)
50 members
= OSArray::withCapacity(inCapacity
);
57 bool OSSet::initWithObjects(const OSObject
*inObjects
[],
59 unsigned int inCapacity
)
61 unsigned int capacity
= inCount
;
64 if ( inCount
> inCapacity
)
67 capacity
= inCapacity
;
70 if (!inObjects
|| !initWithCapacity(capacity
))
73 for ( unsigned int i
= 0; i
< inCount
; i
++ ) {
74 if (members
->getCount() < inCapacity
)
75 setObject(inObjects
[i
]);
83 bool OSSet::initWithArray(const OSArray
*inArray
,
84 unsigned int inCapacity
)
89 return initWithObjects((const OSObject
**) inArray
->array
,
90 inArray
->count
, inCapacity
);
93 bool OSSet::initWithSet(const OSSet
*inSet
,
94 unsigned int inCapacity
)
96 return initWithArray(inSet
->members
, inCapacity
);
99 OSSet
*OSSet::withCapacity(unsigned int capacity
)
101 OSSet
*me
= new OSSet
;
103 if (me
&& !me
->initWithCapacity(capacity
)) {
111 OSSet
*OSSet::withObjects(const OSObject
*objects
[],
113 unsigned int capacity
)
115 OSSet
*me
= new OSSet
;
117 if (me
&& !me
->initWithObjects(objects
, count
, capacity
)) {
125 OSSet
*OSSet::withArray(const OSArray
*array
,
126 unsigned int capacity
)
128 OSSet
*me
= new OSSet
;
130 if (me
&& !me
->initWithArray(array
, capacity
)) {
138 OSSet
*OSSet::withSet(const OSSet
*set
,
139 unsigned int capacity
)
141 OSSet
*me
= new OSSet
;
143 if (me
&& !me
->initWithSet(set
, capacity
)) {
153 (void) members
->super::setOptions(0, kImmutable
);
160 unsigned int OSSet::getCount() const
162 return members
->count
;
165 unsigned int OSSet::getCapacity() const
167 return members
->capacity
;
170 unsigned int OSSet::getCapacityIncrement() const
172 return members
->capacityIncrement
;
175 unsigned int OSSet::setCapacityIncrement(unsigned int increment
)
177 return members
->setCapacityIncrement(increment
);
180 unsigned int OSSet::ensureCapacity(unsigned int newCapacity
)
182 return members
->ensureCapacity(newCapacity
);
185 void OSSet::flushCollection()
188 members
->flushCollection();
191 bool OSSet::setObject(const OSMetaClassBase
*anObject
)
193 if (containsObject(anObject
))
197 return members
->setObject(anObject
);
201 bool OSSet::merge(const OSArray
*array
)
203 const OSMetaClassBase
*anObject
;
206 for (int i
= 0; (anObject
= array
->getObject(i
)); i
++)
207 if (setObject(anObject
))
213 bool OSSet::merge(const OSSet
*set
)
215 return merge(set
->members
);
218 void OSSet::removeObject(const OSMetaClassBase
*anObject
)
220 const OSMetaClassBase
*probeObject
;
222 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
223 if (probeObject
== anObject
) {
225 members
->removeObject(i
);
231 bool OSSet::containsObject(const OSMetaClassBase
*anObject
) const
233 return anObject
&& member(anObject
);
236 bool OSSet::member(const OSMetaClassBase
*anObject
) const
238 OSMetaClassBase
*probeObject
;
240 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
241 if (probeObject
== anObject
)
247 OSObject
*OSSet::getAnyObject() const
249 return members
->getObject(0);
252 bool OSSet::isEqualTo(const OSSet
*aSet
) const
256 const OSMetaClassBase
*obj1
;
257 const OSMetaClassBase
*obj2
;
262 count
= members
->count
;
263 if ( count
!= aSet
->getCount() )
266 for ( i
= 0; i
< count
; i
++ ) {
267 obj1
= aSet
->members
->getObject(i
);
268 obj2
= members
->getObject(i
);
269 if ( !obj1
|| !obj2
)
272 if ( !obj1
->isEqualTo(obj2
) )
279 bool OSSet::isEqualTo(const OSMetaClassBase
*anObject
) const
283 otherSet
= OSDynamicCast(OSSet
, anObject
);
285 return isEqualTo(otherSet
);
290 unsigned int OSSet::iteratorSize() const
292 return sizeof(unsigned int);
295 bool OSSet::initIterator(void *inIterator
) const
297 unsigned int *iteratorP
= (unsigned int *) inIterator
;
303 bool OSSet::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
305 unsigned int *iteratorP
= (unsigned int *) inIterator
;
306 unsigned int index
= (*iteratorP
)++;
308 if (index
< members
->count
)
309 *ret
= members
->getObject(index
);
316 bool OSSet::serialize(OSSerialize
*s
) const
318 const OSMetaClassBase
*o
;
320 if (s
->previouslySerialized(this)) return true;
322 if (!s
->addXMLStartTag(this, "set")) return false;
324 for (int i
= 0; (o
= members
->getObject(i
)); i
++) {
325 if (!o
->serialize(s
)) return false;
328 return s
->addXMLEndTag("set");
331 unsigned OSSet::setOptions(unsigned options
, unsigned mask
, void *)
333 unsigned old
= super::setOptions(options
, mask
);
334 if ((old
^ options
) & mask
)
335 members
->setOptions(options
, mask
);
340 OSCollection
* OSSet::copyCollection(OSDictionary
*cycleDict
)
342 bool allocDict
= !cycleDict
;
343 OSCollection
*ret
= 0;
347 cycleDict
= OSDictionary::withCapacity(16);
354 ret
= super::copyCollection(cycleDict
);
356 continue; // Found it
358 newSet
= OSSet::withCapacity(members
->capacity
);
360 continue; // Couldn't create new set abort
362 // Insert object into cycle Dictionary
363 cycleDict
->setObject((const OSSymbol
*) this, newSet
);
365 OSArray
*newMembers
= newSet
->members
;
366 newMembers
->capacityIncrement
= members
->capacityIncrement
;
368 // Now copy over the contents into the new duplicate
369 for (unsigned int i
= 0; i
< members
->count
; i
++) {
370 OSObject
*obj
= EXT_CAST(members
->array
[i
]);
371 OSCollection
*coll
= OSDynamicCast(OSCollection
, obj
);
373 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
375 obj
= newColl
; // Rely on cycleDict ref for a bit
381 newMembers
->setObject(obj
);
394 cycleDict
->release();