• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / datasketches-java / #306

30 Apr 2024 10:01PM UTC coverage: 97.645% (-0.5%) from 98.139%
#306

push

web-flow
Merge pull request #555 from apache/fix_pom_xml_header

Fix pom xml header

26865 of 27513 relevant lines covered (97.64%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/src/main/java/org/apache/datasketches/quantilescommon/SortedViewIterator.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * 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,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.datasketches.quantilescommon;
21

22
import static org.apache.datasketches.quantilescommon.QuantileSearchCriteria.INCLUSIVE;
23

24
/**
25
 * This is the base interface for the SortedViewIterator hierarchy used with a SortedView obtained
26
 * from a quantile-type sketch. This provides an ordered iterator over the retained quantiles of
27
 * the associated quantile-type sketch.
28
 *
29
 * <p>Prototype example of the recommended iteration loop:</p>
30
 * <pre>{@code
31
 *   SortedViewIterator itr = sketch.getSortedView().iterator();
32
 *   while (itr.next()) {
33
 *     long weight = itr.getWeight();
34
 *     ...
35
 *   }
36
 * }</pre>
37
 * @author Alexander Saydakov
38
 * @author Lee Rhodes
39
 */
40
public class SortedViewIterator {
41
  protected final long[] cumWeights;
42
  protected long totalN;
43
  protected int index;
44

45
  SortedViewIterator(final long[] cumWeights) {
1✔
46
    this.cumWeights = cumWeights; //SpotBugs EI_EXPOSE_REP2 suppressed by FindBugsExcludeFilter
1✔
47
    this.totalN = (cumWeights.length > 0) ? cumWeights[cumWeights.length - 1] : 0;
1✔
48
    index = -1;
1✔
49
  }
1✔
50

51
  /**
52
   * Gets the natural rank at the current index.
53
   * This is equivalent to <i>getNaturalRank(INCLUSIVE)</i>.
54
   *
55
   * <p>Don't call this before calling next() for the first time or after getting false from next().</p>
56
   *
57
   * @return the natural rank at the current index.
58
   */
59
  public long getNaturalRank() {
60
    return cumWeights[index];
×
61
  }
62

63
  /**
64
   * Gets the natural rank at the current index (or previous index) based on the chosen search criterion.
65
   * This is also referred to as the "cumulative weight". The natural rank is a number in the range <i>[1, N]</i>,
66
   * where <i>N</i> ({@link #getN()}) is the total number of items fed to the sketch.
67
   *
68
   * <p>Don't call this before calling next() for the first time or after getting false from next().</p>
69
   *
70
   * @param searchCrit if INCLUSIVE, includes the weight of the item at the current index in the computation of
71
   * the natural rank.
72
   * Otherwise, it will return the natural rank of the previous index.
73
   *
74
   * @return the natural rank at the current index (or previous index) based on the chosen search criterion.
75
   */
76
  public long getNaturalRank(final QuantileSearchCriteria searchCrit) {
77
    if (searchCrit == INCLUSIVE) { return cumWeights[index]; }
1✔
78
    return (index == 0) ? 0 : cumWeights[index - 1];
1✔
79
  }
80

81
  /**
82
   * Gets the total count of all items presented to the sketch.
83
   * @return the total count of all items presented to the sketch.
84
   */
85
  public long getN() {
86
    return totalN;
×
87
  }
88

89
  /**
90
   * Gets the normalized rank at the current index.
91
   * This is equivalent to <i>getNormalizedRank(INCLUSIVE)</i>.
92
   *
93
   * <p>Don't call this before calling next() for the first time or after getting false from next().</p>
94
   *
95
   * @return the normalized rank at the current index
96
   */
97
  public double getNormalizedRank() {
98
    return (double) getNaturalRank() / totalN;
×
99
  }
100

101
  /**
102
   * Gets the normalized rank at the current index (or previous index)
103
   * based on the chosen search criterion. Where <i>normalized rank = natural rank / N</i> ({@link #getN()})
104
   * and is a fraction in the range (0,1.0].
105
   *
106
   * <p>Don't call this before calling next() for the first time or after getting false from next().</p>
107
   *
108
   * @param searchCrit if INCLUSIVE, includes the normalized rank at the current index.
109
   * Otherwise, returns the normalized rank of the previous index.
110
   *
111
   * @return the normalized rank at the current index (or previous index)
112
   * based on the chosen search criterion.
113
   */
114
  public double getNormalizedRank(final QuantileSearchCriteria searchCrit) {
115
    return (double) getNaturalRank(searchCrit) / totalN;
1✔
116
  }
117

118
  /**
119
   * Gets the weight contribution of the item at the current index.
120
   *
121
   * <p>Don't call this before calling next() for the first time or after getting false from next().</p>
122
   *
123
   * @return the weight contribution of the item at the current index.
124
   */
125
  public long getWeight() {
126
    if (index == 0) { return cumWeights[0]; }
1✔
127
    return cumWeights[index] - cumWeights[index - 1];
1✔
128
  }
129

130
  /**
131
   * Advances the index and checks if it is valid.
132
   * The state of this iterator is undefined before the first call of this method.
133
   * @return true if the next index is valid.
134
   */
135
  public boolean next() {
136
    index++;
1✔
137
    return index < cumWeights.length;
1✔
138
  }
139

140
}
141

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc