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;
22 * a set of integers that uses a simple sorted int array and binary search
24 * To be used in a context where
26 * - the number of elements does not have a hard limit
27 * - the number of elements is assumed to be small, but potentially sparse
28 * - the following operations are time critical
32 * + iteration over elements
33 * - adding/removing should be better than O(N)
36 public class SortedArrayIntSet extends ArrayIntSet {
38 static final int DEFAULT_CAPACITY = 8;
39 static final int GROWTH = 8;
43 // returns the index where the match should be
44 // caller has to make sure size > 0
45 protected final int bisect (int val){
51 int mid = (min + max) / 2;
64 // if we already have elements, idx has to be within range
65 protected final void insertElement (int idx){
66 if (elements == null){
67 elements = new int[DEFAULT_CAPACITY];
72 if (size == a.length){
73 int newLength = a.length + GROWTH;
74 int[] newElements = new int[newLength];
76 System.arraycopy(a, 0, newElements, 0, idx);
79 System.arraycopy(a, idx, newElements, idx+1, size-idx);
81 elements = newElements;
84 System.arraycopy(a, idx, a, idx+1, size-idx);
92 public SortedArrayIntSet (){
96 public SortedArrayIntSet (int initialCapacity){
97 super(initialCapacity);
101 public boolean contains (int v) {
102 return ((size > 0) && elements[bisect(v)] == v);
106 public boolean add (int v){
108 elements = new int[DEFAULT_CAPACITY];
127 return false; // was already there
133 public boolean remove (int v) {
147 System.arraycopy(a, i + 1, a, i, (len - i));
156 return false; // wasn't there