Directly using NSString's "hash" (or equivalent) on unichar pointer
Directly using NSString's "hash" (or equivalent) on unichar pointer
- Subject: Directly using NSString's "hash" (or equivalent) on unichar pointer
- From: Ken Tozier <email@hidden>
- Date: Tue, 4 Mar 2008 13:46:23 -0500
Hi
I'm writing a generic parsing class and have one comparison method
(isLiteral) I'd like to make as fast as possible. The basic question
is: Does NSString use a hashing function that can be accessed without
involving NSString? IE, is there a hashing function that can be used
directly on an input pointer?
Here's the relevant code
TIA
Ken
// This is the "isLiteral" method I want to simplify/speed up
- (BOOL) isLiteral:(NSString *) inKey
{
NSString *literal = [charLiterals objectForKey: inKey],
*testString = [[NSString alloc] initWithCharactersNoCopy: s
length: [literal length]
freeWhenDone: NO];
BOOL result = ([testString isEqualToString: literal]) ? YES : NO ;
[testString release];
return result;
}
Currently, I'm storing string literals in an NSDictionary and
accessing them by the passed in key but I thought it could be made
faster with the following changes
1. Compare hashes rather than the literals themselves in the
"charLiterals" dictionary
2. Use IMPs rather than method calls (isLiteral will be called on
every character of an input string so it's internals need to be as
fast as possible)
3. Use NSString's "hash" method (or an exact functional equivalent)
directly on a unichar pointer.
With these 3 changes, the "isLiteral" method could be rewritten like
// Sets up "literalHash" and "literalLength" instance variables in
preparation for use in a loop
- (void) setCurrentLiteral:(NSString *) inKey
{
NSString *temp = [charLiterals objectForKey: inKey];
literalHash = [temp hash],
literalLength = [temp length];
}
// Compares literalHash to a hash of literalLength unichars from buffPtr
- (BOOL) isCurrentLiteral
{
return (literalHash == HashUnicharPointer(buffPtr, literalLength));
}
// And could be used in a search function like this
- (void) skipToLiteral:(NSString *) inKey
{
[self setCurrentLiteral: inKey];
while ((buffPtr < endPtr) && !isCurrentLiteralIMP(self,
isCurrentLiteralSEL))
{
buffPtr++;
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden