Skip to content

Commit

Permalink
added support for NSMapTable collections
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschweizer committed Oct 5, 2015
1 parent 0d83566 commit 5a307a6
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Example/Tests/MappingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ - (void)testMapOnString {
XCTAssertEqualObjects(result, @"ABC");
}

- (void)testMapOnMapTable {
NSMapTable *sut = [NSMapTable strongToStrongObjectsMapTable];
[sut setObject:@1 forKey:@"foo"];
[sut setObject:@2 forKey:@"bar"];
NSMapTable *result = [sut map:^id(OCAssociation *each) {
return [@([each.value integerValue] * 2) asAssociationWithKey:each.key];
}];
XCTAssertTrue([result isKindOfClass:sut.class]);
XCTAssertEqual(result.count, 2);
XCTAssertEqualObjects([result objectForKey:@"foo"], @2);
XCTAssertEqualObjects([result objectForKey:@"bar"], @4);
}

- (void)testInjectIntoOnArray {
id result = [@[@1, @2, @3] inject:@0 into:^id(id running, id each) {
return @([running integerValue] + [each integerValue]);
Expand Down
5 changes: 5 additions & 0 deletions Lib/Internal/OCCollectionCapabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
@end


@interface NSMapTable (OCCollectionCapabilities) <OCEnumerableCollection, OCMutableCollectionConstruction>

@end


@interface NSNull (OCCollectionCapabilities) <OCEnumerableCollection>

@end
Expand Down
25 changes: 25 additions & 0 deletions Lib/Internal/OCCollectionCapabilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ - (id)oc_createCollectionOfMyKindFromMutableCollection:(id)collection {
@end


@implementation NSMapTable (OCCollectionCapabilities)

- (id)oc_createMutableInstanceOfMyKind {
return [[NSMapTable alloc] initWithKeyPointerFunctions:self.keyPointerFunctions valuePointerFunctions:self.valuePointerFunctions capacity:self.count];
}

- (NSEnumerator *)oc_collectionEnumerator {
return [[OCMapTableEnumerator alloc] initWithMapTable:self];
}

- (void)oc_addObject:(id)obj toMutableCollection:(id)collection {
NSMapTable *mapTable = collection;
OCAssociation *association = obj;
[mapTable setObject:association.value forKey:association.key];
}

- (id)oc_createCollectionOfMyKindFromMutableCollection:(id)collection {
// there are no mutable/immutable variants of NSMapTable, so it is
// safe to just return colleciton here
return collection;
}

@end


@implementation NSPointerArray (OCCollectionCapabilities)

- (void)oc_addObject:(id)object {
Expand Down
7 changes: 7 additions & 0 deletions Lib/Internal/OCEnumerators.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
@end


@interface OCMapTableEnumerator : NSEnumerator

- (instancetype)initWithMapTable:(NSMapTable *)mapTable;

@end


@interface OCNullEnumerator : NSEnumerator

@end
33 changes: 33 additions & 0 deletions Lib/Internal/OCEnumerators.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,39 @@ - (id)nextObject {
@end


@interface OCMapTableEnumerator ()

@property (nonatomic, readonly) NSMapTable *mapTable;
@property (nonatomic, readonly) NSEnumerator *keyEnumerator;

@end


@implementation OCMapTableEnumerator

- (instancetype)initWithMapTable:(NSMapTable *)mapTable {
if (self = [super init]) {
_mapTable = mapTable;
_keyEnumerator = [_mapTable keyEnumerator];
}
return self;
}

- (id)nextObject {

id nextKey = [self.keyEnumerator nextObject];
if (!nextKey) {
return nil;
}

id nextValue = [self.mapTable objectForKey:nextKey];
return [nextKey asAssociationWithValue:nextValue];

}

@end


@implementation OCNullEnumerator

- (id)nextObject {
Expand Down

0 comments on commit 5a307a6

Please sign in to comment.