TPCCLIB
Loading...
Searching...
No Matches
intutil.c File Reference

Working with integer values. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "tpcextensions.h"

Go to the source code of this file.

Functions

int atoiCheck (const char *s, int *v)
unsigned int factorial (unsigned int n)
unsigned long long int lfactorial (unsigned long long int n)
uint32_t sqrt_uint64 (uint64_t n)
void intlistInit (INTLIST *l)
void intlistFree (INTLIST *l)
int intlistAdd (INTLIST *l, const int v, const int ifnew)
void intlistSort (INTLIST *l)
int intlistAddFromString (const char *s1, const char *s2, INTLIST *l, const int ifnew)
int intlistExpandFromString (const char *s1, const char *s2, INTLIST *l, const int ifnew)

Detailed Description

Working with integer values.

Author
Vesa Oikonen

Definition in file intutil.c.

Function Documentation

◆ atoiCheck()

int atoiCheck ( const char * s,
int * v )

Verifies that argument string at least seems like a valid integer. '\0' must follow the integer part in the string. Optional result integer value is set to 0 if string was not valid value.

See also
atofCheck, intlistAddFromString, strIsValidNumber
Returns
0 if successful, and 1 in case of an error.
Parameters
sString which is converted to an integer; string must not contain any space characters.
vPointer to the integer; enter NULL, if not needed.

Definition at line 25 of file intutil.c.

30 {
31 if(v!=NULL) *v=0;
32 if(s==NULL || strnlen(s, 2)<1) return(1);
33 errno=0; char *tail;
34 *v=strtol(s, &tail, 10);
35 if(errno) {*v=0; return(2);}
36 if(*tail) {*v=0; return(3);}
37 return 0;
38}
size_t strnlen(const char *s, size_t n)
Definition stringext.c:565

Referenced by intlistAddFromString(), and tacReadQView().

◆ factorial()

unsigned int factorial ( unsigned int n)

Calculate factorial of given number.

See also
lfactorial, igam, igamc
Returns
Returns factorial n!, or zero if factorial would cause wrap-around.
Parameters
nInteger n, from which the factorial is calculated.
Note
Wrap-around will occur if n>12.

Definition at line 46 of file intutil.c.

51 {
52 if(n<1) return(1);
53 if(n>12) return(0);
54 return(n*factorial(n-1));
55}
unsigned int factorial(unsigned int n)
Definition intutil.c:46

Referenced by factorial().

◆ intlistAdd()

int intlistAdd ( INTLIST * l,
const int v,
const int ifnew )

Add one integer to the integer array in INTLIST structure.

See also
intlistInit, intlistFree, intlistSort, intlistAddFromString, strncatInt
Author
Vesa Oikonen
Returns
Returns the nr of added integers (either 0 or 1), or <0 in case of an error.
Parameters
lPointer to initiated or filled INTLIST structure.
vInteger value to add.
ifnewAdd integer to the list only if it is new (0=no, 1=yes).

Definition at line 130 of file intutil.c.

137 {
138 if(l==NULL) return(-1);
139 /* If only new value is to be added, then check that this is new */
140 if(ifnew!=0) {for(int i=0; i<l->nr; i++) if(l->i[i]==v) return(0);}
141 /* Add value to list if there is space left, and quit */
142 if(l->_nr>l->nr) {l->i[l->nr++]=v; return(1);}
143 /* Allocate more space */
144 if(l->_nr==0) l->i=(int*)malloc(10*sizeof(int));
145 else l->i=(int*)realloc(l->i, (10+l->_nr)*sizeof(int));
146 if(l->i==NULL) {l->nr=l->_nr=0; return(-2);}
147 l->_nr+=10;
148 /* Add value to list */
149 l->i[l->nr++]=v;
150 return(1);
151}

Referenced by intlistAddFromString(), and intlistExpandFromString().

◆ intlistAddFromString()

int intlistAddFromString ( const char * s1,
const char * s2,
INTLIST * l,
const int ifnew )

Read a list of integer values from given string with given delimiters.

See also
strTokenNr, strTokenNCpy, intlistAdd, intlistInit, intlistFree, intlistSort, intlistExpandFromString.
Returns
The number of added integer values, or <0 in case of an error.
Author
Vesa Oikonen
Parameters
s1Pointer to string from which the integers are read, for example "2,3,6,8".
s2String containing character delimiters, for example ", ".
lPointer to INTLIST structure; previous contents are preserved.
ifnewAdd integer to the list only if it is new (0=no, 1=yes).

Definition at line 178 of file intutil.c.

187 {
188 if(l==NULL) return(-1);
189 if(s1==NULL || s2==NULL) return(0);
190
191 /* Get the nr of tokens */
192 int i, j, m, n, v;
193 n=strTokenNr((char*)s1, s2); if(n<1) return(0);
194 /* Read the values */
195 char tmp[128];
196 for(i=j=0; i<n; i++) {
197 if(strTokenNCpy(s1, s2, 1+i, tmp, 128)<1) return(-2);
198 if(atoiCheck(tmp, &v)) return(-3);
199 m=intlistAdd(l, v, ifnew); if(m<0) return(-4);
200 j+=m;
201 }
202 return(j);
203}
int atoiCheck(const char *s, int *v)
Definition intutil.c:25
int intlistAdd(INTLIST *l, const int v, const int ifnew)
Definition intutil.c:130
int strTokenNr(const char *s1, const char *s2)
Definition stringext.c:25
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition stringext.c:53

