@@ -15,18 +15,16 @@ use digest::DigestUtil;
15
15
use json;
16
16
use sha1:: Sha1 ;
17
17
use serialize:: { Encoder , Encodable , Decoder , Decodable } ;
18
- use sort ;
18
+ use treemap :: TreeMap ;
19
19
20
20
use std:: cell:: Cell ;
21
- use std:: cmp;
22
21
use std:: comm:: { PortOne , oneshot, send_one, recv_one} ;
23
22
use std:: either:: { Either , Left , Right } ;
24
23
use std:: hashmap:: HashMap ;
25
24
use std:: io;
26
25
use std:: result;
27
26
use std:: run;
28
27
use std:: task;
29
- use std:: to_bytes;
30
28
31
29
/**
32
30
*
@@ -96,36 +94,12 @@ use std::to_bytes;
96
94
*
97
95
*/
98
96
99
- #[ deriving( Clone , Eq , Encodable , Decodable ) ]
97
+ #[ deriving( Clone , Eq , Encodable , Decodable , TotalOrd , TotalEq ) ]
100
98
struct WorkKey {
101
99
kind : ~str ,
102
100
name : ~str
103
101
}
104
102
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
-
129
103
impl WorkKey {
130
104
pub fn new ( kind : & str , name : & str ) -> WorkKey {
131
105
WorkKey {
@@ -135,43 +109,16 @@ impl WorkKey {
135
109
}
136
110
}
137
111
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 > ) ;
145
114
146
115
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 ( ) ) }
170
117
}
171
118
172
119
struct Database {
173
120
db_filename : Path ,
174
- db_cache: HashMap < ~str , ~str > ,
121
+ db_cache : TreeMap < ~str , ~str > ,
175
122
db_dirty : bool
176
123
}
177
124
@@ -217,7 +164,7 @@ struct Context {
217
164
db: @mut Database,
218
165
logger: @mut Logger,
219
166
cfg: @json::Object,
220
- freshness: HashMap <~str,@fn(&str,&str)->bool>
167
+ freshness: TreeMap <~str,@fn(&str,&str)->bool>
221
168
}
222
169
223
170
#[deriving(Clone)]
@@ -273,7 +220,7 @@ impl Context {
273
220
db: db,
274
221
logger: lg,
275
222
cfg: cfg,
276
- freshness: HashMap ::new()
223
+ freshness: TreeMap ::new()
277
224
}
278
225
}
279
226
@@ -312,16 +259,20 @@ impl TPrep for Prep {
312
259
fn is_fresh(&self, cat: &str, kind: &str,
313
260
name: &str, val: &str) -> bool {
314
261
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
+ } ;
316
267
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
325
276
}
326
277
327
278
fn all_fresh ( & self , cat : & str , map : & WorkMap ) -> bool {
@@ -411,7 +362,7 @@ fn test() {
411
362
use std:: io:: WriterUtil ;
412
363
413
364
let db = @mut Database { db_filename : Path ( "db.json" ) ,
414
- db_cache : HashMap :: new( ) ,
365
+ db_cache : TreeMap :: new( ) ,
415
366
db_dirty : false } ;
416
367
let lg = @mut Logger { a : ( ) } ;
417
368
let cfg = @HashMap :: new( ) ;
0 commit comments