VIZ++ Class: HashTable

 VIZ++ Class Library  VIZ++ Samples  VIZ++ ClassTree 

Source code

/*
 * HashTable.h 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// VIZ++2000
// 2012/04/22

#pragma once

#include <viz++\Object.h>
#include <viz++\HashEntry.h>

namespace VIZ {

class HashTable  :public Object {
private:
  int   size;
  HashEntry** table;

  Key   hash(const Key key) { return (key % size); }

private:
  Key hash(const char* key)
  {
    long sum = 0;
    int len  = strlen(key);

    for(int i = 0; i<len; i++) {
      sum = sum + ((int)*key % size);
      key++;
    }
    return (Key)(sum % size);
  }
  
private:
  Key hash(const wchar_t* key)
  {
    long sum = 0;
    int len  = wcslen(key);

    for(int i = 0; i<len; i++) {
      sum = sum + ((int)*key % size);
      key++;
    }
    return (Key)(sum % size);
  }

public:
  HashTable(int size1)
  {
    size = size1;
    table = new HashEntry*[size];

    for (int i =0 ; i<size; i++) {
      table[i] = NULL;
    }
  }

public:
  void  add(Key key, Object* object)
  {
    Key n = hash(key);
    HashEntry* entry = new HashEntry(key, object);
    entry -> add(table[n]);
    table[n] = entry;
  }

public:
  void  add(const char* key, Object* object)
  {
    Key  n = hash(key);
    HashEntry* entry = new HashEntry(key, object);
    entry -> add(table[n]);
    table[n] = entry;
  }

public:
  void  add(const wchar_t* key, Object* object)
  {
    Key  n = hash(key);
    HashEntry* entry = new HashEntry(key, object);
    entry -> add(table[n]);
    table[n] = entry;
  }

public:
  Object* lookup(Key key)
  {
    Object* object = NULL;
    Key  n = hash(key);
    HashEntry* ptr = table[n];

    while (ptr) {
      if (ptr -> getIntKey() == key) {
        object = ptr -> getObject();
        break;
      }
      ptr = ptr -> getNext();
    }
    return object;
  }

public:
  Object* lookup(const char* key)
  {
    Key n = hash(key);
    HashEntry* ptr = table[n];
    Object* object = NULL;

    while (ptr) {
      if(strcmp(ptr -> getCharKey(), key)==0) {
        object = ptr->getObject();
        break;
      }
      ptr = ptr -> getNext();
    }
    return object;
  }

  Object* lookup(const wchar_t* key)
  {
    Key n = hash(key);
    HashEntry* ptr = table[n];
    Object* object = NULL;

    while (ptr) {
      if(wcscmp(ptr -> getWcharKey(), key)==0) {
        object = ptr->getObject();
        break;
      }
      ptr = ptr -> getNext();
    }
    return object;
  }


public:
  int remove(HashEntry* prev, HashEntry* ptr, int n)
  {
    if (ptr == NULL) {
      return False;
    }
    if (prev == ptr) {
      table[n] = ptr -> getNext();
    }
    else {
      prev -> add(ptr -> getNext());
    }
    delete ptr;
    return True;
  }

public:
  int remove(Key key)
  {
    Key n = hash(key);
    HashEntry* ptr  = table[n];
    HashEntry* prev = ptr;

    while(ptr) {
      if(ptr -> getIntKey() == key) {
        remove(prev, ptr, n);
        return True;
      }
      prev = ptr;
      ptr  = ptr -> getNext();
    }
    return False;
  }

public:
  int remove(const char* key)
  {
    Key n = hash(key);
    HashEntry* ptr  = table[n];
    HashEntry* prev = ptr;

    while (ptr) {
      if(strcmp(ptr -> getCharKey(), key)==0 ) {
        remove(prev, ptr, n);
        return True;
      }
      prev = ptr;
      ptr  = ptr -> getNext();
    }
    return False;
  }

  int remove(const wchar_t* key)
  {
    Key n = hash(key);
    HashEntry* ptr  = table[n];
    HashEntry* prev = ptr;

    while (ptr) {
      if (wcscmp(ptr -> getWcharKey(), key)==0 ) {
        remove(prev, ptr, n);
        return True;
      }
      prev = ptr;
      ptr  = ptr -> getNext();
    }
    return False;
  }

public:
  ~HashTable()
  {
    for(int i = 0; i < size; i++) {
      HashEntry* ptr  = table[i];
      HashEntry* prev = ptr;
      while(ptr) {
        prev = ptr;
        ptr  = ptr -> getNext();
        delete prev;
      }
    }

    delete [] table;
  }


};

}



Last modified: 10 Feb 2017

Copyright (c) 2009-2017 Antillia.com ALL RIGHTS RESERVED.