Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #8 from plumbojumbo/similarity-search-profiling
Browse files Browse the repository at this point in the history
Similarity search profiling
  • Loading branch information
ameingast committed Dec 8, 2016
2 parents b0cde81 + 6150f85 commit 01b366d
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 114 deletions.
32 changes: 31 additions & 1 deletion CocoaImageHashing.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
CA7413AC1BD0E7FB00C816B5 /* OSTypes.m in Sources */ = {isa = PBXBuildFile; fileRef = CA963C131BCFCC6200D74D22 /* OSTypes.m */; };
CA7413AD1BD0E7FB00C816B5 /* OSImageHashing.m in Sources */ = {isa = PBXBuildFile; fileRef = CADF6C731BC8F3820039E5A8 /* OSImageHashing.m */; };
CA7413AF1BD0E81700C816B5 /* OSAHash.h in Headers */ = {isa = PBXBuildFile; fileRef = CADF6C691BC8F1DD0039E5A8 /* OSAHash.h */; };
CA7413B01BD0E81700C816B5 /* OSDHash.h in Headers */ = {isa = PBXBuildFile; fileRef = CADF6C5F1BC8F0370039E5A8 /* OSDHash.h */; settings = {ATTRIBUTES = (Public, ); }; };
CA7413B01BD0E81700C816B5 /* OSDHash.h in Headers */ = {isa = PBXBuildFile; fileRef = CADF6C5F1BC8F0370039E5A8 /* OSDHash.h */; };
CA7413B11BD0E81700C816B5 /* OSPHash.h in Headers */ = {isa = PBXBuildFile; fileRef = CADF6C6E1BC8F1E40039E5A8 /* OSPHash.h */; };
CA7413B21BD0E81700C816B5 /* CocoaImageHashing.h in Headers */ = {isa = PBXBuildFile; fileRef = CADF6BB51BC8EF9A0039E5A8 /* CocoaImageHashing.h */; settings = {ATTRIBUTES = (Public, ); }; };
CA7413B41BD0E81700C816B5 /* OSTypes.h in Headers */ = {isa = PBXBuildFile; fileRef = CA963C111BCFCBD500D74D22 /* OSTypes.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -239,6 +239,10 @@
CAEA26681BD110C200380B76 /* OSSimilaritySearch.m in Sources */ = {isa = PBXBuildFile; fileRef = CAEA26621BD110C200380B76 /* OSSimilaritySearch.m */; };
CAEA26691BD110C200380B76 /* OSSimilaritySearch.m in Sources */ = {isa = PBXBuildFile; fileRef = CAEA26621BD110C200380B76 /* OSSimilaritySearch.m */; };
CAEA266A1BD110C200380B76 /* OSSimilaritySearch.m in Sources */ = {isa = PBXBuildFile; fileRef = CAEA26621BD110C200380B76 /* OSSimilaritySearch.m */; };
F7E65B401D99A614004A7B31 /* OSTypes+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = F7E65B3F1D99A614004A7B31 /* OSTypes+Internal.h */; };
F7E65B411D99A614004A7B31 /* OSTypes+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = F7E65B3F1D99A614004A7B31 /* OSTypes+Internal.h */; };
F7E65B421D99A614004A7B31 /* OSTypes+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = F7E65B3F1D99A614004A7B31 /* OSTypes+Internal.h */; };
F7E65B431D99A614004A7B31 /* OSTypes+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = F7E65B3F1D99A614004A7B31 /* OSTypes+Internal.h */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -579,6 +583,7 @@
CAE958EF1BCAD7D30061A909 /* OSCategories.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OSCategories.m; path = CocoaImageHashing/OSCategories.m; sourceTree = "<group>"; };
CAEA26611BD110C200380B76 /* OSSimilaritySearch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OSSimilaritySearch.h; path = CocoaImageHashing/OSSimilaritySearch.h; sourceTree = "<group>"; };
CAEA26621BD110C200380B76 /* OSSimilaritySearch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OSSimilaritySearch.m; path = CocoaImageHashing/OSSimilaritySearch.m; sourceTree = "<group>"; };
F7E65B3F1D99A614004A7B31 /* OSTypes+Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "OSTypes+Internal.h"; path = "CocoaImageHashing/OSTypes+Internal.h"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -752,6 +757,7 @@
CADF6C641BC8F15A0039E5A8 /* OSImageHashing.h */,
CADF6C731BC8F3820039E5A8 /* OSImageHashing.m */,
CA963C111BCFCBD500D74D22 /* OSTypes.h */,
F7E65B3F1D99A614004A7B31 /* OSTypes+Internal.h */,
CA963C131BCFCC6200D74D22 /* OSTypes.m */,
);
name = Source;
Expand Down Expand Up @@ -930,6 +936,7 @@
CA74138E1BD0E65100C816B5 /* OSAHash.h in Headers */,
CA74138F1BD0E65100C816B5 /* OSDHash.h in Headers */,
CAEA26641BD110C200380B76 /* OSSimilaritySearch.h in Headers */,
F7E65B411D99A614004A7B31 /* OSTypes+Internal.h in Headers */,
CA7413901BD0E65100C816B5 /* OSPHash.h in Headers */,
CA7413911BD0E65100C816B5 /* CocoaImageHashing.h in Headers */,
CA7413931BD0E65100C816B5 /* OSTypes.h in Headers */,
Expand All @@ -947,6 +954,7 @@
CA7413AF1BD0E81700C816B5 /* OSAHash.h in Headers */,
CA7413B01BD0E81700C816B5 /* OSDHash.h in Headers */,
CAEA26661BD110C200380B76 /* OSSimilaritySearch.h in Headers */,
F7E65B431D99A614004A7B31 /* OSTypes+Internal.h in Headers */,
CA7413B11BD0E81700C816B5 /* OSPHash.h in Headers */,
CA7413B21BD0E81700C816B5 /* CocoaImageHashing.h in Headers */,
CA7413B41BD0E81700C816B5 /* OSTypes.h in Headers */,
Expand All @@ -964,6 +972,7 @@
CA7413CD1BD0E88300C816B5 /* OSAHash.h in Headers */,
CA7413CE1BD0E88300C816B5 /* OSDHash.h in Headers */,
CAEA26651BD110C200380B76 /* OSSimilaritySearch.h in Headers */,
F7E65B421D99A614004A7B31 /* OSTypes+Internal.h in Headers */,
CA7413CF1BD0E88300C816B5 /* OSPHash.h in Headers */,
CA7413D01BD0E88300C816B5 /* CocoaImageHashing.h in Headers */,
CA7413D21BD0E88300C816B5 /* OSTypes.h in Headers */,
Expand All @@ -981,6 +990,7 @@
CA95045F1BCD9A1000305210 /* OSFastGraphics.h in Headers */,
CAE958F01BCAD7D30061A909 /* OSCategories.h in Headers */,
CAEA26631BD110C200380B76 /* OSSimilaritySearch.h in Headers */,
F7E65B401D99A614004A7B31 /* OSTypes+Internal.h in Headers */,
CADF6C701BC8F1E40039E5A8 /* OSPHash.h in Headers */,
CA963C121BCFCBD500D74D22 /* OSTypes.h in Headers */,
CADF6C661BC8F15A0039E5A8 /* OSImageHashing.h in Headers */,
Expand Down Expand Up @@ -1456,6 +1466,14 @@
"DEBUG=1",
"$(inherited)",
);
"GCC_PREPROCESSOR_DEFINITIONS[sdk=iphoneos10.0]" = (
"OSSPINLOCK_USE_INLINED=1",
"$(inherited)",
);
"GCC_PREPROCESSOR_DEFINITIONS[sdk=macosx10.12]" = (
"OSSPINLOCK_USE_INLINED=1",
"$(inherited)",
);
GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES;
GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES;
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
Expand Down Expand Up @@ -1491,6 +1509,8 @@
"-Wall",
"-Weverything",
"-pedantic",
"-Wno-direct-ivar-access",
"-Wno-objc-interface-ivars",
);
TVOS_DEPLOYMENT_TARGET = 9.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
Expand Down Expand Up @@ -1539,6 +1559,14 @@
GCC_INLINES_ARE_PRIVATE_EXTERN = YES;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = fast;
"GCC_PREPROCESSOR_DEFINITIONS[sdk=iphoneos10.0]" = (
"OSSPINLOCK_USE_INLINED=1",
"$(inherited)",
);
"GCC_PREPROCESSOR_DEFINITIONS[sdk=macosx10.12]" = (
"OSSPINLOCK_USE_INLINED=1",
"$(inherited)",
);
GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES;
GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES;
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
Expand Down Expand Up @@ -1574,6 +1602,8 @@
"-Wall",
"-Weverything",
"-pedantic",
"-Wno-direct-ivar-access",
"-Wno-objc-interface-ivars",
);
TVOS_DEPLOYMENT_TARGET = 9.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
Expand Down
10 changes: 10 additions & 0 deletions CocoaImageHashing/OSAHash.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ @implementation OSAHash

