Index: src/OFDictionary.h ================================================================== --- src/OFDictionary.h +++ src/OFDictionary.h @@ -11,10 +11,11 @@ #include #import "OFObject.h" #import "OFArray.h" +#import "OFFastEnumeration.h" struct of_dictionary_bucket { OFObject *key; OFObject *object; @@ -22,11 +23,12 @@ }; /** * The OFDictionary class is a class for using hash tables. */ -@interface OFDictionary: OFObject +@interface OFDictionary: OFObject { struct of_dictionary_bucket *data; size_t size; size_t count; } Index: src/OFDictionary.m ================================================================== --- src/OFDictionary.m +++ src/OFDictionary.m @@ -505,10 +505,33 @@ ![[dict objectForKey: data[i].key] isEqual: data[i].object]) return NO; return YES; } + +- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state + objects: (id*)objects + count: (int)count_ +{ + size_t i; + + for (i = 0; i < count_; i++) { + for (; state->state < size && data[state->state].key == nil; + state->state++); + + if (state->state < size) { + objects[i] = data[state->state].key; + state->state++; + } else + break; + } + + state->itemsPtr = objects; + state->mutationsPtr = (unsigned long*)self; + + return i; +} - (void)dealloc { size_t i; Index: src/OFMutableDictionary.m ================================================================== --- src/OFMutableDictionary.m +++ src/OFMutableDictionary.m @@ -162,6 +162,29 @@ - (id)copy { return [[OFDictionary alloc] initWithDictionary: self]; } + +- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state + objects: (id*)objects + count: (int)count_ +{ + size_t i; + + for (i = 0; i < count_; i++) { + for (; state->state < size && data[state->state].key == nil; + state->state++); + + if (state->state < size) { + objects[i] = data[state->state].key; + state->state++; + } else + break; + } + + state->itemsPtr = objects; + state->mutationsPtr = &mutations; + + return i; +} @end Index: tests/OFDictionary.m ================================================================== --- tests/OFDictionary.m +++ tests/OFDictionary.m @@ -56,10 +56,39 @@ [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) + +#ifdef OF_HAVE_FAST_ENUMERATION + size_t i = 0; + BOOL ok = YES; + + for (OFString *key in dict) { + if (![key isEqual: keys[i]]) + ok = NO; + [dict setObject: [dict objectForKey: key] + forKey: key]; + i++; + } + + TEST(@"Fast Enumeration", ok) + + ok = NO; + @try { + for (OFString *key in dict) + [dict setObject: @"" + forKey: @""]; + } @catch (OFEnumerationMutationException *e) { + ok = YES; + [e dealloc]; + } + + TEST(@"Detection of mutation during Fast Enumeration", ok) + + [dict removeObjectForKey: @""]; +#endif TEST(@"-[count]", [dict count] == 2) TEST(@"+[dictionaryWithKeysAndObjects:]", (dict = [OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar",