Permalink
Browse files

improved efficiency of goto table.

  • Loading branch information...
1 parent 6da286d commit 7e6e8103384671624a1472125caf4f318db4f20f @beelsebob beelsebob committed Oct 28, 2011
Showing with 22 additions and 10 deletions.
  1. +22 −10 CoreParse/Parsers/CPShiftReduceParsers/CPShiftReduceGotoTable.m
@@ -11,21 +11,22 @@
@implementation CPShiftReduceGotoTable
{
- NSArray *table;
+ NSMutableDictionary **table;
+ NSUInteger capacity;
}
-- (id)initWithCapacity:(NSUInteger)capacity
+- (id)initWithCapacity:(NSUInteger)initCapacity
{
self = [super init];
if (nil != self)
{
- NSMutableArray *initTable = [NSMutableArray arrayWithCapacity:capacity];
+ capacity = initCapacity;
+ table = malloc(capacity * sizeof(NSMutableDictionary *));
for (NSUInteger buildingState = 0; buildingState < capacity; buildingState++)
{
- [initTable addObject:[NSMutableDictionary dictionary]];
+ table[buildingState] = [[NSMutableDictionary alloc] init];
}
- table = [initTable retain];
}
return self;
@@ -39,27 +40,38 @@ - (id)initWithCoder:(NSCoder *)aDecoder
if (nil != self)
{
- table = [[aDecoder decodeObjectForKey:CPShiftReduceGotoTableTableKey] retain];
+ NSArray *rows = [aDecoder decodeObjectForKey:CPShiftReduceGotoTableTableKey];
+ capacity = [rows count];
+ table = malloc(capacity * sizeof(NSMutableDictionary *));
+ [rows getObjects:table range:NSMakeRange(0, capacity)];
+ for (NSUInteger i = 0; i < capacity; i++)
+ {
+ [table[i] retain];
+ }
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
- [aCoder encodeObject:table forKey:CPShiftReduceGotoTableTableKey];
+ [aCoder encodeObject:[NSArray arrayWithObjects:table count:capacity] forKey:CPShiftReduceGotoTableTableKey];
}
- (void)dealloc
{
- [table release];
+ for (NSUInteger state = 0; state < capacity; state++)
+ {
+ [table[state] release];
+ }
+ free(table);
[super dealloc];
}
- (BOOL)setGoto:(NSUInteger)gotoIndex forState:(NSUInteger)state nonTerminalNamed:(NSString *)nonTerminalName
{
- NSMutableDictionary *row = [table objectAtIndex:state];
+ NSMutableDictionary *row = table[state];
if (nil != [row objectForKey:nonTerminalName] && [[row objectForKey:nonTerminalName] unsignedIntegerValue] != gotoIndex)
{
return NO;
@@ -70,7 +82,7 @@ - (BOOL)setGoto:(NSUInteger)gotoIndex forState:(NSUInteger)state nonTerminalName
- (NSUInteger)gotoForState:(NSUInteger)state rule:(CPRule *)rule
{
- return [(NSNumber *)[(NSDictionary *)[table objectAtIndex:state] objectForKey:[rule name]] unsignedIntegerValue];
+ return [(NSNumber *)[table[state] objectForKey:[rule name]] unsignedIntegerValue];
}
@end

0 comments on commit 7e6e810

Please sign in to comment.