~ubuntu-branches/ubuntu/trusty/paje.app/trusty

« back to all changes in this revision

Viewing changes to StatViewer/StatArray.m

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-07-07 22:59:54 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060707225954-cxx4jqfru2lvu4og
Tags: 1.4.0-1ubuntu1
* Re-sync with Debian
* Add desktop file from Phil Bull (Closes Malone: #36387)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 1997-2005 Benhur Stein
 
3
    
 
4
    This file is part of Paj�.
 
5
 
 
6
    Paj� is free software; you can redistribute it and/or modify it under
 
7
    the terms of the GNU Lesser General Public License as published by the
 
8
    Free Software Foundation; either version 2 of the License, or (at your
 
9
    option) any later version.
 
10
 
 
11
    Paj� is distributed in the hope that it will be useful, but WITHOUT ANY
 
12
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
    FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 
14
    for more details.
 
15
 
 
16
    You should have received a copy of the GNU Lesser General Public License
 
17
    along with Paj�; if not, write to the Free Software Foundation, Inc.,
 
18
    59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 
19
*/
 
20
/* StatArray.m created by benhur on Fri 26-Sep-1997 */
 
21
 
 
22
#include "StatArray.h"
 
23
#include "../General/Macros.h"
 
24
 
 
25
@interface StatStateArray : StatArray
 
26
{
 
27
    CondensedEntitiesArray *condensedArray;
 
28
    NSDate *startTime;
 
29
    NSDate *endTime;
 
30
}
 
31
 
 
32
- (id)initWithName:(NSString *)theName
 
33
              type:(PajeEntityType *)type
 
34
         startTime:(NSDate *)start
 
35
           endTime:(NSDate *)end
 
36
            filter:(PajeFilter *)f
 
37
  entityEnumerator:(NSEnumerator *)en;
 
38
 
 
39
- (void)addEntitiesFromEnumerator:(NSEnumerator *)enumerator;
 
40
@end
 
41
 
 
42
@implementation StatArray
 
43
 
 
44
+ (StatArray *)stateArrayWithName:(NSString *)theName
 
45
                             type:(PajeEntityType *)type
 
46
                        startTime:(NSDate *)start
 
47
                          endTime:(NSDate *)end
 
48
                           filter:(PajeFilter *)f
 
49
                 entityEnumerator:(NSEnumerator *)en
 
50
{
 
51
    return [[[StatStateArray alloc] initWithName:theName
 
52
                                            type:type
 
53
                                       startTime:start
 
54
                                         endTime:end
 
55
                                          filter:f
 
56
                                entityEnumerator:en] autorelease];
 
57
}
 
58
 
 
59
- (id)initWithName:(NSString *)theName
 
60
              type:(PajeEntityType *)type
 
61
            filter:(PajeFilter *)f
 
62
{
 
63
    self = [super init];
 
64
    if (self != nil) {
 
65
        Assign(name, theName);
 
66
        Assign(entityType, type);
 
67
        filter = f;
 
68
    }
 
69
    return self;
 
70
}
 
71
 
 
72
- (void)dealloc
 
73
{
 
74
    Assign(name, nil);
 
75
    Assign(entityType, nil);
 
76
    [super dealloc];
 
77
}
 
78
 
 
79
- (NSString *)name
 
80
{
 
81
    return name;
 
82
}
 
83
 
 
84
- (double)totalValue
 
85
{
 
86
    [self subclassResponsibility:_cmd];
 
87
    return 0;
 
88
}
 
89
 
 
90
- (double)maxValue
 
91
{
 
92
    [self subclassResponsibility:_cmd];
 
93
    return 0;
 
94
}
 
95
 
 
96
- (double)minValue
 
97
{
 
98
    [self subclassResponsibility:_cmd];
 
99
    return 0;
 
100
}
 
101
 
 
102
- (unsigned)subCount
 
103
{
 
104
    [self subclassResponsibility:_cmd];
 
105
    return 0;
 
106
}
 
107
 
 
108
- (NSString *)subNameAtIndex:(unsigned)index
 
109
{
 
110
    [self subclassResponsibility:_cmd];
 
111
    return nil;
 
112
}
 
113
 
 
114
- (NSColor *)subColorAtIndex:(unsigned)index
 
115
{
 
116
    [self subclassResponsibility:_cmd];
 
117
    return nil;
 
118
}
 
119
 
 
120
- (double)subValueAtIndex:(unsigned)index
 
121
{
 
122
    [self subclassResponsibility:_cmd];
 
123
    return 0;
 
124
}
 
125
 
 
126
@end
 
127
 
 
128
 
 
129
 
 
130
@implementation StatStateArray
 
131
- (id)initWithName:(NSString *)theName
 
132
              type:(PajeEntityType *)type
 
133
         startTime:(NSDate *)start
 
134
           endTime:(NSDate *)end
 
135
            filter:(PajeFilter *)f
 
136
  entityEnumerator:(NSEnumerator *)en
 
137
{
 
138
    self = [super initWithName:theName
 
139
                          type:type
 
140
                        filter:f];
 
141
    if (self != nil) {
 
142
        Assign(startTime, start);
 
143
        Assign(endTime, end);
 
144
        condensedArray = [[CondensedEntitiesArray alloc] init];
 
145
        [self addEntitiesFromEnumerator:en];
 
146
    }
 
147
    return self;
 
148
}
 
149
 
 
150
- (void)dealloc
 
151
{
 
152
    Assign(condensedArray, nil);
 
153
    Assign(startTime, nil);
 
154
    Assign(endTime, nil);
 
155
    [super dealloc];
 
156
}
 
157
 
 
158
 
 
159
- (void)addEntitiesFromEnumerator:(NSEnumerator *)enumerator
 
160
{
 
161
    PajeEntity *entity;
 
162
    int top = -1;
 
163
    BOOL incomplete[100];
 
164
    NSDate *start[100];
 
165
    NSString *names[100];
 
166
    double duration[100];
 
167
 
 
168
    while ((entity = [enumerator nextObject]) != nil) {
 
169
        NSDate *entityStart;
 
170
        NSDate *entityEnd;
 
171
        BOOL entityIsAggregate;
 
172
        BOOL entityIsIncomplete;
 
173
        double entityDuration;
 
174
        double entityDurationInSelection;
 
175
        
 
176
        entityStart = [filter startTimeForEntity:entity];
 
177
        entityEnd = [filter endTimeForEntity:entity];
 
178
        if ([entityStart isLaterThanDate:endTime]
 
179
            || [entityEnd isEarlierThanDate:startTime]) {
 
180
            continue;
 
181
        }
 
182
        //entityDuration = [filter durationForEntity:entity];
 
183
        entityDuration = [entity duration];
 
184
        entityIsAggregate = [filter isAggregateEntity:entity];
 
185
        
 
186
        while (top >= 0 && [entityEnd isEarlierThanDate:start[top]]) {
 
187
            if (incomplete[top] && duration[top] > 0) {
 
188
                [condensedArray addName:names[top]
 
189
                               duration:duration[top]];
 
190
            }
 
191
            top--;
 
192
        }
 
193
 
 
194
        entityIsIncomplete = NO;
 
195
        entityDurationInSelection = entityDuration;
 
196
        if ([entityStart isEarlierThanDate:startTime]) {
 
197
            entityIsIncomplete = YES;
 
198
            entityDurationInSelection
 
199
                              -= [startTime timeIntervalSinceDate:entityStart];
 
200
        }
 
201
        if ([entityEnd isLaterThanDate:endTime]) {
 
202
            entityIsIncomplete = YES;
 
203
            entityDurationInSelection
 
204
                              -= [entityEnd timeIntervalSinceDate:endTime];
 
205
        }
 
206
        
 
207
        if (!entityIsAggregate) {
 
208
            NSString *entityName;
 
209
            entityName = [filter nameForEntity:entity];
 
210
 
 
211
            if (top >= 0 && incomplete[top]) {
 
212
                duration[top] -= entityDurationInSelection;
 
213
            }
 
214
            top++;
 
215
            incomplete[top] = entityIsIncomplete;
 
216
            start[top] = entityStart;
 
217
            if (entityIsIncomplete) {
 
218
                names[top] = entityName;
 
219
                duration[top] = entityDurationInSelection;
 
220
            } else {
 
221
                double entityExclusiveDuration;
 
222
                //entityExclusiveDuration = [filter exclusiveDurationForEntity:entity];
 
223
                entityExclusiveDuration = [entity exclusiveDuration];
 
224
                [condensedArray addName:entityName
 
225
                               duration:entityExclusiveDuration];
 
226
            }
 
227
            //condensedEntitiesCount ++;
 
228
        } else {
 
229
            double correctionFactor;
 
230
            double correctedDuration;
 
231
            double entityExclusiveDuration;
 
232
            //entityExclusiveDuration = [filter exclusiveDurationForEntity:entity];
 
233
            entityExclusiveDuration = [entity exclusiveDuration];
 
234
            correctionFactor = entityDurationInSelection / entityDuration;
 
235
            correctedDuration = (entityDuration - entityExclusiveDuration)
 
236
                              * correctionFactor;
 
237
            if (correctedDuration > entityDurationInSelection) {
 
238
                correctedDuration = entityDurationInSelection;
 
239
            }
 
240
            if (top >= 0 && incomplete[top]) {
 
241
                duration[top] -= correctedDuration;
 
242
            }
 
243
            // BUG: should be corrected by correctionFactor
 
244
            [condensedArray addArray:[entity condensedEntities]];
 
245
            //[condensedArray addArray:[filter condensedEntitiesForEntity:entity]];
 
246
            //condensedEntitiesCount += [entity condensedEntitiesCount];
 
247
        }
 
248
        
 
249
    }
 
250
    while (top >= 0) {
 
251
        if (incomplete[top]
 
252
            && duration[top]/[endTime timeIntervalSinceDate:startTime] > 1e-5) {
 
253
            [condensedArray addName:names[top]
 
254
                           duration:duration[top]];
 
255
        }
 
256
        top--;
 
257
    }
 
258
}
 
259
 
 
260
 
 
261
- (double)totalValue
 
262
{
 
263
    return [endTime timeIntervalSinceDate:startTime];
 
264
}
 
265
 
 
266
- (double)maxValue
 
267
{
 
268
    return [self subValueAtIndex:0];
 
269
}
 
270
 
 
271
- (double)minValue
 
272
{
 
273
    return [self subValueAtIndex:[self subCount] - 1];
 
274
}
 
275
 
 
276
 
 
277
- (unsigned)subCount
 
278
{
 
279
    return [condensedArray count];
 
280
}
 
281
 
 
282
- (NSString *)subNameAtIndex:(unsigned)i
 
283
{
 
284
    return [condensedArray nameAtIndex:i];
 
285
}
 
286
 
 
287
- (NSColor *)subColorAtIndex:(unsigned)i
 
288
{
 
289
    return [filter colorForName:[condensedArray nameAtIndex:i]
 
290
                   ofEntityType:entityType];
 
291
}
 
292
 
 
293
- (double)subValueAtIndex:(unsigned)i
 
294
{
 
295
    return [condensedArray durationAtIndex:i];
 
296
}
 
297
@end