{"version":3,"file":"dag-map.umd.js","sources":["dag-map.ts"],"sourcesContent":["export interface Callback<T> {\n (key: string, value: T | undefined): void;\n}\n\nexport type MaybeStringOrArray = string | string[] | undefined;\n\n/**\n * A topologically ordered map of key/value pairs with a simple API for adding constraints.\n *\n * Edges can forward reference keys that have not been added yet (the forward reference will\n * map the key to undefined).\n */\nexport default class DAG<T> {\n private _vertices = new Vertices<T>();\n\n /**\n * Adds a key/value pair with dependencies on other key/value pairs.\n *\n * @public\n * @param key The key of the vertex to be added.\n * @param value The value of that vertex.\n * @param before A key or array of keys of the vertices that must\n * be visited before this vertex.\n * @param after An string or array of strings with the keys of the\n * vertices that must be after this vertex is visited.\n */\n public add(key: string, value: T | undefined, before?: MaybeStringOrArray, after?: MaybeStringOrArray) {\n if (!key) throw new Error('argument `key` is required');\n let vertices = this._vertices;\n let v = vertices.add(key);\n v.val = value;\n if (before) {\n if (typeof before === \"string\") {\n vertices.addEdge(v, vertices.add(before));\n } else {\n for (let i = 0; i < before.length; i++) {\n vertices.addEdge(v, vertices.add(before[i]));\n }\n }\n }\n if (after) {\n if (typeof after === \"string\") {\n vertices.addEdge(vertices.add(after), v);\n } else {\n for (let i = 0; i < after.length; i++) {\n vertices.addEdge(vertices.add(after[i]), v);\n }\n }\n }\n }\n\n /**\n * @deprecated please use add.\n */\n public addEdges(key: string, value: T | undefined, before?: MaybeStringOrArray, after?: MaybeStringOrArray) {\n this.add(key, value, before, after);\n }\n\n /**\n * Visits key/value pairs in topological order.\n *\n * @public\n * @param callback The function to be invoked with each key/value.\n */\n public each(callback: Callback<T>) {\n this._vertices.walk(callback);\n }\n\n /**\n * @deprecated please use each.\n */\n public topsort(callback: Callback<T>) {\n this.each(callback);\n }\n}\n\n\n/** @private */\nclass Vertices<T> {\n [index: number]: Vertex<T>;\n length = 0;\n\n private stack: IntStack = new IntStack();\n private path: IntStack = new IntStack();\n private result: IntStack = new IntStack();\n\n public add(key: string): Vertex<T> {\n if (!key) throw new Error(\"missing key\");\n let l = this.length | 0;\n let vertex: Vertex<T>;\n for (let i = 0; i < l; i++) {\n vertex = this[i];\n if (vertex.key === key) return vertex;\n }\n this.length = l + 1;\n return this[l] = {\n idx: l,\n key: key,\n val: undefined,\n out: false,\n flag: false,\n length: 0\n };\n }\n\n public addEdge(v: Vertex<T>, w: Vertex<T>): void {\n this.check(v, w.key);\n let l = w.length | 0;\n for (let i = 0; i < l; i++) {\n if (w[i] === v.idx) return;\n }\n w.length = l + 1;\n w[l] = v.idx;\n v.out = true;\n }\n\n public walk(cb: Callback<T>): void {\n this.reset();\n for (let i = 0; i < this.length; i++) {\n let vertex = this[i];\n if (vertex.out) continue;\n this.visit(vertex, \"\");\n }\n this.each(this.result, cb);\n }\n\n private check(v: Vertex<T>, w: string): void {\n if (v.key === w) {\n throw new Error(\"cycle detected: \" + w + \" <- \" + w);\n }\n // quick check\n if (v.length === 0) return;\n // shallow check\n for (let i = 0; i < v.length; i++) {\n let key = this[v[i]].key;\n if (key === w) {\n throw new Error(\"cycle detected: \" + w + \" <- \" + v.key + \" <- \" + w);\n }\n }\n // deep check\n this.reset();\n this.visit(v, w);\n if (this.path.length > 0) {\n let msg = \"cycle detected: \" + w;\n this.each(this.path, (key) => {\n msg += \" <- \" + key;\n });\n throw new Error(msg);\n }\n }\n\n private reset(): void {\n this.stack.length = 0;\n this.path.length = 0;\n this.result.length = 0;\n for (let i = 0, l = this.length; i < l; i++) {\n this[i].flag = false;\n }\n }\n\n private visit(start: Vertex<T>, search: string): void {\n let { stack, path, result } = this;\n stack.push(start.idx);\n while (stack.length) {\n let index = stack.pop() | 0;\n if (index >= 0) {\n // enter\n let vertex = this[index];\n if (vertex.flag) continue;\n vertex.flag = true;\n path.push(index);\n if (search === vertex.key) break;\n // push exit\n stack.push(~index);\n this.pushIncoming(vertex);\n } else {\n // exit\n path.pop();\n result.push(~index);\n }\n }\n }\n\n private pushIncoming(incomming: ArrayLike<number>): void {\n let { stack } = this;\n for (let i = incomming.length - 1; i >= 0; i--) {\n let index = incomming[i];\n if (!this[index].flag) {\n stack.push(index);\n }\n }\n }\n\n private each(indices: IntStack, cb: Callback<T>): void {\n for (let i = 0, l = indices.length; i < l; i++) {\n let vertex = this[indices[i]];\n cb(vertex.key, vertex.val);\n }\n }\n}\n\n/** @private */\ninterface Vertex<T> {\n idx: number;\n key: string;\n val: T | undefined;\n out: boolean;\n flag: boolean;\n [index: number]: number;\n length: number;\n}\n\n/** @private */\nclass IntStack {\n [index: number]: number;\n public length = 0;\n\n push(n: number) {\n this[this.length++] = n | 0;\n }\n\n pop() {\n return this[--this.length] | 0;\n }\n}\n"],"names":[],"mappings":";;;;;;AAMA;;;;;;AAMA,IAAA,GAAA,GAAA,CAAA,YAAA;IAAA,SAAA,GAAA,GAAA;QACU,IAAV,CAAA,SAAmB,GAAG,IAAI,QAAQ,EAAK,CAAC;KA6DvC;;;;;;;;;;;;IAhDQ,GAAT,CAAA,SAAA,CAAA,GAAY,GAAV,UAAW,GAAW,EAAE,KAAoB,EAAE,MAA2B,EAAE,KAA0B,EAAvG;QACI,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;QACxD,IAAI,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC;QAC9B,IAAI,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC1B,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC;QACd,IAAI,MAAM,EAAE;YACV,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;gBAC9B,QAAQ,CAAC,OAAO,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;aAC3C;iBAAM;gBACL,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;oBACtC,QAAQ,CAAC,OAAO,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,MAAM,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