-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRateSet.m
65 lines (53 loc) · 1.41 KB
/
RateSet.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
//
// RateSet.m
// Flowrate
//
// Created by Nathan Vander Wilt on 12/8/09.
// Copyright 2009 Calf Trail Software, LLC. All rights reserved.
//
#import "RateSet.h"
@implementation RateSet
- (id)initWithItems:(NSArray*)theItems {
self = [super init];
if (self) {
items = [theItems copy];
size_t statesSize = [items count] * sizeof(RateStatus);
itemStates = NSAllocateCollectable(statesSize, 0);
bzero(itemStates, statesSize);
}
return self;
}
+ (RateSet*)rateSetWithItems:(NSArray*)theItems {
RateSet* rs = [[RateSet alloc] initWithItems:theItems];
return [rs autorelease];
}
- (NSUInteger)count {
return [items count];
}
- (id)itemAtIndex:(NSUInteger)itemIdx {
return [items objectAtIndex:itemIdx];
}
- (RateStatus)itemStatusAtIndex:(NSUInteger)itemIdx {
NSAssert2(itemIdx < [items count],
@"Index out of range (%lu >= %lu)", itemIdx, [items count]);
return itemStates[itemIdx];
}
- (void)setItemStatus:(RateStatus)newRateStatus
atIndex:(NSUInteger)itemIdx
{
NSAssert2(itemIdx < [items count],
@"Index out of range (%lu >= %lu)", itemIdx, [items count]);
itemStates[itemIdx] = newRateStatus;
}
- (NSArray*)itemsWithStatus:(RateStatus)desiredStatus {
NSMutableArray* filteredItems = [NSMutableArray array];
NSUInteger itemIdx = 0;
for (id item in items) {
if (itemStates[itemIdx] == desiredStatus) {
[filteredItems addObject:item];
}
++itemIdx;
}
return filteredItems;
}
@end