Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

245
LINES

< > BotCompany Repo | #1029230 // SuffixTree_managed [compact version without object headers]

JavaX fragment (include) [tags: use-pretranspiled]

Uses 11335K of libraries. Click here for Pure Java version (15704L/107K).

sclass SuffixTree_managed implements IManagedObject, AutoCloseable {
  replace Addr with int.
  
  new ManagedIntObjects_v1 mem;
  Node root;
  CharSequence fullText;
  int nodeCount;
  Cl<AutoCloseable> dependentCloseables;
  
  Comparator<Node> childComparator = (a, b) -> a.firstCharOrMinus1(this)-b.firstCharOrMinus1(this);
  
  class Node extends AbstractManagedObject {
    // children is a pointer to an int "array" (length field + ints)
    final static int ofs_from = 0, ofs_to = 1, ofs_children = 2;
    final static int objectSize = ofs_children+1;
    
    *(Addr addr) { super(addr); }
    
    // create new object
    *() {
      addr = mem.alloc(objectSize);
    }
    
    // create new object
    *(int from, int to) {
      this();
      from(from);
      to(to);
    }
    
    // create new object
    *(Substring s) {
      this(s.startIndex(), s.endIndex());
    }
    
    // fields & GC handling
    
    int from() { ret mem.get(addr+ofs_from); }
    int to() { ret mem.get(addr+ofs_to); }
    Addr children_ptr() { ret mem.get(addr+ofs_children); }
    L<Node> children() {
      ret convertListElementsBothWays(
        n -> new Node(n),
        n -> n.addr,
        mem.pointerArray(children_ptr()));
    }
    
    void from(int from) { mem.set(addr+ofs_from, from); }
    void to(int to) { mem.set(addr+ofs_to, to); }
    void children(Addr addr) { mem.set(this.addr+ofs_children, addr); }
    
    public void scanForCollection(IManagedObjectCollector gc) {
      gc.noteObject(addr, objectSize, this == root ? newAddr -> { addr = newAddr; } : null);
      gc.notePointer(addr+ofs_children);
      gc.notePointerArray(children_ptr());
      for (Node n : children())
        n.scanForCollection(gc);
    }
    
    // other methods
    
    Substring text(SuffixTree_managed tree) { ret Substring(tree.fullText, from(), to()); }
    
    int lText() { ret to()-from(); }
    
    bool isTerminal(SuffixTree_managed tree) { ret to() == l(tree.fullText); }
    
    void addChild(SuffixTree_managed tree, Node n) {
      Addr oldChildren = children_ptr();
      int i = l(children());
      //print(children_ptr := children_ptr() + ", l=" + l(children()));
      children(mem.resizePointerArray(oldChildren, i+1);
      //print(children_ptr := children_ptr() + ", l=" + l(children()));
      mem.freePointerArray(oldChildren);
      children().set(i, n);
      sortInPlace(children(), tree.childComparator);
    }
    
    public int firstCharOrMinus1(SuffixTree_managed tree) {
      ret charToIntOrMinus1(text(tree));
    }
    
    Node getChild(SuffixTree_managed tree, int c) {
      L<Node> children = children();
      int i = generalizedBinarySearch2(children, n -> cmp(n.firstCharOrMinus1(tree), c));
      ret i >= 0 ? children.get(i) : null;
    }
    
    toString { ret "Node@" + addr + ", " + from() + "/" + lText() + ". " + nChildren(children()); }
  }

  *() {}
  *(S *fullText) {
    process(fullText);
  }
  
  // load
  *(IIntMemory mem, int root) {
    this.mem = new ManagedIntObjects_v1_virtual(mem);
    this.root = new Node(root);
  }
  
  void process(S fullText) {
    this.fullText = fullText;
    root = new Node(0, 0);
    ++nodeCount;
    for i over fullText: {
      addSuffix(Substring(fullText, i));
      if (((i+1) % 1000000) == 0) {
        print((i+1) + " suffixes added (" + nNodes(nodeCount) + ")");
        mem.printStats();
      }
    }
  }

  void addSuffix(Substring s) {
    Node node = root;
    
    while (!empty(s)) {
      int _n = lCommonPrefix_CharSequence(node.text(SuffixTree_managed.this), s);
      s = s.substring(_n);
      if (_n >= node.lText()) { // node text exhausted
        if (empty(s)) { // pattern also exhausted - done
          //print("Exhausted: " + node.from() + "/" + node.to());
          // add a new termination node
          Node nNew = new Node(s);
          ++nodeCount;
          node.addChild(SuffixTree_managed.this, nNew);
          ret;
        } else {
          Node n = node.getChild(SuffixTree_managed.this, charToIntOrMinus1(s));
          if (n == null) {
            n = new Node(s);
            ++nodeCount;
            node.addChild(SuffixTree_managed.this, n);
            ret;
          } else
            node = n;
        }
      } else { // node text not exhausted
        // split node. first, move all the node's vitals to a new node nOld
        Node nOld = new Node(node.from()+_n, node.to());
        ++nodeCount;
        nOld.children(node.children_ptr());
        node.children(mem.nullPtr());
        node.to(node.from()+_n);
        node.addChild(SuffixTree_managed.this, nOld);

        // now add a new node
        Node nNew = new Node(s);
        ++nodeCount;
        node.addChild(SuffixTree_managed.this, nNew);
        ret;
      }
    }
  }

  public L<Int> indicesOf(S pattern) {
    ret asList(indicesOf_iterator(pattern));
  }

  public ItIt<Int> indicesOf_iterator(S pattern) {
    ret mapI_notNull(allNodesUnder(scanDown(root, pattern)),
      nad -> nad.node.isTerminal(this) ? nad.position() : null);
  }
  
  srecord NodeAndDepth(Node node, int depth) {
    int position() {
      int position = node.from()-depth;
      //print("from=" + node.from() + ", to=" + node.to() + ", depth=" + depth + ", position=" + position);
      ret position;
    }
    
    Cl<NodeAndDepth> children() {
      ret lmap wrapChild(node.children());
    }
    
    NodeAndDepth wrapChild(Node n) {
      ret n == null ? null : new NodeAndDepth(n, depth+node.lText());
    }
    
    NodeAndDepth getChild(SuffixTree_managed tree, int c) {
      ret wrapChild(node.getChild(tree, c));
    }
  }

  NodeAndDepth scanDown(Node node, S pattern) {
    int lPattern = l(pattern), iPattern = 0;
    NodeAndDepth nad = new(node, 0);
    while true {
      int n = lCommonPrefix_CharSequence(nad.node.text(this), Substring(pattern, iPattern));
      iPattern += n;
      if (iPattern >= lPattern) break; // pattern exhausted - done
      if (n < nad.node.lText()) null; // mismatch, exit
      NodeAndDepth child = nad.getChild(SuffixTree_managed.this, charAtAsIntOrMinus1(pattern, iPattern));
      if (child != null) continue with nad = child;
      null;
    }
    
    ret nad;
  }
  
  void printMe() {
    printNode("", "", new NodeAndDepth(root, 0));
  }
  
  void printNode(S indent, S pre, NodeAndDepth nad) {
    print(indent + pre + quote(shorten(20, nad.node.text(this))) + (!nad.node.isTerminal(this) ? "" : " [" + nad.position() + "]"));
    fOr (NodeAndDepth n : nad.children()) {
      printNode(indent + "  ", "[" + (n.node.lText() == 0 ? "end" : quote(n.node.text(this).charAt(0))) + "] ", n);
    }
  }
  
  ItIt<NodeAndDepth> allNodes() {
    ret allNodesUnder(new NodeAndDepth(root, 0));
  }
  
  // includes the node itself
  ItIt<NodeAndDepth> allNodesUnder(NodeAndDepth nad) {
    new L<Iterator<NodeAndDepth>> stack;
    if (nad != null)
      stack.add(iteratorLL(nad));
    ret iteratorFromFunction_if0(() -> {
      while (nempty(stack)) {
        if (!last(stack).hasNext())
          popLast(stack);
        else {
          NodeAndDepth n = last(stack).next();
          stack.add((Iterator) iterator(n.children()));
          ret n;
        }
      }
      null;
    });
  }
  
  public void scanForCollection(IManagedObjectCollector gc) {
    if (root == null) ret;
    root.scanForCollection(gc);
  }
  
  public void close() {
    closeAll(dependentCloseables);
  }
}

Author comment

Began life as a copy of #1029202

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029230
Snippet name: SuffixTree_managed [compact version without object headers]
Eternal ID of this version: #1029230/51
Text MD5: ddff76e4b37841f09d8cf1d94f24c194
Transpilation MD5: 34bb38366c54dfe113cc2de4289aceac
Author: stefan
Category: javax / text searching
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-27 18:14:59
Source code size: 7673 bytes / 245 lines
Pitched / IR pitched: No / No
Views / Downloads: 332 / 805
Version history: 50 change(s)
Referenced in: #1029263 - IntSuffixTree_managed [use ints instead of chars]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)