forked from gavinkwoe/todparsekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKIntersection.m
76 lines (60 loc) · 1.68 KB
/
PKIntersection.m
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
66
67
68
69
70
71
72
73
74
75
76
//
// PKIntersection.m
// ParseKit
//
// Created by Todd Ditchendorf on 6/27/09.
// Copyright 2009 Todd Ditchendorf. All rights reserved.
//
#import "PKIntersection.h"
#import <ParseKit/PKAssembly.h>
@interface NSMutableSet (PKIntersectionAdditions)
- (void)intersectSetTestingEquality:(NSSet *)s;
@end
@implementation NSMutableSet (PKIntersectionAdditions)
- (void)intersectSetTestingEquality:(NSSet *)s {
for (id a1 in self) {
BOOL found = NO;
for (id a2 in s) {
if ([a1 isEqual:a2]) {
found = YES;
break;
}
}
if (!found) {
[self removeObject:a1];
}
}
}
@end
@interface PKParser ()
- (NSSet *)matchAndAssemble:(NSSet *)inAssemblies;
- (NSSet *)allMatchesFor:(NSSet *)inAssemblies;
@end
@interface PKCollectionParser ()
+ (id)collectionParserWithFirst:(PKParser *)p1 rest:(va_list)rest;
@end
@implementation PKIntersection
+ (id)intersection {
return [self intersectionWithSubparsers:nil];
}
+ (id)intersectionWithSubparsers:(PKParser *)p1, ... {
va_list vargs;
va_start(vargs, p1);
PKIntersection *inter = [self collectionParserWithFirst:p1 rest:vargs];
va_end(vargs);
return inter;
}
- (NSSet *)allMatchesFor:(NSSet *)inAssemblies {
NSParameterAssert(inAssemblies);
NSMutableSet *outAssemblies = [NSMutableSet set];
NSInteger i = 0;
for (PKParser *p in subparsers) {
if (0 == i++) {
outAssemblies = [[[p matchAndAssemble:inAssemblies] mutableCopy] autorelease];
} else {
[outAssemblies intersectSetTestingEquality:[p allMatchesFor:inAssemblies]];
}
}
return outAssemblies;
}
@end