#pragma mark - OSImageHashing Protocol

+ (instancetype)sharedInstance
{
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [self new];
});
return instance;
}

- (OSHashType)hashImageData:(NSData *)imageData
{
NSAssert(imageData, @"Image data must not be null");
Expand Down
14 changes: 10 additions & 4 deletions CocoaImageHashing/OSAbstractHash.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "OSAbstractHash.h"
#import "OSCategories.h"
#import "OSImageHashing.h"
#import "OSTypes+Internal.h"

@implementation OSAbstractHash

Expand Down Expand Up @@ -41,8 +42,7 @@ - (OSHashDistanceType)hashDistance:(OSHashType)leftHand
{
NSAssert(leftHand != OSHashTypeError, @"Left hand hash must not be OSHashTypeError");
NSAssert(rightHand != OSHashTypeError, @"Right hand hash must not be OSHashTypeError");
OSHashDistanceType result = (OSHashDistanceType)__builtin_popcountll((UInt64)leftHand ^ (UInt64)rightHand);
return result;
return OSHammingDistance(leftHand, rightHand);
}

- (BOOL)compareImageData:(NSData *)leftHandImageData
Expand Down Expand Up @@ -94,16 +94,22 @@ - (NSComparisonResult)imageSimilarityComparatorForImageForBaseImageData:(NSData

#pragma mark - Abstract methods

+ (instancetype)sharedInstance
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"Abstract method called."
userInfo:nil];
}

