Skip to content

Commit 4c869c9

Browse files
committed
Crashes Ubuntu reliably
Linear probing when table expands... BANG!
1 parent c3a42a2 commit 4c869c9

File tree

9 files changed

+377
-110
lines changed

9 files changed

+377
-110
lines changed

src/algorithms/controllers/HashingChainingInsertion.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
hash1,
66
HASH_GRAPH,
77
EMPTY_CHAR,
8+
EMPTY_CHAR_CH,
89
Colors,
910
INDEX,
1011
POINTER,
@@ -25,7 +26,7 @@ import { createPopper } from '@popperjs/core';
2526
// Bookmarks to link chunker with pseudocode
2627
const IBookmarks = {
2728
Init: 1,
28-
EmptyArray: 2,
29+
EmptyArray: 1, // XXX delete init code
2930
InitInsertion: 3,
3031
NewInsertion: 4,
3132
Hash1: 5,
@@ -35,6 +36,10 @@ const IBookmarks = {
3536
BulkInsert: 1,
3637
}
3738

39+
// text to display after key in table to indicate there are more keys
40+
// XXX MUST be the same in function extractArray in Array2DTracer.js
41+
const ETC = "..";
42+
3843
export default {
3944
explanation: HashingExp,
4045

@@ -66,7 +71,7 @@ export default {
6671

6772
// Initialize arrays
6873
let indexArr = Array.from({ length: SIZE }, (_, i) => i);
69-
let valueArr = Array(SIZE).fill(EMPTY_CHAR);
74+
let valueArr = Array(SIZE).fill(EMPTY_CHAR_CH);
7075
let nullArr = Array(SIZE).fill('');
7176

7277
// For return
@@ -128,7 +133,7 @@ export default {
128133
)
129134

130135
// Internally assign the key to the index
131-
table[i].push(key);
136+
table[i].unshift(key);
132137

133138
// Chunker for placing the key
134139
chunker.add(
@@ -154,11 +159,22 @@ export default {
154159
popper.innerHTML = table[idx];
155160
}
156161

162+
let slotCurValue = vis.array.getValueAt(VALUE, idx);
163+
if (slotCurValue === EMPTY_CHAR_CH)
164+
// Update value of that index when the slot is empty
165+
vis.array.updateValueAt(VALUE, idx, '[' + val + ']');
166+
else
167+
// / Update value of that index when the slot is not empty
168+
vis.array.updateValueAt(VALUE, idx, '[' + val + '..');
169+
vis.array.fill(INDEX, idx, undefined, undefined, Colors.Insert);
170+
171+
/*
157172
let slotCurValue = vis.array.getValueAt(VALUE, idx);
158173
if (slotCurValue === EMPTY_CHAR) vis.array.updateValueAt(VALUE, idx, val); // Update value of that index when the slot is empty
159-
else vis.array.updateValueAt(VALUE, idx, slotCurValue + (table[idx].length == 2 ? ".." : "")); // Update value of that index when the slot is not empty
174+
else vis.array.updateValueAt(VALUE, idx, slotCurValue + (table[idx].length == 2 ? ETC : "")); // Update value of that index when the slot is not empty
160175
vis.array.showKth({key: vis.array.getKth().key, type: HASH_TYPE.BulkInsert, insertions: insertions});
161176
vis.array.fill(INDEX, idx, undefined, undefined, Colors.Insert); // Fill it green, indicating successful insertion
177+
*/
162178
},
163179
[key, i, insertions, table]
164180
)
@@ -185,7 +201,7 @@ export default {
185201
// hashed value
186202
let i = hash1(null, null, key, table.length, false);
187203

188-
table[i].push(key);
204+
table[i].unshift(key);
189205
inserts[key] = i;
190206
lastHash = i;
191207
}
@@ -217,9 +233,13 @@ export default {
217233
}
218234

219235
let slotCurValue = vis.array.getValueAt(VALUE, inserts[key]);
220-
console.log(typeof(slotCurValue));
221-
if (slotCurValue === EMPTY_CHAR) vis.array.updateValueAt(VALUE, inserts[key], key); // Update value of that index when the slot is empty
222-
else vis.array.updateValueAt(VALUE, inserts[key], slotCurValue + ((table[inserts[key]].length >= 2 && typeof(slotCurValue) == 'number') ? ".." : "")); // Update value of that index when the slot is not empty
236+
console.log(typeof(slotCurValue), key, slotCurValue, table[inserts[key]]);
237+
if (slotCurValue === EMPTY_CHAR_CH)
238+
// Update value of that index when the slot is empty
239+
vis.array.updateValueAt(VALUE, inserts[key], '[' + key + ']');
240+
else
241+
// / Update value of that index when the slot is not empty
242+
vis.array.updateValueAt(VALUE, inserts[key], '[' + key + '..');
223243
vis.array.fill(INDEX, inserts[key], undefined, undefined, Colors.Insert);
224244
}
225245
vis.array.showKth({key: vis.array.getKth().key, type: HASH_TYPE.BulkInsert, insertions: insertions});
@@ -265,11 +285,12 @@ export default {
265285
INDEX,
266286
{
267287
rowLength: size > SMALL_SIZE ? SPLIT_SIZE : SMALL_SIZE,
268-
rowHeader: ['Index', 'Value', '']
288+
rowHeader: ['Index', 'Chain', '']
269289
}
270290
);
271291

272-
vis.array.hideArrayAtIndex([VALUE, POINTER]); // Hide value and pointer row intially
292+
// vis.array.hideArrayAtIndex([VALUE, POINTER]); // Hide value and pointer row intially
293+
vis.array.hideArrayAtIndex([POINTER]); // Hide pointer row intially
273294

274295
vis.graph.weighted(true);
275296

@@ -296,8 +317,9 @@ export default {
296317
);
297318

298319
// Chunker to initialize empty array visually
320+
/*
299321
chunker.add(
300-
IBookmarks.EmptyArray,
322+
IBookmarks.Init,
301323
(vis) => {
302324
// Show the value row
303325
vis.array.hideArrayAtIndex(POINTER);
@@ -306,7 +328,7 @@ export default {
306328
307329
// Chunker for intializing insertion stat
308330
chunker.add(
309-
IBookmarks.InitInsertion,
331+
IBookmarks.EmptyArray,
310332
(vis, insertions) => {
311333
vis.array.showKth({
312334
key: "",
@@ -318,6 +340,7 @@ export default {
318340
},
319341
[insertions]
320342
)
343+
*/
321344

322345
// Magic numbers for length of splitting a postive integer string by "-", the index of "", and the number to delete when a negative integer is split by "-"
323346
const POS_INTEGER_SPLIT_LENGTH = 1;

src/algorithms/controllers/HashingCommon.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const FULL_SIGNAL = -1;
1313

1414
// Magic character used between all 3 files
1515
export const POINTER_VALUE = 'i'
16-
export const EMPTY_CHAR = '-';
16+
export const EMPTY_CHAR = '-'; // for empty slots
17+
export const EMPTY_CHAR_CH = '[]'; // for empty slots with chaining
1718
export const DELETE_CHAR = 'X';
1819

1920
// Color indexes

src/algorithms/controllers/HashingDelete.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
DELETE_CHAR,
1212
HASH_TYPE,
1313
newCycle,
14-
EMPTY_CHAR
14+
EMPTY_CHAR,
15+
EMPTY_CHAR_CH
1516
} from './HashingCommon';
1617

