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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! # Overview
//! The **common-range-tools** crate, is a library that, can be used to find all common intersections for ranges of generic types. It interoperates with the built in range types for rust via the [std::ops::RangeBounds] trait. When working with primitive numbers, the increment and decrementing of values are checked (see [working with floats](#working-with-floats), for the exception). Support for all primitive number types in rust are implemented via the [NumberIncDecCpCmp] object.
//!
//! For dealing with ranges beyond just intersections of
//! numbers see: [Generic Data Types](#generic-data-types). For working with custom data structures see: [Beyond Generics](#beyond-generics).
//! For working with custom Ranges and range factories see: [Internal Range Trait](#internal-range-trait).
//! For consolidating duplicate and overlapping ranges see [Consolidation of ranges](#consolidation-of-ranges).
//! For finding intersections between muliple [Iterator] instances, see: [Intersections of multiple Itertors](#intersections-of-multiple-itertors).
//!
//! ## Example
//! This is the most basic example, using the default values from [NumberIncDecCpCmp]. The [OverlapIter] is a [DoubleEndedIterator] and can be reversed.
//!
//!
//! ## Any Range Example
//! This example converts [std::ops::RangeBounds] instances to [std::ops::RangeInclusive].
//! The bounds to the left or right of .. represent the ($ty::MIN)..($ty::MAX), defined by the [NumberIncDecCpCmp] object.
//! The min and max numbers can be changed, but this example uses the defaults.
//!
//! ## Numeric Boundries
//! When working with boundries it is useful to be able to control how ranges are interpeted. The defaults provided by [NumberIncDecCpCmp] are useful
//! but do not cover all casees. The internals of this [crate] allow for setting various options to control how both ranges and intersections are computed.
//!
//! In this example we set the following:
//! | Field | What it does|
//! |------|----|
//! | step | sets the value used to progress to the next begin or end value for a given range |
//! | rebound | sets the value used to redefine a range value fom an instance of: [std::ops::Bound::Excluded] |
//! | min | the minimum value for ranges in the context of: **..** |
//! | max | the maximum vaue for ranges in the context of: **..** |
//!
//! ## Working with Floats
//! When working with floating points, it's nessesary to understand how floats are handled by the [NumberIncDecCpCmp].
//! Floating point numbers are in a word *imprecise*; The internals of [NumberIncDecCpCmp] does not check [f32] or [f64] for over or underflow;
//! The internals of [NumberIncDecCpCmp] simply checks that the values properly increment and decrement.
//!
//! ## Generic Data types
//! The [AnyIncDecCpCmp] object supports working with any data type, provided it implements: [PartialOrd], [std::ops::Add], [std::ops::Sub], [Copy], and [Clone].
//! When working with generics, the value used by *step* and *rebound* values do not have to be the same type as the *values* used by a range.
//! A practical example of this is how [std::time::Duration] and [std::time::SystemTime] handle [std::ops::Add] and [std::ops::Sub].
//!
//! This is an example that shows how to use [std::time::Duration] to provide the *step* and *rebound* values and [std::time::SystemTime] to operate as the range *values*:
//! ## Beyond Generics
//! In some cases the range values do not implement: [PartialOrd], [std::ops::Add], [std::ops::Sub], [Copy], [Clone] or do so in a way
//! that is incompatable with the required data structure used as a value for the range. The internals of [OverlapIter] use a proxy layer which can be customized to meet most requirements.
//! This example shows how to work with ragnes of custom data strcutres.
//!
//! ## Internal Range Trait
//! Rust has no single trait representing rages aside from [std::ops::RangeBounds], which can require recomputing the begin and or end
//! values of a range on each evaluation. To work around this the internals of this [crate] use a common trait range type of [GetBeginEnd].
//! There is also a factory trait for creating instances called [GetBeginEndOption]. This example shows how to create and use both the
//! factory: [GetBeginEndOption] and the range: [GetBeginEnd]. As a note the [GetBeginEnd] trait is implemnted for [std::ops::RangeInclusive] for the
//! internals of this [crate].
//!
//! ## Consolidation of ranges
//! The [Consolidate] object can be used to consolidate duplicate and overlapping ranges via an [Iterator] of ranges.
//! It is recommended to convert an instance of [Consolidate] into an instance of [ConsolidateChecker] instance to verify the integrity of the data returned by the [Iterator].
//!
//! The ranges returned by the [Iterator] must be in [ConsolidationOrder].
//! - For [ConsolidationOrder::Forward] the expected order is: *start asc, end desc*. For more information see: [crate::sort_forward].
//! - For [ConsolidationOrder::Reverse] the expected order is: *end desc, start asc*. For more information see: [crate::sort_reverse].
//!
//! This example demonstrates how to use [Consolidate] wrapped in an instance of [ConsolidateChecker] using [ConsolidationOrder::Forward]:
//!
//! ## Intersections of multiple Itertors
//! The [Columns] object is a factory can be used to construct an [Iterator] that steps through multiple [Iterator] instances of ranges that can contain duplicate
//! and overlapping ranges that intersect with one another. Each [Column] added to [Columns] is wrapped in an instance of [ConsolidateChecker] to ensure that the consolidation is occuring in the expected [ConsolidationOrder].
//! The ranges returned by the [Iterator] must be in [ConsolidationOrder], see: [Consolidation of ranges](#consolidation-of-ranges) for more information.
//! Finding the intersections from multiple [Iterator] instances is a complex process and is error prone if the data is not provided in the proper order.
//!
//! The ranges returned by each [Iterator] must be in same [ConsolidationOrder] as all other [Iterator] instances.
//! - For [ConsolidationOrder::Forward] the expected order is: *start asc, end desc*. For more information see: [crate::sort_forward].
//! - For [ConsolidationOrder::Reverse] the expected order is: *end desc, start asc*. For more information see: [crate::sort_reverse].
//!
//! This example demonstrates how to create a [ColumnsIter] from a [Columns] instance and walk the results. This example also
//! includes an example of how to use [crate::sort_forward].
//!
//!
//! # Motivation
//! In truth there doesn't seem to be a library on crates.io provides the following functionality:
//! - A range intersection library that handles columns of [Iterator] ranges and progress through those ranges correctly.
//! - An intersection library that could be quickly extended to work with any data structure.
//! - An intersection library that can support any range type via a a common trait.
//! - A reversable range intersection iterator.
//!
//! Other implementations:
//! - [range-ext](https://docs.rs/range-ext/0.3.0/range_ext/index.html)
//! - [range-overlap](https://docs.rs/range-overlap/latest/range_overlap/)
//! - [rangetools](https://crates.io/crates/rangetools)
use PhantomData;
use ;
// re-export to be nice!
pub use crate*;
pub use crate*;
pub use crate*;
pub use crate*;
pub use crate*;
pub use crate*;
pub use crate*;
/// [Mrs] **Minimal Range Span**
///
/// In a nut shell this is the minimal struct to represent a range for [crate].
/// Requires that [GetBeginEnd] and [std::ops::RangeBounds] be imported to use all implemented traits.
///
/// ```
/// use common_range_tools::{
/// Mrs,
/// GetBeginEnd // only required for the self.get_begin() and self.get_end() methods.
/// };
/// use std::ops::{RangeBounds,Bound};
///
/// fn main () {
/// let r=Mrs::new(1,2);
/// assert_eq!(r.start_bound(),Bound::Included(&1));
/// assert_eq!(r.end_bound(),Bound::Included(&2));
/// assert_eq!(r.get_begin(),&1);
/// assert_eq!(r.get_end(),&2);
/// }
///
/// ```
/// Proxy data structure for [Mrs].
/// This struct acts as an owned wrapper for the [Option::Some] produced by the [Consolidate] Iterator,
/// which then acts as a normal instance of [GetBeginEnd].
/// Trait used by internals to represent ranges.