Skip to content

Commit 48dd728

Browse files
committed
extra: use deriving more in workcache, switch to treemaps.
1 parent 8990f8b commit 48dd728

File tree

1 file changed

+22
-71
lines changed

1 file changed

+22
-71
lines changed

src/libextra/workcache.rs

Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ use digest::DigestUtil;
1515
use json;
1616
use sha1::Sha1;
1717
use serialize::{Encoder, Encodable, Decoder, Decodable};
18-
use sort;
18+
use treemap::TreeMap;
1919

2020
use std::cell::Cell;
21-
use std::cmp;
2221
use std::comm::{PortOne, oneshot, send_one, recv_one};
2322
use std::either::{Either, Left, Right};
2423
use std::hashmap::HashMap;
2524
use std::io;
2625
use std::result;
2726
use std::run;
2827
use std::task;
29-
use std::to_bytes;
3028

3129
/**
3230
*
@@ -96,36 +94,12 @@ use std::to_bytes;
9694
*
9795
*/
9896

99-
#[deriving(Clone, Eq, Encodable, Decodable)]
97+
#[deriving(Clone, Eq, Encodable, Decodable, TotalOrd, TotalEq)]
10098
struct WorkKey {
10199
kind: ~str,
102100
name: ~str
103101
}
104102

105-
impl to_bytes::IterBytes for WorkKey {
106-
#[inline]
107-
fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool {
108-
self.kind.iter_bytes(lsb0, |b| f(b)) && self.name.iter_bytes(lsb0, |b| f(b))
109-
}
110-
}
111-
112-
impl cmp::Ord for WorkKey {
113-
fn lt(&self, other: &WorkKey) -> bool {
114-
self.kind < other.kind ||
115-
(self.kind == other.kind &&
116-
self.name < other.name)
117-
}
118-
fn le(&self, other: &WorkKey) -> bool {
119-
self.lt(other) || self.eq(other)
120-
}
121-
fn ge(&self, other: &WorkKey) -> bool {
122-
self.gt(other) || self.eq(other)
123-
}
124-
fn gt(&self, other: &WorkKey) -> bool {
125-
! self.le(other)
126-
}
127-
}
128-
129103
impl WorkKey {
130104
pub fn new(kind: &str, name: &str) -> WorkKey {
131105
WorkKey {
@@ -135,43 +109,16 @@ impl WorkKey {
135109
}
136110
}
137111

138-
struct WorkMap(HashMap<WorkKey, ~str>);
139-
140-
impl Clone for WorkMap {
141-
fn clone(&self) -> WorkMap {
142-
WorkMap((**self).clone())
143-
}
144-
}
112+
#[deriving(Clone, Eq, Encodable, Decodable)]
113+
struct WorkMap(TreeMap<WorkKey, ~str>);
145114

146115
impl WorkMap {
147-
fn new() -> WorkMap { WorkMap(HashMap::new()) }
148-
}
149-
150-
impl<S:Encoder> Encodable<S> for WorkMap {
151-
fn encode(&self, s: &mut S) {
152-
let mut d = ~[];
153-
for self.iter().advance |(k, v)| {
154-
d.push(((*k).clone(), (*v).clone()))
155-
}
156-
sort::tim_sort(d);
157-
d.encode(s)
158-
}
159-
}
160-
161-
impl<D:Decoder> Decodable<D> for WorkMap {
162-
fn decode(d: &mut D) -> WorkMap {
163-
let v : ~[(WorkKey,~str)] = Decodable::decode(d);
164-
let mut w = WorkMap::new();
165-
for v.iter().advance |pair| {
166-
w.insert(pair.first(), pair.second());
167-
}
168-
w
169-
}
116+
fn new() -> WorkMap { WorkMap(TreeMap::new()) }
170117
}
171118

172119
struct Database {
173120
db_filename: Path,
174-
db_cache: HashMap<~str, ~str>,
121+
db_cache: TreeMap<~str, ~str>,
175122
db_dirty: bool
176123
}
177124

@@ -217,7 +164,7 @@ struct Context {
217164
db: @mut Database,
218165
logger: @mut Logger,
219166
cfg: @json::Object,
220-
freshness: HashMap<~str,@fn(&str,&str)->bool>
167+
freshness: TreeMap<~str,@fn(&str,&str)->bool>
221168
}
222169
223170
#[deriving(Clone)]
@@ -273,7 +220,7 @@ impl Context {
273220
db: db,
274221
logger: lg,
275222
cfg: cfg,
276-
freshness: HashMap::new()
223+
freshness: TreeMap::new()
277224
}
278225
}
279226
@@ -312,16 +259,20 @@ impl TPrep for Prep {
312259
fn is_fresh(&self, cat: &str, kind: &str,
313260
name: &str, val: &str) -> bool {
314261
let k = kind.to_owned();
315-
let f = (*self.ctxt.freshness.get(&k))(name, val);
262+
let f = self.ctxt.freshness.find(&k);
263+
let fresh = match f {
264+
None => fail!("missing freshness-function for '%s'", kind),
265+
Some(f) => (*f)(name, val)
266+
};
316267
let lg = self.ctxt.logger;
317-
if f {
318-
lg.info(fmt!("%s %s:%s is fresh",
319-
cat, kind, name));
320-
} else {
321-
lg.info(fmt!("%s %s:%s is not fresh",
322-
cat, kind, name))
323-
}
324-
f
268+
if fresh {
269+
lg.info(fmt!("%s %s:%s is fresh",
270+
cat, kind, name));
271+
} else {
272+
lg.info(fmt!("%s %s:%s is not fresh",
273+
cat, kind, name))
274+
}
275+
fresh
325276
}
326277

327278
fn all_fresh(&self, cat: &str, map: &WorkMap) -> bool {
@@ -411,7 +362,7 @@ fn test() {
411362
use std::io::WriterUtil;
412363

413364
let db = @mut Database { db_filename: Path("db.json"),
414-
db_cache: HashMap::new(),
365+
db_cache: TreeMap::new(),
415366
db_dirty: false };
416367
let lg = @mut Logger { a: () };
417368
let cfg = @HashMap::new();

0 commit comments

Comments
 (0)