Overview
Comment: | Add OFIterator to iterate through an OFDictionary. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
cd08bff565a88e052694a6ab8f3c5f9b |
User & Date: | js on 2009-04-23 12:19:52 |
Other Links: | manifest | tags |
Context
2009-04-24
| ||
13:57 | Neither the key nor the value in a dictionary is allowed to be nil. check-in: fa52eae66a user: js tags: trunk | |
2009-04-23
| ||
12:19 | Add OFIterator to iterate through an OFDictionary. check-in: cd08bff565 user: js tags: trunk | |
12:07 | Free the list in the dictionary when the last object in it was removed. check-in: 69b468f9f2 user: js tags: trunk | |
Changes
Modified src/Makefile from [549491be05] to [e9f81ad1c1].
1 2 3 4 5 6 7 8 9 10 11 12 | include ../extra.mk LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX} LIB_MAJOR = 1 LIB_MINOR = 0 SRCS = OFArray.m \ OFAutoreleasePool.m \ OFConstString.m \ OFDictionary.m \ OFExceptions.m \ OFHashes.m \ | > | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | include ../extra.mk LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX} LIB_MAJOR = 1 LIB_MINOR = 0 SRCS = OFArray.m \ OFAutoreleasePool.m \ OFConstString.m \ OFDictionary.m \ OFExceptions.m \ OFFile.m \ OFHashes.m \ OFIterator.m \ OFList.m \ OFNumber.m \ OFObject.m \ ${OFPLUGIN_M} \ OFString.m \ OFTCPSocket.m \ OFXMLFactory.m \ |
︙ | ︙ |
Added src/OFIterator.h version [f7af8e9110].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /* * Copyright (c) 2008 - 2009 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. 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" /** * The OFIterator class provides methods to iterate through objects. */ @interface OFIterator: OFObject { OFList **data; size_t size; size_t pos; of_list_object_t *last; } - initWithData: (OFList**)data andSize: (size_t)size; /** * Returns the next object in the dictionary. * * Always call it twice in a row, as it returns the key on the first call and * the value on the second. Therefore, if you want a key value pair, you have * to call: * * key = [iter nextObject];\n * value = [iter nextObject]; * * When there is no more object left, an OFNotInSetException is thrown. * * \return The key on the first call and the value on the second */ - (id)nextObject; /** * Resets the iterator, so the next call to nextObject returns the first again. */ - reset; @end @interface OFDictionary (OFIterator) /** * \return An OFIterator for the OFDictionary */ - (OFIterator*)getIterator; @end |
Added src/OFIterator.m version [40539f0c13].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | /* * Copyright (c) 2008 - 2009 * Jonathan Schleifer <js@webkeks.org> * * All rights reserved. * * This file is part of libobjfw. 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 <config.h> #import "OFIterator.h" #import "OFDictionary.h" #import "OFExceptions.h" @implementation OFIterator - initWithData: (OFList**)data_ andSize: (size_t)size_ { self = [super init]; data = data_; size = size_; pos = 0; last = NULL; return self; } - (id)nextObject { if (last == NULL) { for (; pos < size && data[pos] == nil; pos++); if (pos == size) @throw [OFNotInSetException newWithClass: [OFDictionary class]]; return (last = [data[pos++] first])->object; } if ((last = last->next) != NULL) return last->object; return [self nextObject]; } - reset { pos = 0; last = NULL; return self; } @end @implementation OFDictionary (OFIterator) - (OFIterator*)getIterator { return [[[OFIterator alloc] initWithData: data andSize: size] autorelease]; } @end |
Modified tests/OFDictionary/OFDictionary.m from [c41b0b3f31] to [014c8dd087].
︙ | ︙ | |||
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #import "config.h" #include <stdio.h> #include <string.h> #import "OFAutoreleasePool.h" #import "OFDictionary.h" #import "OFConstString.h" #import "OFString.h" #import "OFExceptions.h" int main() { BOOL caught; OFDictionary *dict = [OFDictionary dictionaryWithHashSize: 16]; OFAutoreleasePool *pool = [OFAutoreleasePool new]; OFString *key1 = [OFString stringWithCString: "key1"]; OFString *key2 = [OFString stringWithCString: "key2"]; OFString *value1 = [OFString stringWithCString: "value1"]; OFString *value2 = [OFString stringWithCString: "value2"]; [dict set: key1 to: value1]; [dict set: key2 to: value2]; [pool release]; if (strcmp([[dict get: @"key1"] cString], "value1")) { | > > | | > > > > > > > > > > > > > > > > > > > | | | > | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #import "config.h" #include <stdio.h> #include <string.h> #import "OFAutoreleasePool.h" #import "OFDictionary.h" #import "OFIterator.h" #import "OFConstString.h" #import "OFString.h" #import "OFExceptions.h" int main() { BOOL caught; OFDictionary *dict = [OFDictionary dictionaryWithHashSize: 16]; OFIterator *iter = [dict getIterator]; OFAutoreleasePool *pool = [OFAutoreleasePool new]; OFString *key1 = [OFString stringWithCString: "key1"]; OFString *key2 = [OFString stringWithCString: "key2"]; OFString *value1 = [OFString stringWithCString: "value1"]; OFString *value2 = [OFString stringWithCString: "value2"]; [dict set: key1 to: value1]; [dict set: key2 to: value2]; [pool release]; if (strcmp([[dict get: @"key1"] cString], "value1")) { puts("\033[K\033[1;31mTest 1/6 failed!\033[m"); return 1; } if (strcmp([[dict get: key2] cString], "value2")) { puts("\033[K\033[1;31mTest 2/6 failed!\033[m"); return 1; } if (![[iter nextObject] isEqual: @"key2"] || ![[iter nextObject] isEqual: @"value2"] || ![[iter nextObject] isEqual: @"key1"] || ![[iter nextObject] isEqual: @"value1"]) { puts("\033[K\033[1;31mTest 3/6 failed!\033[m"); return 1; } caught = NO; @try { [iter nextObject]; } @catch (OFNotInSetException *e) { caught = YES; } if (!caught) { puts("\033[K\033[1;31mTest 4/6 failed!\033[m"); return 1; } caught = NO; @try { [dict get: @"key3"]; } @catch (OFNotInSetException *e) { caught = YES; } if (!caught) { puts("\033[K\033[1;31mTest 5/6 failed!\033[m"); return 1; } [dict remove: @"key2"]; caught = NO; @try { [dict remove: @"key2"]; } @catch (OFNotInSetException *e) { caught = YES; } if (!caught) { puts("\033[K\033[1;31mTest 6/6 failed!\033[m"); return 1; } puts("\033[1;32mTests successful: 6/6\033[0m"); return 0; } |