2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #ifndef _OS_OSORDEREDSET_H
32 #define _OS_OSORDEREDSET_H
34 #include <libkern/c++/OSCollection.h>
35 #include <libkern/OSTypes.h>
41 @abstract A collection class for maintaining and sorting a set of OSMetaClassBase derived objects.
43 An instance of OSOrderedSet maintains and sorts a collection of OSMetaClassBase derived objects. The sorting algorithm is supplied to the instance via the OSOrderFunction.
45 class OSOrderedSet
: public OSCollection
47 OSDeclareDefaultStructors(OSOrderedSet
)
51 @typedef OSOrderFunction
52 @abstract The sorting function used by the collection to order objects.
53 @param obj1 An object from the collection.
54 @param obj2 An object to be compared to obj1.
55 @param ref The ordering context used by the sorting function as a hint for sorting.
56 @result Returns a comparison result of the object, a negative value if obj1 < obj2, 0 if obj1 == obj2, and a positive value if obj1 > obj2.
58 typedef SInt32 (*OSOrderFunction
)(const OSMetaClassBase
* obj1
,
59 const OSMetaClassBase
* obj2
,
63 struct _Element
* array
;
64 OSOrderFunction ordering
;
67 unsigned int capacity
;
68 unsigned int capacityIncrement
;
70 struct ExpansionData
{ };
73 Reserved for future use. (Internal use only) */
74 ExpansionData
*reserved
;
78 * OSCollectionIterator interfaces.
80 virtual unsigned int iteratorSize() const;
81 virtual bool initIterator(void *iterator
) const;
82 virtual bool getNextObjectForIterator(void *iterator
, OSObject
**ret
) const;
87 @function withCapacity
88 @abstract A static constructor function for creating and initializing an instance of OSOrderedSet.
89 @param capacity The initial storage size in number of objects of the set.
90 @param orderFunc A c-style function which implements the sorting algorithm for the set.
91 @param orderingRef A ordering context used as a hint for ordering objects within the set.
92 @result Returns an instance of OSSet, or 0 if a failure occurred.
94 static OSOrderedSet
*withCapacity(unsigned int capacity
,
95 OSOrderFunction orderFunc
= 0,
96 void * orderingRef
= 0);
99 @function initWithCapacity
100 @abstract A member function for initializing an instance of OSOrderedSet.
101 @param capacity The initial storage size in number of objects of the set.
102 @param orderFunc A c-style function which implements the sorting algorithm for the set.
103 @param orderingRef A ordering context used as a hint for ordering objects within the set.
104 @result Returns true if initialization was successful, or false if a failure occurred.
106 virtual bool initWithCapacity(unsigned int capacity
,
107 OSOrderFunction orderFunc
= 0,
108 void * orderingRef
= 0);
111 @abstract A member function to release and deallocate any resources used by the instance of OSOrderedSet.
117 @abstract A member function to return the number of objects within the collection.
118 @result Returns the number of items in the set.
120 virtual unsigned int getCount() const;
122 @function getCapacity
123 @abstract A member function to return the storage capacity of the collection.
124 @result Returns the total storage capacity of the set.
126 virtual unsigned int getCapacity() const;
128 @function getCapacityIncrement
129 @abstract A member function to get the size by which the collection will grow.
130 @result Returns the current growth size.
132 virtual unsigned int getCapacityIncrement() const;
134 @function setCapacityIncrement
135 @abstract A member function to set the size by which the collection will grow.
136 @param increment The new growth factor for the set.
137 @result Returns new growth size.
139 virtual unsigned int setCapacityIncrement(unsigned increment
);
142 @function ensureCapacity
143 @abstract A member function to expand the size of the collection.
144 @param newCapacity The new size capacity for the collection.
145 @result Returns new capacity of the set when successful or the old capacity on failure.
147 virtual unsigned int ensureCapacity(unsigned int newCapacity
);
150 @function flushCollection
151 @abstract A member function to remove and release all items in the set.
153 virtual void flushCollection();
157 @abstract A member function to place an OSMetaClassBase derived object into the set. The object will be automatically sorted in the set.
158 @param anObject The object to be placed into the collection.
159 @result Returns true if object was successfully added to the collection, false otherwise.
161 virtual bool setObject(const OSMetaClassBase
*anObject
);
163 @function setFirstObject
164 @abstract A member function to place an OSMetaClassBase derived object order it first in the set.
165 @param anObject The object to be placed into the collection.
166 @result Returns true if object was successfully added to the collection, false otherwise.
168 virtual bool setFirstObject(const OSMetaClassBase
*anObject
);
170 @function setLastObject
171 @abstract A member function to place an OSMetaClassBase derived object order it last in the set.
172 @param anObject The object to be placed into the collection.
173 @result Returns true if object was successfully added to the collection, false otherwise.
175 virtual bool setLastObject(const OSMetaClassBase
*anObject
);
178 @function removeObject
179 @abstract A member function to remove and release an object in the set.
180 @param anObject The object to remove from the set.
182 virtual void removeObject(const OSMetaClassBase
*anObject
);
185 @function containsObject
186 @abstract A member function to query the set for the presence of a particular object.
187 @param anObject The object to be located.
188 @result Returns true if the object is present in the set, false otherwise.
190 virtual bool containsObject(const OSMetaClassBase
*anObject
) const;
193 @abstract A member function to query the set for the presence of a particular object.
194 @param anObject The object to be located.
195 @result Returns true if the object is present in the set, false otherwise.
197 virtual bool member(const OSMetaClassBase
*anObject
) const;
200 @function getFirstObject
201 @abstract A member function to return the first object in the set.
202 @result Returns the object ordered first in the set or 0 if none exist.
204 virtual OSObject
*getFirstObject() const;
206 @function getLastObject
207 @abstract A member function to return the last object in the set.
208 @result Returns the object ordered last in the set or 0 if none exist.
210 virtual OSObject
*getLastObject() const;
213 @function orderObject
214 @abstract A member function to return the ordering value of an object.
215 @param anObject The object to be queried.
216 @result Returns the ordering value for an object.
218 virtual SInt32
orderObject( const OSMetaClassBase
* anObject
);
222 @abstract A member function to place an object into the set at a particular index.
223 @param index The index in the set to place the object.
224 @param anObject The object to be placed into the set.
225 @result Returns true if the object was successfully placed into the collection, false otherwise.
227 virtual bool setObject(unsigned int index
, const OSMetaClassBase
*anObject
);
230 @abstract A member function to return a reference to an object at a particular index.
231 @param index The index into the set.
232 @result Returns a reference to the object at the given index, 0 if none exist at that location.
234 virtual OSObject
*getObject( unsigned int index
) const;
236 @function getOrderingRef
237 @abstract A member function to return a the ordering context.
238 @result Returns the ordering context, or NULL if none exist.
240 virtual void *getOrderingRef();
244 @abstract A member function to test the equality between an OSOrderedSet object and the receiver.
245 @param anOrderedSet The OSOrderedSet object to be compared against the receiver.
246 @result Returns true if the two objects are equivalent, false otherwise.
248 virtual bool isEqualTo(const OSOrderedSet
*anOrderedSet
) const;
251 @abstract A member function to test the equality between an arbitrary OSMetaClassBase derived object and the receiver.
252 @param anObject The OSMetaClassBase derived object to be compared against the receiver.
253 @result Returns true if the two objects are equivalent, false otherwise.
255 virtual bool isEqualTo(const OSMetaClassBase
*anObject
) const;
260 @abstract This function is used to recursively set option bits in this set and all child collections.
261 @param options Set the (options & mask) bits.
262 @param mask The mask of bits which need to be set, 0 to get the current value.
263 @result The options before the set operation, NB setOptions(?,0) returns the current value of this collection.
265 virtual unsigned setOptions(unsigned options
, unsigned mask
, void * = 0);
268 @function copyCollection
269 @abstract Do a deep copy of this ordered set and its collections.
270 @discussion This function copies this set and all included collections recursively. Objects that don't derive from OSContainter are NOT copied, that is objects like OSString and OSData.
271 @param cycleDict Is a dictionary of all of the collections that have been, to start the copy at the top level just leave this field 0.
272 @result The newly copied collecton or 0 if insufficient memory
274 OSCollection
*copyCollection(OSDictionary
*cycleDict
= 0);
276 OSMetaClassDeclareReservedUnused(OSOrderedSet
, 0);
277 OSMetaClassDeclareReservedUnused(OSOrderedSet
, 1);
278 OSMetaClassDeclareReservedUnused(OSOrderedSet
, 2);
279 OSMetaClassDeclareReservedUnused(OSOrderedSet
, 3);
280 OSMetaClassDeclareReservedUnused(OSOrderedSet
, 4);
281 OSMetaClassDeclareReservedUnused(OSOrderedSet
, 5);
282 OSMetaClassDeclareReservedUnused(OSOrderedSet
, 6);
283 OSMetaClassDeclareReservedUnused(OSOrderedSet
, 7);
286 #endif /* ! _OS_OSORDEREDSET_H */