Skip to content

Commit 5e0b156

Browse files
feat: introduce operator overloads for Union, Intersect, and Except in RangeSet
Added `|`, `&`, and `-` operator overloads to simplify union, intersection, and difference operations on `RangeSet`. Updated `README.md` with examples of the new operator usage.
1 parent 91006ca commit 5e0b156

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

CodoMetis.ValueRanges/RangeSet.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,27 @@ public RangeSet<TRange, T> Union(RangeSet<TRange, T> other) =>
164164
other.Count == 0 ? this :
165165
Count == 0 ? other : From(_elements.Concat(other._elements));
166166

167+
/// <summary>
168+
/// Returns the union of <paramref name="left"/> and <paramref name="right"/>.
169+
/// </summary>
170+
/// <param name="left">The set to add to.</param>
171+
/// <param name="right">The range to add.</param>
172+
/// <returns>
173+
/// A normalized set containing every value of <paramref name="left"/> and of <paramref name="right"/>;
174+
/// the range is merged into existing elements where it overlaps or is adjacent.
175+
/// </returns>
176+
public static RangeSet<TRange, T> operator |(RangeSet<TRange, T> left, TRange right) =>
177+
left.Union(right);
178+
179+
/// <summary>
180+
/// Returns the union of <paramref name="left"/> and <paramref name="right"/>.
181+
/// </summary>
182+
/// <param name="left">The set to add to.</param>
183+
/// <param name="right">The set to combine with.</param>
184+
/// <returns>A normalized set containing every value of both sets.</returns>
185+
public static RangeSet<TRange, T> operator |(RangeSet<TRange, T> left, RangeSet<TRange, T> right) =>
186+
left.Union(right);
187+
167188
/// <summary>
168189
/// Returns the intersection of this set with <paramref name="other"/>.
169190
/// </summary>
@@ -206,6 +227,27 @@ public RangeSet<TRange, T> Intersect(RangeSet<TRange, T> other)
206227
return From(results);
207228
}
208229

230+
/// <summary>
231+
/// Returns the intersection of <paramref name="left"/> with <paramref name="right"/>.
232+
/// </summary>
233+
/// <param name="left">The set to intersect.</param>
234+
/// <param name="right">The range to intersect with.</param>
235+
/// <returns>
236+
/// A set containing, for each element of <paramref name="left"/>, its overlap with <paramref name="right"/>;
237+
/// <see cref="Empty"/> when nothing overlaps.
238+
/// </returns>
239+
public static RangeSet<TRange, T> operator &(RangeSet<TRange, T> left, TRange right) =>
240+
left.Intersect(right);
241+
242+
/// <summary>
243+
/// Returns the intersection of <paramref name="left"/> with <paramref name="right"/>.
244+
/// </summary>
245+
/// <param name="left">The set to intersect.</param>
246+
/// <param name="right">The set to intersect with.</param>
247+
/// <returns>A normalized set containing the values common to both sets.</returns>
248+
public static RangeSet<TRange, T> operator &(RangeSet<TRange, T> left, RangeSet<TRange, T> right) =>
249+
left.Intersect(right);
250+
209251
/// <summary>
210252
/// Returns what remains of this set after removing every value covered by <paramref name="other"/>.
211253
/// </summary>
@@ -233,6 +275,27 @@ public RangeSet<TRange, T> Except(TRange other)
233275
public RangeSet<TRange, T> Except(RangeSet<TRange, T> other) =>
234276
Enumerable.Aggregate(other._elements, this, (current, range) => current.Except(range));
235277

278+
/// <summary>
279+
/// Returns what remains of <paramref name="left"/> after removing every value covered by <paramref name="right"/>.
280+
/// </summary>
281+
/// <param name="left">The set to subtract from.</param>
282+
/// <param name="right">The range to subtract.</param>
283+
/// <returns>
284+
/// A normalized set of the remaining pieces; an element split by
285+
/// <paramref name="right"/> contributes both of its parts.
286+
/// </returns>
287+
public static RangeSet<TRange, T> operator -(RangeSet<TRange, T> left, TRange right) =>
288+
left.Except(right);
289+
290+
/// <summary>
291+
/// Returns what remains of <paramref name="left"/> after removing every value covered by <paramref name="right"/>.
292+
/// </summary>
293+
/// <param name="left">The set to subtract from.</param>
294+
/// <param name="right">The set to subtract.</param>
295+
/// <returns>A normalized set of the remaining pieces.</returns>
296+
public static RangeSet<TRange, T> operator -(RangeSet<TRange, T> left, RangeSet<TRange, T> right) =>
297+
left.Except(right);
298+
236299
/// <summary>
237300
/// Returns the complement of this set — every value of the domain not covered by it.
238301
/// </summary>

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,15 @@ set.Contains(7); // true
241241
set.Contains(Int32Range.CreateFinite(2, 8)); // true — within a single element
242242
set.Overlaps(Int32Range.CreateFinite(15, 25)); // true
243243
244-
// Set operations — single-range and bulk variants
245-
set.Union(Int32Range.CreateFinite(11, 19)); // { [1, 30] } — bridges the gap
244+
// Set operations — single-range and bulk variants, with operator aliases (|, &, -)
245+
set.Union(Int32Range.CreateFinite(11, 19)); // { [1, 30] } — bridges the gap
246+
set | Int32Range.CreateFinite(11, 19); // { [1, 30] }
247+
246248
set.Intersect(Int32Range.CreateFinite(5, 25)); // { [5, 10], [20, 25] }
249+
set & Int32Range.CreateFinite(5, 25); // { [5, 10], [20, 25] }
250+
247251
set.Except(Int32Range.CreateFinite(4, 6)); // { [1, 3], [7, 10], [20, 30] }
252+
set - Int32Range.CreateFinite(4, 6); // { [1, 3], [7, 10], [20, 30] }
248253
249254
// Complement — every value not covered by the set
250255
set.Complement(); // { (-∞, 0], [11, 19], [31, +∞) }

0 commit comments

Comments
 (0)