@@ -23,27 +23,49 @@ #import "OFXMLElement.h" #import "OFArray.h" #import "OFEnumerationMutationException.h" #import "OFInvalidArgumentException.h" + +struct _OFListItem { + struct _OFListItem *previous, *next; + id object; +}; OF_DIRECT_MEMBERS @interface OFListEnumerator: OFEnumerator { OFList *_list; - of_list_object_t *_Nullable _current; + OFListItem _Nullable _current; unsigned long _mutations; unsigned long *_Nullable _mutationsPtr; } - (instancetype)initWithList: (OFList *)list mutationsPointer: (unsigned long *)mutationsPtr; @end + +OFListItem +OFListItemNext(OFListItem listItem) +{ + return listItem->next; +} + +OFListItem +OFListItemPrevious(OFListItem listItem) +{ + return listItem->previous; +} + +id +OFListItemObject(OFListItem listItem) +{ + return listItem->object; +} @implementation OFList -@synthesize firstListObject = _firstListObject; -@synthesize lastListObject = _lastListObject; +@synthesize firstListItem = _firstListItem, lastListItem = _lastListItem; + (instancetype)list { return [[[self alloc] init] autorelease]; } @@ -54,15 +76,15 @@ @try { void *pool = objc_autoreleasePoolPush(); if (![element.name isEqual: self.className] || - ![element.namespace isEqual: OF_SERIALIZATION_NS]) + ![element.namespace isEqual: OFSerializationNS]) @throw [OFInvalidArgumentException exception]; for (OFXMLElement *child in - [element elementsForNamespace: OF_SERIALIZATION_NS]) { + [element elementsForNamespace: OFSerializationNS]) { void *pool2 = objc_autoreleasePoolPush(); [self appendObject: child.objectByDeserializing]; objc_autoreleasePoolPop(pool2); @@ -77,142 +99,135 @@ return self; } - (void)dealloc { - of_list_object_t *next; + OFListItem next; - for (of_list_object_t *iter = _firstListObject; - iter != NULL; iter = next) { + for (OFListItem iter = _firstListItem; iter != NULL; iter = next) { [iter->object release]; next = iter->next; - free(iter); + OFFreeMemory(iter); } [super dealloc]; } -- (of_list_object_t *)appendObject: (id)object -{ - of_list_object_t *listObject; - - listObject = of_alloc(1, sizeof(of_list_object_t)); - listObject->object = [object retain]; - listObject->next = NULL; - listObject->previous = _lastListObject; - - if (_lastListObject != NULL) - _lastListObject->next = listObject; - - _lastListObject = listObject; - - if (_firstListObject == NULL) - _firstListObject = listObject; - - _count++; - _mutations++; - - return listObject; -} - -- (of_list_object_t *)prependObject: (id)object -{ - of_list_object_t *listObject; - - listObject = of_alloc(1, sizeof(of_list_object_t)); - listObject->object = [object retain]; - listObject->next = _firstListObject; - listObject->previous = NULL; - - if (_firstListObject != NULL) - _firstListObject->previous = listObject; - - _firstListObject = listObject; - if (_lastListObject == NULL) - _lastListObject = listObject; - - _count++; - _mutations++; - - return listObject; -} - -- (of_list_object_t *)insertObject: (id)object - beforeListObject: (of_list_object_t *)listObject -{ - of_list_object_t *newListObject; - - newListObject = of_alloc(1, sizeof(of_list_object_t)); - newListObject->object = [object retain]; - newListObject->next = listObject; - newListObject->previous = listObject->previous; - - if (listObject->previous != NULL) - listObject->previous->next = newListObject; - - listObject->previous = newListObject; - - if (listObject == _firstListObject) - _firstListObject = newListObject; - - _count++; - _mutations++; - - return newListObject; -} - -- (of_list_object_t *)insertObject: (id)object - afterListObject: (of_list_object_t *)listObject -{ - of_list_object_t *newListObject; - - newListObject = of_alloc(1, sizeof(of_list_object_t)); - newListObject->object = [object retain]; - newListObject->next = listObject->next; - newListObject->previous = listObject; - - if (listObject->next != NULL) - listObject->next->previous = newListObject; - - listObject->next = newListObject; - - if (listObject == _lastListObject) - _lastListObject = newListObject; - - _count++; - _mutations++; - - return newListObject; -} - -- (void)removeListObject: (of_list_object_t *)listObject -{ - if (listObject->previous != NULL) - listObject->previous->next = listObject->next; - if (listObject->next != NULL) - listObject->next->previous = listObject->previous; - - if (_firstListObject == listObject) - _firstListObject = listObject->next; - if (_lastListObject == listObject) - _lastListObject = listObject->previous; - - _count--; - _mutations++; - - [listObject->object release]; - free(listObject); -} - -- (id)firstObject -{ - return (_firstListObject != NULL ? _firstListObject->object : nil); -} - -- (id)lastObject -{ - return (_lastListObject != NULL ? _lastListObject->object : nil); +- (OFListItem)appendObject: (id)object +{ + OFListItem listItem = OFAllocMemory(1, sizeof(*listItem)); + + listItem->object = [object retain]; + listItem->next = NULL; + listItem->previous = _lastListItem; + + if (_lastListItem != NULL) + _lastListItem->next = listItem; + + _lastListItem = listItem; + + if (_firstListItem == NULL) + _firstListItem = listItem; + + _count++; + _mutations++; + + return listItem; +} + +- (OFListItem)prependObject: (id)object +{ + OFListItem listItem = OFAllocMemory(1, sizeof(*listItem)); + + listItem->object = [object retain]; + listItem->next = _firstListItem; + listItem->previous = NULL; + + if (_firstListItem != NULL) + _firstListItem->previous = listItem; + + _firstListItem = listItem; + if (_lastListItem == NULL) + _lastListItem = listItem; + + _count++; + _mutations++; + + return listItem; +} + +- (OFListItem)insertObject: (id)object beforeListItem: (OFListItem)listItem +{ + OFListItem newListItem = OFAllocMemory(1, sizeof(*newListItem)); + + newListItem->object = [object retain]; + newListItem->next = listItem; + newListItem->previous = listItem->previous; + + if (listItem->previous != NULL) + listItem->previous->next = newListItem; + + listItem->previous = newListItem; + + if (listItem == _firstListItem) + _firstListItem = newListItem; + + _count++; + _mutations++; + + return newListItem; +} + +- (OFListItem)insertObject: (id)object afterListItem: (OFListItem)listItem +{ + OFListItem newListItem = OFAllocMemory(1, sizeof(*newListItem)); + + newListItem->object = [object retain]; + newListItem->next = listItem->next; + newListItem->previous = listItem; + + if (listItem->next != NULL) + listItem->next->previous = newListItem; + + listItem->next = newListItem; + + if (listItem == _lastListItem) + _lastListItem = newListItem; + + _count++; + _mutations++; + + return newListItem; +} + +- (void)removeListItem: (OFListItem)listItem +{ + if (listItem->previous != NULL) + listItem->previous->next = listItem->next; + if (listItem->next != NULL) + listItem->next->previous = listItem->previous; + + if (_firstListItem == listItem) + _firstListItem = listItem->next; + if (_lastListItem == listItem) + _lastListItem = listItem->previous; + + _count--; + _mutations++; + + [listItem->object release]; + OFFreeMemory(listItem); +} + +- (id)firstObject +{ + return (_firstListItem != NULL ? _firstListItem->object : nil); +} + +- (id)lastObject +{ + return (_lastListItem != NULL ? _lastListItem->object : nil); } - (size_t)count { return _count; @@ -219,11 +234,11 @@ } - (bool)isEqual: (id)object { OFList *list; - of_list_object_t *iter, *iter2; + OFListItem iter, iter2; if (object == self) return true; if (![object isKindOfClass: [OFList class]]) @@ -232,11 +247,11 @@ list = object; if (list.count != _count) return false; - for (iter = _firstListObject, iter2 = list.firstListObject; + for (iter = _firstListItem, iter2 = list.firstListItem; iter != NULL && iter2 != NULL; iter = iter->next, iter2 = iter2->next) if (![iter->object isEqual: iter2->object]) return false; @@ -249,12 +264,11 @@ - (bool)containsObject: (id)object { if (_count == 0) return false; - for (of_list_object_t *iter = _firstListObject; - iter != NULL; iter = iter->next) + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) if ([iter->object isEqual: object]) return true; return false; } @@ -262,80 +276,74 @@ - (bool)containsObjectIdenticalTo: (id)object { if (_count == 0) return false; - for (of_list_object_t *iter = _firstListObject; - iter != NULL; iter = iter->next) + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) if (iter->object == object) return true; return false; } - (void)removeAllObjects { - of_list_object_t *next; + OFListItem next; _mutations++; - for (of_list_object_t *iter = _firstListObject; - iter != NULL; iter = next) { + for (OFListItem iter = _firstListItem; iter != NULL; iter = next) { [iter->object release]; next = iter->next; - free(iter); + OFFreeMemory(iter); } - _firstListObject = _lastListObject = NULL; + _firstListItem = _lastListItem = NULL; } - (id)copy { OFList *copy = [[[self class] alloc] init]; - of_list_object_t *listObject, *previous; - - listObject = NULL; - previous = NULL; + OFListItem listItem = NULL, previous = NULL; @try { - for (of_list_object_t *iter = _firstListObject; + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) { - listObject = of_alloc(1, sizeof(of_list_object_t)); - listObject->object = [iter->object retain]; - listObject->next = NULL; - listObject->previous = previous; + listItem = OFAllocMemory(1, sizeof(*listItem)); + listItem->object = [iter->object retain]; + listItem->next = NULL; + listItem->previous = previous; - if (copy->_firstListObject == NULL) - copy->_firstListObject = listObject; + if (copy->_firstListItem == NULL) + copy->_firstListItem = listItem; if (previous != NULL) - previous->next = listObject; + previous->next = listItem; copy->_count++; - previous = listObject; + previous = listItem; } } @catch (id e) { [copy release]; @throw e; } - copy->_lastListObject = listObject; + copy->_lastListItem = listItem; return copy; } - (unsigned long)hash { - uint32_t hash; - - OF_HASH_INIT(hash); - - for (of_list_object_t *iter = _firstListObject; - iter != NULL; iter = iter->next) - OF_HASH_ADD_HASH(hash, [iter->object hash]); - - OF_HASH_FINALIZE(hash); + unsigned long hash; + + OFHashInit(&hash); + + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) + OFHashAddHash(&hash, [iter->object hash]); + + OFHashFinalize(&hash); return hash; } - (OFString *)description @@ -345,11 +353,11 @@ if (_count == 0) return @"[]"; ret = [OFMutableString stringWithString: @"[\n"]; - for (of_list_object_t *iter = _firstListObject; + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) { void *pool = objc_autoreleasePoolPush(); [ret appendString: [iter->object description]]; @@ -356,12 +364,11 @@ if (iter->next != NULL) [ret appendString: @",\n"]; objc_autoreleasePoolPop(pool); } - [ret replaceOccurrencesOfString: @"\n" - withString: @"\n\t"]; + [ret replaceOccurrencesOfString: @"\n" withString: @"\n\t"]; [ret appendString: @"\n]"]; [ret makeImmutable]; return ret; @@ -369,13 +376,13 @@ - (OFXMLElement *)XMLElementBySerializing { OFXMLElement *element = [OFXMLElement elementWithName: self.className - namespace: OF_SERIALIZATION_NS]; + namespace: OFSerializationNS]; - for (of_list_object_t *iter = _firstListObject; + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) { void *pool = objc_autoreleasePoolPush(); [element addChild: [iter->object XMLElementBySerializing]]; @@ -383,44 +390,44 @@ } return element; } -- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t *)state +- (int)countByEnumeratingWithState: (OFFastEnumerationState *)state objects: (id *)objects count: (int)count { - of_list_object_t *listObject; + OFListItem listItem; - memcpy(&listObject, state->extra, sizeof(listObject)); + memcpy(&listItem, state->extra, sizeof(listItem)); state->itemsPtr = objects; state->mutationsPtr = &_mutations; if (state->state == 0) { - listObject = _firstListObject; + listItem = _firstListItem; state->state = 1; } for (int i = 0; i < count; i++) { - if (listObject == NULL) + if (listItem == NULL) return i; - objects[i] = listObject->object; - listObject = listObject->next; + objects[i] = listItem->object; + listItem = listItem->next; } - memcpy(state->extra, &listObject, sizeof(listObject)); + memcpy(state->extra, &listItem, sizeof(listItem)); return count; } - (OFEnumerator *)objectEnumerator { - return [[[OFListEnumerator alloc] - initWithList: self - mutationsPointer: &_mutations] autorelease]; + return [[[OFListEnumerator alloc] initWithList: self + mutationsPointer: &_mutations] + autorelease]; } @end @implementation OFListEnumerator - (instancetype)initWithList: (OFList *)list @@ -427,11 +434,11 @@ mutationsPointer: (unsigned long *)mutationsPtr { self = [super init]; _list = [list retain]; - _current = _list.firstListObject; + _current = _list.firstListItem; _mutations = *mutationsPtr; _mutationsPtr = mutationsPtr; return self; }