-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ILStockSequence.m
160 lines (120 loc) · 4.23 KB
/
ILStockSequence.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
#import "ILStockSequence.h"
#import "ILSoupEntry.h"
NS_ASSUME_NONNULL_BEGIN
@interface ILStockSequenceEntry : NSObject
@property(retain) NSString* entryHash; // at the time
@property(retain) NSNumber* entryValue;
@property(retain) NSDate* entryDate;
@end
// MARK: -
@implementation ILStockSequenceEntry
@end
// MARK: -
@interface ILStockSequence ()
@property(retain) NSString* sequencePathStorage;
@property(retain) NSMutableDictionary<NSString*,id>* sequenceStorage;
@end
// MARK: -
@implementation ILStockSequence
+ (instancetype) sequenceWithPath:(NSString*) sequencePath
{
ILStockSequence* stockSequence = ILStockSequence.new;
stockSequence.sequencePathStorage = sequencePath;
stockSequence.sequenceStorage = NSMutableDictionary.new;
return stockSequence;
}
// MARK: - Properties
- (NSString*) sequencePath
{
return self.sequencePathStorage;
}
- (void) sequenceEntry:(id<ILSoupEntry>) entry atTime:(NSDate*) timeIndex
{
id value = [entry.entryKeys valueForKeyPath:self.sequencePath];
NSNumber* numberValue = nil;
if ([value isKindOfClass:NSNumber.class]) {
numberValue = (NSNumber*) value;
}
else if ([value respondsToSelector:@selector(doubleValue)]) {
numberValue = @([value doubleValue]);
}
if (numberValue != nil) {
ILStockSequenceEntry* sequencyEntry = [ILStockSequenceEntry new];
sequencyEntry.entryHash = entry.entryHash;
sequencyEntry.entryDate = timeIndex;
sequencyEntry.entryValue = numberValue;
NSMutableArray* sequence = self.sequenceStorage[entry.entryKeys[ILSoupEntryIdentityUUID]];
if (!sequence) {
sequence = NSMutableArray.new;
self.sequenceStorage[entry.entryKeys[ILSoupEntryIdentityUUID]] = sequence;
}
[sequence addObject:sequencyEntry];
}
// else report sequence error?
}
- (void) removeEntry:(id<ILSoupEntry>) entry
{
[self.sequenceStorage removeObjectForKey:entry.entryKeys[ILSoupEntryIdentityUUID]];
}
- (BOOL) includesEntry:(id<ILSoupEntry>) entry
{
return [self.sequenceStorage.allKeys containsObject:entry.entryKeys[ILSoupEntryIdentityUUID]];
}
// MARK: - fetching sequence data
- (BOOL) fetchSequenceFor:(id<ILSoupEntry>) entry times:(NSArray<NSDate*>**) timeArray values:(NSArray<NSNumber*>**) valueArray
{
NSArray<ILStockSequenceEntry*>* sequence = [self.sequenceStorage[entry.entryKeys[ILSoupEntryIdentityUUID]] copy]; // snapshot
NSMutableArray* timeSequence = [NSMutableArray arrayWithCapacity:sequence.count];
NSMutableArray* valueSequence = [NSMutableArray arrayWithCapacity:sequence.count];
NSUInteger index = 0;
for (ILStockSequenceEntry* entry in sequence) {
timeSequence[index] = entry.entryDate;
valueSequence[index] = entry.entryValue;
index++;
}
*timeArray = timeSequence;
*valueArray = valueSequence;
return (sequence != nil);
}
- (nullable id<ILSoupSequenceSource>) fetchSequenceSourceFor:(id<ILSoupEntry>) entry
{
ILStockSequenceSource* source;
// NSArray<NSDate*>* sequenceTimes;
// NSArray<NSNumber*>* sequenceValues;
//
// if ([self fetchSequenceFor:entry times:&sequenceTimes value:&sequenceValues]) {
// source = [ILStockSequenceSource sequencSourceWithTimes:sequenceTimes andValues:sequenceValues];
// }
return source;
}
// MARK: - NSObject
- (NSString*) description
{
return [NSString stringWithFormat:@"%@ %@ %lu entries", self.class, self.sequencePath, self.sequenceStorage.allKeys.count];
}
@end
// MARK: -
@interface ILStockSequenceSource ()
@property(nonatomic, retain) NSArray<NSDate*>* sequenceDates;
@property(nonatomic, retain) NSArray<NSNumber*>* sequenceValues;
@end
// MARK: -
@implementation ILStockSequenceSource
+ (instancetype) sequencSourceWithTimes:(NSArray<NSDate*>*) seqenceTimes andValues:(NSArray<NSNumber*>*) sequenceValues;
{
ILStockSequenceSource* stockSource = ILStockSequenceSource.new;
stockSource.sequenceDates = seqenceTimes;
stockSource.sequenceValues = sequenceValues;
return stockSource;
}
// MARK: -
- (NSArray<NSDate*>*) sampleDates
{
return self.sequenceDates;
}
- (CGFloat)sampleValueAtIndex:(NSUInteger)index
{
return [self.sequenceValues[index] doubleValue];
}
@end
NS_ASSUME_NONNULL_END