bsearch for Windows ce [英] bsearch for Windows ce

查看:70
本文介绍了bsearch for Windows ce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发现Windows CE没有包含bsearch功能。

有人能指出我正确的方向来解决这个问题吗? >
谢谢,

val

解决方案

va **** @ hotmail.com (val)写道:

我发现Windows CE不包含bsearch功能。




这里有一个替代它(在我看来)改进了

界面。它来自一个真实的程序,所以欢迎批评和bug

报告。


/ *比较A和B,给定辅助数据AUX,和返回

strcmp() - 类型结果。 * /

typedef int algo_compare_func(const void * a,const void * b,void * aux);


/ *搜索ARRAY,其中包含COUNT个每个SIZE字节,使用

a二进制搜索。如果

存在,则返回任何等于VALUE的元素,否则返回空指针。 ARRAY必须根据COMPARE订购

。 AUX作为辅助

数据传递给COMPARE。 * /

void *

binary_search(const void * array,size_t count,size_t size,

void * value,

algo_compare_func * compare,void * aux)

{

assert(array!= NULL);

assert(count< = INT_MAX);

断言(比较!= NULL);


if(count!= 0)

{

const unsigned char * first = array;

int low = 0;

int high = count - 1;


while(低< =高)

{

int middle =(low + high)/ 2;

const unsigned char * element = first + middle * size;

int cmp = compare(value,element,aux);


if(cmp> 0)

low = mid + 1;

else if(cmp< 0)

high = middle - 1;

否则

返回(void *)元素;

}

}


返回NULL; < br $>
}


-

"什么适合主人不适合新手。

你必须在超越结构之前理解道。

- 编程之道


val写道:


我发现Windows CE没有包含bsearch功能。可以
有人指出我正确的方向来解决这个问题。




bsearch()是一个标准的C函数,至少从那时起/>
1989.抱怨编译器供应商。它与操作系统无关。

操作系统。

另一种选择是以另一个名称编写自己的本地

已建议。但是这样你就无法利用可能的b $ b系统优化。


-

查克F(cb ****) ****@yahoo.com)(cb********@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>使用worldnet地址!


CBFalconer< cb ******** @ yahoo.com>写道:


[bsearch]

另一种选择是以Ben
建议用另一个名字写自己的。但是这样你就无法利用可能的系统优化。




对于
$ b是否有有意义的优化是值得怀疑的$ b函数可能会花大部分时间通过指针调用另一个函数
。在现代架构中尝试优化bsearch()来优化bsearch()可能是一个失败的原因。我注意到,对于

的例子,glibc中bsearch的实现是用C语言编写的,而没有太多的优化尝试。

-

Ben Pfaff

电子邮件: bl*@cs.stanford.edu

web: http://benpfaff.org


Hi,
I found that Windows CE doesn''t include bsearch function.
Can somebody point me into right direction in order to solve that issue
Thanks,
val

解决方案

va****@hotmail.com (val) writes:

I found that Windows CE doesn''t include bsearch function.



Here''s a replacement for it with an (in my opinion) improved
interface. It''s from a real program, so criticism and bug
reports are welcomed.

/* Compares A and B, given auxiliary data AUX, and returns a
strcmp()-type result. */
typedef int algo_compare_func (const void *a, const void *b, void *aux);

/* Searches ARRAY, which contains COUNT of SIZE bytes each, using
a binary search. Returns any element that equals VALUE, if
one exists, or a null pointer otherwise. ARRAY must ordered
according to COMPARE. AUX is passed to COMPARE as auxiliary
data. */
void *
binary_search (const void *array, size_t count, size_t size,
void *value,
algo_compare_func *compare, void *aux)
{
assert (array != NULL);
assert (count <= INT_MAX);
assert (compare != NULL);

if (count != 0)
{
const unsigned char *first = array;
int low = 0;
int high = count - 1;

while (low <= high)
{
int middle = (low + high) / 2;
const unsigned char *element = first + middle * size;
int cmp = compare (value, element, aux);

if (cmp > 0)
low = middle + 1;
else if (cmp < 0)
high = middle - 1;
else
return (void *) element;
}
}

return NULL;
}

--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming


val wrote:


I found that Windows CE doesn''t include bsearch function. Can
somebody point me into right direction in order to solve that.



bsearch() is a standard C function, and has been since at least
1989. Complain to the compiler vendor. It has nothing to do with
the OS.

The other choice is to write your own, under another name, as Ben
has suggested. But that way you cannot take advantage of possible
system optimizations.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


CBFalconer <cb********@yahoo.com> writes:

[bsearch]

The other choice is to write your own, under another name, as Ben
has suggested. But that way you cannot take advantage of possible
system optimizations.



It''s doubtful whether there are meaningful optimizations for a
function that will probably spend most of its time calling
another function via a pointer. On modern architectures trying
to optimize bsearch() is probably a lost cause. I note, for
example, that the implementation of bsearch in glibc is written
in C without much attempt at optimization.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org


这篇关于bsearch for Windows ce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