xindex.c

来自「seismic software,very useful」· C语言 代码 · 共 92 行

C
92
字号
/* Copyright (c) Colorado School of Mines, 1990./* All rights reserved.                       */voidxindex (int nx, float ax[], float x, int *index)/*****************************************************************************determine index of x with respect to an array of x values******************************************************************************Input:nx		number of x values in array axax		array[nx] of monotonically increasing or decreasing x valuesx		the value for which index is to be determinedindex		index determined previously (used to begin search)Output:index		for monotonically increasing ax values, the largest index		for which ax[index]<=x, except index=0 if ax[0]>x;		for monotonically decreasing ax values, the largest index		for which ax[index]>=x, except index=0 if ax[0]<x******************************************************************************Notes:This function is designed to be particularly efficient when calledrepeatedly for slightly changing x values; in such cases, the index returned from one call should be used in the next.******************************************************************************Author:		Dave Hale, Colorado School of Mines, 12/25/89*****************************************************************************/{	int lower,upper,middle,step;	/* initialize lower and upper indices and step */	lower = *index;	if (lower<0) lower = 0;	if (lower>=nx) lower = nx-1;	upper = lower+1;	step = 1;	/* if x values increasing */	if (ax[nx-1]>ax[0]) {		/* find indices such that ax[lower] <= x < ax[upper] */		while (lower>0 && ax[lower]>x) {			upper = lower;			lower -= step;			step += step;		}		if (lower<0) lower = 0;		while (upper<nx && ax[upper]<=x) {			lower = upper;			upper += step;			step += step;		}		if (upper>nx) upper = nx;		/* find index via bisection */		while ((middle=(lower+upper)>>1)!=lower) {			if (x>=ax[middle])				lower = middle;			else				upper = middle;		}	/* else, if not increasing */	} else {		/* find indices such that ax[lower] >= x > ax[upper] */		while (lower>0 && ax[lower]<x) {			upper = lower;			lower -= step;			step += step;		}		if (lower<0) lower = 0;		while (upper<nx && ax[upper]>=x) {			lower = upper;			upper += step;			step += step;		}		if (upper>nx) upper = nx;		/* find index via bisection */		while ((middle=(lower+upper)>>1)!=lower) {			if (x<=ax[middle])				lower = middle;			else				upper = middle;		}	}	/* return lower index */	*index = lower;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?