All files / Sources/Common/DataModel/EdgeLocator index.js

52.38% Statements 11/21
100% Branches 8/8
62.5% Functions 5/8
52.38% Lines 11/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56    6x 6x       2x       10x               10x 10x 10x   6x 6x   10x                                                 6x        
class EdgeLocator {
  constructor(oriented = false) {
    this.oriented = oriented;
    this.edgeMap = new Map();
  }
 
  initialize() {
    this.edgeMap.clear();
  }
 
  computeEdgeKey(pointId0, pointId1) {
    return this.oriented || pointId0 < pointId1
      ? // Cantor pairing function:
        0.5 * (pointId0 * pointId1) * (pointId0 * pointId1 + 1) + pointId1
      : 0.5 * (pointId1 * pointId0) * (pointId1 * pointId0 + 1) + pointId0;
  }
 
  insertUniqueEdge(pointId0, pointId1, newEdgeValue) {
    // Generate a unique key
    const key = this.computeEdgeKey(pointId0, pointId1);
    let node = this.edgeMap.get(key);
    if (!node) {
      // Didn't find key, so add a new edge entry
      node = { key, edgeId: this.edgeMap.size, value: newEdgeValue };
      this.edgeMap.set(key, node);
    }
    return node;
  }
 
  insertEdge(pointId0, pointId1, newEdgeValue) {
    // Generate a unique key
    const key = this.computeEdgeKey(pointId0, pointId1);
    const node = { key, edgeId: this.edgeMap.size, value: newEdgeValue };
    this.edgeMap.set(key, node);
    return node;
  }
 
  isInsertedEdge(pointId0, pointId1) {
    const key = this.computeEdgeKey(pointId0, pointId1);
    return this.edgeMap.get(key);
  }
 
  static getEdgePointIds(node) {
    const n = 0.5 * (-1 + Math.sqrt(8 * node.key + 1));
    const pointId0 = node.key - 0.5 * (n + 1) * n;
    const pointId1 = n - pointId0;
    return [pointId0, pointId1];
  }
}
 
function newInstance(initialValues = {}) {
  return new EdgeLocator(initialValues.oriented);
}
 
export default { newInstance };