From 2cc6263a479087d69d1f7a76919beb8b3c712fc9 Mon Sep 17 00:00:00 2001 From: yukirij Date: Sun, 13 Aug 2023 12:46:49 -0700 Subject: [PATCH] Update README, add sparse list utility, update implementation of schema. --- README.md | 6 ++- src/bin/main.rs | 2 +- src/interface/schema.rs | 2 +- src/interface/util.rs | 2 +- src/lib.rs | 112 ++------------------------------------- src/runtime/lib.cc | 11 ++-- src/runtime/lib.h | 6 +-- src/runtime/sparselist.h | 70 +++++++++++++++++++++++- src/runtime/type.h | 1 + 9 files changed, 90 insertions(+), 122 deletions(-) diff --git a/README.md b/README.md index 3f313f4..cb0ac07 100644 --- a/README.md +++ b/README.md @@ -81,12 +81,14 @@ release(refer); > Warning: To avoid access violations, `release()` should not be used with interface objects, such as Boolean. --- -`encode(refer:Reference) -> vec` +`encode(refer:Reference) -> Vec` +Serializes an object into binary encoding. > Not implemented --- -`decode(data:Vec) -> Reference` +`decode(data:Vec) -> Result` +Parses a valid binary encoding and produces the represented object. > Not implemented --- diff --git a/src/bin/main.rs b/src/bin/main.rs index 933b1e8..32c3e56 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,4 +1,4 @@ fn main() { - + szun::test(); } diff --git a/src/interface/schema.rs b/src/interface/schema.rs index c34303b..4ab4e9e 100644 --- a/src/interface/schema.rs +++ b/src/interface/schema.rs @@ -121,7 +121,7 @@ impl Schema { // name_keyof(unsafe {schema_key(self.addr, index)}) //} - pub fn bind(&mut self, id:usize) -> usize + pub fn bind(&self, id:usize) -> usize { unsafe {schema_bind(self.addr, id)} } diff --git a/src/interface/util.rs b/src/interface/util.rs index ed7a5ca..e149786 100644 --- a/src/interface/util.rs +++ b/src/interface/util.rs @@ -5,7 +5,7 @@ pub fn acquire(type_id:usize) -> runtime::Reference unsafe {runtime::acquire(type_id)} } -pub fn release(addr:Reference) +pub fn release(addr:runtime::Reference) { unsafe {runtime::release(addr)} } diff --git a/src/lib.rs b/src/lib.rs index fcb20f1..cbe60e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,114 +11,10 @@ fn check(expected:T, actual:T) where T:std::fmt::Display, T:std::cmp::Partial println!("'{}' = '{}'", expected, actual); } -/*pub fn test() -{ - { - println!("Boolean"); - let mut dat = Boolean::new(); - check(false, dat.get()); - dat.set(true); - check(true, dat.get()); - dat.set(false); - check(false, dat.get()); - } - - { - println!("Natural"); - let mut dat = Natural::new(); - check(0, dat.get()); - dat.set(50); - check(50, dat.get()); - dat.set(25000); - check(25000, dat.get()); - } - - { - println!("Integer"); - let mut dat = Integer::new(); - check(0, dat.get()); - dat.set(-12); - check(-12, dat.get()); - dat.set(38695); - check(38695, dat.get()); - } - - { - println!("Block"); - let mut dat = Block::new(4); - dat.set(vec![1, 2, 3, 4]); - check(4, dat.get().len()); - check(1, dat.get()[0]); - check(2, dat.get()[1]); - check(3, dat.get()[2]); - check(4, dat.get()[3]); - } - - { - println!("Sequence"); - let mut dat = Sequence::new(); - check(String::from(""), dat.get()); - dat.set("hello"); - check(String::from("hello"), dat.get()); - } - - { - println!("Array"); - let dat = Array::new(4, natural()); - Natural::from(dat.at(0)).unwrap().set(250); - Natural::from(dat.at(1)).unwrap().set(500); - Natural::from(dat.at(2)).unwrap().set(750); - Natural::from(dat.at(3)).unwrap().set(1000); - check(4, dat.length()); - check(250, Natural::from(dat.at(0)).unwrap().get()); - check(500, Natural::from(dat.at(1)).unwrap().get()); - check(750, Natural::from(dat.at(2)).unwrap().get()); - check(1000,Natural::from(dat.at(3)).unwrap().get()); - } - - { - println!("List - Undefined"); - - println!("List - Variable"); - let mut dat = List::new(varying()); - dat.insert(usize::MAX, *Integer::with(15)); - match Type::from(dat.at(0)) { - Type::Integer(value) => { - println!(" = {}", value.get()); - } - _ => { println!(" - Not Integer"); } - } - - println!("List"); - let mut dat = List::new(sequence()); - dat.insert(usize::MAX, *Sequence::with("hello")); - dat.insert(usize::MAX, *Sequence::with("world")); - dat.insert(usize::MAX, *Sequence::with("konnichiwa")); - dat.insert(usize::MAX, *Sequence::with("konbanwa")); - dat.insert(usize::MAX, *Sequence::with("tasogare")); - println!(" = {}", Integer::from(dat.at(0)).unwrap().get()); - } - - { - println!("Sparse"); - - } - - { - println!("Schema"); - let mut sch = Schema::new(); - sch.add(integer()); - sch.map("abc", 0); - check(1, sch.length()); - check(0, sch.index("abc")); - check(sch.length(), sch.index("def")); - } - - { - println!("Record"); - - } -}*/ +pub fn test() { + let schema = Schema::new(); + schema.bind(1); +} #[cfg(test)] mod tests { diff --git a/src/runtime/lib.cc b/src/runtime/lib.cc index b1bb700..aaf6c33 100644 --- a/src/runtime/lib.cc +++ b/src/runtime/lib.cc @@ -3,8 +3,8 @@ TypeTree DB_TYPE; NameTree DB_NAME; -//Pool DB_SCHEMA; -Pool DB_DATA; +SparseList DB_SCHEMA; +Pool DB_REFERENCE; extern "C" size_t type_outer(size_t type_id, size_t key) { @@ -699,7 +699,7 @@ extern "C" size_t schema_index(Reference addr, size_t key) extern "C" size_t schema_bind(Reference addr, size_t id) { - Type::SchemaBinding binding {0}; + /*Type::SchemaBinding binding {0}; auto& object = (*reinterpret_cast(addr.address)); // prepare binding @@ -713,7 +713,10 @@ extern "C" size_t schema_bind(Reference addr, size_t id) binding.size = size + position; } binding.size = ((binding.size + (binding.alignment - 1)) & ~(binding.alignment - 1)); - + */ + + DB_SCHEMA.set(id, true); + // add binding to pool //DB_SCHEMA.add(id, binding); diff --git a/src/runtime/lib.h b/src/runtime/lib.h index 619e9c9..459b6b1 100644 --- a/src/runtime/lib.h +++ b/src/runtime/lib.h @@ -5,7 +5,7 @@ #include "type.h" #include "typetree.h" #include "nametree.h" -//#include "sparselist.h" +#include "sparselist.h" #include "rawlist.h" #include "pool.h" @@ -16,8 +16,8 @@ extern "C" struct Str { extern TypeTree DB_TYPE; extern NameTree DB_NAME; -//extern Pool DB_SCHEMA; -extern Pool DB_DATA; +extern SparseList DB_SCHEMA; +extern Pool DB_REFERENCE; extern "C" size_t type_outer(size_t id, size_t key); extern "C" size_t type_inner(size_t id); diff --git a/src/runtime/sparselist.h b/src/runtime/sparselist.h index aa222b5..e727100 100644 --- a/src/runtime/sparselist.h +++ b/src/runtime/sparselist.h @@ -14,17 +14,82 @@ public: void set(size_t index, const T& value) { + printf("set(%zu) -begin\n", index); + // find first header with start less than index + size_t data_index = 0; + size_t header_index = 0; + for(; header_index < m_headers.size() && m_headers[header_index].start > index; ++header_index) { + data_index += m_headers[header_index].length; + } + // if no header exists, add header starting at index + if(header_index == m_headers.size()) { + Header header {0}; + header.start = index; + header.length = 1; + header.index = m_data.size(); + m_data.push_back(value); + } + // otherwise, update/append existing or insert new header + else { + size_t offset = index - m_headers[header_index].start; + + if(offset < m_headers[header_index].length) { + // update existing item + size_t data_index = m_headers[header_index].index + offset; + m_data[data_index] = value; + } + else if(offset == m_headers[header_index].length) { + // append value to header + m_headers[header_index].length++; + m_data.insert(m_data.begin() + (m_headers[header_index].index + offset), value); + + // combine headers if intersection is created + if(header_index + 1 != m_headers.size()) { + if(m_headers[header_index + 1].start == m_headers[header_index].start + m_headers[header_index].length) { + m_headers[header_index].length += m_headers[header_index + 1].length; + m_headers.erase(m_headers.begin() + (header_index + 1)); + } + } + } + else { + // insert new header after current + Header header {0}; + header.start = index; + header.length = 1; + header.index = m_headers[header_index].index + 1; + m_data.insert(m_data.begin() + header.index, value); + } + } + printf("set() -end\n"); } void unset(size_t index) { + // find first header with start less than index + size_t header_index = 0; + for(; header_index < m_headers.size() && m_headers[header_index].start > index; ++header_index) { } + if(header_index < m_headers.size()) { + auto& header = m_headers[header_index]; + size_t offset = index - header.start; + + if(offset < header.length) { + // remove value from data + + } + } } T& get(size_t index) { + size_t bound_lower = 0; + size_t bound_upper = m_headers.size(); + size_t header_index = bound_lower + ((bound_upper - bound_lower) / 2); + while(bound_lower != bound_upper) { + + } } private: @@ -34,8 +99,9 @@ private: size_t index; }; - std::vector
headers; - std::vector data; + size_t m_root; + std::vector
m_headers; + std::vector m_data; }; #endif diff --git a/src/runtime/type.h b/src/runtime/type.h index d91b88f..61421a4 100644 --- a/src/runtime/type.h +++ b/src/runtime/type.h @@ -1,4 +1,5 @@ #include +#include #include "rawlist.h" extern "C" struct Reference {