Skip to content

Commit

Permalink
add idvec / idvecafter in core
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenthz committed Dec 18, 2023
1 parent 64fbb77 commit 21fd402
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 121 deletions.
120 changes: 1 addition & 119 deletions werbolg-compile/src/symbols.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use alloc::vec::Vec;
use core::hash::Hash;
use core::marker::PhantomData;
use hashbrown::HashMap;
use werbolg_core::id::IdF;
pub use werbolg_core::idvec::{IdVec, IdVecAfter};
use werbolg_core::Ident;

pub struct SymbolsTable<ID: IdF> {
Expand Down Expand Up @@ -31,124 +31,6 @@ impl<ID: IdF> SymbolsTable<ID> {
}
}

pub struct IdVec<ID, T> {
vec: Vec<T>,
phantom: PhantomData<ID>,
}

impl<ID: IdF, T> core::ops::Index<ID> for IdVec<ID, T> {
type Output = T;

fn index(&self, index: ID) -> &Self::Output {
&self.vec[index.as_index()]
}
}

impl<ID: IdF, T> core::ops::IndexMut<ID> for IdVec<ID, T> {
fn index_mut(&mut self, index: ID) -> &mut T {
&mut self.vec[index.as_index()]
}
}

impl<ID: IdF, T> IdVec<ID, T> {
pub fn new() -> Self {
Self {
vec: Vec::new(),
phantom: PhantomData,
}
}

pub fn get(&self, id: ID) -> Option<&T> {
let idx = id.as_index();
if self.vec.len() > idx {
Some(&self.vec[idx])
} else {
None
}
}

pub fn next_id(&self) -> ID {
ID::from_slice_len(&self.vec)
}

pub fn push(&mut self, v: T) -> ID {
let id = ID::from_slice_len(&self.vec);
self.vec.push(v);
id
}

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
self.vec.iter_mut()
}

pub fn iter(&self) -> impl Iterator<Item = (ID, &T)> {
self.vec
.iter()
.enumerate()
.map(|(i, t)| (ID::from_collection_len(i), t))
}

pub fn into_iter(self) -> impl Iterator<Item = (ID, T)> {
self.vec
.into_iter()
.enumerate()
.map(|(i, t)| (ID::from_collection_len(i), t))
}

pub fn concat(&mut self, after: &mut IdVecAfter<ID, T>) {
assert!(self.vec.len() == after.ofs.as_index());
self.vec.append(&mut after.id_vec.vec)
}

pub fn remap<F, U>(self, f: F) -> IdVec<ID, U>
where
F: Fn(T) -> U,
{
let mut new = IdVec::<ID, U>::new();
for (id, t) in self.into_iter() {
let new_id = new.push(f(t));
assert_eq!(new_id, id);
}
new
}
}

pub struct IdVecAfter<ID, T> {
id_vec: IdVec<ID, T>,
ofs: ID,
}

impl<ID: IdF, T> IdVecAfter<ID, T> {
pub fn new(first_id: ID) -> Self {
Self {
id_vec: IdVec::new(),
ofs: first_id,
}
}

pub fn from_idvec(id_vec: IdVec<ID, T>, first_id: ID) -> Self {
Self {
id_vec,
ofs: first_id,
}
}

pub fn push(&mut self, v: T) -> ID {
let id = self.id_vec.push(v);
let new_id = ID::remap(id, self.ofs);
new_id
}

pub fn remap<F>(&mut self, f: F)
where
F: Fn(&mut T) -> (),
{
for elem in self.id_vec.iter_mut() {
f(elem)
}
}
}

pub struct SymbolsTableData<ID: IdF, T> {
pub table: SymbolsTable<ID>,
pub vecdata: IdVec<ID, T>,
Expand Down
41 changes: 41 additions & 0 deletions werbolg-core/src/symbols.rs → werbolg-core/src/idvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ impl<ID: IdF, T> IdVec<ID, T> {
.map(|(i, t)| (ID::from_collection_len(i), t))
}

pub fn concat(&mut self, after: &mut IdVecAfter<ID, T>) {
assert!(self.vec.len() == after.ofs.as_index());
self.vec.append(&mut after.id_vec.vec)
}

pub fn remap<F, U>(self, f: F) -> IdVec<ID, U>
where
F: Fn(T) -> U,
Expand All @@ -78,3 +83,39 @@ impl<ID: IdF, T> IdVec<ID, T> {
new
}
}

pub struct IdVecAfter<ID, T> {
id_vec: IdVec<ID, T>,
ofs: ID,
}

impl<ID: IdF, T> IdVecAfter<ID, T> {
pub fn new(first_id: ID) -> Self {
Self {
id_vec: IdVec::new(),
ofs: first_id,
}
}

pub fn from_idvec(id_vec: IdVec<ID, T>, first_id: ID) -> Self {
Self {
id_vec,
ofs: first_id,
}
}

pub fn push(&mut self, v: T) -> ID {
let id = self.id_vec.push(v);
let new_id = ID::remap(id, self.ofs);
new_id
}

pub fn remap<F>(&mut self, f: F)
where
F: Fn(&mut T) -> (),
{
for elem in self.id_vec.iter_mut() {
f(elem)
}
}
}
2 changes: 1 addition & 1 deletion werbolg-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ extern crate alloc;

mod basic;
pub mod id;
pub mod idvec;
mod ir;
mod location;
pub mod symbols;

pub use basic::*;
pub use id::{ConstrId, FunId, GlobalId, LitId, NifId, ValueFun};
Expand Down
2 changes: 1 addition & 1 deletion werbolg-interpret/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern crate alloc;

use werbolg_core as ir;
use werbolg_core::{symbols::IdVec, FunDef, FunId, NifId, ValueFun};
use werbolg_core::{idvec::IdVec, FunDef, FunId, NifId, ValueFun};

mod bindings;
mod location;
Expand Down

0 comments on commit 21fd402

Please sign in to comment.