Skip to content

Commit

Permalink
[optimize] Context -- use double index entityAttributeIndex and `…
Browse files Browse the repository at this point in the history
…attributeEntityIndex`
  • Loading branch information
xieyuheng committed Jul 26, 2024
1 parent 555fe4a commit 8310ce7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
20 changes: 8 additions & 12 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# lattice layout

练习 order book 中计算 concept lattice 的算法。

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

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

- 以 concept 之间的蕴含关系为 有向边
- 以 concept 之间的蕴含关系为有向边 -- 方向就是蕴含关系的方向
- 可能的 API:
- `LatticeLayout`
- `layoutLattice(context)`

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

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

# lattice layout

`LatticeLayout`
`layoutLattice(context)`

# optimize

`Context` -- use double index `entityAttributeIndex` and `attributeEntityIndex`

# editing context

如何处理对 context 的修改?
Expand Down
3 changes: 2 additions & 1 deletion src/context/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export type Attribute = string
export type Context = Readonly<{
entities: ReadonlySet<Entity>
attributes: ReadonlySet<Attribute>
entityAttributeIndex: ReadonlyMap<string, ReadonlySet<string>>
entityAttributeIndex: ReadonlyMap<Entity, ReadonlySet<Attribute>>
attributeEntityIndex: ReadonlyMap<Attribute, ReadonlySet<Entity>>
}>
7 changes: 1 addition & 6 deletions src/context/attributesOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,5 @@ export function attributesOf(
context: Context,
entity: Entity,
): ReadonlySet<Attribute> {
const attributes = context.entityAttributeIndex.get(entity)
if (attributes === undefined) {
return new Set()
}

return attributes
return context.entityAttributeIndex.get(entity) || new Set()
}
Empty file removed src/context/conceptFormat.ts
Empty file.
11 changes: 11 additions & 0 deletions src/context/createContextFromCrossTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ export function createContextFromCrossTable(table: CrossTable): Context {
entities: new Set<Entity>(),
attributes: new Set<Attribute>(),
entityAttributeIndex: new Map<Entity, Set<Attribute>>(),
attributeEntityIndex: new Map<Attribute, Set<Entity>>(),
}

context.entities = new Set(Object.keys(table))
context.attributes = new Set(Object.values(table).flat())

for (const [entity, attributes] of Object.entries(table)) {
context.entityAttributeIndex.set(entity, new Set(attributes))
}

for (const [entity, attributes] of Object.entries(table)) {
for (const attribute of attributes) {
let entities = context.attributeEntityIndex.get(attribute)
if (entities === undefined) entities = new Set()
context.attributeEntityIndex.set(attribute, entities)
entities.add(entity)
}
}

return Object.freeze(context)
}
10 changes: 1 addition & 9 deletions src/context/entitiesOf.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import type { Attribute, Context, Entity } from "./Context.js"
import { attributesOf } from "./attributesOf.js"

export function entitiesOf(
context: Context,
attribute: Attribute,
): ReadonlySet<Entity> {
const entities = new Set<Entity>()
for (const entity of context.entities) {
if (attributesOf(context, entity).has(attribute)) {
entities.add(entity)
}
}

return entities
return context.attributeEntityIndex.get(attribute) || new Set()
}

0 comments on commit 8310ce7

Please sign in to comment.