
src.it.unimi.dsi.law.util.OffsetStepLimitIntIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of law Show documentation
Show all versions of law Show documentation
The software distributed by the Laboratory for Web Algorithmics (LAW).
The newest version!
package it.unimi.dsi.law.util;
/*
* Copyright (C) 2004-2020 Paolo Boldi, Massimo Santini and Sebastiano Vigna
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see .
*
*/
import java.util.NoSuchElementException;
import it.unimi.dsi.fastutil.ints.IntIterator;
/** TODO: doesnt' work
* An {@link it.unimi.dsi.fastutil.ints.IntIterator} that starts at a given offset,
* can be incremented optionally at most a given number of times and every time is increased
* by a given step.
*/
public class OffsetStepLimitIntIterator implements IntIterator {
private int step, limit, curr, calls;
/**
* Creates a new {@link OffsetStepLimitIntIterator}.
*
* @param offset the starting value of the iterator.
* @param step the step of the iterator.
* @param limit the maximum number of allowed increments for the iterator (infinite if -1).
*/
public OffsetStepLimitIntIterator(final int offset, final int step, final int limit) {
this.step = step;
this.limit = limit;
curr = offset;
calls = 0;
}
/**
* Creates a new {@link OffsetStepLimitIntIterator} from a string.
* The accepted syntax is [offset[:[step][:limit]]]
, offset is assumed to be 0 if absent,
* step is assumed to be 1 if absent, limit is assumedd to be -1 if absent.
*
* @param str the string to parse to obtain the iterator.
*/
public OffsetStepLimitIntIterator(String str) {
curr = 0;
step = 1;
limit = -1;
calls = 0;
if (str.length() == 0) return;
int sc = str.indexOf(':');
if (sc < 0) sc = str.length();
if (sc > 0) curr = Integer.parseInt(str.substring(0, sc));
str = str.substring(sc + 1);
if (str.length() == 0) return;
sc = str.indexOf(':');
if (sc < 0) sc = str.length();
if (sc > 0) step = Integer.parseInt(str.substring(0, sc));
str = str.substring(sc + 1);
if (str.length() == 0) return;
sc = str.indexOf(':');
if (sc < 0) sc = str.length();
if (sc > 0) limit = Integer.parseInt(str.substring(0, sc));
}
@Override
public boolean hasNext() {
return (limit < 0) || (calls < limit);
}
@Override
public int nextInt() {
if (!hasNext()) throw new NoSuchElementException();
calls++;
curr = curr + step;
return curr - step;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy