1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
//! This module provides a builder pattern implementation (`HashSetFormer`) for `HashSet`-like collections. It is designed to extend the builder pattern, allowing for fluent and dynamic construction of sets within custom data structures.
use crate::*;
use collection_tools::HashSet;
impl< K > Collection for HashSet< K >
where
K : core::cmp::Eq + core::hash::Hash,
{
type Entry = K;
type Val = K;
#[ inline( always ) ]
fn entry_to_val( e : Self::Entry ) -> Self::Val
{
e
}
}
impl< K > CollectionAdd for HashSet< K >
where
K : core::cmp::Eq + core::hash::Hash,
{
// type Entry = K;
// type Val = K;
#[ inline( always ) ]
fn add( &mut self, e : Self::Entry ) -> bool
{
self.insert( e )
}
}
impl< K > CollectionAssign for HashSet< K >
where
K : core::cmp::Eq + core::hash::Hash,
{
// type Entry = K;
fn assign< Elements >( &mut self, elements : Elements ) -> usize
where
Elements : IntoIterator< Item = Self::Entry >
{
let initial_len = self.len();
self.extend( elements );
self.len() - initial_len
}
}
impl< K > CollectionValToEntry< K > for HashSet< K >
where
K : core::cmp::Eq + core::hash::Hash,
{
type Entry = K;
#[ inline( always ) ]
fn val_to_entry( val : K ) -> Self::Entry
{
val
}
}
// /// A trait for collections behaving like a `HashSet`, allowing insertion operations.
// ///
// /// Implementing this trait enables the associated formed to be used with `HashSetFormer`,
// /// facilitating a builder pattern that is both intuitive and concise.
// ///
// /// # Example Implementation
// ///
// /// Implementing `HashSetLike` for `std::collections::HashSet`:
// ///
//
// pub trait HashSetLike< K >
// where
// K : core::cmp::Eq + core::hash::Hash,
// {
// /// Inserts a key-value pair into the map.
// fn insert( &mut self, element : K ) -> Option< K >;
// }
//
// // impl< K > HashSetLike< K > for HashSet< K >
// // where
// // K : core::cmp::Eq + core::hash::Hash,
// // {
// // fn insert( &mut self, element : K ) -> Option< K >
// // {
// // HashSet::replace( self, element )
// // }
// // }
// = storage
impl< K > Storage
for HashSet< K >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
// type Formed = HashSet< K >;
type Preformed = HashSet< K >;
}
impl< K > StoragePreform
for HashSet< K >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
// type Preformed = HashSet< K >;
fn preform( self ) -> Self::Preformed
{
self
}
}
// = definition
/// Represents the formation definition for a hash set-like collection within the former framework.
///
/// This structure defines the essential elements required to form a hash set-like collection, detailing
/// the type of elements, the contextual environment during formation, the final formed type, and the
/// behavior at the end of the formation process. It is designed to support the construction and configuration
/// of hash set collections with dynamic characteristics and behaviors.
///
/// # Type Parameters
/// - `K`: The type of elements in the hash set.
/// - `Context`: The optional context provided during the formation process.
/// - `Formed`: The type of the entity produced, typically a `HashSet<K>`.
/// - `End`: A trait defining the end behavior of the formation process, managing how the hash set is finalized.
///
#[ derive( Debug, Default ) ]
pub struct HashSetDefinition< K, Context = (), Formed = HashSet< K >, End = ReturnStorage >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
End : FormingEnd< HashSetDefinitionTypes< K, Context, Formed > >,
{
_phantom : core::marker::PhantomData< ( K, Context, Formed, End ) >,
}
impl< K, Context, Formed, End > FormerDefinition
for HashSetDefinition< K, Context, Formed, End >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
End : FormingEnd< HashSetDefinitionTypes< K, Context, Formed > >,
{
type Storage = HashSet< K >;
type Formed = Formed;
type Context = Context;
type Types = HashSetDefinitionTypes< K, Context, Formed >;
type End = End;
}
// = definition types
/// Holds the generic parameters for the `HashSetDefinition`.
///
/// This struct encapsulates the type relationships and characteristics essential for the formation process
/// of a `HashSet`, including the storage type, the context, and the type ultimately formed. It ensures that
/// these elements are congruent and coherent throughout the lifecycle of the hash set formation.
///
#[ derive( Debug, Default ) ]
pub struct HashSetDefinitionTypes< K, Context = (), Formed = HashSet< K > >
{
_phantom : core::marker::PhantomData< ( K, Context, Formed ) >,
}
impl< K, Context, Formed > FormerDefinitionTypes
for HashSetDefinitionTypes< K, Context, Formed >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
type Storage = HashSet< K >;
type Formed = Formed;
type Context = Context;
}
// = mutator
impl< K, Context, Formed > FormerMutator
for HashSetDefinitionTypes< K, Context, Formed >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
}
// = entity to
impl< K, Definition > EntityToFormer< Definition > for HashSet< K >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
Definition : FormerDefinition
<
Storage = HashSet< K >,
Types = HashSetDefinitionTypes
<
K,
< Definition as definition::FormerDefinition >::Context,
< Definition as definition::FormerDefinition >::Formed,
>,
>,
Definition::End : forming::FormingEnd< Definition::Types >,
{
type Former = HashSetFormer< K, Definition::Context, Definition::Formed, Definition::End >;
}
impl< K > crate::EntityToStorage
for HashSet< K >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
type Storage = HashSet< K >;
}
impl< K, Context, Formed, End > crate::EntityToDefinition< Context, Formed, End >
for HashSet< K >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
End : crate::FormingEnd< HashSetDefinitionTypes< K, Context, Formed > >,
{
type Definition = HashSetDefinition< K, Context, Formed, End >;
type Types = HashSetDefinitionTypes< K, Context, Formed >;
}
impl< K, Context, Formed > crate::EntityToDefinitionTypes< Context, Formed >
for HashSet< K >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
type Types = HashSetDefinitionTypes< K, Context, Formed >;
}
// = subformer
/// Provides a concise alias for `CollectionFormer` configured specifically for `HashSet`-like collections.
///
/// `HashSetFormer` simplifies the creation of `HashSet` collections within builder patterns by leveraging
/// the `CollectionFormer` with predefined settings. This approach minimizes boilerplate code and enhances
/// readability, making it ideal for fluent and expressive construction of set collections within custom data structures.
///
pub type HashSetFormer< K, Context, Formed, End > =
CollectionFormer::< K, HashSetDefinition< K, Context, Formed, End > >;
// = extension
/// Provides an extension method for `HashSet` to facilitate the use of the builder pattern.
///
/// This trait extends `HashSet`, enabling direct use of the `HashSetFormer` interface for fluent and expressive
/// set construction. It simplifies the process of building `HashSet` instances by providing a straightforward
/// way to start the builder pattern with default context and termination behavior.
///
pub trait HashSetExt< K > : sealed::Sealed
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
/// Initializes a builder pattern for `HashSet` using a default `HashSetFormer`.
fn former() -> HashSetFormer< K, (), HashSet< K >, ReturnStorage >;
}
impl< K > HashSetExt< K > for HashSet< K >
where
K : ::core::cmp::Eq + ::core::hash::Hash,
{
fn former() -> HashSetFormer< K, (), HashSet< K >, ReturnStorage >
{
HashSetFormer::< K, (), HashSet< K >, ReturnStorage >::new( ReturnStorage::default() )
}
}
mod sealed
{
use super::HashSet;
pub trait Sealed {}
impl< K > Sealed for HashSet< K > {}
}