-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuleArray.cpp
273 lines (227 loc) · 4.97 KB
/
RuleArray.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//
// File: RuleArray.cpp
//
// Purpose: Implementation of the RuleArray class. This class represents
// the rules associated with a set of variables.
//
// Copyright � 1999-2001 Louder Than A Bomb! Software
//
// This file is part of the FFLL (Free Fuzzy Logic Library) project (http://ffll.sourceforge.net)
// It is released under the BSD license, see http://ffll.sourceforge.net/license.txt for the full text.
//
#include "RuleArray.h"
#include "FuzzyModelBase.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#include "debug.h"
#endif
//
// Function: RuleArray()
//
// Purpose: default constructor
//
// Arguments:
//
// FuzzyModelBase* _parent - model that these rules belong to
//
// Returns:
//
// nothing
//
// Author: Michael Zarozinski
// Date: 6/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
RuleArray::RuleArray(FuzzyModelBase* _parent) : FFLLBase(_parent)
{
rules = NULL;
max = 0;
}; // end RuleArray::RuleArray()
//
// Function: ~RuleArray()
//
// Purpose: default destructor
//
// Arguments:
//
// none
//
// Returns:
//
// nothing
//
// Author: Michael Zarozinski
// Date: 6/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
//
RuleArray::~RuleArray()
{
free_memory();
};
//
// Function: alloc()
//
// Purpose: This function allocates memory for the rule array
//
// Arguments:
//
// int size - how many rules to allocate space for
//
// Returns:
//
// 0 - success
// non-zero - failure
//
// Author: Michael Zarozinski
// Date: 7/8/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
int RuleArray::alloc(int size)
{
// if we have rules, free the memory
if (rules != NULL)
{
free_memory();
}
// 0 size is OK - we may not have any terms yet
if (size <= 0)
{
max = 0;
return 0;
}
rules = new RuleArrayType[size];
if (rules == NULL)
{
assert(rules != NULL);
set_msg_text_int(ERR_ALLOC_MEM);
return -1;
}
// set the size of the array
max = size;
// init the memory...
clear();
return 0;
} // end RuleArray::alloc()
//
// Function: set()
//
// Purpose: This function sets the rules array equal to the array passed in.
//
// Arguments:
//
// RuleArrayType* source - source data to copy
// int size - how many rules to copy. NOTE this is NOT the total size
// of the memory, it's the number of elements in the rules array
//
// Returns:
//
// 0 - success
// non-zero - failure
//
// Author: Michael Zarozinski
// Date: 7/8/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
int RuleArray::set(RuleArrayType* source, int size)
{
// allocate the rules (this deletes any existing memory)
alloc(size);
// copy the rules passed in
memcpy(rules, source, sizeof(RuleArrayType) * size);
return 0;
} // end RuleArray::set()
//
// Function: no_rules()
//
// Purpose: This function tells us if we have any rules that have been
// defined for this model.
//
// Arguments:
//
// none
//
// Returns:
//
// true - if no rules are defined
// false - if we have at least one rule
//
// Author: Michael Zarozinski
// Date: 7/8/99
//
// Modification History
// Author Date Modification
// ------ ---- ------------
//
bool RuleArray::no_rules() const
{
if (rules == NULL)
return true;
// loop through and find out if there are ANY rules - we need this
// so we don't ask stuipid questions like "this will delete all rules" when
// there are NO rules
for (int i = 0; i < max; i++)
{
if (rules[i] != NO_RULE)
return false;
}
return true;
}; // end RuleArray::no_rules()
/////////////////////////////////////////////////////////////////////
////////// Trivial Functions That Don't Require Headers /////////////
/////////////////////////////////////////////////////////////////////
void RuleArray::clear()
{
memset(rules, NO_RULE, max * sizeof(RuleArrayType));
}; // end RuleArray::clear()
void RuleArray::free_memory()
{
if (rules != NULL)
delete[] rules;
max = 0;
rules = NULL;
};
RuleArrayType RuleArray::get_rule(int index) const
{
assert(index < max);
return(rules[index]);
};
void RuleArray::add_rule(int index, RuleArrayType output_set)
{
assert(index < max);
rules[index] = (RuleArrayType)(output_set);
};
int RuleArray::get_max() const
{
return max;
};
void RuleArray::remove_rule(int index)
{
assert(index < max);
rules[index] = NO_RULE;
};
FuzzyModelBase* RuleArray::get_parent(void) const
{
return static_cast< FuzzyModelBase*>(FFLLBase::get_parent());
};
const char* RuleArray::get_model_name() const
{
FuzzyModelBase* par = get_parent();
if (!par)
return NULL;
return par->get_model_name();
};