Start of binary search for ls command
This commit is contained in:
48
src/lib/stores/bash/search.ts
Normal file
48
src/lib/stores/bash/search.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { Bash } from "./bash";
|
||||
import type { TreeNode } from "./fs";
|
||||
|
||||
export class Search {
|
||||
|
||||
public static nodeBinarySearch(
|
||||
this: Bash,
|
||||
nodes: TreeNode[],
|
||||
max: number
|
||||
): number {
|
||||
let low: number = 1;
|
||||
let high: number = nodes.length;
|
||||
let c: number = (low + high) / 2;
|
||||
const nameLengths: number[] = nodes.map(node => node.name.length);
|
||||
|
||||
}
|
||||
|
||||
private static binarySearchDNC(
|
||||
n: number,
|
||||
nameLengths: number[],
|
||||
low: number,
|
||||
high: number,
|
||||
max: number): number {
|
||||
|
||||
let c: number = (low + high) / 2;
|
||||
|
||||
if(low + 1 < high) {
|
||||
for(let i = 0; i < c; i++) {
|
||||
const colSize: number =
|
||||
i < (n % c) ? n : n -1;
|
||||
}
|
||||
|
||||
const calculatedWidth: number =
|
||||
nameLengths.reduce((result, value) => result + value) + (c-1) * 2
|
||||
|
||||
if (calculatedWidth <= max) {
|
||||
low = c;
|
||||
return c;
|
||||
}
|
||||
else {
|
||||
high = c;
|
||||
}
|
||||
|
||||
this.binarySearchDNC(n, nameLengths, low, high, max);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user