entry work experimenting

This commit is contained in:
rhpidfyre 2025-02-27 00:34:17 -05:00
parent 596a21f49d
commit 245315cb09
3 changed files with 106 additions and 36 deletions

View File

@ -1,37 +1,19 @@
const enum EntryType { import rfwfs_search from "./index"
Directory, import rfwfs_entry_trait, {EntryType, type Entry, type EntryTree} from "./entry"
File
}
const enum Permissions {
r,
w,
rw
}
type FsList = FsEntry[]
type FsEntry = Entry<{}>
type FsDirectory = Entry<FsList>
type File = string
interface Entry<T = File> {
readonly inner?: T,
readonly name: string,
readonly type: EntryType,
readonly permissions: Permissions
}
function Entry<T = File>(name: string, inner: T, permissions: Permissions): Entry<T> { function Entry<T = File>(name: string, inner: T, permissions: Permissions): Entry<T> {
return { return {
type: typeof inner == "object" ? EntryType.Directory : EntryType.File, type: typeof inner == "object" ? EntryType.Directory : EntryType.File,
inner: inner, inner: inner,
name: name, name: name,
permissions: permissions permissions: permissions,
timestamp: Math.floor(Date.now()/1000)
} }
} }
export { export {
type FsDirectory,
type FsEntry,
type FsList,
EntryType, EntryType,
Permissions, Permissions,
Entry Entry

89
src/rt/rfwfs/entry.ts Normal file
View File

@ -0,0 +1,89 @@
import rfwfs_search from "./index"
const enum EntryType {
Directory,
File
}
const enum Permissions {
r,
w,
rw,
none
}
type Files<T> = Entry<T>[]
type File = string
interface EntryListManipulate {
push: <T extends Entry<T>>(entry: Entry<T>) => boolean,
pop: <T extends Entry<T>>(file_name: string) => Entry<T> | undefined,
sort: () => void,
}
interface EntryList<T> extends EntryListManipulate {
readonly tree: Files<T>
}
interface Entry<T = File> {
readonly inner: EntryList<T>,
readonly name: string,
readonly type: EntryType,
readonly timestamp: number,
readonly permissions: Permissions,
}
function rfwfs_entry_trait<T extends Entry<T>>(): EntryList<T> {
const trait = {} as EntryList<T>
trait.sort = function() {
this.tree.sort((a,z) => a.name.localeCompare(z.name))
}
trait.push = function(entry) {
const no_duplicates = rfwfs_search.binary_fs_name(this.tree, entry.name)
if (!no_duplicates) {
this.tree.push(entry)
this.sort()
return true
}
return false
}
trait.pop = function(file_name) {
const file_search = rfwfs_search.binary_fs_name(this.tree, file_name)
if (file_search) {
this.tree.splice(file_search.binary_index, 1)
return file_search.item
}
return
}
return trait
}
function rfwfs_new_entry_tree<T>(files: Files<T>) {
}
function rfwfs_new_entry<T extends EntryList<T>>(
name: string,
inner: T,
permissions: Permissions,
timestamp: number = Math.floor(Date.now()/1000)
): Entry<T> {
return {
type: typeof inner == "object" ? EntryType.Directory : EntryType.File,
timestamp: timestamp,
name: name,
permissions: permissions,
inner: inner,
}
}
export {
rfwfs_entry_trait,
rfwfs_new_entry,
EntryType,
Permissions,
type EntryList,
type Entry,
type Files,
type File,
}

View File

@ -1,12 +1,12 @@
import { type FsList, type FsEntry } from "./core" import { type Entry, type Files } from "./entry"
interface SearchResult<T> { interface SearchResult<T> {
item: T, item: T,
binary_index: number binary_index: number
} }
interface Search { interface Search {
binary_fs: (cloned_list: FsList, file_name: string) => SearchResult<FsEntry> | undefined, binary_fs_name: <T extends Entry<T>>(cloned_list: Files<T>, file_name: string) => SearchResult<Entry<T>> | undefined,
binary_nsort: <T>(list: T[], find: T, start: number, end: number) => SearchResult<T> | undefined, binary_nsort: <T>(list: T[], find: T, start?: number, end?: number) => SearchResult<T> | undefined,
binary: <T>(list: T[], find: T) => SearchResult<T> | undefined, binary: <T>(list: T[], find: T) => SearchResult<T> | undefined,
linear: <T>(list: T[], find: T) => SearchResult<T> | undefined, linear: <T>(list: T[], find: T) => SearchResult<T> | undefined,
} }
@ -15,9 +15,9 @@ function wrap_result<T>(item: T, binary_index: number): SearchResult<T> {
return { item: item, binary_index: binary_index } return { item: item, binary_index: binary_index }
} }
const search = {} as Search const rfwfs_search = {} as Search
search.binary = function(list, find) { rfwfs_search.binary = function(list, find) {
list.sort() list.sort()
let start = 0 let start = 0
let end = list.length-1 let end = list.length-1
@ -34,7 +34,7 @@ search.binary = function(list, find) {
return return
} }
search.binary_nsort = function(list, find, start = 0, end = list.length-1) { rfwfs_search.binary_nsort = function(list, find, start = 0, end = list.length-1) {
if (start>end) { return } if (start>end) { return }
const median = (start+end)>>1 const median = (start+end)>>1
if (list[median] === find) { if (list[median] === find) {
@ -47,16 +47,15 @@ search.binary_nsort = function(list, find, start = 0, end = list.length-1) {
} }
} }
search.binary_fs = function(cloned_list, file_name) { rfwfs_search.binary_fs_name = function(cloned_entry_list, file_name) {
cloned_list.sort((a,z) => a.name.localeCompare(z.name))
let start = 0 let start = 0
let end = cloned_list.length-1 let end = cloned_entry_list.length-1
while (start<=end) { while (start<=end) {
const median = (start+end)>>1 const median = (start+end)>>1
const median_name = cloned_list[median].name const median_name = cloned_entry_list[median].name
if (median_name === file_name) { if (median_name === file_name) {
return wrap_result(cloned_list[median], median) return wrap_result(cloned_entry_list[median], median)
} else if (median_name<file_name) { } else if (median_name<file_name) {
start = median+1 start = median+1
} else { } else {
@ -66,7 +65,7 @@ search.binary_fs = function(cloned_list, file_name) {
return return
} }
search.linear = function(list, find) { rfwfs_search.linear = function(list, find) {
for (let ind = 0; ind<list.length; ind++) { for (let ind = 0; ind<list.length; ind++) {
if (list[ind] === find) { if (list[ind] === find) {
return wrap_result(list[ind], ind) return wrap_result(list[ind], ind)
@ -75,4 +74,4 @@ search.linear = function(list, find) {
return return
} }
export default search export default rfwfs_search