-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBKNode.m
More file actions
78 lines (65 loc) · 1.98 KB
/
BKNode.m
File metadata and controls
78 lines (65 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// BKNode.m
//
// Created by Fabio Batista on 11/23/12.
// Copyright (c) 2012 Fabio Batista. All rights reserved.
// @fbatista & @webreakstuff
#import "BKNode.h"
@implementation BKNode
@synthesize value;
@synthesize children;
- (id)init
{
if (self = [super init]) {
self.children = [[NSMutableDictionary alloc] init];
}
return self;
}
- (id)initWithValue:(id<BKHashableValue>)theValue
{
if (self = [self init]) {
self.value = theValue;
}
return self;
}
/*
Returns the number of inserted nodes (0 or 1)
*/
- (NSUInteger)insertValue:(id<BKHashableValue>)candidate
{
NSUInteger dist = [self.value distanceToValue:candidate];
// If the distance is zero, they're the same objects so don't insert
if (dist != 0) {
//If there is already a child with the same distance, insert it on that child
BKNode* child = nil;
NSNumber* distKey = [NSNumber numberWithInt:dist];
if ((child = [children objectForKey:distKey]) != nil) {
return [child insertValue:candidate];
} else {
// our candidate found a home as one of our children!
[children setObject:[[BKNode alloc] initWithValue:candidate] forKey:distKey];
return 1;
}
}
return 0;
}
- (NSArray*)find:(id<BKHashableValue>)candidate withThreshold:(NSUInteger)threshold
{
NSUInteger dist = [self.value distanceToValue:candidate];
[self.value setScore:dist];
NSMutableArray* results = [[NSMutableArray alloc] init];
if (dist <= threshold) {
[results addObject:self.value];
}
NSInteger dmin = dist - threshold;
NSInteger dmax = dist + threshold;
for (int i = dmin; i <= dmax; i++) {
BKNode* child = nil;
NSNumber* i_key = [[NSNumber alloc]initWithInt:i];
if ((child = [children objectForKey:i_key]) != nil) {
[results addObjectsFromArray:[child find:candidate withThreshold:threshold]];
}
}
return results;
}
@end