Update README, add sparse list utility, update implementation of schema.
This commit is contained in:
parent
b24c3a5c62
commit
2cc6263a47
@ -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<u8>`
|
||||
`encode(refer:Reference) -> Vec<u8>`
|
||||
Serializes an object into binary encoding.
|
||||
|
||||
> Not implemented
|
||||
---
|
||||
|
||||
`decode(data:Vec<u8>) -> Reference`
|
||||
`decode(data:Vec<u8>) -> Result<Reference,()>`
|
||||
Parses a valid binary encoding and produces the represented object.
|
||||
|
||||
> Not implemented
|
||||
---
|
||||
|
@ -1,4 +1,4 @@
|
||||
fn main()
|
||||
{
|
||||
|
||||
szun::test();
|
||||
}
|
||||
|
@ -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)}
|
||||
}
|
||||
|
@ -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)}
|
||||
}
|
||||
|
110
src/lib.rs
110
src/lib.rs
@ -11,115 +11,11 @@ fn check<T>(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());
|
||||
pub fn test() {
|
||||
let schema = Schema::new();
|
||||
schema.bind(1);
|
||||
}
|
||||
|
||||
{
|
||||
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<Natural>");
|
||||
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<Sequence>");
|
||||
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");
|
||||
|
||||
}
|
||||
}*/
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
|
@ -3,8 +3,8 @@
|
||||
|
||||
TypeTree DB_TYPE;
|
||||
NameTree DB_NAME;
|
||||
//Pool<Type::Schema> DB_SCHEMA;
|
||||
Pool<Reference> DB_DATA;
|
||||
SparseList<bool> DB_SCHEMA;
|
||||
Pool<Reference> 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<Type::Schema*>(addr.address));
|
||||
|
||||
// prepare binding
|
||||
@ -713,6 +713,9 @@ 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);
|
||||
|
@ -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<Type::Schema> DB_SCHEMA;
|
||||
extern Pool<Reference> DB_DATA;
|
||||
extern SparseList<bool> DB_SCHEMA;
|
||||
extern Pool<Reference> DB_REFERENCE;
|
||||
|
||||
extern "C" size_t type_outer(size_t id, size_t key);
|
||||
extern "C" size_t type_inner(size_t id);
|
||||
|
@ -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<Header> headers;
|
||||
std::vector<T> data;
|
||||
size_t m_root;
|
||||
std::vector<Header> m_headers;
|
||||
std::vector<T> m_data;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include "rawlist.h"
|
||||
|
||||
extern "C" struct Reference {
|
||||
|
Reference in New Issue
Block a user