Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 243

Warning: Undefined variable $n in /usr/local/www/websvn.planix.org/include/diff_util.php on line 247

Warning: Undefined variable $m in /usr/local/www/websvn.planix.org/include/diff_util.php on line 251
WebSVN – tendra.SVN – Diff – /branches/tendra5/src/utilities/calculus/obj_c/calculus.c – Rev 5 and 6

Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line -... Line 1...
-
 
1
/*
-
 
2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
-
 
3
 * All rights reserved.
-
 
4
 *
-
 
5
 * Redistribution and use in source and binary forms, with or without
-
 
6
 * modification, are permitted provided that the following conditions are met:
-
 
7
 *
-
 
8
 * 1. Redistributions of source code must retain the above copyright notice,
-
 
9
 *    this list of conditions and the following disclaimer.
-
 
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
-
 
11
 *    this list of conditions and the following disclaimer in the documentation
-
 
12
 *    and/or other materials provided with the distribution.
-
 
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
-
 
14
 *    may be used to endorse or promote products derived from this software
-
 
15
 *    without specific, prior written permission.
-
 
16
 *
-
 
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
-
 
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
-
 
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-
 
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
-
 
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-
 
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-
 
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-
 
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-
 
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-
 
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-
 
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
 
28
 *
-
 
29
 * $Id$
-
 
30
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
29
 
59
 
30
 
60
 
31
#define calculus_IO_ROUTINES
61
#define calculus_IO_ROUTINES
32
#include "config.h"
62
#include "config.h"
33
#include "calculus.h"
63
#include "calculus.h"
34
#include "error.h"
64
#include "error.h"
35
#include "extra.h"
65
#include "extra.h"
36
#include "xalloc.h"
66
#include "xalloc.h"
37
 
67
 
38
 
68
 
39
/*
69
/*
40
    CHECK FOR CORRECT HEADERS
70
 * CHECK FOR CORRECT HEADERS
41
 
71
 *
42
    The functions defined in this file give the implementations of various
72
 * The functions defined in this file give the implementations of various
43
    support functions used by the calculus implementation.  They should
73
 * support functions used by the calculus implementation.  They should
44
    therefore be compiled with the calculus implementation rather than the
74
 * therefore be compiled with the calculus implementation rather than the
45
    specification.
75
 * specification.
46
*/
76
 */
47
 
77
 
48
#if calculus_SPECIFICATION
78
#if calculus_SPECIFICATION
49
!!!error Implementation specific functions compiled with token specifications
79
!!!error Implementation specific functions compiled with token specifications
50
#endif
80
#endif
51
 
81
 
52
 
82
 
53
/*
83
/*
54
    FREE OBJECTS
84
 * FREE OBJECTS
55
 
85
 *
56
    These variables indicate the free calculi.  There is an array containing
86
 * These variables indicate the free calculi.  There is an array containing
57
    lists of small blocks, plus a single larger block.
87
 * lists of small blocks, plus a single larger block.
58
*/
88
 */
59
 
89
 
60
static calculus *free_calculi = NULL ;
90
static calculus *free_calculi = NULL;
61
static unsigned free_calculi_left = 0 ;
91
static unsigned free_calculi_left = 0;
62
static calculus *free_calculus_array [] = {
92
static calculus *free_calculus_array[] = {
63
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
93
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
64
    NULL, NULL, NULL
94
    NULL, NULL, NULL
65
} ;
95
};
66
 
96
 
67
 
97
 
68
/*
98
/*
69
    GENERATE A NEW OBJECT BLOCK
99
 * GENERATE A NEW OBJECT BLOCK
-
 
100
 *
-
 
101
 * This routine generates a new blcok of calculi of size sz.  Small blocks
-
 
102
 * are allocated from the calculus array, others from the main calculus list.
-
 
103
 */
70
 
104
 
71
    This routine generates a new blcok of calculi of size sz.  Small blocks
-
 
72
    are allocated from the calculus array, others from the main calculus list.
-
 
73
*/
-
 
74
 
-
 
75
calculus *gen_calculus
105
calculus *
76
    PROTO_N ( ( sz ) )
-
 
77
    PROTO_T ( unsigned sz )
