Skip to content

Commit 6fe76b1

Browse files
committed
Remove visualization controls and related functionality from search algorithms for improved performance and simplicity.
1 parent ca32b2d commit 6fe76b1

1 file changed

Lines changed: 44 additions & 119 deletions

File tree

index.html

Lines changed: 44 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -650,20 +650,6 @@ <h2>Selecione o livro</h2>
650650
<h2>Buscar por:</h2>
651651
<input type="text" class="search-input" placeholder="Digite a palavra ou frase..." value="Caladan">
652652
<button class="search-btn">Buscar</button>
653-
654-
<div class="control-panel">
655-
<div class="toggle-container">
656-
<span>Visualização:</span>
657-
<label class="toggle-switch">
658-
<input type="checkbox" id="visualization-toggle" checked>
659-
<span class="slider"></span>
660-
</label>
661-
</div>
662-
<div class="delay-container">
663-
<span>Delay (ms):</span>
664-
<input type="number" id="delay-input" class="delay-input" value="10" min="0" max="1000">
665-
</div>
666-
</div>
667653
</div>
668654

669655
<div class="results-section">
@@ -697,9 +683,6 @@ <h2>Resultados:</h2>
697683
let bookContent = '';
698684
let selectedBook = null;
699685
let isSearching = false;
700-
let visualizationEnabled = true;
701-
let visualizationDelay = 10; // ms
702-
let updateFrequency = 1000; // Check every 1000 positions
703686

704687
// DOM Elements
705688
const books = document.querySelectorAll('.book');
@@ -711,25 +694,6 @@ <h2>Resultados:</h2>
711694
const rkTimeDisplay = document.querySelector('.rk-time');
712695
const naiveProgress = document.querySelector('.naive-progress');
713696
const rkProgress = document.querySelector('.rk-progress');
714-
const visualizationToggle = document.getElementById('visualization-toggle');
715-
const delayInput = document.getElementById('delay-input');
716-
717-
// Set up visualization controls
718-
visualizationToggle.addEventListener('change', function() {
719-
visualizationEnabled = this.checked;
720-
delayInput.disabled = !visualizationEnabled;
721-
722-
if (!visualizationEnabled) {
723-
updateFrequency = Number.MAX_SAFE_INTEGER; // Effectively disable updates
724-
} else {
725-
updateFrequency = 1000; // Reset to default
726-
visualizationDelay = parseInt(delayInput.value) || 10;
727-
}
728-
});
729-
730-
delayInput.addEventListener('change', function() {
731-
visualizationDelay = parseInt(this.value) || 10;
732-
});
733697

734698
// Set up book selection
735699
books.forEach(book => {
@@ -811,8 +775,8 @@ <h2>Resultados:</h2>
811775
}, 500); // 500ms delay for better UX
812776
}
813777

