-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
9 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
test/it/unimi/dsi/fastutil/ints/Int2IntAVLTreeMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (C) 2017-2022 Sebastiano Vigna | ||
* | ||
* Licensed 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 it.unimi.dsi.fastutil.ints; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
import it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterator; | ||
|
||
public class Int2IntAVLTreeMapTest { | ||
|
||
@Test | ||
public void testIteratorJump() { | ||
final Int2IntSortedMap m = new Int2IntAVLTreeMap(); | ||
for (int i = 0; i < 100; i += 3) { | ||
m.put(i, i); | ||
} | ||
|
||
final IntBidirectionalIterator mapIt = m.keySet().iterator(50); | ||
|
||
assertTrue(mapIt.hasNext()); | ||
assertEquals(51, mapIt.nextInt()); | ||
|
||
mapIt.jump(50); | ||
assertTrue(mapIt.hasPrevious()); | ||
assertEquals(48, mapIt.previousInt()); | ||
|
||
mapIt.jump(-1); | ||
assertTrue(mapIt.hasNext()); | ||
assertFalse(mapIt.hasPrevious()); | ||
assertEquals(0, mapIt.nextInt()); | ||
|
||
mapIt.jump(100); | ||
assertFalse(mapIt.hasNext()); | ||
assertTrue(mapIt.hasPrevious()); | ||
assertEquals(99, mapIt.previousInt()); | ||
|
||
final ObjectBidirectionalIterator<Int2IntMap.Entry> mapEntryIt = m.int2IntEntrySet().iterator(new AbstractInt2IntMap.BasicEntry(50, 0)); | ||
|
||
assertTrue(mapEntryIt.hasNext()); | ||
assertEquals(51, mapEntryIt.next().getIntKey()); | ||
|
||
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(50, 0)); | ||
assertTrue(mapEntryIt.hasPrevious()); | ||
assertEquals(48, mapEntryIt.previous().getIntKey()); | ||
|
||
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(-1, 0)); | ||
assertTrue(mapEntryIt.hasNext()); | ||
assertFalse(mapEntryIt.hasPrevious()); | ||
assertEquals(0, mapEntryIt.next().getIntKey()); | ||
|
||
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(100, 0)); | ||
assertFalse(mapEntryIt.hasNext()); | ||
assertTrue(mapEntryIt.hasPrevious()); | ||
assertEquals(99, mapEntryIt.previous().getIntKey()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (C) 2017-2022 Sebastiano Vigna | ||
* | ||
* Licensed 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 it.unimi.dsi.fastutil.ints; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
import it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterator; | ||
|
||
public class Int2IntRBTreeMapTest { | ||
|
||
@Test | ||
public void testIteratorJump() { | ||
final Int2IntSortedMap m = new Int2IntRBTreeMap(); | ||
for (int i = 0; i < 100; i += 3) { | ||
m.put(i, i); | ||
} | ||
|
||
final IntBidirectionalIterator mapKeyIt = m.keySet().iterator(50); | ||
|
||
assertTrue(mapKeyIt.hasNext()); | ||
assertEquals(51, mapKeyIt.nextInt()); | ||
|
||
mapKeyIt.jump(50); | ||
assertTrue(mapKeyIt.hasPrevious()); | ||
assertEquals(48, mapKeyIt.previousInt()); | ||
|
||
mapKeyIt.jump(-1); | ||
assertTrue(mapKeyIt.hasNext()); | ||
assertFalse(mapKeyIt.hasPrevious()); | ||
assertEquals(0, mapKeyIt.nextInt()); | ||
|
||
mapKeyIt.jump(100); | ||
assertFalse(mapKeyIt.hasNext()); | ||
assertTrue(mapKeyIt.hasPrevious()); | ||
assertEquals(99, mapKeyIt.previousInt()); | ||
|
||
final ObjectBidirectionalIterator<Int2IntMap.Entry> mapEntryIt = m.int2IntEntrySet().iterator(new AbstractInt2IntMap.BasicEntry(50, 0)); | ||
|
||
assertTrue(mapEntryIt.hasNext()); | ||
assertEquals(51, mapEntryIt.next().getIntKey()); | ||
|
||
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(50, 0)); | ||
assertTrue(mapEntryIt.hasPrevious()); | ||
assertEquals(48, mapEntryIt.previous().getIntKey()); | ||
|
||
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(-1, 0)); | ||
assertTrue(mapEntryIt.hasNext()); | ||
assertFalse(mapEntryIt.hasPrevious()); | ||
assertEquals(0, mapEntryIt.next().getIntKey()); | ||
|
||
mapEntryIt.jump(new AbstractInt2IntMap.BasicEntry(100, 0)); | ||
assertFalse(mapEntryIt.hasNext()); | ||
assertTrue(mapEntryIt.hasPrevious()); | ||
assertEquals(99, mapEntryIt.previous().getIntKey()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.