1 | /** |
---|
2 | * @file Variable.c |
---|
3 | * @author Sheng Di |
---|
4 | * @date July, 2016 |
---|
5 | * @brief TypeManager is used to manage the type array: parsing of the bytes and other types in between. |
---|
6 | * (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory. |
---|
7 | * See COPYRIGHT in top-level directory. |
---|
8 | */ |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <string.h> |
---|
13 | #include "VarSet.h" |
---|
14 | #include "sz.h" |
---|
15 | |
---|
16 | void free_Variable_keepOriginalData(SZ_Variable* v) |
---|
17 | { |
---|
18 | if(v->varName!=NULL) |
---|
19 | free(v->varName); |
---|
20 | if(v->data!=NULL) |
---|
21 | free(v->compressedBytes); |
---|
22 | |
---|
23 | free(v); |
---|
24 | } |
---|
25 | |
---|
26 | /** |
---|
27 | * |
---|
28 | * @deprecated |
---|
29 | * */ |
---|
30 | void free_Variable_keepCompressedBytes(SZ_Variable* v) |
---|
31 | { |
---|
32 | if(v->varName!=NULL) |
---|
33 | free(v->varName); |
---|
34 | if(v->data!=NULL) |
---|
35 | free(v->data); |
---|
36 | if(v->multisteps!=NULL) |
---|
37 | free_multisteps(v->multisteps); |
---|
38 | free(v); |
---|
39 | } |
---|
40 | |
---|
41 | void free_Variable_all(SZ_Variable* v) |
---|
42 | { |
---|
43 | if(v->varName!=NULL) |
---|
44 | free(v->varName); |
---|
45 | if(v->data!=NULL) |
---|
46 | free(v->data); |
---|
47 | if(v->compressedBytes!=NULL) |
---|
48 | free(v->compressedBytes); |
---|
49 | if(v->multisteps!=NULL) |
---|
50 | free_multisteps(v->multisteps); |
---|
51 | free(v); |
---|
52 | } |
---|
53 | |
---|
54 | void SZ_batchAddVar(char* varName, int dataType, void* data, |
---|
55 | int errBoundMode, double absErrBound, double relBoundRatio, double pwRelBoundRatio, |
---|
56 | size_t r5, size_t r4, size_t r3, size_t r2, size_t r1) |
---|
57 | { |
---|
58 | if(sz_varset==NULL) |
---|
59 | { |
---|
60 | sz_varset = (SZ_VarSet*)malloc(sizeof(SZ_VarSet)); |
---|
61 | sz_varset->header = (SZ_Variable*)malloc(sizeof(SZ_Variable)); |
---|
62 | sz_varset->header->next = NULL; |
---|
63 | sz_varset->lastVar = sz_varset->header; |
---|
64 | sz_varset->count = 0; |
---|
65 | } |
---|
66 | |
---|
67 | SZ_Variable* var = (SZ_Variable*)malloc(sizeof(SZ_Variable)); |
---|
68 | memset(var, 0, sizeof(SZ_Variable)); |
---|
69 | |
---|
70 | var->varName = (char*)malloc(strlen(varName)+1); |
---|
71 | memcpy(var->varName, varName, strlen(varName)+1); |
---|
72 | //var->varName = varName; |
---|
73 | var->dataType = dataType; |
---|
74 | var->r5 = r5; |
---|
75 | var->r4 = r4; |
---|
76 | var->r3 = r3; |
---|
77 | var->r2 = r2; |
---|
78 | var->r1 = r1; |
---|
79 | var->errBoundMode = errBoundMode; |
---|
80 | var->absErrBound = absErrBound; |
---|
81 | var->relBoundRatio = relBoundRatio; |
---|
82 | var->pwRelBoundRatio = pwRelBoundRatio; |
---|
83 | var->data = data; |
---|
84 | |
---|
85 | var->multisteps = (sz_multisteps*)malloc(sizeof(sz_multisteps)); |
---|
86 | memset(var->multisteps, 0, sizeof(sz_multisteps)); |
---|
87 | |
---|
88 | size_t dataLen = computeDataLength(r5, r4, r3, r2, r1); |
---|
89 | if(dataType==SZ_FLOAT) |
---|
90 | { |
---|
91 | var->multisteps->hist_data = (float*)malloc(sizeof(float)*dataLen); |
---|
92 | memset(var->multisteps->hist_data, 0, sizeof(float)*dataLen); |
---|
93 | } |
---|
94 | else if(dataType==SZ_DOUBLE) |
---|
95 | { |
---|
96 | var->multisteps->hist_data = (double*)malloc(sizeof(double)*dataLen); |
---|
97 | memset(var->multisteps->hist_data, 0, sizeof(double)*dataLen); |
---|
98 | } |
---|
99 | var->compressedBytes = NULL; |
---|
100 | var->next = NULL; |
---|
101 | |
---|
102 | sz_varset->count ++; |
---|
103 | sz_varset->lastVar->next = var; |
---|
104 | sz_varset->lastVar = var; |
---|
105 | } |
---|
106 | |
---|
107 | int SZ_batchDelVar(char* varName) |
---|
108 | { |
---|
109 | int state = SZ_batchDelVar_vset(sz_varset, varName); |
---|
110 | return state; |
---|
111 | } |
---|
112 | |
---|
113 | int SZ_batchDelVar_vset(SZ_VarSet* vset, char* varName) |
---|
114 | { |
---|
115 | int delSuccess = SZ_NSCS; |
---|
116 | SZ_Variable* p = vset->header; |
---|
117 | SZ_Variable* q = p->next; |
---|
118 | while(q != NULL) |
---|
119 | { |
---|
120 | int cmpResult = strcmp(q->varName, varName); |
---|
121 | if(cmpResult==0) |
---|
122 | { |
---|
123 | p->next = q->next; |
---|
124 | free_Variable_all(q); |
---|
125 | vset->count --; |
---|
126 | delSuccess = SZ_SCES; |
---|
127 | break; |
---|
128 | } |
---|
129 | p = p->next; |
---|
130 | q = q->next; |
---|
131 | } |
---|
132 | |
---|
133 | return delSuccess; |
---|
134 | } |
---|
135 | |
---|
136 | SZ_Variable* SZ_searchVar(char* varName) |
---|
137 | { |
---|
138 | SZ_Variable* p = sz_varset->header->next; |
---|
139 | while(p!=NULL) |
---|
140 | { |
---|
141 | int checkName = strcmp(p->varName, varName); |
---|
142 | if(checkName==0) |
---|
143 | return p; |
---|
144 | p = p->next; |
---|
145 | } |
---|
146 | return NULL; |
---|
147 | } |
---|
148 | |
---|
149 | void* SZ_getVarData(char* varName, size_t *r5, size_t *r4, size_t *r3, size_t *r2, size_t *r1) |
---|
150 | { |
---|
151 | SZ_Variable* v = SZ_searchVar(varName); |
---|
152 | *r5 = v->r5; |
---|
153 | *r4 = v->r4; |
---|
154 | *r3 = v->r3; |
---|
155 | *r2 = v->r2; |
---|
156 | *r1 = v->r1; |
---|
157 | return (void*)v->data; |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * |
---|
162 | * int mode: SZ_MAINTAIN_VAR_DATA, Z_DESTROY_WHOLE_VARSET |
---|
163 | * */ |
---|
164 | void SZ_freeVarSet(int mode) |
---|
165 | { |
---|
166 | free_VarSet_vset(sz_varset, mode); |
---|
167 | } |
---|
168 | |
---|
169 | //free_VarSet will completely destroy the SZ_VarSet, so don't do it until you really don't need it any more! |
---|
170 | /** |
---|
171 | * |
---|
172 | * int mode: SZ_MAINTAIN_VAR_DATA, Z_DESTROY_WHOLE_VARSET |
---|
173 | * */ |
---|
174 | void free_VarSet_vset(SZ_VarSet *vset, int mode) |
---|
175 | { |
---|
176 | if(vset==NULL) |
---|
177 | return; |
---|
178 | SZ_Variable *p = vset->header; |
---|
179 | while(p->next!=NULL) |
---|
180 | { |
---|
181 | SZ_Variable *q = p->next; |
---|
182 | p->next = q->next; |
---|
183 | if(mode==SZ_MAINTAIN_VAR_DATA) |
---|
184 | free_Variable_keepOriginalData(q); |
---|
185 | else if(mode==SZ_DESTROY_WHOLE_VARSET) |
---|
186 | free_Variable_all(q); |
---|
187 | } |
---|
188 | free(sz_varset->header); |
---|
189 | free(vset); |
---|
190 | } |
---|
191 | |
---|
192 | void free_multisteps(sz_multisteps* multisteps) |
---|
193 | { |
---|
194 | if(multisteps->hist_data!=NULL) |
---|
195 | free(multisteps->hist_data); |
---|
196 | free(multisteps); |
---|
197 | } |
---|