Ian Jauslin
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Jauslin <ian.jauslin@roma1.infn.it>2015-06-14 00:52:45 +0000
committerIan Jauslin <ian.jauslin@roma1.infn.it>2015-06-14 00:52:45 +0000
commitaa0f3ae2988d372b190b9bde2e75a6d17e744e93 (patch)
tree14482245c2fca27fcdad3078e97d0871352d52a7 /src/tools.c
Initial commitv1.2
Diffstat (limited to 'src/tools.c')
-rw-r--r--src/tools.c142
1 files changed, 142 insertions, 0 deletions
diff --git a/src/tools.c b/src/tools.c
new file mode 100644
index 0000000..2b7e85d
--- /dev/null
+++ b/src/tools.c
@@ -0,0 +1,142 @@
+/*
+Copyright 2015 Ian Jauslin
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+#include "tools.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+int factorial(int n){
+ int i;
+ int res=1;
+ for(i=2;i<=n;i++){
+ res*=i;
+ }
+ return(res);
+}
+
+int ipower(int n, int p){
+ int i;
+ int res=1;
+ for(i=1;i<=p;i++){
+ res*=n;
+ }
+ return(res);
+}
+
+// power of a double
+long double dpower(long double x, int p){
+ int i;
+ long double res=1.;
+ if(p>=0){
+ for(i=1;i<=p;i++){
+ res*=x;
+ }
+ }
+ else{
+ for(i=1;i<=-p;i++){
+ res/=x;
+ }
+ }
+
+ return(res);
+}
+
+
+// find an element in a list whose first element is its size
+int list_find(int* list, int x){
+ int i;
+ for(i=1;i<=*list;i++){
+ if(list[i]==x){return(i);}
+ }
+ return(0);
+}
+
+// find an element in a list whose first element is not its number of elements (skips first element)
+int unlist_find(int* list, int size, int x){
+ int i;
+ for(i=1;i<size;i++){
+ if(list[i]==x){return(i);}
+ }
+ fprintf(stderr,"error: element %d not found\n",x);
+ exit(-1);
+}
+// find an element in a list whose first element is not its number of elements (skips first element)
+// no error reporting
+int unlist_find_noerr(int* list, int size, int x){
+ int i;
+ for(i=1;i<size;i++){
+ if(list[i]==x){return(i);}
+ }
+ return(-1);
+}
+
+
+// find an element in a list (checks first element)
+int intlist_find(int* list, int size, int x){
+ int i;
+ for(i=0;i<size;i++){
+ if(list[i]==x){return(i);}
+ }
+ return(-1);
+}
+// with error
+int intlist_find_err(int* list, int size, int x){
+ int i;
+ for(i=0;i<size;i++){
+ if(list[i]==x){return(i);}
+ }
+ fprintf(stderr,"error: element %d not found\n",x);
+ exit(-1);
+}
+
+
+
+// compare two lists
+int list_cmp(int* list1, int* list2){
+ if(*list1!=*list2){
+ return(0);
+ }
+ int* ptr1=list1+1;
+ int* ptr2=list2+1;
+ int i;
+ for(i=1;i<=*list1;i++){
+ if(*ptr1!=*ptr2){
+ return(0);
+ }
+ ptr1++;
+ ptr2++;
+ }
+ return(1);
+}
+
+
+// max and min
+int max(int x1, int x2){
+ if(x1>=x2){
+ return(x1);
+ }
+ else{
+ return(x2);
+ }
+}
+int min(int x1, int x2){
+ if(x1<=x2){
+ return(x1);
+ }
+ else{
+ return(x2);
+ }
+}