modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / c_cpp / etc / calc / size.c
blob639bad4cceb0f3a6fa0109b012558f2b2f90fdc8
1 /*
2 * size - size and sizeof functions are implemented here
4 * Copyright (C) 1999-2007 David I. Bell
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.1 $
21 * @(#) $Id: size.c,v 30.1 2007/03/16 11:09:46 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/size.c,v $
24 * Under source code control: 1997/03/10 01:56:51
25 * File existed as early as: 1997
27 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
31 #include "value.h"
32 #include "zrand.h"
33 #include "zrandom.h"
34 #include "block.h"
38 * forward declarations
40 S_FUNC size_t zsize(ZVALUE);
41 S_FUNC size_t qsize(NUMBER*);
42 S_FUNC size_t csize(COMPLEX*);
43 S_FUNC size_t memzsize(ZVALUE);
44 S_FUNC size_t memqsize(NUMBER*);
48 * elm_count - return information about the number of elements
50 * Return information about the number of elements or part of
51 * a value. This is what the size() builtin returns with the
52 * exception of the V_FILE type. To get V_FILE size length,
53 * the getsize(vp->v_file, &len) should be called directly.
55 * This is not the sizeof, see lsizeof() for that information.
57 * given:
58 * vp pointer to a value
60 * return:
61 * number of elements
63 long
64 elm_count(VALUE *vp)
66 long result;
69 * return information about the number of elements
71 * This is not the sizeof, see lsizeof() for that information.
72 * This is does not include overhead, see memsize() for that info.
75 switch (vp->v_type) {
76 case V_NULL:
77 case V_INT:
78 case V_ADDR:
79 case V_OCTET:
80 result = 0;
81 break;
82 case V_MAT:
83 result = vp->v_mat->m_size;
84 break;
85 case V_LIST:
86 result = vp->v_list->l_count;
87 break;
88 case V_ASSOC:
89 result = vp->v_assoc->a_count;
90 break;
91 case V_OBJ:
92 result = vp->v_obj->o_actions->oa_count;
93 break;
94 case V_STR:
95 result = vp->v_str->s_len;
96 break;
97 case V_BLOCK:
98 result = (long) vp->v_block->datalen;
99 break;
100 case V_NBLOCK:
101 result = (long) vp->v_nblock->blk->datalen;
102 break;
104 * V_NUM, V_COM, V_RAND, V_RANDOM, V_CONFIG, V_HASH
106 * V_FILE (use getsize(vp->v_file, &len) for file length)
108 default:
109 result = (vp->v_type > 0);
110 break;
112 return result;
117 * zsize - calculate memory footprint of a ZVALUE (exlcuding overhead)
119 * The numeric -1, - and 1 storage values are ignored.
121 * given:
122 * z ZVALUE to examine
124 * returns:
125 * value size
127 S_FUNC size_t
128 zsize(ZVALUE z)
130 /* ignore the size of 0, 1 and -1 */
131 if (z.v != _zeroval_ && z.v != _oneval_ && !zisunit(z) && !ziszero(z)) {
132 return z.len * sizeof(HALF);
133 } else {
134 return (size_t)0;
140 * qsize - calculate memory footprint of a NUMBER (exlcuding overhead)
142 * The numeric -1, - and 1 storage values are ignored. Denominator
143 * parts of integers are ignored.
145 * given:
146 * q pointer to NUMBER to examine
148 * returns:
149 * value size
151 S_FUNC size_t
152 qsize(NUMBER *q)
154 /* ingore denominator parts of integers */
155 if (qisint(q)) {
156 return zsize(q->num);
157 } else {
158 return (zsize(q->num) + zsize(q->den));
164 * csize - calculate memory footprint of a COMPLEX (exlcuding overhead)
166 * The numeric -1, - and 1 storage values are ignored. Denominator
167 * parts of integers are ignored. Imaginary parts of pure reals
168 * are ignored.
170 * given:
171 * c pointer to COMPLEX to examine
173 * returns:
174 * value size
176 S_FUNC size_t
177 csize(COMPLEX *c)
179 /* ingore denominator parts of integers */
180 if (cisreal(c)) {
181 return qsize(c->real);
182 } else {
183 return (qsize(c->real) + qsize(c->imag));
189 * memzsize - calculate memory footprint of a ZVALUE including overhead
191 * given:
192 * z ZVALUE to examine
194 * returns:
195 * memory footprint
197 S_FUNC size_t
198 memzsize(ZVALUE z)
200 return sizeof(ZVALUE) + (z.len * sizeof(HALF));
205 * memqsize - calculate memory footprint of a NUMBER including overhead
207 * given:
208 * q pointer of NUMBER to examine
210 * returns:
211 * memory footprint
213 S_FUNC size_t
214 memqsize(NUMBER *q)
216 return sizeof(NUMBER) + memzsize(q->num) + memzsize(q->den);
221 * lsizeof - calculate memory footprint of a VALUE (not counting overhead)
223 * given:
224 * vp pointer of VALUE to examine
226 * returns:
227 * memory footprint
229 size_t
230 lsizeof(VALUE *vp)
232 VALUE *p;
233 LISTELEM *ep;
234 OBJECTACTIONS *oap;
235 ASSOCELEM *aep;
236 ASSOCELEM **ept;
237 size_t s;
238 long i;
241 * return information about memory footprint
243 * This is not the number of elements, see elm_count() for that info.
244 * This is does not include overhead, see memsize() for that info.
246 i = 0;
247 s = 0;
248 if (vp->v_type > 0) {
249 switch(vp->v_type) {
250 case V_INT:
251 case V_ADDR:
252 case V_OCTET:
253 break;
254 case V_NUM:
255 s = qsize(vp->v_num);
256 break;
257 case V_COM:
258 s = csize(vp->v_com);
259 break;
260 case V_STR:
261 s = vp->v_str->s_len + 1;
262 break;
263 case V_MAT:
264 i = vp->v_mat->m_size;
265 p = vp->v_mat->m_table;
266 while (i-- > 0)
267 s += lsizeof(p++);
268 break;
269 case V_LIST:
270 for (ep = vp->v_list->l_first; ep; ep = ep->e_next) {
271 s += lsizeof(&ep->e_value);
273 break;
274 case V_ASSOC:
275 i = vp->v_assoc->a_size;
276 ept = vp->v_assoc->a_table;
277 while (i-- > 0) {
278 for (aep = ept[i]; aep; aep = aep->e_next) {
279 s += lsizeof(&aep->e_value);
282 break;
283 case V_OBJ:
284 oap = vp->v_obj->o_actions;
285 i = oap->oa_count;
286 p = vp->v_obj->o_table;
287 while (i-- > 0)
288 s += lsizeof(p++);
289 break;
290 case V_FILE:
291 s = sizeof(vp->v_file);
292 break;
293 case V_RAND:
294 s = sizeof(RAND);
295 break;
296 case V_RANDOM:
297 s = sizeof(RANDOM) +
298 zsize(vp->v_random->n) +
299 zsize(vp->v_random->r);
300 break;
301 case V_CONFIG:
302 s = sizeof(CONFIG) +
303 strlen(vp->v_config->prompt1) +
304 strlen(vp->v_config->prompt2) + 2;
305 break;
306 case V_HASH:
307 /* ignore the unused part of the union */
308 s = sizeof(HASH) +
309 vp->v_hash->unionsize -
310 sizeof(vp->v_hash->h_union);
311 break;
312 case V_BLOCK:
313 s = vp->v_block->maxsize;
314 break;
315 case V_NBLOCK:
316 s = vp->v_nblock->blk->maxsize;
317 break;
318 default:
319 math_error("sizeof not defined for value type");
320 /*NOTREACHED*/
323 return s;
328 * memsize - calculate memory footprint of a VALUE including overhead
330 * given:
331 * vp pointer of VALUE to examine
333 * returns:
334 * memory footprint including overhead
336 size_t
337 memsize(VALUE *vp)
339 size_t s;
340 long i, j;
341 VALUE *p;
342 LISTELEM *ep;
343 OBJECTACTIONS *oap;
344 ASSOCELEM *aep;
345 ASSOCELEM **ept;
348 * return information about memory footprint
350 * This is not the sizeof, see memsize() for that information.
351 * This is not the number of elements, see elm_count() for that info.
353 i = j = 0;
354 s = sizeof(VALUE);
355 if (vp->v_type > 0) {
356 switch(vp->v_type) {
357 case V_INT:
358 case V_ADDR:
359 case V_OCTET:
360 break;
361 case V_NUM:
362 s = memqsize(vp->v_num);
363 break;
364 case V_COM:
365 s = sizeof(COMPLEX) +
366 memqsize(vp->v_com->real) +
367 memqsize(vp->v_com->imag);
368 break;
369 case V_STR:
370 s = sizeof(STRING) + vp->v_str->s_len + 1;
371 break;
372 case V_MAT:
373 s = sizeof(MATRIX);
374 i = vp->v_mat->m_size;
375 p = vp->v_mat->m_table;
376 while (i-- > 0)
377 s += memsize(p++);
378 break;
379 case V_LIST:
380 s = sizeof(LIST);
381 for (ep = vp->v_list->l_first; ep; ep = ep->e_next) {
382 s += sizeof(LISTELEM) - sizeof(VALUE) +
383 memsize(&ep->e_value);
385 break;
386 case V_ASSOC:
387 s = sizeof(ASSOC);
388 i = vp->v_assoc->a_size;
389 ept = vp->v_assoc->a_table;
390 while (i-- > 0) {
391 s += sizeof(ASSOCELEM *);
392 for (aep = *ept++; aep; aep = aep->e_next) {
393 s += sizeof(ASSOCELEM) - sizeof(VALUE) +
394 memsize(&aep->e_value);
395 j = aep->e_dim;
396 p = aep->e_indices;
397 while (j-- > 0)
398 s += memsize(p++);
401 break;
402 case V_OBJ:
403 s = sizeof(OBJECT);
404 oap = vp->v_obj->o_actions;
405 i = oap->oa_count;
406 s += (i + 3) * sizeof(int);
407 p = vp->v_obj->o_table;
408 while (i-- > 0)
409 s += memsize(p++);
410 break;
411 case V_FILE:
412 s = sizeof(vp->v_file);
413 break;
414 case V_RAND:
415 s = sizeof(RAND);
416 break;
417 case V_RANDOM:
418 s = sizeof(RANDOM) +
419 memzsize(vp->v_random->n) +
420 memzsize(vp->v_random->r);
421 break;
422 case V_CONFIG:
423 s = sizeof(CONFIG) + 2 +
424 strlen(vp->v_config->prompt1) +
425 strlen(vp->v_config->prompt2);
426 break;
427 case V_HASH:
428 s = sizeof(HASH);
429 break;
430 case V_BLOCK:
431 s = sizeof(BLOCK) + vp->v_block->maxsize;
432 break;
433 case V_NBLOCK:
434 s = sizeof(NBLOCK) + sizeof(BLOCK) +
435 vp->v_nblock->blk->maxsize +
436 strlen(vp->v_nblock->name) + 1;
437 break;
438 default:
439 math_error("memsize not defined for value type");
440 /*NOTREACHED*/
443 return s;