mirror of
https://github.com/lcn2/calc.git
synced 2025-08-16 01:03:29 +03:00
Release calc version 2.10.3t5.45
This commit is contained in:
167
const.c
167
const.c
@@ -1,27 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 1993 David I. Bell
|
||||
* Copyright (c) 1997 David I. Bell
|
||||
* Permission is granted to use, distribute, or modify this source,
|
||||
* provided that this copyright notice remains intact.
|
||||
*
|
||||
* Constant number storage module.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "calc.h"
|
||||
#include "qmath.h"
|
||||
|
||||
#define CONSTALLOCSIZE 400 /* number of constants to allocate */
|
||||
|
||||
|
||||
static long constcount; /* number of constants defined */
|
||||
static long constavail; /* number of constants available */
|
||||
static NUMBER **consttable; /* table of constants */
|
||||
|
||||
|
||||
void
|
||||
initconstants(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
consttable = (NUMBER **) malloc(sizeof(NUMBER *) * CONSTALLOCSIZE);
|
||||
if (consttable == NULL) {
|
||||
math_error("Unable to allocate constant table");
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
for (i = 0; i < 8; i++)
|
||||
consttable[i] = initnumbs[i];
|
||||
constcount = 8;
|
||||
constavail = CONSTALLOCSIZE - 8;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Read in a constant number and add it to the table of constant numbers,
|
||||
* Read in a literal real number and add it to the table of constant numbers,
|
||||
* creating a new entry if necessary. The incoming number is a string
|
||||
* value which must have a correct format, otherwise an undefined number
|
||||
* will result. Returns the index of the number in the constant table.
|
||||
* Returns zero if the number could not be saved.
|
||||
* value which must have a correct format.
|
||||
* Returns the index of the number in the constant table.
|
||||
*
|
||||
* given:
|
||||
* str string representation of number
|
||||
@@ -32,8 +49,6 @@ addnumber(char *str)
|
||||
NUMBER *q;
|
||||
|
||||
q = str2q(str);
|
||||
if (q == NULL)
|
||||
return 0;
|
||||
return addqconstant(q);
|
||||
}
|
||||
|
||||
@@ -59,14 +74,53 @@ addqconstant(NUMBER *q)
|
||||
long denlen; /* denominator length */
|
||||
HALF numlow; /* bottom value of numerator */
|
||||
HALF denlow; /* bottom value of denominator */
|
||||
long first; /* first non-null position found */
|
||||
BOOL havefirst;
|
||||
|
||||
if (constavail <= 0) {
|
||||
if (consttable == NULL) {
|
||||
initconstants();
|
||||
} else {
|
||||
tp = (NUMBER **) realloc((char *) consttable,
|
||||
sizeof(NUMBER *) * (constcount + CONSTALLOCSIZE));
|
||||
if (tp == NULL) {
|
||||
math_error("Unable to reallocate const table");
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
consttable = tp;
|
||||
constavail = CONSTALLOCSIZE;
|
||||
}
|
||||
}
|
||||
numlen = q->num.len;
|
||||
denlen = q->den.len;
|
||||
numlow = q->num.v[0];
|
||||
denlow = q->den.v[0];
|
||||
tp = &consttable[1];
|
||||
for (index = 1; index <= constcount; index++) {
|
||||
t = *tp++;
|
||||
first = 0;
|
||||
havefirst = FALSE;
|
||||
tp = consttable;
|
||||
for (index = 0; index < constcount; index++, tp++) {
|
||||
t = *tp;
|
||||
if (t->links == 0) {
|
||||
if (!havefirst) {
|
||||
havefirst = TRUE;
|
||||
first = index;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (q == t) {
|
||||
if (q->links == 1) {
|
||||
if (havefirst) {
|
||||
*tp = consttable[first];
|
||||
consttable[first] = q;
|
||||
} else {
|
||||
havefirst = TRUE;
|
||||
first = index;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
if ((numlen != t->num.len) || (numlow != t->num.v[0]))
|
||||
continue;
|
||||
if ((denlen != t->den.len) || (denlow != t->den.v[0]))
|
||||
@@ -74,40 +128,95 @@ addqconstant(NUMBER *q)
|
||||
if (q->num.sign != t->num.sign)
|
||||
continue;
|
||||
if (qcmp(q, t) == 0) {
|
||||
t->links++;
|
||||
qfree(q);
|
||||
return index;
|
||||
}
|
||||
}
|
||||
if (constavail <= 0) {
|
||||
if (consttable == NULL) {
|
||||
tp = (NUMBER **)
|
||||
malloc(sizeof(NUMBER *) * (CONSTALLOCSIZE + 1));
|
||||
*tp = NULL;
|
||||
} else
|
||||
tp = (NUMBER **) realloc((char *) consttable,
|
||||
sizeof(NUMBER *) * (constcount+CONSTALLOCSIZE + 1));
|
||||
if (tp == NULL)
|
||||
return 0;
|
||||
consttable = tp;
|
||||
constavail = CONSTALLOCSIZE;
|
||||
if (havefirst) {
|
||||
consttable[first] = q;
|
||||
return first;
|
||||
}
|
||||
constavail--;
|
||||
constcount++;
|
||||
consttable[constcount] = q;
|
||||
return constcount;
|
||||
consttable[constcount++] = q;
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Return the value of a constant number given its index.
|
||||
* Returns address of the number, or NULL if the index is illegal.
|
||||
* Returns address of the number, or NULL if the index is illegal
|
||||
* or points to freed position.
|
||||
*/
|
||||
NUMBER *
|
||||
constvalue(unsigned long index)
|
||||
{
|
||||
if ((index <= 0) || (index > constcount))
|
||||
return NULL;
|
||||
if (index >= constcount) {
|
||||
math_error("Bad index value for constvalue");
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
if (consttable[index]->links == 0) {
|
||||
math_error("Constvalue has been freed!!!");
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
return consttable[index];
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
freeconstant(unsigned long index)
|
||||
{
|
||||
NUMBER *q;
|
||||
|
||||
if (index >= constcount) {
|
||||
math_error("Bad index value for freeconst");
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
q = consttable[index];
|
||||
if (q->links == 0) {
|
||||
math_error("Attempting to free freed const location");
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
qfree(q);
|
||||
if (index == constcount - 1) {
|
||||
trimconstants();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
trimconstants(void)
|
||||
{
|
||||
NUMBER **qp;
|
||||
|
||||
qp = &consttable[constcount];
|
||||
while (constcount > 0 && (*--qp)->links == 0) {
|
||||
constcount--;
|
||||
constavail++;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
showconstants(void)
|
||||
{
|
||||
long index;
|
||||
NUMBER **qp;
|
||||
long count;
|
||||
|
||||
qp = consttable;
|
||||
count = 0;
|
||||
for (index = 0; index < constcount; index++, qp++) {
|
||||
if ((*qp)->links) {
|
||||
if (!count) {
|
||||
printf("\n Index Links Value\n");
|
||||
}
|
||||
count++;
|
||||
printf("\n%8ld%8ld ", index, (*qp)->links);
|
||||
fitprint(*qp, 40);
|
||||
}
|
||||
}
|
||||
printf("\n\nNumber = %ld\n", count);
|
||||
}
|
||||
|
||||
|
||||
/* END CODE */
|
||||
|
Reference in New Issue
Block a user