/* Copyright 2016 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. */ /* Base functions for arrays see polynomial_*.h for the values taken by ARRAY_FUNC, etc... */ // init int ARRAY_FUNC(init) (ARRAY_TYPENAME* array, unsigned int memory){ array->values=calloc(memory,sizeof(ARRAY_VAL_TYPE)); array->memory=memory; array->length=0; return(0); } int ARRAY_FUNC(free) (ARRAY_TYPENAME array){ #ifdef ARRAY_VAL_FREE unsigned int i; for(i=0;ilength;i++){ ARRAY_VAL_SET(new_array.values[i], array->values[i]); } free(array->values); *array=new_array; return(0); } // add a value int ARRAY_FUNC(append) (ARRAY_VAL_TYPE val, ARRAY_TYPENAME* output){ if(output->length >= output->memory){ ARRAY_FUNC(resize) (output,2*output->memory+1); } ARRAY_VAL_CPY(val, output->values[output->length]); output->length++; return(0); } // do not copy the value, instead let the last element of the array point to 'val' // only if the value requires initializing #ifdef ARRAY_VAL_FREE int ARRAY_FUNC(append_noinit) (ARRAY_VAL_TYPE val, ARRAY_TYPENAME* output){ if(output->length >= output->memory){ ARRAY_FUNC(resize) (output,2*output->memory+1); } ARRAY_VAL_SET(output->values[output->length], val); output->length++; return(0); } #endif // add a value only if it is not already present #ifdef ARRAY_VAL_IFEQ int ARRAY_FUNC(append_unique) (ARRAY_VAL_TYPE val, ARRAY_TYPENAME* output){ if(ARRAY_FUNC(find) (val, *output)<0){ ARRAY_FUNC(append) (val, output); } return(0); } #endif // copy int ARRAY_FUNC(cpy) (ARRAY_TYPENAME input, ARRAY_TYPENAME* output){ ARRAY_FUNC(init) (output, input.length); ARRAY_FUNC(cpy_noinit) (input, output); return(0); } // do not init output array int ARRAY_FUNC(cpy_noinit) (ARRAY_TYPENAME input, ARRAY_TYPENAME* output){ unsigned int i; if(output->memoryvalues[i]); } output->length=input.length; return(0); } // concatenate int ARRAY_FUNC(concat) (ARRAY_TYPENAME input, ARRAY_TYPENAME* output){ unsigned int i; for(i=0;iend || end>=array.length){ return(LIBINUM_ERROR_ILLEGAL_MEMORY_ACCESS); } ARRAY_FUNC(init) (subarray,end-begin); for(i=begin;i<=end;i++){ ARRAY_FUNC(append) (array.values[i], subarray); } return(0); } // find (does not assume the array is sorted) #ifdef ARRAY_VAL_IFEQ int ARRAY_FUNC(find) (ARRAY_VAL_TYPE val, ARRAY_TYPENAME array){ unsigned int i; for(i=0;i1){ ARRAY_FUNC(sort_sub) (array, 0, array.length-1); } return(0); } // sort a sub-array int ARRAY_FUNC(sort_sub) (ARRAY_TYPENAME array, unsigned int begin, unsigned int end){ unsigned int i; unsigned int index; ARRAY_VAL_TYPE tmp; // the pivot: middle of the array unsigned int pivot=(begin+end)/2; // if the array is non trivial if(begin0){ ARRAY_FUNC(sort_sub) (array, begin, index-1); } ARRAY_FUNC(sort_sub) (array, index+1, end); } return(0); } #endif // compare arrays #if defined ARRAY_VAL_IFLT && defined ARRAY_VAL_IFGT int ARRAY_FUNC(cmp) (ARRAY_TYPENAME array1, ARRAY_TYPENAME array2){ unsigned int i; // compare lengths if(array1.lengtharray2.length){ return(1); } // compare terms for(i=0;imemorylength; ivalues+i, 2); (array->length)++; } return(0); } #endif