Permalink
Browse files

Something didn't commit…

  • Loading branch information...
Jim Dovey
Jim Dovey committed Jun 28, 2011
1 parent 03421a4 commit 19a4e5a8fc7a1d571f89a42acbd150a3d2e92fe2
Showing with 19 additions and 107 deletions.
  1. +5 −27 AQAppStateMachine/AQStateMaskMatchingDescriptor.h
  2. +14 −80 AQAppStateMachine/AQStateMaskMatchingDescriptor.m
@@ -1,5 +1,5 @@
//
-// AQStateMatchingDescriptor.h
+// AQStateMaskMatchingDescriptor.h
// AQAppStateMachine
//
// Created by Jim Dovey on 11-06-17.
@@ -34,17 +34,14 @@
//
#import <Foundation/Foundation.h>
+#import "AQStateMatchingDescriptor.h"
@class AQBitfield;
/**
- A class describing a comparison match for an AQBitfield.
+ A class describing a masked comparison match for an AQBitfield.
*/
-@interface AQStateMatchingDescriptor : NSObject <NSCopying>
-{
- NSString * _uuid;
- NSIndexSet * _matchingIndices;
-}
+@interface AQStateMaskMatchingDescriptor : AQStateMatchingDescriptor <NSCopying>
/**
Initialize a new descriptor.
@@ -57,29 +54,10 @@
*/
- (id) initWithRanges: (NSArray *) ranges matchingMasks: (NSArray *) masks;
-/// A unique identifier for this descriptor.
-@property (nonatomic, readonly) NSString * uniqueID;
-/// The full range covered by this descriptor.
-@property (nonatomic, readonly) NSRange fullRange;
-
-/**
- Determine whether a descriptor checks any bits within a specific range.
- @param range The range of bits to compare.
- @result `YES` if the descriptor checks bits within _range_, `NO` otherwise.
- */
-- (BOOL) matchesRange: (NSRange) range;
-
-/**
- Compare two descriptors.
- @param other The descriptor against which to compare the receiver.
- @return An `NSComparisonResult` indicating the relative sort ordering of the two descriptors.
- */
-- (NSComparisonResult) compare: (AQStateMatchingDescriptor *) other;
-
@end
/// Convenience functions for creating descriptors for single range/mask pairs.
-@interface AQStateMatchingDescriptor (CreationConvenience)
+@interface AQStateMaskMatchingDescriptor (CreationConvenience)
/**
Initialize a descriptor using a single range and mask.
@@ -1,5 +1,5 @@
//
-// AQStateMatchingDescriptor.m
+// AQStateMaskMatchingDescriptor.m
// AQAppStateMachine
//
// Created by Jim Dovey on 11-06-17.
@@ -33,33 +33,22 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
-#import "AQStateMatchingDescriptor.h"
+#import "AQStateMaskMatchingDescriptor.h"
#import "AQRange.h"
#import "AQBitfield.h"
#import "AQRangeMethods.h"
-@implementation AQStateMatchingDescriptor
-
-@synthesize uniqueID=_uuid;
+@implementation AQStateMaskMatchingDescriptor
- (id) initWithRanges: (NSArray *) ranges matchingMasks: (NSArray *) masks
{
NSParameterAssert([masks count] == 0 || [ranges count] == [masks count]);
- self = [super init];
+ self = [super initWithRanges: ranges];
if ( self == nil )
return ( nil );
- // create a unique identifier
- CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
-#if USING_ARC
- _uuid = CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuid));
-#else
- _uuid = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
-#endif
- CFRelease(uuid);
-
- NSMutableIndexSet * indices = [NSMutableIndexSet new];
+ NSMutableIndexSet * indices = [_matchingIndices mutableCopy];
[ranges enumerateObjectsUsingBlock: ^(__strong id obj, NSUInteger idx, BOOL *stop) {
AQBitfield * mask = nil;
NSRange range = [obj range];
@@ -72,19 +61,23 @@ - (id) initWithRanges: (NSArray *) ranges matchingMasks: (NSArray *) masks
if ( mask == nil )
{
- [indices addIndexesInRange: range];
+ // superclass has already setup the range for us
+ return;
}
else
{
// have to iterate & check each bit against the mask
for ( NSUInteger i = range.location, j=0; i < NSMaxRange(range); i++, j++ )
{
- if ( [mask bitAtIndex: j] == 1 )
- [indices addIndex: i];
+ if ( [mask bitAtIndex: j] == 0 )
+ [indices removeIndex: i];
}
}
}];
+#if !USING_ARC
+ [_matchingIndices release];
+#endif
_matchingIndices = [indices copy];
#if !USING_ARC
[indices release];
@@ -93,86 +86,27 @@ - (id) initWithRanges: (NSArray *) ranges matchingMasks: (NSArray *) masks
return ( self );
}
-#if !USING_ARC
-- (void) dealloc
-{
- [_uuid release];
- [_matchingIndices release];
- [super dealloc];
-}
-#endif
-
-- (NSRange) fullRange
-{
- NSUInteger first = [_matchingIndices firstIndex];
- NSUInteger last = [_matchingIndices lastIndex];
-
- if ( first == NSNotFound )
- return ( NSMakeRange(NSNotFound, 0) );
-
- return ( NSMakeRange(first, (last - first) + 1) );
-}
-
- (BOOL) matchesRange: (NSRange) range
{
return ( [_matchingIndices intersectsIndexesInRange: range] );
}
- (id) copyWithZone: (NSZone *) zone
{
- AQStateMatchingDescriptor * theCopy = [[[self class] alloc] init];
+ AQStateMaskMatchingDescriptor * 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 );
-}
-
- (NSString *) description
{
return ( [NSString stringWithFormat: @"%@{uniqueID=%@, matchingIndices=%@}", [super description], _uuid, _matchingIndices] );
}
@end
-@implementation AQStateMatchingDescriptor (CreationConvenience)
+@implementation AQStateMaskMatchingDescriptor (CreationConvenience)
- (id) initWithRange: (NSRange) range matchingMask: (AQBitfield *) mask
{

0 comments on commit 19a4e5a

Please sign in to comment.