Live data from Hacker News

15-line hash table in C

pastes.archbsd.net

51–60 of 104 posts

Re: 15-line hash table in C

#51
post #4

will need the clockwise spiral rule to parse this: http://c-faq.com/decl/spiral.anderson.html

The "clockwise spiral rule" is wrong in general, as Linus Torvalds helpfully explains here:

http://webcache.googleusercontent.com/search?q=cache:V51nbJE...

(Cache link since original post is 500.)

Re: 15-line hash table in C

#54
post #50
post #44

Earlier quoted context omitted.

Technically there is bounds checking ... & (SIZE - 1) but it looks broken because t does the C-ish mutating accumulator thing rather than acting as a base and using h as an offset You need a temporary variable of int (**)[2] to avoid doing two additions per iter, though maybe the compiler can pick that up and do it for you. Anyway, this no longer crashes but it now runs forever if the hash fills https://gist.github.c…

>Due to C there is no way to declare both an int and that in the loop preamble so you'd need at least one more line In C99 it's allowed to declare in the loop preamble. True for ANSI-C.

Not differing types

Re: 15-line hash table in C

#55
post #44

Earlier quoted context omitted.

It's not hard to fix, however - one extra modulus, as far as I can see.

Technically there is bounds checking ... & (SIZE - 1) but it looks broken because t does the C-ish mutating accumulator thing rather than acting as a base and using h as an offset You need a temporary variable of int (**)[2] to avoid doing two additions per iter, though maybe the compiler can pick that up and do it for you. Anyway, this no longer crashes but it now runs forever if the hash fills https://gist.github.c…

Sorry, non-existent keys return a slot but since it isn't allocated it blew up when I was trying to print out its values :(

Re: 15-line hash table in C

#56

Earlier quoted context omitted.

It's not hard to fix, however - one extra modulus, as far as I can see.

And an extra local variable. Which might take up a line of source code!

With two additional casts you can make it compile[1] as C++ - without the incredibly wasteful extra local variable.

[1] http://codepad.org/

Re: 15-line hash table in C

#60

There's no bounds testing in hget(): some valid sequences of operations will cause buffer overflows. For example, this should segfault: int (**table)[2] = hnew(); for (int j=0; j The problem is, the probing function doesn't wrap (the "t += h" part), so if you have have several colliding keys, it will probe for them past the end of the table.

> There's no bounds testing in hget(): some valid sequences of operations will cause buffer overflows.

It wouldn't be C if that wasn't the case.

Post reply on HN