home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / 554 / JUIN / XORSUM.PAS < prev   
Pascal/Delphi Source File  |  1993-10-07  |  1KB  |  48 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 468 of 619
  3. From : Sean Palmer                         1:104/123.0          08 Jun 93  17:35
  4. To   : All
  5. Subj : XorSum / hashing
  6. ────────────────────────────────────────────────────────────────────────────────
  7. KS> SC> (more than I thought I would have).  There were 190 collisions
  8. KS> SC> out of 572 names.
  9.  
  10. KS>The solution would be to use a better hashing algorythm, simply
  11. KS>adding up the ascii characters is not unique enough.  You best
  12. KS>approach would be to generate a CRC value for your hashing table
  13. KS>rather then the checksum approach.
  14.  
  15. Or try this xorsum method (my own invention, have to plug for it... 8)
  16.  
  17. Lots faster than a crc with no table, with similar results.
  18.  
  19. NOT compatible with a crc.
  20.  
  21. This xorsum algorithm is hereby standardized and if anyone wants to use
  22. it you should make sure your xorsum routines give the same results.}
  23.  
  24. function XorSum16(var data;size:word;prevSum:word):word;assembler;asm
  25.  push ds
  26.  lds si,data
  27.  mov cx,size
  28.  mov bx,prevSum
  29.  mov dx,$ABCD
  30.  cld
  31.  jcxz @X
  32. @L:lodsb
  33.  rol bx,1
  34.  xor bx,dx
  35.  xor bl,al
  36.  loop @L
  37. @X: mov ax,bx
  38.  pop ds
  39.  end;
  40.  
  41. to use on a string, for instance:
  42.  
  43. const s:string='this is a test';
  44.  
  45.  writeln(xorsum16(s,length(s)+1,0));
  46.  
  47. send 0 as prevSum if you're not accumulating a result...otherwise send
  48. the result from the previous buffer.