-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAccumulator.m
116 lines (99 loc) · 2.66 KB
/
Accumulator.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
//
// Accumulator.m
// Accumulator
//
// Created by Jesus Renero on 02/03/13.
// Copyright (c) 2013 Jesus Renero. All rights reserved.
//
#import "Accumulator.h"
@implementation Accumulator
@synthesize name, data;
- (id)initWithName:(NSString *)initName {
if ( self = [super init] )
{
if ( initName != nil) {
name = [[NSString alloc] initWithString:initName];
return self;
} else {
return nil;
}
} else return nil;
}
-(id) initWithData:(NSArray *)initData
{
if ( self = [super init] )
{
if ( [initData count] > 0) {
data = [[NSMutableArray alloc] initWithArray:initData copyItems:YES];
return self;
} else {
return nil;
}
} else return nil;
}
-(id) initWithNameAndData:(NSString *)initName data:(NSArray *)initData
{
if ( self = [super init] )
{
if ( [initData count] > 0) {
data = [[NSMutableArray alloc] initWithArray:initData copyItems:YES];
name = [[NSString alloc] initWithString:initName];
return self;
} else {
return nil;
}
} else
return nil;
}
-(void)logAccumulator
{
NSLog(@"Histogram name: %@", name);
for (int i=0;i<[data count];i++) {
NSLog(@"H[%d]: %@", i, [data objectAtIndex:i]);
}
}
- (void)setAccName:(NSString *)newName {
if (newName == NULL) return;
name = [[NSString alloc] initWithString:newName];
}
- (float)valueAtIndex:(int)index {
if ((index < 0) || (index >= [data count])) {
return 0.0f;
}
return [[data objectAtIndex:index] floatValue];
}
- (void)setValue:(float)value atIndex:(int)index
{
if ((index < 0) || (index >= [data count])) {
return;
}
NSNumber *number = [[NSNumber alloc] initWithFloat:value];
[data setObject:number atIndexedSubscript:index];
return;
}
- (void)addValue:(float)value atIndex:(int)index
{
if ((index < 0) || (index >= [data count])) {
return;
}
NSNumber *oldValue = [data objectAtIndex:index];
float sum = [oldValue floatValue] + value;
NSNumber *newValue = [[NSNumber alloc] initWithFloat:sum];
[data setObject:newValue atIndexedSubscript:index];
return;
}
- (void)addNumber:(NSNumber *)value atIndex:(int)index
{
if ((index < 0) || (index >= [data count])) {
return;
}
NSNumber *oldValue = [data objectAtIndex:index];
float sum = [oldValue floatValue] + [value floatValue];
NSNumber *newValue = [[NSNumber alloc] initWithFloat:sum];
[data setObject:newValue atIndexedSubscript:index];
return;
}
- (long) countOfData {
return [data count];
}
@end