Skip to content

Commit 8310ce7

Browse files
committed
[optimize] Context -- use double index entityAttributeIndex and attributeEntityIndex
1 parent 555fe4a commit 8310ce7

File tree

6 files changed

+23
-28
lines changed

6 files changed

+23
-28
lines changed

TODO.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
# lattice layout
2+
13
练习 order book 中计算 concept lattice 的算法。
24

3-
- 也许可以以这个算法为基础,来给出 lattice 的 layout。
5+
- 以这个算法为基础,来给出 lattice 的 layout。
46

5-
`concept-graph/` -- `generateConcepts` 生成有向图
7+
`concept-graph/` -- `generateConcepts` 的结果为基础,生成有向图
68

7-
- 以 concept 之间的蕴含关系为 有向边
9+
- 以 concept 之间的蕴含关系为有向边 -- 方向就是蕴含关系的方向
10+
- 可能的 API:
11+
- `LatticeLayout`
12+
- `layoutLattice(context)`
813

914
找出 lattice 中的所有最长 chain -- 为计算 rank 做准备
1015

@@ -15,15 +20,6 @@
1520
- 注意,我们要对所有点找到最长的 chain,
1621
最好能一起找,而不只是一个点一个点的找。
1722

18-
# lattice layout
19-
20-
`LatticeLayout`
21-
`layoutLattice(context)`
22-
23-
# optimize
24-
25-
`Context` -- use double index `entityAttributeIndex` and `attributeEntityIndex`
26-
2723
# editing context
2824

2925
如何处理对 context 的修改?

src/context/Context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export type Attribute = string
44
export type Context = Readonly<{
55
entities: ReadonlySet<Entity>
66
attributes: ReadonlySet<Attribute>
7-
entityAttributeIndex: ReadonlyMap<string, ReadonlySet<string>>
7+
entityAttributeIndex: ReadonlyMap<Entity, ReadonlySet<Attribute>>
8+
attributeEntityIndex: ReadonlyMap<Attribute, ReadonlySet<Entity>>
89
}>

src/context/attributesOf.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,5 @@ export function attributesOf(
44
context: Context,
55
entity: Entity,
66
): 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()
138
}

src/context/conceptFormat.ts

Whitespace-only changes.

src/context/createContextFromCrossTable.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ export function createContextFromCrossTable(table: CrossTable): Context {
66
entities: new Set<Entity>(),
77
attributes: new Set<Attribute>(),
88
entityAttributeIndex: new Map<Entity, Set<Attribute>>(),
9+
attributeEntityIndex: new Map<Attribute, Set<Entity>>(),
910
}
1011

1112
context.entities = new Set(Object.keys(table))
1213
context.attributes = new Set(Object.values(table).flat())
14+
1315
for (const [entity, attributes] of Object.entries(table)) {
1416
context.entityAttributeIndex.set(entity, new Set(attributes))
1517
}
1618

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+
1728
return Object.freeze(context)
1829
}

src/context/entitiesOf.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import type { Attribute, Context, Entity } from "./Context.js"
2-
import { attributesOf } from "./attributesOf.js"
32

43
export function entitiesOf(
54
context: Context,
65
attribute: Attribute,
76
): 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()
168
}

0 commit comments

Comments
 (0)