diff options
author | Ian Jauslin <ian.jauslin@roma1.infn.it> | 2015-06-14 00:52:45 +0000 |
---|---|---|
committer | Ian Jauslin <ian.jauslin@roma1.infn.it> | 2015-06-14 00:52:45 +0000 |
commit | aa0f3ae2988d372b190b9bde2e75a6d17e744e93 (patch) | |
tree | 14482245c2fca27fcdad3078e97d0871352d52a7 /src/idtable.c |
Initial commitv1.2
Diffstat (limited to 'src/idtable.c')
-rw-r--r-- | src/idtable.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/idtable.c b/src/idtable.c new file mode 100644 index 0000000..fe409c2 --- /dev/null +++ b/src/idtable.c @@ -0,0 +1,122 @@ +/* +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 "idtable.h" +#include <stdio.h> +#include <stdlib.h> +#include "array.h" +#include "polynomial.h" + +// allocate memory +int init_Id_Table(Id_Table* idtable,int size){ + (*idtable).indices=calloc(size,sizeof(int)); + (*idtable).polynomials=calloc(size,sizeof(Polynomial)); + (*idtable).length=0; + (*idtable).memory=size; + return(0); +} + +// free memory +int free_Id_Table(Id_Table idtable){ + int i; + for(i=0;i<idtable.length;i++){ + free_Polynomial(idtable.polynomials[i]); + } + free(idtable.indices); + free(idtable.polynomials); + + return(0); +} + +// resize the memory allocated to a idtable +int resize_idtable(Id_Table* idtable,int new_size){ + Id_Table new_idtable; + int i; + + init_Id_Table(&new_idtable,new_size); + for(i=0;i<(*idtable).length;i++){ + new_idtable.indices[i]=(*idtable).indices[i]; + new_idtable.polynomials[i]=(*idtable).polynomials[i]; + } + new_idtable.length=(*idtable).length; + + free((*idtable).indices); + free((*idtable).polynomials); + + *idtable=new_idtable; + return(0); +} + +// copy an idtable +int idtable_cpy(Id_Table input, Id_Table* output){ + init_Id_Table(output,input.length); + idtable_cpy_noinit(input,output); + return(0); +} +int idtable_cpy_noinit(Id_Table input, Id_Table* output){ + int i; + if((*output).memory<input.length){ + fprintf(stderr,"error: trying to copy an idtable of length %d to another with memory %d\n",input.length,(*output).memory); + exit(-1); + } + for(i=0;i<input.length;i++){ + (*output).indices[i]=input.indices[i]; + polynomial_cpy(input.polynomials[i],(*output).polynomials+i); + } + (*output).length=input.length; + + return(0); +} + +// append an element to a idtable +int idtable_append(int index, Polynomial polynomial, Id_Table* output){ + int offset=(*output).length; + + if((*output).length>=(*output).memory){ + resize_idtable(output,2*(*output).memory); + } + + // copy and allocate + polynomial_cpy(polynomial,(*output).polynomials+offset); + (*output).indices[offset]=index; + // increment length + (*output).length++; + return(0); +} +// append an element to a idtable without allocating memory +int idtable_append_noinit(int index, Polynomial polynomial, Id_Table* output){ + int offset=(*output).length; + + if((*output).length>=(*output).memory){ + resize_idtable(output,2*(*output).memory); + } + + // copy without allocating + (*output).indices[offset]=index; + (*output).polynomials[offset]=polynomial; + // increment length + (*output).length++; + return(0); +} + +// concatenate two idtables +int idtable_concat(Id_Table input, Id_Table* output){ + int i; + for(i=0;i<input.length;i++){ + idtable_append(input.indices[i],input.polynomials[i],output); + } + return(0); +} |