Permalink
Browse files

Some further optimisation.

  • Loading branch information...
1 parent d4e0e5f commit 39c3e31024061002386cb30e3e52019227c6a936 @beelsebob committed Aug 23, 2012
@@ -82,7 +82,7 @@ - (NSSet *)lr0GotoKernelWithItems:(NSSet *)i symbol:(CPGrammarSymbol *)symbol
{
return [[i objectsPassingTest:^ BOOL (CPItem *item, BOOL *stop)
{
- return [symbol isEqual:[item nextSymbol]];
+ return [symbol isEqualToGrammarSymbol:[item nextSymbol]];
}]
map:^ id (CPItem *item)
{
@@ -161,7 +161,7 @@ - (NSSet *)lr1GotoKernelWithItems:(NSSet *)i symbol:(CPGrammarSymbol *)symbol
{
return [[i objectsPassingTest:^ BOOL (CPItem *item, BOOL *stop)
{
- return [symbol isEqual:[item nextSymbol]];
+ return [symbol isEqualToGrammarSymbol:[item nextSymbol]];
}]
map:^ id (CPItem *item)
{
@@ -67,6 +67,13 @@
*/
@property (readwrite, assign, getter=isTerminal) BOOL terminal;
+/**
+ * Determines whether the grammar symbol is equal to another.
+ * @param object The other grammar symbol to compare.
+ * @return Whether the two symbols are equal.
+ */
+- (BOOL)isEqualToGrammarSymbol:(CPGrammarSymbol *)object;
+
@end
@interface NSObject (CPGrammarSymbol)
@@ -75,6 +75,11 @@ - (BOOL)isEqual:(id)object
[((CPGrammarSymbol *)object)->name isEqualToString:name]);
}
+- (BOOL)isEqualToGrammarSymbol:(CPGrammarSymbol *)object
+{
+ return (object != nil && object->terminal == terminal && [object->name isEqualToString:name]);
+}
+
- (NSUInteger)hash
{
return [[self name] hash];
@@ -88,7 +88,7 @@ - (BOOL)isEqual:(id)object
- (BOOL)isEqualToItem:(CPItem *)item
{
- return item->position == position && item->rule == rule;
+ return item != nil && item->position == position && item->rule == rule;
}
- (NSUInteger)hash
@@ -61,7 +61,7 @@ - (BOOL)isEqual:(id)object
{
return ([object isLR1Item] &&
[super isEqualToItem:(CPLR1Item *)object] &&
- [((CPLR1Item *)object)->terminal isEqual:terminal]);
+ [((CPLR1Item *)object)->terminal isEqualToGrammarSymbol:terminal]);
}
- (NSUInteger)hash
@@ -30,6 +30,8 @@
- (NSString *)descriptionWithGrammar:(CPGrammar *)g;
+- (BOOL)isEqualToShiftReduceAction:(CPShiftReduceAction *)object;
+
@end
@interface NSObject (CPIsShiftReduceAction)
@@ -190,6 +190,24 @@ - (BOOL)isEqual:(id)object
return NO;
}
+- (BOOL)isEqualToShiftReduceAction:(CPShiftReduceAction *)object
+{
+ if (object != nil && object->type == type)
+ {
+ switch (type)
+ {
+ case kActionTypeShift:
+ return [object newState] == details.shift;
+ case kActionTypeReduce:
+ return [object reductionRule] == details.reductionRule;
+ case kActionTypeAccept:
+ return YES;
+ }
+ }
+
+ return NO;
+}
+
- (NSString *)description
{
switch (type)
@@ -10,6 +10,7 @@
#import "CPItem.h"
#import "CPGrammarSymbol.h"
+#import "CPShiftReduceAction.h"
@implementation CPShiftReduceActionTable
{
@@ -74,7 +75,7 @@ - (void)dealloc
- (BOOL)setAction:(CPShiftReduceAction *)action forState:(NSUInteger)state name:(NSString *)token
{
NSMutableDictionary *row = table[state];
- if (nil != [row objectForKey:token] && ![[row objectForKey:token] isEqual:action])
+ if (nil != [row objectForKey:token] && ![[row objectForKey:token] isEqualToShiftReduceAction:action])
{
return NO;
}

0 comments on commit 39c3e31

Please sign in to comment.