diff --git a/src/rt/rfwfs/collection.ts b/src/rt/rfwfs/collection.ts new file mode 100644 index 0000000..a6a97b7 --- /dev/null +++ b/src/rt/rfwfs/collection.ts @@ -0,0 +1,46 @@ +import { type Entry } from "./main" + +import entry_search from "./index" + +type Files = Entry[] + +interface EntryCollectionManipulate { + pop: >(file_name: string) => Entry | undefined, + get: >(file_name: string) => Entry | undefined + push: >(entry: Entry) => boolean, + sort: () => void, +} +interface EntryCollection extends EntryCollectionManipulate { + readonly inner: Files +} + +function entry_trait>() { + const trait = {} as EntryCollection + trait.sort = function() { + this.inner.sort((a,z) => a.name.localeCompare(z.name)) + } + trait.push = function(entry) { + const no_duplicates = entry_search(this.inner, entry.name) + if (!no_duplicates) { + this.push(entry) + this.sort() + return true + } + return false + } + trait.pop = function(file_name) { + const file_search = entry_search(this.inner, file_name) + if (file_search) { + this.inner.splice(file_search.index, 1) + return file_search.result + } + return + } + return trait +} + +export { + entry_trait, + type EntryCollection, + type Files, +} \ No newline at end of file diff --git a/src/rt/rfwfs/core.ts b/src/rt/rfwfs/core.ts deleted file mode 100644 index 57e4615..0000000 --- a/src/rt/rfwfs/core.ts +++ /dev/null @@ -1,20 +0,0 @@ -import rfwfs_search from "./index" -import rfwfs_entry_trait, {EntryType, type Entry, type EntryTree} from "./entry" - - - -function Entry(name: string, inner: T, permissions: Permissions): Entry { - return { - type: typeof inner == "object" ? EntryType.Directory : EntryType.File, - inner: inner, - name: name, - permissions: permissions, - timestamp: Math.floor(Date.now()/1000) - } -} - -export { - EntryType, - Permissions, - Entry -} \ No newline at end of file diff --git a/src/rt/rfwfs/entry.ts b/src/rt/rfwfs/entry.ts deleted file mode 100644 index 7cd95ef..0000000 --- a/src/rt/rfwfs/entry.ts +++ /dev/null @@ -1,89 +0,0 @@ -import rfwfs_search from "./index" - -const enum EntryType { - Directory, - File -} - -const enum Permissions { - r, - w, - rw, - none -} - -type Files = Entry[] -type File = string - -interface EntryListManipulate { - push: >(entry: Entry) => boolean, - pop: >(file_name: string) => Entry | undefined, - sort: () => void, -} - -interface EntryList extends EntryListManipulate { - readonly tree: Files -} - -interface Entry { - readonly inner: EntryList, - readonly name: string, - readonly type: EntryType, - readonly timestamp: number, - readonly permissions: Permissions, -} - -function rfwfs_entry_trait>(): EntryList { - const trait = {} as EntryList - 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(files: Files) { - -} - -function rfwfs_new_entry>( - name: string, - inner: T, - permissions: Permissions, - timestamp: number = Math.floor(Date.now()/1000) -): Entry { - 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, -} \ No newline at end of file diff --git a/src/rt/rfwfs/index.ts b/src/rt/rfwfs/index.ts index f1b58c0..7727ec1 100644 --- a/src/rt/rfwfs/index.ts +++ b/src/rt/rfwfs/index.ts @@ -1,77 +1,28 @@ -import { type Entry, type Files } from "./entry" +import { type Entry } from "./main" +import { type Files } from "./collection" interface SearchResult { - item: T, - binary_index: number + readonly result: T, + readonly index: number } -interface Search { - binary_fs_name: >(cloned_list: Files, file_name: string) => SearchResult> | undefined, - binary_nsort: (list: T[], find: T, start?: number, end?: number) => SearchResult | undefined, - binary: (list: T[], find: T) => SearchResult | undefined, - linear: (list: T[], find: T) => SearchResult | undefined, +function wrap_result(result: T, index: number): SearchResult { + return { result: result, index: index } } -function wrap_result(item: T, binary_index: number): SearchResult { - return { item: item, binary_index: binary_index } -} - -const rfwfs_search = {} as Search - -rfwfs_search.binary = function(list, find) { - list.sort() +export default function entry_search(cloned_file_collection: Files, file_name: string): SearchResult> | undefined { let start = 0 - let end = list.length-1 + let end = cloned_file_collection.length-1 while (start<=end) { const median = (start+end)>>1 - if (list[median] === find) { - return wrap_result(list[median], median) - } else if (list[median]end) { return } - const median = (start+end)>>1 - if (list[median] === find) { - return wrap_result(list[median], median) - } - if (list[median]>find) { - return this.binary_nsort(list, find, start, median-1) - } else { - return this.binary_nsort(list, find, median+1, end) - } -} - -rfwfs_search.binary_fs_name = function(cloned_entry_list, file_name) { - let start = 0 - let end = cloned_entry_list.length-1 - while (start<=end) { - const median = (start+end)>>1 - const median_name = cloned_entry_list[median].name - - if (median_name === file_name) { - return wrap_result(cloned_entry_list[median], median) + if (median_name == file_name) { //left == right && (T == U) is not necessary + return wrap_result(cloned_file_collection[median], median) } else if (median_name [...working_path] - -function get_working_dir_name() { - return working_path[working_path.length-1] -} - -function get_working_dir_name_full(): string { - const w_dir_clone = clone_working_path() - const root = w_dir_clone.shift() - if (root) { - return root+w_dir_clone.join("/") - } - return "shift-error" -} - -const enum SetDirStatus { - Valid, - NotFound, - NotADirectory, - Invalid -} -interface FsIterEntry { - readonly entry: FsDirectory | null, - readonly status: SetDirStatus -} -function find_fs_dir(working_dir_path_clone: string[], find_dir_name: string): FsIterEntry { - let cached_dir_clone = cached_dir.inner - - for (let path_i = 0; path_i cached_dir_file_names.push(file.name)) - - const search_result = index.binary(cached_dir_clone, fstree[0]) - - if (working_dir_path_clone[path_i] === find_dir_name) { - cached_dir_clone = cached_dir_clone - if (path_i === working_dir_path_clone.length) { - const search_result = index.binary(cached_dir_file_names, find_dir_name) - if (search_result) { - - } - } else { - continue - } - } - } - return { entry: null, status: SetDirStatus.Invalid } - } - return { entry: null, status: SetDirStatus.NotFound } -} - -function set_working_dir(name: string): SetDirStatus { - if (name === ".") { return SetDirStatus.Valid } - - const w_dir_clone = clone_working_path() - if (name === "..") { - w_dir_clone.pop() - - } else { - w_dir_clone.push(name) - } - - -} - -function get_working_dir_entries(): FsEntry[] { - -} - -export { - get_working_dir_name, - get_working_dir_name_full, - get_working_dir_entries, - set_working_dir, - SetDirStatus -} \ No newline at end of file diff --git a/src/rt/rfwfs/main.ts b/src/rt/rfwfs/main.ts new file mode 100644 index 0000000..ad9d1fe --- /dev/null +++ b/src/rt/rfwfs/main.ts @@ -0,0 +1,47 @@ +import { type EntryCollection } from "./collection" + +const enum EntryType { + Directory, + File +} +const enum Permissions { + r, + w, + rw, + none +} + +interface Entry { + readonly name: string, + readonly type: EntryType, + readonly inner: T, + readonly timestamp: number, + readonly collection?: EntryCollection, + readonly permissions: Permissions, +} +interface Rfwfs { + new_entry: >(name: string, permissions: Permissions, timestamp: number, inner: T) => Entry, + entry_trait: >(inner: T) => EntryCollection + new_entry_tree: (files: T) => T, +} + +const rfwfs = {} as Rfwfs + +rfwfs.new_entry = function(name, permissions, timestamp, inner) { + const file_type = typeof inner == "object" ? EntryType.Directory : EntryType.File + return { + name: name, + type: file_type, + inner: inner, + timestamp: timestamp ? timestamp : Math.floor(Date.now()/1000), + collection: file_type === EntryType.Directory ? this.entry_trait(inner) : undefined, + permissions: permissions, + } +} + +export { + EntryType, + Permissions, + type EntryCollection, + type Entry, +} \ No newline at end of file diff --git a/src/rt/rfwfs/tree.ts b/src/rt/rfwfs/tree.ts deleted file mode 100644 index 71183c7..0000000 --- a/src/rt/rfwfs/tree.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Entry, Permissions } from "./core" - -const user = [ - Entry("about_me.txt", "about me inside", Permissions.rw), - Entry("services.txt", "services inside", Permissions.rw), - Entry("hi", [], Permissions.rw) -] -const home = [ - Entry("user", user, Permissions.rw) -] -const root = [ - Entry("home", home, Permissions.r), - Entry("bin", {}, Permissions.r), -] -const fstree = [ - Entry("/", root, Permissions.r) -] - -export default fstree \ No newline at end of file