Skip to content

Commit

Permalink
Optimize UnifiedMap.getFirst() to not delegate to an iterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
motlin committed Feb 15, 2016
1 parent 4b63ee4 commit 09b97ee
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Goldman Sachs.
* Copyright (c) 2016 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -995,6 +995,25 @@ else if (cur != null)
}
}

@Override
public V getFirst()
{
for (int i = 0; i < this.table.length; i += 2)
{
Object cur = this.table[i];
if (cur == CHAINED_KEY)
{
Object[] chain = (Object[]) this.table[i + 1];
return (V) chain[1];
}
if (cur != null)
{
return (V) this.table[i + 1];
}
}
return null;
}

public <E> MutableMap<K, V> collectKeysAndValues(
Iterable<E> iterable,
Function<? super E, ? extends K> keyFunction,
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Goldman Sachs.
* Copyright (c) 2016 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -1037,6 +1037,25 @@ else if (cur != null)
}
}

@Override
public V getFirst()
{
for (int i = 0; i < this.table.length; i += 2)
{
Object cur = this.table[i];
if (cur == CHAINED_KEY)
{
Object[] chain = (Object[]) this.table[i + 1];
return (V) chain[1];
}
if (cur != null)
{
return (V) this.table[i + 1];
}
}
return null;
}

public <E> MutableMap<K, V> collectKeysAndValues(
Iterable<E> iterable,
Function<? super E, ? extends K> keyFunction,
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Goldman Sachs.
* Copyright (c) 2016 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -57,8 +57,8 @@ public abstract class UnifiedMapTestCase extends MutableMapTestCase
protected static final MutableList<Integer> MORE_COLLISIONS = FastList.newList(COLLISIONS)
.with(COLLISION_6, COLLISION_7, COLLISION_8, COLLISION_9);
protected static final String[] FREQUENT_COLLISIONS = {"\u9103\ufffe", "\u9104\uffdf",
"\u9105\uffc0", "\u9106\uffa1", "\u9107\uff82", "\u9108\uff63", "\u9109\uff44",
"\u910a\uff25", "\u910b\uff06", "\u910c\ufee7"};
"\u9105\uffc0", "\u9106\uffa1", "\u9107\uff82", "\u9108\uff63", "\u9109\uff44",
"\u910a\uff25", "\u910b\uff06", "\u910c\ufee7"};

@Test
public void valuesCollection_toArray()
Expand Down Expand Up @@ -852,6 +852,16 @@ public void frequentCollision()
Assert.assertArrayEquals(expected, map.keysView().toArray());
}

@Override
public void getFirst()
{
super.getFirst();

MutableMap<String, String> map = this.newMap();
map.collectKeysAndValues(Arrays.asList(FREQUENT_COLLISIONS), Functions.identity(), Functions.identity());
Assert.assertEquals(FREQUENT_COLLISIONS[0], map.getFirst());
}

private static final class NoInstanceOfInEquals
{
private final int value;
Expand Down

0 comments on commit 09b97ee

Please sign in to comment.