1 19 package bak.pcj.set; 20 21 import bak.pcj.BooleanIterator; 22 import bak.pcj.BooleanCollection; 23 import bak.pcj.util.Exceptions; 24 import java.util.NoSuchElementException ; 25 26 35 public class BooleanDirectSet extends AbstractBooleanSet { 36 37 private int values; 38 39 private static final int EMPTY = 0; 40 private static final int FALSE = 1; 41 private static final int TRUE = 2; 42 private static final int FALSETRUE = 3; 43 44 47 public BooleanDirectSet() { 48 values = EMPTY; 49 } 50 51 62 public BooleanDirectSet(BooleanCollection c) { 63 this(); 64 addAll(c); 65 } 66 67 private void removeError() { 68 Exceptions.noElementToRemove(); 69 } 70 71 private void nextError() { 72 Exceptions.endOfIterator(); 73 } 74 75 public boolean contains(boolean v) { 76 switch (values) { 77 case EMPTY: return false; 78 case FALSE: return !v; 79 case TRUE: return v; 80 case FALSETRUE: return true; 81 default: throw new RuntimeException ("Internal error"); 82 } 83 } 84 85 public boolean add(boolean v) { 86 switch (values) { 87 case EMPTY: if (v) values = TRUE; else values = FALSE; return true; 88 case FALSE: if (v) { values = FALSETRUE; return true; } return false; 89 case TRUE: if (!v) { values = FALSETRUE; return true; } return false; 90 case FALSETRUE: return false; 91 default: throw new RuntimeException ("Internal error"); 92 } 93 } 94 95 public boolean remove(boolean v) { 96 switch (values) { 97 case EMPTY: return false; 98 case FALSE: if (!v) { values=EMPTY; return true; } return false; 99 case TRUE: if (v) { values=EMPTY; return true; } return false; 100 case FALSETRUE: if (v) values=FALSE; else values=TRUE; return true; 101 default: throw new RuntimeException ("Internal error"); 102 } 103 } 104 105 public int size() { 106 switch (values) { 107 case EMPTY: return 0; 108 case FALSE: return 1; 109 case TRUE: return 1; 110 case FALSETRUE: return 2; 111 default: throw new RuntimeException ("Internal error"); 112 } 113 } 114 115 public boolean isEmpty() 116 { return values == EMPTY; } 117 118 public void clear() 119 { values = EMPTY; } 120 121 public BooleanIterator iterator() { 122 return new BIterator(); 123 } 124 125 private class BIterator implements BooleanIterator { 126 private static final int I_EMPTY = 0; 127 private static final int I_F_0 = 1; private static final int I_F_1 = 2; private static final int I_F_2 = 3; private static final int I_T_0 = 4; private static final int I_T_1 = 5; private static final int I_T_2 = 6; private static final int I_FT_0 = 7; private static final int I_FT_1 = 8; private static final int I_FT_2 = 9; private static final int I_FT_3 = 10; private static final int I_FT_4 = 11; private static final int I_FT_5 = 12; private static final int I_FT_6 = 13; 141 private int state; 142 143 BIterator() { 144 switch (values) { 145 case EMPTY: state = I_EMPTY; break; 146 case FALSE: state = I_F_0; break; 147 case TRUE: state = I_T_0; break; 148 case FALSETRUE: state = I_FT_0; break; 149 default: throw new RuntimeException ("Internal error"); 150 } 151 } 152 153 public boolean hasNext() { 154 switch (state) { 155 case I_EMPTY: return false; 156 case I_F_0: return true; 157 case I_F_1: return false; 158 case I_F_2: return false; 159 case I_T_0: return true; 160 case I_T_1: return false; 161 case I_T_2: return false; 162 case I_FT_0: return true; 163 case I_FT_1: return true; 164 case I_FT_2: return true; 165 case I_FT_3: return false; 166 case I_FT_4: return false; 167 case I_FT_5: return false; 168 case I_FT_6: return false; 169 default: throw new RuntimeException ("Internal error"); 170 } 171 } 172 173 public boolean next() { 174 switch (state) { 175 case I_EMPTY: 176 nextError(); 177 case I_F_0: 178 state = I_F_1; 179 return false; 180 case I_F_1: 181 nextError(); 182 case I_F_2: 183 nextError(); 184 case I_T_0: 185 state = I_T_1; 186 return true; 187 case I_T_1: 188 nextError(); 189 case I_T_2: 190 nextError(); 191 case I_FT_0: 192 state = I_FT_1; 193 return false; 194 case I_FT_1: 195 state = I_FT_3; 196 return true; 197 case I_FT_2: 198 state = I_FT_5; 199 return true; 200 case I_FT_3: 201 nextError(); 202 case I_FT_4: 203 nextError(); 204 case I_FT_5: 205 nextError(); 206 case I_FT_6: 207 nextError(); 208 default: 209 throw new RuntimeException ("Internal error"); 210 } 211 } 212 213 public void remove() { 214 switch (state) { 215 case I_EMPTY: 216 removeError(); 217 case I_F_0: 218 removeError(); 219 case I_F_1: 220 values = EMPTY; 221 state = I_F_2; 222 break; 223 case I_F_2: 224 removeError(); 225 case I_T_0: 226 removeError(); 227 case I_T_1: 228 values = EMPTY; 229 state = I_T_2; 230 break; 231 case I_T_2: 232 removeError(); 233 case I_FT_0: 234 removeError(); 235 case I_FT_1: 236 values = TRUE; 237 state = I_FT_2; 238 break; 239 case I_FT_2: 240 removeError(); 241 case I_FT_3: 242 values = FALSE; 243 state = I_FT_4; 244 break; 245 case I_FT_4: 246 removeError(); 247 case I_FT_5: 248 values = EMPTY; 249 state = I_FT_6; 250 break; 251 case I_FT_6: 252 removeError(); 253 default: 254 throw new RuntimeException ("Internal error"); 255 } 256 } 257 } 258 259 } 260 | Popular Tags |