Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

override -isEqual: and -hash for comparisons

there is some speculation that -hash for a foundation collection just returns a simple measure like count, in which case this change may not be deep enough. also, this does nothing to resolve the asymmetrical nature of NSDate > PEOSCTimeTag > NSDate.
  • Loading branch information...
commit 0028f280c905a899ac9204093d41f1dd5801b3ff 1 parent 3d6332d
@pizthewiz authored
View
17 PonyExpress/PEOSCMessage.m
@@ -383,6 +383,21 @@ + (NSString*)_typeForCode:(NSString*)code {
#pragma mark -
+// via http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html
+#define NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
+#define NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (NSUINT_BIT - howmuch)))
+
+- (BOOL)isEqual:(id)object {
+ if (![object isKindOfClass:[PEOSCMessage class]]) {
+ return NO;
+ }
+ return [object hash] == [self hash];
+}
+
+- (NSUInteger)hash {
+ return NSUINTROTATE(NSUINTROTATE([self.address hash], NSUINT_BIT / 2) ^ [self.typeTags hash], NSUINT_BIT / 2) ^ [self.arguments hash];
+}
+
- (NSString*)description {
NSMutableString* argDescription = [NSMutableString string];
[self.arguments enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) {
@@ -400,6 +415,8 @@ - (NSString*)description {
return [NSString stringWithFormat:@"<%@: %@ %@ [%@]>", NSStringFromClass([self class]), self.address, typeTagString, argDescription];
}
+#pragma mark -
+
- (void)enumerateTypesAndArgumentsUsingBlock:(void (^)(id type, id argument, BOOL* stop))block {
BOOL stop = NO;
NSUInteger argIndex = 0;
View
2  PonyExpressTests/PEOSCMessageSpec.m
@@ -146,7 +146,6 @@
expect(message.address).to.equal(sourceMessage.address);
expect(message.typeTags).to.equal(sourceMessage.typeTags);
expect(message.arguments).to.equal(sourceMessage.arguments);
- // failure due to -[PEOSCMessage hash] not being overridden (through -[PEOSCMessage isEqual:])
expect(message).to.equal(sourceMessage);
});
});
@@ -166,7 +165,6 @@
expect(message.typeTags).to.equal(sourceMessage.typeTags);
// failure due to NSData/TimeTag not being perfectly symmetrical
expect(message.arguments).to.equal(sourceMessage.arguments);
- // failure due to -[PEOSCMessage hash] not being overridden (through -[PEOSCMessage isEqual:])
expect(message).to.equal(sourceMessage);
});
});
View
1  TODO.md
@@ -1,6 +1,5 @@
### GENERAL
-- override -[PEOSCMessage hash] for support of -[PEOSCMessage isEqual], see [mike ash's friday qa article](http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html)
- pull address validation code out to a class method and use within -[PEOSCMessage initWithData:]
- redo static library for iOS in the mannor [descirbed by jamie](http://www.blog.montgomerie.net/easy-xcode-static-library-subprojects-and-submodules) and migrate Example/Tests to it
- go unicast and later add multicast support
Please sign in to comment.
Something went wrong with that request. Please try again.