xref: /xnu-8796.141.3/libkern/c++/OSOrderedSet.cpp (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2000 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 
29*1b191cb5SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*1b191cb5SApple OSS Distributions 
31*1b191cb5SApple OSS Distributions #include <libkern/c++/OSDictionary.h>
32*1b191cb5SApple OSS Distributions #include <libkern/c++/OSLib.h>
33*1b191cb5SApple OSS Distributions #include <libkern/c++/OSOrderedSet.h>
34*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
35*1b191cb5SApple OSS Distributions #include <os/cpp_util.h>
36*1b191cb5SApple OSS Distributions 
37*1b191cb5SApple OSS Distributions #define super OSCollection
38*1b191cb5SApple OSS Distributions 
39*1b191cb5SApple OSS Distributions OSDefineMetaClassAndStructors(OSOrderedSet, OSCollection)
40*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 0);
41*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 1);
42*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 2);
43*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 3);
44*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 4);
45*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 5);
46*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 6);
47*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 7);
48*1b191cb5SApple OSS Distributions 
49*1b191cb5SApple OSS Distributions 
50*1b191cb5SApple OSS Distributions struct _Element {
51*1b191cb5SApple OSS Distributions 	OSTaggedPtr<const OSMetaClassBase> obj;
52*1b191cb5SApple OSS Distributions };
53*1b191cb5SApple OSS Distributions 
54*1b191cb5SApple OSS Distributions #define EXT_CAST(obj) \
55*1b191cb5SApple OSS Distributions     reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
56*1b191cb5SApple OSS Distributions 
57*1b191cb5SApple OSS Distributions bool
58*1b191cb5SApple OSS Distributions OSOrderedSet::
initWithCapacity(unsigned int inCapacity,OSOrderFunction inOrdering,void * inOrderingRef)59*1b191cb5SApple OSS Distributions initWithCapacity(unsigned int inCapacity,
60*1b191cb5SApple OSS Distributions     OSOrderFunction inOrdering, void *inOrderingRef)
61*1b191cb5SApple OSS Distributions {
62*1b191cb5SApple OSS Distributions 	if (!super::init()) {
63*1b191cb5SApple OSS Distributions 		return false;
64*1b191cb5SApple OSS Distributions 	}
65*1b191cb5SApple OSS Distributions 
66*1b191cb5SApple OSS Distributions 	if (inCapacity > (UINT_MAX / sizeof(_Element))) {
67*1b191cb5SApple OSS Distributions 		return false;
68*1b191cb5SApple OSS Distributions 	}
69*1b191cb5SApple OSS Distributions 
70*1b191cb5SApple OSS Distributions 	array = kallocp_type_container(_Element, &inCapacity, Z_WAITOK_ZERO);
71*1b191cb5SApple OSS Distributions 	if (!array) {
72*1b191cb5SApple OSS Distributions 		return false;
73*1b191cb5SApple OSS Distributions 	}
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions 	count = 0;
76*1b191cb5SApple OSS Distributions 	capacity = inCapacity;
77*1b191cb5SApple OSS Distributions 	capacityIncrement = (inCapacity)? inCapacity : 16;
78*1b191cb5SApple OSS Distributions 	ordering = inOrdering;
79*1b191cb5SApple OSS Distributions 	orderingRef = inOrderingRef;
80*1b191cb5SApple OSS Distributions 
81*1b191cb5SApple OSS Distributions 	OSCONTAINER_ACCUMSIZE(sizeof(_Element) * inCapacity);
82*1b191cb5SApple OSS Distributions 
83*1b191cb5SApple OSS Distributions 	return true;
84*1b191cb5SApple OSS Distributions }
85*1b191cb5SApple OSS Distributions 
86*1b191cb5SApple OSS Distributions OSSharedPtr<OSOrderedSet>
87*1b191cb5SApple OSS Distributions OSOrderedSet::
withCapacity(unsigned int capacity,OSOrderFunction ordering,void * orderingRef)88*1b191cb5SApple OSS Distributions withCapacity(unsigned int capacity,
89*1b191cb5SApple OSS Distributions     OSOrderFunction ordering, void * orderingRef)
90*1b191cb5SApple OSS Distributions {
91*1b191cb5SApple OSS Distributions 	auto me = OSMakeShared<OSOrderedSet>();
92*1b191cb5SApple OSS Distributions 
93*1b191cb5SApple OSS Distributions 	if (me && !me->initWithCapacity(capacity, ordering, orderingRef)) {
94*1b191cb5SApple OSS Distributions 		return nullptr;
95*1b191cb5SApple OSS Distributions 	}
96*1b191cb5SApple OSS Distributions 
97*1b191cb5SApple OSS Distributions 	return me;
98*1b191cb5SApple OSS Distributions }
99*1b191cb5SApple OSS Distributions 
100*1b191cb5SApple OSS Distributions static SInt32
OSOrderedSetBlockToFunc(const OSMetaClassBase * obj1,const OSMetaClassBase * obj2,void * context)101*1b191cb5SApple OSS Distributions OSOrderedSetBlockToFunc(const OSMetaClassBase * obj1,
102*1b191cb5SApple OSS Distributions     const OSMetaClassBase * obj2,
103*1b191cb5SApple OSS Distributions     void * context)
104*1b191cb5SApple OSS Distributions {
105*1b191cb5SApple OSS Distributions 	OSOrderedSet::OSOrderBlock ordering = (typeof(ordering))context;
106*1b191cb5SApple OSS Distributions 
107*1b191cb5SApple OSS Distributions 	return ordering(obj1, obj2);
108*1b191cb5SApple OSS Distributions }
109*1b191cb5SApple OSS Distributions 
110*1b191cb5SApple OSS Distributions 
111*1b191cb5SApple OSS Distributions OSSharedPtr<OSOrderedSet>
withCapacity(unsigned int capacity,OSOrderBlock ordering)112*1b191cb5SApple OSS Distributions OSOrderedSet::withCapacity(unsigned int capacity, OSOrderBlock ordering)
113*1b191cb5SApple OSS Distributions {
114*1b191cb5SApple OSS Distributions 	auto me = OSMakeShared<OSOrderedSet>();
115*1b191cb5SApple OSS Distributions 
116*1b191cb5SApple OSS Distributions 	if (me && !me->initWithCapacity(capacity, &OSOrderedSetBlockToFunc, ordering)) {
117*1b191cb5SApple OSS Distributions 		return nullptr;
118*1b191cb5SApple OSS Distributions 	}
119*1b191cb5SApple OSS Distributions 
120*1b191cb5SApple OSS Distributions 	return me;
121*1b191cb5SApple OSS Distributions }
122*1b191cb5SApple OSS Distributions 
123*1b191cb5SApple OSS Distributions void
free()124*1b191cb5SApple OSS Distributions OSOrderedSet::free()
125*1b191cb5SApple OSS Distributions {
126*1b191cb5SApple OSS Distributions 	(void) super::setOptions(0, kImmutable);
127*1b191cb5SApple OSS Distributions 	flushCollection();
128*1b191cb5SApple OSS Distributions 
129*1b191cb5SApple OSS Distributions 	if (array) {
130*1b191cb5SApple OSS Distributions 		kfree_type(_Element, capacity, array);
131*1b191cb5SApple OSS Distributions 		OSCONTAINER_ACCUMSIZE( -(sizeof(_Element) * capacity));
132*1b191cb5SApple OSS Distributions 	}
133*1b191cb5SApple OSS Distributions 
134*1b191cb5SApple OSS Distributions 	super::free();
135*1b191cb5SApple OSS Distributions }
136*1b191cb5SApple OSS Distributions 
137*1b191cb5SApple OSS Distributions unsigned int
getCount() const138*1b191cb5SApple OSS Distributions OSOrderedSet::getCount() const
139*1b191cb5SApple OSS Distributions {
140*1b191cb5SApple OSS Distributions 	return count;
141*1b191cb5SApple OSS Distributions }
142*1b191cb5SApple OSS Distributions unsigned int
getCapacity() const143*1b191cb5SApple OSS Distributions OSOrderedSet::getCapacity() const
144*1b191cb5SApple OSS Distributions {
145*1b191cb5SApple OSS Distributions 	return capacity;
146*1b191cb5SApple OSS Distributions }
147*1b191cb5SApple OSS Distributions unsigned int
getCapacityIncrement() const148*1b191cb5SApple OSS Distributions OSOrderedSet::getCapacityIncrement() const
149*1b191cb5SApple OSS Distributions {
150*1b191cb5SApple OSS Distributions 	return capacityIncrement;
151*1b191cb5SApple OSS Distributions }
152*1b191cb5SApple OSS Distributions unsigned int
setCapacityIncrement(unsigned int increment)153*1b191cb5SApple OSS Distributions OSOrderedSet::setCapacityIncrement(unsigned int increment)
154*1b191cb5SApple OSS Distributions {
155*1b191cb5SApple OSS Distributions 	capacityIncrement = (increment)? increment : 16;
156*1b191cb5SApple OSS Distributions 	return capacityIncrement;
157*1b191cb5SApple OSS Distributions }
158*1b191cb5SApple OSS Distributions 
159*1b191cb5SApple OSS Distributions unsigned int
ensureCapacity(unsigned int newCapacity)160*1b191cb5SApple OSS Distributions OSOrderedSet::ensureCapacity(unsigned int newCapacity)
161*1b191cb5SApple OSS Distributions {
162*1b191cb5SApple OSS Distributions 	_Element *newArray;
163*1b191cb5SApple OSS Distributions 	unsigned int finalCapacity;
164*1b191cb5SApple OSS Distributions 
165*1b191cb5SApple OSS Distributions 	if (newCapacity <= capacity) {
166*1b191cb5SApple OSS Distributions 		return capacity;
167*1b191cb5SApple OSS Distributions 	}
168*1b191cb5SApple OSS Distributions 
169*1b191cb5SApple OSS Distributions 	// round up
170*1b191cb5SApple OSS Distributions 	finalCapacity = (((newCapacity - 1) / capacityIncrement) + 1)
171*1b191cb5SApple OSS Distributions 	    * capacityIncrement;
172*1b191cb5SApple OSS Distributions 	if (finalCapacity < newCapacity) {
173*1b191cb5SApple OSS Distributions 		return capacity;
174*1b191cb5SApple OSS Distributions 	}
175*1b191cb5SApple OSS Distributions 
176*1b191cb5SApple OSS Distributions 	newArray = kreallocp_type_container(_Element, array,
177*1b191cb5SApple OSS Distributions 	    capacity, &finalCapacity, Z_WAITOK_ZERO);
178*1b191cb5SApple OSS Distributions 	if (newArray) {
179*1b191cb5SApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(sizeof(_Element) * (finalCapacity - capacity));
180*1b191cb5SApple OSS Distributions 		array = newArray;
181*1b191cb5SApple OSS Distributions 		capacity = finalCapacity;
182*1b191cb5SApple OSS Distributions 	}
183*1b191cb5SApple OSS Distributions 
184*1b191cb5SApple OSS Distributions 	return capacity;
185*1b191cb5SApple OSS Distributions }
186*1b191cb5SApple OSS Distributions 
187*1b191cb5SApple OSS Distributions void
flushCollection()188*1b191cb5SApple OSS Distributions OSOrderedSet::flushCollection()
189*1b191cb5SApple OSS Distributions {
190*1b191cb5SApple OSS Distributions 	unsigned int i;
191*1b191cb5SApple OSS Distributions 
192*1b191cb5SApple OSS Distributions 	haveUpdated();
193*1b191cb5SApple OSS Distributions 
194*1b191cb5SApple OSS Distributions 	for (i = 0; i < count; i++) {
195*1b191cb5SApple OSS Distributions 		array[i].obj.reset();
196*1b191cb5SApple OSS Distributions 	}
197*1b191cb5SApple OSS Distributions 
198*1b191cb5SApple OSS Distributions 	count = 0;
199*1b191cb5SApple OSS Distributions }
200*1b191cb5SApple OSS Distributions 
201*1b191cb5SApple OSS Distributions /* internal */
202*1b191cb5SApple OSS Distributions bool
setObject(unsigned int index,const OSMetaClassBase * anObject)203*1b191cb5SApple OSS Distributions OSOrderedSet::setObject(unsigned int index, const OSMetaClassBase *anObject)
204*1b191cb5SApple OSS Distributions {
205*1b191cb5SApple OSS Distributions 	unsigned int i;
206*1b191cb5SApple OSS Distributions 	unsigned int newCount = count + 1;
207*1b191cb5SApple OSS Distributions 
208*1b191cb5SApple OSS Distributions 	if ((index > count) || !anObject) {
209*1b191cb5SApple OSS Distributions 		return false;
210*1b191cb5SApple OSS Distributions 	}
211*1b191cb5SApple OSS Distributions 
212*1b191cb5SApple OSS Distributions 	if (containsObject(anObject)) {
213*1b191cb5SApple OSS Distributions 		return false;
214*1b191cb5SApple OSS Distributions 	}
215*1b191cb5SApple OSS Distributions 
216*1b191cb5SApple OSS Distributions 	// do we need more space?
217*1b191cb5SApple OSS Distributions 	if (newCount > capacity && newCount > ensureCapacity(newCount)) {
218*1b191cb5SApple OSS Distributions 		return false;
219*1b191cb5SApple OSS Distributions 	}
220*1b191cb5SApple OSS Distributions 
221*1b191cb5SApple OSS Distributions 	haveUpdated();
222*1b191cb5SApple OSS Distributions 	if (index != count) {
223*1b191cb5SApple OSS Distributions 		for (i = count; i > index; i--) {
224*1b191cb5SApple OSS Distributions 			array[i] = os::move(array[i - 1]);
225*1b191cb5SApple OSS Distributions 		}
226*1b191cb5SApple OSS Distributions 	}
227*1b191cb5SApple OSS Distributions 	array[index].obj.reset(anObject, OSRetain);
228*1b191cb5SApple OSS Distributions 	count++;
229*1b191cb5SApple OSS Distributions 
230*1b191cb5SApple OSS Distributions 	return true;
231*1b191cb5SApple OSS Distributions }
232*1b191cb5SApple OSS Distributions 
233*1b191cb5SApple OSS Distributions bool
setObject(unsigned int index,OSSharedPtr<const OSMetaClassBase> const & anObject)234*1b191cb5SApple OSS Distributions OSOrderedSet::setObject(unsigned int index, OSSharedPtr<const OSMetaClassBase> const& anObject)
235*1b191cb5SApple OSS Distributions {
236*1b191cb5SApple OSS Distributions 	return setObject(index, anObject.get());
237*1b191cb5SApple OSS Distributions }
238*1b191cb5SApple OSS Distributions 
239*1b191cb5SApple OSS Distributions bool
setFirstObject(const OSMetaClassBase * anObject)240*1b191cb5SApple OSS Distributions OSOrderedSet::setFirstObject(const OSMetaClassBase *anObject)
241*1b191cb5SApple OSS Distributions {
242*1b191cb5SApple OSS Distributions 	return setObject(0, anObject);
243*1b191cb5SApple OSS Distributions }
244*1b191cb5SApple OSS Distributions 
245*1b191cb5SApple OSS Distributions bool
setFirstObject(OSSharedPtr<const OSMetaClassBase> const & anObject)246*1b191cb5SApple OSS Distributions OSOrderedSet::setFirstObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
247*1b191cb5SApple OSS Distributions {
248*1b191cb5SApple OSS Distributions 	return setFirstObject(anObject.get());
249*1b191cb5SApple OSS Distributions }
250*1b191cb5SApple OSS Distributions 
251*1b191cb5SApple OSS Distributions bool
setLastObject(const OSMetaClassBase * anObject)252*1b191cb5SApple OSS Distributions OSOrderedSet::setLastObject(const OSMetaClassBase *anObject)
253*1b191cb5SApple OSS Distributions {
254*1b191cb5SApple OSS Distributions 	return setObject( count, anObject);
255*1b191cb5SApple OSS Distributions }
256*1b191cb5SApple OSS Distributions 
257*1b191cb5SApple OSS Distributions bool
setLastObject(OSSharedPtr<const OSMetaClassBase> const & anObject)258*1b191cb5SApple OSS Distributions OSOrderedSet::setLastObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
259*1b191cb5SApple OSS Distributions {
260*1b191cb5SApple OSS Distributions 	return setLastObject(anObject.get());
261*1b191cb5SApple OSS Distributions }
262*1b191cb5SApple OSS Distributions 
263*1b191cb5SApple OSS Distributions 
264*1b191cb5SApple OSS Distributions #define ORDER(obj1, obj2) \
265*1b191cb5SApple OSS Distributions     (ordering ? ((*ordering)( (const OSObject *) obj1, (const OSObject *) obj2, orderingRef)) : 0)
266*1b191cb5SApple OSS Distributions 
267*1b191cb5SApple OSS Distributions bool
setObject(const OSMetaClassBase * anObject)268*1b191cb5SApple OSS Distributions OSOrderedSet::setObject(const OSMetaClassBase *anObject )
269*1b191cb5SApple OSS Distributions {
270*1b191cb5SApple OSS Distributions 	unsigned int i;
271*1b191cb5SApple OSS Distributions 
272*1b191cb5SApple OSS Distributions 	// queue it behind those with same priority
273*1b191cb5SApple OSS Distributions 	for (i = 0;
274*1b191cb5SApple OSS Distributions 	    (i < count) && (ORDER(array[i].obj.get(), anObject) >= 0);
275*1b191cb5SApple OSS Distributions 	    i++) {
276*1b191cb5SApple OSS Distributions 	}
277*1b191cb5SApple OSS Distributions 
278*1b191cb5SApple OSS Distributions 	return setObject(i, anObject);
279*1b191cb5SApple OSS Distributions }
280*1b191cb5SApple OSS Distributions 
281*1b191cb5SApple OSS Distributions bool
setObject(OSSharedPtr<const OSMetaClassBase> const & anObject)282*1b191cb5SApple OSS Distributions OSOrderedSet::setObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
283*1b191cb5SApple OSS Distributions {
284*1b191cb5SApple OSS Distributions 	return setObject(anObject.get());
285*1b191cb5SApple OSS Distributions }
286*1b191cb5SApple OSS Distributions 
287*1b191cb5SApple OSS Distributions void
removeObject(const OSMetaClassBase * anObject)288*1b191cb5SApple OSS Distributions OSOrderedSet::removeObject(const OSMetaClassBase *anObject)
289*1b191cb5SApple OSS Distributions {
290*1b191cb5SApple OSS Distributions 	bool                deleted = false;
291*1b191cb5SApple OSS Distributions 	unsigned int        i;
292*1b191cb5SApple OSS Distributions 
293*1b191cb5SApple OSS Distributions 	for (i = 0; i < count; i++) {
294*1b191cb5SApple OSS Distributions 		if (deleted) {
295*1b191cb5SApple OSS Distributions 			array[i - 1] = os::move(array[i]);
296*1b191cb5SApple OSS Distributions 		} else if (array[i].obj == anObject) {
297*1b191cb5SApple OSS Distributions 			deleted = true;
298*1b191cb5SApple OSS Distributions 			haveUpdated(); // Pity we can't flush the log
299*1b191cb5SApple OSS Distributions 			array[i].obj.reset();
300*1b191cb5SApple OSS Distributions 		}
301*1b191cb5SApple OSS Distributions 	}
302*1b191cb5SApple OSS Distributions 
303*1b191cb5SApple OSS Distributions 	if (deleted) {
304*1b191cb5SApple OSS Distributions 		count--;
305*1b191cb5SApple OSS Distributions 	}
306*1b191cb5SApple OSS Distributions }
307*1b191cb5SApple OSS Distributions 
308*1b191cb5SApple OSS Distributions void
removeObject(OSSharedPtr<const OSMetaClassBase> const & anObject)309*1b191cb5SApple OSS Distributions OSOrderedSet::removeObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
310*1b191cb5SApple OSS Distributions {
311*1b191cb5SApple OSS Distributions 	return removeObject(anObject.get());
312*1b191cb5SApple OSS Distributions }
313*1b191cb5SApple OSS Distributions 
314*1b191cb5SApple OSS Distributions bool
containsObject(const OSMetaClassBase * anObject) const315*1b191cb5SApple OSS Distributions OSOrderedSet::containsObject(const OSMetaClassBase *anObject) const
316*1b191cb5SApple OSS Distributions {
317*1b191cb5SApple OSS Distributions 	return anObject && member(anObject);
318*1b191cb5SApple OSS Distributions }
319*1b191cb5SApple OSS Distributions 
320*1b191cb5SApple OSS Distributions bool
member(const OSMetaClassBase * anObject) const321*1b191cb5SApple OSS Distributions OSOrderedSet::member(const OSMetaClassBase *anObject) const
322*1b191cb5SApple OSS Distributions {
323*1b191cb5SApple OSS Distributions 	unsigned int i;
324*1b191cb5SApple OSS Distributions 
325*1b191cb5SApple OSS Distributions 	for (i = 0;
326*1b191cb5SApple OSS Distributions 	    (i < count) && (array[i].obj != anObject);
327*1b191cb5SApple OSS Distributions 	    i++) {
328*1b191cb5SApple OSS Distributions 	}
329*1b191cb5SApple OSS Distributions 
330*1b191cb5SApple OSS Distributions 	return i < count;
331*1b191cb5SApple OSS Distributions }
332*1b191cb5SApple OSS Distributions 
333*1b191cb5SApple OSS Distributions /* internal */
334*1b191cb5SApple OSS Distributions OSObject *
getObject(unsigned int index) const335*1b191cb5SApple OSS Distributions OSOrderedSet::getObject( unsigned int index ) const
336*1b191cb5SApple OSS Distributions {
337*1b191cb5SApple OSS Distributions 	if (index >= count) {
338*1b191cb5SApple OSS Distributions 		return NULL;
339*1b191cb5SApple OSS Distributions 	}
340*1b191cb5SApple OSS Distributions 
341*1b191cb5SApple OSS Distributions 	return const_cast<OSObject *>((const OSObject *) array[index].obj.get());
342*1b191cb5SApple OSS Distributions }
343*1b191cb5SApple OSS Distributions 
344*1b191cb5SApple OSS Distributions OSObject *
getFirstObject() const345*1b191cb5SApple OSS Distributions OSOrderedSet::getFirstObject() const
346*1b191cb5SApple OSS Distributions {
347*1b191cb5SApple OSS Distributions 	if (count) {
348*1b191cb5SApple OSS Distributions 		return const_cast<OSObject *>((const OSObject *) array[0].obj.get());
349*1b191cb5SApple OSS Distributions 	} else {
350*1b191cb5SApple OSS Distributions 		return NULL;
351*1b191cb5SApple OSS Distributions 	}
352*1b191cb5SApple OSS Distributions }
353*1b191cb5SApple OSS Distributions 
354*1b191cb5SApple OSS Distributions OSObject *
getLastObject() const355*1b191cb5SApple OSS Distributions OSOrderedSet::getLastObject() const
356*1b191cb5SApple OSS Distributions {
357*1b191cb5SApple OSS Distributions 	if (count) {
358*1b191cb5SApple OSS Distributions 		return const_cast<OSObject *>((const OSObject *) array[count - 1].obj.get());
359*1b191cb5SApple OSS Distributions 	} else {
360*1b191cb5SApple OSS Distributions 		return NULL;
361*1b191cb5SApple OSS Distributions 	}
362*1b191cb5SApple OSS Distributions }
363*1b191cb5SApple OSS Distributions 
364*1b191cb5SApple OSS Distributions SInt32
orderObject(const OSMetaClassBase * anObject)365*1b191cb5SApple OSS Distributions OSOrderedSet::orderObject( const OSMetaClassBase * anObject )
366*1b191cb5SApple OSS Distributions {
367*1b191cb5SApple OSS Distributions 	return ORDER( anObject, NULL );
368*1b191cb5SApple OSS Distributions }
369*1b191cb5SApple OSS Distributions 
370*1b191cb5SApple OSS Distributions void *
getOrderingRef()371*1b191cb5SApple OSS Distributions OSOrderedSet::getOrderingRef()
372*1b191cb5SApple OSS Distributions {
373*1b191cb5SApple OSS Distributions 	return orderingRef;
374*1b191cb5SApple OSS Distributions }
375*1b191cb5SApple OSS Distributions 
376*1b191cb5SApple OSS Distributions bool
isEqualTo(const OSOrderedSet * anOrderedSet) const377*1b191cb5SApple OSS Distributions OSOrderedSet::isEqualTo(const OSOrderedSet *anOrderedSet) const
378*1b191cb5SApple OSS Distributions {
379*1b191cb5SApple OSS Distributions 	unsigned int i;
380*1b191cb5SApple OSS Distributions 
381*1b191cb5SApple OSS Distributions 	if (this == anOrderedSet) {
382*1b191cb5SApple OSS Distributions 		return true;
383*1b191cb5SApple OSS Distributions 	}
384*1b191cb5SApple OSS Distributions 
385*1b191cb5SApple OSS Distributions 	if (count != anOrderedSet->getCount()) {
386*1b191cb5SApple OSS Distributions 		return false;
387*1b191cb5SApple OSS Distributions 	}
388*1b191cb5SApple OSS Distributions 
389*1b191cb5SApple OSS Distributions 	for (i = 0; i < count; i++) {
390*1b191cb5SApple OSS Distributions 		if (!array[i].obj->isEqualTo(anOrderedSet->getObject(i))) {
391*1b191cb5SApple OSS Distributions 			return false;
392*1b191cb5SApple OSS Distributions 		}
393*1b191cb5SApple OSS Distributions 	}
394*1b191cb5SApple OSS Distributions 
395*1b191cb5SApple OSS Distributions 	return true;
396*1b191cb5SApple OSS Distributions }
397*1b191cb5SApple OSS Distributions 
398*1b191cb5SApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * anObject) const399*1b191cb5SApple OSS Distributions OSOrderedSet::isEqualTo(const OSMetaClassBase *anObject) const
400*1b191cb5SApple OSS Distributions {
401*1b191cb5SApple OSS Distributions 	OSOrderedSet *oSet;
402*1b191cb5SApple OSS Distributions 
403*1b191cb5SApple OSS Distributions 	oSet = OSDynamicCast(OSOrderedSet, anObject);
404*1b191cb5SApple OSS Distributions 	if (oSet) {
405*1b191cb5SApple OSS Distributions 		return isEqualTo(oSet);
406*1b191cb5SApple OSS Distributions 	} else {
407*1b191cb5SApple OSS Distributions 		return false;
408*1b191cb5SApple OSS Distributions 	}
409*1b191cb5SApple OSS Distributions }
410*1b191cb5SApple OSS Distributions 
411*1b191cb5SApple OSS Distributions unsigned int
iteratorSize() const412*1b191cb5SApple OSS Distributions OSOrderedSet::iteratorSize() const
413*1b191cb5SApple OSS Distributions {
414*1b191cb5SApple OSS Distributions 	return sizeof(unsigned int);
415*1b191cb5SApple OSS Distributions }
416*1b191cb5SApple OSS Distributions 
417*1b191cb5SApple OSS Distributions bool
initIterator(void * inIterator) const418*1b191cb5SApple OSS Distributions OSOrderedSet::initIterator(void *inIterator) const
419*1b191cb5SApple OSS Distributions {
420*1b191cb5SApple OSS Distributions 	unsigned int *iteratorP = (unsigned int *) inIterator;
421*1b191cb5SApple OSS Distributions 
422*1b191cb5SApple OSS Distributions 	*iteratorP = 0;
423*1b191cb5SApple OSS Distributions 	return true;
424*1b191cb5SApple OSS Distributions }
425*1b191cb5SApple OSS Distributions 
426*1b191cb5SApple OSS Distributions bool
427*1b191cb5SApple OSS Distributions OSOrderedSet::
getNextObjectForIterator(void * inIterator,OSObject ** ret) const428*1b191cb5SApple OSS Distributions getNextObjectForIterator(void *inIterator, OSObject **ret) const
429*1b191cb5SApple OSS Distributions {
430*1b191cb5SApple OSS Distributions 	unsigned int *iteratorP = (unsigned int *) inIterator;
431*1b191cb5SApple OSS Distributions 	unsigned int index = (*iteratorP)++;
432*1b191cb5SApple OSS Distributions 
433*1b191cb5SApple OSS Distributions 	if (index < count) {
434*1b191cb5SApple OSS Distributions 		*ret = const_cast<OSObject *>((const OSObject *) array[index].obj.get());
435*1b191cb5SApple OSS Distributions 	} else {
436*1b191cb5SApple OSS Distributions 		*ret = NULL;
437*1b191cb5SApple OSS Distributions 	}
438*1b191cb5SApple OSS Distributions 
439*1b191cb5SApple OSS Distributions 	return *ret != NULL;
440*1b191cb5SApple OSS Distributions }
441*1b191cb5SApple OSS Distributions 
442*1b191cb5SApple OSS Distributions 
443*1b191cb5SApple OSS Distributions unsigned
setOptions(unsigned options,unsigned mask,void *)444*1b191cb5SApple OSS Distributions OSOrderedSet::setOptions(unsigned options, unsigned mask, void *)
445*1b191cb5SApple OSS Distributions {
446*1b191cb5SApple OSS Distributions 	unsigned old = super::setOptions(options, mask);
447*1b191cb5SApple OSS Distributions 	if ((old ^ options) & mask) {
448*1b191cb5SApple OSS Distributions 		// Value changed need to recurse over all of the child collections
449*1b191cb5SApple OSS Distributions 		for (unsigned i = 0; i < count; i++) {
450*1b191cb5SApple OSS Distributions 			OSCollection *coll = OSDynamicCast(OSCollection, array[i].obj.get());
451*1b191cb5SApple OSS Distributions 			if (coll) {
452*1b191cb5SApple OSS Distributions 				coll->setOptions(options, mask);
453*1b191cb5SApple OSS Distributions 			}
454*1b191cb5SApple OSS Distributions 		}
455*1b191cb5SApple OSS Distributions 	}
456*1b191cb5SApple OSS Distributions 
457*1b191cb5SApple OSS Distributions 	return old;
458*1b191cb5SApple OSS Distributions }
459*1b191cb5SApple OSS Distributions 
460*1b191cb5SApple OSS Distributions OSSharedPtr<OSCollection>
copyCollection(OSDictionary * cycleDict)461*1b191cb5SApple OSS Distributions OSOrderedSet::copyCollection(OSDictionary *cycleDict)
462*1b191cb5SApple OSS Distributions {
463*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSDictionary> ourCycleDict;
464*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSCollection> ret;
465*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSOrderedSet> newSet;
466*1b191cb5SApple OSS Distributions 
467*1b191cb5SApple OSS Distributions 	if (!cycleDict) {
468*1b191cb5SApple OSS Distributions 		ourCycleDict = OSDictionary::withCapacity(16);
469*1b191cb5SApple OSS Distributions 		if (!ourCycleDict) {
470*1b191cb5SApple OSS Distributions 			return nullptr;
471*1b191cb5SApple OSS Distributions 		}
472*1b191cb5SApple OSS Distributions 		cycleDict = ourCycleDict.get();
473*1b191cb5SApple OSS Distributions 	}
474*1b191cb5SApple OSS Distributions 
475*1b191cb5SApple OSS Distributions 	do {
476*1b191cb5SApple OSS Distributions 		// Check for a cycle
477*1b191cb5SApple OSS Distributions 		ret = super::copyCollection(cycleDict);
478*1b191cb5SApple OSS Distributions 		if (ret) {
479*1b191cb5SApple OSS Distributions 			continue;
480*1b191cb5SApple OSS Distributions 		}
481*1b191cb5SApple OSS Distributions 
482*1b191cb5SApple OSS Distributions 		// Duplicate the set with no contents
483*1b191cb5SApple OSS Distributions 		newSet = OSOrderedSet::withCapacity(capacity, ordering, orderingRef);
484*1b191cb5SApple OSS Distributions 		if (!newSet) {
485*1b191cb5SApple OSS Distributions 			continue;
486*1b191cb5SApple OSS Distributions 		}
487*1b191cb5SApple OSS Distributions 
488*1b191cb5SApple OSS Distributions 		// Insert object into cycle Dictionary
489*1b191cb5SApple OSS Distributions 		cycleDict->setObject((const OSSymbol *) this, newSet.get());
490*1b191cb5SApple OSS Distributions 
491*1b191cb5SApple OSS Distributions 		newSet->capacityIncrement = capacityIncrement;
492*1b191cb5SApple OSS Distributions 
493*1b191cb5SApple OSS Distributions 		// Now copy over the contents to the new duplicate
494*1b191cb5SApple OSS Distributions 		for (unsigned int i = 0; i < count; i++) {
495*1b191cb5SApple OSS Distributions 			OSObject *obj = EXT_CAST(array[i].obj.get());
496*1b191cb5SApple OSS Distributions 			OSCollection *coll = OSDynamicCast(OSCollection, obj);
497*1b191cb5SApple OSS Distributions 			if (coll) {
498*1b191cb5SApple OSS Distributions 				OSSharedPtr<OSCollection> newColl = coll->copyCollection(cycleDict);
499*1b191cb5SApple OSS Distributions 				if (newColl) {
500*1b191cb5SApple OSS Distributions 					obj = newColl.get(); // Rely on cycleDict ref for a bit
501*1b191cb5SApple OSS Distributions 				} else {
502*1b191cb5SApple OSS Distributions 					return ret;
503*1b191cb5SApple OSS Distributions 				}
504*1b191cb5SApple OSS Distributions 			}
505*1b191cb5SApple OSS Distributions 
506*1b191cb5SApple OSS Distributions 			newSet->setLastObject(obj);
507*1b191cb5SApple OSS Distributions 		}
508*1b191cb5SApple OSS Distributions 
509*1b191cb5SApple OSS Distributions 		ret = os::move(newSet);
510*1b191cb5SApple OSS Distributions 	} while (false);
511*1b191cb5SApple OSS Distributions 
512*1b191cb5SApple OSS Distributions 	return ret;
513*1b191cb5SApple OSS Distributions }
514