auto merge of #5095 : thestinger/rust/smallintmap, r=catamorphism

Closes #4738.

The `std::smallintmap` module doesn't use an implicit @ box so I just added that explicitly for now to make porting simple.
This commit is contained in:
bors 2013-02-25 15:18:45 -08:00
commit 08d870e566
9 changed files with 27 additions and 268 deletions

View File

@ -860,7 +860,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
}
}
}
do option::iter(&(*tcx.node_types).find(id as uint)) |ty| {
do option::iter(&tcx.node_types.find(&(id as uint))) |&ty| {
do ebml_w.tag(c::tag_table_node_type) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
@ -1135,7 +1135,7 @@ fn decode_side_tables(xcx: @ExtendedDecodeContext,
let ty = val_dsr.read_ty(xcx);
debug!("inserting ty for node %?: %s",
id, ty_to_str(dcx.tcx, ty));
(*dcx.tcx.node_types).insert(id as uint, ty);
dcx.tcx.node_types.insert(id as uint, ty);
} else if tag == (c::tag_table_node_type_subst as uint) {
let tys = val_dsr.read_tys(xcx);
dcx.tcx.node_type_substs.insert(id, tys);

View File

@ -34,8 +34,7 @@ use core::uint;
use core::vec;
use std::oldmap::{Map, HashMap};
use std::oldmap;
use std::oldsmallintmap::{Map, SmallIntMap};
use std::oldsmallintmap;
use std::smallintmap::SmallIntMap;
use syntax::ast_util::{path_to_ident};
use syntax::attr;
use syntax::codemap::span;
@ -275,7 +274,7 @@ pub fn get_lint_dict() -> LintDict {
}
// This is a highly not-optimal set of data structure decisions.
type LintModes = SmallIntMap<level>;
type LintModes = @mut SmallIntMap<level>;
type LintModeMap = HashMap<ast::node_id, LintModes>;
// settings_map maps node ids of items with non-default lint settings
@ -288,14 +287,14 @@ pub struct LintSettings {
pub fn mk_lint_settings() -> LintSettings {
LintSettings {
default_settings: oldsmallintmap::mk(),
default_settings: @mut SmallIntMap::new(),
settings_map: HashMap()
}
}
pub fn get_lint_level(modes: LintModes, lint: lint) -> level {
match modes.find(lint as uint) {
Some(c) => c,
match modes.find(&(lint as uint)) {
Some(&c) => c,
None => allow
}
}
@ -314,8 +313,7 @@ pub fn get_lint_settings_level(settings: LintSettings,
// This is kind of unfortunate. It should be somewhere else, or we should use
// a persistent data structure...
fn clone_lint_modes(modes: LintModes) -> LintModes {
oldsmallintmap::SmallIntMap_(@oldsmallintmap::SmallIntMap_
{v: copy modes.v})
@mut (copy *modes)
}
struct Context {
@ -332,7 +330,7 @@ impl Context {
fn set_level(&self, lint: lint, level: level) {
if level == allow {
self.curr.remove(lint as uint);
self.curr.remove(&(lint as uint));
} else {
self.curr.insert(lint as uint, level);
}
@ -440,7 +438,7 @@ fn build_settings_item(i: @ast::item, &&cx: Context, v: visit::vt<Context>) {
pub fn build_settings_crate(sess: session::Session, crate: @ast::crate) {
let cx = Context {
dict: get_lint_dict(),
curr: oldsmallintmap::mk(),
curr: @mut SmallIntMap::new(),
is_default: true,
sess: sess
};
@ -458,7 +456,7 @@ pub fn build_settings_crate(sess: session::Session, crate: @ast::crate) {
do cx.with_lint_attrs(/*bad*/copy crate.node.attrs) |cx| {
// Copy out the default settings
for cx.curr.each |k, v| {
for cx.curr.each |&(k, &v)| {
sess.lint_settings.default_settings.insert(k, v);
}

View File

@ -76,7 +76,6 @@ use core::option::{is_none, is_some};
use core::option;
use core::uint;
use std::oldmap::HashMap;
use std::oldsmallintmap;
use std::{oldmap, time, list};
use syntax::ast_map::{path, path_elt_to_str, path_mod, path_name};
use syntax::ast_util::{def_id_of_def, local_def, path_to_ident};

View File

@ -42,7 +42,8 @@ use core::uint;
use core::vec;
use core::hashmap::linear::LinearMap;
use std::oldmap::HashMap;
use std::{oldmap, oldsmallintmap};
use std::oldmap;
use std::smallintmap::SmallIntMap;
use syntax::ast::*;
use syntax::ast_util::{is_local, local_def};
use syntax::ast_util;
@ -767,7 +768,7 @@ type type_cache = HashMap<ast::def_id, ty_param_bounds_and_ty>;
type constness_cache = HashMap<ast::def_id, const_eval::constness>;
pub type node_type_table = @oldsmallintmap::SmallIntMap<t>;
pub type node_type_table = @mut SmallIntMap<t>;
fn mk_rcache() -> creader_cache {
type val = {cnum: int, pos: uint, len: uint};
@ -812,7 +813,7 @@ pub fn mk_ctxt(s: session::Session,
def_map: dm,
region_map: region_map,
region_paramd_items: region_paramd_items,
node_types: @oldsmallintmap::mk(),
node_types: @mut SmallIntMap::new(),
node_type_substs: oldmap::HashMap(),
items: amap,
intrinsic_defs: oldmap::HashMap(),
@ -2787,8 +2788,8 @@ pub fn br_hashmap<V:Copy>() -> HashMap<bound_region, V> {
pub fn node_id_to_type(cx: ctxt, id: ast::node_id) -> t {
//io::println(fmt!("%?/%?", id, cx.node_types.len()));
match oldsmallintmap::find(*cx.node_types, id as uint) {
Some(t) => t,
match cx.node_types.find(&(id as uint)) {
Some(&t) => t,
None => cx.sess.bug(
fmt!("node_id_to_type: no type for node `%s`",
ast_map::node_id_to_str(cx.items, id,
@ -3179,8 +3180,8 @@ pub fn expr_kind(tcx: ctxt,
}
ast::expr_cast(*) => {
match oldsmallintmap::find(*tcx.node_types, expr.id as uint) {
Some(t) => {
match tcx.node_types.find(&(expr.id as uint)) {
Some(&t) => {
if ty::type_is_immediate(t) {
RvalueDatumExpr
} else {

View File

@ -277,7 +277,7 @@ use core::result;
use core::vec;
use std::list::Nil;
use std::oldmap::HashMap;
use std::oldsmallintmap;
use std::smallintmap::SmallIntMap;
use syntax::ast::{ret_style, purity};
use syntax::ast::{m_const, m_imm, m_mutbl};
use syntax::ast::{unsafe_fn, impure_fn, pure_fn, extern_fn};
@ -353,7 +353,7 @@ pub fn fixup_err_to_str(f: fixup_err) -> ~str {
fn new_ValsAndBindings<V:Copy,T:Copy>() -> ValsAndBindings<V, T> {
ValsAndBindings {
vals: oldsmallintmap::mk(),
vals: @mut SmallIntMap::new(),
bindings: ~[]
}
}

View File

@ -10,7 +10,7 @@
use core::prelude::*;
use core::result;
use std::oldsmallintmap::SmallIntMap;
use std::smallintmap::SmallIntMap;
use middle::ty::{Vid, expected_found, IntVarValue};
use middle::ty;
@ -27,7 +27,7 @@ pub enum VarValue<V, T> {
}
pub struct ValsAndBindings<V, T> {
vals: SmallIntMap<VarValue<V, T>>,
vals: @mut SmallIntMap<VarValue<V, T>>,
bindings: ~[(V, VarValue<V, T>)],
}
@ -64,12 +64,12 @@ pub impl InferCtxt {
vid: V) -> Node<V, T>
{
let vid_u = vid.to_uint();
match vb.vals.find(vid_u) {
match vb.vals.find(&vid_u) {
None => {
tcx.sess.bug(fmt!(
"failed lookup of vid `%u`", vid_u));
}
Some(ref var_val) => {
Some(var_val) => {
match *var_val {
Redirect(vid) => {
let node: Node<V,T> = helper(tcx, vb, vid);
@ -103,8 +103,8 @@ pub impl InferCtxt {
{ // FIXME(#4903)---borrow checker is not flow sensitive
let vb = UnifyVid::appropriate_vals_and_bindings(self);
let old_v = vb.vals.get(vid.to_uint());
vb.bindings.push((vid, old_v));
let old_v = vb.vals.get(&vid.to_uint());
vb.bindings.push((vid, *old_v));
vb.vals.insert(vid.to_uint(), new_v);
}
}

View File

@ -69,7 +69,6 @@ use std::list::{List, Nil, Cons};
use std::list;
use std::oldmap::HashMap;
use std::oldmap;
use std::oldsmallintmap;
use syntax::ast::{provided, required};
use syntax::ast_map::node_id_to_str;
use syntax::ast_util::{local_def, split_trait_methods};

View File

@ -1,237 +0,0 @@
// Copyright 2012 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*!
* A simple map based on a vector for small integer keys. Space requirements
* are O(highest integer key).
*/
#[forbid(deprecated_mode)];
use core::container::{Container, Mutable, Map, Set};
use core::dvec::DVec;
use core::ops;
use core::option::{Some, None};
use core::option;
use core::prelude::*;
// FIXME (#2347): Should not be @; there's a bug somewhere in rustc that
// requires this to be.
struct SmallIntMap_<T> {
v: DVec<Option<T>>,
}
pub enum SmallIntMap<T> {
SmallIntMap_(@SmallIntMap_<T>)
}
/// Create a smallintmap
pub fn mk<T:Copy>() -> SmallIntMap<T> {
let v = DVec();
SmallIntMap_(@SmallIntMap_ { v: v } )
}
/**
* Add a value to the map. If the map already contains a value for
* the specified key then the original value is replaced.
*/
#[inline(always)]
pub fn insert<T:Copy>(self: SmallIntMap<T>, key: uint, val: T) {
//io::println(fmt!("%?", key));
self.v.grow_set_elt(key, &None, Some(val));
}
/**
* Get the value for the specified key. If the key does not exist
* in the map then returns none
*/
pub pure fn find<T:Copy>(self: SmallIntMap<T>, key: uint) -> Option<T> {
if key < self.v.len() { return self.v.get_elt(key); }
return None::<T>;
}
/**
* Get the value for the specified key
*
* # Failure
*
* If the key does not exist in the map
*/
pub pure fn get<T:Copy>(self: SmallIntMap<T>, key: uint) -> T {
match find(self, key) {
None => {
error!("smallintmap::get(): key not present");
fail!();
}
Some(v) => return v
}
}
/// Returns true if the map contains a value for the specified key
pub pure fn contains_key<T:Copy>(self: SmallIntMap<T>, key: uint) -> bool {
return !find(self, key).is_none();
}
impl<V> Container for SmallIntMap<V> {
/// Return the number of elements in the map
pure fn len(&self) -> uint {
let mut sz = 0u;
for self.v.each |item| {
match *item {
Some(_) => sz += 1u,
_ => ()
}
}
sz
}
/// Return true if the map contains no elements
pure fn is_empty(&self) -> bool { self.len() == 0 }
}
impl<V> Mutable for SmallIntMap<V> {
fn clear(&mut self) { self.v.set(~[]) }
}
/// Implements the map::map interface for smallintmap
impl<V:Copy> SmallIntMap<V> {
#[inline(always)]
fn insert(key: uint, value: V) -> bool {
let exists = contains_key(self, key);
insert(self, key, value);
return !exists;
}
fn remove(key: uint) -> bool {
if key >= self.v.len() {
return false;
}
let old = self.v.get_elt(key);
self.v.set_elt(key, None);
old.is_some()
}
pure fn contains_key(key: uint) -> bool {
contains_key(self, key)
}
pure fn contains_key_ref(key: &uint) -> bool {
contains_key(self, *key)
}
pure fn get(key: uint) -> V { get(self, key) }
pure fn find(key: uint) -> Option<V> { find(self, key) }
fn update_with_key(key: uint, val: V, ff: fn(uint, V, V) -> V) -> bool {
match self.find(key) {
None => return self.insert(key, val),
Some(copy orig) => return self.insert(key, ff(key, orig, val)),
}
}
fn update(key: uint, newval: V, ff: fn(V, V) -> V) -> bool {
return self.update_with_key(key, newval, |_k, v, v1| ff(v,v1));
}
pure fn each(it: fn(key: uint, value: V) -> bool) {
self.each_ref(|k, v| it(*k, *v))
}
pure fn each_key(it: fn(key: uint) -> bool) {
self.each_ref(|k, _v| it(*k))
}
pure fn each_value(it: fn(value: V) -> bool) {
self.each_ref(|_k, v| it(*v))
}
pure fn each_ref(it: fn(key: &uint, value: &V) -> bool) {
let mut idx = 0u, l = self.v.len();
while idx < l {
match self.v.get_elt(idx) {
Some(ref elt) => if !it(&idx, elt) { break },
None => ()
}
idx += 1u;
}
}
pure fn each_key_ref(blk: fn(key: &uint) -> bool) {
self.each_ref(|k, _v| blk(k))
}
pure fn each_value_ref(blk: fn(value: &V) -> bool) {
self.each_ref(|_k, v| blk(v))
}
}
impl<V:Copy> ops::Index<uint, V> for SmallIntMap<V> {
pure fn index(&self, key: uint) -> V {
unsafe {
get(*self, key)
}
}
}
#[cfg(test)]
mod tests {
use super::{mk, SmallIntMap};
use core::option::None;
#[test]
fn test_len() {
let mut map = mk();
assert map.len() == 0;
assert map.is_empty();
map.insert(5, 20);
assert map.len() == 1;
assert !map.is_empty();
map.insert(11, 12);
assert map.len() == 2;
assert !map.is_empty();
map.insert(14, 22);
assert map.len() == 3;
assert !map.is_empty();
}
#[test]
fn test_clear() {
let mut map = mk();
map.insert(5, 20);
map.insert(11, 12);
map.insert(14, 22);
map.clear();
assert map.is_empty();
assert map.find(5).is_none();
assert map.find(11).is_none();
assert map.find(14).is_none();
}
#[test]
fn test_insert_with_key() {
let map: SmallIntMap<uint> = mk();
// given a new key, initialize it with this new count, given
// given an existing key, add more to its count
fn addMoreToCount(_k: uint, v0: uint, v1: uint) -> uint {
v0 + v1
}
fn addMoreToCount_simple(v0: uint, v1: uint) -> uint {
v0 + v1
}
// count integers
map.update(3, 1, addMoreToCount_simple);
map.update_with_key(9, 1, addMoreToCount);
map.update(3, 7, addMoreToCount_simple);
map.update_with_key(5, 3, addMoreToCount);
map.update_with_key(3, 2, addMoreToCount);
// check the total counts
assert map.find(3).get() == 10;
assert map.find(5).get() == 3;
assert map.find(9).get() == 1;
// sadly, no sevens were counted
assert None == map.find(7);
}
}

View File

@ -75,7 +75,6 @@ pub mod oldmap;
pub mod priority_queue;
pub mod rope;
pub mod smallintmap;
pub mod oldsmallintmap;
pub mod sort;
pub mod treemap;