UNPKG

@harmoniclabs/plu-ts-onchain

Version:

An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript

47 lines (46 loc) 1.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.lowestCommonAncestor = exports.getDepthInMaxScope = void 0; var isIRTerm_1 = require("../../utils/isIRTerm.js"); // type IRWithDepth = IRTerm & { depth: number } function getDepthInMaxScope(term, maxScope) { var depth = 0; while (term.parent !== maxScope) { depth++; if (term.parent === undefined) return undefined; term = term.parent; } return depth; } exports.getDepthInMaxScope = getDepthInMaxScope; function lowestCommonAncestor(n1, n2, maxScope) { if (!(0, isIRTerm_1.isIRTerm)(n1) || !(0, isIRTerm_1.isIRTerm)(n2)) return undefined; var d1 = getDepthInMaxScope(n1, maxScope); var d2 = getDepthInMaxScope(n2, maxScope); if (d1 === undefined || d2 === undefined) { return undefined; } var diff = d1 - d2; // If node b is deeper, swap node a and node b if (diff < 0) { var temp = n1; n1 = n2; n2 = temp; diff = -diff; } // Move n1 up until it reaches the same level as n2 while (diff-- > 0 && n1.parent) n1 = n1.parent; // Now n1 and n2 are at same levels while (n1 && n2) { if (n1 === n2) return n1; // undefined will exit the loop n1 = n1.parent; n2 = n2.parent; } return undefined; } exports.lowestCommonAncestor = lowestCommonAncestor;