blob: 4adb9d79be621c7c23af1e743eb439b968d2c947 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.datasketches.kll;
import org.apache.datasketches.quantilescommon.QuantilesGenericSketchIterator;
/**
* Iterator over KllItemsSketch. The order is not defined.
*/
public final class KllItemsSketchIterator<T> implements QuantilesGenericSketchIterator<T> {
private final Object[] quantiles;
private final int[] levelsArr;
private final int numLevels;
private int level;
private int index;
private long weight;
private boolean isInitialized_;
KllItemsSketchIterator(final Object[] quantiles, final int[] levelsArr, final int numLevels) {
this.quantiles = quantiles;
this.levelsArr = levelsArr;
this.numLevels = numLevels;
this.isInitialized_ = false;
}
@SuppressWarnings("unchecked")
@Override
public T getQuantile() {
return (T)quantiles[index];
}
@Override
public long getWeight() {
return weight;
}
@Override
public boolean next() {
if (!isInitialized_) {
level = 0;
index = levelsArr[level];
weight = 1;
isInitialized_ = true;
} else {
index++;
}
if (index < levelsArr[level + 1]) {
return true;
}
// go to the next non-empty level
do {
level++;
if (level == numLevels) {
return false; // run out of levels
}
weight *= 2;
} while (levelsArr[level] == levelsArr[level + 1]);
index = levelsArr[level];
return true;
}
}