SHOW:
|
|
- or go back to the newest paste.
1 | #include <assert.h> | |
2 | #include <stdlib.h> | |
3 | #include <stddef.h> | |
4 | #include <stdio.h> | |
5 | #include "HashTableOpen.h" | |
6 | #include <iostream> | |
7 | #include <string> | |
8 | ||
9 | //while the other method provides better next slot access, an array provides better speed since it will save a ton | |
10 | //of recursive nexts when going to a position deeper into the table. also since the array is always gonna be [size] | |
11 | //no worry about array resizing.E | |
12 | ||
13 | int full = 999; | |
14 | ||
15 | std::string* HashTableOpen_construct(int size) { | |
16 | std::string *HashtableOpen; //make pointer | |
17 | HashtableOpen = new std::string[size]; // new creates a array of size [size] and assigns the pointer for it to HashtableOpen | |
18 | // HashtableOpen[size] = { NULL }; //???? \nprofit | |
19 | memset(HashtableOpen, NULL, size); | |
20 | if (HashtableOpen == nullptr) { | |
21 | printf("Failed to create array."); | |
22 | exit(EXIT_FAILURE); | |
23 | } | |
24 | else | |
25 | printf("HashtableOpen created sucessfully."); | |
26 | return HashtableOpen; | |
27 | //using std::cout; to print | |
28 | //HashtableOpen[5] = 'chat'; | |
29 | } | |
30 | ||
31 | bool HashTableOpen_insert(std::string *HashtableOpen, std::string item, int size) { | |
32 | int h = tinyhash(item); | |
33 | std::cout << '\n' << item << ' ' << (tinyhash(item)) << " h: " << h ; | |
34 | ||
35 | - | //if (HashtableOpen[0] != NULL){; |
35 | + | //if (HashtableOpen[h] != NULL){; |
36 | - | if (!HashtableOpen[0].empty()) { |
36 | + | if (!HashtableOpen[h].empty()) {; |
37 | - | ; |
37 | + | std::cout << "\nNotify: table here at " << h <<" is already matched"; |
38 | - | printf("Notify: table here at %i is already matched") % h; |
38 | + | |
39 | if (slot != full) { | |
40 | HashtableOpen[slot] = h; | |
41 | return true; //sucess | |
42 | } | |
43 | if (slot == full) { | |
44 | return false; //failure | |
45 | } | |
46 | ||
47 | } | |
48 | else { | |
49 | HashtableOpen[h] = h; | |
50 | return true; | |
51 | } | |
52 | } | |
53 | ||
54 | int findslot(std::string *HashtableOpen, int size, int h) { //uses the next open slot | |
55 | int hO = h; //orignal location | |
56 | int nslot; | |
57 | printf("\nDebug: finding slot"); | |
58 | - | printf("Debug: finding slot"); |
58 | + | if ((h + 1) > size) { //end ot table |
59 | - | if (h + 1 > size) { //end ot table |
59 | + | |
60 | } | |
61 | if (h + 1 <= size) { | |
62 | printf("\n D-F1"); | |
63 | while (h + 1 < size & !HashtableOpen[h + 1].empty()) { | |
64 | printf("Notify: table here already matched"); | |
65 | h += 1; | |
66 | } | |
67 | if (h + 1 < size & HashtableOpen[h + 1].empty()) { | |
68 | printf("\n D-F2 "); | |
69 | - | printf("Notify: new slot is %s", nslot); |
69 | + | std::cout << h; |
70 | nslot = h + 1; | |
71 | printf("\n D-F2-2 "); | |
72 | printf("Notify: new slot is %i", nslot); | |
73 | printf("\n D-F2-3"); | |
74 | return nslot; | |
75 | } | |
76 | else {//h+1 > size: | |
77 | printf("\n D-F3"); | |
78 | nslot = overrun(HashtableOpen, hO); | |
79 | return nslot; | |
80 | } | |
81 | } | |
82 | else { | |
83 | printf("\n D-F4"); | |
84 | nslot = overrun(HashtableOpen, hO); | |
85 | return nslot; | |
86 | printf("\n D-F5"); | |
87 | } | |
88 | printf("\n D_F6"); | |
89 | } | |
90 | ||
91 | int overrun(std::string *HashtableOpen, int hO) { //table overrun | |
92 | int n = 0; | |
93 | int h = -1; //loop back to check the start | |
94 | printf("\nDebug: overrun"); | |
95 | ||
96 | while (!HashtableOpen[h + 1].empty() & h<hO) { | |
97 | printf("Debug: Looping back h= ", h); | |
98 | printf("Notify: table here already matched"); | |
99 | h += 1; | |
100 | } | |
101 | if (HashtableOpen[h + 1].empty() & h<hO) { | |
102 | int nslot = h + 1; | |
103 | printf("Notify: new slot is %s") % nslot; | |
104 | return nslot; | |
105 | } | |
106 | ||
107 | if (h >= hO) { | |
108 | printf("Notify: table full!\n"); | |
109 | return full; | |
110 | } | |
111 | ||
112 | } | |
113 | ||
114 | ||
115 | void HashTableOpen_destroy(std::string *HashtableOpen, int size) { | |
116 | //memset(HashtableOpen, NULL, size); | |
117 | delete[] HashtableOpen; | |
118 | printf("\nDestroy\n"); | |
119 | } | |
120 | ||
121 | /* | |
122 | int hashtable_lookup(std::string *HashtableOpen, int size, std::string item) { | |
123 | int ih = 0; //index table | |
124 | std::string results; | |
125 | int match = tinyhash(item); //since this is a destructive hash we need to hash the item to know what to look for | |
126 | while (ih < size) { | |
127 | if (HashtableOpen[ih] == match) { | |
128 | results = results + str(ih) + ' '; | |
129 | ih += 1; | |
130 | } | |
131 | else { | |
132 | ih += 1; | |
133 | } | |
134 | } | |
135 | return results; | |
136 | } | |
137 | */ | |
138 | int tinyhash(std::string item) { | |
139 | return item.length(); | |
140 | } | |
141 | ||
142 | ||
143 | /*working code in python I made to understand the assignment because it takes 10x less time to see if things are gonna work as | |
144 | intended using python. | |
145 | ||
146 | #stuff to make it more c like | |
147 | hashtable=['']*10 | |
148 | NULL='' #mimics NULL being a empty string | |
149 | equals='equals' | |
150 | hashfunction="length of input" | |
151 | ||
152 | ||
153 | def hashtable_construct(size, hashtable, hashfunction, equals): | |
154 | hashtable=['']*size #void HashTableOpen_construct(ect) | |
155 | tablelen=len(hashtable) | |
156 | ||
157 | def openinsbuild_table(hashtable): | |
158 | while 1==1: | |
159 | item = raw_input("Enter value: ") | |
160 | if item=="exit": | |
161 | return | |
162 | else: | |
163 | h=tinyhash(item) | |
164 | if hashtable[h]!=NULL: | |
165 | print 'Notify: table here at %s is already matched' % h | |
166 | slot=findslot(hashtable,tablelen,h) | |
167 | if slot!= 'full': | |
168 | print 'slot %s' % slot | |
169 | hashtable[slot]=h | |
170 | else: | |
171 | hashtable[h]=h | |
172 | ||
173 | def hashtable_insert(hashtable,item): | |
174 | h=tinyhash(item) | |
175 | if hashtable[h]!=NULL: | |
176 | print 'Notify: table here at %s is already matched' % h | |
177 | slot=findslot(hashtable,tablelen,h) | |
178 | if slot!= 'full': | |
179 | hashtable[slot]=h | |
180 | return True | |
181 | if slot=='full': | |
182 | return False | |
183 | else: | |
184 | hashtable[h]=h | |
185 | return True | |
186 | ||
187 | def findslot(hashtable,tablelen,h): #uses the next open slot | |
188 | hO=h #orignal location | |
189 | if h+1> tablelen: #end ot table | |
190 | nslot=overrun(hashtable,hO) | |
191 | if h+1 <= tablelen: | |
192 | while h+1 < tablelen and hashtable[h+1]!=NULL: | |
193 | print 'Notify: table here already matched' | |
194 | h+=1 | |
195 | if h+1 < tablelen and hashtable[h+1]==NULL : | |
196 | nslot=h+1 | |
197 | print 'Notify: new slot is %s' % nslot | |
198 | return nslot | |
199 | else: #h+1 > tablelen: | |
200 | nslot=overrun(hashtable,hO) | |
201 | return nslot | |
202 | else: | |
203 | nslot=overrun(hashtable,hO) | |
204 | return nslot | |
205 | ||
206 | def overrun(hashtable, hO): #table overrun | |
207 | n=0 | |
208 | h=-1 #loop back to check the start | |
209 | while hashtable[h+1]!=NULL and h<hO: | |
210 | print 'Debug: Looping back h= ', h | |
211 | print 'Notify: table here already matched' | |
212 | h+=1 | |
213 | if hashtable[h+1]==NULL and h<hO: | |
214 | nslot=h+1 | |
215 | print 'Notify: new slot is %s' % nslot | |
216 | return nslot | |
217 | ||
218 | if h>=hO: | |
219 | print 'Notify: table full!\n' | |
220 | return 'full' | |
221 | ||
222 | def tinyhash(item): | |
223 | return len(item) | |
224 | ||
225 | def hashtable_destroy(hashtable,tablelen): | |
226 | hashtable=['']*0 | |
227 | print '\nDestroy\n' | |
228 | return hashtable | |
229 | ||
230 | def hprint(tablelen): | |
231 | n=0 | |
232 | print "index,bucket" | |
233 | while n <tablelen: | |
234 | print '[%s,%s]' % (n, hashtable[n]) | |
235 | n+=1 | |
236 | ||
237 | def hashtable_lookup(hashtable, tablelen, item): | |
238 | ih=0 #index table | |
239 | results='' | |
240 | match=tinyhash(item)#since this is a destructive hash we need to hash the item to know what to look for | |
241 | while ih< tablelen: | |
242 | if hashtable[ih]==match: | |
243 | results=results+str(ih)+' ' | |
244 | ih+=1 | |
245 | else: | |
246 | ih+=1 | |
247 | return results | |
248 | ||
249 | ||
250 | '''----------------CODE TIME------------ ''' | |
251 | ||
252 | size=10 | |
253 | tablelen=size | |
254 | ||
255 | hashtable_construct(size,hashtable,hashfunction,equals) | |
256 | item= 'tree' | |
257 | hashtable_insert(hashtable,item) | |
258 | item='frog' | |
259 | hashtable_insert(hashtable,item) | |
260 | ||
261 | hprint(tablelen) | |
262 | ||
263 | results = hashtable_lookup(hashtable, tablelen, 'frog') | |
264 | print 'locations:',results | |
265 | ||
266 | hashtable=hashtable_destroy(hashtable,tablelen) | |
267 | ||
268 | tablelen=0 | |
269 | hprint(tablelen) | |
270 | ||
271 | ||
272 | ||
273 | ||
274 | ||
275 | ||
276 | ||
277 | ||
278 | */ |