]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* IOSet.m created by rsulack on Thu 11-Jun-1998 */
27 #include <libkern/c++/OSSet.h>
28 #include <libkern/c++/OSArray.h>
29 #include <libkern/c++/OSSerialize.h>
31 #define super OSCollection
33 OSDefineMetaClassAndStructors(OSSet
, OSCollection
)
34 OSMetaClassDefineReservedUnused(OSSet
, 0);
35 OSMetaClassDefineReservedUnused(OSSet
, 1);
36 OSMetaClassDefineReservedUnused(OSSet
, 2);
37 OSMetaClassDefineReservedUnused(OSSet
, 3);
38 OSMetaClassDefineReservedUnused(OSSet
, 4);
39 OSMetaClassDefineReservedUnused(OSSet
, 5);
40 OSMetaClassDefineReservedUnused(OSSet
, 6);
41 OSMetaClassDefineReservedUnused(OSSet
, 7);
43 bool OSSet::initWithCapacity(unsigned int inCapacity
)
48 members
= OSArray::withCapacity(inCapacity
);
55 bool OSSet::initWithObjects(const OSObject
*inObjects
[],
57 unsigned int inCapacity
)
59 unsigned int capacity
= inCount
;
62 if ( inCount
> inCapacity
)
65 capacity
= inCapacity
;
68 if (!inObjects
|| !initWithCapacity(capacity
))
71 for ( unsigned int i
= 0; i
< inCount
; i
++ ) {
72 if (members
->getCount() < inCapacity
)
73 setObject(inObjects
[i
]);
81 bool OSSet::initWithArray(const OSArray
*inArray
,
82 unsigned int inCapacity
)
87 return initWithObjects((const OSObject
**) inArray
->array
,
88 inArray
->count
, inCapacity
);
91 bool OSSet::initWithSet(const OSSet
*inSet
,
92 unsigned int inCapacity
)
94 return initWithArray(inSet
->members
, inCapacity
);
97 OSSet
*OSSet::withCapacity(unsigned int capacity
)
99 OSSet
*me
= new OSSet
;
101 if (me
&& !me
->initWithCapacity(capacity
)) {
109 OSSet
*OSSet::withObjects(const OSObject
*objects
[],
111 unsigned int capacity
)
113 OSSet
*me
= new OSSet
;
115 if (me
&& !me
->initWithObjects(objects
, count
, capacity
)) {
123 OSSet
*OSSet::withArray(const OSArray
*array
,
124 unsigned int capacity
)
126 OSSet
*me
= new OSSet
;
128 if (me
&& !me
->initWithArray(array
, capacity
)) {
136 OSSet
*OSSet::withSet(const OSSet
*set
,
137 unsigned int capacity
)
139 OSSet
*me
= new OSSet
;
141 if (me
&& !me
->initWithSet(set
, capacity
)) {
157 unsigned int OSSet::getCount() const
159 return members
->count
;
162 unsigned int OSSet::getCapacity() const
164 return members
->capacity
;
167 unsigned int OSSet::getCapacityIncrement() const
169 return members
->capacityIncrement
;
172 unsigned int OSSet::setCapacityIncrement(unsigned int increment
)
174 return members
->setCapacityIncrement(increment
);
177 unsigned int OSSet::ensureCapacity(unsigned int newCapacity
)
179 return members
->ensureCapacity(newCapacity
);
182 void OSSet::flushCollection()
185 members
->flushCollection();
188 bool OSSet::setObject(const OSMetaClassBase
*anObject
)
190 if (containsObject(anObject
))
194 return members
->setObject(anObject
);
198 bool OSSet::merge(const OSArray
*array
)
200 const OSMetaClassBase
*anObject
;
203 for (int i
= 0; (anObject
= array
->getObject(i
)); i
++)
204 if (setObject(anObject
))
210 bool OSSet::merge(const OSSet
*set
)
212 return setObject(set
->members
);
215 void OSSet::removeObject(const OSMetaClassBase
*anObject
)
217 const OSMetaClassBase
*probeObject
;
219 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
220 if (probeObject
== anObject
) {
222 members
->removeObject(i
);
228 bool OSSet::containsObject(const OSMetaClassBase
*anObject
) const
230 return anObject
&& member(anObject
);
233 bool OSSet::member(const OSMetaClassBase
*anObject
) const
235 OSMetaClassBase
*probeObject
;
237 for (int i
= 0; (probeObject
= members
->getObject(i
)); i
++)
238 if (probeObject
== anObject
)
244 OSObject
*OSSet::getAnyObject() const
246 return members
->getObject(0);
249 bool OSSet::isEqualTo(const OSSet
*aSet
) const
253 const OSMetaClassBase
*obj1
;
254 const OSMetaClassBase
*obj2
;
259 count
= members
->count
;
260 if ( count
!= aSet
->getCount() )
263 for ( i
= 0; i
< count
; i
++ ) {
264 obj1
= aSet
->members
->getObject(i
);
265 obj2
= members
->getObject(i
);
266 if ( !obj1
|| !obj2
)
269 if ( !obj1
->isEqualTo(obj2
) )
276 bool OSSet::isEqualTo(const OSMetaClassBase
*anObject
) const
280 otherSet
= OSDynamicCast(OSSet
, anObject
);
282 return isEqualTo(otherSet
);
287 unsigned int OSSet::iteratorSize() const
289 return sizeof(unsigned int);
292 bool OSSet::initIterator(void *inIterator
) const
294 unsigned int *iteratorP
= (unsigned int *) inIterator
;
300 bool OSSet::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
302 unsigned int *iteratorP
= (unsigned int *) inIterator
;
303 unsigned int index
= (*iteratorP
)++;
305 if (index
< members
->count
)
306 *ret
= members
->getObject(index
);
313 bool OSSet::serialize(OSSerialize
*s
) const
315 const OSMetaClassBase
*o
;
317 if (s
->previouslySerialized(this)) return true;
319 if (!s
->addXMLStartTag(this, "set")) return false;
321 for (int i
= 0; (o
= members
->getObject(i
)); i
++) {
322 if (!o
->serialize(s
)) return false;
325 return s
->addXMLEndTag("set");