-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRealObject.h
327 lines (272 loc) · 10.1 KB
/
RealObject.h
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
RealLib, a library for efficient exact real computation
Copyright (C) 2006 Branimir Lambov
This library is licensed under the Apache License, Version 2.0 (the "License");
you may not use this library except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
RealObject.h
This header defines the objects that represent real numbers.
A real is a tree that represents the procedure by which it
is calculated. It might be a conversion from another type
(double or string), a constant (RealNullary), or a operation
applied to another real (RealUnary and RealBinary).
RealObjects save this information, also handle the repeated
usage of a term, and the caching of calculated estimates.
They are written in zero-overhead manner, i.e. care is taken
to never introduce extra calculation or extra storage than
what is minimally needed, and what is actually needed.
*/
#ifndef FILE_REALOBJECT_H
#define FILE_REALOBJECT_H
#include "RealEncapsulation.h"
namespace RealLib {
struct ObjectList;
struct EvalList;
// g_pEstimatesList stores pointers to all temporary estimates
// it is used to delete them in case of library Reset (precision
// change), when they become invalid
extern ObjectList *g_pEstimatesList;
typedef const char* (*OracleFunction) (unsigned precision);
// prec and the Encapsulation working precision will grow together
// RealObject: the abstract base class
class RealObject {
// reference counter
i32 m_RefCount;
// a pointer to the pointer to this object in g_pEstimatesList
ObjectList *m_pPtrInObjList;
protected:
// temporary Encapsulation
EncapsulationPointer m_pEstimate;
RealObject(u32 rc = 0);
// evaluation procedure.
// To be overridden by implementations
virtual Encapsulation Evaluate() = 0;
// destructor
virtual ~RealObject();
// release the other objects this one points to
// only in RealUnary and RealBinary
virtual void ReleaseSiblings();
public:
// depth. equal to the max depth of the siblings + 1
i32 m_Depth;
// Encapsulation refs. set to refcount when the Encapsulation is created.
// decreased by GetEstimate. The Encapsulation is destroyed when
// m_EstimateRefs reaches 0.
i32 m_EstimateRefs;
// get the Encapsulation, evaluate if necessary
Encapsulation GetEstimate();
// do we hold an Encapsulation?
bool HasEstimate()
{ return m_pEstimate; }
void AddToEstimatesList();
void RemoveFromEstimatesList();
// create a new Encapsulation (differs only in arrays, usually new Encapsulation (val))
// and add to g_pEstimatesList
virtual EncapsulationPointer CreateEstimate(const Encapsulation &val);
// destroy the Encapsulation and remove this object from g_pEstimatesList
virtual void DestroyEstimate();
// add reference. Called when a pointer to this object is saved
RealObject* AddRef();
// release: decreases refcount and deletes object if rc == 0
void Release(int DecreaseCachedRefCount = 0);
// get the reference count
u32 GetRefCount()
{ return m_RefCount; }
// returns the sibling with the given index
// returns NULL if there are no more
// to be used to do recursion with a separate stack
virtual RealObject* GetSibling(int index);
// non-recursive release, to be combined with GetSibling
void NonRecursiveRelease();
/* obsolete older version
// gets all objects that need to be pre-evaluated
// for the evaluation procedure not to dig deeper
// than the required level. calls testers to see
// if search can be stopped. won't get any list if
// pList is Null. returning from recursion calls
// OnExit
typedef bool (RealObject::*TesterFunc)(i32 Depth);
typedef void (RealObject::*OnExitFunc)();
virtual void GetDepthList(i32 Depth, TesterFunc Test, OnExitFunc OnExit, EvalList **pList, bool DiveIntoDeeperFirst);
// tester for depth-restricted evaluation
// no OnExit needed
bool HasEstimateReference(i32 Depth); // stops if m_EstimateRefs > 0, marks if already visited
// followed by evaluation which cleans marks
// tester/OnExit pair for depth-restricted release
bool StartRelease(i32 Depth); // decreases m_RefCount, stops if > 0, or if Depth < m_Depth with ReleaseSiblings()
void FinishRelease(); // cleans-up object without releasing siblings if m_RefCount == 0
*/
/*
// testers if depth search can be stopped
// decreases refcount, returns true if > 0
bool WontBeDeleted();
// not used
// stops if m_EstimateRefs > 0.
// increases m_EstimateRefs if not to mark visited objects
bool HasEstimateReference();
// on exit function for deletion
// deletes the object if m_RefCount == 0
void NonRecursiveRelease();
// not used
void SetEstimateReference();
*/
};
struct ObjectList {
ObjectList *next;
ObjectList *prev;
RealObject *obj;
};
// this struct saves RealObjects to be visited
struct EvalList {
EvalList *next;
bool used;
RealObject *obj;
};
// real from double. Handles conversion.
class RealFromDouble : public RealObject {
double m_Value;
protected:
virtual ~RealFromDouble();
virtual Encapsulation Evaluate();
public:
RealFromDouble(const double src);
};
// real from string. Handles conversion
class RealFromString : public RealObject {
char *m_pString;
protected:
virtual ~RealFromString();
virtual Encapsulation Evaluate();
public:
RealFromString(const char *src);
};
// real from oracle function
class RealFromOracle : public RealObject {
OracleFunction m_pOracle;
protected:
virtual ~RealFromOracle();
virtual Encapsulation Evaluate();
public:
RealFromOracle(OracleFunction oracle);
};
// RealNullary. constants
class RealNullary : public RealObject {
public:
typedef Encapsulation (*FuncNullary) (unsigned int prec, UserInt otherData);
private:
FuncNullary m_pFunc;
UserInt m_iUserData;
protected:
virtual ~RealNullary();
virtual Encapsulation Evaluate();
public:
RealNullary(FuncNullary pFunc, UserInt userData);
};
// unary functions
class RealUnary : public RealObject {
public:
typedef Encapsulation (*FuncUnary) (const Encapsulation &arg, UserInt otherData);
private:
FuncUnary m_pFunc;
RealObject *m_pArg;
UserInt m_iUserData;
protected:
virtual ~RealUnary();
virtual void ReleaseSiblings();
virtual Encapsulation Evaluate();
public:
//virtual void GetDepthList(i32 Depth, TesterFunc Test, OnExitFunc OnExit, EvalList **pList, bool DiveIntoDeeperFirst);
RealUnary(FuncUnary pFunc, RealObject *pArg, UserInt userData);
virtual RealObject* GetSibling(int index);
};
// binary functions
class RealBinary : public RealObject {
public:
typedef Encapsulation (*FuncBinary) (const Encapsulation &left, const Encapsulation &right, UserInt otherData);
private:
FuncBinary m_pFunc;
RealObject *m_pLeft;
RealObject *m_pRight;
UserInt m_iUserData;
protected:
virtual ~RealBinary();
virtual void ReleaseSiblings();
virtual Encapsulation Evaluate();
public:
//virtual void GetDepthList(i32 Depth, TesterFunc Test, OnExitFunc OnExit, EvalList **pList, bool DiveIntoDeeperFirst);
RealBinary(FuncBinary pFunc, RealObject *pLeft, RealObject *pRight, UserInt userData);
virtual RealObject* GetSibling(int index);
};
/*
class RealBinaryOnInt : public RealObject {
public:
typedef Encapsulation (*FuncBinaryOnInt) (const Encapsulation &arg, long iarg);
private:
FuncBinaryOnInt m_pFunc;
RealObject *m_pArg;
long m_iArg;
protected:
virtual ~RealBinaryOnInt();
virtual void ReleaseSiblings();
virtual Encapsulation Evaluate();
public:
//virtual void GetDepthList(i32 Depth, TesterFunc Test, OnExitFunc OnExit, EvalList **pList, bool DiveIntoDeeperFirst);
RealBinaryOnInt(FuncBinaryOnInt pFunc, RealObject *pArg, long iarg);
virtual RealObject* GetSibling(int index);
};*/
// functions on array
// this is a bit complicated.
// first a RealArray object is created that holds references to all
// realobject's in the passed array
// then, for each element in the array, a RealArrayElement is
// created that holds a reference to the array.
// Evaluation of the element is passed along as evaluation of the
// array, having set the request index to its own.
class RealArray : public RealObject {
public:
typedef void (*FuncArray) (ArrayInterface<Encapsulation>&, UserInt otherData);
//typedef void (*FuncArray) (Encapsulation* arr, unsigned int count, void* userdata);
private:
FuncArray m_pFunc;
RealObject **m_pArray;
EncapsulationPointer m_pEstimateArray;
u32 m_uCount;
UserInt m_iUserData;
i32 m_uRequestIndex;
protected:
virtual ~RealArray();
virtual void ReleaseSiblings();
virtual Encapsulation Evaluate();
public:
virtual EncapsulationPointer CreateEstimate(const Encapsulation &val);
virtual void DestroyEstimate();
void SetRequestIndex(i32 index);
//virtual void GetDepthList(i32 Depth, TesterFunc Test, OnExitFunc OnExit, EvalList **pList, bool DiveIntoDeeperFirst);
RealArray(FuncArray pFunc, RealObject **pArray, unsigned int count, UserInt user);
virtual RealObject* GetSibling(int index);
};
class RealArrayElement : public RealObject {
private:
RealArray* m_pArray;
u32 m_uMyIndex;
protected:
virtual ~RealArrayElement();
virtual void ReleaseSiblings();
virtual Encapsulation Evaluate();
public:
virtual EncapsulationPointer CreateEstimate(const Encapsulation &val);
virtual void DestroyEstimate();
//virtual void GetDepthList(i32 Depth, TesterFunc Test, OnExitFunc OnExit, EvalList **pList, bool DiveIntoDeeperFirst);
RealArrayElement(RealArray *pArray, u32 myindex);
virtual RealObject* GetSibling(int index);
};
} // namespace
#endif