Index: ObjFW.xcodeproj/project.pbxproj ================================================================== --- ObjFW.xcodeproj/project.pbxproj +++ ObjFW.xcodeproj/project.pbxproj @@ -67,10 +67,12 @@ remoteInfo = configure; }; /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 4B0108C910EB8C9300631877 /* OFEnumerator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFEnumerator.h; path = src/OFEnumerator.h; sourceTree = SOURCE_ROOT; }; + 4B0108CA10EB8C9300631877 /* OFEnumerator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFEnumerator.m; path = src/OFEnumerator.m; sourceTree = SOURCE_ROOT; }; 4B6799561099E7C50041064A /* asprintf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = asprintf.h; path = src/asprintf.h; sourceTree = ""; }; 4B6799581099E7C50041064A /* objc_sync.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = objc_sync.m; path = src/objc_sync.m; sourceTree = ""; }; 4B67995A1099E7C50041064A /* OFArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFArray.h; path = src/OFArray.h; sourceTree = ""; }; 4B67995B1099E7C50041064A /* OFArray.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFArray.m; path = src/OFArray.m; sourceTree = ""; }; 4B67995C1099E7C50041064A /* OFAutoreleasePool.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFAutoreleasePool.h; path = src/OFAutoreleasePool.h; sourceTree = ""; }; @@ -85,12 +87,10 @@ 4B6799651099E7C50041064A /* OFExceptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFExceptions.m; path = src/OFExceptions.m; sourceTree = ""; }; 4B6799661099E7C50041064A /* OFFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFFile.h; path = src/OFFile.h; sourceTree = ""; }; 4B6799671099E7C50041064A /* OFFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFFile.m; path = src/OFFile.m; sourceTree = ""; }; 4B6799681099E7C50041064A /* OFHashes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFHashes.h; path = src/OFHashes.h; sourceTree = ""; }; 4B6799691099E7C50041064A /* OFHashes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFHashes.m; path = src/OFHashes.m; sourceTree = ""; }; - 4B67996A1099E7C50041064A /* OFIterator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFIterator.h; path = src/OFIterator.h; sourceTree = ""; }; - 4B67996B1099E7C50041064A /* OFIterator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFIterator.m; path = src/OFIterator.m; sourceTree = ""; }; 4B67996C1099E7C50041064A /* OFList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFList.h; path = src/OFList.h; sourceTree = ""; }; 4B67996D1099E7C50041064A /* OFList.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFList.m; path = src/OFList.m; sourceTree = ""; }; 4B67996E1099E7C50041064A /* OFMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFMacros.h; path = src/OFMacros.h; sourceTree = ""; }; 4B67996F1099E7C50041064A /* OFMutableArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFMutableArray.h; path = src/OFMutableArray.h; sourceTree = ""; }; 4B6799701099E7C50041064A /* OFMutableArray.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFMutableArray.m; path = src/OFMutableArray.m; sourceTree = ""; }; @@ -141,18 +141,18 @@ 4B67995F1099E7C50041064A /* OFConstString.m */, 4B6799601099E7C50041064A /* OFDataArray.h */, 4B6799611099E7C50041064A /* OFDataArray.m */, 4B6799621099E7C50041064A /* OFDictionary.h */, 4B6799631099E7C50041064A /* OFDictionary.m */, + 4B0108C910EB8C9300631877 /* OFEnumerator.h */, + 4B0108CA10EB8C9300631877 /* OFEnumerator.m */, 4B6799641099E7C50041064A /* OFExceptions.h */, 4B6799651099E7C50041064A /* OFExceptions.m */, 4B6799661099E7C50041064A /* OFFile.h */, 4B6799671099E7C50041064A /* OFFile.m */, 4B6799681099E7C50041064A /* OFHashes.h */, 4B6799691099E7C50041064A /* OFHashes.m */, - 4B67996A1099E7C50041064A /* OFIterator.h */, - 4B67996B1099E7C50041064A /* OFIterator.m */, 4B67996C1099E7C50041064A /* OFList.h */, 4B67996D1099E7C50041064A /* OFList.m */, 4B67996E1099E7C50041064A /* OFMacros.h */, 4B67996F1099E7C50041064A /* OFMutableArray.h */, 4B6799701099E7C50041064A /* OFMutableArray.m */, Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -10,11 +10,11 @@ OFDataArray.m \ OFDictionary.m \ OFExceptions.m \ OFFile.m \ OFHashes.m \ - OFIterator.m \ + OFEnumerator.m \ OFList.m \ OFMutableArray.m \ OFMutableDictionary.m \ OFMutableString.m \ OFNumber.m \ Index: src/OFDictionary.h ================================================================== --- src/OFDictionary.h +++ src/OFDictionary.h @@ -146,7 +146,7 @@ - setObject: (OFObject*)obj forKey: (OFObject *)key; - removeObjectForKey: (OFObject*)key; @end -#import "OFIterator.h" +#import "OFEnumerator.h" #import "OFMutableDictionary.h" Index: src/OFDictionary.m ================================================================== --- src/OFDictionary.m +++ src/OFDictionary.m @@ -12,21 +12,21 @@ #include "config.h" #include #import "OFDictionary.h" -#import "OFIterator.h" +#import "OFEnumerator.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" #import "OFMacros.h" #define BUCKET_SIZE sizeof(struct of_dictionary_bucket) /* References for static linking */ void _references_to_categories_of_OFDictionary() { - _OFIterator_reference = 1; + _OFEnumerator_reference = 1; } @implementation OFDictionary + dictionary; { ADDED src/OFEnumerator.h Index: src/OFEnumerator.h ================================================================== --- src/OFEnumerator.h +++ src/OFEnumerator.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2008 - 2009 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import "OFObject.h" +#import "OFList.h" +#import "OFDictionary.h" + +/** + * An enumerator pair combines a key and its object in a single struct. + */ +typedef struct __of_enumerator_pair { + /// The key + id key; + /// The object for the key + id object; +} of_enumerator_pair_t; + +extern int _OFEnumerator_reference; + +/** + * The OFEnumerator class provides methods to enumerate through objects. + */ +@interface OFEnumerator: OFObject +{ + struct of_dictionary_bucket *data; + size_t size; + size_t pos; +} + +- initWithData: (struct of_dictionary_bucket*)data + size: (size_t)size; + +/** + * \return A struct containing the next key and object + */ +- (of_enumerator_pair_t)nextKeyObjectPair; + +/** + * Resets the enumerator, so the next call to nextObject returns the first + * again. + */ +- reset; +@end + +/** + * The OFEnumerator category adds functions to get an interator to OFDictionary. + */ +@interface OFDictionary (OFEnumerator) +/** + * Creates an OFEnumerator for the dictionary. + * + * It will copy the data of the OFDictionary so that OFEnumerator will always + * operate on the data that was present when it was created. If you changed the + * OFDictionary and want to operate on the new data, you need to create a new + * OFEnumerator, as using reset will only reset the OFEnumerator, but won't + * update the data. It will also retain the data inside the OFDictionary so the + * OFEnumerator still works after you released the OFDictionary. Thus, if you + * want to get rid of the objects in the OFDictionary, you also need to release + * the OFEnumerator. + * + * \return An OFEnumerator for the OFDictionary + */ +- (OFEnumerator*)enumerator; +@end ADDED src/OFEnumerator.m Index: src/OFEnumerator.m ================================================================== --- src/OFEnumerator.m +++ src/OFEnumerator.m @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2008 - 2009 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#include "config.h" + +#import "OFEnumerator.h" +#import "OFDictionary.h" +#import "OFExceptions.h" + +/* Reference for static linking */ +int _OFEnumerator_reference; + +@implementation OFEnumerator +- init +{ + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; +} + +- initWithData: (struct of_dictionary_bucket*)data_ + size: (size_t)size_ +{ + size_t i; + + self = [super init]; + + size = size_; + data = [self allocMemoryForNItems: size + withSize: sizeof(struct of_dictionary_bucket)]; + + for (i = 0; i < size; i++) { + if (data_[i].key != nil) { + data[i].key = [data_[i].key copy]; + data[i].object = [data_[i].object retain]; + } else + data[i].key = nil; + } + + return self; +} + +- (void)dealloc +{ + size_t i; + + for (i = 0; i < size; i++) { + if (data[i].key != nil) { + [data[i].key release]; + [data[i].object release]; + } + } + + [super dealloc]; +} + +- (of_enumerator_pair_t)nextKeyObjectPair +{ + of_enumerator_pair_t next; + + for (; pos < size && data[pos].key == nil; pos++); + + if (pos < size) { + next.key = data[pos].key; + next.object = data[pos].object; + pos++; + } else { + next.key = nil; + next.object = nil; + } + + return next; +} + +- reset +{ + pos = 0; + + return self; +} +@end + +@implementation OFDictionary (OFEnumerator) +- (OFEnumerator*)enumerator +{ + return [[[OFEnumerator alloc] initWithData: data + size: size] autorelease]; +} +@end DELETED src/OFIterator.h Index: src/OFIterator.h ================================================================== --- src/OFIterator.h +++ src/OFIterator.h @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE included in - * the packaging of this file. - */ - -#import "OFObject.h" -#import "OFList.h" -#import "OFDictionary.h" - -/** - * An iterator pair combines a key and its object in a single struct. - */ -typedef struct __of_iterator_pair { - /// The key - id key; - /// The object for the key - id object; -} of_iterator_pair_t; - -extern int _OFIterator_reference; - -/** - * The OFIterator class provides methods to iterate through objects. - */ -@interface OFIterator: OFObject -{ - struct of_dictionary_bucket *data; - size_t size; - size_t pos; -} - -- initWithData: (struct of_dictionary_bucket*)data - size: (size_t)size; - -/** - * \return A struct containing the next key and object - */ -- (of_iterator_pair_t)nextKeyObjectPair; - -/** - * Resets the iterator, so the next call to nextObject returns the first again. - */ -- reset; -@end - -/** - * The OFIterator category adds functions to get an interator to OFDictionary. - */ -@interface OFDictionary (OFIterator) -/** - * Creates an OFIterator for the dictionary. - * - * It will copy the data of the OFDictionary so that OFIterator will always - * operate on the data that was present when it was created. If you changed the - * OFDictionary and want to operate on the new data, you need to create a new - * OFIterator, as using reset will only reset the OFIterator, but won't update - * the data. It will also retain the data inside the OFDictionary so the - * OFIterator still works after you released the OFDictionary. Thus, if you want - * to get rid of the objects in the OFDictionary, you also need to release the - * OFIterator. - * - * \return An OFIterator for the OFDictionary - */ -- (OFIterator*)iterator; -@end DELETED src/OFIterator.m Index: src/OFIterator.m ================================================================== --- src/OFIterator.m +++ src/OFIterator.m @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE included in - * the packaging of this file. - */ - -#include "config.h" - -#import "OFIterator.h" -#import "OFDictionary.h" -#import "OFExceptions.h" - -/* Reference for static linking */ -int _OFIterator_reference; - -@implementation OFIterator -- init -{ - @throw [OFNotImplementedException newWithClass: isa - selector: _cmd]; -} - -- initWithData: (struct of_dictionary_bucket*)data_ - size: (size_t)size_ -{ - size_t i; - - self = [super init]; - - size = size_; - data = [self allocMemoryForNItems: size - withSize: sizeof(struct of_dictionary_bucket)]; - - for (i = 0; i < size; i++) { - if (data_[i].key != nil) { - data[i].key = [data_[i].key copy]; - data[i].object = [data_[i].object retain]; - } else - data[i].key = nil; - } - - return self; -} - -- (void)dealloc -{ - size_t i; - - for (i = 0; i < size; i++) { - if (data[i].key != nil) { - [data[i].key release]; - [data[i].object release]; - } - } - - [super dealloc]; -} - -- (of_iterator_pair_t)nextKeyObjectPair -{ - of_iterator_pair_t next; - - for (; pos < size && data[pos].key == nil; pos++); - - if (pos < size) { - next.key = data[pos].key; - next.object = data[pos].object; - pos++; - } else { - next.key = nil; - next.object = nil; - } - - return next; -} - -- reset -{ - pos = 0; - - return self; -} -@end - -@implementation OFDictionary (OFIterator) -- (OFIterator*)iterator -{ - return [[[OFIterator alloc] initWithData: data - size: size] autorelease]; -} -@end Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -19,11 +19,11 @@ #import "OFArray.h" #import "OFList.h" #import "OFDictionary.h" -#import "OFIterator.h" +#import "OFNumerator.h" #import "OFNumber.h" #import "OFStream.h" Index: tests/OFDictionary.m ================================================================== --- tests/OFDictionary.m +++ tests/OFDictionary.m @@ -31,12 +31,12 @@ void dictionary_tests() { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFDictionary *dict = [OFMutableDictionary dictionary], *dict2; - OFIterator *iter; - of_iterator_pair_t pair[3]; + OFEnumerator *enumerator; + of_enumerator_pair_t pair[3]; OFArray *akeys, *avalues; [dict setObject: values[0] forKey: keys[0]]; [dict setObject: values[1] @@ -45,16 +45,16 @@ TEST(@"-[objectForKey:]", [[dict objectForKey: keys[0]] isEqual: values[0]] && [[dict objectForKey: keys[1]] isEqual: values[1]] && [dict objectForKey: @"key3"] == nil) - TEST(@"-[iterator]", (iter = [dict iterator])) + TEST(@"-[enumerator]", (enumerator = [dict enumerator])) - pair[0] = [iter nextKeyObjectPair]; - pair[1] = [iter nextKeyObjectPair]; - pair[2] = [iter nextKeyObjectPair]; - TEST(@"OFIterator's -[nextKeyObjectPair]", + pair[0] = [enumerator nextKeyObjectPair]; + pair[1] = [enumerator nextKeyObjectPair]; + pair[2] = [enumerator nextKeyObjectPair]; + TEST(@"OFEnumerator's -[nextKeyObjectPair]", [pair[0].key isEqual: keys[0]] && [pair[0].object isEqual: values[0]] && [pair[1].key isEqual: keys[1]] && [pair[1].object isEqual: values[1]] && pair[2].key == nil && pair[2].object == nil)