former_types/collection/
hash_set.rs

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