former_types/collection/
binary_heap.rs

1//! This module provides a comprehensive approach to applying the builder pattern to `BinaryHeap` collections.
2//!
3//! By leveraging traits such as `Collection`, `CollectionAdd`, `CollectionAssign`, and `CollectionValToEntry`,
4//! this module abstracts the operations on binary heap-like data structures, making them more flexible and easier to integrate as
5//! as subformer, enabling fluid and intuitive manipulation of binary heaps via builder patterns.
6//!
7
8#[ allow( clippy::wildcard_imports ) ]
9use crate::*;
10#[ allow( unused ) ]
11use collection_tools::BinaryHeap;
12
13impl< E > Collection for BinaryHeap< E >
14{
15  type Entry = E;
16  type Val = E;
17
18  #[ inline( always ) ]
19  fn entry_to_val( e : Self::Entry ) -> Self::Val
20  {
21    e
22  }
23
24}
25
26impl< E > CollectionAdd for BinaryHeap< E >
27where
28  E : Ord
29{
30
31  #[ inline( always ) ]
32  fn add( &mut self, e : Self::Entry ) -> bool
33  {
34    self.push( e );
35    true
36  }
37
38}
39
40impl< E > CollectionAssign for BinaryHeap< E >
41where
42  E : Ord
43{
44  #[ inline( always ) ]
45  fn assign< Elements >( &mut self, elements : Elements ) -> usize
46  where
47    Elements : IntoIterator< Item = Self::Entry >
48  {
49    let initial_len = self.len();
50    self.extend( elements );
51    self.len() - initial_len
52  }
53
54}
55
56impl< E > CollectionValToEntry< E > for BinaryHeap< E >
57{
58  type Entry = E;
59  #[ inline( always ) ]
60  fn val_to_entry( val : E ) -> Self::Entry
61  {
62    val
63  }
64}
65
66// = storage
67
68impl< E > Storage
69for BinaryHeap< E >
70where
71  E : Ord
72{
73  type Preformed = BinaryHeap< E >;
74}
75
76impl< E > StoragePreform
77for BinaryHeap< E >
78where
79  E : Ord
80{
81  fn preform( self ) -> Self::Preformed
82  {
83    self
84  }
85}
86
87// = definition
88
89/// Represents the formation definition for a binary heap-like collection within the former framework.
90///
91/// This structure defines the necessary parameters and relationships needed to form a binary heap-like collection,
92/// including its storage, context, the result of the formation process, and the behavior at the end of the formation.
93///
94/// # Type Parameters
95/// - `E`: The element type of the binary heap.
96/// - `Context`: The context needed for the formation, can be provided externally.
97/// - `Formed`: The type formed at the end of the formation process, typically a `BinaryHeap<E>`.
98/// - `End`: A trait determining the behavior at the end of the formation process.
99///
100
101#[ derive( Debug, Default ) ]
102pub struct BinaryHeapDefinition< E, Context, Formed, End >
103where
104  E : Ord,
105  End : FormingEnd< BinaryHeapDefinitionTypes< E, Context, Formed > >,
106{
107  _phantom : core::marker::PhantomData< ( E, Context, Formed, End ) >,
108}
109
110impl< E, Context, Formed, End > FormerDefinition
111for BinaryHeapDefinition< E, Context, Formed, End >
112where
113  E : Ord,
114  End : FormingEnd< BinaryHeapDefinitionTypes< E, Context, Formed > >,
115{
116  type Storage = BinaryHeap< E >;
117  type Context = Context;
118  type Formed = Formed;
119
120  type Types = BinaryHeapDefinitionTypes< E, Context, Formed >;
121  type End = End;
122}
123
124// = definition type
125
126/// Holds the generic parameters for the `BinaryHeapDefinition`.
127///
128/// This struct acts as a companion to `BinaryHeapDefinition`, providing a concrete definition of types used
129/// in the formation process.
130///
131/// # Type Parameters
132///
133/// - `E`: The element type of the binary heap.
134/// - `Context`: The context in which the binary heap is formed.
135/// - `Formed`: The type produced as a result of the formation process.
136
137#[ derive( Debug, Default ) ]
138pub struct BinaryHeapDefinitionTypes< E, Context = (), Formed = BinaryHeap< E > >
139{
140  _phantom : core::marker::PhantomData< ( E, Context, Formed ) >,
141}
142
143impl< E, Context, Formed > FormerDefinitionTypes
144for BinaryHeapDefinitionTypes< E, Context, Formed >
145where
146  E : Ord
147{
148  type Storage = BinaryHeap< E >;
149  type Context = Context;
150  type Formed = Formed;
151}
152
153// = mutator
154
155impl< E, Context, Formed > FormerMutator
156for BinaryHeapDefinitionTypes< E, Context, Formed >
157where
158  E : Ord
159{
160}
161
162// = Entity To
163
164impl< E, Definition > EntityToFormer< Definition >
165for BinaryHeap< E >
166where
167  E : Ord,
168  Definition : FormerDefinition
169  <
170    Storage = BinaryHeap< E >,
171    Types = BinaryHeapDefinitionTypes
172    <
173      E,
174      < Definition as definition::FormerDefinition >::Context,
175      < Definition as definition::FormerDefinition >::Formed,
176    >,
177  >,
178  Definition::End : forming::FormingEnd< Definition::Types >,
179{
180  type Former = BinaryHeapFormer< E, Definition::Context, Definition::Formed, Definition::End >;
181}
182
183impl< E > crate::EntityToStorage
184for BinaryHeap< E >
185{
186  type Storage = BinaryHeap< E >;
187}
188
189impl< E, Context, Formed, End > crate::EntityToDefinition< Context, Formed, End >
190for BinaryHeap< E >
191where
192  E : Ord,
193  End : crate::FormingEnd< BinaryHeapDefinitionTypes< E, Context, Formed > >,
194{
195  type Definition = BinaryHeapDefinition< E, Context, Formed, End >;
196  type Types = BinaryHeapDefinitionTypes< E, Context, Formed >;
197}
198
199impl< E, Context, Formed > crate::EntityToDefinitionTypes< Context, Formed >
200for BinaryHeap< E >
201where
202  E : Ord
203{
204  type Types = BinaryHeapDefinitionTypes< E, Context, Formed >;
205}
206
207// = subformer
208
209/// Provides a streamlined builder interface for constructing binary heap-like collections.
210///
211/// `BinaryHeapFormer` is a type alias that configures the `CollectionFormer` for use specifically with binary heaps.
212/// It integrates the `BinaryHeapDefinition` to facilitate the fluent and dynamic construction of binary heaps, leveraging
213/// predefined settings to reduce boilerplate code. This approach enhances readability and simplifies the use of
214/// binary heaps in custom data structures where builder patterns are desired.
215///
216/// The alias encapsulates complex generic parameters, making the construction process more accessible and maintainable.
217/// It is particularly useful in scenarios where binary heaps are repeatedly used or configured in similar ways across different
218/// parts of an application.
219///
220pub type BinaryHeapFormer< E, Context, Formed, End > =
221CollectionFormer::< E, BinaryHeapDefinition< E, Context, Formed, End > >;
222
223// = extension
224
225/// Provides an extension method for binary heaps to facilitate the use of the builder pattern.
226///
227/// This trait extends the `BinaryHeap` type, enabling it to use the `BinaryHeapFormer` interface directly.
228/// This allows for fluent, expressive construction and manipulation of binary heaps, integrating seamlessly
229/// with the builder pattern provided by the `former` framework. It's a convenience trait that simplifies
230/// creating configured binary heap builders with default settings.
231///
232pub trait BinaryHeapExt< E > : sealed::Sealed
233where
234  E : Ord
235{
236  /// Initializes a builder pattern for `BinaryHeap` using a default `BinaryHeapFormer`.
237  fn former() -> BinaryHeapFormer< E, (), BinaryHeap< E >, ReturnStorage >;
238}
239
240impl< E > BinaryHeapExt< E > for BinaryHeap< E >
241where
242  E : Ord
243{
244  #[ allow( clippy::default_constructed_unit_structs ) ]
245  fn former() -> BinaryHeapFormer< E, (), BinaryHeap< E >, ReturnStorage >
246  {
247    BinaryHeapFormer::< E, (), BinaryHeap< E >, ReturnStorage >::new( ReturnStorage::default() )
248  }
249}
250
251mod sealed
252{
253  pub trait Sealed {}
254  impl< E > Sealed for super::BinaryHeap< E > {}
255}