-
Notifications
You must be signed in to change notification settings - Fork 15
/
KLDateSort.m
194 lines (161 loc) · 5.3 KB
/
KLDateSort.m
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// KLDateSort.m
// CalendarModule
//
// Created by Scott Montgomerie on 10-09-14.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "KLDateSort.h"
#import "KLDate.h"
NSInteger KLDateSort(id date, id kldate, void *context)
{
// NSLog(@"KLDateSortComparing %@ with %@", date, kldate);
if ([date isKindOfClass:[NSDate class]] && [kldate isKindOfClass:[NSDate class]])
{
NSLog(@"Can't compare two NSDates... one has to be a KLDate");
// Because the compareWithNSDate function only takes into account dd/mm/yyyy, not time
}
else if ([date isKindOfClass:[KLDate class]])
{
return [((KLDate* )date) compareWithNSDate: ((NSDate*) kldate)];
}
else if ([kldate isKindOfClass:[KLDate class]])
{
return [((KLDate* )kldate) compareWithNSDate: ((NSDate*) date)];
}
return 0;
}
//
// MroBinarySearch.m
//
// Created by Marcus Rohrmoser on 12.01.10.
// Copyright 2010 Marcus Rohrmoser mobile Software. All rights reserved.
//
@implementation MroBinarySearch
#pragma mark Using Selector
- (MroBinarySearch*) initWithArray: (NSArray*) array
{
self = [super init];
if ( self )
{
theArray = array;
}
return self;
}
- (int)count {
return [theArray count];
}
- (id)objectAtIndex:(NSUInteger)index
{
return [theArray objectAtIndex:index];
}
-(NSInteger)binarySearch:(id)key
{
return [self binarySearch:key usingSelector:nil];
}
-(NSInteger)binarySearch:(id)key usingSelector:(SEL)comparator
{
return [self binarySearch:key usingSelector:comparator inRange:NSMakeRange(0, self.count)];
}
-(NSInteger)binarySearch:(id)key usingSelector:(SEL)comparator inRange:(NSRange)range
{
// NSLogD(@"[NSArray(MroBinarySearch) binarySearch:%@ usingSelector:]", key);
if (self.count == 0 || key == nil)
return -1;
if(comparator == nil)
comparator = @selector(compare:);
// check overflow?
NSInteger min = range.location;
NSInteger max = range.location + range.length - 1;
while (min <= max)
{
// http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
const NSInteger mid = min + (max - min) / 2;
switch ((NSComparisonResult)[key performSelector:comparator withObject:[self objectAtIndex:mid]])
{
case NSOrderedSame:
return mid;
case NSOrderedDescending:
min = mid + 1;
break;
case NSOrderedAscending:
max = mid - 1;
break;
}
}
return -(min + 1);
}
#pragma mark Using C-Function
-(NSInteger)binarySearch:(id)key usingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context
{
return [self binarySearch:key usingFunction:comparator context:context inRange:NSMakeRange(0, self.count)];
}
-(NSInteger)binarySearch:(id)key usingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context inRange:(NSRange)range
{
// NSLogD(@"[NSArray(MroBinarySearch) binarySearch:%@ usingFunction:]", key);
if(self.count == 0 || key == nil || comparator == NULL)
return [self binarySearch:key usingSelector:nil inRange:range];
// check overflow?
NSInteger min = range.location;
NSInteger max = range.location + range.length - 1;
while (min <= max)
{
// http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
const NSInteger mid = min + (max - min) / 2;
switch (comparator(key, [self objectAtIndex:mid], context))
{
case NSOrderedSame:
return mid;
case NSOrderedDescending:
min = mid + 1;
break;
case NSOrderedAscending:
max = mid - 1;
break;
}
}
return -(min + 1);
}
#pragma mark Using NSSortDescriptors
-(NSInteger)binarySearch:(id)key usingDescriptors:(NSArray *)sortDescriptors
{
return [self binarySearch:key usingDescriptors:sortDescriptors inRange:NSMakeRange(0, self.count)];
}
/// internal helper
-(NSComparisonResult)_mroInternalCompare:(const NSArray const*)sortDescriptors a:(id)object1 b:(id)object2
{
for (const NSSortDescriptor const *d in sortDescriptors)
{
const NSComparisonResult r = [d compareObject:object1 toObject:object2];
if (r != NSOrderedSame)
return r;
}
return NSOrderedSame;
}
-(NSInteger)binarySearch:(id)key usingDescriptors:(NSArray *)sortDescriptors inRange:(NSRange)range
{
// NSLogD(@"[NSArray(MroBinarySearch) binarySearch:%@ usingDescriptors:]", key);
if (self.count == 0 || key == nil || sortDescriptors == nil || sortDescriptors.count == 0)
return [self binarySearch:key usingSelector:nil inRange:range];
// check overflow?
NSInteger min = range.location;
NSInteger max = range.location + range.length - 1;
while (min <= max)
{
// http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
const NSInteger mid = min + (max - min) / 2;
switch ([self _mroInternalCompare:sortDescriptors a:key b:[self objectAtIndex:mid]])
{
case NSOrderedSame:
return mid;
case NSOrderedDescending:
min = mid + 1;
break;
case NSOrderedAscending:
max = mid - 1;
break;
}
}
return -(min + 1);
}
@end