home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / wxwin140 / src / wx_hash.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  6.3 KB  |  307 lines

  1. /*
  2.  * File:     wx_hash.cc
  3.  * Purpose:  Hash table implementation
  4.  *
  5.  *                       wxWindows 1.40
  6.  * Copyright (c) 1993 Artificial Intelligence Applications Institute,
  7.  *                   The University of Edinburgh
  8.  *
  9.  *                     Author: Julian Smart
  10.  *                       Date: 19-4-93
  11.  *
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose is hereby granted without fee, provided
  14.  * that the above copyright notice, author statement and this permission
  15.  * notice appear in all copies of this software and related documentation.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  18.  * IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  19.  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * IN NO EVENT SHALL THE ARTIFICIAL INTELLIGENCE APPLICATIONS INSTITUTE OR THE
  22.  * UNIVERSITY OF EDINBURGH BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR
  23.  * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM
  24.  * LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF
  25.  * DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
  26.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <windows.h>
  30. #include <iostream.h>
  31. #include <string.h>
  32. #include <stdarg.h>
  33.  
  34. #include "common.h"
  35. #include "wx_list.h"
  36. #include "wx_hash.h"
  37.  
  38. wxHashTable::wxHashTable(unsigned int the_key_type, int size)
  39. {
  40.   n = size;
  41.   current_position = -1;
  42.   current_node = NULL;
  43.  
  44.   key_type = wxKEY_NONE;
  45.   hash_table = new wxList *[size];
  46.   for (int i = 0; i < size; i++)
  47.   {
  48.     hash_table[i] = NULL;
  49.   }
  50. }
  51.  
  52. wxHashTable::~wxHashTable(void)
  53. {
  54.   for (int i = 0; i < n; i++)
  55.     if (hash_table[i])
  56.       delete hash_table[i];
  57.   delete hash_table;
  58. }
  59.  
  60. void wxHashTable::Put(long key, long value, wxObject *object)
  61. {
  62.   long position = key % n;
  63.   if (!hash_table[position])
  64.     hash_table[position] = new wxList(wxKEY_INTEGER);
  65.  
  66.   hash_table[position]->Append(value, object);
  67. }
  68.  
  69. void wxHashTable::Put(long key, char *value, wxObject *object)
  70. {
  71.   long position = key % n;
  72.   if (!hash_table[position])
  73.     hash_table[position] = new wxList(wxKEY_INTEGER);
  74.  
  75.   hash_table[position]->Append(value, object);
  76. }
  77.  
  78. void wxHashTable::Put(long key, wxObject *object)
  79. {
  80.   long position = key % n;
  81.   if (!hash_table[position])
  82.     hash_table[position] = new wxList(wxKEY_INTEGER);
  83.  
  84.   hash_table[position]->Append(key, object);
  85. }
  86.  
  87. void wxHashTable::Put(char *key, wxObject *object)
  88. {
  89.   long int_key = 0;
  90.   int len = strlen(key);
  91.   for (int i = 0; i < len; i++)
  92.     int_key += (long)key[i];
  93.  
  94.   long position = int_key % n;
  95.   if (!hash_table[position])
  96.     hash_table[position] = new wxList(wxKEY_INTEGER);
  97.  
  98.   hash_table[position]->Append(key, object);
  99. }
  100.  
  101. wxObject *wxHashTable::Get(long key, long value)
  102. {
  103.   long position = key % n;
  104.   if (!hash_table[position])
  105.     return NULL;
  106.   else
  107.   {
  108.     wxNode *node = hash_table[position]->Find(value);
  109.     if (node)
  110.       return node->Data();
  111.     else return NULL;
  112.   }
  113. }
  114.  
  115. wxObject *wxHashTable::Get(long key, char *value)
  116. {
  117.   long position = key % n;
  118.   if (!hash_table[position])
  119.     return NULL;
  120.   else
  121.   {
  122.     wxNode *node = hash_table[position]->Find(value);
  123.     if (node)
  124.       return node->Data();
  125.     else return NULL;
  126.   }
  127. }
  128.  
  129. wxObject *wxHashTable::Get(long key)
  130. {
  131.   long position = key % n;
  132.   if (!hash_table[position])
  133.     return NULL;
  134.   else
  135.   {
  136.     wxNode *node = hash_table[position]->Find(key);
  137.     if (node)
  138.       return node->Data();
  139.     else return NULL;
  140.   }
  141. }
  142.  
  143. wxObject *wxHashTable::Get(char *key)
  144. {
  145.   long int_key = 0;
  146.   int len = strlen(key);
  147.   for (int i = 0; i < len; i++)
  148.     int_key += (long)key[i];
  149.  
  150.   long position = int_key % n;
  151.   if (!hash_table[position])
  152.     return NULL;
  153.   else
  154.   {
  155.     wxNode *node = hash_table[position]->Find(key);
  156.     if (node)
  157.       return node->Data();
  158.     else return NULL;
  159.   }
  160. }
  161.  
  162. wxObject *wxHashTable::Delete(long key)
  163. {
  164.   long position = key % n;
  165.   if (!hash_table[position])
  166.     return NULL;
  167.   else
  168.   {
  169.     wxNode *node = hash_table[position]->Find(key);
  170.     if (node)
  171.     {
  172.       wxObject *data = node->Data();
  173.       delete node;
  174.       return data;
  175.     }
  176.     else return NULL;
  177.   }
  178. }
  179.  
  180. wxObject *wxHashTable::Delete(char *key)
  181. {
  182.   long int_key = 0;
  183.   int len = strlen(key);
  184.   for (int i = 0; i < len; i++)
  185.     int_key += (long)key[i];
  186.  
  187.   long position = int_key % n;
  188.   if (!hash_table[position])
  189.     return NULL;
  190.   else
  191.   {
  192.     wxNode *node = hash_table[position]->Find(key);
  193.     if (node)
  194.     {
  195.       wxObject *data = node->Data();
  196.       delete node;
  197.       return data;
  198.     }
  199.     else return NULL;
  200.   }
  201. }
  202.  
  203. wxObject *wxHashTable::Delete(long key, int value)
  204. {
  205.   long position = key % n;
  206.   if (!hash_table[position])
  207.     return NULL;
  208.   else
  209.   {
  210.     wxNode *node = hash_table[position]->Find(value);
  211.     if (node)
  212.     {
  213.       wxObject *data = node->Data();
  214.       delete node;
  215.       return data;
  216.     }
  217.     else return NULL;
  218.   }
  219. }
  220.  
  221. wxObject *wxHashTable::Delete(long key, char *value)
  222. {
  223.   long position = key % n;
  224.   if (!hash_table[position])
  225.     return NULL;
  226.   else
  227.   {
  228.     wxNode *node = hash_table[position]->Find(value);
  229.     if (node)
  230.     {
  231.       wxObject *data = node->Data();
  232.       delete node;
  233.       return data;
  234.     }
  235.     else return NULL;
  236.   }
  237. }
  238.  
  239. long wxHashTable::MakeKey(char *string)
  240. {
  241.   long int_key = 0;
  242.   int len = strlen(string);
  243.   for (int i = 0; i < len; i++)
  244.     int_key += (long)string[i];
  245.  
  246.   return int_key;
  247. }
  248.  
  249. void wxHashTable::BeginFind(void)
  250. {
  251.   current_position = -1;
  252.   current_node = NULL;
  253. }
  254.  
  255. wxNode *wxHashTable::Next(void)
  256. {
  257.   wxNode *found = NULL;
  258.   Bool end = FALSE;
  259.   while (!end && !found)
  260.   {
  261.     if (!current_node)
  262.     {
  263.       current_position ++;
  264.       if (current_position >= n)
  265.       {
  266.         current_position = -1;
  267.         current_node = NULL;
  268.         end = TRUE;
  269.       }
  270.       else
  271.       {
  272.         if (hash_table[current_position])
  273.         {
  274.           current_node = hash_table[current_position]->First();
  275.           found = current_node;
  276.         }
  277.       }
  278.     }
  279.     else
  280.     {
  281.       current_node = current_node->Next();
  282.       found = current_node;
  283.     }
  284.   }
  285.   return found;
  286. }
  287.  
  288. void wxHashTable::DeleteContents(Bool flag)
  289. {
  290.   for (int i = 0; i < n; i++)
  291.   {
  292.     if (hash_table[i])
  293.       hash_table[i]->DeleteContents(flag);
  294.   }
  295. }
  296.  
  297. void wxHashTable::Clear(void)
  298. {
  299.   for (int i = 0; i < n; i++)
  300.   {
  301.     if (hash_table[i])
  302.       hash_table[i]->Clear();
  303.   }
  304. }
  305.  
  306.  
  307.