1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.numbers.examples.jmh.arrays;
18
19 /**
20 * An iterator of indices used for partitioning an array into multiple regions.
21 *
22 * <p>The iterator provides the functionality to iterate over blocks of indices
23 * defined by an inclusive interval {@code [left, right]}:
24 *
25 * <pre>
26 * l----r
27 * l----r
28 * lr
29 * lr
30 * l----------------r
31 * </pre>
32 *
33 * @since 1.2
34 */
35 interface IndexIterator {
36 /**
37 * The start (inclusive) of the current block of indices.
38 *
39 * @return start index
40 */
41 int left();
42
43 /**
44 * The end (inclusive) of the current block of indices.
45 *
46 * @return end index
47 */
48 int right();
49
50 /**
51 * The end index.
52 *
53 * @return the end index
54 */
55 int end();
56
57 /**
58 * Advance the iterator to the next block of indices.
59 *
60 * <p>If there are no more indices the result of {@link #left()} and
61 * {@link #right()} is undefined.
62 *
63 * @return true if the iterator was advanced
64 */
65 boolean next();
66
67 /**
68 * Advance the iterator so that {@code right > index}.
69 *
70 * <p>If there are no more indices the result of {@link #left()} and
71 * {@link #right()} is undefined.
72 *
73 * <p>The default implementation is:
74 * <pre>{@code
75 * while (right() <= index) {
76 * if (!next()) {
77 * return false;
78 * }
79 * }
80 * return false;
81 * }</pre>
82 *
83 * <p>Implementations may choose to set {@code left = index + 1} if the iterator
84 * range spans the {@code index}; all indices before {@code index} are no
85 * longer available for iteration.
86 *
87 * @param index Index.
88 * @return true if {@code right > index}
89 */
90 default boolean positionAfter(int index) {
91 while (right() <= index) {
92 if (!next()) {
93 return false;
94 }
95 }
96 return true;
97 }
98
99 /**
100 * Return true if the start of the next block of indices is after the specified {@code index}.
101 * A partition algorithm can use this to decide how to process the current block.
102 *
103 * <p>The default implementation is only true if there is no next index:
104 * <pre>{@code
105 * return right() >= end();
106 * }</pre>
107 *
108 * @param index Index.
109 * @return true if the next {@code left > index}, or there is no next left
110 */
111 default boolean nextAfter(int index) {
112 return right() >= end();
113 }
114 }