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