]>
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++/OSSet.h>
25 #include <libkern/c++/OSArray.h>
26 #include <libkern/c++/OSSerialize.h>
28 #define super OSCollection
30 OSDefineMetaClassAndStructors(OSSet
, OSCollection
)
31 OSMetaClassDefineReservedUnused(OSSet
, 0);
32 OSMetaClassDefineReservedUnused(OSSet
, 1);
33 OSMetaClassDefineReservedUnused(OSSet
, 2);
34 OSMetaClassDefineReservedUnused(OSSet
, 3);
35 OSMetaClassDefineReservedUnused(OSSet
, 4);
36 OSMetaClassDefineReservedUnused(OSSet
, 5);
37 OSMetaClassDefineReservedUnused(OSSet
, 6);
38 OSMetaClassDefineReservedUnused(OSSet
, 7);
40 bool OSSet::initWithCapacity(unsigned int inCapacity
)
45 members
= OSArray::withCapacity(inCapacity
);
52 bool OSSet::initWithObjects(const OSObject
*inObjects
[],
54 unsigned int inCapacity
= 0)
56 unsigned int capacity
= inCount
;
59 if ( inCount
> inCapacity
)
62 capacity
= inCapacity
;
65 if (!inObjects
|| !initWithCapacity(capacity
))
68 for ( unsigned int i
= 0; i
< inCount
; i
++ ) {
69 if (members
->getCount() < inCapacity
)
70 setObject(inObjects
[i
]);
78 bool OSSet::initWithArray(const OSArray
*inArray
,
79 unsigned int inCapacity
= 0)
84 return initWithObjects((const OSObject
**) inArray
->array
,
85 inArray
->count
, inCapacity
);
88 bool OSSet::initWithSet(const OSSet
*inSet
,
89 unsigned int inCapacity
= 0)
91 return initWithArray(inSet
->members
, inCapacity
);
94 OSSet
*OSSet::withCapacity(unsigned int capacity
)
96 OSSet
*me
= new OSSet
;
98 if (me
&& !me
->initWithCapacity(capacity
)) {
106 OSSet
*OSSet::withObjects(const OSObject
*objects
[],
108 unsigned int capacity
= 0)
110 OSSet
*me
= new OSSet
;
112 if (me
&& !me
->initWithObjects(objects
, count
, capacity
)) {
120 OSSet
*OSSet::withArray(const OSArray
*array
,
121 unsigned int capacity
= 0)
123 OSSet
*me
= new OSSet
;
125 if (me
&& !me
->initWithArray(array
, capacity
)) {
133 OSSet
*OSSet::withSet(const OSSet
*set
,
134 unsigned int capacity
= 0)
136 OSSet
*me
= new OSSet
;
138 if (me
&& !me
->initWithSet(set
, capacity
)) {
154 unsigned int OSSet::getCount() const
156 return members
->count
;
159 unsigned int OSSet::getCapacity() const
161 return members
->capacity
;
164 unsigned int OSSet::getCapacityIncrement() const
166 return members
->capacityIncrement
;
169 unsigned int OSSet::setCapacityIncrement(unsigned int increment
)
171 return members
->setCapacityIncrement(increment
);
174 unsigned int OSSet::ensureCapacity(unsigned int newCapacity
)
176 return members
->ensureCapacity(newCapacity
);
179 void OSSet::flushCollection()
182 members
->flushCollection();
185 bool OSSet::setObject(const OSMetaClassBase
*anObject
)
187 if (containsObject(anObject
))
191 return members
->setObject(anObject
);
195 bool OSSet::merge(const OSArray
*array
)
197 const OSMetaClassBase
*anObject
;
200 for (int i
= 0; (anObject
= array
->getObject(i
)); i
++)
201 if (setObject(anObject
))
207 bool OSSet::merge(const OSSet
*set
)
209 return setObject(set
->members
);
212 void OSSet::removeObject(const OSMetaClassBase
*anObject
)
214 const OSMetaClassBase
*probeObject
;
216 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
217 if (probeObject
== anObject
) {
219 members
->removeObject(i
);
225 bool OSSet::containsObject(const OSMetaClassBase
*anObject
) const
227 return anObject
&& member(anObject
);
230 bool OSSet::member(const OSMetaClassBase
*anObject
) const
232 OSMetaClassBase
*probeObject
;
234 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
235 if (probeObject
== anObject
)
241 OSObject
*OSSet::getAnyObject() const
243 return members
->getObject(0);
246 bool OSSet::isEqualTo(const OSSet
*aSet
) const
250 const OSMetaClassBase
*obj1
;
251 const OSMetaClassBase
*obj2
;
256 count
= members
->count
;
257 if ( count
!= aSet
->getCount() )
260 for ( i
= 0; i
< count
; i
++ ) {
261 obj1
= aSet
->members
->getObject(i
);
262 obj2
= members
->getObject(i
);
263 if ( !obj1
|| !obj2
)
266 if ( !obj1
->isEqualTo(obj2
) )
273 bool OSSet::isEqualTo(const OSMetaClassBase
*anObject
) const
277 otherSet
= OSDynamicCast(OSSet
, anObject
);
279 return isEqualTo(otherSet
);
284 unsigned int OSSet::iteratorSize() const
286 return sizeof(unsigned int);
289 bool OSSet::initIterator(void *inIterator
) const
291 unsigned int *iteratorP
= (unsigned int *) inIterator
;
297 bool OSSet::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
299 unsigned int *iteratorP
= (unsigned int *) inIterator
;
300 unsigned int index
= (*iteratorP
)++;
302 if (index
< members
->count
)
303 *ret
= members
->getObject(index
);
310 bool OSSet::serialize(OSSerialize
*s
) const
312 const OSMetaClassBase
*o
;
314 if (s
->previouslySerialized(this)) return true;
316 if (!s
->addXMLStartTag(this, "set")) return false;
318 for (int i
= 0; (o
= members
->getObject(i
)); i
++) {
319 if (!o
->serialize(s
)) return false;
322 return s
->addXMLEndTag("set");