/** * Computes intersections using a O(log n) insertion method */ int intersectionsFast(int *left, int *right, int n) { int i, intersections, leftPos, rightPos; int *leftSorted, *rightSorted; leftSorted = (int*)malloc(n * sizeof(int)); rightSorted = (int*)malloc(n * sizeof(int)); /* Place initial values */ leftSorted[0] = left[0]; rightSorted[0] = right[0]; for (i = 1; i < n; i++) { leftPos = binaryInsert(leftSorted, left[i], i); rightPos = binaryInsert(rightSorted, right[i], i); if (leftPos > rightPos) { intersections += leftPos - rightPos; } else { intersections += rightPos - leftPos; } } free(leftSorted); free(rightSorted); return intersections; }
long rpn_create_mem(char *name, short is_string) { long i_mem; int32_t duplicate; MEMORY *newMem; if (is_func(name)!=-1 || find_udf(name)!=-1) { fprintf(stderr, "error: attempt to create rpn memory with reserved name \"%s\"\n", name); return -1; } if (Memory==NULL || n_memories>=max_n_memories) { Memory = trealloc(Memory, sizeof(*Memory)*(max_n_memories+=10)); memoryData = trealloc(memoryData, sizeof(*memoryData)*max_n_memories); str_memoryData = trealloc(str_memoryData, sizeof(*str_memoryData)*max_n_memories); } newMem = tmalloc(sizeof(*newMem)); newMem->name = name; /* temporary copy */ i_mem = binaryInsert((void**)Memory, n_memories, (void*)newMem, compare_mem, &duplicate); if (duplicate) { free(newMem); return Memory[i_mem]->index; } cp_str(&newMem->name, name); newMem->index = n_memories; newMem->is_string = is_string; memoryData[n_memories] = 0; str_memoryData[n_memories] = NULL; n_memories++; memory_added = 1; return Memory[i_mem]->index; }
void binary(Elements *tobetested) { int id; int i,j; int found; char message[STRINGLENGTH]; double dif; time_t start,end; Elements copy[NUMBER_OF]; binaryInialize(copy); binaryInsert(tobetested,copy); printf("Enter key number to be searched:\n"); scanf("%d",&id); //searching for a particular key printf("Start binary searching...\n"); time(&start); //for(i=0;i<COUNTER;i++) //for(j=0;j<COUNTER;j++) found=binarySearch(id,tobetested,message); printf("End binary seaching\n"); time(&end); dif = difftime (end,start);// time taken to find a particular key. if(found==0) // if not found printf("Key not found ,Please try with a different key\n"); if(found!=0)// if found show it. { printf("Binary search found key: %2d in %.lf0 microseconds\n",found,dif); for(j=0;j<STRINGLENGTH;j++) printf("%2c",message[j]); } printf("\n"); }
void add_element_links(ELEMENT_LINKS *links, NAMELIST_TEXT *nltext, LINE_LIST *beamline) { long n_links, src_position_code=0, n_targets, n_sources, mode_code=0; long targets, iTarget, j; char **targetList; ELEMENT_LIST *t_context, *s_context, **eptr, *eptr1; double dz_min, dz; #if DEBUG long i; #endif log_entry("add_element_links"); /* set namelist variables to defaults */ target = item = source = equation = exclude = NULL; /* must initialize these hear rather than in the .nl file * to avoid problems with str_tolower() and other operations */ cp_str(&source_position, "before"); cp_str(&mode, "dynamic"); /* process namelist text */ if (processNamelist(&link_elements, nltext)==NAMELIST_ERROR) bombElegant(NULL, NULL); if (target) str_toupper(target); if (exclude) str_toupper(exclude); if (item) str_toupper(item); if (source) str_toupper(source); if (source_position) str_tolower(source_position); else cp_str(&source_position, "nearest"); if (mode) str_tolower(mode); else cp_str(&mode, "dynamic"); if (echoNamelists) print_namelist(stdout, &link_elements); /* check for valid input */ if (!target) bombElegant("link target not named", NULL); if (!item) bombElegant("link item not named", NULL); if (!source) bombElegant("link source not named", NULL); if (!equation) bombElegant("link equation not given", NULL); if (!source_position || (src_position_code=match_string(source_position, src_position_name, N_SRC_POSITIONS, 0))<0) bombElegant("source_position not given/unknown", NULL); if (!mode || (mode_code=match_string(mode, link_mode, N_LINK_MODES, 0))<0) bombElegant("link mode not known", NULL); if (minimum>maximum) bombElegant("minimum>maximum", NULL); t_context = s_context = NULL; if (has_wildcards(target) && strchr(target, '-')) target = expand_ranges(target); if (exclude && strlen(exclude) && has_wildcards(exclude) && strchr(exclude, '-')) exclude = expand_ranges(exclude); if (!(t_context=wfind_element(target, &t_context, &(beamline->elem)))) { fprintf(stdout, "error: cannot make link with target element %s--not in beamline\n", target); fflush(stdout); exitElegant(1); } if (!(s_context=find_element(source, &s_context, &(beamline->elem)))) { fprintf(stdout, "error: cannot make link with source element %s--not in beamline\n", source); fflush(stdout); exitElegant(1); } targets = 0; targetList = NULL; /* make a list of all the unique element names that match this (possibly wildcard) target */ do { int32_t duplic; if (!exclude || !strlen(exclude) || !wild_match(t_context->name, exclude)) { targetList = SDDS_Realloc(targetList, sizeof(*targetList)*(targets+1)); binaryInsert((void**)targetList, targets, t_context->name, strcmp, &duplic); if (!duplic) targets++; } } while ((t_context=wfind_element(target, &t_context, &(beamline->elem)))); if (!targets) bombElegant("cannot make link--no targets found\n", NULL); /* note that targets==1 if all the targets have the same name ! */ for (iTarget=0; iTarget<targets; iTarget++) { n_links = links->n_links; target = targetList[iTarget]; t_context = NULL; t_context = find_element(target, &t_context, &(beamline->elem)); /* expand the arrays */ links->target_name = trealloc(links->target_name, sizeof(*links->target_name)*(n_links+1)); links->target_elem = trealloc(links->target_elem, sizeof(*links->target_elem)*(n_links+1)); links->item = trealloc(links->item, sizeof(*links->item)*(n_links+1)); links->target_param = trealloc(links->target_param, sizeof(*links->target_param)*(n_links+1)); links->source_name = trealloc(links->source_name, sizeof(*links->source_name)*(n_links+1)); links->source_position = trealloc(links->source_position, sizeof(*links->source_position)*(n_links+1)); links->flags = trealloc(links->flags, sizeof(*links->flags)*(n_links+1)); links->source_elem = trealloc(links->source_elem, sizeof(*links->source_elem)*(n_links+1)); links->equation = trealloc(links->equation, sizeof(*links->equation)*(n_links+1)); links->n_targets = trealloc(links->n_targets, sizeof(*links->n_targets)*(n_links+1)); links->initial_value = trealloc(links->initial_value, sizeof(*links->initial_value)*(n_links+1)); links->baseline_value = trealloc(links->baseline_value, sizeof(*links->baseline_value)*(n_links+1)); links->minimum = trealloc(links->minimum, sizeof(*links->minimum)*(n_links+1)); links->maximum = trealloc(links->maximum, sizeof(*links->maximum)*(n_links+1)); /* copy the basic data */ cp_str(links->target_name+n_links, target); cp_str(links->item+n_links, item); cp_str(links->source_name+n_links, source); cp_str(links->equation+n_links, equation); links->source_position[n_links] = src_position_code; links->flags[n_links] = link_mode_flag[mode_code]; links->minimum[n_links] = minimum; links->maximum[n_links] = maximum; /* make the list of pointers to targets */ eptr = tmalloc(sizeof(*eptr)); eptr[0] = t_context; if ((links->target_param[n_links] = confirm_parameter(item, t_context->type))<0) { fprintf(stdout, "error: element %s does not have a parameter %s\n", target, item); fflush(stdout); exitElegant(1); } n_targets = 1; while ((t_context=find_element(target, &t_context, &(beamline->elem)))) { eptr = trealloc(eptr, sizeof(*eptr)*(n_targets+1)); eptr[n_targets] = t_context; n_targets++; } links->baseline_value[n_links] = tmalloc(sizeof(*links->baseline_value[n_links])*n_targets); links->n_targets[n_links] = n_targets; links->target_elem[n_links] = eptr; t_context = links->target_elem[n_links][0]; switch (entity_description[eptr[0]->type].parameter[links->target_param[n_links]].type) { case IS_DOUBLE: links->initial_value[n_links] = *((double*)(eptr[0]->p_elem+entity_description[eptr[0]->type].parameter[links->target_param[n_links]].offset)); break; case IS_LONG: links->initial_value[n_links] = *((long*)(eptr[0]->p_elem+entity_description[eptr[0]->type].parameter[links->target_param[n_links]].offset)); break; default: bombElegant("invalid type of item for target of link", NULL); break; } for (j=0; j<n_targets; j++) links->baseline_value[n_links][j] = links->initial_value[n_links]; /* make the list of pointers to sources */ if (iTarget) { s_context = NULL; if (!(s_context=find_element(source, &s_context, &(beamline->elem)))) { fprintf(stdout, "error: cannot make link with source element %s--not in beamline\n", source); fflush(stdout); exitElegant(1); } } eptr = tmalloc(sizeof(*eptr)*(n_targets)); if (src_position_code==SRC_POSITION_SAME_OCCURENCE) { n_sources = 0; while (n_sources<n_targets) { eptr1 = NULL; s_context = NULL; while (find_element(source, &s_context, &(beamline->elem))) { if (s_context->occurence==links->target_elem[n_links][n_sources]->occurence) { eptr1 = s_context; break; } } if (!eptr1) { fprintf(stdout, "error: no %s element is found with the same occurence number as the %ld-th %s element--can't link as requested\n", source, n_sources, target); fflush(stdout); exitElegant(1); } eptr[n_sources++] = eptr1; } } else if (src_position_code==SRC_POSITION_NEAREST) { n_sources = 0; while (n_sources<n_targets) { dz_min = DBL_MAX; eptr1 = NULL; s_context = NULL; while (find_element(source, &s_context, &(beamline->elem))) { if ((dz = fabs(s_context->end_pos-links->target_elem[n_links][n_sources]->end_pos))<dz_min) { eptr1 = s_context; dz_min = dz; } } if (!eptr1) { fprintf(stdout, "error: no %s element is found near the %ld-th %s element--can't link as requested\n", source, n_sources, target); fflush(stdout); exitElegant(1); } eptr[n_sources++] = eptr1; } } else if (src_position_code==SRC_POSITION_ADJACENT) { n_sources = 0; while (n_sources<n_targets) { eptr1 = NULL; if ((eptr1=links->target_elem[n_links][n_sources]->pred)) { if (strcmp(eptr1->name, source)!=0) eptr1 = NULL; } if (!eptr1 && (eptr1=links->target_elem[n_links][n_sources]->succ)) { if (strcmp(eptr1->name, source)!=0) eptr1 = NULL; } if (!eptr1) { fprintf(stdout, "error: no %s element is found adjacent to the %ld-th %s element--can't link as requested\n", source, n_sources, target); fflush(stdout); exitElegant(1); } eptr[n_sources++] = eptr1; } } else if (src_position_code==SRC_POSITION_BEFORE) { if (links->target_elem[n_links][0]->end_pos<s_context->end_pos) { fprintf(stdout, "error: there is no %s element before the first %s element--can't link as requested\n", source, target); fflush(stdout); exitElegant(1); } eptr[0] = s_context; n_sources = 0; while (n_sources<n_targets) { eptr1 = NULL; do { if (s_context->end_pos<links->target_elem[n_links][n_sources]->end_pos) eptr1 = s_context; else if (s_context->end_pos==links->target_elem[n_links][n_sources]->end_pos) { eptr1 = s_context; break; } else break; } while (find_element(source, &s_context, &(beamline->elem))); if (!eptr1) { fprintf(stdout, "error: no %s element is found before the %ld-th %s element--can't link as requested\n", source, n_sources, target); fflush(stdout); exitElegant(1); } eptr[n_sources++] = eptr1; s_context = eptr[n_sources-1]; } } else if (src_position_code==SRC_POSITION_AFTER) { if (links->target_elem[n_links][0]->end_pos>=s_context->end_pos) { /* search for first source element after first target element */ while (find_element(source, &s_context, &(beamline->elem))) { if (links->target_elem[n_links][0]->end_pos<s_context->end_pos) break; } if (!s_context) { fprintf(stdout, "error: no %s element after the first %s element--can't link as requested\n", source, target); fflush(stdout); exitElegant(1); } } eptr[0] = s_context; n_sources = 1; while (n_sources<n_targets) { s_context = links->target_elem[n_links][n_sources-1]; while (find_element(source, &s_context, &(beamline->elem))) { if (s_context->end_pos>links->target_elem[n_links][n_sources]->end_pos) break; } if (!s_context) { fprintf(stdout, "error: no %s element is found after the %ld-th %s element--can't link as requested\n", source, n_sources, target); fflush(stdout); exitElegant(1); } eptr[n_sources++] = s_context; } } links->source_elem[n_links] = eptr; #if DEBUG fprintf(stdout, "list of targets and sources:\n"); fflush(stdout); for (i=0; i<n_targets; i++) fprintf(stdout, "%s at z=%em linked to %s at z=%em\n", links->target_elem[n_links][i]->name, links->target_elem[n_links][i]->end_pos, links->source_elem[n_links][i]->name, links->source_elem[n_links][i]->end_pos); fflush(stdout); #endif links->n_links += 1; } log_exit("add_element_links"); }