@@ -213,16 +213,16 @@ impl API {
213
213
214
214
// histogram aggregation via join
215
215
pub async fn abs_histogram ( & self , abs : Abstraction ) -> Result < Histogram , E > {
216
+ let idx = i64:: from ( abs) ;
216
217
let mass = abs. street ( ) . n_children ( ) as f32 ;
217
- let abs = i64:: from ( abs) ;
218
218
const SQL : & ' static str = r#"
219
219
SELECT next, dx
220
220
FROM transitions
221
221
WHERE prev = $1
222
222
"# ;
223
223
Ok ( self
224
224
. 0
225
- . query ( SQL , & [ & abs ] )
225
+ . query ( SQL , & [ & idx ] )
226
226
. await ?
227
227
. iter ( )
228
228
. map ( |row| ( row. get :: < _ , i64 > ( 0 ) , row. get :: < _ , Energy > ( 1 ) ) )
@@ -235,26 +235,26 @@ impl API {
235
235
}
236
236
pub async fn obs_histogram ( & self , obs : Observation ) -> Result < Histogram , E > {
237
237
// Kd8s~6dJsAc
238
- let isos = obs
239
- . children ( )
240
- . map ( Isomorphism :: from)
241
- . map ( Observation :: from)
242
- . map ( i64:: from)
243
- . collect :: < Vec < i64 > > ( ) ;
238
+ let idx = i64:: from ( Observation :: from ( Isomorphism :: from ( obs) ) ) ;
239
+ let mass = obs. street ( ) . n_children ( ) as f32 ;
244
240
const SQL : & ' static str = r#"
245
- SELECT abs
246
- FROM encoder
247
- WHERE obs = ANY($1)
241
+ SELECT next, dx
242
+ FROM transitions
243
+ JOIN encoder ON encoder.abs = transitions.prev
244
+ WHERE encoder.obs = $1
248
245
"# ;
249
246
Ok ( self
250
247
. 0
251
- . query ( SQL , & [ & isos ] )
248
+ . query ( SQL , & [ & idx ] )
252
249
. await ?
253
250
. iter ( )
254
- . map ( |row| row. get :: < _ , i64 > ( 0 ) )
255
- . map ( Abstraction :: from)
256
- . collect :: < Vec < Abstraction > > ( )
257
- . into ( ) )
251
+ . map ( |row| ( row. get :: < _ , i64 > ( 0 ) , row. get :: < _ , Energy > ( 1 ) ) )
252
+ . map ( |( next, dx) | ( next, ( dx * mass) . round ( ) as usize ) )
253
+ . map ( |( next, dx) | ( Abstraction :: from ( next) , dx) )
254
+ . fold ( Histogram :: default ( ) , |mut h, ( next, dx) | {
255
+ h. set ( next, dx) ;
256
+ h
257
+ } ) )
258
258
}
259
259
260
260
// observation similarity lookups
0 commit comments