2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.util;
20 import java.util.NoSuchElementException;
21 import gov.nasa.jpf.JPFException;
24 * common base for array based IntSet implementations
26 public abstract class ArrayIntSet implements IntSet, Cloneable {
29 protected int[] elements;
31 private class Iterator implements IntIterator {
35 public void remove() {
39 System.arraycopy(elements, next, elements, idx, size-idx);
47 public boolean hasNext() {
54 return elements[next++];
56 throw new NoSuchElementException();
61 protected ArrayIntSet (){
65 protected ArrayIntSet (int initialCapacity){
66 elements = new int[initialCapacity];
70 public boolean isEmpty(){
86 public String toString(){
87 StringBuilder sb = new StringBuilder(/*getClass().getName()*/);
89 for (int i=0; i<size; i++){
93 sb.append(elements[i]);
100 public ArrayIntSet clone(){
102 ArrayIntSet other = (ArrayIntSet) super.clone();
104 if (elements != null) {
105 other.elements = elements.clone();
109 } catch (CloneNotSupportedException cnsx){
110 throw new JPFException("clone failed " + this);
115 * this is probably a bad hash function, but we just need something that
116 * is order independent
119 public int hashCode(){
122 int h = (n << 16) + (n % 3);
124 for (int i = 0; i < n; i++) {
127 e = Integer.MAX_VALUE;
130 h ^= (h << rot) | (h >>> (32 - rot)); // rotate left
137 public boolean equals (Object o){
138 if (o instanceof IntSet){
139 IntSet other = (IntSet)o;
140 if (size == other.size()){
143 for (int i=0; i<len; i++){
144 if (!other.contains(a[i])){
155 public IntIterator intIterator (){
156 return new Iterator();