Skip to content

Commit cd3b850

Browse files
committed
Introduced jump inside iterator
This is a memory optimization for the case when a user required to modify iterator by moving it to a desire position. This works the same way as `iteraotr(fromElement)` but doesn't create a new iterator that decreases memory footprint at an algorithms which makes a lot of jumps.
1 parent 7944e1d commit cd3b850

File tree

9 files changed

+354
-0
lines changed

9 files changed

+354
-0
lines changed

drv/AVLTreeMap.drv

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,16 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC extends ABSTRACT_SORTED_MAP KEY_VAL
11851185
while(i-- != 0 && hasPrevious()) previousEntry();
11861186
return n - i - 1;
11871187
}
1188+
1189+
public void jumpKey(final KEY_GENERIC_TYPE fromElement) {
1190+
if ((next = locateKey(fromElement)) != null) {
1191+
if (compare(next.key, fromElement) <= 0) {
1192+
prev = next;
1193+
next = next.next();
1194+
}
1195+
else prev = next.prev();
1196+
}
1197+
}
11881198
}
11891199

11901200

@@ -1211,6 +1221,8 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC extends ABSTRACT_SORTED_MAP KEY_VAL
12111221

12121222
@Override
12131223
public void add(MAP.Entry KEY_VALUE_GENERIC ok) { throw new UnsupportedOperationException(); }
1224+
1225+
public void jump(final MAP.Entry KEY_VALUE_GENERIC fromElement) { jumpKey(fromElement.ENTRY_GET_KEY()); }
12141226
}
12151227

12161228

@@ -1304,6 +1316,9 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC extends ABSTRACT_SORTED_MAP KEY_VAL
13041316
public KEY_GENERIC_TYPE NEXT_KEY() { return nextEntry().key; }
13051317
@Override
13061318
public KEY_GENERIC_TYPE PREV_KEY() { return previousEntry().key; }
1319+
1320+
@Override
1321+
public void jump(final KEY_GENERIC_TYPE fromElement) { jumpKey(fromElement); }
13071322
}
13081323

13091324
/** A keyset implementation using a more direct implementation for iterators. */

drv/AVLTreeSet.drv

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,17 @@ public class AVL_TREE_SET KEY_GENERIC extends ABSTRACT_SORTED_SET KEY_GENERIC im
11271127
AVL_TREE_SET.this.remove(curr.key);
11281128
curr = null;
11291129
}
1130+
1131+
@Override
1132+
public void jump(final KEY_GENERIC_TYPE fromElement) {
1133+
if ((next = locateKey(fromElement)) != null) {
1134+
if (compare(next.key, fromElement) <= 0) {
1135+
prev = next;
1136+
next = next.next();
1137+
}
1138+
else prev = next.prev();
1139+
}
1140+
}
11301141
}
11311142

11321143
@Override
@@ -1360,6 +1371,25 @@ public class AVL_TREE_SET KEY_GENERIC extends ABSTRACT_SORTED_SET KEY_GENERIC im
13601371
next = next.next();
13611372
if (! top && next != null && AVL_TREE_SET.this.compare(next.key, to) >= 0) next = null;
13621373
}
1374+
1375+
@Override
1376+
public void jump(final KEY_GENERIC_TYPE fromElement) {
1377+
next = firstEntry();
1378+
1379+
if (next != null) {
1380+
if (! bottom && compare(fromElement, next.key) < 0) prev = null;
1381+
else if (! top && compare(fromElement, (prev = lastEntry()).key) >= 0) next = null;
1382+
else {
1383+
next = locateKey(fromElement);
1384+
1385+
if (compare(next.key, fromElement) <= 0) {
1386+
prev = next;
1387+
next = next.next();
1388+
}
1389+
else prev = next.prev();
1390+
}
1391+
}
1392+
}
13631393
}
13641394
}
13651395

drv/Iterator.drv

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,27 @@ public interface KEY_ITERATOR KEY_GENERIC extends Iterator<KEY_GENERIC_CLASS> {
153153
while(i-- != 0 && hasNext()) NEXT_KEY();
154154
return n - i - 1;
155155
}
156+
157+
/** Moves back or forward to the elements in this set,
158+
* starting from a given element of the domain.
159+
*
160+
* <p>This method moves an iterator to the specified starting point.
161+
* The starting point is any element comparable to the elements of this set
162+
* (even if it does not actually belong to the set).
163+
* The next element of the returned iterator is the least element of
164+
* the set that is greater than the starting point (if there are no
165+
* elements greater than the starting point, {@link
166+
* it.unimi.dsi.fastutil.BidirectionalIterator#hasNext() hasNext()} will return
167+
* {@code false}). The previous element of the returned iterator is
168+
* the greatest element of the set that is smaller than or equal to the
169+
* starting point (if there are no elements smaller than or equal to the
170+
* starting point, {@link it.unimi.dsi.fastutil.BidirectionalIterator#hasPrevious()
171+
* hasPrevious()} will return {@code false}).
172+
*
173+
* @param fromElement an element to start from.
174+
* @throws UnsupportedOperationException if this set does not support jump at iterator.
175+
*/
176+
default void jump(final KEY_GENERIC_TYPE fromElement) {
177+
throw new UnsupportedOperationException();
178+
}
156179
}

