@@ -23,23 +23,46 @@ #import "OFXMLElement.h" #import "OFArray.h" #import "OFEnumerationMutationException.h" #import "OFInvalidArgumentException.h" + +struct OFListItem { + OFListItem previous, next; + id object; +}; OF_DIRECT_MEMBERS @interface OFListEnumerator: OFEnumerator { OFList *_list; - OFListItem *_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 firstListItem = _firstListItem, lastListItem = _lastListItem; + (instancetype)list @@ -76,26 +99,25 @@ return self; } - (void)dealloc { - OFListItem *next; + OFListItem next; - for (OFListItem *iter = _firstListItem; iter != NULL; iter = next) { + for (OFListItem iter = _firstListItem; iter != NULL; iter = next) { [iter->object release]; next = iter->next; OFFreeMemory(iter); } [super dealloc]; } -- (OFListItem *)appendObject: (id)object +- (OFListItem)appendObject: (id)object { - OFListItem *listItem; + OFListItem listItem = OFAllocMemory(1, sizeof(*listItem)); - listItem = OFAllocMemory(1, sizeof(OFListItem)); listItem->object = [object retain]; listItem->next = NULL; listItem->previous = _lastListItem; if (_lastListItem != NULL) @@ -110,13 +132,13 @@ _mutations++; return listItem; } -- (OFListItem *)prependObject: (id)object +- (OFListItem)prependObject: (id)object { - OFListItem *listItem = OFAllocMemory(1, sizeof(OFListItem)); + OFListItem listItem = OFAllocMemory(1, sizeof(*listItem)); listItem->object = [object retain]; listItem->next = _firstListItem; listItem->previous = NULL; @@ -131,14 +153,13 @@ _mutations++; return listItem; } -- (OFListItem *)insertObject: (id)object - beforeListItem: (OFListItem *)listItem +- (OFListItem)insertObject: (id)object beforeListItem: (OFListItem)listItem { - OFListItem *newListItem = OFAllocMemory(1, sizeof(OFListItem)); + OFListItem newListItem = OFAllocMemory(1, sizeof(*newListItem)); newListItem->object = [object retain]; newListItem->next = listItem; newListItem->previous = listItem->previous; @@ -154,14 +175,13 @@ _mutations++; return newListItem; } -- (OFListItem *)insertObject: (id)object - afterListItem: (OFListItem *)listItem +- (OFListItem)insertObject: (id)object afterListItem: (OFListItem)listItem { - OFListItem *newListItem = OFAllocMemory(1, sizeof(OFListItem)); + OFListItem newListItem = OFAllocMemory(1, sizeof(*newListItem)); newListItem->object = [object retain]; newListItem->next = listItem->next; newListItem->previous = listItem; @@ -177,11 +197,11 @@ _mutations++; return newListItem; } -- (void)removeListItem: (OFListItem *)listItem +- (void)removeListItem: (OFListItem)listItem { if (listItem->previous != NULL) listItem->previous->next = listItem->next; if (listItem->next != NULL) listItem->next->previous = listItem->previous; @@ -214,11 +234,11 @@ } - (bool)isEqual: (id)object { OFList *list; - OFListItem *iter, *iter2; + OFListItem iter, iter2; if (object == self) return true; if (![object isKindOfClass: [OFList class]]) @@ -244,11 +264,11 @@ - (bool)containsObject: (id)object { if (_count == 0) return false; - for (OFListItem *iter = _firstListItem; iter != NULL; iter = iter->next) + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) if ([iter->object isEqual: object]) return true; return false; } @@ -256,24 +276,24 @@ - (bool)containsObjectIdenticalTo: (id)object { if (_count == 0) return false; - for (OFListItem *iter = _firstListItem; iter != NULL; iter = iter->next) + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) if (iter->object == object) return true; return false; } - (void)removeAllObjects { - OFListItem *next; + OFListItem next; _mutations++; - for (OFListItem *iter = _firstListItem; iter != NULL; iter = next) { + for (OFListItem iter = _firstListItem; iter != NULL; iter = next) { [iter->object release]; next = iter->next; OFFreeMemory(iter); } @@ -281,19 +301,16 @@ } - (id)copy { OFList *copy = [[[self class] alloc] init]; - OFListItem *listItem, *previous; - - listItem = NULL; - previous = NULL; + OFListItem listItem = NULL, previous = NULL; @try { - for (OFListItem *iter = _firstListItem; + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) { - listItem = OFAllocMemory(1, sizeof(OFListItem)); + listItem = OFAllocMemory(1, sizeof(*listItem)); listItem->object = [iter->object retain]; listItem->next = NULL; listItem->previous = previous; if (copy->_firstListItem == NULL) @@ -319,11 +336,11 @@ { unsigned long hash; OFHashInit(&hash); - for (OFListItem *iter = _firstListItem; iter != NULL; iter = iter->next) + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) OFHashAddHash(&hash, [iter->object hash]); OFHashFinalize(&hash); return hash; @@ -336,11 +353,11 @@ if (_count == 0) return @"[]"; ret = [OFMutableString stringWithString: @"[\n"]; - for (OFListItem *iter = _firstListItem; + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) { void *pool = objc_autoreleasePoolPush(); [ret appendString: [iter->object description]]; @@ -361,11 +378,11 @@ { OFXMLElement *element = [OFXMLElement elementWithName: self.className namespace: OFSerializationNS]; - for (OFListItem *iter = _firstListItem; + for (OFListItem iter = _firstListItem; iter != NULL; iter = iter->next) { void *pool = objc_autoreleasePoolPush(); [element addChild: [iter->object XMLElementBySerializing]]; @@ -377,11 +394,11 @@ - (int)countByEnumeratingWithState: (OFFastEnumerationState *)state objects: (id *)objects count: (int)count { - OFListItem *listItem; + OFListItem listItem; memcpy(&listItem, state->extra, sizeof(listItem)); state->itemsPtr = objects; state->mutationsPtr = &_mutations;