1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.nodes.cfg;
  24 
  25 import java.util.ArrayList;
  26 import java.util.Arrays;
  27 import java.util.List;
  28 
  29 import org.graalvm.compiler.core.common.LocationIdentity;
  30 
  31 public class LocationSet {
  32     private LocationIdentity firstLocation;
  33     private List<LocationIdentity> list;
  34 
  35     public LocationSet() {
  36         list = null;
  37     }
  38 
  39     public LocationSet(LocationSet other) {
  40         this.firstLocation = other.firstLocation;
  41         if (other.list != null && other.list.size() > 0) {
  42             list = new ArrayList<>(other.list);
  43         }
  44     }
  45 
  46     private void initList() {
  47         if (list == null) {
  48             list = new ArrayList<>(4);
  49         }
  50     }
  51 
  52     public boolean isEmpty() {
  53         return firstLocation == null;
  54     }
  55 
  56     public boolean isAny() {
  57         return firstLocation != null && firstLocation.isAny();
  58     }
  59 
  60     public void add(LocationIdentity location) {
  61         if (this.isAny()) {
  62             return;
  63         } else if (location.isAny()) {
  64             firstLocation = location;
  65             list = null;
  66         } else if (location.isImmutable()) {
  67             return;
  68         } else {
  69             assert location.isMutable() && location.isSingle();
  70             if (firstLocation == null) {
  71                 firstLocation = location;
  72             } else if (location.equals(firstLocation)) {
  73                 return;
  74             } else {
  75                 initList();
  76                 for (int i = 0; i < list.size(); ++i) {
  77                     LocationIdentity value = list.get(i);
  78                     if (location.equals(value)) {
  79                         return;
  80                     }
  81                 }
  82                 list.add(location);
  83             }
  84         }
  85     }
  86 
  87     public void addAll(LocationSet other) {
  88         if (other.firstLocation != null) {
  89             add(other.firstLocation);
  90         }
  91         List<LocationIdentity> otherList = other.list;
  92         if (otherList != null) {
  93             for (LocationIdentity l : otherList) {
  94                 add(l);
  95             }
  96         }
  97     }
  98 
  99     public boolean contains(LocationIdentity locationIdentity) {
 100         assert locationIdentity.isSingle();
 101         assert locationIdentity.isMutable();
 102         if (LocationIdentity.any().equals(firstLocation)) {
 103             return true;
 104         }
 105         if (locationIdentity.equals(firstLocation)) {
 106             return true;
 107         }
 108         if (list != null) {
 109             for (int i = 0; i < list.size(); ++i) {
 110                 LocationIdentity value = list.get(i);
 111                 if (locationIdentity.equals(value)) {
 112                     return true;
 113                 }
 114             }
 115         }
 116         return false;
 117     }
 118 
 119     public List<LocationIdentity> getCopyAsList() {
 120         ArrayList<LocationIdentity> result = new ArrayList<>();
 121         if (firstLocation != null) {
 122             result.add(firstLocation);
 123         }
 124         if (list != null) {
 125             result.addAll(list);
 126         }
 127         return result;
 128     }
 129 
 130     @Override
 131     public String toString() {
 132         if (this.isAny()) {
 133             return "ANY";
 134         } else if (this.isEmpty()) {
 135             return "EMPTY";
 136         } else {
 137             List<LocationIdentity> copyAsList = getCopyAsList();
 138             return Arrays.toString(copyAsList.toArray(new LocationIdentity[0]));
 139         }
 140     }
 141 }