| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public Iterator<IndexValue> getNonZeroIterator(final int start) |
| 254 | { |
| 255 | if(!isSparse())//dense case, just add the shift and use the base implemenaton since its going to do the exact same thing I would |
| 256 | return super.getNonZeroIterator(start); |
| 257 | final Iterator<IndexValue> baseIter = base.getNonZeroIterator(start); |
| 258 | if(shift == 0)//easy case, just use the base's iterator |
| 259 | return baseIter; |
| 260 | |
| 261 | //ugly case, sparse vec with shifted values iterating over non zeros (which should generally be all of them) |
| 262 | final int lastIndx = length()-1; |
| 263 | |
| 264 | return new Iterator<IndexValue>() |
| 265 | { |
| 266 | IndexValue nextBaseVal; |
| 267 | IndexValue nextVal; |
| 268 | IndexValue toRet = null; |
| 269 | |
| 270 | //init |
| 271 | { |
| 272 | |
| 273 | for(int effectiveStart = start; effectiveStart <= lastIndx; effectiveStart++) |
| 274 | { |
| 275 | nextBaseVal = baseIter.hasNext() ? baseIter.next() : null; |
| 276 | if (nextBaseVal != null && nextBaseVal.getIndex() == effectiveStart) |
| 277 | { |
| 278 | if (nextBaseVal.getValue() + shift == 0) |
| 279 | continue;//no starting on zero! |
| 280 | else |
| 281 | nextVal = new IndexValue(effectiveStart, nextBaseVal.getValue() + shift); |
| 282 | nextBaseVal = baseIter.hasNext() ? baseIter.next() : null; |
| 283 | } |
| 284 | else//was zero + shift |
| 285 | nextVal = new IndexValue(effectiveStart, shift); |
| 286 | toRet = new IndexValue(effectiveStart, shift); |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public boolean hasNext() |
| 293 | { |
| 294 | return nextVal != null; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | |
| 299 | @Override |
| 300 | public IndexValue next() |
| 301 | { |
| 302 | toRet.setIndex(nextVal.getIndex()); |
| 303 | toRet.setValue(nextVal.getValue()); |
| 304 | |
| 305 | //loop to get next value b/c we may have to skip over zeros |
| 306 | do |
| 307 | { |
| 308 | nextVal.setIndex(nextVal.getIndex()+1);//pre-bump index |
| 309 | //prep next value |