814-
// Naive Search Algorithm with visualization
815-
async function naiveSearch(text, pattern, updateProgress) {
778+
// Naive Search Algorithm without visualization
779+
function naiveSearch(text, pattern, updateProgress) {
816780
const occurrences = [];
817781
const textLength = text.length;
818782
const patternLength = pattern.length;
@@ -821,22 +785,13 @@ <h2>Resultados:</h2>
821785
if (patternLength > textLength) return [];
822786
if (patternLength === 0) return [];
823787

824-
// Clear previous highlights if visualization is enabled
825-
if (visualizationEnabled) {
826-
updateTextDisplay(text);
827-
}
788+
// Update progress at the start
789+
updateProgress(0);
828790

829-
// Brute force search with visualization
791+
// Brute force search without delays
830792
for (let i = 0; i <= textLength - patternLength; i++) {
831793
let match = true;
832794

833-
// Update progress periodically if visualization is enabled
834-
if (visualizationEnabled && (i % updateFrequency === 0 || i === 0)) {
835-
await new Promise(resolve => setTimeout(resolve, 0)); // Let the UI breathe
836-
updateProgress(i / (textLength - patternLength + 1));
837-
highlightCurrent(i, patternLength, 'examining');
838-
}
839-
840795
for (let j = 0; j < patternLength; j++) {
841796
if (text[i + j] !== pattern[j]) {
842797
match = false;
@@ -846,10 +801,6 @@ <h2>Resultados:</h2>
846801

847802
if (match) {
848803
occurrences.push(i);
849-
if (visualizationEnabled) {
850-
highlightMatch(i, patternLength);
851-
await new Promise(resolve => setTimeout(resolve, visualizationDelay));
852-
}
853804
}
854805
}
855806

@@ -859,8 +810,8 @@ <h2>Resultados:</h2>
859810
return occurrences;
860811
}
861812

862-
// Rabin-Karp Search Algorithm with visualization
863-
async function rabinKarpSearch(text, pattern, updateProgress) {
813+
// Rabin-Karp Search Algorithm without visualization
814+
function rabinKarpSearch(text, pattern, updateProgress) {
864815
const occurrences = [];
865816
const textLength = text.length;
866817
const patternLength = pattern.length;
@@ -869,10 +820,8 @@ <h2>Resultados:</h2>
869820
if (patternLength > textLength) return [];
870821
if (patternLength === 0) return [];
871822

872-
// Clear previous highlights but keep matches from naive search
873-
if (visualizationEnabled) {
874-
updateTextDisplay(text, true);
875-
}
823+
// Update progress at start
824+
updateProgress(0);
876825

877826
// Prime number for hash calculation
878827
const prime = 101;
@@ -890,15 +839,8 @@ <h2>Resultados:</h2>
890839
const patternHash = hash(pattern, patternLength);
891840
let textHash = hash(text, patternLength);
892841

893-
// Search with visualization
842+
// Search without delays
894843
for (let i = 0; i <= textLength - patternLength; i++) {
895-
// Update progress periodically if visualization is enabled
896-
if (visualizationEnabled && (i % updateFrequency === 0 || i === 0)) {
897-
await new Promise(resolve => setTimeout(resolve, 0)); // Let the UI breathe
898-
updateProgress(i / (textLength - patternLength + 1));
899-
highlightCurrent(i, patternLength, 'examining-rk');
900-
}
901-
902844
// Check if hash values match
903845
if (patternHash === textHash) {
904846
// Verify character by character
@@ -911,10 +853,6 @@ <h2>Resultados:</h2>
911853
}
912854
if (match) {
913855
occurrences.push(i);
914-
if (visualizationEnabled) {
915-
highlightMatch(i, patternLength, 'match-rk');
916-
await new Promise(resolve => setTimeout(resolve, visualizationDelay));
917-
}
918856
}
919857
}
920858

@@ -926,17 +864,13 @@ <h2>Resultados:</h2>
926864
}
927865
}
928866

867+
// Update progress at end
929868
updateProgress(1);
930869
return occurrences;
931870
}
932871

933872
// Function to update text display
934873
function updateTextDisplay(text, keepMatches = false) {
935-
if (!visualizationEnabled) {
936-
textDisplay.textContent = text;
937-
return;
938-
}
939-
940874
if (!keepMatches) {
941875
textDisplay.textContent = text;
942876
} else {
@@ -950,14 +884,6 @@ <h2>Resultados:</h2>
950884

951885
// Function to highlight current position being examined
952886
function highlightCurrent(index, length, className = 'examining') {
953-
if (!visualizationEnabled) return;
954-
955-
const oldSpans = textDisplay.querySelectorAll('.highlight-current');
956-
oldSpans.forEach(span => {
957-
const textNode = document.createTextNode(span.textContent);
958-
span.parentNode.replaceChild(textNode, span);
959-
});
960-
961887
const textContent = textDisplay.textContent;
962888
const beforeText = textContent.substring(0, index);
963889
const highlightText = textContent.substring(index, index + length);
@@ -977,8 +903,6 @@ <h2>Resultados:</h2>
977903

978904
// Function to highlight matches
979905
function highlightMatch(index, length, className = 'match') {
980-
if (!visualizationEnabled) return;
981-
982906
const textContent = textDisplay.textContent;
983907
const beforeText = textContent.substring(0, index);
984908
const matchText = textContent.substring(index, index + length);
@@ -998,19 +922,15 @@ <h2>Resultados:</h2>
998922
// Function to jump to position and highlight
999923
function jumpToPosition(position, length) {
1000924
const textContent = document.querySelector('.text-content');
1001-
const text = textContent.textContent;
1002925

1003-
// Clear previous highlights
1004-
const spans = textContent.querySelectorAll('.highlight-match');
1005-
spans.forEach(span => {
1006-
const textNode = document.createTextNode(span.textContent);
1007-
span.parentNode.replaceChild(textNode, span);
1008-
});
926+
// First, clear all existing highlights to prevent interference
927+
const originalText = bookContent;
928+
textContent.textContent = originalText;
1009929

1010-
// Create new highlight
1011-
const beforeText = text.substring(0, position);
1012-
const matchText = text.substring(position, position + length);
1013-
const afterText = text.substring(position + length);
930+
// Now create a new highlight at the correct position
931+
const beforeText = originalText.substring(0, position);
932+
const matchText = originalText.substring(position, position + length);
933+
const afterText = originalText.substring(position + length);
1014934

1015935
textContent.innerHTML = '';
1016936
textContent.appendChild(document.createTextNode(beforeText));
@@ -1038,28 +958,34 @@ <h2>Resultados:</h2>
1038958
// Function to highlight all matches
1039959
function highlightAllMatches(positions, length) {
1040960
const textContent = document.querySelector('.text-content');
1041-
const text = textContent.textContent;
1042961

1043-
// Clear previous highlights
1044-
const spans = textContent.querySelectorAll('.highlight-match');
1045-
spans.forEach(span => {
1046-
const textNode = document.createTextNode(span.textContent);
1047-
span.parentNode.replaceChild(textNode, span);
1048-
});
962+
// Start with the original text from bookContent
963+
const originalText = bookContent;
1049964

1050-
// Sort positions in descending order to avoid offset issues
1051-
positions.sort((a, b) => b - a);
965+
// Build HTML with highlights
966+
let resultHTML = '';
967+
let lastIndex = 0;
1052968

1053-
let currentText = text;
969+
// Sort positions in ascending order
970+
positions.sort((a, b) => a - b);
971+
972+
// Add each highlight
1054973
positions.forEach(pos => {
1055-
const beforeText = currentText.substring(0, pos);
1056-
const matchText = currentText.substring(pos, pos + length);
1057-
const afterText = currentText.substring(pos + length);
974+
// Add text before this match
975+
resultHTML += originalText.substring(lastIndex, pos);
976+
977+
// Add the highlighted match
978+
resultHTML += `<span class="highlight-match">${originalText.substring(pos, pos + length)}</span>`;
1058979

1059-
currentText = beforeText + `<span class="highlight-match">${matchText}</span>` + afterText;
980+
// Update lastIndex to after this match
981+
lastIndex = pos + length;
1060982
});
1061983

1062-
textContent.innerHTML = currentText;
984+
// Add any remaining text
985+
resultHTML += originalText.substring(lastIndex);
986+
987+
// Set the HTML
988+
textContent.innerHTML = resultHTML;
1063989
}
1064990

1065991
// Update the results display HTML
@@ -1150,7 +1076,7 @@ <h4>Posições encontradas:</h4>
11501076
}
11511077
}
11521078

1153-
// Update the runSearch function to use the new highlighting
1079+
// Update the runSearch function to remove all delays
11541080
async function runSearch(pattern) {
11551081
if (isSearching) return;
11561082
isSearching = true;
@@ -1166,24 +1092,23 @@ <h4>Posições encontradas:</h4>
11661092

11671093
try {
11681094
const naiveStartTime = performance.now();
1169-
const naiveResults = await naiveSearch(bookContent, pattern, (progress) => {
1095+
const naiveResults = naiveSearch(bookContent, pattern, (progress) => {
11701096
naiveProgress.style.width = `${progress * 100}%`;
11711097
});
11721098
const naiveEndTime = performance.now();
11731099
const naiveTime = naiveEndTime - naiveStartTime;
11741100

11751101
const rkStartTime = performance.now();
1176-
const rkResults = await rabinKarpSearch(bookContent, pattern, (progress) => {
1102+
const rkResults = rabinKarpSearch(bookContent, pattern, (progress) => {
11771103
rkProgress.style.width = `${progress * 100}%`;
11781104
});
11791105
const rkEndTime = performance.now();
11801106
const rkTime = rkEndTime - rkStartTime;
11811107

11821108
updateResultsDisplay(pattern, naiveResults, rkResults, naiveTime, rkTime);
11831109

1184-
if (visualizationEnabled) {
1185-
highlightAllMatches(naiveResults, pattern.length);
1186-
}
1110+
// Still highlight matches after search is complete
1111+
highlightAllMatches(naiveResults, pattern.length);
11871112

11881113
} catch (error) {
11891114
console.error('Error during search:', error);

0 commit comments

Comments
 (0)