/* Copyright 2015-2022 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 "array.h" #include #include #include #include "istring.h" #include "definitions.cpp" // init int init_Int_Array(Int_Array* array, int memory){ (*array).values=calloc(memory,sizeof(int)); (*array).memory=memory; (*array).length=0; return(0); } int free_Int_Array(Int_Array array){ free(array.values); return(0); } // copy int int_array_cpy(Int_Array input, Int_Array* output){ init_Int_Array(output,input.length); int_array_cpy_noinit(input,output); return(0); } int int_array_cpy_noinit(Int_Array input, Int_Array* output){ int i; if((*output).memory=(*output).memory){ int_array_resize(output,2*(*output).memory+1); } (*output).values[(*output).length]=val; (*output).length++; return(0); } // add a value only if it is not already present int int_array_append_unique(int val, Int_Array* output){ if(int_array_find(val,*output)<0){ int_array_append(val,output); } return(0); } // concatenate int int_array_concat(Int_Array input, Int_Array* output){ int i; int offset=(*output).length; if((*output).length+input.length>(*output).memory){ // make it longer than needed by (*output).length (for speed) int_array_resize(output,2*(*output).length+input.length); } for(i=0;iarray2.length){ return(1); } // compare terms for(i=0;iarray2.values[i]){ return(1); } } // if equal return(0); } // check whether an array is a sub-array of another // allows for the elements to be separated by others, but the order must be respected int int_array_is_subarray_ordered(Int_Array input, Int_Array test_array){ int i; int matches=0; for(i=0;i0){ printf("%d)",array.values[array.length-1]); } else{ printf(")"); } return(0); } // read array int int_array_read(Char_Array str, Int_Array* array){ char* buffer=calloc(str.length+1,sizeof(char)); char* buffer_ptr=buffer; int i,j; int comment_mode=0; // alloc init_Int_Array(array,str.length); *buffer_ptr='\0'; // loop over the input for(j=0;j=(*output).memory){ char_array_resize(output,2*(*output).memory+1); } (*output).str[(*output).length]=val; (*output).length++; return(0); } // append a string int char_array_append_str(char* str, Char_Array* output){ char* ptr; for (ptr=str;*ptr!='\0';ptr++){ char_array_append(*ptr, output); } return(0); } // concatenate int char_array_concat(Char_Array input, Char_Array* output){ int i; int offset=(*output).length; if((*output).length+input.length>(*output).memory){ // make it longer than needed by (*output).length (for speed) char_array_resize(output,2*(*output).length+input.length); } for(i=0;iend || begin<0 || end>=str.length){ fprintf(stderr,"error: cannot extract a substring [%d,%d] from a string of length %d\n", begin, end, str.length); exit(-1); } init_Char_Array(substr,end-begin); for(i=begin;i<=end;i++){ char_array_append(str.str[i],substr); } return(0); } // convert to char* int char_array_to_str(Char_Array input, char** output){ int i; (*output)=calloc(input.length+1,sizeof(char)); for(i=0;isize){ // resize free(out_str); // +1 for '\0' size=extra_size+1; out_str=calloc(size,sizeof(char)); // read format again va_start(vaptr, fmt); vsnprintf(out_str,size,fmt,vaptr); va_end(vaptr); } // write to char array for(ptr=out_str;*ptr!='\0';ptr++){ char_array_append(*ptr, output); } free(out_str); return(0); } // replace '%' with given character int replace_star(char c, Char_Array str, Char_Array* out){ int i; init_Char_Array(out, str.length); for(i=0;i=(*output).memory){ str_array_resize(output,2*(*output).memory+1); } char_array_cpy(val, (*output).strs+(*output).length); (*output).length++; return(0); } int str_array_append_noinit(Char_Array val, Str_Array* output){ if((*output).length>=(*output).memory){ str_array_resize(output,2*(*output).memory+1); } (*output).strs[(*output).length]=val; (*output).length++; return(0); } // concatenate int str_array_concat(Str_Array input, Str_Array* output){ int i; int offset=(*output).length; if((*output).length+input.length>(*output).memory){ // make it longer than needed by (*output).length (for speed) str_array_resize(output,2*(*output).length+input.length); } for(i=0;i(*output).memory){ // make it longer than needed by (*output).length (for speed) str_array_resize(output,2*(*output).length+input.length); } for(i=0;i=(*output).memory){ resize_labels(output,2*(*output).memory); } // copy and allocate char_array_cpy(label,(*output).labels+offset); (*output).indices[offset]=index; // increment length (*output).length++; return(0); } // append an element to a labels without allocating memory int labels_append_noinit(Char_Array label, int index, Labels* output){ int offset=(*output).length; if((*output).length>=(*output).memory){ resize_labels(output,2*(*output).memory); } // copy without allocating (*output).labels[offset]=label; (*output).indices[offset]=index; // increment length (*output).length++; return(0); } // concatenate two labels tables int labels_concat(Labels input, Labels* output){ int i; for(i=0;i