TPCCLIB
Loading...
Searching...
No Matches
intutil.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <ctype.h>
14#include <math.h>
15#include "tpcextensions.h"
16/*****************************************************************************/
17
18/*****************************************************************************/
27 const char *s,
29 int *v
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}
39/*****************************************************************************/
40
41/*****************************************************************************/
46unsigned int factorial(
50 unsigned int n
51) {
52 if(n<1) return(1);
53 if(n>12) return(0);
54 return(n*factorial(n-1));
55}
56/*****************************************************************************/
57
58/*****************************************************************************/
63unsigned long long int lfactorial(
67 unsigned long long int n
68) {
69 if(n<1) return(1);
70 if(n>20) return(0);
71 return(n*lfactorial(n-1));
72}
73/*****************************************************************************/
74
75/*****************************************************************************/
79uint32_t sqrt_uint64(
80 /* Unsigned integer */
81 uint64_t n
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}
95/*****************************************************************************/
96
97/*****************************************************************************/
104 INTLIST *l
105) {
106 if(l==NULL) return;
107 l->nr=0; l->_nr=0;
108 l->i=NULL;
109}
110
116 INTLIST *l
117) {
118 if(l==NULL) return;
119 free(l->i);
120 intlistInit(l);
121}
122/*****************************************************************************/
123
124/*****************************************************************************/
132 INTLIST *l,
134 const int v,
136 const int ifnew
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}
152/*****************************************************************************/
153
154/*****************************************************************************/
161 INTLIST *l
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}
169/*****************************************************************************/
170
171/*****************************************************************************/
180 const char *s1,
182 const char *s2,
184 INTLIST *l,
186 const int ifnew
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}
204/*****************************************************************************/
205
206/*****************************************************************************/
215 const char *s1,
217 const char *s2,
219 INTLIST *l,
221 const int ifnew
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}
255/*****************************************************************************/
256
257/*****************************************************************************/
uint32_t sqrt_uint64(uint64_t n)
Definition intutil.c:79
void intlistInit(INTLIST *l)
Definition intutil.c:102
int intlistExpandFromString(const char *s1, const char *s2, INTLIST *l, const int ifnew)
Definition intutil.c:212
unsigned long long int lfactorial(unsigned long long int n)
Definition intutil.c:63
void intlistFree(INTLIST *l)
Definition intutil.c:114
void intlistSort(INTLIST *l)
Definition intutil.c:159
int atoiCheck(const char *s, int *v)
Definition intutil.c:25
int intlistAddFromString(const char *s1, const char *s2, INTLIST *l, const int ifnew)
Definition intutil.c:178
int intlistAdd(INTLIST *l, const int v, const int ifnew)
Definition intutil.c:130
unsigned int factorial(unsigned int n)
Definition intutil.c:46
int strTokenNr(const char *s1, const char *s2)
Definition stringext.c:25
size_t strnlen(const char *s, size_t n)
Definition stringext.c:565
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition stringext.c:53
Header file for library libtpcextensions.