- (OSHashDistanceType)hashDistanceSimilarityThreshold
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"Abstract method called."
userInfo:nil];
}

- (OSHashType)hashImageData:(NSData *)imageData
- (OSHashType)hashImageData:(NSData * OS_UNUSED)imageData
{
OS_MARK_UNUSED(imageData);
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"Abstract method called."
userInfo:nil];
Expand Down
5 changes: 1 addition & 4 deletions CocoaImageHashing/OSCategories.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ NS_ASSUME_NONNULL_BEGIN

- (NSArray<OSTuple<id, id> *> *)arrayWithPairCombinations;

- (NSArray<OSTuple<id, id> *> *)arrayWithPairCombinations:(BOOL (^)(id leftHand, id rightHand))matcher;

- (void)arrayWithPairCombinations:(BOOL (^)(id leftHand, id rightHand))matcher
withResultHandler:(void (^)(id leftHand, id rightHand))resultHandler;
- (void)enumeratePairCombinationsUsingBlock:(void (^)(id __unsafe_unretained leftHand, id __unsafe_unretained rightHand))block;

NS_ASSUME_NONNULL_END

Expand Down
54 changes: 28 additions & 26 deletions CocoaImageHashing/OSCategories.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,47 @@
//

#import "OSCategories.h"
#import "OSTypes.h"
#import "OSTypes+Internal.h"

@import Darwin.libkern.OSAtomic;

#pragma mark - NSArray Category

@implementation NSArray (CocoaImageHashing)

- (NSArray<OSTuple<id, id> *> *)arrayWithPairCombinations
{
NSArray<OSTuple<id, id> *> *result = [self arrayWithPairCombinations:^BOOL(id leftHand, id rightHand) {
OS_MARK_UNUSED(leftHand);
OS_MARK_UNUSED(rightHand);
return YES;
}];
return result;
}

