UNPKG

@rxflow/manhattan

Version:

Manhattan routing algorithm for ReactFlow - generates orthogonal paths with obstacle avoidance

110 lines (102 loc) 4.1 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var OPEN = 1; var CLOSE = 2; /** * SortedSet class for managing open and closed sets in A* algorithm * Maintains items sorted by their values */ export var SortedSet = /*#__PURE__*/function () { function SortedSet() { _classCallCheck(this, SortedSet); _defineProperty(this, "items", void 0); _defineProperty(this, "hash", void 0); _defineProperty(this, "values", void 0); this.items = []; this.hash = new Map(); this.values = new Map(); } /** * Add an item with its value, maintaining sorted order */ _createClass(SortedSet, [{ key: "add", value: function add(item, value) { if (this.hash.get(item)) { // Item removal - remove from items array var _index = this.items.indexOf(item); if (_index !== -1) { this.items.splice(_index, 1); } } else { this.hash.set(item, OPEN); } this.values.set(item, value); // Find insertion index using binary search var index = this.sortedIndexBy(item); this.items.splice(index, 0, item); } /** * Pop the item with minimum value and mark it as closed */ }, { key: "pop", value: function pop() { var item = this.items.shift(); if (item) { this.hash.set(item, CLOSE); } return item; } /** * Check if item is in open set */ }, { key: "isOpen", value: function isOpen(item) { return this.hash.get(item) === OPEN; } /** * Check if item is in closed set */ }, { key: "isClose", value: function isClose(item) { return this.hash.get(item) === CLOSE; } /** * Check if open set is empty */ }, { key: "isEmpty", value: function isEmpty() { return this.items.length === 0; } /** * Find sorted insertion index for an item */ }, { key: "sortedIndexBy", value: function sortedIndexBy(item) { var value = this.values.get(item); var low = 0; var high = this.items.length; while (low < high) { var mid = low + high >>> 1; var midValue = this.values.get(this.items[mid]); if (midValue < value) { low = mid + 1; } else { high = mid; } } return low; } }]); return SortedSet; }();