Skip to content

Commit

Permalink
[GSCOLLECT-1581] Add binarySearch() to <Primitive>List. Fixes #20.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://gscollections.svn.services.gs.com/svnroot/gscollections-svn/trunk@865 d5c9223b-1aff-41ac-aadd-f810b4a99ac4
  • Loading branch information
Rebecca John committed Jun 26, 2015
1 parent f0a1183 commit 5c24d68
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 Goldman Sachs.
* Copyright 2015 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -764,6 +764,23 @@ public double median()
return (double) sortedArray[middleIndex];
}

public int binarySearch(int value)
{
if (this.step > 0 && this.from > value || this.step < 0 && this.from < value)
{
return -1;
}

if (this.step > 0 && this.to < value || this.step < 0 && this.to > value)
{
return -1 - this.size();
}

int diff = value - this.from;
int index = diff / this.step;
return diff % this.step == 0 ? index : (index + 2) * -1;
}

public int[] toSortedArray()
{
int[] array = this.toArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ arithmeticMethods ::= [
allMethods(name, type) ::=<<
<wideType.(type)> dotProduct(<name>List list);

int binarySearch(<type> value);

>>

noMethods(name, type) ::= ""
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ final class Immutable<name>ArrayList
return <name>ArrayList.newList(this).sortThis();
}

public int binarySearch(<type> value)
{
return Arrays.binarySearch(this.items, value);
}

public <type>[] toArray()
{
<type>[] newItems = new <type>[this.items.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ public Mutable<name>List toSortedList()
return new <name>ArrayList();
}

public int binarySearch(<type> value)
{
return -1;
}

public <wideType.(type)> dotProduct(<name>List list)
{
if (!list.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,19 @@ public Mutable<name>List toSortedList()
return <name>ArrayList.newListWith(this.element1);
}

public int binarySearch(<type> value)
{
if (<(equals.(type))("this.element1", "value")>)
{
return 0;
}
if (<(lessThan.(type))("this.element1", "value")>)
{
return -2;
}
return -1;
}

public <wideType.(type)> dotProduct(<name>List list)
{
if (list.size() != 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,11 @@ public class <name>ArrayList extends Abstract<name>Iterable
return <name>ArrayList.newList(this.asReversed());
}

public int binarySearch(<type> value)
{
return Arrays.binarySearch(this.items, 0, this.size, value);
}

public Mutable<name>List distinct()
{
<name>ArrayList target = new <name>ArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,19 @@ public final class Synchronized<name>List
@Override
public Immutable<name>List toImmutable()
{
int size = this.size();
if (size == 0)
{
return <name>Lists.immutable.with();
}
if (size == 1)
synchronized (this.getLock())
{
return <name>Lists.immutable.with(this.getFirst());
int size = this.size();
if (size == 0)
{
return <name>Lists.immutable.with();
}
if (size == 1)
{
return <name>Lists.immutable.with(this.getFirst());
}
return <name>Lists.immutable.with(this.toArray());
}
return <name>Lists.immutable.with(this.toArray());
}

public Mutable<name>List reverseThis()
Expand Down Expand Up @@ -350,9 +353,20 @@ public Mutable<name>List sortThis()
return this;
}

public int binarySearch(<type> value)
{
synchronized (this.getLock())
{
return this.getMutable<name>List().binarySearch(value);
}
}

public <wideType.(type)> dotProduct(<name>List list)
{
return this.getMutable<name>List().dotProduct(list);
synchronized (this.getLock())
{
return this.getMutable<name>List().dotProduct(list);
}
}

>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ public Mutable<name>List sortThis()
throw new UnsupportedOperationException("Cannot call sortThis() on " + this.getClass().getSimpleName());
}

public int binarySearch(<type> value)
{
return this.getMutable<name>List().binarySearch(value);
}

public <wideType.(type)> dotProduct(<name>List list)
{
return this.getMutable<name>List().dotProduct(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ public class Immutable<name>ArrayListTest extends AbstractImmutable<name>ListTes
Immutable<name>ArrayList list2 = Immutable<name>ArrayList.newListWith(<["1", "2"]:(literal.(type))(); separator=", ">);
list1.dotProduct(list2);
}

@Test
public void binarySearch()
{
Immutable<name>ArrayList list = Immutable<name>ArrayList.newListWith(<["2", "3", "5", "6", "9"]:(literal.(type))(); separator=", ">);
Assert.assertEquals(-1, list.binarySearch(<(literal.(type))("1")>));
Assert.assertEquals(0, list.binarySearch(<(literal.(type))("2")>));
Assert.assertEquals(1, list.binarySearch(<(literal.(type))("3")>));
Assert.assertEquals(-3, list.binarySearch(<(literal.(type))("4")>));
Assert.assertEquals(2, list.binarySearch(<(literal.(type))("5")>));
Assert.assertEquals(3, list.binarySearch(<(literal.(type))("6")>));
Assert.assertEquals(-5, list.binarySearch(<(literal.(type))("7")>));
Assert.assertEquals(-5, list.binarySearch(<(literal.(type))("8")>));
Assert.assertEquals(4, list.binarySearch(<(literal.(type))("9")>));
Assert.assertEquals(-6, list.binarySearch(<(literal.(type))("10")>));
}
}

>>
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ public class Immutable<name>EmptyListTest extends AbstractImmutable<name>ListTes

Assert.assertEquals(0, sum[0], <(literal.(type))("0")>);
}

@Test
public void binarySearch()
{
Assert.assertEquals(-1, this.classUnderTest().binarySearch(<(literal.(type))("7")>));
Assert.assertEquals(-1, this.classUnderTest().binarySearch(<(literal.(type))("0")>));
Assert.assertEquals(-1, this.classUnderTest().binarySearch(<(literal.(type))("100")>));
Assert.assertEquals(-1, this.classUnderTest().binarySearch(<(literal.(type))("-1")>));
}
}

>>
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public class Immutable<name>SingletonListTest extends AbstractImmutable<name>Lis

Assert.assertEquals(1, sum[0], <(literal.(type))("0")>);
}