- (NSArray<OSTuple<id, id> *> *)arrayWithPairCombinations:(BOOL (^)(id leftHand, id rightHand))matcher
{
NSMutableArray<OSTuple<id, id> *> *pairs = [NSMutableArray new];
[self arrayWithPairCombinations:matcher
withResultHandler:^(id leftHand, id rightHand) {
OSTuple<id, id> *tuple = [OSTuple tupleWithFirst:leftHand
andSecond:rightHand];
[pairs addObject:tuple];
}];
OSSpinLock volatile __block lock = OS_SPINLOCK_INIT;
[self enumeratePairCombinationsUsingBlock:^(id __unsafe_unretained leftHand, id __unsafe_unretained rightHand) {
OSTuple<id, id> *tuple = [OSTuple tupleWithFirst:leftHand
andSecond:rightHand];
OSSpinLockLock(&lock);
[pairs addObject:tuple];
OSSpinLockUnlock(&lock);
}];
return pairs;
}

- (void)arrayWithPairCombinations:(BOOL (^)(id leftHand, id rightHand))matcher
withResultHandler:(void (^)(id leftHand, id rightHand))resultHandler
- (void)enumeratePairCombinationsUsingBlock:(void (^)(id __unsafe_unretained leftHand, id __unsafe_unretained rightHand))block
{
for (NSUInteger i = 0; i < [self count] - 1; i++) {
for (NSUInteger j = i + 1; j < [self count]; j++) {
BOOL result = matcher(self[i], self[j]);
if (result) {
resultHandler(self[i], self[j]);
}
}
NSUInteger count = [self count];
if (!count) {
return;
}
id __unsafe_unretained *objects = (id __unsafe_unretained *)malloc(sizeof(id) * count);
if (!objects) {
return;
}
[self getObjects:objects range:NSMakeRange(0, count)];
dispatch_apply(count - 1, dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^(size_t i) {
id __unsafe_unretained left = objects[i];
for (NSUInteger j = i + 1; j < count; j++) {
id __unsafe_unretained right = objects[j];
block(left, right);
}
});
free(objects);
}

@end
Expand Down
10 changes: 10 additions & 0 deletions CocoaImageHashing/OSDHash.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ @implementation OSDHash

#pragma mark - OSImageHashing Protocol

+ (instancetype)sharedInstance
{
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [self new];
});
return instance;
}

- (OSHashType)hashImageData:(NSData *)imageData
{
NSAssert(imageData, @"Image data must not be null");
Expand Down
10 changes: 10 additions & 0 deletions CocoaImageHashing/OSPHash.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ @implementation OSPHash

#pragma mark - OSImageHashingProvider

+ (instancetype)sharedInstance
{
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [self new];
});
return instance;
}

- (OSHashType)hashImageData:(NSData *)imageData
{
NSAssert(imageData, @"Image data must not be null");
Expand Down
4 changes: 2 additions & 2 deletions CocoaImageHashing/OSSimilaritySearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2015 Andreas Meingast. All rights reserved.
//

#import "OSTypes.h"
#import "OSTypes+Internal.h"

@interface OSSimilaritySearch : NSObject

Expand All @@ -19,7 +19,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)similarImagesWithProvider:(OSImageHashingProviderId)imageHashingProviderId
withHashDistanceThreshold:(OSHashDistanceType)hashDistanceThreshold
forImageStreamHandler:(OSTuple<OSImageId *, NSData *> * (^)())imageStreamHandler
forResultHandler:(void (^)(OSImageId *leftHandImageId, OSImageId *rightHandImageId))resultHandler;
forResultHandler:(void (^)(OSImageId * __unsafe_unretained leftHandImageId, OSImageId * __unsafe_unretained rightHandImageId))resultHandler;

- (NSArray<OSTuple<OSImageId *, OSImageId *> *> *)similarImagesWithProvider:(OSImageHashingProviderId)imageHashingProviderId
withHashDistanceThreshold:(OSHashDistanceType)hashDistanceThreshold
Expand Down
Loading

0 comments on commit 01b366d

Please sign in to comment.