From 4625883b7fa58fb648b11fb9ca4b7ee3e56c5473 Mon Sep 17 00:00:00 2001 From: yrutschle Date: Fri, 8 Apr 2022 18:38:29 +0200 Subject: [PATCH] simplify insertion using distance instead of floors and wrapped --- hash.c | 56 ++++++++++++++----------------------------------- hashtest/htest | Bin 25312 -> 25304 bytes 2 files changed, 16 insertions(+), 40 deletions(-) diff --git a/hash.c b/hash.c index da112fd..6e5a809 100644 --- a/hash.c +++ b/hash.c @@ -128,34 +128,18 @@ hash_item hash_find(hash* h, hash_item item) return out; } -/* says if we should swap the bubble (element that's going up) and the current - * indexed element. - * index: current insertion index - * wanted_index: index wanted by the element at the current index - * bubble: bubble wanted index - * floor: index of lowest wanted index (or index of the highest wrapped wanted - * index) - * wrapped: whether we wrapped or not (if we reach the floor from below after - * wrapping, we should swap as it's a 'highest' element pushing the floor up. - * it we're inserting a higher-than bottom element at the floor, it should - * bubble up). - */ -static int i_should_swap(int index, int wanted_index, int bubble_index, int floor, int wrapped) + +/* Returns DIB: distance to initial bucket */ +static int distance(int actual_index, hash* h, hash_item item) { - int res; - - if ((index == floor)) - res = wrapped; + int wanted_index = hash_make_key(h, item); + if (wanted_index <= actual_index) + return wanted_index - actual_index; else - res = bubble_index < wanted_index; - -#if DEBUG - fprintf(stderr, "i_should_swap(%d, %d, %d, %d) = %d\n", index, wanted_index, bubble_index, floor, res); -#endif - - return res; + return wanted_index + hash_size - actual_index; } + int hash_insert(hash* h, hash_item new) { int bubble_wanted_index = hash_make_key(h, new); @@ -169,35 +153,27 @@ int hash_insert(hash* h, hash_item new) hash_item curr_item = gap_get(hash, index); int orig_floor = h->floor; - int wrapped = 0; - while (1) { - if (curr_item == NULL) { -#if DEBUG - fprintf(stderr, "final insert at %d\n", index); -#endif - gap_set(hash, index, new); - h->item_cnt++; - return 0; - } - - int curr_wanted_index = hash_make_key(h, curr_item); - - if (i_should_swap(index, curr_wanted_index, bubble_wanted_index, orig_floor, wrapped)) { + while (curr_item) { + if (distance(index, h, curr_item) > distance(index, h, new)) { gap_set(h->data, index, new); #if DEBUG fprintf(stderr, "intermediate insert [%s] at %d\n", &new->client_addr, index); #endif new = curr_item; - bubble_wanted_index = curr_wanted_index; } index = hash_next_index(h, index); curr_item = gap_get(hash, index); if (index == 0) h->floor++; - if (index == 0) wrapped = 1; } +#if DEBUG + fprintf(stderr, "final insert at %d\n", index); +#endif + gap_set(hash, index, new); + h->item_cnt++; + return 0; } diff --git a/hashtest/htest b/hashtest/htest index b525ec4f1be9fb5f019a89ea2f72304b88a59b13..4ca0afc146a043de0d7654da1756e2732c908284 100755 GIT binary patch delta 2638 zcmZWrdr(wW7(Zth5OxF?MeZ&Th(Llyv6Bv{Fzrg$E+qlNPK`=I$n=^TO{ss}$hNZE z#sqzoIt``OKfNY%d(gv-A%$LqJt>=!njY72DD37iv8Ondw&hQ_haAMp?~h) zab8a}*_o9#=Y~|qjILCtgsR<9UwS#`N9p>xeO#ki^V)cnZk)HNa~1duQ=c#*`ouef zLBssc2*=9IP|Pqp#tkz#W<4-kPQjc{e1DEKr6REE)z_IYc zNAYU&G=j)5%uqj=_JIX|I&8_ZN3whs(L28~^_UDq{K+`SsIVhU(k&nDaP`OW9FK!s z2K))j^;pQIY?MZVqI%S3y)8lZGJYof{_!4J8HB60KZ2di5%i#e(;SpX>YV9k@Gl>{ zJzvxt;ygHXq&8-bh{j`rTehhnTFU7e?^o(s#YPcXuv zaW?1fjq!L{Ty28LsJ+o$_C`?Qbb-4cT`(j3wz0mMdguFi1s$G$5r2u2GOPGHx;XPG zzK?#&EUe2+5!((~k50wo@>0X03D%Jb?ze<{C)@`)sI2EiLPQ^qphv7Y5w73pTK0Le zB#`@A#nh?=vJd}()NsgRTT5|NOXRCd4A241mlm8^(nZr+Nl^)0)`+MRtk`%V?Cvwu zA-7?;CxePVsusMV7ErWYK!zTU6@m{Y^lAa)dW9?#-PQRt*IqWS9>PCO;{OVvQkb` zPtK~`PYdu#1gC4vsemcOeO?ZQI{YsBC1-W#W~7)$4096UjW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