1718
// Bookmarks to link chunker with pseudocode
@@ -24,7 +25,8 @@ const IBookmarks = {
2425
Found: 14,
2526
Delete: 15,
2627
NotFound: 18,
27-
Pending: 19,
28+
Pending: 20,
29+
CheckFull: 21,
2830
}
2931

3032
/**
@@ -157,13 +159,15 @@ export default function HashingDelete(
157159
/** This part is for Chaining */
158160
else {
159161

162+
/*
160163
chunker.add(
161164
IBookmarks.Pending,
162165
(vis, idx) => {
163166
vis.array.fill(INDEX, idx, undefined, undefined, Colors.Pending); // Fill pending slots with yellow
164167
},
165168
[i]
166169
);
170+
*/
167171

168172
if (table[i].includes(key)) {
169173
const index = table[i].indexOf(key);
@@ -176,9 +180,15 @@ export default function HashingDelete(
176180
const popper = document.getElementById('float_box_' + idx);
177181
popper.innerHTML = table[idx];
178182

179-
let firstItemOfChain = table[idx][0];
180-
if (firstItemOfChain != undefined) vis.array.updateValueAt(VALUE, idx, firstItemOfChain + '..');
181-
else vis.array.updateValueAt(VALUE, idx, EMPTY_CHAR);
183+
let chainLength = table[idx].length;
184+
// let firstItemOfChain = table[idx][0];
185+
// if (firstItemOfChain === undefined)
186+
if (chainLength === 0)
187+
vis.array.updateValueAt(VALUE, idx, EMPTY_CHAR_CH);
188+
else if (chainLength === 1)
189+
vis.array.updateValueAt(VALUE, idx, '[' + table[idx][0] + ']');
190+
else
191+
vis.array.updateValueAt(VALUE, idx, '[' + table[idx][0] + '..');
182192

183193
vis.array.fill(INDEX, idx, undefined, undefined, Colors.Found); // Fill the slot with green, indicating that the key is found
184194
},
@@ -189,7 +199,8 @@ export default function HashingDelete(
189199
}
190200
else {
191201
chunker.add(
192-
IBookmarks.NotFound,
202+
// IBookmarks.NotFound, // delete code no longer expanded
203+
IBookmarks.Found,
193204
(vis, idx) => {
194205
vis.array.fill(INDEX, idx, undefined, undefined, Colors.NotFound); // Fill the slot with green, indicating that the key is found
195206
},

0 commit comments

Comments
 (0)