◆ intlistExpandFromString()

int intlistExpandFromString ( const char * s1,
const char * s2,
INTLIST * l,
const int ifnew )

Read ranges and individual integer values from given string with given delimiters.

See also
strTokenNCpy, intlistAddFromString, intlistInit, intlistFree, intlistSort, strIsValidNumber
Returns
The number of added integer values, or <0 in case of an error.
Author
Vesa Oikonen
Parameters
s1Pointer to string from which the integers are read, for example "0-8,12,32-28" or "0..8, 12, 28..34".
s2String containing character delimiters, for example ", ".
lPointer to INTLIST struct; previous contents are preserved.
ifnewAdd integer to the list only if it is new (0=no, 1=yes).

Definition at line 212 of file intutil.c.

222 {
223 if(l==NULL) return(-1);
224 if(s1==NULL || s2==NULL) return(0);
225
226 /* Get the nr of tokens */
227 int n=strTokenNr((char*)s1, s2); if(n<1) return(0);
228 /* Read the values */
229 char tmp[128], tmp2[128], *t, *tail;
230 int i, j, m, first, last, sw;
231 for(i=j=0; i<n; i++) {
232 if(strTokenNCpy(s1, s2, 1+i, tmp, 128)<1) return(-2);
233 t=tmp; errno=0;
234 first=strtol(t, &tail, 10); if(errno) return(-3);
235 if(*tail) {
236 strcpy(tmp2, tail); t=tmp2;
237 if(*t=='-') t++;
238 else if(*t=='.') {t++; if(*t=='.') t++; else return(-4);}
239 else return(-5);
240 if(!*t) return(-6);
241 last=strtol(t, &tail, 10); if(errno) return(-7);
242 if(*tail) return(-8);
243 } else {
244 last=first;
245 }
246
247 if(first>last) {sw=first; first=last; last=sw;}
248 for(int v=first; v<=last; v++) {
249 m=intlistAdd(l, v, ifnew); if(m<0) return(-10);
250 j+=m;
251 }
252 }
253 return(j);
254}

Referenced by parSelectParameters(), and parSelectTACs().

◆ intlistFree()

void intlistFree ( INTLIST * l)

Free memory allocated for INTLIST. All data is cleared.

See also
intlistInit
Author
Vesa Oikonen
Parameters
lPointer to INTLIST structure

Definition at line 114 of file intutil.c.

117 {
118 if(l==NULL) return;
119 free(l->i);
120 intlistInit(l);
121}
void intlistInit(INTLIST *l)
Definition intutil.c:102

Referenced by parSelectParameters(), and parSelectTACs().

◆ intlistInit()

void intlistInit ( INTLIST * l)

Initiate the INTLIST structure before any use.

See also
intlistFree, intlistAdd, intlistSort
Author
Vesa Oikonen
Parameters
lPointer to INTLIST.

Definition at line 102 of file intutil.c.

105 {
106 if(l==NULL) return;
107 l->nr=0; l->_nr=0;
108 l->i=NULL;
109}

Referenced by intlistFree(), parSelectParameters(), and parSelectTACs().

◆ intlistSort()

void intlistSort ( INTLIST * l)

Sort the integer array in INTLIST structure.

See also
intlistInit, intlistFree, intlistAdd
Author
Vesa Oikonen
Parameters
lPointer to INTLIST structure.

Definition at line 159 of file intutil.c.

162 {
163 if(l==NULL || l->i==NULL || l->nr<2) return;
164 int i, j, v;
165 for(i=0; i<l->nr; i++) for(j=i+1; j<l->nr; j++) {
166 if(l->i[i]>l->i[j]) {v=l->i[i]; l->i[i]=l->i[j]; l->i[j]=v;}
167 }
168}

Referenced by parSelectParameters(), and parSelectTACs().

◆ lfactorial()

unsigned long long int lfactorial ( unsigned long long int n)

Calculate factorial of given number.

See also
factorial, igam, igamc
Returns
Returns factorial n!, or zero if factorial would cause wrap-around.
Parameters
nInteger n, from which the factorial is calculated.
Note
Wrap-around will occur if n>20.

Definition at line 63 of file intutil.c.

68 {
69 if(n<1) return(1);
70 if(n>20) return(0);
71 return(n*lfactorial(n-1));
72}
unsigned long long int lfactorial(unsigned long long int n)
Definition intutil.c:63

Referenced by lfactorial(), mfEvalInt(), and mfEvalY().

◆ sqrt_uint64()

uint32_t sqrt_uint64 ( uint64_t n)

Calculate integer square root of a positive integer.

Returns
Returns the square root, round down to an integer.

Definition at line 79 of file intutil.c.

82 {
83 uint32_t r=0, a=1;
84 a <<= 31;
85 uint32_t temp;
86 uint64_t quad;
87 while(a>0) {
88 quad = temp = r+a;
89 quad *= temp;
90 if(n>=quad) r=temp;
91 a >>= 1;
92 }
93 return(r);
94}