Overview
Comment: | Add a new convenience method for OFDictionary. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
b02800172f80f8ca4db25b9c2e52ead9 |
User & Date: | js on 2009-05-18 22:21:46 |
Other Links: | manifest | tags |
Context
2009-05-18
| ||
22:47 | Add another convenience method for OFDictionary. check-in: 5d9ea7b9cc user: js tags: trunk | |
22:21 | Add a new convenience method for OFDictionary. check-in: b02800172f user: js tags: trunk | |
22:09 | Fix error handling in init methods in OFArray. check-in: cc6d2d3987 user: js tags: trunk | |
Changes
Modified src/OFDictionary.h from [e068e112c6] to [8dfc026fb1].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /* * 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" /** * The OFDictionary class provides a class for using hash tables. */ | > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /* * 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. */ #include <stdarg.h> #import "OFObject.h" #import "OFList.h" /** * The OFDictionary class provides a class for using hash tables. */ |
︙ | ︙ | |||
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 | * Creates a new OFDictionary with a hash of N bits. * * \param bits The size of the hash to use * \return A new autoreleased OFDictionary */ + dictionaryWithHashSize: (int)hashsize; /** * Initializes an already allocated OFDictionary, defaulting to a 12 bit hash. * * \return An initialized OFDictionary */ - init; /** * Initializes an already allocated OFDictionary with a hash of N bits. * * \param bits The size of the hash to use * \return An initialized OFDictionary */ - initWithHashSize: (int)hashsize; /** * \return The average number of items in a used bucket. Buckets that are * completely empty are not in the calculation. If this value is >= 2.0, * you should resize the dictionary, in most cases even earlier! */ - (float)averageItemsPerBucket; | > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | * Creates a new OFDictionary with a hash of N bits. * * \param bits The size of the hash to use * \return A new autoreleased OFDictionary */ + dictionaryWithHashSize: (int)hashsize; /** * Creates a new OFDictionary with the specified objects. * * \param first The first key * \return A new autoreleased OFDictionary */ + dictionaryWithKeysAndObjects: (OFObject <OFCopying>*)first, ...; /** * Initializes an already allocated OFDictionary, defaulting to a 12 bit hash. * * \return An initialized OFDictionary */ - init; /** * Initializes an already allocated OFDictionary with a hash of N bits. * * \param bits The size of the hash to use * \return An initialized OFDictionary */ - initWithHashSize: (int)hashsize; /** * Initialized an already allocated OFDictionary with the specified objects. * * \param first The first key * \return A new initialized OFDictionary */ - initWithKeysAndObjects: (OFObject <OFCopying>*)first, ...; /** * Initialized an already allocated OFDictionary with the specified objects. * * \param first The first key * \return A new initialized OFDictionary */ - initWithKey: (OFObject <OFCopying>*)first andArgList: (va_list)args; /** * \return The average number of items in a used bucket. Buckets that are * completely empty are not in the calculation. If this value is >= 2.0, * you should resize the dictionary, in most cases even earlier! */ - (float)averageItemsPerBucket; |
︙ | ︙ |
Modified src/OFDictionary.m from [c51c9bd24f] to [e1fdd39466].
︙ | ︙ | |||
29 30 31 32 33 34 35 36 37 38 39 40 41 42 | return [[[self alloc] init] autorelease]; } + dictionaryWithHashSize: (int)hashsize { return [[[self alloc] initWithHashSize: hashsize] autorelease]; } - init { self = [super init]; size = 4096; | > > > > > > > > > > > > > | 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 | return [[[self alloc] init] autorelease]; } + dictionaryWithHashSize: (int)hashsize { return [[[self alloc] initWithHashSize: hashsize] autorelease]; } + dictionaryWithKeysAndObjects: (OFObject <OFCopying>*)first, ... { id ret; va_list args; va_start(args, first); ret = [[[self alloc] initWithKey: first andArgList: args] autorelease]; va_end(args); return ret; } - init { self = [super init]; size = 4096; |
︙ | ︙ | |||
79 80 81 82 83 84 85 86 87 88 89 90 91 92 | * Anyway, set size to 0 so that [self dealloc] works. */ size = 0; [self dealloc]; @throw e; } memset(data, 0, size * sizeof(OFList*)); return self; } - (float)averageItemsPerBucket { size_t items, buckets, i; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | * Anyway, set size to 0 so that [self dealloc] works. */ size = 0; [self dealloc]; @throw e; } memset(data, 0, size * sizeof(OFList*)); return self; } - initWithKeysAndObjects: (OFObject <OFCopying>*)first, ... { id ret; va_list args; va_start(args, first); ret = [self initWithKey: first andArgList: args]; va_end(args); return ret; } - initWithKey: (OFObject <OFCopying>*)first andArgList: (va_list)args { id key, obj; Class c; uint32_t hash; self = [self init]; obj = va_arg(args, id); if (obj == nil) { c = isa; [self dealloc]; @throw [OFInvalidArgumentException newWithClass: isa andSelector: _cmd]; } hash = [first hash] & (size - 1); key = [first copy]; @try { if (data[hash] == nil) data[hash] = [[OFList alloc] init]; [data[hash] append: key]; [data[hash] append: obj]; } @catch (OFException *e) { [self dealloc]; @throw e; } @finally { [key release]; } while ((key = va_arg(args, id)) != nil) { if ((obj = va_arg(args, id)) == nil) { c = isa; [self dealloc]; @throw [OFInvalidArgumentException newWithClass: isa andSelector: _cmd]; } hash = [key hash] & (size - 1); @try { key = [key copy]; } @catch (OFException *e) { [self dealloc]; @throw e; } @try { if (data[hash] == nil) data[hash] = [[OFList alloc] init]; [data[hash] append: key]; [data[hash] append: obj]; } @catch (OFException *e) { [self dealloc]; @throw e; } @finally { [key release]; } } return self; } - (float)averageItemsPerBucket { size_t items, buckets, i; |
︙ | ︙ |
Modified tests/OFDictionary/OFDictionary.m from [53a9503f21] to [1082626ba9].
︙ | ︙ | |||
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #import "OFString.h" #import "OFExceptions.h" int main() { OFDictionary *dict = [OFMutableDictionary dictionaryWithHashSize: 16]; OFIterator *iter = [dict iterator]; OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; 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")) { | > | | | | | | | > > > > > > > > > > > > > > | | 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 100 101 | #import "OFString.h" #import "OFExceptions.h" int main() { OFDictionary *dict = [OFMutableDictionary dictionaryWithHashSize: 16]; OFDictionary *dict2; OFIterator *iter = [dict iterator]; OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; 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/9 failed!\033[m"); return 1; } if (strcmp([[dict get: key2] cString], "value2")) { puts("\033[K\033[1;31mTest 2/9 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/9 failed!\033[m"); return 1; } [dict changeHashSize: 8]; iter = [dict iterator]; if (![[iter nextObject] isEqual: @"key1"] || ![[iter nextObject] isEqual: @"value1"] || ![[iter nextObject] isEqual: @"key2"] || ![[iter nextObject] isEqual: @"value2"]) { puts("\033[K\033[1;31mTest 4/9 failed!\033[m"); return 1; } if ([dict averageItemsPerBucket] != 1.0) { puts("\033[K\033[1;31mTest 5/9 failed!\033[m"); return 1; } if ([iter nextObject] != nil) { puts("\033[K\033[1;31mTest 6/9 failed!\033[m"); return 1; } if ([dict get: @"key3"] != nil) { puts("\033[K\033[1;31mTest 7/9 failed!\033[m"); return 1; } dict2 = [OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar", @"baz", @"qux", nil]; if (![[dict2 get: @"foo"] isEqual: @"bar"]) { puts("\033[K\033[1;31mTest 8/9 failed!\033[m"); return 1; } if (![[dict2 get: @"baz"] isEqual: @"qux"]) { puts("\033[K\033[1;31mTest 9/9 failed!\033[m"); return 1; } puts("\033[1;32mTests successful: 9/9\033[0m"); return 0; } |