add vector
This commit is contained in:
parent
b01343ebfb
commit
67f310f481
@ -1,3 +1,4 @@
|
|||||||
pub mod btree;
|
pub mod btree;
|
||||||
pub mod queue;
|
pub mod queue;
|
||||||
pub mod string;
|
pub mod string;
|
||||||
|
pub mod vector;
|
||||||
|
@ -4,39 +4,31 @@ use crate::{
|
|||||||
transaction, FilePointer, FileRange, RawFilePointer, ReaderTrait, TransactionHandle, U64,
|
transaction, FilePointer, FileRange, RawFilePointer, ReaderTrait, TransactionHandle, U64,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::vector::Vector;
|
||||||
|
|
||||||
#[derive(Clone, Copy, FromBytes, FromZeroes, AsBytes, Unaligned)]
|
#[derive(Clone, Copy, FromBytes, FromZeroes, AsBytes, Unaligned)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct Str {
|
pub struct Str(Vector);
|
||||||
data: FileRange,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Str {
|
impl Str {
|
||||||
fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self(Vector::new())
|
||||||
data: RawFilePointer::null().range(0),
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Str {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Str {
|
impl Str {
|
||||||
pub fn set<R>(self, transaction: &mut TransactionHandle<R>, s: &str) -> Self {
|
pub fn set<R>(self, transaction: &mut TransactionHandle<R>, s: &str) -> Self {
|
||||||
let data = if s.is_empty() {
|
Self(self.0.set(transaction, s.as_bytes()))
|
||||||
Str::new().data
|
|
||||||
} else {
|
|
||||||
let (range, data) = transaction.allocate_range(s.len() as u64);
|
|
||||||
data.copy_from_slice(s.as_bytes());
|
|
||||||
range
|
|
||||||
};
|
|
||||||
|
|
||||||
if self.data.len() != 0 {
|
|
||||||
transaction.free_range(self.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
Str { data }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(self, reader: &impl ReaderTrait) -> &str {
|
pub fn get(self, reader: &impl ReaderTrait) -> &str {
|
||||||
std::str::from_utf8(reader.read_raw(self.data)).unwrap()
|
std::str::from_utf8(self.0.get(reader)).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
60
src/datastructures/vector.rs
Normal file
60
src/datastructures/vector.rs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
use zerocopy::{AsBytes, FromBytes, FromZeroes, Unaligned};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
transaction, FilePointer, FileRange, RawFilePointer, ReaderTrait, TransactionHandle, U64,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, FromBytes, FromZeroes, AsBytes, Unaligned)]
|
||||||
|
#[repr(transparent)]
|
||||||
|
pub struct Vector {
|
||||||
|
data: FileRange,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Vector {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
data: RawFilePointer::null().range(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Vector {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Vector {
|
||||||
|
pub fn set<R>(self, transaction: &mut TransactionHandle<R>, s: &[u8]) -> Self {
|
||||||
|
let data = if s.is_empty() {
|
||||||
|
Vector::new().data
|
||||||
|
} else {
|
||||||
|
let (range, data) = transaction.allocate_range(s.len() as u64);
|
||||||
|
data.copy_from_slice(s.as_bytes());
|
||||||
|
range
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.data.len() != 0 {
|
||||||
|
transaction.free_range(self.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector { data }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(self, reader: &impl ReaderTrait) -> &[u8] {
|
||||||
|
reader.read_raw(self.data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FilePointer<Vector> {
|
||||||
|
pub fn set<R>(self, transaction: &mut TransactionHandle<R>, s: &[u8]) -> FilePointer<Vector> {
|
||||||
|
let new_str = transaction.read::<Vector>(self).set(transaction, s);
|
||||||
|
let (ptr, data) = transaction.modify(self);
|
||||||
|
*data = new_str;
|
||||||
|
ptr
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(self, reader: &impl ReaderTrait) -> &[u8] {
|
||||||
|
reader.read(self).get(reader)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user