Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <stdio.h>
- #include "HashTableOpen.h"
- #include <iostream>
- #include <string>
- //while the other method provides better next slot access, an array provides better speed since it will save a ton
- //of recursive nexts when going to a position deeper into the table. also since the array is always gonna be [size]
- //no worry about array resizing.E
- int full = 999;
- std::string* HashTableOpen_construct(int size) {
- std::string *HashtableOpen; //make pointer
- HashtableOpen = new std::string[size]; // new creates a array of size [size] and assigns the pointer for it to HashtableOpen
- // HashtableOpen[size] = { NULL }; //???? \nprofit
- memset(HashtableOpen, NULL, size);
- if (HashtableOpen == nullptr) {
- printf("Failed to create array.");
- exit(EXIT_FAILURE);
- }
- else
- printf("HashtableOpen created sucessfully.");
- return HashtableOpen;
- //using std::cout; to print
- //HashtableOpen[5] = 'chat';
- }
- bool HashTableOpen_insert(std::string *HashtableOpen, std::string item, int size) {
- int h = tinyhash(item);
- std::cout << '\n' << item << ' ' << (tinyhash(item)) << " h: " << h ;
- //if (HashtableOpen[h] != NULL){;
- if (!HashtableOpen[h].empty()) {;
- std::cout << "\nNotify: table here at " << h <<" is already matched";
- int slot = findslot(HashtableOpen, size, h);
- if (slot != full) {
- HashtableOpen[slot] = h;
- return true; //sucess
- }
- if (slot == full) {
- return false; //failure
- }
- }
- else {
- HashtableOpen[h] = h;
- return true;
- }
- }
- int findslot(std::string *HashtableOpen, int size, int h) { //uses the next open slot
- int hO = h; //orignal location
- int nslot;
- printf("\nDebug: finding slot");
- if ((h + 1) > size) { //end ot table
- nslot = overrun(HashtableOpen, hO);
- }
- if (h + 1 <= size) {
- printf("\n D-F1");
- while (h + 1 < size & !HashtableOpen[h + 1].empty()) {
- printf("Notify: table here already matched");
- h += 1;
- }
- if (h + 1 < size & HashtableOpen[h + 1].empty()) {
- printf("\n D-F2 ");
- std::cout << h;
- nslot = h + 1;
- printf("\n D-F2-2 ");
- printf("Notify: new slot is %i", nslot);
- printf("\n D-F2-3");
- return nslot;
- }
- else {//h+1 > size:
- printf("\n D-F3");
- nslot = overrun(HashtableOpen, hO);
- return nslot;
- }
- }
- else {
- printf("\n D-F4");
- nslot = overrun(HashtableOpen, hO);
- return nslot;
- printf("\n D-F5");
- }
- printf("\n D_F6");
- }
- int overrun(std::string *HashtableOpen, int hO) { //table overrun
- int n = 0;
- int h = -1; //loop back to check the start
- printf("\nDebug: overrun");
- while (!HashtableOpen[h + 1].empty() & h<hO) {
- printf("Debug: Looping back h= ", h);
- printf("Notify: table here already matched");
- h += 1;
- }
- if (HashtableOpen[h + 1].empty() & h<hO) {
- int nslot = h + 1;
- printf("Notify: new slot is %s") % nslot;
- return nslot;
- }
- if (h >= hO) {
- printf("Notify: table full!\n");
- return full;
- }
- }
- void HashTableOpen_destroy(std::string *HashtableOpen, int size) {
- //memset(HashtableOpen, NULL, size);
- delete[] HashtableOpen;
- printf("\nDestroy\n");
- }
- /*
- int hashtable_lookup(std::string *HashtableOpen, int size, std::string item) {
- int ih = 0; //index table
- std::string results;
- int match = tinyhash(item); //since this is a destructive hash we need to hash the item to know what to look for
- while (ih < size) {
- if (HashtableOpen[ih] == match) {
- results = results + str(ih) + ' ';
- ih += 1;
- }
- else {
- ih += 1;
- }
- }
- return results;
- }
- */
- int tinyhash(std::string item) {
- return item.length();
- }
- /*working code in python I made to understand the assignment because it takes 10x less time to see if things are gonna work as
- intended using python.
- #stuff to make it more c like
- hashtable=['']*10
- NULL='' #mimics NULL being a empty string
- equals='equals'
- hashfunction="length of input"
- def hashtable_construct(size, hashtable, hashfunction, equals):
- hashtable=['']*size #void HashTableOpen_construct(ect)
- tablelen=len(hashtable)
- def openinsbuild_table(hashtable):
- while 1==1:
- item = raw_input("Enter value: ")
- if item=="exit":
- return
- else:
- h=tinyhash(item)
- if hashtable[h]!=NULL:
- print 'Notify: table here at %s is already matched' % h
- slot=findslot(hashtable,tablelen,h)
- if slot!= 'full':
- print 'slot %s' % slot
- hashtable[slot]=h
- else:
- hashtable[h]=h
- def hashtable_insert(hashtable,item):
- h=tinyhash(item)
- if hashtable[h]!=NULL:
- print 'Notify: table here at %s is already matched' % h
- slot=findslot(hashtable,tablelen,h)
- if slot!= 'full':
- hashtable[slot]=h
- return True
- if slot=='full':
- return False
- else:
- hashtable[h]=h
- return True
- def findslot(hashtable,tablelen,h): #uses the next open slot
- hO=h #orignal location
- if h+1> tablelen: #end ot table
- nslot=overrun(hashtable,hO)
- if h+1 <= tablelen:
- while h+1 < tablelen and hashtable[h+1]!=NULL:
- print 'Notify: table here already matched'
- h+=1
- if h+1 < tablelen and hashtable[h+1]==NULL :
- nslot=h+1
- print 'Notify: new slot is %s' % nslot
- return nslot
- else: #h+1 > tablelen:
- nslot=overrun(hashtable,hO)
- return nslot
- else:
- nslot=overrun(hashtable,hO)
- return nslot
- def overrun(hashtable, hO): #table overrun
- n=0
- h=-1 #loop back to check the start
- while hashtable[h+1]!=NULL and h<hO:
- print 'Debug: Looping back h= ', h
- print 'Notify: table here already matched'
- h+=1
- if hashtable[h+1]==NULL and h<hO:
- nslot=h+1
- print 'Notify: new slot is %s' % nslot
- return nslot
- if h>=hO:
- print 'Notify: table full!\n'
- return 'full'
- def tinyhash(item):
- return len(item)
- def hashtable_destroy(hashtable,tablelen):
- hashtable=['']*0
- print '\nDestroy\n'
- return hashtable
- def hprint(tablelen):
- n=0
- print "index,bucket"
- while n <tablelen:
- print '[%s,%s]' % (n, hashtable[n])
- n+=1
- def hashtable_lookup(hashtable, tablelen, item):
- ih=0 #index table
- results=''
- match=tinyhash(item)#since this is a destructive hash we need to hash the item to know what to look for
- while ih< tablelen:
- if hashtable[ih]==match:
- results=results+str(ih)+' '
- ih+=1
- else:
- ih+=1
- return results
- '''----------------CODE TIME------------ '''
- size=10
- tablelen=size
- hashtable_construct(size,hashtable,hashfunction,equals)
- item= 'tree'
- hashtable_insert(hashtable,item)
- item='frog'
- hashtable_insert(hashtable,item)
- hprint(tablelen)
- results = hashtable_lookup(hashtable, tablelen, 'frog')
- print 'locations:',results
- hashtable=hashtable_destroy(hashtable,tablelen)
- tablelen=0
- hprint(tablelen)
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement