RangeSet

Struct RangeSet 

Source
pub struct RangeSet<T, const C: usize = 128>
where T: Ord + Copy,
{ /* private fields */ }
Expand description

一个「区间集合」数据结构:维护一组有序、互不重叠的半开区间 [start, end)

  • 插入时会把重叠/相邻的区间自动合并。
  • 删除一个区间时,会从集合里移除交集;必要时把已有区间拆成左右两段。

约定:空区间(start >= end)会被忽略。

Implementations§

Source§

impl<T, const C: usize> RangeSet<T, C>
where T: Ord + Copy,

Source

pub fn new() -> Self

创建空集合。

Examples found in repository?
examples/debug_demo.rs (line 4)
3fn main() {
4    let mut set: RangeSet<i32, 16> = RangeSet::new();
5    set.add(1..5);
6    set.add(3..8);
7    set.add(10..15);
8    set.add(12..18);
9
10    println!("=== 区间合并结果 ===");
11    for (i, range) in set.iter().enumerate() {
12        println!("Element {}: [{}, {})", i, range.start, range.end);
13    }
14
15    println!("\n=== Debug 格式 ===");
16    for (i, range) in set.iter().enumerate() {
17        println!("Element {}: {:?}", i, range);
18    }
19
20    println!("\n=== 完整切片 ===");
21    println!("{:?}", set.as_slice());
22}
Source

pub fn as_slice(&self) -> &[Range<T>]

返回内部区间的切片(已排序、已合并、互不重叠)。

Examples found in repository?
examples/debug_demo.rs (line 21)
3fn main() {
4    let mut set: RangeSet<i32, 16> = RangeSet::new();
5    set.add(1..5);
6    set.add(3..8);
7    set.add(10..15);
8    set.add(12..18);
9
10    println!("=== 区间合并结果 ===");
11    for (i, range) in set.iter().enumerate() {
12        println!("Element {}: [{}, {})", i, range.start, range.end);
13    }
14
15    println!("\n=== Debug 格式 ===");
16    for (i, range) in set.iter().enumerate() {
17        println!("Element {}: {:?}", i, range);
18    }
19
20    println!("\n=== 完整切片 ===");
21    println!("{:?}", set.as_slice());
22}
Source

pub fn iter(&self) -> impl Iterator<Item = &Range<T>>

返回区间迭代器(零拷贝)。

Examples found in repository?
examples/debug_demo.rs (line 11)
3fn main() {
4    let mut set: RangeSet<i32, 16> = RangeSet::new();
5    set.add(1..5);
6    set.add(3..8);
7    set.add(10..15);
8    set.add(12..18);
9
10    println!("=== 区间合并结果 ===");
11    for (i, range) in set.iter().enumerate() {
12        println!("Element {}: [{}, {})", i, range.start, range.end);
13    }
14
15    println!("\n=== Debug 格式 ===");
16    for (i, range) in set.iter().enumerate() {
17        println!("Element {}: {:?}", i, range);
18    }
19
20    println!("\n=== 完整切片 ===");
21    println!("{:?}", set.as_slice());
22}
Source

pub fn is_empty(&self) -> bool

Source

pub fn len(&self) -> usize

Source

pub fn clear(&mut self)

Source

pub fn add(&mut self, range: Range<T>)

添加一个区间;会把与其重叠或相邻的区间合并。

Examples found in repository?
examples/debug_demo.rs (line 5)
3fn main() {
4    let mut set: RangeSet<i32, 16> = RangeSet::new();
5    set.add(1..5);
6    set.add(3..8);
7    set.add(10..15);
8    set.add(12..18);
9
10    println!("=== 区间合并结果 ===");
11    for (i, range) in set.iter().enumerate() {
12        println!("Element {}: [{}, {})", i, range.start, range.end);
13    }
14
15    println!("\n=== Debug 格式 ===");
16    for (i, range) in set.iter().enumerate() {
17        println!("Element {}: {:?}", i, range);
18    }
19
20    println!("\n=== 完整切片 ===");
21    println!("{:?}", set.as_slice());
22}
Source

pub fn contains(&self, value: T) -> bool

查询某个值是否落在任意一个区间中。

Source

pub fn remove(&mut self, range: Range<T>)

删除一个区间:从集合中移除与其相交的部分。

若被删除区间位于某个已有区间内部,会导致该已有区间被拆分为两段。

Source

pub fn extend<I>(&mut self, ranges: I)
where I: IntoIterator<Item = Range<T>>,

批量添加多个区间。

Trait Implementations§

Source§

impl<T, const C: usize> Clone for RangeSet<T, C>
where T: Ord + Copy + Clone,

Source§

fn clone(&self) -> RangeSet<T, C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, const C: usize> Debug for RangeSet<T, C>
where T: Ord + Copy + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const C: usize> Default for RangeSet<T, C>
where T: Ord + Copy,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, const C: usize> Freeze for RangeSet<T, C>
where T: Freeze,

§

impl<T, const C: usize> RefUnwindSafe for RangeSet<T, C>
where T: RefUnwindSafe,

§

impl<T, const C: usize> Send for RangeSet<T, C>
where T: Send,

§

impl<T, const C: usize> Sync for RangeSet<T, C>
where T: Sync,

§

impl<T, const C: usize> Unpin for RangeSet<T, C>
where T: Unpin,

§

impl<T, const C: usize> UnwindSafe for RangeSet<T, C>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.