9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* RE: [9fans] bsearch ?
@ 2003-06-30 14:15 Trickey, Howard W (Howard)
  0 siblings, 0 replies; 3+ messages in thread
From: Trickey, Howard W (Howard) @ 2003-06-30 14:15 UTC (permalink / raw)
  To: '9fans@cse.psu.edu'

> bsearch(2) is not in libc.
>
> I am sure it will not be for a good reason, but why?
> qsort is there so why not its friend?

bsearch was not in the standard C library in our research Unix;
it must have been added to the standard by someone outside
the Labs.  So since we weren't using it, noone felt the need
to put it into the Plan 9 libc.

When I did APE, I needed to do a bsearch, so there is one
in /sys/src/ape/lib/ap/gen/bsearch.c, pretty much isomorphic
to the one just posted by Dan Cross.

- Howard


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9fans] bsearch ?
  2003-06-30 11:02 steve.simon
@ 2003-06-30 13:57 ` Dan Cross
  0 siblings, 0 replies; 3+ messages in thread
From: Dan Cross @ 2003-06-30 13:57 UTC (permalink / raw)
  To: 9fans

> bsearch(2) is not in libc.
>
> I am sure it will not be for a good reason, but why?
> qsort is there so why not its friend?

Probably because it's not used that much.  Regardless, I once
wondered this myself and wrote a version.  Since binary search
is for some inexplicable reason one of those things that seems
is impossibly hard to get right, I'll paste it below; it
probably would be good to stick it in libc.

	- Dan C.

void *
bsearch(void *key, void *base, long nel, long width, int (*cmp)(void *, void *))
{
	char *bp;
	long m, l, r;
	int c;

	for (bp = base, l = 0, r = nel - 1; l <= r; ) {
		m = (l + r) / 2;
		c = cmp(key, bp + m * width);
		if (c == 0)
			return(bp + m * width);
		else if (c < 0)
			r = m - 1;
		else
			l = m + 1;
	}

	return(nil);
}


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [9fans] bsearch ?
@ 2003-06-30 11:02 steve.simon
  2003-06-30 13:57 ` Dan Cross
  0 siblings, 1 reply; 3+ messages in thread
From: steve.simon @ 2003-06-30 11:02 UTC (permalink / raw)
  To: 9fans

Hi,

bsearch(2) is not in libc.

I am sure it will not be for a good reason, but why?
qsort is there so why not its friend?

-Steve


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-06-30 14:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-30 14:15 [9fans] bsearch ? Trickey, Howard W (Howard)
  -- strict thread matches above, loose matches on Subject: below --
2003-06-30 11:02 steve.simon
2003-06-30 13:57 ` Dan Cross

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).