drv/RBTreeMap.drv

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,16 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC extends ABSTRACT_SORTED_MAP KEY_VALUE
11161116
while(i-- != 0 && hasPrevious()) previousEntry();
11171117
return n - i - 1;
11181118
}
1119+
1120+
public void jumpKey(final KEY_GENERIC_TYPE fromElement) {
1121+
if ((next = locateKey(fromElement)) != null) {
1122+
if (compare(next.key, fromElement) <= 0) {
1123+
prev = next;
1124+
next = next.next();
1125+
}
1126+
else prev = next.prev();
1127+
}
1128+
}
11191129
}
11201130

11211131

@@ -1135,6 +1145,9 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC extends ABSTRACT_SORTED_MAP KEY_VALUE
11351145
public MAP.Entry KEY_VALUE_GENERIC next() { return nextEntry(); }
11361146
@Override
11371147
public MAP.Entry KEY_VALUE_GENERIC previous() { return previousEntry(); }
1148+
1149+
@Override
1150+
public void jump(final MAP.Entry KEY_VALUE_GENERIC fromElement) { jumpKey(fromElement.ENTRY_GET_KEY()); }
11381151
}
11391152

11401153

@@ -1231,6 +1244,9 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC extends ABSTRACT_SORTED_MAP KEY_VALUE
12311244

12321245
@Override
12331246
public KEY_GENERIC_TYPE PREV_KEY() { return previousEntry().key; }
1247+
1248+
@Override
1249+
public void jump(final KEY_GENERIC_TYPE fromElement) { jumpKey(fromElement); }
12341250
};
12351251

12361252

drv/RBTreeSet.drv

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,17 @@ public class RB_TREE_SET KEY_GENERIC extends ABSTRACT_SORTED_SET KEY_GENERIC imp
10551055
RB_TREE_SET.this.remove(curr.key);
10561056
curr = null;
10571057
}
1058+
1059+
@Override
1060+
public void jump(final KEY_GENERIC_TYPE fromElement) {
1061+
if ((next = locateKey(fromElement)) != null) {
1062+
if (compare(next.key, fromElement) <= 0) {
1063+
prev = next;
1064+
next = next.next();
1065+
}
1066+
else prev = next.prev();
1067+
}
1068+
}
10581069
}
10591070

