Skip to content

Commit

Permalink
former : experimenting
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Mar 23, 2024
1 parent fe787f8 commit 590e5bc
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 115 deletions.
58 changes: 30 additions & 28 deletions module/core/former/src/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ where

//

#[ derive( Debug ) ]
pub struct HashMapDescriptor< K, E >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
Expand All @@ -58,7 +59,7 @@ impl< K, E > HashMapDescriptor< K, E >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
fn new() -> Self
pub fn new() -> Self
{
Self { _phantom : ::core::marker::PhantomData }
}
Expand Down Expand Up @@ -132,8 +133,7 @@ where
Descriptor : FormerDescriptor,
Descriptor::Storage : ContainerAdd< Element = ( K, E ) >,
{
// xxx : rename
formed : ::core::option::Option< Descriptor::Storage >,
storage : ::core::option::Option< Descriptor::Storage >,
context : ::core::option::Option< Context >,
on_end : ::core::option::Option< End >,
_e_phantom : ::core::marker::PhantomData< E >,
Expand All @@ -152,65 +152,67 @@ where

/// Form current former into target structure.
#[ inline( always ) ]
pub fn preform( mut self ) -> Descriptor::Storage
pub fn storage( mut self ) -> Descriptor::Storage
{
let formed = if self.formed.is_some()
// xxx
let storage = if self.storage.is_some()
{
self.formed.take().unwrap()
self.storage.take().unwrap()
}
else
{
let val = Default::default();
val
};
formed
// formed.preform()
storage
// storage.preform()
}
// xxx


/// Make a new HashMapSubformer. It should be called by a context generated for your structure.
/// The context is returned after completion of forming by function `on_end``.
#[ inline( always ) ]
pub fn begin
(
formed : ::core::option::Option< Descriptor::Storage >,
storage : ::core::option::Option< Descriptor::Storage >,
context : ::core::option::Option< Context >,
on_end : End,
)
-> Self
{
Self
{
formed,
storage,
context,
on_end : Some( on_end ),
_e_phantom : ::core::marker::PhantomData,
_k_phantom : ::core::marker::PhantomData,
}
}

/// Return context of your struct moving formed there. Should be called after configuring the formed.
#[ inline( always ) ]
pub fn form( mut self ) -> Descriptor::Formed
{
self.end()
}

/// Return context of your struct moving formed there. Should be called after configuring the formed.
/// Return context of your struct moving formed there. Should be called after forming process.
#[ inline( always ) ]
pub fn end( mut self ) -> Descriptor::Formed
{
let on_end = self.on_end.take().unwrap();
let context = self.context.take();
let storage = self.preform();
let storage = self.storage();
on_end.call( storage, context )
}

/// Set the whole formed instead of setting each element individually.
/// Return context of your struct moving formed there. Should be called after forming process.
#[ inline( always ) ]
pub fn form( self ) -> Descriptor::Formed
{
self.end()
}

/// Set the whole storage instead of setting each element individually.
#[ inline( always ) ]
pub fn replace( mut self, formed : Descriptor::Storage ) -> Self
pub fn replace( mut self, storage : Descriptor::Storage ) -> Self
{
self.formed = Some( formed );
self.storage = Some( storage );
self
}

Expand Down Expand Up @@ -259,20 +261,20 @@ where
/// Returns `self` for chaining further insertions or operations.
///
#[ inline( always ) ]
pub fn insert< K2, E2 >( self, k : K2, e : E2 ) -> Self
pub fn insert< K2, E2 >( mut self, k : K2, e : E2 ) -> Self
where
K2 : ::core::convert::Into< K >,
E2 : ::core::convert::Into< E >,
// Descriptor::Storage : ContainerAdd< Element = ( K, E ) >,
{
if self.formed.is_none()
if self.storage.is_none()
{
self.formed = ::core::option::Option::Some( Default::default() );
self.storage = ::core::option::Option::Some( Default::default() );
}
if let ::core::option::Option::Some( ref mut formed ) = self.formed
if let ::core::option::Option::Some( ref mut storage ) = self.storage
{
ContainerAdd::add( formed, ( k.into(), e.into() ) );
// formed.insert( k.into(), e.into() );
ContainerAdd::add( storage, ( k.into(), e.into() ) );
// storage.insert( k.into(), e.into() );
}
self
}
Expand Down
99 changes: 61 additions & 38 deletions module/core/former/src/hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ where

//

#[ derive( Debug ) ]
pub struct HashSetDescriptor< K >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
Expand All @@ -46,7 +47,7 @@ impl< K > HashSetDescriptor< K >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
fn new() -> Self
pub fn new() -> Self
{
Self { _phantom : ::core::marker::PhantomData }
}
Expand Down Expand Up @@ -106,65 +107,68 @@ where
/// ```
#[ derive( Debug, Default ) ]
pub struct HashSetSubformer< K, Context, End >
pub struct HashSetSubformer< K, Descriptor, Context, End >
where
K : core::cmp::Eq + core::hash::Hash,
// Formed : HashSetLike< K > + core::default::Default,
End : FormingEnd< HashSetDescriptor< K >, Context >,
End : FormingEnd< Descriptor, Context >,
Descriptor : FormerDescriptor,
Descriptor::Storage : ContainerAdd< Element = K >,
{
formed : core::option::Option< < HashSetDescriptor< K > as axiomatic::FormerDescriptor >::Formed >,
// xxx : rename
storage : core::option::Option< Descriptor::Storage >,
context : core::option::Option< Context >,
on_end : core::option::Option< End >,
_e_phantom : core::marker::PhantomData< K >,
}

impl< K, Context, End >
HashSetSubformer< K, Context, End >
impl< K, Descriptor, Context, End >
HashSetSubformer< K, Descriptor, Context, End >
where
K : core::cmp::Eq + core::hash::Hash,
// Formed : HashSetLike< K > + core::default::Default,
End : FormingEnd< HashSetDescriptor< K >, Context >,
End : FormingEnd< Descriptor, Context >,
Descriptor : FormerDescriptor,
Descriptor::Storage : ContainerAdd< Element = K >,
{

/// Form current former into target structure.
#[ inline( always ) ]
pub fn form( mut self ) -> < HashSetDescriptor< K > as axiomatic::FormerDescriptor >::Formed
pub fn storage( mut self ) -> Descriptor::Storage
{
let formed = if self.formed.is_some()
let storage = if self.storage.is_some()
{
self.formed.take().unwrap()
self.storage.take().unwrap()
}
else
{
let val = Default::default();
val
};
formed
storage
}
// xxx

/// Begins the building process with an optional context and formed.
/// Begins the building process with an optional context and storage.
///
/// This method is typically called internally by the builder but can be used directly
/// to initialize the builder with specific contexts or containers.
///
/// # Parameters
/// - `context`: An optional context for the building process.
/// - `formed`: An optional initial formed to populate.
/// - `storage`: An optional initial storage to populate.
/// - `on_end`: A handler to be called at the end of the building process.
///
#[ inline( always ) ]
pub fn begin
(
formed : core::option::Option< < HashSetDescriptor< K > as axiomatic::FormerDescriptor >::Formed >,
storage : core::option::Option< Descriptor::Storage >,
context : core::option::Option< Context >,
on_end : End,
) -> Self
{
Self
{
formed,
storage,
context : context,
on_end : Some( on_end ),
_e_phantom : core::marker::PhantomData,
Expand All @@ -182,30 +186,46 @@ where
/// constructed formed or a context that incorporates the formed.
///
#[ inline( always ) ]
pub fn end( mut self ) -> < HashSetDescriptor< K > as axiomatic::FormerDescriptor >::Formed
pub fn form( self ) -> Descriptor::Formed
{
self.end()
}

/// Finalizes the building process and returns the constructed formed or a context.
///
/// This method concludes the building process by applying the `on_end` handler to transform
/// the formed or incorporate it into a given context. It's typically called at the end
/// of the builder chain to retrieve the final product of the building process.
///
/// # Returns
/// Depending on the `on_end` handler's implementation, this method can return either the
/// constructed formed or a context that incorporates the formed.
///
#[ inline( always ) ]
pub fn end( mut self ) -> Descriptor::Formed
{
let on_end = self.on_end.take().unwrap();
let context = self.context.take();
let formed = self.form();
on_end.call( formed, context )
let storage = self.storage();
on_end.call( storage, context )
}

/// Replaces the current formed with a new one.
/// Replaces the current storage with a new one.
///
/// This method allows for replacing the entire set being built with a different one.
/// It can be useful in scenarios where a pre-populated set needs to be modified or
/// replaced entirely during the building process.
///
/// # Parameters
/// - `formed`: The new formed to use for subsequent builder operations.
/// - `storage`: The new storage to use for subsequent builder operations.
///
/// # Returns
/// The builder instance with the formed replaced, enabling further chained operations.
/// The builder instance with the storage replaced, enabling further chained operations.
///
#[ inline( always ) ]
pub fn replace( mut self, formed : < HashSetDescriptor< K > as axiomatic::FormerDescriptor >::Formed ) -> Self
pub fn replace( mut self, storage : Descriptor::Storage ) -> Self
{
self.formed = Some( formed );
self.storage = Some( storage );
self
}

Expand All @@ -216,18 +236,20 @@ where
// Formed : VectorLike< K > + core::default::Default,
// {

impl< K >
HashSetSubformer< K, (), crate::ReturnStorage >
impl< K, Descriptor >
HashSetSubformer< K, Descriptor, (), crate::ReturnStorage >
where
K : core::cmp::Eq + core::hash::Hash,
Descriptor : FormerDescriptor,
Descriptor::Storage : ContainerAdd< Element = K >,
// Formed : HashSetLike< K > + core::default::Default,
// End : FormingEnd< HashSetDescriptor< K >, Context >,
// End : FormingEnd< Descriptor, Context >,
{

/// Initializes a new instance of the builder with default settings.
///
/// This method provides a starting point for building a `HashSetLike` formed using
/// a fluent interface. It sets up an empty formed ready to be populated.
/// This method provides a starting point for forming a `HashSetLike` using
/// a fluent interface.
///
/// # Returns
/// A new instance of `HashSetSubformer` with no elements.
Expand All @@ -245,18 +267,19 @@ where

}

impl< K, Context, End >
HashSetSubformer< K, Context, End >
impl< K, Descriptor, Context, End >
HashSetSubformer< K, Descriptor, Context, End >
where
K : core::cmp::Eq + core::hash::Hash,
// Formed : HashSetLike< K > + core::default::Default,
End : FormingEnd< HashSetDescriptor< K >, Context >,
End : FormingEnd< Descriptor, Context >,
Descriptor : FormerDescriptor,
Descriptor::Storage : ContainerAdd< Element = K >,
{

/// Inserts an element into the set, possibly replacing an existing element.
///
/// This method ensures that the set contains the given element, and if the element
/// was already present, it might replace it depending on the formed's behavior.
/// was already present, it might replace it depending on the storage's behavior.
///
/// # Parameters
/// - `element`: The element to insert into the set.
Expand All @@ -270,13 +293,13 @@ where
where
E2 : core::convert::Into< K >,
{
if self.formed.is_none()
if self.storage.is_none()
{
self.formed = core::option::Option::Some( Default::default() );
self.storage = core::option::Option::Some( Default::default() );
}
if let core::option::Option::Some( ref mut formed ) = self.formed
if let core::option::Option::Some( ref mut storage ) = self.storage
{
formed.insert( element.into() );
ContainerAdd::add( storage, element.into() );
}
self
}
Expand Down
Loading

0 comments on commit 590e5bc

Please sign in to comment.