Skip to content

Commit 0796ee7

Browse files
committed
add indexed_set mod for typed wrappers around bitarrays representing sets.
It provides an `Idx` trait for usize wrappers used to represent the elements of such sets.
1 parent f18bafd commit 0796ee7

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/librustc_borrowck/indexed_set.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::fmt;
12+
use std::marker::PhantomData;
13+
use std::mem;
14+
use bitslice::{BitSlice, Word};
15+
16+
pub trait Indexed {
17+
type Idx: Idx;
18+
}
19+
20+
pub trait Idx {
21+
fn idx(&self) -> usize;
22+
}
23+
24+
pub struct OwnIdxSet<T: Idx> {
25+
_pd: PhantomData<fn(&[T], usize) -> &T>,
26+
bits: Vec<Word>,
27+
}
28+
29+
// pnkfelix wants to have this be `IdxSet<T>([Word]) and then pass
30+
// around `&mut IdxSet<T>` or `&IdxSet<T>`.
31+
//
32+
// Mmapping a `&OwnIdxSet<T>` to `&IdxSet<T>` (at least today)
33+
// requires a transmute relying on representation guarantees that may
34+
// not hold in the future.
35+
36+
pub struct IdxSet<T: Idx> {
37+
_pd: PhantomData<fn(&[T], usize) -> &T>,
38+
bits: [Word],
39+
}
40+
41+
impl<T: Idx> fmt::Debug for OwnIdxSet<T> {
42+
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { self.bits.fmt(w) }
43+
}
44+
45+
impl<T: Idx> fmt::Debug for IdxSet<T> {
46+
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { self.bits.fmt(w) }
47+
}
48+
49+
impl<T: Idx> OwnIdxSet<T> {
50+
fn new(init: Word, universe_size: usize) -> Self {
51+
let bits_per_word = mem::size_of::<Word>();
52+
let num_words = (universe_size + (bits_per_word - 1)) / bits_per_word;
53+
OwnIdxSet {
54+
_pd: Default::default(),
55+
bits: vec![init; num_words],
56+
}
57+
}
58+
59+
/// Creates set holding every element whose index falls in range 0..universe_size.
60+
pub fn new_filled(universe_size: usize) -> Self {
61+
Self::new(!0, universe_size)
62+
}
63+
64+
/// Creates set holding no elements.
65+
pub fn new_empty(universe_size: usize) -> Self {
66+
Self::new(0, universe_size)
67+
}
68+
69+
/// Removes `elem` from the set `self`; returns true iff this changed `self`.
70+
pub fn clear(&mut self, elem: &T) -> bool {
71+
self.bits.clear_bit(elem.idx())
72+
}
73+
74+
/// Adds `elem` to the set `self`; returns true iff this changed `self`.
75+
pub fn add(&mut self, elem: &T) -> bool {
76+
self.bits.set_bit(elem.idx())
77+
}
78+
79+
/// Returns true iff set `self` contains `elem`.
80+
pub fn contains(&self, elem: &T) -> bool {
81+
self.bits.get_bit(elem.idx())
82+
}
83+
84+
pub fn bits(&self) -> &[Word] {
85+
&self.bits[..]
86+
}
87+
}

src/librustc_borrowck/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub mod diagnostics;
4747

4848
mod borrowck;
4949
mod bitslice;
50+
mod indexed_set;
5051

5152
pub mod graphviz;
5253

0 commit comments

Comments
 (0)