comparison src/main/gov/nasa/jpf/util/BitSetN.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
1 /*
2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
4 * All rights reserved.
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0.
11 *
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.
17 */
18
19 package gov.nasa.jpf.util;
20
21 import java.util.BitSet;
22 import java.util.NoSuchElementException;
23
24 /**
25 * a FixedBitSet implementation that is based on java.util.BitSet
26 */
27 public class BitSetN extends BitSet implements FixedBitSet {
28
29 class SetBitIterator implements IntIterator {
30 int cur = 0;
31 int nBits;
32 int cardinality; // <2do> this should be lifted since it makes the iterator brittle
33
34 SetBitIterator (){
35 cardinality = cardinality();
36 }
37
38 @Override
39 public void remove() {
40 if (cur >0){
41 clear(cur-1);
42 }
43 }
44
45 @Override
46 public boolean hasNext() {
47 return nBits < cardinality;
48 }
49
50 @Override
51 public int next() {
52 if (nBits < cardinality){
53 int idx = nextSetBit(cur);
54 if (idx >= 0){
55 nBits++;
56 cur = idx+1;
57 }
58 return idx;
59
60 } else {
61 throw new NoSuchElementException();
62 }
63 }
64 }
65
66
67 public BitSetN (int nBits){
68 super(nBits);
69 }
70
71 @Override
72 public FixedBitSet clone() {
73 return (FixedBitSet) super.clone();
74 }
75
76 @Override
77 public int capacity() {
78 return size();
79 }
80
81
82 @Override
83 public void hash (HashData hd){
84 long[] data = toLongArray();
85 for (int i=0; i<data.length; i++){
86 hd.add(data[i]);
87 }
88 }
89
90
91 //--- IntSet interface
92 @Override
93 public boolean add(int i) {
94 if (get(i)) {
95 return false;
96 } else {
97 set(i);
98 return true;
99 }
100 }
101
102 @Override
103 public boolean remove(int i) {
104 if (get(i)) {
105 clear(i);
106 return true;
107 } else {
108 return false;
109 }
110 }
111
112 @Override
113 public boolean contains(int i) {
114 return get(i);
115 }
116
117 @Override
118 public IntIterator intIterator() {
119 return new SetBitIterator();
120 }
121
122 }