-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollatz.c
More file actions
234 lines (151 loc) · 3.7 KB
/
Collatz.c
File metadata and controls
234 lines (151 loc) · 3.7 KB
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
// The template for class
#include "Collatz.h"
#include <stdio.h>
#include <string.h>
/* -------------------------------
*
* Memory management methods
*
--------------------------------- */
void Collatz_free_fields(struct Collatz *this)
{
vdmFree(this->m_Collatz_val);
}
static void Collatz_free(struct Collatz *this)
{
--this->_Collatz_refs;
if (this->_Collatz_refs < 1)
{
Collatz_free_fields(this);
free(this);
}
}
/* -------------------------------
*
* Member methods
*
--------------------------------- */
static TVP _Z17fieldInitializer1EV() {
TVP ret_1 = vdmCloneGC(newIntGC(1, NULL), &ret_1)
;
return ret_1;
}
/* A.vdmrt 9:8 */
static void _Z3runEV(CollatzCLASS this) {
/* A.vdmrt 11:2 */
if ( toBool(vdmEqualsGC(GET_FIELD_PTR_GC(Collatz, Collatz, this, val), newIntGC(1, NULL), NULL)) )
/* A.vdmrt 12:3 */
return ;else if ( toBool(vdmEqualsGC(vdmModGC(GET_FIELD_PTR_GC(Collatz, Collatz, this, val), newIntGC(2, NULL), NULL), newIntGC(0, NULL), NULL)) )
{
/* A.vdmrt 12:14 */
TVP field_tmp_2 = vdmClone(vdmDivGC(GET_FIELD_PTR_GC(Collatz, Collatz, this, val), newIntGC(2, NULL), NULL))
;
SET_FIELD_PTR(Collatz, Collatz, this, val, field_tmp_2);
vdmFree(field_tmp_2);
}
else
{
/* A.vdmrt 14:18 */
TVP field_tmp_3 = vdmClone(vdmSumGC(vdmProductGC(newIntGC(3, NULL), GET_FIELD_PTR_GC(Collatz, Collatz, this, val), NULL), newIntGC(1, NULL), NULL))
;
SET_FIELD_PTR(Collatz, Collatz, this, val, field_tmp_3);
vdmFree(field_tmp_3);
}
;
//printf("%d\n", this->m_Collatz_val->value.intVal);
}
void Collatz_const_init() {
numFields_1 = _Z17fieldInitializer1EV();
return ;
}
void Collatz_const_shutdown() {
vdmFree(numFields_1);
return ;
}
void Collatz_static_init() {
return ;
}
void Collatz_static_shutdown() {
return ;
}
/* -------------------------------
*
* VTable
*
--------------------------------- */
// VTable for this class
static struct VTable VTableArrayForCollatz [] ={
{0,0,((VirtualFunctionPointer) _Z7CollatzEI),},
{0,0,((VirtualFunctionPointer) _Z3runEV),},
{0,0,((VirtualFunctionPointer) _Z7CollatzEV),},
} ;
// Overload VTables
/* -------------------------------
*
* Internal memory constructor
*
--------------------------------- */
CollatzCLASS Collatz_Constructor(CollatzCLASS this_ptr)
{
if(this_ptr==NULL)
{
this_ptr = (CollatzCLASS) malloc(sizeof(struct Collatz));
}
if(this_ptr!=NULL)
{
// Collatz init
this_ptr->_Collatz_id = CLASS_ID_Collatz_ID;
this_ptr->_Collatz_refs = 0;
this_ptr->_Collatz_pVTable=VTableArrayForCollatz;
this_ptr->m_Collatz_val= NULL ;
}
return this_ptr;
}
// Method for creating new "class"
static TVP new()
{
CollatzCLASS ptr=Collatz_Constructor(NULL);
return newTypeValue(VDM_CLASS, (TypedValueType)
{ .ptr=newClassValue(ptr->_Collatz_id, &ptr->_Collatz_refs, (freeVdmClassFunction)&Collatz_free, ptr)});
}
/* -------------------------------
*
* Public class constructors
*
--------------------------------- */
/* A.vdmrt 6:8 */
TVP _Z7CollatzEI(CollatzCLASS this, TVP v) {
TVP __buf = NULL
;
if ( this == NULL )
{
__buf = new();
this = TO_CLASS_PTR(__buf, Collatz);
}
;
/* A.vdmrt 7:22 */
TVP field_tmp_1 = vdmClone(v)
;
SET_FIELD_PTR(Collatz, Collatz, this, val, field_tmp_1);
vdmFree(field_tmp_1);
return __buf;
}
/* A.vdmrt 1:7 */
TVP _Z7CollatzEV(CollatzCLASS this) {
TVP __buf = NULL
;
if ( this == NULL )
{
__buf = new();
this = TO_CLASS_PTR(__buf, Collatz);
}
;
return __buf;
}
/* -------------------------------
*
* Global class fields
*
--------------------------------- */
// initialize globals - this is done last since they are declared in the header but uses init functions which are printet in any order
TVP numFields_1 = NULL ;