2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <libkern/c++/OSDictionary.h>
30 #include <libkern/c++/OSOrderedSet.h>
31 #include <libkern/c++/OSLib.h>
33 #define super OSCollection
35 OSDefineMetaClassAndStructors(OSOrderedSet
, OSCollection
)
36 OSMetaClassDefineReservedUnused(OSOrderedSet
, 0);
37 OSMetaClassDefineReservedUnused(OSOrderedSet
, 1);
38 OSMetaClassDefineReservedUnused(OSOrderedSet
, 2);
39 OSMetaClassDefineReservedUnused(OSOrderedSet
, 3);
40 OSMetaClassDefineReservedUnused(OSOrderedSet
, 4);
41 OSMetaClassDefineReservedUnused(OSOrderedSet
, 5);
42 OSMetaClassDefineReservedUnused(OSOrderedSet
, 6);
43 OSMetaClassDefineReservedUnused(OSOrderedSet
, 7);
47 const OSMetaClassBase
* obj
;
51 #define EXT_CAST(obj) \
52 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
55 initWithCapacity(unsigned int inCapacity
,
56 OSOrderFunction inOrdering
, void *inOrderingRef
)
63 if (inCapacity
> (UINT_MAX
/ sizeof(_Element
)))
66 size
= sizeof(_Element
) * inCapacity
;
67 array
= (_Element
*) kalloc_container(size
);
72 capacity
= inCapacity
;
73 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
74 ordering
= inOrdering
;
75 orderingRef
= inOrderingRef
;
78 OSCONTAINER_ACCUMSIZE(size
);
83 OSOrderedSet
* OSOrderedSet::
84 withCapacity(unsigned int capacity
,
85 OSOrderFunction ordering
, void * orderingRef
)
87 OSOrderedSet
*me
= new OSOrderedSet
;
89 if (me
&& !me
->initWithCapacity(capacity
, ordering
, orderingRef
)) {
97 void OSOrderedSet::free()
99 (void) super::setOptions(0, kImmutable
);
103 kfree(array
, sizeof(_Element
) * capacity
);
104 OSCONTAINER_ACCUMSIZE( -(sizeof(_Element
) * capacity
) );
110 unsigned int OSOrderedSet::getCount() const { return count
; }
111 unsigned int OSOrderedSet::getCapacity() const { return capacity
; }
112 unsigned int OSOrderedSet::getCapacityIncrement() const
113 { return capacityIncrement
; }
114 unsigned int OSOrderedSet::setCapacityIncrement(unsigned int increment
)
116 capacityIncrement
= (increment
)? increment
: 16;
117 return capacityIncrement
;
120 unsigned int OSOrderedSet::ensureCapacity(unsigned int newCapacity
)
123 unsigned int finalCapacity
, oldSize
, newSize
;
125 if (newCapacity
<= capacity
)
129 finalCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
131 if ((finalCapacity
< newCapacity
) ||
132 (finalCapacity
> (UINT_MAX
/ sizeof(_Element
)))) {
135 newSize
= sizeof(_Element
) * finalCapacity
;
137 newArray
= (_Element
*) kalloc_container(newSize
);
139 oldSize
= sizeof(_Element
) * capacity
;
141 OSCONTAINER_ACCUMSIZE(((size_t)newSize
) - ((size_t)oldSize
));
143 bcopy(array
, newArray
, oldSize
);
144 bzero(&newArray
[capacity
], newSize
- oldSize
);
145 kfree(array
, oldSize
);
147 capacity
= finalCapacity
;
153 void OSOrderedSet::flushCollection()
159 for (i
= 0; i
< count
; i
++)
160 array
[i
].obj
->taggedRelease(OSTypeID(OSCollection
));
166 bool OSOrderedSet::setObject(unsigned int index
, const OSMetaClassBase
*anObject
)
169 unsigned int newCount
= count
+ 1;
171 if ((index
> count
) || !anObject
)
174 if (containsObject(anObject
))
177 // do we need more space?
178 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
182 if (index
!= count
) {
183 for (i
= count
; i
> index
; i
--)
184 array
[i
] = array
[i
-1];
186 array
[index
].obj
= anObject
;
187 // array[index].pri = pri;
188 anObject
->taggedRetain(OSTypeID(OSCollection
));
195 bool OSOrderedSet::setFirstObject(const OSMetaClassBase
*anObject
)
197 return( setObject(0, anObject
));
200 bool OSOrderedSet::setLastObject(const OSMetaClassBase
*anObject
)
202 return( setObject( count
, anObject
));
206 #define ORDER(obj1,obj2) \
207 (ordering ? ((*ordering)( (const OSObject *) obj1, (const OSObject *) obj2, orderingRef)) : 0)
209 bool OSOrderedSet::setObject(const OSMetaClassBase
*anObject
)
213 // queue it behind those with same priority
215 (i
< count
) && (ORDER(array
[i
].obj
, anObject
) >= 0);
218 return( setObject(i
, anObject
));
221 void OSOrderedSet::removeObject(const OSMetaClassBase
*anObject
)
223 bool deleted
= false;
226 for (i
= 0; i
< count
; i
++) {
229 array
[i
-1] = array
[i
];
230 else if (array
[i
].obj
== anObject
) {
232 haveUpdated(); // Pity we can't flush the log
233 array
[i
].obj
->taggedRelease(OSTypeID(OSCollection
));
241 bool OSOrderedSet::containsObject(const OSMetaClassBase
*anObject
) const
243 return anObject
&& member(anObject
);
246 bool OSOrderedSet::member(const OSMetaClassBase
*anObject
) const
251 (i
< count
) && (array
[i
].obj
!= anObject
);
258 OSObject
*OSOrderedSet::getObject( unsigned int index
) const
264 // *pri = array[index].pri;
266 return( const_cast<OSObject
*>((const OSObject
*) array
[index
].obj
) );
269 OSObject
*OSOrderedSet::getFirstObject() const
272 return( const_cast<OSObject
*>((const OSObject
*) array
[0].obj
) );
277 OSObject
*OSOrderedSet::getLastObject() const
280 return( const_cast<OSObject
*>((const OSObject
*) array
[count
-1].obj
) );
285 SInt32
OSOrderedSet::orderObject( const OSMetaClassBase
* anObject
)
287 return( ORDER( anObject
, 0 ));
290 void *OSOrderedSet::getOrderingRef()
295 bool OSOrderedSet::isEqualTo(const OSOrderedSet
*anOrderedSet
) const
299 if ( this == anOrderedSet
)
302 if ( count
!= anOrderedSet
->getCount() )
305 for ( i
= 0; i
< count
; i
++ ) {
306 if ( !array
[i
].obj
->isEqualTo(anOrderedSet
->getObject(i
)) )
313 bool OSOrderedSet::isEqualTo(const OSMetaClassBase
*anObject
) const
317 oSet
= OSDynamicCast(OSOrderedSet
, anObject
);
319 return isEqualTo(oSet
);
324 unsigned int OSOrderedSet::iteratorSize() const
326 return( sizeof(unsigned int));
329 bool OSOrderedSet::initIterator(void *inIterator
) const
331 unsigned int *iteratorP
= (unsigned int *) inIterator
;
338 getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
340 unsigned int *iteratorP
= (unsigned int *) inIterator
;
341 unsigned int index
= (*iteratorP
)++;
344 *ret
= const_cast<OSObject
*>((const OSObject
*) array
[index
].obj
);
352 unsigned OSOrderedSet::setOptions(unsigned options
, unsigned mask
, void *)
354 unsigned old
= super::setOptions(options
, mask
);
355 if ((old
^ options
) & mask
) {
357 // Value changed need to recurse over all of the child collections
358 for ( unsigned i
= 0; i
< count
; i
++ ) {
359 OSCollection
*coll
= OSDynamicCast(OSCollection
, array
[i
].obj
);
361 coll
->setOptions(options
, mask
);
368 OSCollection
* OSOrderedSet::copyCollection(OSDictionary
*cycleDict
)
370 bool allocDict
= !cycleDict
;
371 OSCollection
*ret
= 0;
372 OSOrderedSet
*newSet
= 0;
375 cycleDict
= OSDictionary::withCapacity(16);
382 ret
= super::copyCollection(cycleDict
);
386 // Duplicate the set with no contents
387 newSet
= OSOrderedSet::withCapacity(capacity
, ordering
, orderingRef
);
391 // Insert object into cycle Dictionary
392 cycleDict
->setObject((const OSSymbol
*) this, newSet
);
394 newSet
->capacityIncrement
= capacityIncrement
;
396 // Now copy over the contents to the new duplicate
397 for (unsigned int i
= 0; i
< count
; i
++) {
398 OSObject
*obj
= EXT_CAST(array
[i
].obj
);
399 OSCollection
*coll
= OSDynamicCast(OSCollection
, obj
);
401 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
403 obj
= newColl
; // Rely on cycleDict ref for a bit
409 newSet
->setLastObject(obj
);
422 cycleDict
->release();