xref: /xnu-8796.141.3/libkern/c++/OSSet.cpp (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2000, 2014 Apple Computer, Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions /* IOSet.m created by rsulack on Thu 11-Jun-1998 */
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31*1b191cb5SApple OSS Distributions 
32*1b191cb5SApple OSS Distributions #include <libkern/c++/OSArray.h>
33*1b191cb5SApple OSS Distributions #include <libkern/c++/OSDictionary.h>
34*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSerialize.h>
35*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSet.h>
36*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
37*1b191cb5SApple OSS Distributions #include <os/cpp_util.h>
38*1b191cb5SApple OSS Distributions #include <kern/zalloc.h>
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions #define super OSCollection
41*1b191cb5SApple OSS Distributions 
42*1b191cb5SApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(OSSet, OSCollection,
43*1b191cb5SApple OSS Distributions     ZC_ZFREE_CLEARMEM)
44*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 0);
45*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 1);
46*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 2);
47*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 3);
48*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 4);
49*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 5);
50*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 6);
51*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 7);
52*1b191cb5SApple OSS Distributions 
53*1b191cb5SApple OSS Distributions #define EXT_CAST(obj) \
54*1b191cb5SApple OSS Distributions     reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
55*1b191cb5SApple OSS Distributions 
56*1b191cb5SApple OSS Distributions bool
initWithCapacity(unsigned int inCapacity)57*1b191cb5SApple OSS Distributions OSSet::initWithCapacity(unsigned int inCapacity)
58*1b191cb5SApple OSS Distributions {
59*1b191cb5SApple OSS Distributions 	if (!super::init()) {
60*1b191cb5SApple OSS Distributions 		return false;
61*1b191cb5SApple OSS Distributions 	}
62*1b191cb5SApple OSS Distributions 
63*1b191cb5SApple OSS Distributions 	members = OSArray::withCapacity(inCapacity);
64*1b191cb5SApple OSS Distributions 	if (!members) {
65*1b191cb5SApple OSS Distributions 		return false;
66*1b191cb5SApple OSS Distributions 	}
67*1b191cb5SApple OSS Distributions 
68*1b191cb5SApple OSS Distributions 	return true;
69*1b191cb5SApple OSS Distributions }
70*1b191cb5SApple OSS Distributions 
71*1b191cb5SApple OSS Distributions bool
initWithObjects(const OSObject * inObjects[],unsigned int inCount,unsigned int inCapacity)72*1b191cb5SApple OSS Distributions OSSet::initWithObjects(const OSObject *inObjects[],
73*1b191cb5SApple OSS Distributions     unsigned int inCount,
74*1b191cb5SApple OSS Distributions     unsigned int inCapacity)
75*1b191cb5SApple OSS Distributions {
76*1b191cb5SApple OSS Distributions 	unsigned int capacity = inCount;
77*1b191cb5SApple OSS Distributions 
78*1b191cb5SApple OSS Distributions 	if (inCapacity) {
79*1b191cb5SApple OSS Distributions 		if (inCount > inCapacity) {
80*1b191cb5SApple OSS Distributions 			return false;
81*1b191cb5SApple OSS Distributions 		}
82*1b191cb5SApple OSS Distributions 
83*1b191cb5SApple OSS Distributions 		capacity = inCapacity;
84*1b191cb5SApple OSS Distributions 	}
85*1b191cb5SApple OSS Distributions 
86*1b191cb5SApple OSS Distributions 	if (!inObjects || !initWithCapacity(capacity)) {
87*1b191cb5SApple OSS Distributions 		return false;
88*1b191cb5SApple OSS Distributions 	}
89*1b191cb5SApple OSS Distributions 
90*1b191cb5SApple OSS Distributions 	for (unsigned int i = 0; i < inCount; i++) {
91*1b191cb5SApple OSS Distributions // xx-review: no test here for failure of setObject()
92*1b191cb5SApple OSS Distributions 		if (members->getCount() < capacity) {
93*1b191cb5SApple OSS Distributions 			setObject(inObjects[i]);
94*1b191cb5SApple OSS Distributions 		} else {
95*1b191cb5SApple OSS Distributions 			return false;
96*1b191cb5SApple OSS Distributions 		}
97*1b191cb5SApple OSS Distributions 	}
98*1b191cb5SApple OSS Distributions 
99*1b191cb5SApple OSS Distributions 	return true;
100*1b191cb5SApple OSS Distributions }
101*1b191cb5SApple OSS Distributions 
102*1b191cb5SApple OSS Distributions bool
initWithArray(const OSArray * inArray,unsigned int inCapacity)103*1b191cb5SApple OSS Distributions OSSet::initWithArray(const OSArray *inArray,
104*1b191cb5SApple OSS Distributions     unsigned int inCapacity)
105*1b191cb5SApple OSS Distributions {
106*1b191cb5SApple OSS Distributions 	if (!inArray) {
107*1b191cb5SApple OSS Distributions 		return false;
108*1b191cb5SApple OSS Distributions 	}
109*1b191cb5SApple OSS Distributions 
110*1b191cb5SApple OSS Distributions 	return initWithObjects((const OSObject **) inArray->array,
111*1b191cb5SApple OSS Distributions 	           inArray->count, inCapacity);
112*1b191cb5SApple OSS Distributions }
113*1b191cb5SApple OSS Distributions 
114*1b191cb5SApple OSS Distributions bool
initWithSet(const OSSet * inSet,unsigned int inCapacity)115*1b191cb5SApple OSS Distributions OSSet::initWithSet(const OSSet *inSet,
116*1b191cb5SApple OSS Distributions     unsigned int inCapacity)
117*1b191cb5SApple OSS Distributions {
118*1b191cb5SApple OSS Distributions 	return initWithArray(inSet->members.get(), inCapacity);
119*1b191cb5SApple OSS Distributions }
120*1b191cb5SApple OSS Distributions 
121*1b191cb5SApple OSS Distributions OSSharedPtr<OSSet>
withCapacity(unsigned int capacity)122*1b191cb5SApple OSS Distributions OSSet::withCapacity(unsigned int capacity)
123*1b191cb5SApple OSS Distributions {
124*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
125*1b191cb5SApple OSS Distributions 
126*1b191cb5SApple OSS Distributions 	if (me && !me->initWithCapacity(capacity)) {
127*1b191cb5SApple OSS Distributions 		return nullptr;
128*1b191cb5SApple OSS Distributions 	}
129*1b191cb5SApple OSS Distributions 
130*1b191cb5SApple OSS Distributions 	return me;
131*1b191cb5SApple OSS Distributions }
132*1b191cb5SApple OSS Distributions 
133*1b191cb5SApple OSS Distributions OSSharedPtr<OSSet>
withObjects(const OSObject * objects[],unsigned int count,unsigned int capacity)134*1b191cb5SApple OSS Distributions OSSet::withObjects(const OSObject *objects[],
135*1b191cb5SApple OSS Distributions     unsigned int count,
136*1b191cb5SApple OSS Distributions     unsigned int capacity)
137*1b191cb5SApple OSS Distributions {
138*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
139*1b191cb5SApple OSS Distributions 
140*1b191cb5SApple OSS Distributions 	if (me && !me->initWithObjects(objects, count, capacity)) {
141*1b191cb5SApple OSS Distributions 		return nullptr;
142*1b191cb5SApple OSS Distributions 	}
143*1b191cb5SApple OSS Distributions 
144*1b191cb5SApple OSS Distributions 	return me;
145*1b191cb5SApple OSS Distributions }
146*1b191cb5SApple OSS Distributions 
147*1b191cb5SApple OSS Distributions OSSharedPtr<OSSet>
withArray(const OSArray * array,unsigned int capacity)148*1b191cb5SApple OSS Distributions OSSet::withArray(const OSArray *array,
149*1b191cb5SApple OSS Distributions     unsigned int capacity)
150*1b191cb5SApple OSS Distributions {
151*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
152*1b191cb5SApple OSS Distributions 
153*1b191cb5SApple OSS Distributions 	if (me && !me->initWithArray(array, capacity)) {
154*1b191cb5SApple OSS Distributions 		return nullptr;
155*1b191cb5SApple OSS Distributions 	}
156*1b191cb5SApple OSS Distributions 
157*1b191cb5SApple OSS Distributions 	return me;
158*1b191cb5SApple OSS Distributions }
159*1b191cb5SApple OSS Distributions 
160*1b191cb5SApple OSS Distributions OSSharedPtr<OSSet>
withSet(const OSSet * set,unsigned int capacity)161*1b191cb5SApple OSS Distributions OSSet::withSet(const OSSet *set,
162*1b191cb5SApple OSS Distributions     unsigned int capacity)
163*1b191cb5SApple OSS Distributions {
164*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
165*1b191cb5SApple OSS Distributions 
166*1b191cb5SApple OSS Distributions 	if (me && !me->initWithSet(set, capacity)) {
167*1b191cb5SApple OSS Distributions 		return nullptr;
168*1b191cb5SApple OSS Distributions 	}
169*1b191cb5SApple OSS Distributions 
170*1b191cb5SApple OSS Distributions 	return me;
171*1b191cb5SApple OSS Distributions }
172*1b191cb5SApple OSS Distributions 
173*1b191cb5SApple OSS Distributions void
free()174*1b191cb5SApple OSS Distributions OSSet::free()
175*1b191cb5SApple OSS Distributions {
176*1b191cb5SApple OSS Distributions 	if (members) {
177*1b191cb5SApple OSS Distributions 		(void) members->super::setOptions(0, kImmutable);
178*1b191cb5SApple OSS Distributions 	}
179*1b191cb5SApple OSS Distributions 
180*1b191cb5SApple OSS Distributions 	super::free();
181*1b191cb5SApple OSS Distributions }
182*1b191cb5SApple OSS Distributions 
183*1b191cb5SApple OSS Distributions unsigned int
getCount() const184*1b191cb5SApple OSS Distributions OSSet::getCount() const
185*1b191cb5SApple OSS Distributions {
186*1b191cb5SApple OSS Distributions 	return members->count;
187*1b191cb5SApple OSS Distributions }
188*1b191cb5SApple OSS Distributions 
189*1b191cb5SApple OSS Distributions unsigned int
getCapacity() const190*1b191cb5SApple OSS Distributions OSSet::getCapacity() const
191*1b191cb5SApple OSS Distributions {
192*1b191cb5SApple OSS Distributions 	return members->capacity;
193*1b191cb5SApple OSS Distributions }
194*1b191cb5SApple OSS Distributions 
195*1b191cb5SApple OSS Distributions unsigned int
getCapacityIncrement() const196*1b191cb5SApple OSS Distributions OSSet::getCapacityIncrement() const
197*1b191cb5SApple OSS Distributions {
198*1b191cb5SApple OSS Distributions 	return members->capacityIncrement;
199*1b191cb5SApple OSS Distributions }
200*1b191cb5SApple OSS Distributions 
201*1b191cb5SApple OSS Distributions unsigned int
setCapacityIncrement(unsigned int increment)202*1b191cb5SApple OSS Distributions OSSet::setCapacityIncrement(unsigned int increment)
203*1b191cb5SApple OSS Distributions {
204*1b191cb5SApple OSS Distributions 	return members->setCapacityIncrement(increment);
205*1b191cb5SApple OSS Distributions }
206*1b191cb5SApple OSS Distributions 
207*1b191cb5SApple OSS Distributions unsigned int
ensureCapacity(unsigned int newCapacity)208*1b191cb5SApple OSS Distributions OSSet::ensureCapacity(unsigned int newCapacity)
209*1b191cb5SApple OSS Distributions {
210*1b191cb5SApple OSS Distributions 	return members->ensureCapacity(newCapacity);
211*1b191cb5SApple OSS Distributions }
212*1b191cb5SApple OSS Distributions 
213*1b191cb5SApple OSS Distributions void
flushCollection()214*1b191cb5SApple OSS Distributions OSSet::flushCollection()
215*1b191cb5SApple OSS Distributions {
216*1b191cb5SApple OSS Distributions 	haveUpdated();
217*1b191cb5SApple OSS Distributions 	members->flushCollection();
218*1b191cb5SApple OSS Distributions }
219*1b191cb5SApple OSS Distributions 
220*1b191cb5SApple OSS Distributions bool
setObject(const OSMetaClassBase * anObject)221*1b191cb5SApple OSS Distributions OSSet::setObject(const OSMetaClassBase *anObject)
222*1b191cb5SApple OSS Distributions {
223*1b191cb5SApple OSS Distributions 	if (containsObject(anObject)) {
224*1b191cb5SApple OSS Distributions 		return false;
225*1b191cb5SApple OSS Distributions 	} else {
226*1b191cb5SApple OSS Distributions 		haveUpdated();
227*1b191cb5SApple OSS Distributions 		return members->setObject(anObject);
228*1b191cb5SApple OSS Distributions 	}
229*1b191cb5SApple OSS Distributions }
230*1b191cb5SApple OSS Distributions 
231*1b191cb5SApple OSS Distributions bool
setObject(OSSharedPtr<const OSMetaClassBase> const & anObject)232*1b191cb5SApple OSS Distributions OSSet::setObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
233*1b191cb5SApple OSS Distributions {
234*1b191cb5SApple OSS Distributions 	return setObject(anObject.get());
235*1b191cb5SApple OSS Distributions }
236*1b191cb5SApple OSS Distributions 
237*1b191cb5SApple OSS Distributions bool
merge(const OSArray * array)238*1b191cb5SApple OSS Distributions OSSet::merge(const OSArray * array)
239*1b191cb5SApple OSS Distributions {
240*1b191cb5SApple OSS Distributions 	const OSMetaClassBase * anObject = NULL;
241*1b191cb5SApple OSS Distributions 	bool                    result   = true;
242*1b191cb5SApple OSS Distributions 
243*1b191cb5SApple OSS Distributions 	for (int i = 0; (anObject = array->getObject(i)); i++) {
244*1b191cb5SApple OSS Distributions 		/* setObject() returns false if the object is already in the set,
245*1b191cb5SApple OSS Distributions 		 * so we have to check beforehand here with containsObject().
246*1b191cb5SApple OSS Distributions 		 */
247*1b191cb5SApple OSS Distributions 		if (containsObject(anObject)) {
248*1b191cb5SApple OSS Distributions 			continue;
249*1b191cb5SApple OSS Distributions 		}
250*1b191cb5SApple OSS Distributions 		if (!setObject(anObject)) {
251*1b191cb5SApple OSS Distributions 			result = false;
252*1b191cb5SApple OSS Distributions 		}
253*1b191cb5SApple OSS Distributions 	}
254*1b191cb5SApple OSS Distributions 
255*1b191cb5SApple OSS Distributions 	return result;
256*1b191cb5SApple OSS Distributions }
257*1b191cb5SApple OSS Distributions 
258*1b191cb5SApple OSS Distributions bool
merge(const OSSet * set)259*1b191cb5SApple OSS Distributions OSSet::merge(const OSSet * set)
260*1b191cb5SApple OSS Distributions {
261*1b191cb5SApple OSS Distributions 	return merge(set->members.get());
262*1b191cb5SApple OSS Distributions }
263*1b191cb5SApple OSS Distributions 
264*1b191cb5SApple OSS Distributions void
removeObject(const OSMetaClassBase * anObject)265*1b191cb5SApple OSS Distributions OSSet::removeObject(const OSMetaClassBase *anObject)
266*1b191cb5SApple OSS Distributions {
267*1b191cb5SApple OSS Distributions 	const OSMetaClassBase *probeObject;
268*1b191cb5SApple OSS Distributions 
269*1b191cb5SApple OSS Distributions 	for (int i = 0; (probeObject = members->getObject(i)); i++) {
270*1b191cb5SApple OSS Distributions 		if (probeObject == anObject) {
271*1b191cb5SApple OSS Distributions 			haveUpdated();
272*1b191cb5SApple OSS Distributions 			members->removeObject(i);
273*1b191cb5SApple OSS Distributions 			return;
274*1b191cb5SApple OSS Distributions 		}
275*1b191cb5SApple OSS Distributions 	}
276*1b191cb5SApple OSS Distributions }
277*1b191cb5SApple OSS Distributions 
278*1b191cb5SApple OSS Distributions void
removeObject(OSSharedPtr<const OSMetaClassBase> const & anObject)279*1b191cb5SApple OSS Distributions OSSet::removeObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
280*1b191cb5SApple OSS Distributions {
281*1b191cb5SApple OSS Distributions 	removeObject(anObject.get());
282*1b191cb5SApple OSS Distributions }
283*1b191cb5SApple OSS Distributions 
284*1b191cb5SApple OSS Distributions 
285*1b191cb5SApple OSS Distributions bool
containsObject(const OSMetaClassBase * anObject) const286*1b191cb5SApple OSS Distributions OSSet::containsObject(const OSMetaClassBase *anObject) const
287*1b191cb5SApple OSS Distributions {
288*1b191cb5SApple OSS Distributions 	return anObject && member(anObject);
289*1b191cb5SApple OSS Distributions }
290*1b191cb5SApple OSS Distributions 
291*1b191cb5SApple OSS Distributions bool
member(const OSMetaClassBase * anObject) const292*1b191cb5SApple OSS Distributions OSSet::member(const OSMetaClassBase *anObject) const
293*1b191cb5SApple OSS Distributions {
294*1b191cb5SApple OSS Distributions 	OSMetaClassBase *probeObject;
295*1b191cb5SApple OSS Distributions 
296*1b191cb5SApple OSS Distributions 	for (int i = 0; (probeObject = members->getObject(i)); i++) {
297*1b191cb5SApple OSS Distributions 		if (probeObject == anObject) {
298*1b191cb5SApple OSS Distributions 			return true;
299*1b191cb5SApple OSS Distributions 		}
300*1b191cb5SApple OSS Distributions 	}
301*1b191cb5SApple OSS Distributions 
302*1b191cb5SApple OSS Distributions 	return false;
303*1b191cb5SApple OSS Distributions }
304*1b191cb5SApple OSS Distributions 
305*1b191cb5SApple OSS Distributions OSObject *
getAnyObject() const306*1b191cb5SApple OSS Distributions OSSet::getAnyObject() const
307*1b191cb5SApple OSS Distributions {
308*1b191cb5SApple OSS Distributions 	return members->getObject(0);
309*1b191cb5SApple OSS Distributions }
310*1b191cb5SApple OSS Distributions 
311*1b191cb5SApple OSS Distributions bool
isEqualTo(const OSSet * aSet) const312*1b191cb5SApple OSS Distributions OSSet::isEqualTo(const OSSet *aSet) const
313*1b191cb5SApple OSS Distributions {
314*1b191cb5SApple OSS Distributions 	unsigned int count;
315*1b191cb5SApple OSS Distributions 	unsigned int i;
316*1b191cb5SApple OSS Distributions 	const OSMetaClassBase *obj1;
317*1b191cb5SApple OSS Distributions 	const OSMetaClassBase *obj2;
318*1b191cb5SApple OSS Distributions 
319*1b191cb5SApple OSS Distributions 	if (this == aSet) {
320*1b191cb5SApple OSS Distributions 		return true;
321*1b191cb5SApple OSS Distributions 	}
322*1b191cb5SApple OSS Distributions 
323*1b191cb5SApple OSS Distributions 	count = members->count;
324*1b191cb5SApple OSS Distributions 	if (count != aSet->getCount()) {
325*1b191cb5SApple OSS Distributions 		return false;
326*1b191cb5SApple OSS Distributions 	}
327*1b191cb5SApple OSS Distributions 
328*1b191cb5SApple OSS Distributions 	for (i = 0; i < count; i++) {
329*1b191cb5SApple OSS Distributions 		obj1 = aSet->members->getObject(i);
330*1b191cb5SApple OSS Distributions 		if (containsObject(obj1)) {
331*1b191cb5SApple OSS Distributions 			continue;
332*1b191cb5SApple OSS Distributions 		}
333*1b191cb5SApple OSS Distributions 		obj2 = members->getObject(i);
334*1b191cb5SApple OSS Distributions 		if (!obj1 || !obj2) {
335*1b191cb5SApple OSS Distributions 			return false;
336*1b191cb5SApple OSS Distributions 		}
337*1b191cb5SApple OSS Distributions 
338*1b191cb5SApple OSS Distributions 		if (!obj1->isEqualTo(obj2)) {
339*1b191cb5SApple OSS Distributions 			return false;
340*1b191cb5SApple OSS Distributions 		}
341*1b191cb5SApple OSS Distributions 	}
342*1b191cb5SApple OSS Distributions 
343*1b191cb5SApple OSS Distributions 	return true;
344*1b191cb5SApple OSS Distributions }
345*1b191cb5SApple OSS Distributions 
346*1b191cb5SApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * anObject) const347*1b191cb5SApple OSS Distributions OSSet::isEqualTo(const OSMetaClassBase *anObject) const
348*1b191cb5SApple OSS Distributions {
349*1b191cb5SApple OSS Distributions 	OSSet *otherSet;
350*1b191cb5SApple OSS Distributions 
351*1b191cb5SApple OSS Distributions 	otherSet = OSDynamicCast(OSSet, anObject);
352*1b191cb5SApple OSS Distributions 	if (otherSet) {
353*1b191cb5SApple OSS Distributions 		return isEqualTo(otherSet);
354*1b191cb5SApple OSS Distributions 	} else {
355*1b191cb5SApple OSS Distributions 		return false;
356*1b191cb5SApple OSS Distributions 	}
357*1b191cb5SApple OSS Distributions }
358*1b191cb5SApple OSS Distributions 
359*1b191cb5SApple OSS Distributions unsigned int
iteratorSize() const360*1b191cb5SApple OSS Distributions OSSet::iteratorSize() const
361*1b191cb5SApple OSS Distributions {
362*1b191cb5SApple OSS Distributions 	return sizeof(unsigned int);
363*1b191cb5SApple OSS Distributions }
364*1b191cb5SApple OSS Distributions 
365*1b191cb5SApple OSS Distributions bool
initIterator(void * inIterator) const366*1b191cb5SApple OSS Distributions OSSet::initIterator(void *inIterator) const
367*1b191cb5SApple OSS Distributions {
368*1b191cb5SApple OSS Distributions 	unsigned int *iteratorP = (unsigned int *) inIterator;
369*1b191cb5SApple OSS Distributions 
370*1b191cb5SApple OSS Distributions 	*iteratorP = 0;
371*1b191cb5SApple OSS Distributions 	return true;
372*1b191cb5SApple OSS Distributions }
373*1b191cb5SApple OSS Distributions 
374*1b191cb5SApple OSS Distributions bool
getNextObjectForIterator(void * inIterator,OSObject ** ret) const375*1b191cb5SApple OSS Distributions OSSet::getNextObjectForIterator(void *inIterator, OSObject **ret) const
376*1b191cb5SApple OSS Distributions {
377*1b191cb5SApple OSS Distributions 	unsigned int *iteratorP = (unsigned int *) inIterator;
378*1b191cb5SApple OSS Distributions 	unsigned int index = (*iteratorP)++;
379*1b191cb5SApple OSS Distributions 
380*1b191cb5SApple OSS Distributions 	if (index < members->count) {
381*1b191cb5SApple OSS Distributions 		*ret = members->getObject(index);
382*1b191cb5SApple OSS Distributions 	} else {
383*1b191cb5SApple OSS Distributions 		*ret = NULL;
384*1b191cb5SApple OSS Distributions 	}
385*1b191cb5SApple OSS Distributions 
386*1b191cb5SApple OSS Distributions 	return *ret != NULL;
387*1b191cb5SApple OSS Distributions }
388*1b191cb5SApple OSS Distributions 
389*1b191cb5SApple OSS Distributions bool
serialize(OSSerialize * s) const390*1b191cb5SApple OSS Distributions OSSet::serialize(OSSerialize *s) const
391*1b191cb5SApple OSS Distributions {
392*1b191cb5SApple OSS Distributions 	const OSMetaClassBase *o;
393*1b191cb5SApple OSS Distributions 
394*1b191cb5SApple OSS Distributions 	if (s->previouslySerialized(this)) {
395*1b191cb5SApple OSS Distributions 		return true;
396*1b191cb5SApple OSS Distributions 	}
397*1b191cb5SApple OSS Distributions 
398*1b191cb5SApple OSS Distributions 	if (!s->addXMLStartTag(this, "set")) {
399*1b191cb5SApple OSS Distributions 		return false;
400*1b191cb5SApple OSS Distributions 	}
401*1b191cb5SApple OSS Distributions 
402*1b191cb5SApple OSS Distributions 	for (int i = 0; (o = members->getObject(i)); i++) {
403*1b191cb5SApple OSS Distributions 		if (!o->serialize(s)) {
404*1b191cb5SApple OSS Distributions 			return false;
405*1b191cb5SApple OSS Distributions 		}
406*1b191cb5SApple OSS Distributions 	}
407*1b191cb5SApple OSS Distributions 
408*1b191cb5SApple OSS Distributions 	return s->addXMLEndTag("set");
409*1b191cb5SApple OSS Distributions }
410*1b191cb5SApple OSS Distributions 
411*1b191cb5SApple OSS Distributions unsigned
setOptions(unsigned options,unsigned mask,void *)412*1b191cb5SApple OSS Distributions OSSet::setOptions(unsigned options, unsigned mask, void *)
413*1b191cb5SApple OSS Distributions {
414*1b191cb5SApple OSS Distributions 	unsigned old = super::setOptions(options, mask);
415*1b191cb5SApple OSS Distributions 	if ((old ^ options) & mask) {
416*1b191cb5SApple OSS Distributions 		members->setOptions(options, mask);
417*1b191cb5SApple OSS Distributions 	}
418*1b191cb5SApple OSS Distributions 
419*1b191cb5SApple OSS Distributions 	return old;
420*1b191cb5SApple OSS Distributions }
421*1b191cb5SApple OSS Distributions 
422*1b191cb5SApple OSS Distributions OSSharedPtr<OSCollection>
copyCollection(OSDictionary * cycleDict)423*1b191cb5SApple OSS Distributions OSSet::copyCollection(OSDictionary *cycleDict)
424*1b191cb5SApple OSS Distributions {
425*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSDictionary> ourCycleDict;
426*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSCollection> ret;
427*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSSet> newSet;
428*1b191cb5SApple OSS Distributions 
429*1b191cb5SApple OSS Distributions 	if (!cycleDict) {
430*1b191cb5SApple OSS Distributions 		ourCycleDict = OSDictionary::withCapacity(16);
431*1b191cb5SApple OSS Distributions 		if (!ourCycleDict) {
432*1b191cb5SApple OSS Distributions 			return nullptr;
433*1b191cb5SApple OSS Distributions 		}
434*1b191cb5SApple OSS Distributions 		cycleDict = ourCycleDict.get();
435*1b191cb5SApple OSS Distributions 	}
436*1b191cb5SApple OSS Distributions 
437*1b191cb5SApple OSS Distributions 	do {
438*1b191cb5SApple OSS Distributions 		// Check for a cycle
439*1b191cb5SApple OSS Distributions 		ret = super::copyCollection(cycleDict);
440*1b191cb5SApple OSS Distributions 		if (ret) {
441*1b191cb5SApple OSS Distributions 			continue; // Found it
442*1b191cb5SApple OSS Distributions 		}
443*1b191cb5SApple OSS Distributions 		newSet = OSSet::withCapacity(members->capacity);
444*1b191cb5SApple OSS Distributions 		if (!newSet) {
445*1b191cb5SApple OSS Distributions 			continue; // Couldn't create new set abort
446*1b191cb5SApple OSS Distributions 		}
447*1b191cb5SApple OSS Distributions 		// Insert object into cycle Dictionary
448*1b191cb5SApple OSS Distributions 		cycleDict->setObject((const OSSymbol *) this, newSet.get());
449*1b191cb5SApple OSS Distributions 
450*1b191cb5SApple OSS Distributions 		OSArray *newMembers = newSet->members.get();
451*1b191cb5SApple OSS Distributions 		newMembers->capacityIncrement = members->capacityIncrement;
452*1b191cb5SApple OSS Distributions 
453*1b191cb5SApple OSS Distributions 		// Now copy over the contents into the new duplicate
454*1b191cb5SApple OSS Distributions 		for (unsigned int i = 0; i < members->count; i++) {
455*1b191cb5SApple OSS Distributions 			OSObject *obj = EXT_CAST(members->array[i].get());
456*1b191cb5SApple OSS Distributions 			OSCollection *coll = OSDynamicCast(OSCollection, obj);
457*1b191cb5SApple OSS Distributions 			if (coll) {
458*1b191cb5SApple OSS Distributions 				OSSharedPtr<OSCollection> newColl = coll->copyCollection(cycleDict);
459*1b191cb5SApple OSS Distributions 				if (newColl) {
460*1b191cb5SApple OSS Distributions 					obj = newColl.get(); // Rely on cycleDict ref for a bit
461*1b191cb5SApple OSS Distributions 				} else {
462*1b191cb5SApple OSS Distributions 					return ret;
463*1b191cb5SApple OSS Distributions 				}
464*1b191cb5SApple OSS Distributions 			}
465*1b191cb5SApple OSS Distributions 			newMembers->setObject(obj);
466*1b191cb5SApple OSS Distributions 		}
467*1b191cb5SApple OSS Distributions 
468*1b191cb5SApple OSS Distributions 		ret = os::move(newSet);
469*1b191cb5SApple OSS Distributions 	} while (false);
470*1b191cb5SApple OSS Distributions 
471*1b191cb5SApple OSS Distributions 	return ret;
472*1b191cb5SApple OSS Distributions }
473