106
gen_calculus(unsigned sz)
78
{
107
{
79
    calculus *p ;
108
    calculus *p;
80
    unsigned n = sz ;
109
    unsigned n = sz;
81
    ASSERT ( array_size ( free_calculus_array ) == calculus_GEN_MAX ) ;
110
    ASSERT(array_size(free_calculus_array) == calculus_GEN_MAX);
82
    ASSERT ( n != 0 ) ;
111
    ASSERT(n != 0);
83
 
112
 
84
    if ( n < calculus_GEN_MAX ) {
113
    if (n < calculus_GEN_MAX) {
85
	/* Allocate from small block array */
114
	/* Allocate from small block array */
86
	p = free_calculus_array [n] ;
115
	p = free_calculus_array[n];
87
	if ( p ) {
116
	if (p) {
88
	    free_calculus_array [n] = TAIL_list ( p ) ;
117
	    free_calculus_array[n] = TAIL_list(p);
89
	    return ( p ) ;
118
	    return(p);
90
	}
119
	}
91
    }
120
    }
92
 
121
 
289
 
257
 
290
calculus *find_calculus_alias
258
calculus *
291
    PROTO_N ( ( n ) )
-
 
292
    PROTO_T ( unsigned n )
259
end_calculus_list(calculus *p)
293
{
260
{
294
    calculus *p = crt_alias_list ;
261
    calculus *r = p;
295
    while ( p != NULL ) {
262
    if (r == NULL) {
296
	calculus *q = HEAD_list ( p )->ag_ptr ;
-
 
297
	if ( q->ag_tag == n ) return ( q ) ;
-
 
298
	p = TAIL_list ( p ) ;
263
	    return(NULL);
299
    }
264
    }
300
    error ( ERROR_FATAL, "Can't find alias %u", n ) ;
265
    while (TAIL_list(r)) {
-
 
266
	    r = TAIL_list(r);
-
 
267
    }
301
    return ( NULL ) ;
268
    return(r);
302
}
269
}
303
 
270
 
304
 
271
 
305
/*
272
/*
306
    CLEAR ALL ALIASES
273
 * EMPTY VECTOR
-
 
274
 *
-
 
275
 * This calculus represents the generic empty vector.  It is only defined
-
 
276
 * if vector operations have been enabled.  Note that the element field
-
 
277
 * of a vector is not be NULL, even if the vector is empty.
-
 
278
 */
307
 
279
 
-
 
280
#ifdef VEC
308
    This routine clears all aliases.  Each alias in the list is reset to
281
static calculus dummy_elem;
309
    zero, and the list itself is freed.
282
calculus_VEC empty_calculus_vec = {0, {&dummy_elem, &dummy_elem}};
-
 
283
#endif
310
*/
284
 
311
 
285
 
-
 
286
/*
312
void clear_calculus_alias
287
 * ALIASING VARIABLES
-
 
288
 *
-
 
289
 * These variables give respectively the current alias number and the
313
    PROTO_Z ()
290
 * list of all aliases.
-
 
291
 */
314
{
292
 
315
    calculus *p = crt_alias_list ;
293
unsigned crt_calculus_alias = 0;
316
    calculus *q = NULL ;
294
static calculus *crt_alias_list = NULL;
-
 
295
 
-
 
296
 
-
 
297
/*
317
    while ( p != NULL ) {
298
 * SET AN ALIAS
-
 
299
 *
318
	HEAD_list ( p )->ag_ptr->ag_tag = 0 ;
300
 * This routine sets up an alias of p to n.
319
	q = p ;
301
 */
-
 
302
 
-
 
303
void
320
	p = TAIL_list ( p ) ;
304
set_calculus_alias(calculus *p, unsigned n)
321
    }
305
{
322
    if ( q ) {
306
    calculus *q;
-
 
307
    ASSERT(p != NULL);
323
	TAIL_list ( q ) = free_calculus_array [2] ;
308
    q = gen_calculus((unsigned)2);
324
	free_calculus_array [2] = crt_alias_list ;
309
    TAIL_list(q) = crt_alias_list;
325
    }
310
    HEAD_list(q)->ag_ptr = p;
326
    crt_calculus_alias = 0 ;
311
    p->ag_tag = n;
327
    crt_alias_list = NULL ;
312
    crt_alias_list = q;
328
    return ;
313
    return;
329
}
314
}
330
 
315
 
331
 
316
 
332
/*
317
/*
333
    ADD TWO LISTS
318
 * FIND AN ALIAS
-
 
319
 *
-
 
320
 * This routine searches for alias number n.
-
 
321
 */
334
 
322
 
335
    This routine copies the list of blocks of calculi p and adds the list
-
 
336
    q to the end.  sz gives the size of the blocks in the list.  This is
-
 
337
    used for the user defined tokens in extra.h.
-
 
338
*/
-
 
339
 
-
 
340
calculus *add_calculus_list
323
calculus *
341
    PROTO_N ( ( p, q, sz ) )
-
 
342
    PROTO_T ( calculus *p X calculus *q X int sz )
324
find_calculus_alias(unsigned n)
343
{
325
{
344
    int i ;
-
 
345
    calculus *r ;
326
    calculus *p = crt_alias_list;
346
    if ( p == NULL ) return ( q ) ;
327
    while (p != NULL) {
347
    r = gen_calculus ( ( unsigned ) ( sz + 1 ) ) ;
328
	calculus *q = HEAD_list(p)->ag_ptr;
348
    for ( i = 1 ; i <= sz ; i++ ) r [i] = p [i] ;
329
	if (q->ag_tag == n) {
-
 
330
		return(q);
-
 
331
	}
-
 
332
	p = TAIL_list(p);
-
 
333
    }
349
    TAIL_list ( r ) = add_calculus_list ( TAIL_list ( p ), q, sz ) ;
334
    error(ERROR_FATAL, "Can't find alias %u", n);
350
    return ( r ) ;
335
    return(NULL);
351
}
336
}
352
 
337
 
353
 
338
 
354
/*
339
/*
355
    ASSERTION ROUTINES
340
 * CLEAR ALL ALIASES
-
 
341
 *
-
 
342
 * This routine clears all aliases.  Each alias in the list is reset to
-
 
343
 * zero, and the list itself is freed.
-
 
344
 */
356
 
345
 
-
 
346
void
-
 
347
clear_calculus_alias(void)
-
 
348
{
-
 
349
    calculus *p = crt_alias_list;
-
 
350
    calculus *q = NULL;
-
 
351
    while (p != NULL) {
-
 
352
	HEAD_list(p) ->ag_ptr->ag_tag = 0;
-
 
353
	q = p;
-
 
354
	p = TAIL_list(p);
-
 
355
    }
-
 
356
    if (q) {
-
 
357
	TAIL_list(q) = free_calculus_array[2];
-
 
358
	free_calculus_array[2] = crt_alias_list;
-
 
359
    }
-
 
360
    crt_calculus_alias = 0;
-
 
361
    crt_alias_list = NULL;
-
 
362
    return;
-
 
363
}
-
 
364
 
-
 
365
 
-
 
366
/*
-
 
367
 * ADD TWO LISTS
-
 
368
 *
-
 
369
 * This routine copies the list of blocks of calculi p and adds the list
-
 
370
 * q to the end.  sz gives the size of the blocks in the list.  This is
-
 
371
 * used for the user defined tokens in extra.h.
-
 
372
 */
-
 
373
 
-
 
374
calculus *
-
 
375
add_calculus_list(calculus *p, calculus *q, int sz)
-
 
376
{
-
 
377
    int i;
-
 
378
    calculus *r;
-
 
379
    if (p == NULL) {
-
 
380
	    return(q);
-
 
381
    }
-
 
382
    r = gen_calculus((unsigned)(sz + 1));
-
 
383
    for (i = 1; i <= sz; i++) {
-
 
384
	    r[i] = p[i];
-
 
385
    }
-
 
386
    TAIL_list(r) = add_calculus_list(TAIL_list(p), q, sz);
-
 
387
    return(r);
-
 
388
}
-
 
389
 
-
 
390
 
-
 
391
/*
-
 
392
 * ASSERTION ROUTINES
-
 
393
 *
357
    These routine implement the assertion checks.
394
 * These routine implement the assertion checks.
358
*/
395
 */
359
 
396
 
360
#ifdef ASSERTS
397
#ifdef ASSERTS
361
#define assert_calculus assertion
398
#define assert_calculus assertion
362
#include "assert_def.h"
399
#include "assert_def.h"
363
#endif
400
#endif