Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: FL03 <jo3mccain@icloud.com>
  • Loading branch information
FL03 committed Sep 7, 2024
1 parent 94c3344 commit 43c7947
Show file tree
Hide file tree
Showing 18 changed files with 375 additions and 84 deletions.
17 changes: 8 additions & 9 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub use self::{
error::Error,
rules::{Rule, Ruleset},
state::State,
tape::StdTape,
traits::prelude::*,
types::prelude::*,
};
Expand All @@ -48,18 +47,18 @@ pub(crate) mod seal;

pub mod actors;
pub mod error;
pub mod mem;
pub mod rules;
pub mod state;
pub mod tape;
pub mod traits;
pub mod types;

pub mod prelude {
pub use crate::actors::prelude::*;
pub use crate::error::Error;
pub use crate::rules::prelude::*;
pub use crate::state::prelude::*;
pub use crate::tape::prelude::*;
pub use crate::traits::prelude::*;
pub use crate::types::prelude::*;
pub use super::actors::prelude::*;
pub use super::error::Error;
pub use super::mem::prelude::*;
pub use super::rules::prelude::*;
pub use super::state::prelude::*;
pub use super::traits::prelude::*;
pub use super::types::prelude::*;
}
16 changes: 16 additions & 0 deletions core/src/mem/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Appellation: mem <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
//! # Memory (mem)
//!
//!
#[doc(inline)]
pub use self::store::*;

pub mod store;
pub mod tape;

pub(crate) mod prelude {
pub use super::tape::prelude::*;
}
209 changes: 209 additions & 0 deletions core/src/mem/store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
Appellation: store <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/

/// [RawMemory] is a trait that provides a common interface for memory storage.
pub trait RawMemory {
type Elem;

private!();

fn is_empty(&self) -> bool {
self.len() == 0
}

fn len(&self) -> usize;

fn to_vec(&self) -> Vec<Self::Elem>
where
Self::Elem: Clone;
}

/// [Sequential] extends the base trait [RawMemory] to provide sequential access to memory.
pub trait Sequential: RawMemory {
fn as_ptr(&self) -> *const Self::Elem;

fn as_mut_ptr(&mut self) -> *mut Self::Elem;

fn as_slice(&self) -> &[Self::Elem];

fn as_mut_slice(&mut self) -> &mut [Self::Elem];
}

pub trait Memory: RawMemory {
type Key;

fn get(&self, index: &Self::Key) -> Option<&Self::Elem>;

fn get_mut(&mut self, index: &Self::Key) -> Option<&mut Self::Elem>;
}

pub trait MemoryMut: Memory {
fn clear(&mut self);

fn insert(&mut self, index: usize, elem: Self::Elem);

fn remove(&mut self, index: usize) -> Option<Self::Elem>;
}

/*
************* Implementations *************
*/
impl<T> RawMemory for [T] {
type Elem = T;

seal!();

fn is_empty(&self) -> bool {
<[T]>::is_empty(self)
}

fn len(&self) -> usize {
<[T]>::len(self)
}

fn to_vec(&self) -> Vec<T>
where
T: Clone,
{
<[T]>::to_vec(self)
}
}

impl<T> Memory for [T] {
type Key = usize;

fn get(&self, index: &usize) -> Option<&T> {
<[T]>::get(self, *index)
}

fn get_mut(&mut self, index: &usize) -> Option<&mut T> {
<[T]>::get_mut(self, *index)
}
}

impl<T> Sequential for [T] {
fn as_ptr(&self) -> *const T {
<[T]>::as_ptr(self)
}

fn as_mut_ptr(&mut self) -> *mut T {
<[T]>::as_mut_ptr(self)
}

fn as_slice(&self) -> &[T] {
&self

Check warning

Code scanning / clippy

this expression creates a reference which is immediately dereferenced by the compiler Warning

this expression creates a reference which is immediately dereferenced by the compiler
}

fn as_mut_slice(&mut self) -> &mut [T] {
self
}
}

