OrdMask
ordmask is a library for efficient range-based set operations and membership checking. It represents a set of values as a collection of intervals and supports various set operations.
Features
- Efficient range membership checking
- Support for
union,intersection,minus,complement, andsymmetric_differenceoperations - Works with any type that implements
Ord,Clone, andWithMintraits - Zero-allocation operations where possible
- Optional
serdefeature for serialization/deserialization
Construction
use ;
// [0, 10), [20, 30) and [40, MAX]
let mask = ordmask!;
assert!;
assert!;
assert!;
// Create from `Vec<T>`
assert_eq!;
// Create from suspicious_points and a predicate
use BTreeSet;
assert_eq!;
// Create from suspicious_points_map
use BTreeMap;
let map = from;
assert_eq!;
// [MIN, 10)
let mask = ordmask!;
assert_eq!;
assert!;
assert!;
// [10, MAX]
let mask = ordmask!;
assert_eq!;
assert!;
assert!;
// [10, 20)
let mask = ordmask!;
assert_eq!;
assert!;
assert!;
assert!;
assert!;
// Universal
let mask = ordmask!;
assert_eq!;
assert!;
assert!;
// Empty
let mask = ordmask!;
assert_eq!;
assert!;
assert!;
Type Annotation
You can specify the type explicitly using the <T> syntax in the macro:
use ;
// Explicit type annotation with <T>
let mask = ordmask!; // Empty
let mask = ordmask!; // Universal
let mask = ordmask!; // [10, MAX]
let mask = ordmask!; // [10, 20)
let mask = ordmask!; // [MIN, 10)
Union
use ;
let a = ordmask!;
let b = ordmask!;
let c = ordmask!;
// &a | &b | &c: reference operators do not move (consume) the values
assert_eq!;
// a | b | c: non-reference operators move (consume) the values
assert_eq!;
Intersection
use ;
let a = ordmask!;
let b = ordmask!;
let c = ordmask!;
// &a & &b & &c: reference operators do not move (consume) the values
assert_eq!;
// a & b & c: non-reference operators move (consume) the values
assert_eq!;
Minus and Complement
use ;
let a = ordmask!;
let b = ordmask!;
let c = ordmask!;
// &a - &b - &c: reference operators do not move (consume) the values
assert_eq!;
// a - b - c: non-reference operators move (consume) the values
assert_eq!;
let a = ordmask!;
// !&a: reference operator and `a.complement()` do not move (consume) the value
assert_eq!;
// !a: non-reference operator and `a.to_complement()` move (consume) the value
assert_eq!;
Symmetric Difference
use ;
let a = ordmask!;
let b = ordmask!;
// &a ^ &b: reference operators do not move (consume) the values
assert_eq!;
// a ^ b: non-reference operators move (consume) the values
assert_eq!;
Spans
OrdMask provides methods to iterate over included spans. Each span is returned as a tuple (start, end) representing a half-open interval [start, end).
Note:
- Using spans requires type
Tto implement theWithMaxtrait (the library provides implementations for all standard integer types).- Since spans are half-open intervals
[start, end), whetherMAXis included can be confusing. Use.is_max_value_included()to check if the maximum value is in the mask.
Basic Iteration
Use .spans() to iterate over included spans.
use ordmask;
// Empty mask has no spans
assert_eq!;
// Universal mask has one span [MIN, MAX]
assert_eq!;
// Single span [1, 2)
assert_eq!;
// Multiple spans: [MIN, 1) and [2, MAX]
assert_eq!;
Owning Iteration
Use .into_spans() to consume the mask and return an owning iterator:
use ordmask;
assert_eq!;
Span Count and Size
Use .spans_count() to get the number of spans in O(1) time without consuming an iterator.
It's equivalent to .spans().count() but more efficient.
use ;
// Span count
assert_eq!; // [MIN, 10)
assert_eq!; // [MIN, 10), [20, MAX]
assert_eq!;
assert_eq!;
// Sum of span sizes
// [0, 10)
assert_eq!;
// [0, 10), [20, MAX]
assert_eq!;
// Empty mask has size 0
assert_eq!;
Warning:
.spans().sum_size()may panic due to overflow when called on a universal mask (becauseMAX - MIN + 1overflows). Use.is_universal()to check before calling this.
Type Requirements
OrdMask<T> requires T to implement the WithMin trait, a trait for types that have a minimum value. The library provides implementations for all standard integer types:
use WithMin;
// Built-in implementations for:
// u8, u16, u32, u64, u128, usize
// i8, i16, i32, i64, i128, isize
assert_eq!;
assert_eq!;
To use custom types:
- At minimum, implement
WithMinto useOrdMask - To use
.spans()or.into_spans(), also implementWithMax - To use
.spans().sum_size(), also implementOrderedSub
Note:
spans_count()is special—it only requiresWithMin, notWithMax.
use ;
;
// Required implementation, Enables `.spans_count()`
// Enables `.spans()`, `.into_spans()`
// Enables `.spans().sum_size()`
assert!;
assert_eq!;