home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / nicol / sti_hash / sti_hash.txt next >
Encoding:
Text File  |  1990-08-22  |  20.6 KB  |  739 lines

  1.  
  2.  
  3.  
  4.         
  5.         
  6.         
  7.         
  8.         
  9.         
  10.         
  11.         
  12.         
  13.         
  14.         
  15.         
  16.                                 GENERIC HASHING SYSTEM 1.0
  17.         
  18.                                For Turbo Pascal Version 5.0
  19.         
  20.         
  21.         
  22.         
  23.         
  24.         
  25.         
  26.         
  27.         
  28.         
  29.         
  30.         
  31.                                   Copyright 1990, 1991 By
  32.         
  33.                              Software Technology International
  34.         
  35.                                     All Rights Reserved
  36.         
  37.         
  38.         
  39.         
  40.         
  41.         
  42.         
  43.         
  44.         
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.                                             -1-
  67.                                           NOTICE
  68.                                           ------
  69.  
  70.         
  71.         
  72.                      All parts of this manual and the accompanying  soft-
  73.                   ware  are  copyrighted material. You, as  a  registered 
  74.                   user, are granted permission to make as many copies  of 
  75.                   the  software, or manual, as you wish, as long as  they 
  76.                   are for your personal use. You may not copy this  soft-
  77.                   ware,  or  manual,  in any form  whatsoever  for  usage 
  78.                   outside of your personal use. This includes, but is not 
  79.                   limited  to, duplication of the disk, the files on  the 
  80.                   disk or the manual, by manual or electronic means.
  81.         
  82.         
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                                             -2-
  134.                                      TABLE OF CONTENTS
  135.                                      -----------------
  136.         
  137.  
  138.                    Subject                                     Page
  139.                    -----------------------------------------------------
  140.         
  141.                    GENERAL DESCRIPTION ......................   4 
  142.                      Introduction ...........................   4 
  143.                      The software ...........................   4 
  144.                                                              
  145.                    THE SOFTWARE IN DETAIL ...................   5 
  146.                      Structures and Procedures ..............   5 
  147.                      Usage Pointers .........................   9 
  148.                                                              
  149.                    INDEX ....................................   10
  150.         
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                                             -3-
  201.                                     GENERAL DESCRIPTION
  202.                                     -------------------
  203.                   Introduction
  204.                   ------------
  205.  
  206.                      Welcome to Version 1.0 of Software Technology Inter-
  207.                   nationals' Generic Hashing System. This group of  func-
  208.                   tions  will  enable your Turbo Pascal  Version  5.0  to
  209.                   create  hash  tables containing any kind  of  data  and
  210.                   using any kind of key. The system does not limit you to
  211.                   one  hash table per unit, but rather uses a pointer  to
  212.                   specify  the current table. The functions were  written
  213.                   to be flexible, as fast as possible, and as easy to use
  214.                   as possible. We hope they live up to your expectations.
  215.                   Please  feel free to write to us anytime with  bug  re-
  216.                   ports or suggestions for future versions. If you are  a
  217.                   registered  user, this will entitle you to a  free  up-
  218.                   grade.
  219.                      Remember  that  this  software  is  copyrighted,  so
  220.                   please don't copy and distribute it, this is a  federal
  221.                   offense.
  222.  
  223.                   The Software
  224.                   ------------
  225.  
  226.                      The software is a group of procedures that implement
  227.                   a  generic hash table system for Turbo  Pascal  Version
  228.                   4.0  or  5.0.  Using this system, you  can  create  any
  229.                   number of hash tables, and use each by only reassigning
  230.                   a single pointer. The only limits imposed are that  you
  231.                   are  limited  by the size of available memory  for  the
  232.                   hash tables, and that each table can has only 23 direct
  233.                   entries, after that the entries are stored in a  colli-
  234.                   sion  table. Also, the keys are only significant up  to
  235.                   65535 bytes, everything else is ignored.
  236.                       However, despite these limitations, the search time
  237.                   is  still very fast, and the unit is very flexible.  If
  238.                   you  buy the source, you can easily modify the  maximum
  239.                   number of entries.
  240.                       The  system  uses  one of the  simplest,  but  most
  241.                   effective  algorithms  for  hashing,  namely,  the  one
  242.                   specified  by Wirth in "Algorithms + Data Structures  =
  243.                   Programs." With this algorithm, the best tables size is
  244.                   a  prime number, for rather obvious, if  esoteric  rea-
  245.                   sons.
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.                                            -4-
  268.                                   THE SOFTWARE IN DETAIL
  269.                                   ----------------------
  270.  
  271.  
  272.                   The Structures And Procedures
  273.                   -----------------------------
  274.  
  275.                      The unit is interfaced through the following  struc-
  276.                   tures and procedures.
  277.  
  278.                   Constants
  279.                   ---------
  280.  
  281.                   MAXENTRY = 23;
  282.  
  283.                      This is the size of the hash table. This was  chosen
  284.                   because A) it is a prime and B) because increasing  the
  285.                   size of the table doesn't decrease search time so much.
  286.                   This is a good tradeoff between memory usage and speed.
  287.  
  288.                   Types
  289.                   -----
  290.  
  291.                   EntryPtr = ^EntryRec;
  292.  
  293.                      This is a pointer to the generic entry record.
  294.  
  295.                   EntryRec  = record
  296.                                 Data : pointer;
  297.                                 Key  : pointer;
  298.                                 Next : EntryPtr;
  299.                               end;
  300.  
  301.                      This  is the generic entry record. The Data and  Key
  302.                   fields  general  pointers, and can point to  any  valid
  303.                   data  type, thereby freeing you from limits imposed  by
  304.                   defining  a  specific type. The Next  field  points  to
  305.                   another entry record, which is used to create a  linked
  306.                   list when there are collisions.
  307.  
  308.                   Entries  = array[0..MAXENTRY] of EntryPtr;
  309.  
  310.                      This is the array of entries used in the hash table.
  311.                   The size is set to the same value as MAXENTRY (above).
  312.  
  313.                   UsedFlags = array[0..MAXENTRY] of boolean;
  314.  
  315.                      This  is a set of flags used to show whether a  par-
  316.                   ticular entry is used or not.
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                                             -5-
  335.                   HashTable = record
  336.                                 Entry : Entries;
  337.                                 Used  : UsedFlags;
  338.                               end;
  339.  
  340.                      This is the actual declaration of the hash table. It
  341.                   uses  the arrays defined above as the core to the  sys-
  342.                   tem.
  343.  
  344.                   HashTPtr  = ^HashTable;
  345.  
  346.                      This is a pointer to a hashtable, the system actual-
  347.                   ly  uses this to do all the work, thereby enabling  you
  348.                   to use as many tables as you like.
  349.  
  350.                   Variables
  351.                   ---------
  352.  
  353.                   HTable    : HashTPtr;
  354.  
  355.                      This  is  a pointer to the current  hash  table  the
  356.                   system  is using. By reassigning this pointer  you  can
  357.                   use  as many tables as you like. This pointer  must  be
  358.                   valid otherwise the system will crash. The STIHASH unit
  359.                   currently does little error checking.
  360.  
  361.                   Procedures
  362.                   ----------
  363.  
  364.                   Procedure STI_EntryInit(Var Entry : EntryRec;
  365.                                           DataSize,
  366.                                           KeySize   : word);
  367.  
  368.                      This  procedure  initialises  an  entry  record.  It
  369.                   allocates  and  zeros the memory for the KEY  and  DATA
  370.                   records.  The variable ENTRY will reflect  the  changes
  371.                   made to the pointers.
  372.  
  373.                   Procedure STI_EntryCreate(Var Entry : EntryRec);
  374.  
  375.                      This  procedure creates a new entry. It is a  little
  376.                   different  from STI_EntryInit in that it doesn't  allo-
  377.                   cate memory, it just sets the pointers to NIL.
  378.  
  379.                   Procedure STI_EntryLink(Var Entry  : EntryRec;
  380.                                               NewRec : EntryRec);
  381.  
  382.                      This  procedure  links NEWREC  to  ENTRY.NEXT.  This
  383.                   should  NOT be used externally except in  very  special
  384.                   cases.  It  has been interfaced only to  allow  maximum
  385.                   flexibility.
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.                                             -6-
  402.                   Procedure STI_EntryDestroy(Var Entry  : EntryRec;
  403.                                                KeySize,
  404.                                                DataSize : word);
  405.  
  406.                      This  procedure is the opposite of  STI_EntryCreate.
  407.                   It  frees  the memory allocated for the  KEY  and  DATA
  408.                   records  (using KEYSIZE and DATASIZE), and  assign  the
  409.                   pointers  to NIL. Be careful to match the sizes to  the
  410.                   actual  allocated size, otherwise you will eat up  your
  411.                   memory. Changes will be reflected in EntryRec.
  412.  
  413.                   Procedure  STI_EntrySet_Key(Var Entry  : EntryRec;
  414.                                               Var Key;
  415.                                                   Size   : word);
  416.  
  417.                      This  procedure  copies  the KEY  structure  to  the
  418.                   ENTRY.KEY pointer, using size as the number of bytes to
  419.                   copy. Make sure that SIZE is correct, otherwise the key
  420.                   will  be  corrupted. Changes will be reflected  in  the
  421.                   ENTRY.KEY pointer.
  422.  
  423.                   Procedure STI_EntrySet_Data(Var Entry : EntryRec;
  424.                                               Var Data;
  425.                                                   Size  : word);
  426.  
  427.                      This  procedure is very similar to  STI_EntrySetKey,
  428.                   except  that  it  copies  the  DATA  variable  to   the
  429.                   ENTRY.DATA  pointer.  Again,  make sure  that  SIZE  is
  430.                   correct  otherwise the data will be corrupted.  Changes
  431.                   will be reflected in the ENTRY.DATA pointer.
  432.  
  433.                   Procedure STI_EntryGet_Data(Var Entry : EntryRec;
  434.                                               Var Data;
  435.                                                   Size  : word);
  436.  
  437.                      This procedure is the opposite of  STI_EntrySet_Data
  438.                   in  that  it copies the data from ENTRY.DATA  to  DATA.
  439.                   Again,  be very careful with size, otherwise  the  data
  440.                   will  be  corrupted. Changes will be reflected  in  the
  441.                   DATA variable.
  442.  
  443.                   Procedure STI_HashTableCreate;
  444.  
  445.                      This  procedure creates a hash tables. This  is  the
  446.                   current  hash  table pointed to by HTABLE. It  does  NO
  447.                   checking to make sure that everything is valid.
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.                                             -7-
  469.                   Procedure STI_HashTableEnter(E             : EntryRec;
  470.                                                KeySize       : word;
  471.                                                Var Duplicate : Boolean);
  472.  
  473.                      This procedure enters an entry into the hash  table.
  474.                   The entry is E. KEYSIZE determines the size of the  key
  475.                   to  be HASHED and COMPARED. This procedure does not  do
  476.                   any data allocation at all, so try to keep the size  to
  477.                   a minimum, otherwise things will get a lot slower  VERY
  478.                   quickly.  If  there is already a matching  key  in  the
  479.                   table the entry will NOT be stored, and DUPLICATE  will
  480.                   be set to true. If this is a problem, think of a better
  481.                   key structure than the one you are using.
  482.  
  483.  
  484.                   Procedure STI_HashTableRetrieve(TheKey    : EntryRec;
  485.                                                   KeySize   : word;
  486.                                                   Var Found : Boolean;
  487.                                                   Var E     : EntryRec);
  488.  
  489.                      This  procedure  retrieves an entry  from  the  hash
  490.                   table.  KEYSIZE  has  the  same  effect  here,  as   in
  491.                   STI_HashTableEnter,  so  again, try to keep  it  small.
  492.                   THEKEY  is an entry record, but only the key  is  used.
  493.                   This is a bit of a kludge, but it is the easiest way to
  494.                   do  things. FOUND will bet set to TRUE if the  key  was
  495.                   retrieved, and E will contain the entry from the table.
  496.                   If the entry was not in the table FOUND will be set  to
  497.                   FALSE, and E will be junk.
  498.  
  499.                   Procedure STI_HashTableDestroy(KeySize,
  500.                                                  DataSize : word);
  501.  
  502.                      This procedure destroys the current hashtable point-
  503.                   ed to by HTABLE. It frees all memory, except the actual
  504.                   table itself. The table is returned to the state it was
  505.                   in  just after STI_HashTableCreate was called. Be  very
  506.                   careful the KEYSIZE and DATASIZE are correct, otherwise
  507.                   you will waste memory.
  508.  
  509.  
  510.                   NOTE
  511.                   ----
  512.  
  513.                      Some  of these procedures and types  are  interfaced
  514.                   only to allow the maximum flexibility possible. If  you
  515.                   use  them,  you face the risk of crashing  the  system.
  516.                   Take a good look at the demonstration program and  unit
  517.                   for a good idea of implementation.
  518.  
  519.                      Also,  BE  VERY CAREFUL TO USE THE RIGHT  SIZES  FOR
  520.                   PROCEDURES THAT NEED THEM. YOU WILL LOOSE MEMORY OTHER-
  521.                   WISE.
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.                                             -8-
  536.                                       USAGE POINTERS
  537.                                       --------------
  538.  
  539.                      You  should  have a very good look at the  unit  and
  540.                   demonstration program provided to get a feel for how to
  541.                   use  this unit. As stated before, this  unit  currently
  542.                   does very little in the way of error checking, so it is
  543.                   easy  to make it do strange things. This is partly  due
  544.                   to  us wanting to make it as flexible as possible,  and
  545.                   thereby ignoring data types.
  546.  
  547.                      The  unit provided is a generic symbol table,  which
  548.                   is  basically  the main type of  application  for  hash
  549.                   tables.  You  can easily tailor this to  your  specific
  550.                   needs  by  changing the DATAREC and KEYREC  types.  The
  551.                   unit should handle these changes with few problems. Try
  552.                   that before attempting to write your own.
  553.  
  554.                      As  stated earlier, the KEY and DATA fields  in  the
  555.                   ENTRY record can be any valid type.. up to 65535 bytes,
  556.                   and  the KEY is significant to 65535 bytes.  Therefore,
  557.                   if  your  are writing a compiler for  a  language  like
  558.                   PASCAL,  you should limit the name size to 254  charac-
  559.                   ters  or  so, and use 1 byte to hold the LEVEL  of  the
  560.                   symbol (depth of nesting).
  561.  
  562.                      Another  point is that, because the hashtable  is  a
  563.                   pointer you SHOULD be able to push and pop them off the
  564.                   stack, thereby making recursive functions using hashta-
  565.                   bles possible. A problem here would be memory....
  566.  
  567.                      THE  MOST IMPORTANT THING TO REMEMBER IS TO USE  THE
  568.                   CORRECT SIZES WITH PROCEDURES THAT NEED THEM.  Generous
  569.                   use of sizeof() is recommended.
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.                                             -9-
  603.                                            INDEX
  604.                                            -----
  605.  
  606.                     Topic                                     Page Number
  607.                   -------------------------------------------------------
  608.  
  609.                  Constants..................................     5
  610.                  Contents...................................     3
  611.                  Copying....................................     2
  612.                  Copyright..................................     2
  613.  
  614.                  Data Size..................................     9
  615.                  Demonstration..............................     9
  616.                  Descriptions...............................     5
  617.  
  618.                  Entering Data..............................     8
  619.                  Entries....................................     5
  620.                  EntryPtr...................................     5
  621.                  EntryRec...................................     5
  622.  
  623.                  General Description........................     4
  624.  
  625.                  Hashing....................................     4
  626.                  HashTable(Type)............................     6
  627.                  HashTPtr...................................     6
  628.                  HTable.....................................     6
  629.         
  630.                  Introduction...............................     4
  631.         
  632.                  Key Size...................................     9
  633.         
  634.                  Notes......................................     8
  635.         
  636.                  Procedures.................................     5,6,7,8
  637.         
  638.                  Recursion..................................     9
  639.                  Retrieving Data............................     8
  640.         
  641.                  STI_EnrtySet_Data..........................     7
  642.                  STI_EntryCreate............................     6
  643.                  STI_EntryDestroy...........................     7
  644.                  STI_EntryGet_Data..........................     7
  645.                  STI_EntryInit..............................     6
  646.                  STI_EntryLink..............................     6
  647.                  STI_EntrySet_Key...........................     7
  648.                  STI_HashTableCreate........................     7
  649.                  STI_HashTableDestroy.......................     8
  650.                  STI_HashTableEnter.........................     8
  651.                  STI_HashTableRetrieve......................     8
  652.                  Structures.................................     5
  653.         
  654.                  Types......................................     5
  655.         
  656.                  Usage......................................     9
  657.                  UsedFlags..................................     5
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.                                             -10-
  670.                  Users......................................     2
  671.         
  672.                  Variables..................................     6
  673.         
  674.                  Warnings...................................     8, 9
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736.                                             -11-
  737.  
  738.  
  739.