#[cfg(feature = "alloc")]
mod impl_alloc {
use super::{Memory, RawMemory, Sequential};
use alloc::vec::Vec;

impl<T> RawMemory for Vec<T> {
type Elem = T;

seal!();

fn is_empty(&self) -> bool {
Vec::is_empty(self)
}

fn len(&self) -> usize {
Vec::len(self)
}

fn to_vec(&self) -> Vec<T>
where
T: Clone,
{
self.clone()
}
}

impl<T> Memory for Vec<T> {
type Key = usize;

fn get(&self, index: &usize) -> Option<&T> {
if *index < self.len() {
Some(&self[*index])
} else {
None
}
}

fn get_mut(&mut self, index: &usize) -> Option<&mut T> {
if *index < self.len() {
Some(&mut self[*index])
} else {
None
}
}
}

impl<T> Sequential for Vec<T> {
fn as_ptr(&self) -> *const T {
Vec::as_ptr(self)
}

fn as_mut_ptr(&mut self) -> *mut T {
Vec::as_mut_ptr(self)
}

fn as_slice(&self) -> &[T] {
Vec::as_slice(self)
}

fn as_mut_slice(&mut self) -> &mut [T] {
Vec::as_mut_slice(self)
}
}
}

#[cfg(feature = "std")]
mod impl_std {
use super::{Memory, RawMemory};
use std::collections::HashMap;

impl<K, V> RawMemory for HashMap<K, V> {
type Elem = V;

seal!();

fn is_empty(&self) -> bool {
HashMap::is_empty(self)
}

fn len(&self) -> usize {
HashMap::len(self)
}

fn to_vec(&self) -> Vec<V>
where
V: Clone,
{
self.values().cloned().collect()
}
}

impl<K, V> Memory for HashMap<K, V>
where
K: Eq + core::hash::Hash,
{
type Key = K;

fn get(&self, index: &K) -> Option<&V> {
HashMap::get(self, index)
}

fn get_mut(&mut self, index: &K) -> Option<&mut V> {
HashMap::get_mut(self, index)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Appellation: hash_tape <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
#![cfg(feature = "std")]

use crate::Direction;
use std::collections::hash_map::{self, HashMap};

Expand Down
45 changes: 2 additions & 43 deletions core/src/tape/mod.rs → core/src/mem/tape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
//! Idealized Turing machines consider a tape, or memory, that is infinite in both directions.
//! This tape is a one-dimensional array of symbols manipulated by the tape head according to
//! some set of pre-defined rules.
#[doc(inline)]
pub use self::tape::StdTape;

pub(crate) mod tape;

Check warning

Code scanning / clippy

module has the same name as its containing module Warning

module has the same name as its containing module

#[cfg(feature = "std")]
pub mod hash_tape;

pub(crate) mod prelude {
Expand All @@ -19,19 +21,6 @@ pub(crate) mod prelude {

use core::option::Option;

#[doc(hidden)]
pub trait RawIndex {
private!();
}

pub trait Index: RawIndex {
fn increment(self) -> Self;

fn decrement(self) -> Self;
}

pub trait HashIndex: Index + core::cmp::Eq + core::hash::Hash {}

#[doc(hidden)]
pub trait RawTape {
type Elem;
Expand Down Expand Up @@ -145,33 +134,3 @@ where
HashMap::len(self)
}
}

macro_rules! impl_index {
(@impl $T:ty) => {
impl RawIndex for $T {
seal!();
}
};
($($T:ty),* $(,)?) => {
$(
impl_index!(@impl $T);
)*
};
}

impl_index!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usize);

impl<T> Index for T
where
T: RawIndex + core::ops::Add<Output = T> + core::ops::Sub<Output = T> + num::One,
{
fn increment(self) -> Self {
self + T::one()
}

fn decrement(self) -> Self {
self - T::one()
}
}

impl<T> HashIndex for T where T: Index + core::cmp::Eq + core::hash::Hash {}
File renamed without changes.
32 changes: 32 additions & 0 deletions core/src/traits/container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Appellation: container <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/

pub trait RawContainer {
type Elem;
}

/*
************* Implementations *************
*/

impl<T> RawContainer for Vec<T> {
type Elem = T;
}

impl<T> RawContainer for Box<[T]> {
type Elem = T;
}

impl<T> RawContainer for [T] {
type Elem = T;
}

impl<T> RawContainer for &mut [T] {
type Elem = T;
}

impl<const N: usize, T> RawContainer for &mut [T; N] {
type Elem = T;
}
Loading

0 comments on commit 43c7947

Please sign in to comment.