diff --git a/src/rt/rfwfs/collection.ts b/src/rt/rfwfs/collection.ts deleted file mode 100644 index 1e9a405..0000000 --- a/src/rt/rfwfs/collection.ts +++ /dev/null @@ -1,58 +0,0 @@ -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 sort>(self: EntryCollection) { - self.inner.sort((a,z) => a.name.localeCompare(z.name)) -} - -function push>(self: EntryCollection, entry: Entry) { - const no_duplicates = entry_search(self.inner, entry.name) - if (!no_duplicates) { - self.push(entry) - self.sort() - return true - } - return false -} - -function get>(self: EntryCollection, file_name: string) { - const file_search = entry_search(self.inner, file_name) - return file_search ? file_search.result : undefined -} - -function pop>(self: EntryCollection, file_name: string) { - const file_search = entry_search(self.inner, file_name) - if (file_search) { - self.inner.splice(file_search.index, 1) - return file_search.result - } - return undefined -} - -function entry_collection>(inner: Files): EntryCollection { - const collection = { inner: inner } as EntryCollection - collection.sort = function() { return sort(this) } - collection.push = function(entry) { return push(this, entry) } - collection.get = function(file_name) { return get(this, file_name) } - collection.pop = function(file_name) { return pop(this, file_name) } - return collection -} - -export { - entry_collection, - type EntryCollection, - type Files, -} \ No newline at end of file diff --git a/src/rt/rfwfs/index.ts b/src/rt/rfwfs/index.ts index 79b9bbd..ec5785f 100644 --- a/src/rt/rfwfs/index.ts +++ b/src/rt/rfwfs/index.ts @@ -1,5 +1,4 @@ import { type Entry } from "./main" -import { type Files } from "./collection" interface Wrap { readonly result: T, @@ -9,14 +8,14 @@ function wrap_result(result: T, index: number): Wrap { return { result: result, index: index } } -export default function entry_search>(cloned_file_collection: Files, file_name: string): Wrap> | undefined { +export default function entry_search(cloned_file_collection: T[], file_name: string): Wrap | undefined { let start = 0 let end = cloned_file_collection.length-1 while (start<=end) { const median = (start+end)>>1 const median_name = cloned_file_collection[median].name - if (median_name == file_name) { //left == right && (T == U) is not necessary + if (median_name === file_name) { return wrap_result(cloned_file_collection[median], median) } else if (median_name> { +interface Entry { readonly name: string, readonly type: EntryType, - readonly inner: T, readonly timestamp: number, - readonly collection?: EntryCollection, readonly permissions: Permissions, } +interface EntryCollectionInner extends Entry { + readonly collection: T[], +} +interface EntryFile extends Entry { + inner: T +} + +interface EntryCollection extends EntryCollectionInner { + pop: (file_name: string) => T | undefined, + get: (file_name: string) => T | undefined + push: (entry: E) => boolean, + sort: () => void, +} interface Rfwfs { - new_entry: >(name: string, permissions: Permissions, timestamp: number, inner: T) => Entry, - entry_trait: >(inner: T) => EntryCollection - new_entry_tree: (files: T) => T, + is_dir: (entry: T) => boolean, + is_file: (entry: T) => boolean, + new_entry: (name: string, permissions: Permissions, timestamp: number, inner: T) => EntryFile, + new_collection: (inner: T[]) => EntryCollection, +} + +function sort(self: EntryCollection) { + self.collection.sort((a,z) => a.name.localeCompare(z.name)) +} + +function push(self: EntryCollection, entry: Entry) { + const no_duplicates = entry_search(self.collection, entry.name) + if (!no_duplicates) { + self.push(entry) + self.sort() + return true + } + return false +} + +function get(self: EntryCollection, file_name: string) { + const file_search = entry_search(self.collection, file_name) + return file_search ? file_search.result : undefined +} + +function pop(self: EntryCollection, file_name: string) { + const file_search = entry_search(self.collection, file_name) + if (file_search) { + self.collection.splice(file_search.index, 1) + return file_search.result + } + return undefined } const rfwfs = {} as Rfwfs +rfwfs.is_dir = function(entry) { + return entry.type === EntryType.Directory +} + +rfwfs.is_file = function(entry) { + return entry.type === EntryType.File +} + rfwfs.new_entry = function(name, permissions, timestamp, inner) { - const file_type = typeof inner == "object" ? EntryType.Directory : EntryType.File return { name: name, - type: file_type, + type: typeof inner === "object" ? EntryType.Directory : EntryType.File, inner: inner, - timestamp: timestamp ? timestamp : Math.floor(Date.now()/1000), - collection: file_type === EntryType.Directory ? this.entry_trait(inner) : undefined, + timestamp: timestamp ? timestamp : (Date.now()/1000)|0, permissions: permissions, } } +rfwfs.new_collection = function(collection: T[]): EntryCollection { + const collection_trait = { collection: collection } as EntryCollection + collection_trait.sort = function() { return sort(this) } + collection_trait.push = function(entry) { return push(this, entry) } + collection_trait.get = function(file_name) { return get(this, file_name) } + collection_trait.pop = function(file_name) { return pop(this, file_name) } + return collection_trait +} + +export default rfwfs export { EntryType, Permissions, diff --git a/src/rt/rfwfs/tree.ts b/src/rt/rfwfs/tree.ts new file mode 100644 index 0000000..433bde0 --- /dev/null +++ b/src/rt/rfwfs/tree.ts @@ -0,0 +1,11 @@ +import rfwfs, { Permissions } from "./main"; + +const time_now = (Date.now()/1000)|0 + +const fs = rfwfs.new_collection([ + rfwfs.new_entry("/", Permissions.r, time_now, rfwfs.new_collection([ + rfwfs.new_entry("home", Permissions.r, time_now, "hi") + ])) +]) + +export default fs \ No newline at end of file