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.
19 package gov.nasa.jpf.util;
21 import java.util.Iterator;
22 import java.util.NoSuchElementException;
25 * a generic set of comparable objects with value based inclusion check
26 * (i.e. using equals())
27 * objects that return compareTo() == 0 are entered LIFO
29 public class SortedArrayObjectSet<T extends Comparable<T>> implements Iterable<T> {
31 static final int DEFAULT_CAPACITY = 8;
32 static final int GROWTH = 8;
34 class StorageIterator implements Iterator<T>{
38 public boolean hasNext() {
45 return (T)elements[next++];
47 throw new NoSuchElementException();
52 public void remove() {
56 System.arraycopy(elements, next, elements, idx, size-idx);
66 Comparable<T>[] elements;
70 // returns the index where the match should be
71 // caller has to make sure size > 0
72 protected final int bisect (T val){
75 Comparable<T>[] a = elements;
78 int mid = (min + max) / 2;
80 if (a[mid].compareTo(val) < 0) {
91 // if we already have elements, idx has to be within range
92 protected final void insertElement (int idx){
93 if (elements == null){
94 elements = new Comparable[DEFAULT_CAPACITY];
97 Comparable[] a = elements;
99 if (size == a.length){
100 int newLength = a.length + GROWTH;
101 Comparable[] newElements = new Comparable[newLength];
103 System.arraycopy(a, 0, newElements, 0, idx);
106 System.arraycopy(a, idx, newElements, idx+1, size-idx);
108 elements = newElements;
111 System.arraycopy(a, idx, a, idx+1, size-idx);
119 public SortedArrayObjectSet (){
123 public SortedArrayObjectSet (int initialCapacity){
124 elements = new Comparable[initialCapacity];
131 public boolean isEmpty(){
135 public boolean contains (T v) {
136 return ((size > 0) && elements[bisect(v)].equals(v));
139 public void add (T v){
141 elements = new Comparable[DEFAULT_CAPACITY];
147 Comparable<T> e = elements[i];
149 if (e.compareTo(v) < 0){
160 public void remove (T v) {
164 Comparable<T>[] a = elements;
166 for (int i = bisect(v); i<size && a[i].compareTo(v) == 0; i++) {
175 System.arraycopy(a, i + 1, a, i, (len - i));
187 public Iterator<T> iterator(){
188 return new StorageIterator();
192 public String toString (){
194 StringBuilder sb = new StringBuilder("{");
196 for (int i=0; i<size; i++){
200 sb.append(elements[i]);
204 return sb.toString();