@Test
public void binarySearch()
{
Assert.assertEquals(-1, this.classUnderTest().binarySearch(<(literal.(type))("0")>));
Assert.assertEquals(0, this.classUnderTest().binarySearch(<(literal.(type))("1")>));
Assert.assertEquals(-2, this.classUnderTest().binarySearch(<(literal.(type))("5")>));
}
}

>>
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,22 @@ public abstract class Abstract<name>ListTestCase extends AbstractMutable<name>Co
Assert.assertEquals(<(literal.(type))("1")>, list.get(0)<(wideDelta.(type))>);
}

@Test
public void binarySearch()
{
Mutable<name>List list = this.newWith(<["2", "3", "5", "6", "9"]:(literal.(type))(); separator=", ">);
Assert.assertEquals(-1, list.binarySearch(<(literal.(type))("1")>));
Assert.assertEquals(0, list.binarySearch(<(literal.(type))("2")>));
Assert.assertEquals(1, list.binarySearch(<(literal.(type))("3")>));
Assert.assertEquals(-3, list.binarySearch(<(literal.(type))("4")>));
Assert.assertEquals(2, list.binarySearch(<(literal.(type))("5")>));
Assert.assertEquals(3, list.binarySearch(<(literal.(type))("6")>));
Assert.assertEquals(-5, list.binarySearch(<(literal.(type))("7")>));
Assert.assertEquals(-5, list.binarySearch(<(literal.(type))("8")>));
Assert.assertEquals(4, list.binarySearch(<(literal.(type))("9")>));
Assert.assertEquals(-6, list.binarySearch(<(literal.(type))("10")>));
}

@Test
public void toReversed()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 Goldman Sachs.
* Copyright 2015 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -239,6 +239,84 @@ public void collect()
Assert.assertEquals(FastList.newListWith(0, 1, 2), this.intInterval.collect(parameter -> parameter - 1).toList());
}

