@rxflow/manhattan
Version:
Manhattan routing algorithm for ReactFlow - generates orthogonal paths with obstacle avoidance
95 lines (86 loc) • 1.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SortedSet = void 0;
const OPEN = 1;
const CLOSE = 2;
/**
* SortedSet class for managing open and closed sets in A* algorithm
* Maintains items sorted by their values
*/
class SortedSet {
items;
hash;
values;
constructor() {
this.items = [];
this.hash = new Map();
this.values = new Map();
}
/**
* Add an item with its value, maintaining sorted order
*/
add(item, value) {
if (this.hash.get(item)) {
// Item removal - remove from items array
const 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
const index = this.sortedIndexBy(item);
this.items.splice(index, 0, item);
}
/**
* Pop the item with minimum value and mark it as closed
*/
pop() {
const item = this.items.shift();
if (item) {
this.hash.set(item, CLOSE);
}
return item;
}
/**
* Check if item is in open set
*/
isOpen(item) {
return this.hash.get(item) === OPEN;
}
/**
* Check if item is in closed set
*/
isClose(item) {
return this.hash.get(item) === CLOSE;
}
/**
* Check if open set is empty
*/
isEmpty() {
return this.items.length === 0;
}
/**
* Find sorted insertion index for an item
*/
sortedIndexBy(item) {
const value = this.values.get(item);
let low = 0;
let high = this.items.length;
while (low < high) {
const mid = low + high >>> 1;
const midValue = this.values.get(this.items[mid]);
if (midValue < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
}
exports.SortedSet = SortedSet;