Skip to content
Snippets Groups Projects
Select Git revision
  • 951a00c4806c75b4c9cb7316e0272709d71b09e7
  • master default protected
  • rednatco-v2
  • rednatco
  • test
  • ntc-tube-uniform-color
  • ntc-tube-missing-atoms
  • restore-vertex-array-per-program
  • watlas2
  • dnatco_new
  • cleanup-old-nodejs
  • webmmb
  • fix_auth_seq_id
  • update_deps
  • ext_dev
  • ntc_balls
  • nci-2
  • plugin
  • bugfix-0.4.5
  • nci
  • servers
  • v0.5.0-dev.1
  • v0.4.5
  • v0.4.4
  • v0.4.3
  • v0.4.2
  • v0.4.1
  • v0.4.0
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
41 results

linked-index.ts

Blame
  • linked-index.ts 1.36 KiB
    /**
     * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author David Sehnal <david.sehnal@gmail.com>
     */
    
    /** A data structure useful for graph traversal */
    interface LinkedIndex {
        readonly head: number,
        has(i: number): boolean,
        remove(i: number): void
    }
    
    function LinkedIndex(size: number): LinkedIndex {
        return new LinkedIndexImpl(size);
    }
    
    class LinkedIndexImpl implements LinkedIndex {
        private prev: Int32Array;
        private next: Int32Array;
        head: number;
    
        remove(i: number) {
            const { prev, next } = this;
            const p = prev[i], n = next[i];
            if (p >= 0) {
                next[p] = n;
                prev[i] = -1;
            }
            if (n >= 0) {
                prev[n] = p;
                next[i] = -1;
            }
            if (i === this.head) {
                if (p < 0) this.head = n;
                else this.head = p;
            }
        }
    
        has(i: number) {
            return this.prev[i] >= 0 || this.next[i] >= 0 || this.head === i;
        }
    
        constructor(size: number) {
            this.head = size > 0 ? 0 : -1;
            this.prev = new Int32Array(size);
            this.next = new Int32Array(size);
    
            for (let i = 0; i < size; i++) {
                this.next[i] = i + 1;
                this.prev[i] = i - 1;
            }
            this.prev[0] = -1;
            this.next[size - 1] = -1;
        }
    }
    
    export default LinkedIndex;