From 0796ee77baf4a186413c1176878e6705fa9dc377 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 23 May 2016 14:31:52 +0200 Subject: [PATCH] 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. --- src/librustc_borrowck/indexed_set.rs | 87 ++++++++++++++++++++++++++++ src/librustc_borrowck/lib.rs | 1 + 2 files changed, 88 insertions(+) create mode 100644 src/librustc_borrowck/indexed_set.rs diff --git a/src/librustc_borrowck/indexed_set.rs b/src/librustc_borrowck/indexed_set.rs new file mode 100644 index 00000000000..c743e58aa99 --- /dev/null +++ b/src/librustc_borrowck/indexed_set.rs @@ -0,0 +1,87 @@ +// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::fmt; +use std::marker::PhantomData; +use std::mem; +use bitslice::{BitSlice, Word}; + +pub trait Indexed { + type Idx: Idx; +} + +pub trait Idx { + fn idx(&self) -> usize; +} + +pub struct OwnIdxSet { + _pd: PhantomData &T>, + bits: Vec, +} + +// pnkfelix wants to have this be `IdxSet([Word]) and then pass +// around `&mut IdxSet` or `&IdxSet`. +// +// Mmapping a `&OwnIdxSet` to `&IdxSet` (at least today) +// requires a transmute relying on representation guarantees that may +// not hold in the future. + +pub struct IdxSet { + _pd: PhantomData &T>, + bits: [Word], +} + +impl fmt::Debug for OwnIdxSet { + fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { self.bits.fmt(w) } +} + +impl fmt::Debug for IdxSet { + fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { self.bits.fmt(w) } +} + +impl OwnIdxSet { + fn new(init: Word, universe_size: usize) -> Self { + let bits_per_word = mem::size_of::(); + let num_words = (universe_size + (bits_per_word - 1)) / bits_per_word; + OwnIdxSet { + _pd: Default::default(), + bits: vec![init; num_words], + } + } + + /// Creates set holding every element whose index falls in range 0..universe_size. + pub fn new_filled(universe_size: usize) -> Self { + Self::new(!0, universe_size) + } + + /// Creates set holding no elements. + pub fn new_empty(universe_size: usize) -> Self { + Self::new(0, universe_size) + } + + /// Removes `elem` from the set `self`; returns true iff this changed `self`. + pub fn clear(&mut self, elem: &T) -> bool { + self.bits.clear_bit(elem.idx()) + } + + /// Adds `elem` to the set `self`; returns true iff this changed `self`. + pub fn add(&mut self, elem: &T) -> bool { + self.bits.set_bit(elem.idx()) + } + + /// Returns true iff set `self` contains `elem`. + pub fn contains(&self, elem: &T) -> bool { + self.bits.get_bit(elem.idx()) + } + + pub fn bits(&self) -> &[Word] { + &self.bits[..] + } +} diff --git a/src/librustc_borrowck/lib.rs b/src/librustc_borrowck/lib.rs index e38677de662..9d7e05ed9fa 100644 --- a/src/librustc_borrowck/lib.rs +++ b/src/librustc_borrowck/lib.rs @@ -47,6 +47,7 @@ pub mod diagnostics; mod borrowck; mod bitslice; +mod indexed_set; pub mod graphviz;