File tree 6 files changed +23
-28
lines changed 6 files changed +23
-28
lines changed Original file line number Diff line number Diff line change
1
+ # lattice layout
2
+
1
3
练习 order book 中计算 concept lattice 的算法。
2
4
3
- - 也许可以以这个算法为基础 ,来给出 lattice 的 layout。
5
+ - 以这个算法为基础 ,来给出 lattice 的 layout。
4
6
5
- ` concept-graph/ ` -- 从 ` generateConcepts ` 生成有向图
7
+ ` concept-graph/ ` -- 以 ` generateConcepts ` 的结果为基础, 生成有向图
6
8
7
- - 以 concept 之间的蕴含关系为 有向边
9
+ - 以 concept 之间的蕴含关系为有向边 -- 方向就是蕴含关系的方向
10
+ - 可能的 API:
11
+ - ` LatticeLayout `
12
+ - ` layoutLattice(context) `
8
13
9
14
找出 lattice 中的所有最长 chain -- 为计算 rank 做准备
10
15
15
20
- 注意,我们要对所有点找到最长的 chain,
16
21
最好能一起找,而不只是一个点一个点的找。
17
22
18
- # lattice layout
19
-
20
- ` LatticeLayout `
21
- ` layoutLattice(context) `
22
-
23
- # optimize
24
-
25
- ` Context ` -- use double index ` entityAttributeIndex ` and ` attributeEntityIndex `
26
-
27
23
# editing context
28
24
29
25
如何处理对 context 的修改?
Original file line number Diff line number Diff line change @@ -4,5 +4,6 @@ export type Attribute = string
4
4
export type Context = Readonly < {
5
5
entities : ReadonlySet < Entity >
6
6
attributes : ReadonlySet < Attribute >
7
- entityAttributeIndex : ReadonlyMap < string , ReadonlySet < string > >
7
+ entityAttributeIndex : ReadonlyMap < Entity , ReadonlySet < Attribute > >
8
+ attributeEntityIndex : ReadonlyMap < Attribute , ReadonlySet < Entity > >
8
9
} >
Original file line number Diff line number Diff line change @@ -4,10 +4,5 @@ export function attributesOf(
4
4
context : Context ,
5
5
entity : Entity ,
6
6
) : ReadonlySet < Attribute > {
7
- const attributes = context . entityAttributeIndex . get ( entity )
8
- if ( attributes === undefined ) {
9
- return new Set ( )
10
- }
11
-
12
- return attributes
7
+ return context . entityAttributeIndex . get ( entity ) || new Set ( )
13
8
}
Original file line number Diff line number Diff line change @@ -6,13 +6,24 @@ export function createContextFromCrossTable(table: CrossTable): Context {
6
6
entities : new Set < Entity > ( ) ,
7
7
attributes : new Set < Attribute > ( ) ,
8
8
entityAttributeIndex : new Map < Entity , Set < Attribute > > ( ) ,
9
+ attributeEntityIndex : new Map < Attribute , Set < Entity > > ( ) ,
9
10
}
10
11
11
12
context . entities = new Set ( Object . keys ( table ) )
12
13
context . attributes = new Set ( Object . values ( table ) . flat ( ) )
14
+
13
15
for ( const [ entity , attributes ] of Object . entries ( table ) ) {
14
16
context . entityAttributeIndex . set ( entity , new Set ( attributes ) )
15
17
}
16
18
19
+ for ( const [ entity , attributes ] of Object . entries ( table ) ) {
20
+ for ( const attribute of attributes ) {
21
+ let entities = context . attributeEntityIndex . get ( attribute )
22
+ if ( entities === undefined ) entities = new Set ( )
23
+ context . attributeEntityIndex . set ( attribute , entities )
24
+ entities . add ( entity )
25
+ }
26
+ }
27
+
17
28
return Object . freeze ( context )
18
29
}
Original file line number Diff line number Diff line change 1
1
import type { Attribute , Context , Entity } from "./Context.js"
2
- import { attributesOf } from "./attributesOf.js"
3
2
4
3
export function entitiesOf (
5
4
context : Context ,
6
5
attribute : Attribute ,
7
6
) : ReadonlySet < Entity > {
8
- const entities = new Set < Entity > ( )
9
- for ( const entity of context . entities ) {
10
- if ( attributesOf ( context , entity ) . has ( attribute ) ) {
11
- entities . add ( entity )
12
- }
13
- }
14
-
15
- return entities
7
+ return context . attributeEntityIndex . get ( attribute ) || new Set ( )
16
8
}
You can’t perform that action at this time.
0 commit comments