Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workcache fixes 1 #7885

Merged
merged 9 commits into from
Jul 24, 2013
22 changes: 11 additions & 11 deletions src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum Json {
}

pub type List = ~[Json];
pub type Object = HashMap<~str, Json>;
pub type Object = TreeMap<~str, Json>;

#[deriving(Eq)]
/// If an error occurs while parsing some JSON, this is the structure which is
Expand Down Expand Up @@ -809,7 +809,7 @@ impl<T : iterator::Iterator<char>> Parser<T> {
self.bump();
self.parse_whitespace();

let mut values = ~HashMap::new();
let mut values = ~TreeMap::new();

if self.ch == '}' {
self.bump();
Expand Down Expand Up @@ -1087,7 +1087,7 @@ impl serialize::Decoder for Decoder {
let len = match self.stack.pop() {
Object(obj) => {
let len = obj.len();
for obj.consume().advance |(key, value)| {
for obj.consume_iter().advance |(key, value)| {
self.stack.push(value);
self.stack.push(String(key));
}
Expand Down Expand Up @@ -1294,19 +1294,19 @@ impl<A:ToJson> ToJson for ~[A] {
fn to_json(&self) -> Json { List(self.map(|elt| elt.to_json())) }
}

impl<A:ToJson> ToJson for HashMap<~str, A> {
impl<A:ToJson> ToJson for TreeMap<~str, A> {
fn to_json(&self) -> Json {
let mut d = HashMap::new();
let mut d = TreeMap::new();
for self.iter().advance |(key, value)| {
d.insert((*key).clone(), value.to_json());
}
Object(~d)
}
}

impl<A:ToJson> ToJson for TreeMap<~str, A> {
impl<A:ToJson> ToJson for HashMap<~str, A> {
fn to_json(&self) -> Json {
let mut d = HashMap::new();
let mut d = TreeMap::new();
for self.iter().advance |(key, value)| {
d.insert((*key).clone(), value.to_json());
}
Expand Down Expand Up @@ -1338,11 +1338,11 @@ mod tests {

use super::*;

use std::hashmap::HashMap;
use std::io;
use std::result;

use extra::serialize::Decodable;
use serialize::Decodable;
use treemap::TreeMap;

#[deriving(Eq, Encodable, Decodable)]
enum Animal {
Expand All @@ -1363,7 +1363,7 @@ mod tests {
}

fn mk_object(items: &[(~str, Json)]) -> Json {
let mut d = ~HashMap::new();
let mut d = ~TreeMap::new();

for items.iter().advance |item| {
match *item {
Expand Down Expand Up @@ -1954,7 +1954,7 @@ mod tests {
fn test_decode_map() {
let s = ~"{\"a\": \"Dog\", \"b\": [\"Frog\", \"Henry\", 349]}";
let mut decoder = Decoder(from_str(s).unwrap());
let mut map: HashMap<~str, Animal> = Decodable::decode(&mut decoder);
let mut map: TreeMap<~str, Animal> = Decodable::decode(&mut decoder);

assert_eq!(map.pop(&~"a"), Some(Dog));
assert_eq!(map.pop(&~"b"), Some(Frog(~"Henry", 349)));
Expand Down
3 changes: 1 addition & 2 deletions src/libextra/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use std::task;
use std::to_str::ToStr;
use std::u64;
use std::f64;
use std::hashmap::HashMap;
use std::os;


Expand Down Expand Up @@ -852,7 +851,7 @@ fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {

impl ToJson for Metric {
fn to_json(&self) -> json::Json {
let mut map = ~HashMap::new();
let mut map = ~TreeMap::new();
map.insert(~"value", json::Number(self.value as float));
map.insert(~"noise", json::Number(self.noise as float));
json::Object(map)
Expand Down
63 changes: 63 additions & 0 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ impl<K: TotalOrd, V> TreeMap<K, V> {
pub fn iter<'a>(&'a self) -> TreeMapIterator<'a, K, V> {
TreeMapIterator{stack: ~[], node: &self.root, remaining: self.length}
}

/// Get a lazy iterator that consumes the treemap.
pub fn consume_iter(self) -> TreeMapConsumeIterator<K, V> {
let TreeMap { root: root, length: length } = self;
let stk = match root {
None => ~[],
Some(~tn) => ~[tn]
};
TreeMapConsumeIterator {
stack: stk,
remaining: length
}
}
}

/// Lazy forward iterator over a map
Expand Down Expand Up @@ -241,6 +254,56 @@ impl<'self, K, V> Iterator<(&'self K, &'self V)> for TreeMapIterator<'self, K, V
}
}

/// Lazy forward iterator over a map that consumes the map while iterating
pub struct TreeMapConsumeIterator<K, V> {
priv stack: ~[TreeNode<K, V>],
priv remaining: uint
}

impl<K, V> Iterator<(K, V)> for TreeMapConsumeIterator<K,V> {
#[inline]
fn next(&mut self) -> Option<(K, V)> {
while !self.stack.is_empty() {
let TreeNode {
key: key,
value: value,
left: left,
right: right,
level: level
} = self.stack.pop();

match left {
Some(~left) => {
let n = TreeNode {
key: key,
value: value,
left: None,
right: right,
level: level
};
self.stack.push(n);
self.stack.push(left);
}
None => {
match right {
Some(~right) => self.stack.push(right),
None => ()
}
self.remaining -= 1;
return Some((key, value))
}
}
}
None
}

#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
(self.remaining, Some(self.remaining))
}

}

impl<'self, T> Iterator<&'self T> for TreeSetIterator<'self, T> {
/// Advance the iterator to the next node (in order). If there are no more nodes, return `None`.
#[inline]
Expand Down
Loading