add binary search

This commit is contained in:
Triston Armstrong 2024-02-16 19:35:38 -06:00
parent 2fd3530911
commit 9fc0562b61
3 changed files with 43 additions and 13 deletions

View File

@ -35,10 +35,11 @@ export default function FolderTree(props) {
defaultExpanded={expandedNodes} defaultExpanded={expandedNodes}
defaultExpandIcon={<ChevronRightIcon />} defaultExpandIcon={<ChevronRightIcon />}
onNodeSelect={(event, nodIds) => { onNodeSelect={(event, nodIds) => {
// TODO: already sorted, impliment binary search const currentNode = binaryFind(props.flattenNodes, nodIds);
const currentNode = props.flattenNodes.find((aNode) => { // const currentNode = props.flattenNodes.find((aNode) => {
return aNode.id === nodIds; // console.log(aNode.id);
}); // return aNode.id === nodIds;
// });
console.log({ currentNode }); console.log({ currentNode });
if (currentNode != null && currentNode.routePath != null) { if (currentNode != null && currentNode.routePath != null) {
router.push(currentNode.routePath); router.push(currentNode.routePath);
@ -52,6 +53,31 @@ export default function FolderTree(props) {
); );
} }
function binaryFind(sourceList, valToFind) {
let leftIndex = 0;
let rightIndex = sourceList.length - 1;
let middleIndex;
while (rightIndex >= leftIndex) {
middleIndex = leftIndex + Math.floor((rightIndex - leftIndex) / 2);
const currentValue = sourceList[middleIndex];
console.log(currentValue);
// If the element is present at the middle
// itself
if (currentValue.id == valToFind) return currentValue;
// If element is smaller than mid, then
// it can only be present in left subarray
if (currentValue.id > valToFind) rightIndex = middleIndex - 1;
// Else the element can only be present
// in right subarray
else leftIndex = middleIndex + 1;
}
// We reach here when element is not
// present in array
return null;
}
function ChevronRightIcon() { function ChevronRightIcon() {
return ( return (
<svg <svg

View File

@ -4,7 +4,6 @@ import ListItem, { type ListItemProps } from '../components/portfolio/ListItem'
import { UsefulLinksList } from '../components/UsefulLinks/index' import { UsefulLinksList } from '../components/UsefulLinks/index'
import { SocialLinksList } from '../components/SocialLinks/SocialLinks' import { SocialLinksList } from '../components/SocialLinks/SocialLinks'
import type { GetStaticProps } from 'next' import type { GetStaticProps } from 'next'
import AlienHeader from 'components/portfolio/AlienHeader'
import Head from 'next/head' import Head from 'next/head'
interface LandingProps { interface LandingProps {
@ -19,6 +18,7 @@ export default function Landing({ jobs, skills, projects }: LandingProps): React
<Head> <Head>
<title>Triston Armstrong</title> <title>Triston Armstrong</title>
</Head> </Head>
<div> <div>
<article className='flex-row align-baseline'> <article className='flex-row align-baseline'>
<p>👷 This site is under construction 🚧 🏗 🚧</p> <p>👷 This site is under construction 🚧 🏗 🚧</p>
@ -26,6 +26,16 @@ export default function Landing({ jobs, skills, projects }: LandingProps): React
<br /> <br />
<div className='flex-row gap-1'>
<a href="/notes" target="_blank" className='text-md font-muted font-bold'>🏃 Notes</a>
{'|'}
<a href="/playground" target="_blank" className='text-md font-muted font-bold'>🏃 Playground</a>
{'|'}
<a href="/thai" target="_blank" className='text-md font-muted font-bold'>🏃 Thai Playground</a>
</div>
<br />
<header> <header>
<div className="flex-col gap-05"> <div className="flex-col gap-05">
@ -47,7 +57,7 @@ export default function Landing({ jobs, skills, projects }: LandingProps): React
<hr /> <hr />
</header> </header>
<main className=""> <main >
<section> <section>
<h4>🤹 Skills <i className='font-muted'><small>(Click me to fast track to docs)</small></i></h4> <h4>🤹 Skills <i className='font-muted'><small>(Click me to fast track to docs)</small></i></h4>
<div className="flex gap-1" id="skills"> <div className="flex gap-1" id="skills">
@ -71,12 +81,6 @@ export default function Landing({ jobs, skills, projects }: LandingProps): React
</main> </main>
<footer className='flex-col flex-between gap-1 mt-4 '> <footer className='flex-col flex-between gap-1 mt-4 '>
<div className='flex-row gap-1'>
<a href="/notes" target="_blank" className='text-md font-muted font-bold'>🏃 Notes</a>
{'|'}
<a href="/playground" target="_blank" className='text-md font-muted font-bold'>🏃 Playground</a>
</div>
<div className='flex-row gap-1'> <div className='flex-row gap-1'>
<UsefulLinksList /> <UsefulLinksList />
</div> </div>

View File

@ -9,7 +9,7 @@ export interface HomeProps {
flattenNodes: unknown[] flattenNodes: unknown[]
backLinks: LinkType[] backLinks: LinkType[]
} }
// This trick is to dynamically load component that interact with window object (browser only)
export default function Home({ content, tree, flattenNodes, backLinks }: HomeProps) { export default function Home({ content, tree, flattenNodes, backLinks }: HomeProps) {
return ( return (
<Layout> <Layout>