Skip to content
Browse files

State match descriptors complete.

  • Loading branch information...
1 parent 4515f07 commit f6a66d7ab478619a54a2b8d29f4c0ade6f31bd53 Jim Dovey committed
View
5 AQAppStateMachine/AQAppStateMachine.m
@@ -8,6 +8,7 @@
#import "AQAppStateMachine.h"
#import "AQRange.h"
+#import "AQStateMatchingDescriptor.h"
#import <dispatch/dispatch.h>
@implementation AQAppStateMachine
@@ -51,7 +52,8 @@ - (void) notifyForChangesToStateBitAtIndex: (NSUInteger) index usingBlock: (void
- (void) notifyForChangesToStateBitsInRange: (NSRange) range usingBlock: (void (^)(void)) block
{
// create match descriptor and store it
- id desc = nil; // TODO: implement
+ id desc = [[AQStateMatchingDescriptor alloc] initWithRange: range matchingMask: nil];
+
[_matchDescriptors addObject: desc];
[_notifierLookup setObject: block forKey: [desc uniqueID]];
@@ -95,6 +97,7 @@ static inline NSUInteger HighestOneBit64(UInt64 x)
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
+ x |= x >> 32;
return ( (NSUInteger)(x & ~(x >> 1)) );
}
View
4 AQAppStateMachine/AQStateMatchingDescriptor.h
@@ -10,7 +10,7 @@
@class AQBitfield;
-@interface AQStateMatchingDescriptor : NSObject
+@interface AQStateMatchingDescriptor : NSObject <NSCopying>
{
NSString * _uuid;
NSIndexSet * _matchingIndices;
@@ -23,6 +23,8 @@
- (BOOL) matchesRange: (NSRange) range;
+- (NSComparisonResult) compare: (AQStateMatchingDescriptor *) other;
+
@end
@interface AQStateMatchingDescriptor (CreationConvenience)
View
49 AQAppStateMachine/AQStateMatchingDescriptor.m
@@ -60,7 +60,54 @@ - (id) initWithRanges: (NSIndexSet *) ranges matchingMasks: (NSArray *) masks
- (BOOL) matchesRange: (NSRange) range
{
- return ( [_matchingIndices countOfIndexesInRange: range] > 0 );
+ return ( [_matchingIndices intersectsIndexesInRange: range] );
+}
+
+- (id) copyWithZone: (NSZone *) zone
+{
+ AQStateMatchingDescriptor * theCopy = [[[self class] alloc] init];
+ theCopy->_uuid = [_uuid copy];
+ theCopy->_matchingIndices = [_matchingIndices copy];
+ return ( theCopy );
+}
+
+- (BOOL) isEqual: (id) object
+{
+ if ( [object isKindOfClass: [AQStateMatchingDescriptor class]] == NO )
+ return ( NO );
+
+ AQStateMatchingDescriptor * other = (AQStateMatchingDescriptor *)object;
+ return ( [_matchingIndices isEqual: other->_matchingIndices] );
+}
+
+- (NSComparisonResult) compare: (AQStateMatchingDescriptor *) other
+{
+ __block NSUInteger otherIdx = [other->_matchingIndices firstIndex];
+ __block NSComparisonResult result = NSOrderedSame;
+ [_matchingIndices enumerateIndexesUsingBlock: ^(NSUInteger idx, BOOL *stop) {
+ if ( idx == otherIdx )
+ {
+ otherIdx = [other->_matchingIndices indexGreaterThanIndex: otherIdx];
+ return; // continue comparison
+ }
+ else if ( otherIdx == NSNotFound )
+ {
+ result = NSOrderedDescending;
+ *stop = YES;
+ return;
+ }
+
+ result = (idx < otherIdx ? NSOrderedAscending : NSOrderedDescending);
+ *stop = YES;
+ }];
+
+ if ( result != NSOrderedSame )
+ return ( result );
+
+ if ( otherIdx != NSNotFound )
+ return ( NSOrderedAscending );
+
+ return ( NSOrderedSame );
}
@end

0 comments on commit f6a66d7

Please sign in to comment.
Something went wrong with that request. Please try again.