10601071
@Override
@@ -1299,6 +1310,25 @@ public class RB_TREE_SET KEY_GENERIC extends ABSTRACT_SORTED_SET KEY_GENERIC imp
12991310
next = next.next();
13001311
if (! top && next != null && RB_TREE_SET.this.compare(next.key, to) >= 0) next = null;
13011312
}
1313+
1314+
@Override
1315+
public void jump(final KEY_GENERIC_TYPE fromElement) {
1316+
next = firstEntry();
1317+
1318+
if (next != null) {
1319+
if (! bottom && compare(fromElement, next.key) < 0) prev = null;
1320+
else if (! top && compare(fromElement, (prev = lastEntry()).key) >= 0) next = null;
1321+
else {
1322+
next = locateKey(fromElement);
1323+
1324+
if (compare(next.key, fromElement) <= 0) {
1325+
prev = next;
1326+
next = next.next();
1327+
}
1328+
else prev = next.prev();
1329+
}
1330+
}
1331+
}
13021332
}
13031333
}
13041334

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (C) 2017-2022 Sebastiano Vigna
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package it.unimi.dsi.fastutil.ints;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import org.junit.Test;
24+
25+
import it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterator;
26+
27+
public class Int2IntAVLTreeMapTest {
28+
29+
@Test
30+
public void testIteratorJump() {
31+
final Int2IntSortedMap m = new Int2IntAVLTreeMap();
32+
for (int i = 0; i < 100; i += 3) {
33+
m.put(i, i);
34+
}
35+
36+
final IntBidirectionalIterator mapIt = m.keySet().iterator(50);
37+
38+
assertTrue(mapIt.hasNext());
39+
assertEquals(51, mapIt.nextInt());
40+
41+
mapIt.jump(50);
42+
assertTrue(mapIt.hasPrevious());
43+
assertEquals(48, mapIt.previousInt());
44+
45+
mapIt.jump(-1);
46+
assertTrue(mapIt.hasNext());
47+
assertFalse(mapIt.hasPrevious());
48+
assertEquals(0, mapIt.nextInt());
49+
50+
mapIt.jump(100);
51+
assertFalse(mapIt.hasNext());
52+
assertTrue(mapIt.hasPrevious());
53+
assertEquals(99, mapIt.previousInt());
54+
55+
final ObjectBidirectionalIterator<Int2IntMap.Entry> mapEntryIt = m.int2IntEntrySet().iterator(new AbstractInt2IntMap.BasicEntry(50, 0));
56+
57+
assertTrue(mapEntryIt.hasNext());
58+
assertEquals(51, mapEntryIt.next().getIntKey());
59+
60+
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(50, 0));
61+
assertTrue(mapEntryIt.hasPrevious());
62+
assertEquals(48, mapEntryIt.previous().getIntKey());
63+
64+
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(-1, 0));
65+
assertTrue(mapEntryIt.hasNext());
66+
assertFalse(mapEntryIt.hasPrevious());
67+
assertEquals(0, mapEntryIt.next().getIntKey());
68+
69+
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(100, 0));
70+
assertFalse(mapEntryIt.hasNext());
71+
assertTrue(mapEntryIt.hasPrevious());
72+
assertEquals(99, mapEntryIt.previous().getIntKey());
73+
}
74+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (C) 2017-2022 Sebastiano Vigna
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package it.unimi.dsi.fastutil.ints;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import org.junit.Test;
24+
25+
import it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterator;
26+
27+
public class Int2IntRBTreeMapTest {
28+
29+
@Test
30+
public void testIteratorJump() {
31+
final Int2IntSortedMap m = new Int2IntRBTreeMap();
32+
for (int i = 0; i < 100; i += 3) {
33+
m.put(i, i);
34+
}
35+
36+
final IntBidirectionalIterator mapKeyIt = m.keySet().iterator(50);
37+
38+
assertTrue(mapKeyIt.hasNext());
39+
assertEquals(51, mapKeyIt.nextInt());
40+
41+
mapKeyIt.jump(50);
42+
assertTrue(mapKeyIt.hasPrevious());
43+
assertEquals(48, mapKeyIt.previousInt());
44+
45+
mapKeyIt.jump(-1);
46+
assertTrue(mapKeyIt.hasNext());
47+
assertFalse(mapKeyIt.hasPrevious());
48+
assertEquals(0, mapKeyIt.nextInt());
49+
50+
mapKeyIt.jump(100);
51+
assertFalse(mapKeyIt.hasNext());
52+
assertTrue(mapKeyIt.hasPrevious());
53+
assertEquals(99, mapKeyIt.previousInt());
54+
55+
final ObjectBidirectionalIterator<Int2IntMap.Entry> mapEntryIt = m.int2IntEntrySet().iterator(new AbstractInt2IntMap.BasicEntry(50, 0));
56+
57+
assertTrue(mapEntryIt.hasNext());
58+
assertEquals(51, mapEntryIt.next().getIntKey());
59+
60+
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(50, 0));
61+
assertTrue(mapEntryIt.hasPrevious());
62+
assertEquals(48, mapEntryIt.previous().getIntKey());
63+
64+
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(-1, 0));
65+
assertTrue(mapEntryIt.hasNext());
66+
assertFalse(mapEntryIt.hasPrevious());
67+
assertEquals(0, mapEntryIt.next().getIntKey());
68+
69+
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(100, 0));
70+
assertFalse(mapEntryIt.hasNext());
71+
assertTrue(mapEntryIt.hasPrevious());
72+
assertEquals(99, mapEntryIt.previous().getIntKey());
73+
}
74+
}

test/it/unimi/dsi/fastutil/ints/IntAVLTreeSetTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,50 @@ public void testGet() {
4141
public void testLegacyMainMethodTests() throws Exception {
4242
MainRunner.callMainIfExists(IntAVLTreeSet.class, "test", /*num=*/"20", /*seed=*/"423429");
4343
}
44+
45+
@Test
46+
public void testIteratorJump() {
47+
final IntSortedSet s = new IntAVLTreeSet();
48+
for (int i = 0; i < 100; i += 3) {
49+
s.add(i);
50+
}
51+
52+
final IntBidirectionalIterator it = s.iterator(50);
53+
54+
assertTrue(it.hasNext());
55+
assertEquals(51, it.nextInt());
56+
57+
it.jump(50);
58+
assertTrue(it.hasPrevious());
59+
assertEquals(48, it.previousInt());
60+
61+
it.jump(-1);
62+
assertTrue(it.hasNext());
63+
assertFalse(it.hasPrevious());
64+
assertEquals(0, it.nextInt());
65+
66+
it.jump(100);
67+
assertFalse(it.hasNext());
68+
assertTrue(it.hasPrevious());
69+
assertEquals(99, it.previousInt());
70+
71+
final IntBidirectionalIterator subIt = s.subSet(10, 90).iterator(50);
72+
73+
assertTrue(subIt.hasNext());
74+
assertEquals(51, subIt.nextInt());
75+
76+
subIt.jump(50);
77+
assertTrue(subIt.hasPrevious());
78+
assertEquals(48, subIt.previousInt());
79+
80+
subIt.jump(-1);
81+
assertTrue(subIt.hasNext());
82+
assertFalse(subIt.hasPrevious());
83+
assertEquals(12, subIt.nextInt());
84+
85+
subIt.jump(90);
86+
assertFalse(subIt.hasNext());
87+
assertTrue(subIt.hasPrevious());
88+
assertEquals(87, subIt.previousInt());
89+
}
4490
}

0 commit comments

Comments
 (0)