1 /*
   2  * Permission is hereby granted, free of charge, to any person obtaining a copy of
   3  * this software and associated documentation files (the "Software"), to deal in
   4  * the Software without restriction, including without limitation the rights to
   5  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
   6  * of the Software, and to permit persons to whom the Software is furnished to do
   7  * so, subject to the following conditions:
   8  *
   9  * The above copyright notice and this permission notice shall be included in all
  10  * copies or substantial portions of the Software.
  11  *
  12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18  * SOFTWARE.
  19  */
  20 package jdk.nashorn.internal.joni;
  21 
  22 public class CaptureTreeNode {
  23 
  24 
  25     int group;
  26     int beg;
  27     int end;
  28     // int allocated;
  29     int numChildren;
  30     CaptureTreeNode[]children;
  31 
  32     CaptureTreeNode() {
  33         beg = Region.REGION_NOTPOS;
  34         end = Region.REGION_NOTPOS;
  35         group = -1;
  36     }
  37 
  38     static final int HISTORY_TREE_INIT_ALLOC_SIZE = 8;
  39     void addChild(CaptureTreeNode child) {
  40         if (children == null) {
  41             children = new CaptureTreeNode[HISTORY_TREE_INIT_ALLOC_SIZE];
  42         } else if (numChildren >= children.length) {
  43             CaptureTreeNode[]tmp = new CaptureTreeNode[children.length << 1];
  44             System.arraycopy(children, 0, tmp, 0, children.length);
  45             children = tmp;
  46         }
  47 
  48         children[numChildren] = child;
  49         numChildren++;
  50     }
  51 
  52     void clear() {
  53         for (int i=0; i<numChildren; i++) {
  54             children[i] = null; // ???
  55         }
  56         numChildren = 0;
  57         beg = end = Region.REGION_NOTPOS;
  58         group = -1;
  59     }
  60 
  61     CaptureTreeNode cloneTree() {
  62         CaptureTreeNode clone = new CaptureTreeNode();
  63         clone.beg = beg;
  64         clone.end = end;
  65 
  66         for (int i=0; i<numChildren; i++) {
  67             CaptureTreeNode child = children[i].cloneTree();
  68             clone.addChild(child);
  69         }
  70         return clone;
  71     }
  72 
  73 
  74 }