|
|
1.1 root 1: /*
2: * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * The contents of this file constitute Original Code as defined in and
7: * are subject to the Apple Public Source License Version 1.1 (the
8: * "License"). You may not use this file except in compliance with the
9: * License. Please obtain a copy of the License at
10: * http://www.apple.com/publicsource and read it before using this file.
11: *
12: * This Original Code and all software distributed under the License are
13: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17: * License for the specific language governing rights and limitations
18: * under the License.
19: *
20: * @APPLE_LICENSE_HEADER_END@
21: */
22: /* IOSet.m created by rsulack on Thu 11-Jun-1998 */
23:
24: #include <libkern/c++/OSSet.h>
25: #include <libkern/c++/OSArray.h>
26: #include <libkern/c++/OSSerialize.h>
27:
28: #define super OSCollection
29:
30: OSDefineMetaClassAndStructors(OSSet, OSCollection)
31:
32: bool OSSet::initWithCapacity(unsigned int inCapacity)
33: {
34: if ( !super::init() )
35: return false;
36:
37: members = OSArray::withCapacity(inCapacity);
38: if (!members)
39: return false;
40:
41: return true;
42: }
43:
44: bool OSSet::initWithObjects(OSObject *inObjects[],
45: unsigned int inCount,
46: unsigned int inCapacity = 0)
47: {
48: unsigned int capacity = inCount;
49:
50: if ( inCapacity ) {
51: if ( inCount > inCapacity )
52: return false;
53:
54: capacity = inCapacity;
55: }
56:
57: if (!inObjects || !initWithCapacity(capacity))
58: return false;
59:
60: for ( unsigned int i = 0; i < inCount; i++ ) {
61: if (members->getCount() < inCapacity)
62: setObject(inObjects[i]);
63: else
64: return false;
65: }
66:
67: return true;
68: }
69:
70: bool OSSet::initWithArray(const OSArray *inArray,
71: unsigned int inCapacity = 0)
72: {
73: if ( !inArray )
74: return false;
75:
76: return initWithObjects(inArray->array, inArray->count,
77: inCapacity);
78: }
79:
80: bool OSSet::initWithSet(const OSSet *inSet,
81: unsigned int inCapacity = 0)
82: {
83: return initWithArray(inSet->members, inCapacity);
84: }
85:
86: OSSet *OSSet::withCapacity(unsigned int capacity)
87: {
88: OSSet *me = new OSSet;
89:
90: if (me && !me->initWithCapacity(capacity)) {
91: me->free();
92: return 0;
93: }
94:
95: return me;
96: }
97:
98: OSSet *OSSet::withObjects(OSObject *objects[],
99: unsigned int count,
100: unsigned int capacity = 0)
101: {
102: OSSet *me = new OSSet;
103:
104: if (me && !me->initWithObjects(objects, count, capacity)) {
105: me->free();
106: return 0;
107: }
108:
109: return me;
110: }
111:
112: OSSet *OSSet::withArray(const OSArray *array,
113: unsigned int capacity = 0)
114: {
115: OSSet *me = new OSSet;
116:
117: if (me && !me->initWithArray(array, capacity)) {
118: me->free();
119: return 0;
120: }
121:
122: return me;
123: }
124:
125: OSSet *OSSet::withSet(const OSSet *set,
126: unsigned int capacity = 0)
127: {
128: OSSet *me = new OSSet;
129:
130: if (me && !me->initWithSet(set, capacity)) {
131: me->free();
132: return 0;
133: }
134:
135: return me;
136: }
137:
138: void OSSet::free()
139: {
140: if (members)
141: members->release();
142:
143: super::free();
144: }
145:
146: unsigned int OSSet::getCount() const
147: {
148: return members->count;
149: }
150:
151: unsigned int OSSet::getCapacity() const
152: {
153: return members->capacity;
154: }
155:
156: unsigned int OSSet::getCapacityIncrement() const
157: {
158: return members->capacityIncrement;
159: }
160:
161: unsigned int OSSet::setCapacityIncrement(unsigned int increment)
162: {
163: return members->setCapacityIncrement(increment);
164: }
165:
166: unsigned int OSSet::ensureCapacity(unsigned int newCapacity)
167: {
168: return members->ensureCapacity(newCapacity);
169: }
170:
171: void OSSet::flushCollection()
172: {
173: haveUpdated();
174: members->flushCollection();
175: }
176:
177: bool OSSet::setObject(OSObject *anObject)
178: {
179: if (containsObject(anObject))
180: return false;
181: else {
182: haveUpdated();
183: return members->setObject(anObject);
184: }
185: }
186:
187: bool OSSet::merge(const OSArray *array)
188: {
189: OSObject *anObject;
190: bool retVal = false;
191:
192: for (int i = 0; (anObject = array->getObject(i)); i++)
193: if (setObject(anObject))
194: retVal = true;
195:
196: return retVal;
197: }
198:
199: bool OSSet::merge(const OSSet *set)
200: {
201: return setObject(set->members);
202: }
203:
204: void OSSet::removeObject(OSObject *anObject)
205: {
206: OSObject *probeObject;
207:
208: for (int i = 0; (probeObject = members->getObject(i)); i++)
209: if (probeObject == anObject) {
210: haveUpdated();
211: members->removeObject(i);
212: return;
213: }
214: }
215:
216:
217: bool OSSet::containsObject(const OSObject *anObject) const
218: {
219: return anObject && member(anObject);
220: }
221:
222: bool OSSet::member(const OSObject *anObject) const
223: {
224: OSObject *probeObject;
225:
226: for (int i = 0; (probeObject = members->getObject(i)); i++)
227: if (probeObject == anObject)
228: return true;
229:
230: return false;
231: }
232:
233: OSObject *OSSet::getAnyObject() const
234: {
235: return members->getObject(0);
236: }
237:
238: bool OSSet::isEqualTo(OSSet *aSet) const
239: {
240: unsigned int count;
241: unsigned int i;
242: OSObject *obj1;
243: OSObject *obj2;
244:
245: if ( this == aSet )
246: return true;
247:
248: count = members->count;
249: if ( count != aSet->getCount() )
250: return false;
251:
252: for ( i = 0; i < count; i++ ) {
253: obj1 = aSet->members->getObject(i);
254: obj2 = members->getObject(i);
255: if ( !obj1 || !obj2 )
256: return false;
257:
258: if ( !obj1->isEqualTo(obj2) )
259: return false;
260: }
261:
262: return true;
263: }
264:
265: bool OSSet::isEqualTo(const OSObject *anObject) const
266: {
267: OSSet *otherSet;
268:
269: otherSet = OSDynamicCast(OSSet, (OSObject *)anObject);
270: if ( otherSet )
271: return isEqualTo(otherSet);
272: else
273: return false;
274: }
275:
276: unsigned int OSSet::iteratorSize() const
277: {
278: return sizeof(unsigned int);
279: }
280:
281: bool OSSet::initIterator(void *inIterator) const
282: {
283: unsigned int *iteratorP = (unsigned int *) inIterator;
284:
285: *iteratorP = 0;
286: return true;
287: }
288:
289: bool OSSet::getNextObjectForIterator(void *inIterator, OSObject **ret) const
290: {
291: unsigned int *iteratorP = (unsigned int *) inIterator;
292: unsigned int index = (*iteratorP)++;
293:
294: if (index < members->count)
295: *ret = members->getObject(index);
296: else
297: *ret = 0;
298:
299: return (*ret != 0);
300: }
301:
302: bool OSSet::serialize(OSSerialize *s) const
303: {
304: OSObject *o;
305:
306: if (s->previouslySerialized(this)) return true;
307:
308: if (!s->addXMLStartTag(this, "set")) return false;
309:
310: for (int i = 0; (o = members->getObject(i)); i++) {
311: if (!o->serialize(s)) return false;
312: }
313:
314: return s->addXMLEndTag("set");
315: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.