@Test
public void binarySearch()
{
IntInterval interval1 = IntInterval.oneTo(3);
Assert.assertEquals(-1, interval1.binarySearch(-1));
Assert.assertEquals(-1, interval1.binarySearch(0));
Assert.assertEquals(0, interval1.binarySearch(1));
Assert.assertEquals(1, interval1.binarySearch(2));
Assert.assertEquals(2, interval1.binarySearch(3));
Assert.assertEquals(-4, interval1.binarySearch(4));
Assert.assertEquals(-4, interval1.binarySearch(5));

IntInterval interval2 = IntInterval.fromTo(7, 17).by(3);
Assert.assertEquals(0, interval2.binarySearch(7));
Assert.assertEquals(1, interval2.binarySearch(10));
Assert.assertEquals(2, interval2.binarySearch(13));
Assert.assertEquals(3, interval2.binarySearch(16));
Assert.assertEquals(-1, interval2.binarySearch(6));
Assert.assertEquals(-2, interval2.binarySearch(8));
Assert.assertEquals(-2, interval2.binarySearch(9));
Assert.assertEquals(-3, interval2.binarySearch(12));
Assert.assertEquals(-4, interval2.binarySearch(15));
Assert.assertEquals(-5, interval2.binarySearch(17));
Assert.assertEquals(-5, interval2.binarySearch(19));

IntInterval interval3 = IntInterval.fromTo(-21, -11).by(5);
Assert.assertEquals(-1, interval3.binarySearch(-22));
Assert.assertEquals(0, interval3.binarySearch(-21));
Assert.assertEquals(-2, interval3.binarySearch(-17));
Assert.assertEquals(1, interval3.binarySearch(-16));
Assert.assertEquals(-3, interval3.binarySearch(-15));
Assert.assertEquals(2, interval3.binarySearch(-11));
Assert.assertEquals(-4, interval3.binarySearch(-9));

IntInterval interval4 = IntInterval.fromTo(50, 30).by(-10);
Assert.assertEquals(-1, interval4.binarySearch(60));
Assert.assertEquals(0, interval4.binarySearch(50));
Assert.assertEquals(-2, interval4.binarySearch(45));
Assert.assertEquals(1, interval4.binarySearch(40));
Assert.assertEquals(-3, interval4.binarySearch(35));
Assert.assertEquals(2, interval4.binarySearch(30));
Assert.assertEquals(-4, interval4.binarySearch(25));

IntInterval interval5 = IntInterval.fromTo(-30, -50).by(-10);
Assert.assertEquals(-1, interval5.binarySearch(-20));
Assert.assertEquals(0, interval5.binarySearch(-30));
Assert.assertEquals(-2, interval5.binarySearch(-35));
Assert.assertEquals(1, interval5.binarySearch(-40));
Assert.assertEquals(-3, interval5.binarySearch(-47));
Assert.assertEquals(2, interval5.binarySearch(-50));
Assert.assertEquals(-4, interval5.binarySearch(-65));

IntInterval interval6 = IntInterval.fromTo(27, -30).by(-9);
Assert.assertEquals(-1, interval6.binarySearch(30));
Assert.assertEquals(0, interval6.binarySearch(27));
Assert.assertEquals(-2, interval6.binarySearch(20));
Assert.assertEquals(1, interval6.binarySearch(18));
Assert.assertEquals(-3, interval6.binarySearch(15));
Assert.assertEquals(2, interval6.binarySearch(9));
Assert.assertEquals(-4, interval6.binarySearch(2));
Assert.assertEquals(3, interval6.binarySearch(0));
Assert.assertEquals(-5, interval6.binarySearch(-7));
Assert.assertEquals(4, interval6.binarySearch(-9));
Assert.assertEquals(-6, interval6.binarySearch(-12));
Assert.assertEquals(5, interval6.binarySearch(-18));
Assert.assertEquals(-7, interval6.binarySearch(-23));
Assert.assertEquals(6, interval6.binarySearch(-27));
Assert.assertEquals(-8, interval6.binarySearch(-28));
Assert.assertEquals(-8, interval6.binarySearch(-30));

IntInterval interval7 = IntInterval.fromTo(-1, 1).by(1);
Assert.assertEquals(-1, interval7.binarySearch(-2));
Assert.assertEquals(0, interval7.binarySearch(-1));
Assert.assertEquals(1, interval7.binarySearch(0));
Assert.assertEquals(2, interval7.binarySearch(1));
Assert.assertEquals(-4, interval7.binarySearch(2));
}

@Test
public void max()
{
Expand Down

0 comments on commit 5c24d68

Please sign in to comment.