-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
120 lines (114 loc) · 3.7 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
let searchedIndex = null
function clearAndRedraw() {
clear()
redraw()
}
function modalPopUp(error) {
const modelBody = document.querySelector('#model-body')
modelBody.querySelector('p').innerText = error
$('#errorModel').modal()
}
function setup() {
createCanvas(displayWidth, windowHeight)
linear = new LinearHashing()
insertTableInput = createInput()
insertTableInput.position(20, 90)
insertTableInput.attribute('placeholder', 'Insert Key')
insertTableButton = createButton('Insert')
insertTableButton.position(insertTableInput.x + insertTableInput.width + 3, insertTableInput.y - 1)
insertTableButton.addClass('btn btn-primary')
insertTableButton.mousePressed(() => {
searchedIndex = null
const key = insertTableInput.value()
if (key) {
try {
linear.insert(key)
console.log(`${key} inserted!`)
} catch (error) {
console.error(error)
modalPopUp(error)
}
insertTableInput.value('')
}
clearAndRedraw()
})
searchTableInput = createInput()
searchTableInput.position(insertTableInput.x, insertTableInput.y + insertTableInput.height + 8)
searchTableInput.attribute('placeholder', 'Search Key')
searchTableButton = createButton('Search')
searchTableButton.position(searchTableInput.x + searchTableInput.width + 3, searchTableInput.y - 1)
searchTableButton.addClass('btn btn-primary')
searchTableButton.mousePressed(() => {
searchedIndex = null
const key = searchTableInput.value()
if (key) {
try {
searchedIndex = linear.search(key)
} catch (error) {
console.error(error)
modalPopUp(error)
}
searchTableInput.value('')
}
clearAndRedraw()
})
deleteTableInput = createInput()
deleteTableInput.position(searchTableInput.x, searchTableInput.y + searchTableInput.height + 8)
deleteTableInput.attribute('placeholder', 'Delete Key')
deleteTableButton = createButton('Delete')
deleteTableButton.position(deleteTableInput.x + deleteTableInput.width + 3, deleteTableInput.y - 1)
deleteTableButton.addClass('btn btn-primary')
deleteTableButton.mousePressed(() => {
searchedIndex = null
const key = deleteTableInput.value()
if (key) {
try {
linear.delete(key)
console.log(`${key} deleted`)
} catch (error) {
console.error(error)
modalPopUp(error)
}
deleteTableInput.value('')
}
clearAndRedraw()
})
textAlign(CENTER, CENTER)
textSize(30)
ellipseMode(CENTER)
strokeWeight(3)
noLoop()
}
function draw() {
for (let i = 0; i < linear.tableSize; ++i) {
let key = linear.hashTable[i]
if (key === null) {
key = "DEL"
fill('orange')
}
c = getCirclePosition(i)
if (key !== undefined)
stroke('orange')
if (searchedIndex === i)
stroke('blue')
circle(c.x, c.y, 70)
if (key !== undefined) {
if (key == "DEL")
fill(255)
else if (i === searchedIndex)
fill('blue')
else
fill('orange')
text(key, c.x, c.y)
fill(255)
stroke('black')
}
}
}
function getCirclePosition(index) {
return Object.freeze({
x: deleteTableInput.x + 30 + (displayWidth / linear.tableSize) * index,
// y: deleteTableInput.y + deleteTableInput.height + 50
y: windowHeight / 2.25
})
}