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
//! This module provides a comprehensive approach to applying the builder pattern to `BTreeSet` collections.
//!
//! By leveraging traits such as `Collection`, `CollectionAdd`, `CollectionAssign`, and `CollectionValToEntry`,
//! this module abstracts the operations on binary tree set-like data structures, making them more flexible and easier to integrate as
//! as subformer, enabling fluid and intuitive manipulation of binary tree sets via builder patterns.
//!

use crate::*;
#[ allow( unused ) ]
use collection_tools::BTreeSet;

impl< E > Collection for BTreeSet< E >
{
  type Entry = E;
  type Val = E;

  #[ inline( always ) ]
  fn entry_to_val( e : Self::Entry ) -> Self::Val
  {
    e
  }

}

impl< E > CollectionAdd for BTreeSet< E >
where
  E : Ord
{

  #[ inline( always ) ]
  fn add( &mut self, e : Self::Entry ) -> bool
  {
    self.insert( e );
    true
  }

}

impl< E > CollectionAssign for BTreeSet< E >
where
  E : Ord
{
  #[ inline( always ) ]
  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< E > CollectionValToEntry< E > for BTreeSet< E >
where
{
  type Entry = E;
  #[ inline( always ) ]
  fn val_to_entry( val : E ) -> Self::Entry
  {
    val
  }
}

// = storage

impl< E > Storage
for BTreeSet< E >
{
  type Preformed = BTreeSet< E >;
}

impl< E > StoragePreform
for BTreeSet< E >
{
  fn preform( self ) -> Self::Preformed
  {
    self
  }
}

// = definition

/// Represents the formation definition for a binary tree set-like collection within the former framework.
///
/// This structure defines the necessary parameters and relationships needed to form a binary tree set-like collection,
/// including its storage, context, the result of the formation process, and the behavior at the end of the formation.
///
/// # Type Parameters
/// - `E`: The element type of the binary tree set.
/// - `Context`: The context needed for the formation, can be provided externally.
/// - `Formed`: The type formed at the end of the formation process, typically a `BTreeSet<E>`.
/// - `End`: A trait determining the behavior at the end of the formation process.
///

#[ derive( Debug, Default ) ]
pub struct BTreeSetDefinition< E, Context, Formed, End >
where
  End : FormingEnd< BTreeSetDefinitionTypes< E, Context, Formed > >,
{
  _phantom : core::marker::PhantomData< ( E, Context, Formed, End ) >,
}

impl< E, Context, Formed, End > FormerDefinition
for BTreeSetDefinition< E, Context, Formed, End >
where
  End : FormingEnd< BTreeSetDefinitionTypes< E, Context, Formed > >,
{
  type Storage = BTreeSet< E >;
  type Context = Context;
  type Formed = Formed;

  type Types = BTreeSetDefinitionTypes< E, Context, Formed >;
  type End = End;
}

// = definition type

/// Holds the generic parameters for the `BTreeSetDefinition`.
///
/// This struct acts as a companion to `BTreeSetDefinition`, providing a concrete definition of types used
/// in the formation process. It is crucial for linking the type parameters with the operational mechanics
/// of the formation and ensuring type safety and correctness throughout the formation lifecycle.
///
/// # Type Parameters
///
/// - `E`: The element type of the binary tree set.
/// - `Context`: The context in which the binary tree set is formed.
/// - `Formed`: The type produced as a result of the formation process.

#[ derive( Debug, Default ) ]
pub struct BTreeSetDefinitionTypes< E, Context = (), Formed = BTreeSet< E > >
{
  _phantom : core::marker::PhantomData< ( E, Context, Formed ) >,
}

impl< E, Context, Formed > FormerDefinitionTypes
for BTreeSetDefinitionTypes< E, Context, Formed >
{
  type Storage = BTreeSet< E >;
  type Context = Context;
  type Formed = Formed;
}

// = mutator

impl< E, Context, Formed > FormerMutator
for BTreeSetDefinitionTypes< E, Context, Formed >
{
}

// = Entity To

impl< E, Definition > EntityToFormer< Definition >
for BTreeSet< E >
where
  E : Ord,
  Definition : FormerDefinition
  <
    Storage = BTreeSet< E >,
    Types = BTreeSetDefinitionTypes
    <
      E,
      < Definition as definition::FormerDefinition >::Context,
      < Definition as definition::FormerDefinition >::Formed,
    >,
  >,
  Definition::End : forming::FormingEnd< Definition::Types >,
{
  type Former = BTreeSetFormer< E, Definition::Context, Definition::Formed, Definition::End >;
}

impl< E > crate::EntityToStorage
for BTreeSet< E >
{
  type Storage = BTreeSet< E >;
}

impl< E, Context, Formed, End > crate::EntityToDefinition< Context, Formed, End >
for BTreeSet< E >
where
  End : crate::FormingEnd< BTreeSetDefinitionTypes< E, Context, Formed > >,
{
  type Definition = BTreeSetDefinition< E, Context, Formed, End >;
  type Types = BTreeSetDefinitionTypes< E, Context, Formed >;
}

impl< E, Context, Formed > crate::EntityToDefinitionTypes< Context, Formed >
for BTreeSet< E >
{
  type Types = BTreeSetDefinitionTypes< E, Context, Formed >;
}

// = subformer

/// Provides a streamlined builder interface for constructing binary tree set-like collections.
///
/// `BTreeSetFormer` is a type alias that configures the `CollectionFormer` for use specifically with binary tree sets.
/// It integrates the `BTreeSetDefinition` to facilitate the fluent and dynamic construction of binary tree sets, leveraging
/// predefined settings to reduce boilerplate code. This approach enhances readability and simplifies the use of
/// binary tree sets in custom data structures where builder patterns are desired.
///
/// The alias encapsulates complex generic parameters, making the construction process more accessible and maintainable.
/// It is particularly useful in scenarios where binary tree sets are repeatedly used or configured in similar ways across different
/// parts of an application.
///

pub type BTreeSetFormer< E, Context, Formed, End > =
CollectionFormer::< E, BTreeSetDefinition< E, Context, Formed, End > >;

// = extension

/// Provides an extension method for binary tree sets to facilitate the use of the builder pattern.
///
/// This trait extends the `BTreeSet` type, enabling it to use the `BTreeSetFormer` interface directly.
/// This allows for fluent, expressive construction and manipulation of binary tree sets, integrating seamlessly
/// with the builder pattern provided by the `former` framework. It's a convenience trait that simplifies
/// creating configured binary tree set builders with default settings.
///
pub trait BTreeSetExt< E > : sealed::Sealed
where
  E : Ord
{
  /// Initializes a builder pattern for `BTreeSet` using a default `BTreeSetFormer`.
  fn former() -> BTreeSetFormer< E, (), BTreeSet< E >, ReturnStorage >;
}

impl< E > BTreeSetExt< E > for BTreeSet< E >
where
  E : Ord
{
  fn former() -> BTreeSetFormer< E, (), BTreeSet< E >, ReturnStorage >
  {
    BTreeSetFormer::< E, (), BTreeSet< E >, ReturnStorage >::new( ReturnStorage::default() )
  }
}

mod sealed
{
  pub trait Sealed {}
  impl< E > Sealed for super::BTreeSet< E > {}
}