-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 抽取 container 中拓扑排序部分到 utils/graph
- Loading branch information
Showing
14 changed files
with
221 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export function includes<T> (arr: T[], val: T): boolean { | ||
for (const item of arr) if (item === val) return true; | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Map, decrease, increase } from './map'; | ||
|
||
export class Graph<T> { | ||
private inDegrees = new Map<T, number>() | ||
private adj = new Map<T, T[]>() | ||
// 添加边 | ||
addEdge (fr: T, to: T) { | ||
const edges = this.adj.get(fr) || []; | ||
edges.push(to); | ||
this.adj.set(fr, edges); | ||
increase(this.inDegrees, to); | ||
} | ||
addVertex (v: T) { | ||
if (!this.adj.has(v)) { | ||
this.adj.set(v, []); | ||
} | ||
} | ||
// 按照拓扑序移除所有节点 | ||
popAll () { | ||
const free = []; | ||
const ordered = []; | ||
for (const key of this.adj.keys()) { | ||
if (!this.inDegrees.get(key)) free.push(key); | ||
} | ||
while (free.length) { | ||
const fr = free.shift(); | ||
ordered.push(fr); | ||
for (const to of this.adj.get(fr) || []) { | ||
const degree = decrease(this.inDegrees, to); | ||
if (!degree) free.push(to); | ||
} | ||
} | ||
this.inDegrees.clear(); | ||
this.adj.clear(); | ||
return ordered; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,55 @@ | ||
import { includes } from './array'; | ||
|
||
/** | ||
* ES6 Map 的部分实现 | ||
*/ | ||
export class Map { | ||
private _keys: any[] = [] | ||
private _values: any[] = [] | ||
|
||
set (key: any, value: any) { | ||
for (let i = 0; i < this._keys.length; i++) { | ||
if (this._keys[i] === key) { | ||
this._values[i] = value; | ||
return; | ||
} | ||
} | ||
this._keys.push(key); | ||
this._values.push(value); | ||
} | ||
|
||
get (key) { | ||
for (let i = 0; i < this._keys.length; i++) { | ||
if (this._keys[i] === key) return this._values[i]; | ||
} | ||
return null; | ||
} | ||
|
||
clear () { | ||
this._keys = []; | ||
this._values = []; | ||
} | ||
|
||
keys (callbackFn) { | ||
return this._keys.forEach(callbackFn); | ||
} | ||
export class Map<KeyType, ValueType> { | ||
private _keys: KeyType[] = [] | ||
private _values: ValueType[] = [] | ||
|
||
set (key: KeyType, value: ValueType) { | ||
for (let i = 0; i < this._keys.length; i++) { | ||
if (this._keys[i] === key) { | ||
this._values[i] = value; | ||
return; | ||
} | ||
} | ||
this._keys.push(key); | ||
this._values.push(value); | ||
} | ||
|
||
get (key: KeyType): ValueType | undefined { | ||
for (let i = 0; i < this._keys.length; i++) { | ||
if (this._keys[i] === key) return this._values[i]; | ||
} | ||
} | ||
|
||
has (key: KeyType): boolean { | ||
return includes(this._keys, key); | ||
} | ||
|
||
clear () { | ||
this._keys = []; | ||
this._values = []; | ||
} | ||
|
||
keys () { | ||
return this._keys; | ||
} | ||
|
||
values () { | ||
return this._values; | ||
} | ||
} | ||
|
||
export function increase<T> (map: Map<T, any>, key: T) { | ||
const degree = (map.get(key) || 0) + 1; | ||
map.set(key, degree); | ||
return degree; | ||
} | ||
|
||
export function decrease<T> (map: Map<T, any>, key: T) { | ||
const degree = (map.get(key) || 0) - 1; | ||
map.set(key, degree); | ||
return degree; | ||
} |
Oops, something went wrong.