Comment: | New OFDictionary implementation and removal of a hack in OFList.
The new implementation is easier to use as it does automatic resizing, I decided to change the implementation as only very few need a As the new implementation no longer uses OFList, this also made it |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
bbf1f79b8fc25a5f7213dd14b73c649d |
User & Date: | js on 2009-09-08 16:06:10 |
Other Links: | manifest | tags |
2009-09-11
| ||
11:41 | Fix a warning with old GCC versions. check-in: 3468abde19 user: js tags: trunk | |
2009-09-08
| ||
16:06 | New OFDictionary implementation and removal of a hack in OFList. check-in: bbf1f79b8f user: js tags: trunk | |
2009-09-01
| ||
11:39 | Use OFMutableString in OFPlugin instead of doing it manually. check-in: cdfdea289e user: js tags: trunk | |
Modified src/OFDictionary.h from [a1a71dda9a] to [27b0d8980d].
︙ | ︙ | |||
8 9 10 11 12 13 14 | * 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" | < | | < < | < < | < > | | > | < < < < < < < < | 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 | * 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 "OFArray.h" struct of_dictionary_bucket { OFObject <OFCopying> *key; OFObject *object; uint32_t hash; }; /** * The OFDictionary class provides a class for using hash tables. */ @interface OFDictionary: OFObject <OFCopying, OFMutableCopying> { struct of_dictionary_bucket *data; size_t size; size_t count; } /** * Creates a new OFDictionary. * * \return A new autoreleased OFDictionary */ + dictionary; /** * Creates a new OFDictionary with the specified dictionary. * * \param dict An OFDictionary * \return A new autoreleased OFDictionary */ + dictionaryWithDictionary: (OFDictionary*)dict; /** * Creates a new OFDictionary with the specified key and object. * * \param key The key * \param obj The object * \return A new autoreleased OFDictionary */ |
︙ | ︙ | |||
77 78 79 80 81 82 83 | */ + dictionaryWithObjects: (OFArray*)objs forKeys: (OFArray*)keys; /** * Creates a new OFDictionary with the specified keys objects. * | | | | < < < < < < < < | 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 | */ + dictionaryWithObjects: (OFArray*)objs forKeys: (OFArray*)keys; /** * Creates a new OFDictionary with the specified keys objects. * * \param key The first key * \return A new autoreleased OFDictionary */ + dictionaryWithKeysAndObjects: (OFObject <OFCopying>*)key, ...; /** * Initializes an already allocated OFDictionary. * * \return An initialized OFDictionary */ - init; /** * Initializes an already allocated OFDictionary with the specified * OFDictionary. * * \param dict An OFDictionary * \return An initialized OFDictionary */ - initWithDictionary: (OFDictionary*)dict; /** * Initializes an already allocated OFDictionary with the specified key and * object. * * \param key The key * \param obj The object * \return A new initialized OFDictionary |
︙ | ︙ | |||
147 148 149 150 151 152 153 | * * \param first The first key * \return A new initialized OFDictionary */ - initWithKey: (OFObject <OFCopying>*)first argList: (va_list)args; | < < < < < < < | > > > > > < | 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 | * * \param first The first key * \return A new initialized OFDictionary */ - initWithKey: (OFObject <OFCopying>*)first argList: (va_list)args; /** * \param key The key whose object should be returned * \return The object for the given key or nil if the key was not found */ - (id)objectForKey: (OFObject <OFCopying>*)key; /** * \return The number of objects in the dictionary */ - (size_t)count; - setObject: (OFObject*)obj forKey: (OFObject <OFCopying>*)key; - removeObjectForKey: (OFObject*)key; @end #import "OFIterator.h" #import "OFMutableDictionary.h" |
Modified src/OFDictionary.m from [c824cfaef7] to [3ddd956ea4].
︙ | ︙ | |||
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <string.h> #import "OFDictionary.h" #import "OFIterator.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" /* References for static linking */ void _references_to_categories_of_OFDictionary() { _OFIterator_reference = 1; } @implementation OFDictionary + dictionary; { return [[[self alloc] init] autorelease]; } + dictionaryWithDictionary: (OFDictionary*)dict { return [[[self alloc] initWithDictionary: dict] autorelease]; } | > > < < < < < | 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 | #include <string.h> #import "OFDictionary.h" #import "OFIterator.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" #define BUCKET_SIZE sizeof(struct of_dictionary_bucket) /* References for static linking */ void _references_to_categories_of_OFDictionary() { _OFIterator_reference = 1; } @implementation OFDictionary + dictionary; { return [[[self alloc] init] autorelease]; } + dictionaryWithDictionary: (OFDictionary*)dict { return [[[self alloc] initWithDictionary: dict] autorelease]; } + dictionaryWithObject: (OFObject*)obj forKey: (OFObject <OFCopying>*)key { return [[[self alloc] initWithObject: obj forKey: key] autorelease]; } |
︙ | ︙ | |||
67 68 69 70 71 72 73 | return ret; } - init { self = [super init]; | | | < | < < | < < < | | < < < < < < | > < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < | < < | < < < | < < < < < < | < | < < < < < < < < | < < < | | < < < < < < < < < < < < < < < | < < < < < < < < < < < < | | > > < < < < < < < | | < < < < < < < < < < < < < | < | < | | > | | < | < < < < | < < < < | < | | < | | | < < < < < < < < < < < < < > | > < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | < | > > | > > > > > > > | > | < < > | | | 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 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | return ret; } - init { self = [super init]; size = 1; @try { data = [self allocMemoryWithSize: BUCKET_SIZE]; } @catch (OFException *e) { /* * We can't use [super dealloc] on OS X here. Compiler bug? * Anyway, set size to 0 so that [self dealloc] works. */ size = 0; [self dealloc]; @throw e; } data[0].key = nil; return self; } - initWithDictionary: (OFDictionary*)dict { size_t i; self = [super init]; if (dict == nil) { Class c = isa; size = 0; [self dealloc]; @throw [OFInvalidArgumentException newWithClass: c selector: _cmd]; } @try { data = [self allocMemoryForNItems: dict->size withSize: BUCKET_SIZE]; } @catch (OFException *e) { /* * We can't use [super dealloc] on OS X here. Compiler bug? * Anyway, we didn't do anything yet anyway, so [self dealloc] * works. */ [self dealloc]; @throw e; } size = dict->size; for (i = 0; i < size; i++) { if (dict->data[i].key != nil) { data[i].key = [dict->data[i].key copy]; data[i].object = [dict->data[i].object retain]; data[i].hash = dict->data[i].hash; } else data[i].key = nil; } return self; } - initWithObject: (OFObject*)obj forKey: (OFObject <OFCopying>*)key { const SEL sel = @selector(setObject:forKey:); IMP set = [OFMutableDictionary instanceMethodForSelector: sel]; self = [self init]; @try { set(self, sel, obj, key); } @catch (OFException *e) { [self dealloc]; @throw e; } return self; } - initWithObjects: (OFArray*)objs forKeys: (OFArray*)keys { id *objs_data, *keys_data; size_t objs_count, i; const SEL sel = @selector(setObject:forKey:); IMP set = [OFMutableDictionary instanceMethodForSelector: sel]; self = [self init]; objs_data = [objs data]; keys_data = [keys data]; objs_count = [objs count]; if (objs == nil || keys == nil || objs_count != [keys count]) { Class c = isa; [self dealloc]; @throw [OFInvalidArgumentException newWithClass: c selector: _cmd]; } @try { for (i = 0; i < objs_count; i++) set(self, sel, objs_data[i], keys_data[i]); } @catch (OFException *e) { [self dealloc]; @throw e; } return self; } - initWithKeysAndObjects: (OFObject <OFCopying>*)first, ... { id ret; va_list args; va_start(args, first); ret = [self initWithKey: first argList: args]; va_end(args); return ret; } - initWithKey: (OFObject <OFCopying>*)key argList: (va_list)args { const SEL sel = @selector(setObject:forKey:); IMP set = [OFMutableDictionary instanceMethodForSelector: sel]; self = [self init]; @try { set(self, sel, va_arg(args, OFObject*), key); while ((key = va_arg(args, OFObject <OFCopying>*)) != nil) set(self, sel, va_arg(args, OFObject*), key); } @catch (OFException *e) { [self dealloc]; @throw e; } return self; } - (id)objectForKey: (OFObject <OFCopying>*)key { uint32_t i, hash; if (key == nil) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; hash = [key hash]; for (i = hash & (size - 1); i < size && data[i].key != nil && ![data[i].key isEqual: key]; i++); /* In case the last bucket is already used */ if (i >= size) for (i = 0; i < size && data[i].key != nil && ![data[i].key isEqual: key]; i++); /* Key not in dictionary */ if (i >= size || data[i].key == nil) return nil; return data[i].object; } - (size_t)count { return count; } - (id)copy { return [self retain]; } |
︙ | ︙ | |||
460 461 462 463 464 465 466 | */ - (void)dealloc { size_t i; for (i = 0; i < size; i++) { | | < < < < | < | < < < < < < | 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | */ - (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]; } - setObject: (OFObject*)obj forKey: (OFObject <OFCopying>*)key { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } - removeObjectForKey: (OFObject*)key { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } @end |
Modified src/OFIterator.h from [0fb8f20721] to [86392c7533].
︙ | ︙ | |||
17 18 19 20 21 22 23 | * 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; | < < | | < | > > > > > > > > > > > | 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 | * 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 @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 |
Modified src/OFIterator.m from [6e8102a07c] to [d7091488bd].
︙ | ︙ | |||
21 22 23 24 25 26 27 | @implementation OFIterator - init { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } | | > > < > > > > > > > | > | | | < < | | | < < | | | > > | < | < > | | > | | | > > | | | | > < | 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 | @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 |
Modified src/OFList.h from [03977d0bbb] to [d42967ddf0].
︙ | ︙ | |||
28 29 30 31 32 33 34 | /** * The OFList class provides easy to use double-linked lists. */ @interface OFList: OFObject <OFCopying> { of_list_object_t *first; of_list_object_t *last; | < > < < < < < < < < < < < < < < | 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | /** * The OFList class provides easy to use double-linked lists. */ @interface OFList: OFObject <OFCopying> { of_list_object_t *first; of_list_object_t *last; size_t count; BOOL retain_and_release; } /** * \return A new autoreleased OFList */ + list; /** * Initializes an already allocated OFList that does not retain/release objects * added to it. * * \return An initialized OFList */ - initWithoutRetainAndRelease; |
︙ | ︙ |
Modified src/OFList.m from [6526cc451e] to [18ec5ebfcc].
︙ | ︙ | |||
18 19 20 21 22 23 24 | @implementation OFList + list { return [[[self alloc] init] autorelease]; } | < < < < < < < < < < < < < < < < < < < < | 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 | @implementation OFList + list { return [[[self alloc] init] autorelease]; } - init { self = [super init]; first = NULL; last = NULL; retain_and_release = YES; return self; } - initWithoutRetainAndRelease { self = [super init]; first = NULL; last = NULL; return self; } - (void)dealloc { of_list_object_t *iter; |
︙ | ︙ | |||
83 84 85 86 87 88 89 | return last; } - (of_list_object_t*)append: (id)obj { of_list_object_t *o; | | | 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | return last; } - (of_list_object_t*)append: (id)obj { of_list_object_t *o; o = [self allocMemoryWithSize: sizeof(of_list_object_t)]; o->object = obj; o->next = NULL; o->prev = last; if (last != NULL) last->next = o; |
︙ | ︙ | |||
107 108 109 110 111 112 113 | return o; } - (of_list_object_t*)prepend: (id)obj { of_list_object_t *o; | | | 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | return o; } - (of_list_object_t*)prepend: (id)obj { of_list_object_t *o; o = [self allocMemoryWithSize: sizeof(of_list_object_t)]; o->object = obj; o->next = first; o->prev = NULL; if (first != NULL) first->prev = o; |
︙ | ︙ | |||
132 133 134 135 136 137 138 | } - (of_list_object_t*)insert: (id)obj before: (of_list_object_t*)listobj { of_list_object_t *o; | | | 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | } - (of_list_object_t*)insert: (id)obj before: (of_list_object_t*)listobj { of_list_object_t *o; o = [self allocMemoryWithSize: sizeof(of_list_object_t)]; o->object = obj; o->next = listobj; o->prev = listobj->prev; if (listobj->prev != NULL) listobj->prev->next = o; |
︙ | ︙ | |||
158 159 160 161 162 163 164 | } - (of_list_object_t*)insert: (id)obj after: (of_list_object_t*)listobj { of_list_object_t *o; | | | 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | } - (of_list_object_t*)insert: (id)obj after: (of_list_object_t*)listobj { of_list_object_t *o; o = [self allocMemoryWithSize: sizeof(of_list_object_t)]; o->object = obj; o->next = listobj->next; o->prev = listobj; if (listobj->next != NULL) listobj->next->prev = o; |
︙ | ︙ | |||
242 243 244 245 246 247 248 | new = [[OFList alloc] initWithoutRetainAndRelease]; o = NULL; prev = NULL; @try { for (iter = first; iter != NULL; iter = iter->next) { | | | 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | new = [[OFList alloc] initWithoutRetainAndRelease]; o = NULL; prev = NULL; @try { for (iter = first; iter != NULL; iter = iter->next) { o = [new allocMemoryWithSize: sizeof(of_list_object_t)]; o->object = iter->object; o->next = NULL; o->prev = prev; if (new->first == NULL) new->first = o; if (prev != NULL) |
︙ | ︙ |
Modified src/OFMutableDictionary.h from [2cfd9e5d81] to [21cc3f3819].
︙ | ︙ | |||
26 27 28 29 30 31 32 | /** * Remove the object with the given key from the dictionary. * * \param key The key whose object should be removed */ - removeObjectForKey: (OFObject*)key; | < < < < < < < | 26 27 28 29 30 31 32 33 | /** * Remove the object with the given key from the dictionary. * * \param key The key whose object should be removed */ - removeObjectForKey: (OFObject*)key; @end |
Modified src/OFMutableDictionary.m from [48a81d9fea] to [436bcef63e].
︙ | ︙ | |||
12 13 14 15 16 17 18 19 20 21 22 23 | #include "config.h" #include <string.h> #import "OFMutableDictionary.h" #import "OFExceptions.h" #import "OFMacros.h" @implementation OFMutableDictionary - setObject: (OFObject*)obj forKey: (OFObject <OFCopying>*)key { | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | < | < | | < > > | < | < < < | > > | > > | > > > > > | > > > | < | < < < < < < | > > > | < | < < | < < | < < < < < < | < < < | < < | < < < < < | < < < < < < < | < | < | < < < < | < < < | < < | < | | < < | < < < | 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 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 | #include "config.h" #include <string.h> #import "OFMutableDictionary.h" #import "OFExceptions.h" #import "OFMacros.h" #define BUCKET_SIZE sizeof(struct of_dictionary_bucket) static OF_INLINE void resize(id self, size_t count, struct of_dictionary_bucket **data, size_t *size) { float fill = (float)count / *size; size_t newsize; struct of_dictionary_bucket *newdata; uint32_t i; /* * FIXME: * * Throw an OFOutOfRangeException if it would overflow (unlikely to * happen). */ if (fill > 0.75) newsize = *size * 2; else if (fill < 0.25) newsize = *size / 2; else return; newdata = [self allocMemoryForNItems: newsize withSize: BUCKET_SIZE]; memset(newdata, 0, newsize * BUCKET_SIZE); for (i = 0; i < *size; i++) { if ((*data)[i].key != nil) { uint32_t j = (*data)[i].hash & (newsize - 1); for (; j < newsize && newdata[j].key != nil; j++); /* In case the last bucket is already used */ if (j >= newsize) for (j = 0; j < newsize && newdata[j].key != nil; i++); if (j >= newsize) @throw [OFOutOfRangeException newWithClass: [self class]]; newdata[j].key = (*data)[i].key; newdata[j].object = (*data)[i].object; newdata[j].hash = (*data)[i].hash; } } [self freeMemory: *data]; *data = newdata; *size = newsize; } @implementation OFMutableDictionary - setObject: (OFObject*)obj forKey: (OFObject <OFCopying>*)key { uint32_t i, hash; if (key == nil || obj == nil) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; hash = [key hash]; for (i = hash & (size - 1); i < size && data[i].key != nil && ![data[i].key isEqual: key]; i++); /* In case the last bucket is already used */ if (i >= size) for (i = 0; i < size && data[i].key != nil && ![data[i].key isEqual: key]; i++); /* Key not in dictionary */ if (i >= size || data[i].key == nil) { resize(self, count + 1, &data, &size); i = hash & (size - 1); for (; i < size && data[i].key != nil; i++); /* In case the last bucket is already used */ if (i >= size) for (i = 0; i < size && data[i].key != nil; i++); if (i >= size) @throw [OFOutOfRangeException newWithClass: isa]; data[i].key = [key copy]; data[i].object = [obj retain]; data[i].hash = hash; count++; return self; } [obj retain]; [data[i].object release]; data[i].object = obj; return self; } - removeObjectForKey: (OFObject*)key { uint32_t i, hash; if (key == nil) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; hash = [key hash]; for (i = hash & (size - 1); i < size && data[i].key != nil && ![data[i].key isEqual: key]; i++); /* In case the last bucket is already used */ if (i >= size) for (i = 0; i < size && data[i].key != nil && ![data[i].key isEqual: key]; i++); /* Key not in dictionary */ if (i >= size || data[i].key == nil) return self; [data[i].key release]; [data[i].object release]; data[i].key = nil; count--; resize(self, count, &data, &size); return self; } - (id)copy { return [[OFDictionary alloc] initWithDictionary: self]; } @end |
Modified tests/OFDictionary/OFDictionary.m from [3fa2e6f5e3] to [cdca107008].
︙ | ︙ | |||
15 16 17 18 19 20 21 | #include <string.h> #import "OFAutoreleasePool.h" #import "OFDictionary.h" #import "OFString.h" #import "OFExceptions.h" | | | | | | | | < < < < < < < < < < < < | | | | 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 100 | #include <string.h> #import "OFAutoreleasePool.h" #import "OFDictionary.h" #import "OFString.h" #import "OFExceptions.h" #define TESTS 14 int main() { int i = 0; OFDictionary *dict = [OFMutableDictionary dictionary]; OFDictionary *dict2; OFArray *keys, *objs; OFIterator *iter; of_iterator_pair_t pair[2]; OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFString *key1 = [OFString stringWithString: @"key1"]; OFString *key2 = [OFString stringWithString: @"key2"]; OFString *value1 = [OFString stringWithString: @"value1"]; OFString *value2 = [OFString stringWithString: @"value2"]; [dict setObject: value1 forKey: key1]; [dict setObject: value2 forKey: key2]; [pool release]; i++; if (strcmp([[dict objectForKey: @"key1"] cString], "value1")) { printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS); return 1; } i++; if (strcmp([[dict objectForKey: key2] cString], "value2")) { printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS); return 1; } i++; iter = [dict iterator]; pair[0] = [iter nextKeyObjectPair]; pair[1] = [iter nextKeyObjectPair]; if (![pair[0].key isEqual: @"key1"] || ![pair[0].object isEqual: @"value1"] || ![pair[1].key isEqual: @"key2"] || ![pair[1].object isEqual: @"value2"]) { printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS); return 1; } i++; if ([dict count] != 2) { printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS); return 1; } i++; if ([iter nextKeyObjectPair].object != nil) { printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS); return 1; } i++; if ([dict objectForKey: @"key3"] != nil) { printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS); return 1; } i++; [dict release]; dict = [OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar", @"baz", @"qux", nil]; if (![[dict objectForKey: @"foo"] isEqual: @"bar"]) { printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS); return 1; } i++; |
︙ | ︙ |