forked from natikgadzhi/XNMaths
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XNAbstractFunction.m
118 lines (89 loc) · 2.79 KB
/
XNAbstractFunction.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
//
// XNAbstractFunction.m
// XNMaths
//
// Created by Нат Гаджибалаев on 23.12.09.
// Copyright 2009 Нат Гаджибалаев. All rights reserved.
//
#import "XNAbstractFunction.h"
#import "GCMathParser.h"
@implementation XNAbstractFunction
//
// Synthesize properties.
@synthesize expression, arguments;
//
// Class initialization methods.
+ (XNAbstractFunction*) functionWithExpression: (NSString*)aExpression
{
return [[[XNAbstractFunction alloc] initWithExpression: aExpression] autorelease];
}
+ (XNAbstractFunction*) functionWithExpression: (NSString*)aExpression andArguments: (NSArray *)anArgumentNames
{
return [[[XNAbstractFunction alloc] initWithExpression: aExpression andArguments: anArgumentNames] autorelease];
}
#pragma mark -
#pragma mark Instance inititalization methods
//
// Initialize with just an expression.
- (XNAbstractFunction*) initWithExpression: (NSString*)aExpression
{
self = [super init];
// init an expression
expression = [aExpression copy];
// init the parser
parser = [[GCMathParser parser] retain];
// init an empty arguments dictionary
// 3 elements will be ok in most cases.
arguments = [[NSMutableDictionary alloc] initWithCapacity: 3];
return self;
}
- (XNAbstractFunction*) initWithExpression: (NSString*)aExpression andArguments: (NSArray *)anArgumentNames
{
self = [super init];
// init an expression
expression = [aExpression copy];
// init the parser
parser = [[GCMathParser parser] retain];
// init an empty arguments dictionary
arguments = [[NSMutableDictionary alloc] initWithCapacity: [anArgumentNames count]];
// copy the arguments
for( NSString *argName in anArgumentNames ){
[arguments setObject: [NSNumber numberWithFloat: 0.] forKey: argName];
}
return self;
}
#pragma mark -
#pragma mark Working with values and arguments
- (void) setValue: (CGFloat) aValue forArgument: (NSString *)anArgumentName
{
if( ![[arguments allKeys] containsObject: anArgumentName]){
[NSException raise: @"XNAbstractFunction unknown argument error."
format: @"Argument %@ is unknown in %@ function", anArgumentName, expression];
}
[arguments setObject: [NSNumber numberWithFloat: aValue] forKey: anArgumentName];
}
- (CGFloat) valueWithArguments: (NSDictionary *) anArgumentsDictionary
{
[arguments release];
arguments = [anArgumentsDictionary retain];
return [self valueWithCurrentArguments];
}
- (CGFloat) valueWithCurrentArguments
{
NSEnumerator *enumerator = [arguments keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
[parser setSymbolValue: [[arguments objectForKey: key] floatValue] forKey:key];
}
return [parser evaluate:expression];
}
//
// Deallocate instance variables
- (void) dealloc
{
[expression release];
[parser release];
[arguments release];
[super dealloc];
}
@end