home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / DC-POS24.LZX / pOS / pOS_RKRM.lzx / pOS_RKRM / pUtility / TstHash.c
Encoding:
C/C++ Source or Header  |  1997-03-18  |  4.8 KB  |  229 lines

  1.  
  2. /*******************************************************************
  3.  $CRT 26 Oct 1996 : hb
  4.  
  5.  $AUT Holger Burkarth
  6.  $DAT >>TstHash.c<<   29 Jan 1997    13:19:16 - (C) ProDAD
  7. *******************************************************************/
  8.  
  9. //##ex mcpp:cppc -gs -o pos:pos/Ex/TstHash p:pLib/StartCode.o p:/pOS_RKRM/pUtility/TstHash.c p:pLib/StdIO.o -l pOSStub -l pOS
  10.  
  11. /***********************************************************
  12.   pOS programing example - Copyright (C) 1995-97 proDAD
  13.  
  14.   This code was written as an easy to understand example,
  15.   how to program pOS features. It is provided 'as-is',
  16.   without any express or implied warranty.
  17.  
  18.   Permission is hereby granted to use, copy and modify
  19.   this source code for any purpose, without fee, subject
  20.   to the following conditions:
  21.  
  22.     (1) This notice may not be removed or altered from any
  23.         source distribution.
  24.  
  25.     (2) Altered source versions must be plainly marked as
  26.         such, and must not be misrepresented as being
  27.         the original source code.
  28.  
  29.     (3) If only executable code is distributed, then the
  30.         accompanying documentation have to state that
  31.         "this software is based in part on examples of
  32.         the pOS developer packet".
  33.  
  34.     (4) Permission for use of this code is granted only
  35.         if the user accepts full responsibility for any
  36.         undesirable consequences. proDAD accept NO LIABILITY
  37.         for damages of any kind.
  38.  
  39.   ©proDAD
  40. ***********************************************************/
  41.  
  42. /*\
  43. *** Example:
  44. ***
  45. \*/
  46.  
  47.  
  48. #define __COMPUTER_AMIGA 1
  49. #define NOMYDEBUG
  50.  
  51. #include "p:pExec/Types.h"
  52. #include "p:pDOS/ArgTags.h"
  53. #include "p:pDOS/DosSig.h"
  54. #include "p:pDOS/DosErrors.h"
  55. #include "p:pUtil/Hash.h"
  56. #include "p:proto/pLibExt.h"
  57. #include "p:proto/pExec2.h"
  58. #include "p:proto/pDOS2.h"
  59. #include "p:proto/pUtil2.h"
  60.  
  61.  
  62. #ifdef _____ME_____
  63.   #include "grund/inc_string.h"
  64.   #include "grund/inc_stdio.h"
  65. #else
  66.  #ifdef __cplusplus
  67.  extern "C" {
  68.  #endif
  69.   #include <string.h>
  70.   #include <stdio.h>
  71.  #ifdef __cplusplus
  72.  }
  73.  #endif
  74. #endif
  75.  
  76.  
  77. const CHAR *HelpText=
  78. ""
  79. ;
  80.  
  81. const CHAR *PrgHeader=
  82. "";
  83.  
  84. const CHAR *PrgVerText=
  85. "$VER: 1.0 ("__DATE2__") (Copyright 1996-97 by proDAD) (Created by Holger Burkarth)";
  86.  
  87. struct pOS_UtilityBase *gb_UtilityBase;
  88.  
  89. static const CHAR *Names[]=
  90. {
  91.   "null",
  92.   "eins",
  93.   "zwei",
  94.   "drei",
  95.   "vier",
  96.   "fünf",
  97.   "sechs",
  98.   "sieben",
  99.   "acht",
  100.   "neun",
  101.   "zen",
  102.   "Text",
  103.   "pOS",
  104.   "proDAD",
  105.   "Hash",
  106.   NULL
  107. };
  108.  
  109. static UWORD gb_Hash;
  110.  
  111.  
  112.  
  113. /*----------------------------------
  114. -----------------------------------*/
  115. VOID FillHash(struct pOS_HashList* hl,BOOL prt)
  116. {
  117.   ULONG i;
  118.  
  119.   for(i=0; Names[i]!=NULL; ++i) {
  120.     pOS_HashNode *HN;
  121.  
  122.     HN=pOS_CreateHashNode(hl,(VOID*)Names[i]);
  123.     if(HN) {
  124.       pOS_AddHashNode(hl,HN);
  125.       if(prt) printf("AddNode 0x%lx |%s|\n",HN,Names[i]);
  126.     }
  127.     else {
  128.        printf("Cannot create HashNode\n");
  129.        break;
  130.     }
  131.   }
  132. }
  133.  
  134.  
  135. /*----------------------------------
  136. -----------------------------------*/
  137. VOID PrintHash(const struct pOS_HashList* hl)
  138. {
  139.   ULONG i,Sum;
  140.   const struct pOS_HashNode *HN;
  141.  
  142.   printf("Hash-Verteilung bei %ld\n\n",gb_Hash);
  143.  
  144.   for(i=0; i<hl->hl_LstNum; ++i) {
  145.     const struct pOS_List *const Lst=&hl->hl_List[i];
  146.  
  147.     Sum=0;
  148.     for(HN=(struct pOS_HashNode*)Lst->lh_Head;
  149.         HN->hn_Node.ln_Succ;
  150.         HN=(struct pOS_HashNode*)HN->hn_Node.ln_Succ)
  151.     {
  152.       ++Sum;
  153.     }
  154.     printf("Index %3ld Sum=%ld\n",i,Sum);
  155.   }
  156. }
  157.  
  158.  
  159. /*----------------------------------
  160. -----------------------------------*/
  161. static
  162. ULONG StrHash_func(_R_A0 const struct pOS_HashList* hl,_R_A1 const VOID* data)
  163. {
  164.   ULONG Sum;
  165.   const UBYTE *Str=(UBYTE*)data;
  166.  
  167.   for(Sum=0; *Str; ++Str) {
  168.     Sum=Sum*gb_Hash + (Str[0] - '0');
  169.   }
  170.  
  171.   return(Sum % hl->hl_LstNum);
  172. }
  173.  
  174.  
  175.  
  176.  
  177. /*----------------------------------
  178. -----------------------------------*/
  179. #ifdef __cplusplus
  180. extern "C"
  181. #endif
  182.  
  183. VOID main()
  184. {
  185.   struct pOS_DosArgs* Args;
  186.   ULONG Ops[2]={7,1};
  187.  
  188.   gb_UtilityBase=(struct pOS_UtilityBase*)pOS_OpenLibrary("pUtility.library",0);
  189.  
  190.  
  191.   Args=pOS_ReadDosArgs(
  192. // 0         1
  193. "HASH/N, VERBOSE/S",
  194. Ops,sizeof(Ops)/sizeof(ULONG),
  195.  
  196.     ARGTAG_PrgHeaderText, (ULONG)PrgHeader,    /* kurze Programm-Beschreibung */
  197.     ARGTAG_HelpText,      (ULONG)HelpText,     /* Help-Texte */
  198.     ARGTAG_PrgVerText,    (ULONG)PrgVerText,   /* VER-String */
  199.     TAG_END);
  200.  
  201.   if(Args) {
  202.     struct pOS_HashList *HL;
  203.     struct pOS_HashNode *HN;
  204.     ULONG i;
  205.  
  206.     gb_Hash=*((ULONG*)Ops[0]);
  207.  
  208.     HL=pOS_CreateHash(13);
  209.     if(HL) {
  210.       HL->hl_Hash_func=StrHash_func;  // *** überschreibe System-Default
  211.  
  212.       FillHash(HL,Ops[1]);
  213.       if(Ops[1]) {
  214.         for(i=0; Names[i]!=NULL; ++i) {
  215.           HN=pOS_MatchHash(HL,(APTR)Names[i]);
  216.           if(HN) printf("Found |%s| 0x%lx\n",Names[i],HN);
  217.           else   printf("FATAL |%s|\n",Names[i]);
  218.         }
  219.       }
  220.       PrintHash(HL);
  221.  
  222.       pOS_DeleteHash(HL);
  223.     }
  224.     pOS_DeleteDosArgs(Args);
  225.   }
  226.  
  227.   pOS_CloseLibrary((pOS_Library*)gb_UtilityBase);
  228. }
  229.